Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cImageGDIP.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cImageGDIP_H
8 #define _INC_cImageGDIP_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cImageBase.h"
14 
15 #if defined(_WIN32) && (_MSC_VER >= 1300) && ! defined(UNDER_CE)
16 
17 #include "../WinAPI/cWinResource.h"
20 #include "GrayCore/include/cAtom.h"
22 #include "GrayCore/include/cFile.h"
24 
25 #include <minmax.h> // stupid M$ doesn't include this for gdiplus.h.
26 #include <gdiplus.h> // let windows GDIPLUS do all the work.
27 
28 namespace GrayLib
29 {
30  UNITTEST2_PREDEF(cImageGDIP);
31 
32  enum CIMAGEGDIP_TYPE
33  {
37  CIMAGEGDIP_NONE = -1,
38  CIMAGEGDIP_JPG, // D3DXIFF_JPG
39  CIMAGEGDIP_PNG,
40  CIMAGEGDIP_GIF,
41  CIMAGEGDIP_BMP,
42  CIMAGEGDIP_TIF,
43  // CIMAGEGDIP_TGA
44  CIMAGEGDIP_QTY,
45  };
46 
47  class GRAYLIB_LINK cImageGDIP : public cImageBase
48  {
53  typedef cImageBase SUPER_t;
54 
55  public:
56  static const FILECHAR_t* const k_szImageExtsRev[CIMAGEGDIP_QTY + 1];
57  static const FILECHAR_t* const k_szImageExts[CIMAGEGDIP_QTY + 1];
58 
59  int m_iLockSurfaceCount;
60  cNewPtr<Gdiplus::Bitmap> m_pBitmap;
61  Gdiplus::BitmapData m_Data;
62 
63  protected:
64  CWinGlobalV m_Buffer;
65 
66  protected:
67  bool UpdateSurfaceParams();
68 
69  public:
70  cImageGDIP() noexcept
71  : m_pBitmap(nullptr)
72  , m_iLockSurfaceCount(0)
73  {}
74 
75  cImageGDIP(const wchar_t* pwFile);
76  cImageGDIP(const char* pwFile);
77  cImageGDIP(const GChar_t* pResName, const void* pResType = RT_RCDATA, HMODULE hInst = HMODULE_NULL)
78  {
79  LoadResource(pResName, pResType, hInst);
80  }
81  cImageGDIP(RESOURCEID_t id, const void* pResType = RT_RCDATA, HMODULE hInst = HMODULE_NULL)
82  {
83  LoadResource(id, pResType, hInst);
84  }
85  virtual ~cImageGDIP();
86 
87  virtual bool isValidImage() const noexcept override
88  {
90  return m_pBitmap != nullptr;
91  }
92 
93  virtual void SetEmpty() override;
94  HRESULT CreateSurface(PIXELS_t cx, PIXELS_t cy, Gdiplus::PixelFormat ePixelFormat = PixelFormatDontCare);
95 
96  HRESULT LoadDirectDrawSurface7(IN IDirectDrawSurface7* pSurface);
97  HRESULT LoadFileGDIP(const wchar_t* pwFile);
98  HRESULT LoadFileGDIP(const char* pszFile);
99  virtual HRESULT LoadFile(const FILECHAR_t* pszFileName) override
100  {
101  return LoadFileGDIP(pszFileName);
102  }
103 
104  HRESULT LoadFileWildcard(const FILECHAR_t* pszFilePath, bool bPreferCompressed = false);
105 
106  virtual HRESULT ReadStream(cStreamInput& strIn) override;
107  virtual HRESULT SaveFile(const FILECHAR_t* pszPath) const override;
108 
109  void CopyTo(cSurfaceBase& TexDst, PIXELS_t itxs, PIXELS_t itys, bool bTextureXReverse, bool bTextureYReverse) const;
110  void CopyFrom(const cSurfaceBase& TexSrc, PIXELS_t itxs, PIXELS_t itys, bool bTextureXReverse, bool bTextureYReverse, bool bColorReverse);
111 
112  HRESULT LockSurface(Gdiplus::PixelFormat ePixelFormat = PixelFormatDontCare, bool bWrite = false);
113  void UnlockSurface();
114 
115  operator Gdiplus::Bitmap*() const
116  {
117  return m_pBitmap;
118  }
119 
120  static HRESULT GRAYCALL GetHResultFrom(Gdiplus::Status status);
121  static HRESULT GRAYCALL GetEncoderClsid(const wchar_t* format, CLSID& encoderClsid);
122  static HRESULT GRAYCALL GetEncoderClsid(CIMAGEGDIP_TYPE eExtType, CLSID& encoderClsid);
123  static CIMAGEGDIP_TYPE GRAYCALL GetImageTypeForFile(const FILECHAR_t* pszPath);
124 
125  UNITTEST2_PREDEF(cImageGDIP);
126  };
127 
128  class GRAYLIB_LINK cImageGDIPType
129  : public cSingleton<cImageGDIPType>
130  , public IImageCreator
131  {
136  friend class cSingleton<cImageGDIPType>;
137 
138  public:
139  ULONG_PTR m_gdiplusToken;
140 
141  protected:
142  cImageGDIPType();
143  ~cImageGDIPType();
144 
145  public:
146  bool StartupGDIP();
147  void ShutdownGDIP();
148 
149  virtual bool IsFileExt(const FILECHAR_t* pszFileExt) const
150  {
151  UNREFERENCED_PARAMETER(pszFileExt);
152  return true;
153  }
154  virtual cImageBasePtr CreateObject() const override
155  {
156  return new cImageGDIP;
157  }
158  };
159 };
160 #endif // _WIN32
161 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define HMODULE_NULL
Definition: cOSModule.h:31
Definition: cImageBase.h:66
Definition: cMesh.h:22
cRefPtr< cImageBase > cImageBasePtr
Definition: cImageBase.h:64
UNITTEST2_PREDEF(cQuadtree)
int PIXELS_t
Count of pixels in a dimension of some image/surface.
Definition: cSurfaceInfo.h:20
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
WORD RESOURCEID_t
A resource in .RC attached to a file/module. like MAKEINTRESOURCE(). https://docs....
Definition: cResourceBase.h:19
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
class __DECL_IMPORT cStreamInput
Definition: cString.h:26
Definition: IUnknown.h:34