1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.cpp
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/palette.h"
15 #include "wx/bitmap.h"
19 #include "wx/xpmdecod.h"
21 #include "wx/cocoa/autorelease.h"
22 #include "wx/cocoa/string.h"
24 #import <AppKit/NSBitmapImageRep.h>
25 #import <AppKit/NSGraphics.h>
27 // ========================================================================
29 // ========================================================================
30 class wxBitmapRefData: public wxGDIRefData
32 friend class wxBitmap;
35 wxBitmapRefData( const wxBitmapRefData& data );
36 virtual ~wxBitmapRefData();
44 wxPalette m_bitmapPalette;
46 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
47 wxMask *m_bitmapMask; // Optional mask
50 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
52 wxBitmapRefData::wxBitmapRefData()
60 m_cocoaNSBitmapImageRep = nil;
64 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
66 m_width = data.m_width;
67 m_height = data.m_height;
68 m_depth = data.m_depth;
70 m_numColors = data.m_numColors;
71 m_bitmapPalette = data.m_bitmapPalette;
72 m_quality = data.m_quality;
73 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
74 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
77 wxBitmapRefData::~wxBitmapRefData()
79 [m_cocoaNSBitmapImageRep release];
80 m_cocoaNSBitmapImageRep = NULL;
86 // ========================================================================
88 // ========================================================================
89 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
100 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
102 m_refData = new wxBitmapRefData;
104 M_BITMAPDATA->m_width = the_width ;
105 M_BITMAPDATA->m_height = the_height ;
106 M_BITMAPDATA->m_depth = no_bits ;
107 M_BITMAPDATA->m_numColors = 0;
109 /* TODO: create the bitmap from data */
112 wxBitmap::wxBitmap(int w, int h, int d)
114 (void)Create(w, h, d);
117 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
119 (void) Create(data, type, width, height, depth);
122 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
124 LoadFile(filename, type);
127 wxObjectRefData *wxBitmap::CreateRefData() const
129 return new wxBitmapRefData;
132 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
134 return new wxBitmapRefData(*(wxBitmapRefData*)data);
137 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
141 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
144 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
148 // NOTE: No checking is done to make sure width/height agree
149 [bitmapImageRep retain];
150 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
151 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
154 void wxBitmap::SetWidth(int w)
157 m_refData = new wxBitmapRefData;
159 M_BITMAPDATA->m_width = w;
162 void wxBitmap::SetHeight(int h)
165 m_refData = new wxBitmapRefData;
167 M_BITMAPDATA->m_height = h;
170 void wxBitmap::SetDepth(int d)
173 m_refData = new wxBitmapRefData;
175 M_BITMAPDATA->m_depth = d;
178 void wxBitmap::SetQuality(int q)
181 m_refData = new wxBitmapRefData;
183 M_BITMAPDATA->m_quality = q;
186 void wxBitmap::SetOk(bool isOk)
189 m_refData = new wxBitmapRefData;
191 M_BITMAPDATA->m_ok = isOk;
194 void wxBitmap::SetPalette(const wxPalette& palette)
197 m_refData = new wxBitmapRefData;
199 M_BITMAPDATA->m_bitmapPalette = palette ;
202 void wxBitmap::SetMask(wxMask *mask)
205 m_refData = new wxBitmapRefData;
207 M_BITMAPDATA->m_bitmapMask = mask ;
210 bool wxBitmap::Ok() const
212 return m_refData && M_BITMAPDATA->m_ok;
215 wxPalette* wxBitmap::GetPalette() const
219 return &M_BITMAPDATA->m_bitmapPalette;
222 wxMask* wxBitmap::GetMask() const
226 return M_BITMAPDATA->m_bitmapMask;
229 int wxBitmap::GetDepth() const
233 return M_BITMAPDATA->m_depth;
236 int wxBitmap::GetWidth() const
240 return M_BITMAPDATA->m_width;
243 int wxBitmap::GetHeight() const
247 return M_BITMAPDATA->m_height;
250 bool wxBitmap::Create(int w, int h, int d)
254 m_refData = new wxBitmapRefData;
256 M_BITMAPDATA->m_width = w;
257 M_BITMAPDATA->m_height = h;
258 M_BITMAPDATA->m_depth = d;
260 /* TODO: create new bitmap */
261 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
262 initWithBitmapDataPlanes: NULL
269 colorSpaceName: NSCalibratedRGBColorSpace
273 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
274 M_BITMAPDATA->m_ok = true;
275 M_BITMAPDATA->m_numColors = 0;
276 M_BITMAPDATA->m_quality = 0;
277 M_BITMAPDATA->m_bitmapMask = NULL;
279 return M_BITMAPDATA->m_ok;
282 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
284 wxAutoNSAutoreleasePool pool;
287 m_refData = new wxBitmapRefData;
289 NSBitmapImageRep *imageRep = [NSBitmapImageRep
290 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
294 M_BITMAPDATA->m_width = [imageRep pixelsWide];
295 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
296 M_BITMAPDATA->m_depth = 24; // FIXME
297 M_BITMAPDATA->m_ok = true;
298 M_BITMAPDATA->m_numColors = 0;
299 M_BITMAPDATA->m_quality = 0;
300 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
301 M_BITMAPDATA->m_bitmapMask = NULL;
305 if(!image.LoadFile(filename,type))
309 *this = wxBitmap(image);
313 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
317 m_refData = new wxBitmapRefData;
322 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
327 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
332 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
337 wxImage wxBitmap::ConvertToImage() const
339 if(!M_BITMAPDATA->m_ok)
340 return wxImage(5,5)/*wxNullImage*/;
341 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
344 bool wxBitmap::CreateFromXpm(const char **xpm)
346 #if wxUSE_IMAGE && wxUSE_XPM
349 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
351 wxXPMDecoder decoder;
352 wxImage img = decoder.ReadData(xpm);
353 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
355 *this = wxBitmap(img);
362 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
366 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
367 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
369 m_refData = new wxBitmapRefData();
371 M_BITMAPDATA->m_width = image.GetWidth();
372 M_BITMAPDATA->m_height = image.GetHeight();
373 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
374 initWithBitmapDataPlanes: NULL
375 pixelsWide: image.GetWidth()
376 pixelsHigh: image.GetHeight()
381 colorSpaceName: NSCalibratedRGBColorSpace
385 const int numBytes = image.GetWidth()*image.GetHeight()*3;
386 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
387 // TODO: Alpha and convert to desired depth
388 M_BITMAPDATA->m_depth = 24;
389 M_BITMAPDATA->m_ok = true;
390 M_BITMAPDATA->m_numColors = 0;
391 M_BITMAPDATA->m_quality = 0;
392 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
393 M_BITMAPDATA->m_bitmapMask = NULL;
397 // ========================================================================
399 // ========================================================================
401 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
410 // Construct a mask from a bitmap and a colour indicating
411 // the transparent area
412 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
417 Create(bitmap, colour);
420 // Construct a mask from a bitmap and a palette index indicating
421 // the transparent area
422 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
428 Create(bitmap, paletteIndex);
431 // Construct a mask from a mono bitmap (copies the bitmap).
432 wxMask::wxMask(const wxBitmap& bitmap)
443 // TODO: delete mask bitmap
446 // Create a mask from a mono bitmap (copies the bitmap).
447 bool wxMask::Create(const wxBitmap& bitmap)
453 // Create a mask from a bitmap and a palette index indicating
454 // the transparent area
455 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
461 // Create a mask from a bitmap and a colour indicating
462 // the transparent area
463 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)