DIBSectionLite.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #if !defined(AFX_CDIBSECTIONLITE_H__35D9F3D4_B960_11D2_A981_2C4476000000__INCLUDED_)
  2. #define AFX_CDIBSECTIONLITE_H__35D9F3D4_B960_11D2_A981_2C4476000000__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. // DIBSectionLite.h : header file
  7. //
  8. // Copyright ?Dundas Software Ltd. 1999, All Rights Reserved
  9. // //////////////////////////////////////////////////////////////////////////
  10. // Properties:
  11. // NO Abstract class (does not have any objects)
  12. // NO Derived from CWnd
  13. // NO Is a CWnd.
  14. // NO Two stage creation (constructor & Create())
  15. // NO Has a message map
  16. // NO Needs a resource (template)
  17. // YES Persistent objects (saveable on disk)
  18. // YES Uses exceptions
  19. // //////////////////////////////////////////////////////////////////////////
  20. // Desciption :
  21. // DIBSection is DIBSection wrapper class for Win32 platforms.
  22. // This class provides a simple interface to DIBSections including loading,
  23. // saving and displaying DIBsections.
  24. //
  25. // Using DIBSection :
  26. // This class is very simple to use. The bitmap can be set using either SetBitmap()
  27. // (which accepts either a Device dependant or device independant bitmap, or a
  28. // resource ID) or by using Load(), which allows an image to be loaded from disk.
  29. // To display the bitmap simply use Draw or Stretch.
  30. //
  31. // eg.
  32. //
  33. // CDIBSectionLite dibsection;
  34. // dibsection.Load(_T("image.bmp"));
  35. // dibsection.Draw(pDC, CPoint(0,0)); // pDC is of type CDC*
  36. //
  37. // CDIBSectionLite dibsection;
  38. // dibsection.SetBitmap(IDB_BITMAP);
  39. // dibsection.Draw(pDC, CPoint(0,0)); // pDC is of type CDC*
  40. //
  41. // The CDIBSectionLite API includes many methods to extract information about the
  42. // image, as well as palette options for getting and setting the current palette.
  43. //
  44. // Author: Chris Maunder (cmaunder@dundas.com)
  45. // Date : 12 April 1999
  46. //#include <afx.h>
  47. #include <afxctl.h>
  48. #include <afxwin.h>
  49. #include <vfw.h>
  50. #pragma comment(lib, "vfw32")
  51. /////////////////////////////////////////////////////////////////////////////
  52. // defines
  53. #define DS_BITMAP_FILEMARKER ((WORD) ('M' << 8) | 'B') // is always "BM" = 0x4D42
  54. #define BMGRADIENT_DIRECTION_HORZ 0
  55. #define BMGRADIENT_DIRECTION_VERT 1
  56. /////////////////////////////////////////////////////////////////////////////
  57. // BITMAPINFO wrapper
  58. struct DIBINFO : public BITMAPINFO
  59. {
  60. RGBQUAD arColors[255]; // Color table info - adds an extra 255 entries to palette
  61. operator LPBITMAPINFO() { return (LPBITMAPINFO) this; }
  62. operator LPBITMAPINFOHEADER() { return &bmiHeader; }
  63. RGBQUAD* ColorTable() { return bmiColors; }
  64. };
  65. /////////////////////////////////////////////////////////////////////////////
  66. // LOGPALETTE wrapper
  67. struct PALETTEINFO : public LOGPALETTE
  68. {
  69. PALETTEENTRY arPalEntries[255]; // Palette entries
  70. PALETTEINFO() { palVersion = (WORD) 0x300; }
  71. operator LPLOGPALETTE() { return (LPLOGPALETTE) this; }
  72. operator LPPALETTEENTRY() { return (LPPALETTEENTRY) (palPalEntry); }
  73. };
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CDIBSectionLite object
  76. class CDIBSectionLite : public CObject
  77. {
  78. // Construction
  79. public:
  80. CDIBSectionLite();
  81. virtual ~CDIBSectionLite();
  82. void DeleteObject();
  83. void ClearObject();
  84. // static helpers
  85. public:
  86. static int BytesPerLine(int nWidth, int nBitsPerPixel);
  87. static int NumColorEntries(int nBitsPerPixel);
  88. static PALETTEENTRY ms_StdColours[];
  89. static BOOL UsesPalette(CDC* pDC) { return (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE); }
  90. static BOOL CreateHalftonePalette(CPalette& palette, int nNumColours);
  91. // Attributes
  92. public:
  93. HBITMAP GetSafeHandle() const { return (this)? m_hBitmap : NULL; }
  94. CSize GetSize() const { return CSize(GetWidth(), GetHeight()); }
  95. int GetHeight() const { return m_DIBinfo.bmiHeader.biHeight; }
  96. int GetWidth() const { return m_DIBinfo.bmiHeader.biWidth; }
  97. int GetPlanes() const { return m_DIBinfo.bmiHeader.biPlanes; }
  98. int GetBitCount() const { return m_DIBinfo.bmiHeader.biBitCount; }
  99. LPVOID GetDIBits() { return m_ppvBits; }
  100. LPBITMAPINFO GetBitmapInfo() { return (BITMAPINFO*) m_DIBinfo; }
  101. DWORD GetImageSize() const { return m_DIBinfo.bmiHeader.biSizeImage; }
  102. LPBITMAPINFOHEADER GetBitmapInfoHeader() { return (BITMAPINFOHEADER*) m_DIBinfo; }
  103. void CreateGradientBmp (COLORREF clrBack,
  104. COLORREF clrStart,
  105. COLORREF clrEnd,
  106. int iWidth,
  107. int iHeight,
  108. int iDirection);
  109. void Create32BitFromPicture (CPictureHolder* pPicture,
  110. int iWidth,
  111. int iHeight);
  112. COLORREF FixColorRef (COLORREF clr);
  113. BOOL SetBitmap(UINT nIDResource);
  114. BOOL SetBitmap(LPCTSTR lpszResourceName);
  115. BOOL SetBitmap(HBITMAP hBitmap, CPalette* pPalette = NULL);
  116. BOOL SetBitmap(LPBITMAPINFO lpBitmapInfo, LPVOID lpBits);
  117. CPalette *GetPalette() { return &m_Palette; }
  118. BOOL SetPalette(CPalette* pPalette);
  119. BOOL SetLogPalette(LOGPALETTE* pLogPalette);
  120. BOOL SetDither(BOOL bDither);
  121. BOOL GetDither();
  122. // Operations
  123. public:
  124. BOOL Load(LPCTSTR lpszFileName);
  125. BOOL Save(LPCTSTR lpszFileName);
  126. BOOL Draw(CDC* pDC, CPoint ptDest, BOOL bForceBackground = FALSE);
  127. BOOL Stretch(CDC* pDC, CPoint ptDest, CSize size, BOOL bForceBackground = FALSE);
  128. // Overrideables
  129. // Implementation
  130. public:
  131. #ifdef _DEBUG
  132. virtual void AssertValid() const;
  133. virtual void Dump(CDumpContext& dc) const;
  134. #endif
  135. // Implementation
  136. protected:
  137. void _ShowLastError();
  138. BOOL CreatePalette();
  139. BOOL FillDIBColorTable(UINT nNumColours, RGBQUAD *pRGB);
  140. protected:
  141. HBITMAP m_hBitmap; // Handle to DIBSECTION
  142. DIBINFO m_DIBinfo; // Bitmap header & color table info
  143. DWORD *m_pdwBits;
  144. VOID *m_ppvBits; // Pointer to bitmap bits
  145. UINT m_iColorDataType; // color data type (palette or RGB values)
  146. UINT m_iColorTableSize; // Size of color table
  147. CPalette m_Palette; // Color palette
  148. BOOL m_bDither; // Use DrawDib routines for dithering?
  149. HDRAWDIB m_hDrawDib; // handle to a DrawDib DC
  150. private:
  151. HBITMAP m_hOldBitmap; // Storage for previous bitmap in Memory DC
  152. };
  153. /////////////////////////////////////////////////////////////////////////////
  154. //{{AFX_INSERT_LOCATION}}
  155. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  156. #endif // !defined(AFX_CDIBSECTIONLITE_H__35D9F3D4_B960_11D2_A981_2C4476000000__INCLUDED_)