dib.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/dib.h
  3. // Purpose: wxDIB class representing Win32 device independent bitmaps
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 03.03.03 (replaces the old file with the same name)
  7. // Copyright: (c) 1997-2003 wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSW_DIB_H_
  11. #define _WX_MSW_DIB_H_
  12. class WXDLLIMPEXP_FWD_CORE wxPalette;
  13. #include "wx/msw/private.h"
  14. #if wxUSE_WXDIB
  15. #ifdef __WXMSW__
  16. #include "wx/bitmap.h"
  17. #endif // __WXMSW__
  18. // ----------------------------------------------------------------------------
  19. // wxDIB: represents a DIB section
  20. // ----------------------------------------------------------------------------
  21. class WXDLLIMPEXP_CORE wxDIB
  22. {
  23. public:
  24. // ctors and such
  25. // --------------
  26. // create an uninitialized DIB with the given width, height and depth (only
  27. // 24 and 32 bpp DIBs are currently supported)
  28. //
  29. // after using this ctor, GetData() and GetHandle() may be used if IsOk()
  30. // returns true
  31. wxDIB(int width, int height, int depth)
  32. { Init(); (void)Create(width, height, depth); }
  33. #ifdef __WXMSW__
  34. // create a DIB from the DDB
  35. wxDIB(const wxBitmap& bmp)
  36. { Init(); (void)Create(bmp); }
  37. #endif // __WXMSW__
  38. // create a DIB from the Windows DDB
  39. wxDIB(HBITMAP hbmp)
  40. { Init(); (void)Create(hbmp); }
  41. // load a DIB from file (any depth is supoprted here unlike above)
  42. //
  43. // as above, use IsOk() to see if the bitmap was loaded successfully
  44. wxDIB(const wxString& filename)
  45. { Init(); (void)Load(filename); }
  46. // same as the corresponding ctors but with return value
  47. bool Create(int width, int height, int depth);
  48. #ifdef __WXMSW__
  49. bool Create(const wxBitmap& bmp) { return Create(GetHbitmapOf(bmp)); }
  50. #endif
  51. bool Create(HBITMAP hbmp);
  52. bool Load(const wxString& filename);
  53. // dtor is not virtual, this class is not meant to be used polymorphically
  54. ~wxDIB();
  55. // operations
  56. // ----------
  57. #ifndef __WXWINCE__
  58. // create a bitmap compatible with the given HDC (or screen by default) and
  59. // return its handle, the caller is responsible for freeing it (using
  60. // DeleteObject())
  61. HBITMAP CreateDDB(HDC hdc = 0) const;
  62. #endif // !__WXWINCE__
  63. // get the handle from the DIB and reset it, i.e. this object won't destroy
  64. // the DIB after this (but the caller should do it)
  65. HBITMAP Detach() { HBITMAP hbmp = m_handle; m_handle = 0; return hbmp; }
  66. #if wxUSE_PALETTE
  67. // create a palette for this DIB (always a trivial/default one for 24bpp)
  68. wxPalette *CreatePalette() const;
  69. #endif // wxUSE_PALETTE
  70. // save the DIB as a .BMP file to the file with the given name
  71. bool Save(const wxString& filename);
  72. // accessors
  73. // ---------
  74. // return true if DIB was successfully created, false otherwise
  75. bool IsOk() const { return m_handle != 0; }
  76. // get the bitmap size
  77. wxSize GetSize() const { DoGetObject(); return wxSize(m_width, m_height); }
  78. int GetWidth() const { DoGetObject(); return m_width; }
  79. int GetHeight() const { DoGetObject(); return m_height; }
  80. // get the number of bits per pixel, or depth
  81. int GetDepth() const { DoGetObject(); return m_depth; }
  82. // get the DIB handle
  83. HBITMAP GetHandle() const { return m_handle; }
  84. // get raw pointer to bitmap bits, you should know what you do if you
  85. // decide to use it
  86. unsigned char *GetData() const
  87. { DoGetObject(); return (unsigned char *)m_data; }
  88. // HBITMAP conversion
  89. // ------------------
  90. // these functions are only used by wxWidgets internally right now, please
  91. // don't use them directly if possible as they're subject to change
  92. #ifndef __WXWINCE__
  93. // creates a DDB compatible with the given (or screen) DC from either
  94. // a plain DIB or a DIB section (in which case the last parameter must be
  95. // non NULL)
  96. static HBITMAP ConvertToBitmap(const BITMAPINFO *pbi,
  97. HDC hdc = 0,
  98. void *bits = NULL);
  99. // create a plain DIB (not a DIB section) from a DDB, the caller is
  100. // responsable for freeing it using ::GlobalFree()
  101. static HGLOBAL ConvertFromBitmap(HBITMAP hbmp);
  102. // creates a DIB from the given DDB or calculates the space needed by it:
  103. // if pbi is NULL, only the space is calculated, otherwise pbi is supposed
  104. // to point at BITMAPINFO of the correct size which is filled by this
  105. // function (this overload is needed for wxBitmapDataObject code in
  106. // src/msw/ole/dataobj.cpp)
  107. static size_t ConvertFromBitmap(BITMAPINFO *pbi, HBITMAP hbmp);
  108. #endif // __WXWINCE__
  109. // wxImage conversion
  110. // ------------------
  111. #if wxUSE_IMAGE
  112. // Possible formats for DIBs created by the functions below.
  113. enum PixelFormat
  114. {
  115. PixelFormat_PreMultiplied = 0,
  116. PixelFormat_NotPreMultiplied = 1
  117. };
  118. // Create a DIB from the given image, the DIB will be either 24 or 32 (if
  119. // the image has alpha channel) bpp.
  120. //
  121. // By default the DIB stores pixel data in pre-multiplied format so that it
  122. // can be used with ::AlphaBlend() but it is also possible to disable
  123. // pre-multiplication for the DIB to be usable with ImageList_Draw() which
  124. // does pre-multiplication internally.
  125. wxDIB(const wxImage& image, PixelFormat pf = PixelFormat_PreMultiplied)
  126. {
  127. Init();
  128. (void)Create(image, pf);
  129. }
  130. // same as the above ctor but with the return code
  131. bool Create(const wxImage& image, PixelFormat pf = PixelFormat_PreMultiplied);
  132. // create wxImage having the same data as this DIB
  133. wxImage ConvertToImage() const;
  134. #endif // wxUSE_IMAGE
  135. // helper functions
  136. // ----------------
  137. // return the size of one line in a DIB with given width and depth: the
  138. // point here is that as the scan lines need to be DWORD aligned so we may
  139. // need to add some padding
  140. static unsigned long GetLineSize(int width, int depth)
  141. {
  142. return ((width*depth + 31) & ~31) >> 3;
  143. }
  144. private:
  145. // common part of all ctors
  146. void Init();
  147. // free resources
  148. void Free();
  149. // initialize the contents from the provided DDB (Create() must have been
  150. // already called)
  151. bool CopyFromDDB(HBITMAP hbmp);
  152. // the DIB section handle, 0 if invalid
  153. HBITMAP m_handle;
  154. // NB: we could store only m_handle and not any of the other fields as
  155. // we may always retrieve them from it using ::GetObject(), but we
  156. // decide to still store them for efficiency concerns -- however if we
  157. // don't have them from the very beginning (e.g. DIB constructed from a
  158. // bitmap), we only retrieve them when necessary and so these fields
  159. // should *never* be accessed directly, even from inside wxDIB code
  160. // function which must be called before accessing any members and which
  161. // gets their values from m_handle, if not done yet
  162. void DoGetObject() const;
  163. // pointer to DIB bits, may be NULL
  164. void *m_data;
  165. // size and depth of the image
  166. int m_width,
  167. m_height,
  168. m_depth;
  169. // in some cases we could be using a handle which we didn't create and in
  170. // this case we shouldn't free it neither -- this flag tell us if this is
  171. // the case
  172. bool m_ownsHandle;
  173. // DIBs can't be copied
  174. wxDIB(const wxDIB&);
  175. wxDIB& operator=(const wxDIB&);
  176. };
  177. // ----------------------------------------------------------------------------
  178. // inline functions implementation
  179. // ----------------------------------------------------------------------------
  180. inline
  181. void wxDIB::Init()
  182. {
  183. m_handle = 0;
  184. m_ownsHandle = true;
  185. m_data = NULL;
  186. m_width =
  187. m_height =
  188. m_depth = 0;
  189. }
  190. inline
  191. void wxDIB::Free()
  192. {
  193. if ( m_handle && m_ownsHandle )
  194. {
  195. if ( !::DeleteObject(m_handle) )
  196. {
  197. wxLogLastError(wxT("DeleteObject(hDIB)"));
  198. }
  199. Init();
  200. }
  201. }
  202. inline wxDIB::~wxDIB()
  203. {
  204. Free();
  205. }
  206. #endif
  207. // wxUSE_WXDIB
  208. #endif // _WX_MSW_DIB_H_