Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSurfaceBase.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSurfaceBase_H
7 #define _INC_cSurfaceBase_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cSurfaceInfo.h"
13 #include "cColorRef.h"
14 #include "../WinAPI/WinTypes.h" // COLORREF
20 
21 namespace GrayLib
22 {
24 
26  {
32 
33  friend class cSurfaceDC;
34  friend class cSurfaceDC2;
35 
36  protected:
38 
39  protected:
40  cSurfaceBase& operator = (const cSurfaceBase& rSurf)
41  {
42  SetPixelData(rSurf);
43  return *this;
44  }
45 
46  bool InitSurfaceInfo(const cSurfaceInfo& rInfo);
47  bool InitSurfaceAlign(PIXELS_t cx, PIXELS_t cy, size_t nAlignBytes, SURF_FORM_TYPE ePixelFormat);
48  bool InitSurfacePitch(PIXELS_t cx, PIXELS_t cy, size_t uPitchBytes, SURF_FORM_TYPE ePixelFormat);
49 
50  public:
51  cSurfaceBase() noexcept;
52  cSurfaceBase(void* pPixelData, const cSurfaceInfo& rInfo);
53  cSurfaceBase(void* pPixelData, PIXELS_t cx, PIXELS_t cy, size_t uPitchBytes, SURF_FORM_TYPE ePixelFormat);
54  virtual ~cSurfaceBase();
55 
56  HRESULT SaveRaw(cStreamOutput& file);
57  HRESULT SaveRaw(const FILECHAR_t* pszFilename);
58 
59  virtual void SetEmpty();
60 
61  bool isValidCheck() const noexcept // memory allocation and structure definitions are valid.
62  {
63  if (!IS_TYPE_OF(cSurfaceBase, this)) // structure definitions are valid..
64  return false;
65  return true;
66  }
67 
68  // Access cSurfaceInfo m_Info
69  const cSurfaceInfo& get_Info() const noexcept
70  {
71  return m_Info;
72  }
73  void put_Info(const cSurfaceInfo& rInfo)
74  {
75  InitSurfaceInfo(rInfo);
76  }
77 
78  size_t get_PitchBytes() const noexcept
79  {
81  return m_Info.get_PitchBytes();
82  }
83  PIXELS_t get_Width() const noexcept
84  {
86  return m_Info.get_Width();
87  }
88  PIXELS_t get_Height() const noexcept
89  {
91  return m_Info.get_Height();
92  }
93  bool isTopDown() const noexcept
94  {
95  return m_Info.isTopDown();
96  }
98  {
99  return m_Info.get_PixelFormat();
100  }
101 
103  bool isValidPixelData() const noexcept
104  {
105  return isValidPtr();
106  }
107  void* get_PixelData() const noexcept
108  {
111  return get_Data();
112  }
113  BYTE* get_PixelBytes() const noexcept
114  {
115  return get_DataBytes();
116  }
117 
118  void put_PixelData(void* pPixelData) noexcept
119  {
122  this->SetBlock(pPixelData, m_Info.get_SurfaceSize());
123  }
124 
125  void* GetLinePtrRaw(PIXELS_t y) const noexcept
126  {
128  return get_PixelBytes() + (m_Info.get_PitchBytes() * y);
129  }
130  BYTE* GetLinePtrByte(PIXELS_t y) const noexcept
131  {
133  return (BYTE*)GetLinePtrRaw(y);
134  }
135  void* GetLinePtr(PIXELS_t y) const noexcept
136  {
139  DEBUG_CHECK(get_PixelData() != nullptr);
140  int cy = m_Info.get_HeightRaw();
141  if (cy < 0) // ! m_Info.isTopDown()
142  {
143  cy = -cy;
144  y = (cy - y) - 1;
145  }
146  DEBUG_CHECK(IS_INDEX_GOOD(y, cy));
147  return GetLinePtrRaw(y);
148  }
149  void* GetNextLine(const void* pLinePtr) const noexcept
150  {
151  INT_PTR nPitchBytes = (INT_PTR)m_Info.get_PitchBytes();
152  if (!m_Info.isTopDown())
153  {
154  nPitchBytes = -nPitchBytes;
155  }
156  return(((BYTE*)pLinePtr) + nPitchBytes);
157  }
158 
159  bool IsValidPixPtr(const void* p) const noexcept
160  {
161  // is this pointer inside my space ?
162  // return IsValidPtr(p);
163  ptrdiff_t i = cMem::Diff(p, get_DataBytes());
164  return IS_INDEX_GOOD(i, m_Info.get_SurfaceSize());
165  }
166 
167  inline BYTE* GetPixPtr(const void* pvLine, PIXELS_t x, size_t sizePixel) const noexcept
168  {
170  DEBUG_CHECK(IS_INDEX_GOOD(x, m_Info.get_Width()));
171  DEBUG_CHECK(IsValidPixPtr(pvLine));
172  DEBUG_CHECK(m_Info.get_BytesPerPixel() == sizePixel);
173  return ((BYTE*)pvLine) + x * sizePixel;
174  }
175  inline BYTE* GetPixPtr(PIXELS_t x, PIXELS_t y, size_t sizePixel) const noexcept
176  {
177  return GetPixPtr(GetLinePtr(y), x, sizePixel);
178  }
179  inline COLOR16_t* GetPixPtr16(PIXELS_t x, PIXELS_t y) const noexcept
180  {
182  return (COLOR16_t*)GetPixPtr(x, y, 2);
183  }
184  inline BYTE* GetPixPtr(PIXELS_t x, PIXELS_t y) const noexcept
185  {
187  BIT_SIZE_t wBpp = m_Info.get_BitsPerPixel();
188  DEBUG_CHECK(wBpp >= 8 || x == 0); // ( x % 8 ) == 0 really is ok
189  return ((BYTE*)GetLinePtr(y)) + ((x * wBpp) / 8);
190  }
191 
192  bool Lock()
193  {
195  return true;
196  }
197  void Unlock()
198  {
200  }
201 
202  inline static bool GetPixColor1P(const void* pvLine, PIXELS_t x) noexcept
203  {
205  return ((const BYTE*)pvLine)[x / 8] & (1 << (7 - (x & 0x7)));
206  }
207  inline static void SetPixColor1P(void* pvLine, PIXELS_t x, bool b) noexcept
208  {
210  BYTE* pb = ((BYTE*)pvLine) + (x / 8);
211  BYTE xb = (1 << (7 - (x & 0x7)));
212  if (b)
213  *pb |= xb;
214  else
215  *pb &= ~xb;
216  }
217  bool GetPixColor1(const void* pvLine, PIXELS_t x) const
218  {
220  ASSERT(m_Info.get_BitsPerPixel() == 1);
221  ASSERT(IsValidPixPtr(pvLine));
222  ASSERT(IS_INDEX_GOOD(x, m_Info.get_Width()));
223  return GetPixColor1P(pvLine, x);
224  }
225  void SetPixColor1(void* pvLine, PIXELS_t x, bool b)
226  {
228  ASSERT(m_Info.get_BitsPerPixel() == 1);
229  ASSERT(IsValidPixPtr(pvLine));
230  ASSERT(IS_INDEX_GOOD(x, m_Info.get_Width()));
231  SetPixColor1P(pvLine, x, b);
232  }
233 
234  // 8 BPP
235  inline BYTE GetPixColor8(const void* pvLine, PIXELS_t x) const noexcept
236  {
238  const BYTE* pPix = GetPixPtr(pvLine, x, 1);
239  return *pPix;
240  }
241  inline void SetPixColor8(void* pvLine, PIXELS_t x, BYTE bColor) noexcept
242  {
244  BYTE* pPix = GetPixPtr(pvLine, x, 1);
245  *pPix = bColor;
246  }
247 
248  // Assume 16 bit per pixel SURF_FORM_RGB565 colors
249  inline COLOR16_t GetPixColor16(const void* pvLine, PIXELS_t x) const noexcept
250  {
253  const COLOR16_t* pPix = (const COLOR16_t*)GetPixPtr(pvLine, x, 2);
254  return *pPix;
255  }
256  inline void SetPixColor16(void* pvLine, PIXELS_t x, COLOR16_t wColor) noexcept
257  {
260  COLOR16_t* pPix = (COLOR16_t*)GetPixPtr(pvLine, x, 2);
261  *pPix = wColor;
262  }
263 
264  // Assume 16 bit grey scale
265  COLOR16_t GetPixColor16G(const void* pvLine, PIXELS_t x) const;
267  {
268  return GetPixColor16G(GetLinePtr(y), x);
269  }
270  void SetPixColor16G(void* pvLine, PIXELS_t x, COLOR16_t wVal);
272  {
273  SetPixColor16G(GetLinePtr(y), x, wVal);
274  }
275 
276  inline COLORREF GetPixColor24g(const void* pvLine, PIXELS_t x) const noexcept
277  {
280  const BYTE* pPix = GetPixPtr(pvLine, x, 3);
281  return RGB(pPix[0], pPix[1], pPix[2]);
282  }
283  inline D3DCOLOR GetPixColor24d(const void* pvLine, PIXELS_t x) const noexcept
284  {
287  const BYTE* pPix = GetPixPtr(pvLine, x, 3);
288  return RGB(pPix[2], pPix[1], pPix[0]);
289  }
290  inline void SetPixColor24g(void* pvLine, PIXELS_t x, COLORREF dwColor) noexcept
291  {
294  BYTE* pPix = GetPixPtr(pvLine, x, 3);
295  pPix[0] = cColorRef::GetR(dwColor);
296  pPix[1] = cColorRef::GetG(dwColor);
297  pPix[2] = cColorRef::GetB(dwColor);
298  }
299  inline void SetPixColor24d(void* pvLine, PIXELS_t x, D3DCOLOR dwColor) noexcept
300  {
303  BYTE* pPix = GetPixPtr(pvLine, x, 3);
304  pPix[0] = cColorRef::GetB(dwColor);
305  pPix[1] = cColorRef::GetG(dwColor);
306  pPix[2] = cColorRef::GetR(dwColor);
307  }
308 
309  inline COLORREF GetPixColor32(const void* pvLine, PIXELS_t x) const noexcept
310  {
312  const COLORREF* pPix = (const COLORREF*)GetPixPtr(pvLine, x, 4);
313  return *pPix;
314  }
315  inline void SetPixColor32(void* pvLine, PIXELS_t x, COLORREF dwColor) noexcept
316  {
318  COLORREF* pPix = (COLORREF*)GetPixPtr(pvLine, x, 4);
319  *pPix = dwColor;
320  }
321 
322  // convert FROM ANY BPP
323  COLORREF GetPixColorRef(const void* pvLine, PIXELS_t x) const; // ABGR
325  {
326  return GetPixColorRef(GetLinePtr(y), x);
327  }
328  void SetPixColorRef(void* pvLine, PIXELS_t x, COLORREF c); // ABGR
330  {
331  SetPixColorRef(GetLinePtr(y), x, c);
332  }
333 
334  void ScrollPixels(PIXELS_t cx, PIXELS_t cy);
335  void FillColor(COLORREF colorBack);
336  void SwapTopBottom();
337 
338  void SetErase()
339  {
340  FillColor(0);
341  }
342  virtual HRESULT SetPixelData(const cSurfaceBase& rSurfSrc);
343  virtual HRESULT SetCopySurface(const cSurfaceBase& rSurfSrc) // virtual
344  {
346  return SetPixelData(rSurfSrc);
347  }
348 
349  void SetCopyFrom(const cSurfaceBase& TexSrc, PIXELS_t itxs = 0, PIXELS_t itys = 0, bool bTextureXReverse = false, bool bTextureYReverse = false, bool bColorReverse = false);
350  void CopyTo(cSurfaceBase& TexDst, PIXELS_t itxs = 0, PIXELS_t itys = 0, bool bTextureXReverse = false, bool bTextureYReverse = false) const;
351 
352 #ifdef _WIN32
353  static HBITMAP GRAYCALL CreateBitmapFromPixels(const tagBITMAPINFO* pBMI, const void FAR* pBits);
354  HBITMAP CreateBitmapFromSurface() const;
355  int FlipToDC(HDC hDC, PIXELS_t XDest = 0, PIXELS_t YDest = 0) const;
356  int StretchToDC(HDC hDC, PIXELS_t XDest, PIXELS_t YDest, PIXELS_t nDestWidth, PIXELS_t nDestHeight) const;
357  HRGN CreateMaskRegion(COLOR16_t wTransparentColor);
358 #endif
359 
360  HRESULT InitTestPattern();
361 
363  };
364 
366  {
372 
373  public:
376 
378  size_t m_nFrameOffset;
380 
381  public:
382  cSurfaceFrames() noexcept
383  : m_SizeOrig(0, 0)
384  , m_nFrameQty(0)
385  , m_nFrameCur(0)
386  , m_nFrameOffset(0)
387  {
388  m_ptFrameOffset.x = 0;
389  m_ptFrameOffset.y = 0;
390  }
391 
392  void InitFrameCur() noexcept
393  {
395  m_nFrameCur = 0;
396  m_nFrameOffset = 0;
397  m_ptFrameOffset.x = 0;
398  m_ptFrameOffset.y = 0;
399  }
400 
401  virtual HRESULT SetFrameCurrent(int iFrameNum) = 0;
402  };
403 }
404 #endif // _INC_cSurfaceBase_H
#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 IS_INDEX_GOOD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:35
#define IS_TYPE_OF(t, p)
Definition: PtrCast.h:23
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
DWORD D3DCOLOR
Stuff normally defined in windows.h or DirectX headers.
Definition: cColorRef.h:24
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
UINT32 COLORREF
ABGR (high to low bits)
Definition: cVariantData.h:21
static COLOR_ELEM_t GetG(COLORREF color) noexcept
Definition: cColorRef.h:93
static COLOR_ELEM_t GetB(COLORREF color) noexcept
Definition: cColorRef.h:98
static COLOR_ELEM_t GetR(COLORREF color) noexcept
Definition: cColorRef.h:88
Definition: cSurfaceBase.h:26
void SetPixColor1(void *pvLine, PIXELS_t x, bool b)
Definition: cSurfaceBase.h:225
COLOR16_t GetPixColor16(const void *pvLine, PIXELS_t x) const noexcept
Definition: cSurfaceBase.h:249
virtual HRESULT SetCopySurface(const cSurfaceBase &rSurfSrc)
Definition: cSurfaceBase.h:343
const cSurfaceInfo & get_Info() const noexcept
Definition: cSurfaceBase.h:69
void SetPixColor24d(void *pvLine, PIXELS_t x, D3DCOLOR dwColor) noexcept
Definition: cSurfaceBase.h:299
UNITTEST_FRIEND(cSurfaceBase)
BYTE GetPixColor8(const void *pvLine, PIXELS_t x) const noexcept
Definition: cSurfaceBase.h:235
bool IsValidPixPtr(const void *p) const noexcept
Definition: cSurfaceBase.h:159
void * GetLinePtrRaw(PIXELS_t y) const noexcept
Definition: cSurfaceBase.h:125
BYTE * GetPixPtr(const void *pvLine, PIXELS_t x, size_t sizePixel) const noexcept
Definition: cSurfaceBase.h:167
void SetErase()
Definition: cSurfaceBase.h:338
BYTE * get_PixelBytes() const noexcept
Definition: cSurfaceBase.h:113
COLORREF GetPixColor24g(const void *pvLine, PIXELS_t x) const noexcept
Definition: cSurfaceBase.h:276
void SetPixColor32(void *pvLine, PIXELS_t x, COLORREF dwColor) noexcept
Definition: cSurfaceBase.h:315
COLOR16_t GetPixColor16G(PIXELS_t x, PIXELS_t y) const
Definition: cSurfaceBase.h:266
PIXELS_t get_Height() const noexcept
Definition: cSurfaceBase.h:88
void SetPixColor8(void *pvLine, PIXELS_t x, BYTE bColor) noexcept
Definition: cSurfaceBase.h:241
void put_Info(const cSurfaceInfo &rInfo)
Definition: cSurfaceBase.h:73
void * GetNextLine(const void *pLinePtr) const noexcept
Definition: cSurfaceBase.h:149
COLORREF GetPixColor32(const void *pvLine, PIXELS_t x) const noexcept
Definition: cSurfaceBase.h:309
SURF_FORM_TYPE get_PixelFormat() const noexcept
Definition: cSurfaceBase.h:97
void SetPixColorRef(PIXELS_t x, PIXELS_t y, COLORREF c)
Definition: cSurfaceBase.h:329
COLOR16_t * GetPixPtr16(PIXELS_t x, PIXELS_t y) const noexcept
Definition: cSurfaceBase.h:179
cSurfaceInfo m_Info
the image size, pitch and pixel format for m_pPixelData below.
Definition: cSurfaceBase.h:37
BYTE * GetPixPtr(PIXELS_t x, PIXELS_t y, size_t sizePixel) const noexcept
Definition: cSurfaceBase.h:175
COLORREF GetPixColorRef(PIXELS_t x, PIXELS_t y) const
Definition: cSurfaceBase.h:324
static void SetPixColor1P(void *pvLine, PIXELS_t x, bool b) noexcept
Definition: cSurfaceBase.h:207
void SetPixColor24g(void *pvLine, PIXELS_t x, COLORREF dwColor) noexcept
Definition: cSurfaceBase.h:290
BYTE * GetLinePtrByte(PIXELS_t y) const noexcept
Definition: cSurfaceBase.h:130
void * GetLinePtr(PIXELS_t y) const noexcept
Definition: cSurfaceBase.h:135
void put_PixelData(void *pPixelData) noexcept
Definition: cSurfaceBase.h:118
BYTE * GetPixPtr(PIXELS_t x, PIXELS_t y) const noexcept
Definition: cSurfaceBase.h:184
D3DCOLOR GetPixColor24d(const void *pvLine, PIXELS_t x) const noexcept
Definition: cSurfaceBase.h:283
void SetPixColor16G(PIXELS_t x, PIXELS_t y, COLOR16_t wVal)
Definition: cSurfaceBase.h:271
PIXELS_t get_Width() const noexcept
Definition: cSurfaceBase.h:83
size_t get_PitchBytes() const noexcept
Definition: cSurfaceBase.h:78
static bool GetPixColor1P(const void *pvLine, PIXELS_t x) noexcept
Definition: cSurfaceBase.h:202
bool isTopDown() const noexcept
Definition: cSurfaceBase.h:93
void SetPixColor16(void *pvLine, PIXELS_t x, COLOR16_t wColor) noexcept
Definition: cSurfaceBase.h:256
void Unlock()
Definition: cSurfaceBase.h:197
bool Lock()
Definition: cSurfaceBase.h:192
void * get_PixelData() const noexcept
Definition: cSurfaceBase.h:107
bool GetPixColor1(const void *pvLine, PIXELS_t x) const
Definition: cSurfaceBase.h:217
bool isValidPixelData() const noexcept
basic attributes.
Definition: cSurfaceBase.h:103
Definition: cSurfaceDC.h:121
Definition: cSurfaceDC.h:50
Definition: cSurfaceBase.h:366
cWinSize m_SizeOrig
Original MAX reference image size. NOT the current frame size.
Definition: cSurfaceBase.h:374
int m_nFrameCur
the enum of the current locked component/frame. < m_nFrameQty
Definition: cSurfaceBase.h:377
int m_nFrameQty
max components/frames in the file (DDS,GIF,JP2,PNG) (NOTE doesn't know how many frames until reach en...
Definition: cSurfaceBase.h:375
POINT m_ptFrameOffset
Current Frame offset in m_SizeOrig.
Definition: cSurfaceBase.h:379
virtual HRESULT SetFrameCurrent(int iFrameNum)=0
size_t m_nFrameOffset
offset in bytes from the start to m_nFrameCur frame data.
Definition: cSurfaceBase.h:378
void InitFrameCur() noexcept
Definition: cSurfaceBase.h:392
cSurfaceFrames() noexcept
Definition: cSurfaceBase.h:382
Definition: cSurfaceInfo.h:109
bool isTopDown() const noexcept
Definition: cSurfaceInfo.h:166
size_t get_BytesPerPixel() const noexcept
Definition: cSurfaceInfo.h:202
PIXELS_t get_HeightRaw() const noexcept
Definition: cSurfaceInfo.h:161
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
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
Definition: WinTypes.h:128
Definition: cMem.h:311
Definition: cStream.h:126
Definition: cMesh.h:22
WORD COLOR16_t
A 16 bit greyscale color.
Definition: cColorRef.h:39
UNITTEST2_PREDEF(cQuadtree)
SURF_FORM_TYPE
Definition: cSurfaceInfo.h:24
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
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
static ptrdiff_t Diff(const void *pEnd, const void *pStart) noexcept
Definition: cMem.h:34