bitmap.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/osx/bitmap.h
  3. // Purpose: wxBitmap class
  4. // Author: Stefan Csomor
  5. // Modified by:
  6. // Created: 1998-01-01
  7. // Copyright: (c) Stefan Csomor
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_BITMAP_H_
  11. #define _WX_BITMAP_H_
  12. #include "wx/palette.h"
  13. // Bitmap
  14. class WXDLLIMPEXP_FWD_CORE wxBitmap;
  15. class wxBitmapRefData ;
  16. class WXDLLIMPEXP_FWD_CORE wxBitmapHandler;
  17. class WXDLLIMPEXP_FWD_CORE wxControl;
  18. class WXDLLIMPEXP_FWD_CORE wxCursor;
  19. class WXDLLIMPEXP_FWD_CORE wxDC;
  20. class WXDLLIMPEXP_FWD_CORE wxIcon;
  21. class WXDLLIMPEXP_FWD_CORE wxImage;
  22. class WXDLLIMPEXP_FWD_CORE wxPixelDataBase;
  23. // A mask is a bitmap used for drawing bitmaps
  24. // Internally it is stored as a 8 bit deep memory chunk, 0 = black means the source will be drawn
  25. // 255 = white means the source will not be drawn, no other values will be present
  26. // 8 bit is chosen only for performance reasons, note also that this is the inverse value range
  27. // from alpha, where 0 = invisible , 255 = fully drawn
  28. class WXDLLIMPEXP_CORE wxMask: public wxObject
  29. {
  30. DECLARE_DYNAMIC_CLASS(wxMask)
  31. public:
  32. wxMask();
  33. // Copy constructor
  34. wxMask(const wxMask& mask);
  35. // Construct a mask from a bitmap and a colour indicating
  36. // the transparent area
  37. wxMask(const wxBitmap& bitmap, const wxColour& colour);
  38. // Construct a mask from a mono bitmap (black meaning show pixels, white meaning transparent)
  39. wxMask(const wxBitmap& bitmap);
  40. // implementation helper only : construct a mask from a 32 bit memory buffer
  41. wxMask(const wxMemoryBuffer& buf, int width , int height , int bytesPerRow ) ;
  42. virtual ~wxMask();
  43. bool Create(const wxBitmap& bitmap, const wxColour& colour);
  44. bool Create(const wxBitmap& bitmap);
  45. bool Create(const wxMemoryBuffer& buf, int width , int height , int bytesPerRow ) ;
  46. wxBitmap GetBitmap() const;
  47. // Implementation below
  48. void Init() ;
  49. // a 8 bit depth mask
  50. void* GetRawAccess() const;
  51. int GetBytesPerRow() const { return m_bytesPerRow ; }
  52. // renders/updates native representation when necessary
  53. void RealizeNative() ;
  54. WXHBITMAP GetHBITMAP() const ;
  55. private:
  56. wxMemoryBuffer m_memBuf ;
  57. int m_bytesPerRow ;
  58. int m_width ;
  59. int m_height ;
  60. WXHBITMAP m_maskBitmap ;
  61. };
  62. class WXDLLIMPEXP_CORE wxBitmap: public wxBitmapBase
  63. {
  64. DECLARE_DYNAMIC_CLASS(wxBitmap)
  65. friend class WXDLLIMPEXP_FWD_CORE wxBitmapHandler;
  66. public:
  67. wxBitmap() {} // Platform-specific
  68. // Initialize with raw data.
  69. wxBitmap(const char bits[], int width, int height, int depth = 1);
  70. // Initialize with XPM data
  71. wxBitmap(const char* const* bits);
  72. // Load a file or resource
  73. wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
  74. // Constructor for generalised creation from data
  75. wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth = 1);
  76. // creates an bitmap from the native image format
  77. wxBitmap(CGImageRef image, double scale = 1.0);
  78. wxBitmap(WX_NSImage image);
  79. wxBitmap(CGContextRef bitmapcontext);
  80. // Create a bitmap compatible with the given DC
  81. wxBitmap(int width, int height, const wxDC& dc);
  82. // If depth is omitted, will create a bitmap compatible with the display
  83. wxBitmap(int width, int height, int depth = -1) { (void)Create(width, height, depth); }
  84. wxBitmap(const wxSize& sz, int depth = -1) { (void)Create(sz, depth); }
  85. // Convert from wxImage:
  86. wxBitmap(const wxImage& image, int depth = -1, double scale = 1.0);
  87. // Convert from wxIcon
  88. wxBitmap(const wxIcon& icon) { CopyFromIcon(icon); }
  89. virtual ~wxBitmap() {}
  90. wxImage ConvertToImage() const;
  91. // get the given part of bitmap
  92. wxBitmap GetSubBitmap( const wxRect& rect ) const;
  93. virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
  94. virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH)
  95. { return Create(sz.GetWidth(), sz.GetHeight(), depth); }
  96. virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1);
  97. bool Create( CGImageRef image, double scale = 1.0 );
  98. bool Create( WX_NSImage image );
  99. bool Create( CGContextRef bitmapcontext);
  100. // Create a bitmap compatible with the given DC, inheriting its magnification factor
  101. bool Create(int width, int height, const wxDC& dc);
  102. // Create a bitmap with a scale factor, width and height are multiplied with that factor
  103. bool CreateScaled(int logwidth, int logheight, int depth, double logicalScale);
  104. // virtual bool Create( WXHICON icon) ;
  105. virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
  106. virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const;
  107. wxBitmapRefData *GetBitmapData() const
  108. { return (wxBitmapRefData *)m_refData; }
  109. // copies the contents and mask of the given (colour) icon to the bitmap
  110. virtual bool CopyFromIcon(const wxIcon& icon);
  111. int GetWidth() const;
  112. int GetHeight() const;
  113. int GetDepth() const;
  114. void SetWidth(int w);
  115. void SetHeight(int h);
  116. void SetDepth(int d);
  117. void SetOk(bool isOk);
  118. #if wxUSE_PALETTE
  119. wxPalette* GetPalette() const;
  120. void SetPalette(const wxPalette& palette);
  121. #endif // wxUSE_PALETTE
  122. wxMask *GetMask() const;
  123. void SetMask(wxMask *mask) ;
  124. static void InitStandardHandlers();
  125. // raw bitmap access support functions, for internal use only
  126. void *GetRawData(wxPixelDataBase& data, int bpp);
  127. void UngetRawData(wxPixelDataBase& data);
  128. // these functions are internal and shouldn't be used, they risk to
  129. // disappear in the future
  130. bool HasAlpha() const;
  131. void UseAlpha();
  132. // returns the 'native' implementation, a GWorldPtr for the content and one for the mask
  133. WXHBITMAP GetHBITMAP( WXHBITMAP * mask = NULL ) const;
  134. // returns a CGImageRef which must released after usage with CGImageRelease
  135. CGImageRef CreateCGImage() const ;
  136. #if wxOSX_USE_COCOA
  137. // returns an autoreleased version of the image
  138. WX_NSImage GetNSImage() const;
  139. #endif
  140. #if wxOSX_USE_IPHONE
  141. // returns an autoreleased version of the image
  142. WX_UIImage GetUIImage() const;
  143. #endif
  144. // returns a IconRef which must be retained before and released after usage
  145. IconRef GetIconRef() const;
  146. // returns a IconRef which must be released after usage
  147. IconRef CreateIconRef() const;
  148. // get read only access to the underlying buffer
  149. void *GetRawAccess() const ;
  150. // brackets to the underlying OS structure for read/write access
  151. // makes sure that no cached images will be constructed until terminated
  152. void *BeginRawAccess() ;
  153. void EndRawAccess() ;
  154. double GetScaleFactor() const;
  155. protected:
  156. virtual wxGDIRefData *CreateGDIRefData() const;
  157. virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
  158. };
  159. #endif // _WX_BITMAP_H_