1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.cpp
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/palette.h"
19 #include "wx/bitmap.h"
21 #include "wx/xpmdecod.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #import <AppKit/NSBitmapImageRep.h>
27 #import <AppKit/NSGraphics.h>
29 // ========================================================================
31 // ========================================================================
32 class wxBitmapRefData: public wxGDIRefData
34 friend class wxBitmap;
37 wxBitmapRefData( const wxBitmapRefData& data );
38 virtual ~wxBitmapRefData();
46 wxPalette m_bitmapPalette;
48 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
49 wxMask *m_bitmapMask; // Optional mask
52 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
54 wxBitmapRefData::wxBitmapRefData()
62 m_cocoaNSBitmapImageRep = nil;
66 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
68 m_width = data.m_width;
69 m_height = data.m_height;
70 m_depth = data.m_depth;
72 m_numColors = data.m_numColors;
73 m_bitmapPalette = data.m_bitmapPalette;
74 m_quality = data.m_quality;
75 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
76 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
79 wxBitmapRefData::~wxBitmapRefData()
81 [m_cocoaNSBitmapImageRep release];
82 m_cocoaNSBitmapImageRep = NULL;
88 // ========================================================================
90 // ========================================================================
91 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
102 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
104 m_refData = new wxBitmapRefData;
106 M_BITMAPDATA->m_width = the_width ;
107 M_BITMAPDATA->m_height = the_height ;
108 M_BITMAPDATA->m_depth = no_bits ;
109 M_BITMAPDATA->m_numColors = 0;
111 /* TODO: create the bitmap from data */
114 wxBitmap::wxBitmap(int w, int h, int d)
116 (void)Create(w, h, d);
119 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
121 (void) Create(data, type, width, height, depth);
124 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
126 LoadFile(filename, type);
129 wxObjectRefData *wxBitmap::CreateRefData() const
131 return new wxBitmapRefData;
134 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
136 return new wxBitmapRefData(*(wxBitmapRefData*)data);
139 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
143 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
146 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
150 // NOTE: No checking is done to make sure width/height agree
151 [bitmapImageRep retain];
152 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
153 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
156 void wxBitmap::SetWidth(int w)
159 m_refData = new wxBitmapRefData;
161 M_BITMAPDATA->m_width = w;
164 void wxBitmap::SetHeight(int h)
167 m_refData = new wxBitmapRefData;
169 M_BITMAPDATA->m_height = h;
172 void wxBitmap::SetDepth(int d)
175 m_refData = new wxBitmapRefData;
177 M_BITMAPDATA->m_depth = d;
180 void wxBitmap::SetQuality(int q)
183 m_refData = new wxBitmapRefData;
185 M_BITMAPDATA->m_quality = q;
188 void wxBitmap::SetOk(bool isOk)
191 m_refData = new wxBitmapRefData;
193 M_BITMAPDATA->m_ok = isOk;
196 void wxBitmap::SetPalette(const wxPalette& palette)
199 m_refData = new wxBitmapRefData;
201 M_BITMAPDATA->m_bitmapPalette = palette ;
204 void wxBitmap::SetMask(wxMask *mask)
207 m_refData = new wxBitmapRefData;
209 M_BITMAPDATA->m_bitmapMask = mask ;
212 bool wxBitmap::Ok() const
214 return m_refData && M_BITMAPDATA->m_ok;
217 wxPalette* wxBitmap::GetPalette() const
221 return &M_BITMAPDATA->m_bitmapPalette;
224 wxMask* wxBitmap::GetMask() const
228 return M_BITMAPDATA->m_bitmapMask;
231 int wxBitmap::GetDepth() const
235 return M_BITMAPDATA->m_depth;
238 int wxBitmap::GetWidth() const
242 return M_BITMAPDATA->m_width;
245 int wxBitmap::GetHeight() const
249 return M_BITMAPDATA->m_height;
252 bool wxBitmap::Create(int w, int h, int d)
256 m_refData = new wxBitmapRefData;
258 M_BITMAPDATA->m_width = w;
259 M_BITMAPDATA->m_height = h;
260 M_BITMAPDATA->m_depth = d;
262 /* TODO: create new bitmap */
263 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
264 initWithBitmapDataPlanes: NULL
271 colorSpaceName: NSCalibratedRGBColorSpace
275 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
276 M_BITMAPDATA->m_ok = true;
277 M_BITMAPDATA->m_numColors = 0;
278 M_BITMAPDATA->m_quality = 0;
279 M_BITMAPDATA->m_bitmapMask = NULL;
281 return M_BITMAPDATA->m_ok;
284 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
286 wxAutoNSAutoreleasePool pool;
289 m_refData = new wxBitmapRefData;
291 NSBitmapImageRep *imageRep = [NSBitmapImageRep
292 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
296 M_BITMAPDATA->m_width = [imageRep pixelsWide];
297 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
298 M_BITMAPDATA->m_depth = 24; // FIXME
299 M_BITMAPDATA->m_ok = true;
300 M_BITMAPDATA->m_numColors = 0;
301 M_BITMAPDATA->m_quality = 0;
302 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
303 M_BITMAPDATA->m_bitmapMask = NULL;
307 if(!image.LoadFile(filename,type))
311 *this = wxBitmap(image);
315 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
319 m_refData = new wxBitmapRefData;
324 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
329 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
334 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
339 wxImage wxBitmap::ConvertToImage() const
341 if(!M_BITMAPDATA->m_ok)
342 return wxImage(5,5)/*wxNullImage*/;
343 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
346 bool wxBitmap::CreateFromXpm(const char **xpm)
348 #if wxUSE_IMAGE && wxUSE_XPM
351 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
353 wxXPMDecoder decoder;
354 wxImage img = decoder.ReadData(xpm);
355 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
357 *this = wxBitmap(img);
364 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
368 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
369 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
371 m_refData = new wxBitmapRefData();
373 M_BITMAPDATA->m_width = image.GetWidth();
374 M_BITMAPDATA->m_height = image.GetHeight();
375 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
376 initWithBitmapDataPlanes: NULL
377 pixelsWide: image.GetWidth()
378 pixelsHigh: image.GetHeight()
383 colorSpaceName: NSCalibratedRGBColorSpace
387 const int numBytes = image.GetWidth()*image.GetHeight()*3;
388 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
389 // TODO: Alpha and convert to desired depth
390 M_BITMAPDATA->m_depth = 24;
391 M_BITMAPDATA->m_ok = true;
392 M_BITMAPDATA->m_numColors = 0;
393 M_BITMAPDATA->m_quality = 0;
394 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
395 M_BITMAPDATA->m_bitmapMask = NULL;
399 // ========================================================================
401 // ========================================================================
403 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
412 // Construct a mask from a bitmap and a colour indicating
413 // the transparent area
414 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
419 Create(bitmap, colour);
422 // Construct a mask from a bitmap and a palette index indicating
423 // the transparent area
424 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
430 Create(bitmap, paletteIndex);
433 // Construct a mask from a mono bitmap (copies the bitmap).
434 wxMask::wxMask(const wxBitmap& bitmap)
445 // TODO: delete mask bitmap
448 // Create a mask from a mono bitmap (copies the bitmap).
449 bool wxMask::Create(const wxBitmap& bitmap)
455 // Create a mask from a bitmap and a palette index indicating
456 // the transparent area
457 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
463 // Create a mask from a bitmap and a colour indicating
464 // the transparent area
465 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)