YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Image.h
浏览该文件的文档.
1 /*
2  © 2013-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_Adaptor_Image_h_
29 #define YSL_INC_Adaptor_Image_h_ 1
30 
31 #include "../Core/YModules.h"
32 #include YFM_YSLib_Core_YGDIBase
33 #include YFM_YSLib_Adaptor_YContainer
34 
35 //包含 FreeImage 。
36 //#include <FreeImage.h>
37 
39 struct FIBITMAP;
41 struct FIMEMORY;
42 
43 namespace YSLib
44 {
45 
46 namespace Drawing
47 {
48 
50 class CompactPixmap;
52 class HBitmap;
53 
55 using BitPerPixel = u8;
56 
57 
64 enum class ImageFormat : int
65 {
66  Unknown = -1,
67  BMP = 0,
68  ICO = 1,
69  JPEG = 2,
71  PNG = 13,
72  GIF = 25
73 };
74 
75 
81 enum class ImageDecoderFlags : int
82 {
83  Default = 0,
84  GIF_Load256 = 1,
85  GIF_Playback = 2,
86  ICO_MakeAlpha = 1,
87  JPEG_Fast = 0x0001,
88  JPEG_Accurate = 0x0002,
89  JPEG_CMYK = 0x0004,
90  JPEG_EXIFRotate = 0x0008,
91  JPEG_GreyScale = 0x0010,
92  JPEG_QualitySuperb = 0x80,
93  JPEG_QualityGood = 0x0100,
94  JPEG_QualityNormal = 0x0200,
95  JPEG_QualityAverage = 0x0400,
96  JPEG_QualityBad = 0x0800,
97  JPEG_Progressive = 0x2000,
98  JPEG_Subsampling_411 = 0x1000,
99  JPEG_Subsampling_420 = 0x4000,
100  JPEG_Subsampling_422 = 0x8000,
101  JPEG_Subsampling_444 = 0x10000,
102  JPEG_Optimize = 0x20000,
103  JPEG_Baseline = 0x40000
104 };
105 
111 
112 
113 
119 enum class SamplingFilter
120 {
121  Box = 0,
122  Bicubic = 1,
123  Bilinear = 2,
124  BSpline = 3,
125  CatmullRom = 4,
126  Lanczos3 = 5
127 };
128 
129 
134 class YF_API BadImageAlloc : public std::bad_alloc
135 {};
136 
137 
143  : public LoggedEvent, public ystdex::unsupported
144 {
145 public:
147  : LoggedEvent(str), unsupported()
148  {}
149 };
150 
151 
153 
154 
158 {
159 public:
162  {}
163 };
164 
165 
170 class YF_API ImageMemory final
171 {
172 public:
173  using NativeHandle = ::FIMEMORY*;
175  using Buffer = vector<octet>;
176 
177 private:
181 
182 public:
196  explicit
210  template<typename _fCallable>
211  ImageMemory(_fCallable f)
212  : ImageMemory(f())
213  {}
215  template<typename _fCallable>
216  ImageMemory(_fCallable f, ImageFormat fmt)
217  : ImageMemory(f(), fmt)
218  {}
222  ~ImageMemory();
224 
226  DefGetter(const ynothrow, const Buffer&, Buffer, buffer)
228  DefGetter(const ynothrow, NativeHandle, NativeHandle, handle)
229 };
231 
232 
238 class YF_API HBitmap final
239 {
240 public:
241  using DataPtr = ::FIBITMAP*;
242 
243 private:
245 
246 public:
247  /*
248  \brief 构造:使用现有数据指针。
249  \note 取得所有权。
250  \since build 430
251  */
252  HBitmap(DataPtr ptr = {}) ynothrow
253  : bitmap(ptr)
254  {}
256  HBitmap(const Size&, BitPerPixel = 0);
265  explicit
266  HBitmap(BitmapPtr, const Size&, size_t = 0);
272  HBitmap(const CompactPixmap&);
278  /*
279  \brief 构造:使用指定 UTF-8 文件名和解码器标识。
280  \throw UnknownImageFormat 未知图像格式。
281  */
284  HBitmap(const char*, ImageFormat,
292  HBitmap(const char16_t*, ImageFormat,
299  template<class _tString,
301  HBitmap(const _tString& filename,
303  : HBitmap(&filename[0])
304  {}
309  template<class _tString,
311  HBitmap(const _tString& filename, ImageFormat fmt,
313  : HBitmap(&filename[0], fmt)
314  {}
316 
328  HBitmap(const HBitmap&, BitPerPixel);
330 
331 
336  HBitmap(const HBitmap&, const Size&, SamplingFilter);
338  HBitmap(const HBitmap&);
339  HBitmap(HBitmap&&) ynothrow;
341  ~HBitmap();
343 
345 
346  HBitmap&
348  operator=(HBitmap pixmap) ynothrow
349  {
350  pixmap.swap(*this);
351  return *this;
352  }
353 
354  PDefHOp(bool, !, ) const ynothrow
355  ImplRet(!bitmap)
356 
365  byte*
366  operator[](size_t) const ynothrow;
367 
368  explicit DefCvt(const ynothrow, bool, bitmap)
370 
375  operator CompactPixmap() const;
376 
378  GetBPP() const ynothrow;
380  DefGetter(const ynothrow, DataPtr, DataPtr, bitmap)
381  SDst
382  GetHeight() const ynothrow;
384  DefGetter(const ynothrow, Size, Size, {GetWidth(), GetHeight()});
386  SDst
387  GetPitch() const ynothrow;
394  byte*
395  GetPixels() const ynothrow;
404  PDefH(byte*, GetScanLine, size_t idx) const ynothrow
405  ImplRet(bitmap ? (*this)[idx] : nullptr)
406  SDst
407  GetWidth() const ynothrow;
408 
413  void
414  Rescale(const Size&, SamplingFilter = SamplingFilter::Box);
415 
421  bool
423  SaveTo(const char*, ImageFormat = ImageFormat::BMP,
424  ImageDecoderFlags = ImageDecoderFlags::Default) const ynothrow;
426  bool
427  SaveTo(const char16_t*, ImageFormat = ImageFormat::BMP,
428  ImageDecoderFlags = ImageDecoderFlags::Default) const ynothrow;
433  template<class _tString,
434  yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
435  bool
436  SaveTo(const _tString& filename, ImageFormat fmt = ImageFormat::BMP,
438  {
439  return SaveTo(&filename[0], fmt, flags);
440  }
442 
443  /*
444  \brief 交换。
445  \since build 430
446  */
447  PDefH(void, swap, HBitmap& pixmap) ynothrow
448  ImplExpr(std::swap(bitmap, pixmap.bitmap))
449 };
450 
455 inline DefSwap(ynothrow, HBitmap)
456 
457 
458 
460 
464 class MultiBitmapData;
465 
471 class YF_API HMultiBitmap final
472 {
473 public:
474  using DataPtr = shared_ptr<MultiBitmapData>;
476 
477  class YF_API iterator : public std::iterator<std::input_iterator_tag,
478  HBitmap, ptrdiff_t, const HBitmap*, HBitmap>
479  {
480  private:
482  size_t index;
483 
484  public:
486  : p_bitmaps()
487  {}
488  iterator(const HMultiBitmap& bmps, size_t idx = 0)
489  : p_bitmaps(&bmps), index(idx)
490  {}
491 
492  iterator&
493  operator++() ynothrowv;
494 
495  reference
496  operator*() const;
497 
498  YF_API friend bool
499  operator==(const iterator&, const iterator&) ynothrow;
500 
501  DefGetter(const ynothrow, const HMultiBitmap*, HMultiBitmapPtr,
502  p_bitmaps)
503  DefGetter(const ynothrow, size_t, Index, index)
504  };
505  using const_iterator = iterator;
507 
508 private:
509  DataPtr pages;
510 
511 public:
522  HMultiBitmap(const char*, ImageDecoderFlags = ImageDecoderFlags::Default);
524  HMultiBitmap(const char*, ImageFormat,
530  HMultiBitmap(const char16_t*,
533  HMultiBitmap(const char16_t*, ImageFormat,
536 
537 
541  template<class _tString,
542  yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
543  HMultiBitmap(const _tString& filename,
544  ImageDecoderFlags flags = ImageDecoderFlags::Default)
545  : HMultiBitmap(&filename[0], flags)
546  {}
548  template<class _tString,
550  HMultiBitmap(const _tString& filename, ImageFormat fmt,
552  : HMultiBitmap(&filename[0], fmt, flags)
553  {}
555 
558 
561 
562  PDefHOp(bool, !, ) const ynothrow
563  ImplRet(!pages)
564 
565  explicit DefCvt(const ynothrow, bool, bool(pages))
566 
567  size_t
568  GetPageCount() const ynothrow;
569 
570  HBitmap
571  Lock(size_t = 0) const;
572 
574  PDefH(void, swap, HMultiBitmap& multi_pixmap) ynothrow
575  ImplExpr(std::swap(pages, multi_pixmap.pages))
576 
578 
579  PDefH(iterator, begin, ) const ynothrow
580  ImplRet(GetPageCount() != 0 ? HMultiBitmap::iterator(*this)
581  : HMultiBitmap::iterator())
582 
583  PDefH(iterator, end, ) const ynothrow
584  ImplRet(HMultiBitmap::iterator())
586 };
587 
588 inline HMultiBitmap::iterator&
589 HMultiBitmap::iterator::operator++() ynothrowv
590 {
591  YAssertNonnull(p_bitmaps);
592  if(++index == p_bitmaps->GetPageCount())
593  p_bitmaps = {};
594  return *this;
595 }
596 
597 inline HMultiBitmap::iterator::reference
599 {
600  YAssertNonnull(p_bitmaps);
601  return p_bitmaps->Lock(index);
602 }
603 
604 inline bool
606  ynothrow
607 {
608  return !(x == y);
609 }
610 
612 inline bool
614 {
615  return !i.GetHMultiBitmapPtr();
616 }
617 
619 inline DefSwap(ynothrow, HMultiBitmap)
621 
622 
623 class YF_API ImageCodec final
625 {
626 public:
627  ImageCodec();
629  ~ImageCodec();
630 
637  static ImageFormat
639  DetectFormat(ImageMemory::NativeHandle, size_t);
641  static ImageFormat
642  DetectFormat(const char*);
644  static ImageFormat
645  DetectFormat(const char16_t*);
647 
649  static CompactPixmap
650  Load(ImageMemory::Buffer);
651 
659  static HMultiBitmap
661  LoadForPlaying(const char*);
663  static HMultiBitmap
664  LoadForPlaying(const char16_t*);
669  template<class _tString,
671  static HMultiBitmap
672  LoadForPlaying(const _tString& filename)
673  {
674  return LoadForPlaying(&filename[0]);
675  }
677 
684  template<class _tSeqCon, typename _type>
685  static _tSeqCon
686  LoadSequence(const _type& path)
687  {
688  const auto multi_bitmap(LoadForPlaying(path));
689  _tSeqCon con{multi_bitmap.begin(), multi_bitmap.end()};
690 
691  if(con.empty())
692  con.emplace_back(path);
693  return con;
694  }
695 };
696 
697 } // namespace Drawing;
698 
699 } // namespace YSLib;
700 
701 #endif
702 
handle(::FreeImage_OpenMemory(static_cast< byte * >(buffer.data()), static_cast< ::DWORD >(buffer.size())))
ImageMemory(_fCallable f, ImageFormat fmt)
构造:以指定格式新建并打开内存缓冲区。
Definition: Image.h:216
UnknownImageFormat(const std::string &str)
Definition: Image.h:160
#define ynothrowv
YSLib 无异常抛出保证验证:有条件地使用无异常抛出规范。
Definition: ydef.h:494
标准矩形像素图缓冲区。
Definition: ygdi.h:201
#define ImplRet(...)
Definition: YBaseMacro.h:97
异常:不支持的操作。
Definition: utility.hpp:42
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
#define YF_API
Definition: Platform.h:64
unsigned char byte
字节类型。
Definition: ydef.h:555
enable_if_t< is_class< decay_t< _tParam >>::value, int > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:67
ImageMemory(_fCallable f)
构造:新建并打开内存缓冲区。
Definition: Image.h:211
::FIBITMAP * DataPtr
Definition: Image.h:241
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:392
图像资源分配失败异常:表示存储等资源不足导致无法创建图像。
Definition: Image.h:134
#define yimpl(...)
实现标签。
Definition: ydef.h:177
HBitmap(const _tString &filename, ImageDecoderFlags=ImageDecoderFlags::Default)
构造:使用指定字符串文件名和解码器标识。
Definition: Image.h:301
vector< octet > Buffer
Definition: Image.h:175
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
图像内存对象;保存图像内容的数据缓冲对象。
Definition: Image.h:170
::FIMEMORY * NativeHandle
Definition: Image.h:173
pixmap bitmap
Definition: Image.cpp:317
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
#define ImplExpr(...)
Definition: YBaseMacro.h:93
HMultiBitmap(const _tString &filename, ImageFormat fmt, ImageDecoderFlags flags=ImageDecoderFlags::Default)
构造:使用指定字符串文件名和解码器标识。
Definition: Image.h:550
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
未知图像格式异常:表示请求的操作涉及的图像格式因为不明确而不受库的支持。
Definition: Image.h:157
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
iterator(const HMultiBitmap &bmps, size_t idx=0)
Definition: Image.h:488
u8 BitPerPixel
Definition: Image.h:55
#define YAssertNonnull(_expr)
Definition: cassert.h:81
#define DefSwap(_q, _t)
Definition: Image.h:619
const HMultiBitmap * p_bitmaps
Definition: Image.h:481
多页面位图句柄:指向多页面位图数据。
Definition: Image.h:471
_tWidget _fCallable && f
Definition: ywgtevt.h:597
UnsupportedImageFormat(const std::string &str)
Definition: Image.h:146
NativeHandle handle
Definition: Image.h:179
bool is_undereferenceable(const HMultiBitmap::iterator &i) ynothrow
Definition: Image.h:613
HBitmap(DataPtr ptr={}) ynothrow
Definition: Image.h:252
位图句柄:指向位图数据。
Definition: Image.h:238
std::uint8_t u8
通用数据类型。
Definition: yadaptor.h:67
ImageDecoderFlags
图像解码器标识。
Definition: Image.h:81
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
记录日志的异常事件类。
Definition: yexcept.h:58
bool operator!=(const HMultiBitmap::iterator &x, const HMultiBitmap::iterator &y) ynothrow
Definition: Image.h:605
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
shared_ptr< MultiBitmapData > DataPtr
Definition: Image.h:474
PixelType * BitmapPtr
Definition: Video.h:295
PDefH(void, DrawLineSeg, const Graphics &g, const Rect &bounds, const Point &p1, const Point &p2, Color c) ImplExpr(DrawLineSeg(g
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
屏幕区域大小。
Definition: ygdibase.h:249
表示锁定键。
Definition: Keys.h:113
HBitmap(const _tString &filename, ImageFormat fmt, ImageDecoderFlags=ImageDecoderFlags::Default)
构造:使用指定字符串文件名和解码器标识。
Definition: Image.h:311
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
未被支持的图像格式异常:表示请求的操作涉及的图像格式不受库的支持。
Definition: Image.h:142
reference operator*() const
Definition: Image.h:598
#define DefDelMoveCtor(_t)
Definition: YBaseMacro.h:143
ImageFormat
图像格式。
Definition: Image.h:64