YSTest: Image.h 源文件

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
110 DefBitmaskEnum(ImageDecoderFlags)
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
142 class YF_API UnsupportedImageFormat
143 : public LoggedEvent, public ystdex::unsupported
144 {
145 public:
146 UnsupportedImageFormat(const std::string& str)
147 : LoggedEvent(str), unsupported()
148 {}
149 };
150
151
153
154
157 class YF_API UnknownImageFormat : public UnsupportedImageFormat
158 {
159 public:
160 UnknownImageFormat(const std::string& str)
161 : UnsupportedImageFormat(str)
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:
178 Buffer buffer;
179 NativeHandle handle;
180 ImageFormat format;
181
182 public:
196 explicit
197 ImageMemory(const HBitmap&, ImageFormat = ImageFormat::BMP,
198 ImageDecoderFlags = ImageDecoderFlags::Default);
205 ImageMemory(Buffer);
208 ImageMemory(Buffer, ImageFormat);
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 {}
220 DefDelMoveCtor(ImageMemory)
222 ~ImageMemory();
224
226 DefGetter(const ynothrow, const Buffer&, Buffer, buffer)
227 DefGetter(const ynothrow, ImageFormat, Format, format)
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:
244 DataPtr bitmap;
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 */
282 HBitmap(const char*, ImageDecoderFlags = ImageDecoderFlags::Default);
284 HBitmap(const char*, ImageFormat,
285 ImageDecoderFlags = ImageDecoderFlags::Default);
290 HBitmap(const char16_t*, ImageDecoderFlags = ImageDecoderFlags::Default);
292 HBitmap(const char16_t*, ImageFormat,
293 ImageDecoderFlags = ImageDecoderFlags::Default);
299 template<class _tString,
300 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
301 HBitmap(const _tString& filename,
302 ImageDecoderFlags = ImageDecoderFlags::Default)
303 : HBitmap(&filename[0])
304 {}
309 template<class _tString,
310 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
311 HBitmap(const _tString& filename, ImageFormat fmt,
312 ImageDecoderFlags = ImageDecoderFlags::Default)
313 : HBitmap(&filename[0], fmt)
314 {}
316
320 HBitmap(const ImageMemory&, ImageDecoderFlags = ImageDecoderFlags::Default);
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
377 BitPerPixel
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,
437 ImageDecoderFlags flags = ImageDecoderFlags::Default) const
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:
481 const HMultiBitmap* p_bitmaps;
482 size_t index;
483
484 public:
485 iterator()
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,
525 ImageDecoderFlags = ImageDecoderFlags::Default);
530 HMultiBitmap(const char16_t*,
531 ImageDecoderFlags = ImageDecoderFlags::Default);
533 HMultiBitmap(const char16_t*, ImageFormat,
534 ImageDecoderFlags = ImageDecoderFlags::Default);
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,
549 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
550 HMultiBitmap(const _tString& filename, ImageFormat fmt,
551 ImageDecoderFlags flags = ImageDecoderFlags::Default)
552 : HMultiBitmap(&filename[0], fmt, flags)
553 {}
555
556 DefDeCopyCtor(HMultiBitmap)
557 DefDeMoveCtor(HMultiBitmap)
558
559 DefDeCopyAssignment(HMultiBitmap)
560 DefDeMoveAssignment(HMultiBitmap)
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
598 HMultiBitmap::iterator::operator*() const
599 {
600 YAssertNonnull(p_bitmaps);
601 return p_bitmaps->Lock(index);
602 }
603
604 inline bool
605 operator!=(const HMultiBitmap::iterator& x, const HMultiBitmap::iterator& y)
606 ynothrow
607 {
608 return !(x == y);
609 }
610
612 inline bool
613 is_undereferenceable(const HMultiBitmap::iterator& i) ynothrow
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,
670 yimpl(typename = ystdex::enable_for_string_class_t<_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
YSLib::Drawing::ImageDecoderFlags::JPEG_Subsampling_420
YSLib::Drawing::handle
handle(::FreeImage_OpenMemory(static_cast< byte * >(buffer.data()), static_cast< ::DWORD >(buffer.size())))
YSLib::Drawing::ImageMemory::ImageMemory
ImageMemory(_fCallable f, ImageFormat fmt)
构造：以指定格式新建并打开内存缓冲区。
Definition: Image.h:216
YSLib::Drawing::UnknownImageFormat::UnknownImageFormat
UnknownImageFormat(const std::string &str)
Definition: Image.h:160
YSLib::Drawing::ImageDecoderFlags::Default
YSLib::Drawing::ImageDecoderFlags::JPEG_QualityBad
YSLib::Drawing::MultiBitmapData
Definition: Image.cpp:409
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:494
YSLib::Drawing::CompactPixmap
标准矩形像素图缓冲区。
Definition: ygdi.h:201
ImplRet
#define ImplRet(...)
Definition: YBaseMacro.h:97
YSLib::Drawing::ImageDecoderFlags::JPEG_EXIFRotate
ystdex::unsupported
异常：不支持的操作。
Definition: utility.hpp:42
YSLib::Drawing::ImageDecoderFlags::JPEG_Optimize
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
YF_API
#define YF_API
Definition: Platform.h:64
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:555
YSLib::Drawing::ImageDecoderFlags::JPEG_QualityNormal
YSLib::Drawing::ImageDecoderFlags::JPEG_CMYK
ystdex::enable_for_string_class_t
enable_if_t< is_class< decay_t< _tParam >>::value, int > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:67
YSLib::Drawing::ImageMemory::ImageMemory
ImageMemory(_fCallable f)
构造：新建并打开内存缓冲区。
Definition: Image.h:211
YSLib::Drawing::ImageDecoderFlags::GIF_Load256
YSLib::Drawing::ImageFormat::Unknown
YSLib::Drawing::HBitmap::DataPtr
::FIBITMAP * DataPtr
Definition: Image.h:241
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:392
YSLib::Drawing::BadImageAlloc
图像资源分配失败异常：表示存储等资源不足导致无法创建图像。
Definition: Image.h:134
YSLib::Drawing::ImageDecoderFlags::JPEG_Subsampling_422
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:177
YSLib::Drawing::ImageDecoderFlags::JPEG_QualityAverage
YSLib::Drawing::HBitmap::HBitmap
HBitmap(const _tString &filename, ImageDecoderFlags=ImageDecoderFlags::Default)
构造：使用指定字符串文件名和解码器标识。
Definition: Image.h:301
YSLib::Drawing::ImageMemory::Buffer
vector< octet > Buffer
Definition: Image.h:175
YSLib::Drawing::HMultiBitmap::iterator::iterator
iterator()
Definition: Image.h:485
ystdex::swap
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
YSLib::Drawing::ImageMemory
图像内存对象；保存图像内容的数据缓冲对象。
Definition: Image.h:170
YSLib::Drawing::ImageMemory::NativeHandle
::FIMEMORY * NativeHandle
Definition: Image.h:173
YSLib::Drawing::bitmap
pixmap bitmap
Definition: Image.cpp:317
YSLib::Drawing::ImageDecoderFlags::JPEG_QualityGood
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
YSLib::Drawing::ImageDecoderFlags::JPEG_Subsampling_444
ImplExpr
#define ImplExpr(...)
Definition: YBaseMacro.h:93
YSLib::Drawing::HMultiBitmap::HMultiBitmap
HMultiBitmap(const _tString &filename, ImageFormat fmt, ImageDecoderFlags flags=ImageDecoderFlags::Default)
构造：使用指定字符串文件名和解码器标识。
Definition: Image.h:550
YSLib::Drawing::ImageFormat::BMP
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::Drawing::ImageDecoderFlags::JPEG_Accurate
YSLib::Drawing::UnknownImageFormat
未知图像格式异常：表示请求的操作涉及的图像格式因为不明确而不受库的支持。
Definition: Image.h:157
YSLib::Drawing::ImageFormat::JPEG
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
YSLib::Drawing::HMultiBitmap::iterator::iterator
iterator(const HMultiBitmap &bmps, size_t idx=0)
Definition: Image.h:488
YSLib::Drawing::BitPerPixel
u8 BitPerPixel
Definition: Image.h:55
YSLib::Drawing::ImageDecoderFlags::GIF_Playback
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:81
YSLib::Drawing::HMultiBitmap::DefSwap
#define DefSwap(_q, _t)
Definition: Image.h:619
YSLib::Drawing::HMultiBitmap::iterator::p_bitmaps
const HMultiBitmap * p_bitmaps
Definition: Image.h:481
YSLib::Drawing::HMultiBitmap
多页面位图句柄：指向多页面位图数据。
Definition: Image.h:471
YSLib::Drawing::HBitmap::bitmap
DataPtr bitmap
Definition: Image.h:244
YSLib::Drawing::ImageFormat::PNG
YSLib::UI::f
_tWidget _fCallable && f
Definition: ywgtevt.h:597
YSLib::Drawing::ImageDecoderFlags::JPEG_GreyScale
YSLib::Drawing::UnsupportedImageFormat::UnsupportedImageFormat
UnsupportedImageFormat(const std::string &str)
Definition: Image.h:146
YSLib::Drawing::ImageDecoderFlags::JPEG_Progressive
YSLib::Drawing::ImageMemory::handle
NativeHandle handle
Definition: Image.h:179
YSLib::Drawing::ImageDecoderFlags::JPEG_Fast
YSLib::Drawing::HMultiBitmap::iterator::is_undereferenceable
bool is_undereferenceable(const HMultiBitmap::iterator &i) ynothrow
Definition: Image.h:613
YSLib::Drawing::HBitmap::HBitmap
HBitmap(DataPtr ptr={}) ynothrow
Definition: Image.h:252
YSLib::Drawing::HBitmap
位图句柄：指向位图数据。
Definition: Image.h:238
YSLib::u8
std::uint8_t u8
通用数据类型。
Definition: yadaptor.h:67
YSLib::Drawing::ImageDecoderFlags
ImageDecoderFlags
图像解码器标识。
Definition: Image.h:81
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::Drawing::operator!=
bool operator!=(const HMultiBitmap::iterator &x, const HMultiBitmap::iterator &y) ynothrow
Definition: Image.h:605
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
YSLib::Drawing::ImageDecoderFlags::ICO_MakeAlpha
YSLib::Drawing::HMultiBitmap::DataPtr
shared_ptr< MultiBitmapData > DataPtr
Definition: Image.h:474
YSLib::Drawing::HMultiBitmap::iterator::index
size_t index
Definition: Image.h:482
YSLib::Drawing::ImageFormat::ICO
platform::BitmapPtr
PixelType * BitmapPtr
Definition: Video.h:295
YSLib::Drawing::PDefH
PDefH(void, DrawLineSeg, const Graphics &g, const Rect &bounds, const Point &p1, const Point &p2, Color c) ImplExpr(DrawLineSeg(g
YSLib::Drawing::ImageDecoderFlags::JPEG_Subsampling_411
YSLib::Drawing::ImageDecoderFlags::JPEG_Baseline
YSLib::Drawing::DefGetter
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
YSLib::Drawing::ImageMemory::buffer
Buffer buffer
Definition: Image.h:178
YSLib::Drawing::format
format(fmt)
Definition: Image.cpp:242
YSLib::Drawing::HMultiBitmap::iterator
Definition: Image.h:477
YSLib::Drawing::Size
屏幕区域大小。
Definition: ygdibase.h:249
platform::KeyCategory::Lock
表示锁定键。
Definition: Keys.h:113
YSLib::Drawing::HBitmap::HBitmap
HBitmap(const _tString &filename, ImageFormat fmt, ImageDecoderFlags=ImageDecoderFlags::Default)
构造：使用指定字符串文件名和解码器标识。
Definition: Image.h:311
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
YSLib::Drawing::UnsupportedImageFormat
未被支持的图像格式异常：表示请求的操作涉及的图像格式不受库的支持。
Definition: Image.h:142
YSLib::Drawing::HMultiBitmap::iterator::operator*
reference operator*() const
Definition: Image.h:598
YSLib::Drawing::ImageDecoderFlags::JPEG_QualitySuperb
DefDelMoveCtor
#define DefDelMoveCtor(_t)
Definition: YBaseMacro.h:143
YSLib::Drawing::ImageMemory::format
ImageFormat format
Definition: Image.h:180
YSLib::Drawing::ImageFormat::GIF
YSLib::Drawing::ImageFormat
ImageFormat
图像格式。
Definition: Image.h:64