Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSurfaceInfo.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSurfaceInfo_H
7 #define _INC_cSurfaceInfo_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
13 #include "../WinAPI/WinTypes.h"
15 #include "GrayCore/include/cMem.h"
16 #include "GrayCore/include/cBits.h"
17 
18 namespace GrayLib
19 {
20  typedef int PIXELS_t;
21  typedef BYTE COLOR_ELEM_t;
22 
24  {
31 
34 
37 
40 
42  SURF_FORM_A8 = 28,
43 
47 
51 
52  SURF_FORM_A1 = 118,
53 
54 #if 0
55  // Special DirectX compressed formats. just leave these as compressed. no pixel access.
56  D3DFMT_DXT1 = MAKEFOURCC('D', 'X', 'T', '1'),
57  D3DFMT_DXT2 = MAKEFOURCC('D', 'X', 'T', '2'),
58  D3DFMT_DXT3 = MAKEFOURCC('D', 'X', 'T', '3'),
59  D3DFMT_DXT4 = MAKEFOURCC('D', 'X', 'T', '4'),
60  D3DFMT_DXT5 = MAKEFOURCC('D', 'X', 'T', '5'),
61 #endif
62 
64  };
65 
66 #ifdef __linux__
67 #pragma pack(push,1)
68  struct CATTR_PACKED tagBITMAPINFOHEADER
69  {
70  UINT32 biSize;
71  INT32 biWidth;
72  INT32 biHeight;
73  WORD biPlanes;
74  WORD biBitCount;
75  UINT32 biCompression;
76  UINT32 biSizeImage;
77  INT32 biXPelsPerMeter;
78  INT32 biYPelsPerMeter;
79  UINT32 biClrUsed;
80  UINT32 biClrImportant;
81  };
82  struct CATTR_PACKED tagRGBQUAD
83  {
84  BYTE rgbBlue;
85  BYTE rgbGreen;
86  BYTE rgbRed;
87  BYTE rgbReserved;
88  };
89  struct CATTR_PACKED tagBITMAPINFO
90  {
91  tagBITMAPINFOHEADER bmiHeader;
92  tagRGBQUAD bmiColors[1];
93  };
94  enum BI_TYPE
95  {
96  BI_RGB = 0,
97  BI_RLE8 = 1,
98  BI_RLE4 = 2,
99  BI_BITFIELDS = 3,
100  BI_JPEG = 4,
101  BI_PNG = 5,
102  };
103 #pragma pack(pop)
104 #endif
105 
106  class cSurfaceBitmapInfo;
107 
109  {
114 
115  private:
116  cWinSize m_Size;
117  size_t m_uPitchBytes;
118  SURF_FORM_TYPE m_ePixelFormat;
119 
120  public:
121  static const PIXELS_t kMaxX = 8 * 1024; // Arbitrary Max reasonable value.
122  static const PIXELS_t kMaxY = 8 * 1024; // Arbitrary Max reasonable value.
123 
124  public:
125  cSurfaceInfo(PIXELS_t cx = 0, PIXELS_t cy = 0, size_t uPitchBytes = 0, SURF_FORM_TYPE ePixelFormat = SURF_FORM_UNKNOWN)
126  : m_Size(cx, cy)
127  , m_uPitchBytes(uPitchBytes)
128  , m_ePixelFormat(ePixelFormat)
129  {
130  ASSERT(isReasonableInfo());
131  }
133  {
134  this->SetBitmapInfo(pBMI);
135  }
136  cSurfaceInfo(struct tagBITMAPINFOHEADER* pBMI)
137  {
138  SetBitmapInfo((const cSurfaceBitmapInfo*)pBMI);
139  }
140  cSurfaceInfo(const tagBITMAPINFO* pBMI)
141  {
142  SetBitmapInfo((const cSurfaceBitmapInfo*)pBMI);
143  }
144 #ifdef _WIN32
145  cSurfaceInfo(const BITMAP& bm)
146  {
147  SetBitmapInfo(bm);
148  }
149 #endif
150 
151  PIXELS_t get_Width() const noexcept
152  {
154  return m_Size.cx;
155  }
156  PIXELS_t get_Height() const noexcept
157  {
159  return ABS(m_Size.cy);
160  }
161  PIXELS_t get_HeightRaw() const noexcept
162  {
164  return m_Size.cy;
165  }
166  bool isTopDown() const noexcept
167  {
170  return m_Size.cy >= 0;
171  }
172  cWinSize get_Size() const noexcept
173  {
174  // Remove isTopDown info.
175  return cWinSize(m_Size.cx, get_Height());
176  }
177  size_t get_PitchBytes() const noexcept
178  {
180  return m_uPitchBytes;
181  }
182  void put_PitchBytes(size_t uPitchBytes) noexcept
183  {
185  m_uPitchBytes = uPitchBytes;
186  }
187 
189  {
191  return m_ePixelFormat;
192  }
193  size_t get_SurfaceSize() const noexcept
194  {
196  return get_Height() * get_PitchBytes();
197  }
198  BIT_SIZE_t get_BitsPerPixel() const noexcept
199  {
200  return GetBitsPerPixel(m_ePixelFormat);
201  }
202  size_t get_BytesPerPixel() const noexcept
203  {
204  // @note m_ePixelFormat = SURF_FORM_A1 not truly valid here. just returns 0.
205  return get_BitsPerPixel() / cBits::k_8;
206  }
207 
208  bool isValidInfo() const noexcept
209  {
211  const PIXELS_t cx = get_Height();
212  if (cx <= 0 || cx > kMaxX)
213  return false;
214  const PIXELS_t cy = get_Height();
215  if (cy <= 0 || cy > kMaxY)
216  return false;
217  if (m_uPitchBytes <= 0 || m_uPitchBytes > kMaxX * 4)
218  return false;
219  if (m_ePixelFormat <= SURF_FORM_UNKNOWN || m_ePixelFormat >= SURF_FORM_QTY)
220  return false;
221  return true;
222  }
223 
224  bool isReasonableInfo() const noexcept
225  {
228  if (m_Size.cx < 0 || m_Size.cx > kMaxX)
229  return false;
230  const PIXELS_t cy = get_Height();
231  if (cy < 0 || cy > kMaxY)
232  return false;
233  if (m_uPitchBytes > kMaxX * 4) // m_uPitchBytes < 0 not possible for unsigned of course.
234  return false;
235  if (m_ePixelFormat < SURF_FORM_UNKNOWN || m_ePixelFormat >= SURF_FORM_QTY)
236  return false;
237  return true;
238  }
239  bool IsEqual(const cSurfaceInfo& rInfo) const noexcept
240  {
241  if (m_Size.cx != rInfo.m_Size.cx)
242  return false;
243  if (m_Size.cy != rInfo.m_Size.cy)
244  return false;
245  if (m_uPitchBytes != rInfo.m_uPitchBytes)
246  return false;
247  if (m_ePixelFormat != rInfo.m_ePixelFormat)
248  return false;
249  return true;
250  }
251 
252  void SetSurfaceInfo(const cSurfaceInfo& rInfo)
253  {
254  m_Size = rInfo.m_Size;
255  m_uPitchBytes = rInfo.m_uPitchBytes;
256  m_ePixelFormat = rInfo.m_ePixelFormat;
257  ASSERT(isReasonableInfo());
258  }
259  void InitSurfaceInfo(PIXELS_t cx = 0, PIXELS_t cy = 0, size_t uPitchBytes = 0, SURF_FORM_TYPE ePixelFormat = SURF_FORM_UNKNOWN) noexcept
260  {
261  m_Size.cx = cx;
262  m_Size.cy = cy;
263  m_uPitchBytes = uPitchBytes;
264  m_ePixelFormat = ePixelFormat;
265  DEBUG_CHECK(isReasonableInfo());
266  }
267 
268  static BIT_SIZE_t GRAYCALL GetBitsPerPixel(SURF_FORM_TYPE ePixelFormat) noexcept;
269  static COLOR_ELEM_t GRAYCALL GetColorElements(SURF_FORM_TYPE ePixelFormat) noexcept;
270  static SURF_FORM_TYPE GRAYCALL GetPixelFormat(int iBitsPerPixel, int iColorElements);
271 
272  static inline constexpr size_t CvtBitsToAlignedBytes(UINT nBitsTotal, size_t nAlignBytes) noexcept
273  {
279 
280  const size_t nAlignBits = nAlignBytes * cBits::k_8;
281  return (((nBitsTotal + nAlignBits - 1) / nAlignBits) * nAlignBytes);
282  }
283  void InitAligned(PIXELS_t cx, PIXELS_t cy, size_t nAlignBytes, SURF_FORM_TYPE ePixelFormat)
284  {
287  ASSERT(nAlignBytes > 0);
288  const BIT_SIZE_t uBitsPerPixel = GetBitsPerPixel(ePixelFormat);
289  ASSERT(uBitsPerPixel > 0);
290  InitSurfaceInfo(cx, cy, CvtBitsToAlignedBytes(cx * uBitsPerPixel, nAlignBytes), ePixelFormat);
291  }
292 
293  void SetBitmapInfo(const cSurfaceBitmapInfo* pBMI);
294  void SetBitmapInfo(const struct tagBITMAPINFOHEADER* pBMI)
295  {
296  SetBitmapInfo((const cSurfaceBitmapInfo*)pBMI);
297  }
298  void SetBitmapInfo(const tagBITMAPINFO* pBMI)
299  {
300  SetBitmapInfo((const cSurfaceBitmapInfo*)pBMI);
301  }
302 #ifdef _WIN32
303  void SetBitmapInfo(const BITMAP& bm);
304 #endif
305  };
306 
307  class GRAYLIB_LINK cSurfaceBitmapInfo : public tagBITMAPINFO
308  {
314 
315  public:
317  {
318  InitZero();
319  }
321  {
322  SetSurfaceInfo(rInfo);
323  }
324 
325  void InitZero() noexcept
326  {
327  cMem::Zero(this, sizeof(*this));
328  }
329  bool isCompressed() const noexcept
330  {
332  switch (this->bmiHeader.biCompression)
333  {
334  case BI_RGB:
335  return false;
336  default:
337  return true; // everything else is considered compressed.
338  }
339  }
340  bool isValidInfo() const;
341 
342  PIXELS_t get_Width() const noexcept
343  {
345  DEBUG_CHECK(this != nullptr);
346  return (PIXELS_t)this->bmiHeader.biWidth ;
347  }
348  PIXELS_t get_Height() const noexcept
349  {
351  DEBUG_CHECK(this != nullptr);
352  return ABS((int)this->bmiHeader.biHeight) ;
353  }
354  PIXELS_t get_HeightRaw() const noexcept
355  {
358  DEBUG_CHECK(this != nullptr);
359  return this->bmiHeader.biHeight;
360  }
361 
362  void SetHeightTopDown() noexcept
363  {
365  this->bmiHeader.biHeight = ABS(this->bmiHeader.biHeight);
366  }
367  bool isTopDown() const noexcept
368  {
372  if (isCompressed()) // all compressed images don't do this bullshit.
373  return true;
374  if (this->bmiHeader.biHeight < 0) // GDI marks these as negative.
375  return true;
376  return false; // old fashion _WIN32 GDI bottom up mode.
377  }
378  BIT_SIZE_t get_BitsPerPixel() const noexcept
379  {
380  DEBUG_CHECK(this != nullptr);
381  return((BIT_SIZE_t)this->bmiHeader.biBitCount * this->bmiHeader.biPlanes);
382  }
383 
384  void SetSurfaceInfo(const cSurfaceInfo& rInfo);
385  void SetCopy(const tagBITMAPINFO& rCopy);
386 
387  size_t get_PitchBytes() const;
388  SURF_FORM_TYPE get_PixelFormat() const;
389 
390  WORD get_NumColors() const;
391  size_t get_ColorSize() const noexcept
392  {
395  return this->get_NumColors() * sizeof(tagRGBQUAD);
396  }
397  tagRGBQUAD* get_ColorTablePtr() const noexcept
398  {
400  DEBUG_CHECK(this != nullptr);
401  return((tagRGBQUAD*)(((BYTE*)const_cast<cSurfaceBitmapInfo*>(this)) + this->bmiHeader.biSize));
402  }
403 
404  size_t get_SurfaceSize() const noexcept;
405  size_t get_ImageSize() const noexcept;
406  size_t get_TotalSize() const noexcept;
407 
408  BYTE* get_PixelDataPtr() const;
409  };
410 };
411 #endif // _INC_cSurfaceInfo_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define CATTR_PACKED
Definition: GrayCore.h:87
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define ABS(n)
Definition: SysTypes.h:460
@ D3DFMT_DXT5
Definition: UseDX.h:198
@ D3DFMT_DXT2
Definition: UseDX.h:195
@ D3DFMT_DXT1
Definition: UseDX.h:194
@ D3DFMT_DXT4
Definition: UseDX.h:197
@ D3DFMT_DXT3
Definition: UseDX.h:196
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define MAKEFOURCC(ch0, ch1, ch2, ch3)
Definition: cFourCC.h:24
Definition: cSurfaceInfo.h:308
PIXELS_t get_HeightRaw() const noexcept
Definition: cSurfaceInfo.h:354
bool isCompressed() const noexcept
Definition: cSurfaceInfo.h:329
void InitZero() noexcept
Definition: cSurfaceInfo.h:325
PIXELS_t get_Width() const noexcept
Definition: cSurfaceInfo.h:342
tagRGBQUAD * get_ColorTablePtr() const noexcept
Definition: cSurfaceInfo.h:397
size_t get_ColorSize() const noexcept
Definition: cSurfaceInfo.h:391
bool isTopDown() const noexcept
Definition: cSurfaceInfo.h:367
PIXELS_t get_Height() const noexcept
Definition: cSurfaceInfo.h:348
cSurfaceBitmapInfo() noexcept
Definition: cSurfaceInfo.h:316
BIT_SIZE_t get_BitsPerPixel() const noexcept
Definition: cSurfaceInfo.h:378
cSurfaceBitmapInfo(const cSurfaceInfo &rInfo)
Definition: cSurfaceInfo.h:320
void SetHeightTopDown() noexcept
Definition: cSurfaceInfo.h:362
Definition: cSurfaceInfo.h:109
bool isTopDown() const noexcept
Definition: cSurfaceInfo.h:166
void SetSurfaceInfo(const cSurfaceInfo &rInfo)
Definition: cSurfaceInfo.h:252
static constexpr size_t CvtBitsToAlignedBytes(UINT nBitsTotal, size_t nAlignBytes) noexcept
Definition: cSurfaceInfo.h:272
void SetBitmapInfo(const struct tagBITMAPINFOHEADER *pBMI)
Definition: cSurfaceInfo.h:294
cSurfaceInfo(const cSurfaceBitmapInfo *pBMI)
Definition: cSurfaceInfo.h:132
void SetBitmapInfo(const tagBITMAPINFO *pBMI)
Definition: cSurfaceInfo.h:298
cSurfaceInfo(struct tagBITMAPINFOHEADER *pBMI)
Definition: cSurfaceInfo.h:136
cSurfaceInfo(const tagBITMAPINFO *pBMI)
Definition: cSurfaceInfo.h:140
size_t get_BytesPerPixel() const noexcept
Definition: cSurfaceInfo.h:202
PIXELS_t get_HeightRaw() const noexcept
Definition: cSurfaceInfo.h:161
bool IsEqual(const cSurfaceInfo &rInfo) const noexcept
Definition: cSurfaceInfo.h:239
void InitAligned(PIXELS_t cx, PIXELS_t cy, size_t nAlignBytes, SURF_FORM_TYPE ePixelFormat)
Definition: cSurfaceInfo.h:283
bool isValidInfo() const noexcept
Definition: cSurfaceInfo.h:208
void InitSurfaceInfo(PIXELS_t cx=0, PIXELS_t cy=0, size_t uPitchBytes=0, SURF_FORM_TYPE ePixelFormat=SURF_FORM_UNKNOWN) noexcept
Definition: cSurfaceInfo.h:259
PIXELS_t get_Width() const noexcept
Definition: cSurfaceInfo.h:151
size_t get_PitchBytes() const noexcept
Definition: cSurfaceInfo.h:177
BIT_SIZE_t get_BitsPerPixel() const noexcept
Definition: cSurfaceInfo.h:198
cWinSize get_Size() const noexcept
Definition: cSurfaceInfo.h:172
bool isReasonableInfo() const noexcept
Definition: cSurfaceInfo.h:224
PIXELS_t get_Height() const noexcept
Definition: cSurfaceInfo.h:156
SURF_FORM_TYPE get_PixelFormat() const noexcept
Definition: cSurfaceInfo.h:188
size_t get_SurfaceSize() const noexcept
Definition: cSurfaceInfo.h:193
cSurfaceInfo(PIXELS_t cx=0, PIXELS_t cy=0, size_t uPitchBytes=0, SURF_FORM_TYPE ePixelFormat=SURF_FORM_UNKNOWN)
Definition: cSurfaceInfo.h:125
void put_PitchBytes(size_t uPitchBytes) noexcept
Definition: cSurfaceInfo.h:182
Definition: WinTypes.h:128
Definition: cMesh.h:22
SURF_FORM_TYPE
Definition: cSurfaceInfo.h:24
@ SURF_FORM_R32F
grey scale 32 bit float. D3DFMT_R32F
Definition: cSurfaceInfo.h:50
@ SURF_FORM_ARGB8888
32 bit D3DFMT_A8R8G8B8 (DX format)
Definition: cSurfaceInfo.h:35
@ SURF_FORM_ABGR8888
GDI 32 bit format. D3DFMT_A8B8G8R8.
Definition: cSurfaceInfo.h:45
@ SURF_FORM_QTY
Definition: cSurfaceInfo.h:63
@ SURF_FORM_A8
8 bit alpha = D3DFMT_A8
Definition: cSurfaceInfo.h:42
@ SURF_FORM_D16
grey scale 16 bit unsigned. D3DFMT_D16
Definition: cSurfaceInfo.h:49
@ SURF_FORM_BGR888
24 bit (GDI format). NO Alpha channel.
Definition: cSurfaceInfo.h:44
@ SURF_FORM_RGB565
16 bit = D3DFMT_R5G6B5
Definition: cSurfaceInfo.h:38
@ SURF_FORM_RGB888
24 bit D3DFMT_R8G8B8 (DX format). NO Alpha channel.
Definition: cSurfaceInfo.h:33
@ SURF_FORM_D32
grey scale 32 bit unsigned. D3DFMT_D32
Definition: cSurfaceInfo.h:48
@ SURF_FORM_UNKNOWN
AKA D3DFMT_UNKNOWN.
Definition: cSurfaceInfo.h:32
@ SURF_FORM_XRGB1555
16 bit
Definition: cSurfaceInfo.h:39
@ SURF_FORM_XRGB8888
32 bit D3DFMT_X8R8G8B8 (DX format). Ignored Alpha channel.
Definition: cSurfaceInfo.h:36
@ SURF_FORM_ARGB1555
16 bit 555 RGB + 1 A. 1 bit Alpha channel.
Definition: cSurfaceInfo.h:41
@ SURF_FORM_XBGR8888
GDI 32 bit format. D3DFMT_X8B8G8R8. Ignored Alpha channel.
Definition: cSurfaceInfo.h:46
@ SURF_FORM_A1
1 alpha bit per pixel. (D3DFMT_A1)
Definition: cSurfaceInfo.h:52
BYTE COLOR_ELEM_t
A single 8 bit color element. alpha, red, green, or blue intensity as 0-255.
Definition: cColorRef.h:34
int PIXELS_t
Count of pixels in a dimension of some image/surface.
Definition: cSurfaceInfo.h:20
unsigned short BIT_SIZE_t
number of bits in some intrinsic type. <= 256 ?
Definition: cBits.h:17
static const BYTE k_8
represent the 8 bits in a byte. BIT_ENUM_t
Definition: cBits.h:52
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100