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;
74 [m_cocoaNSBitmapImageRep retain];
75 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
78 wxBitmapRefData::~wxBitmapRefData()
80 [m_cocoaNSBitmapImageRep release];
81 m_cocoaNSBitmapImageRep = NULL;
87 // ========================================================================
89 // ========================================================================
90 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
96 if ( wxTheBitmapList )
97 wxTheBitmapList->AddBitmap(this);
100 wxBitmap::~wxBitmap()
103 wxTheBitmapList->DeleteObject(this);
106 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
108 m_refData = new wxBitmapRefData;
110 M_BITMAPDATA->m_width = the_width ;
111 M_BITMAPDATA->m_height = the_height ;
112 M_BITMAPDATA->m_depth = no_bits ;
113 M_BITMAPDATA->m_numColors = 0;
115 /* TODO: create the bitmap from data */
117 if ( wxTheBitmapList )
118 wxTheBitmapList->AddBitmap(this);
121 wxBitmap::wxBitmap(int w, int h, int d)
123 (void)Create(w, h, d);
125 if ( wxTheBitmapList )
126 wxTheBitmapList->AddBitmap(this);
129 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
131 (void) Create(data, type, width, height, depth);
133 if ( wxTheBitmapList )
134 wxTheBitmapList->AddBitmap(this);
137 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
139 LoadFile(filename, type);
141 if ( wxTheBitmapList )
142 wxTheBitmapList->AddBitmap(this);
145 wxObjectRefData *wxBitmap::CreateRefData() const
147 return new wxBitmapRefData;
150 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
152 return new wxBitmapRefData(*(wxBitmapRefData*)data);
155 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
159 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
162 void wxBitmap::SetWidth(int w)
165 m_refData = new wxBitmapRefData;
167 M_BITMAPDATA->m_width = w;
170 void wxBitmap::SetHeight(int h)
173 m_refData = new wxBitmapRefData;
175 M_BITMAPDATA->m_height = h;
178 void wxBitmap::SetDepth(int d)
181 m_refData = new wxBitmapRefData;
183 M_BITMAPDATA->m_depth = d;
186 void wxBitmap::SetQuality(int q)
189 m_refData = new wxBitmapRefData;
191 M_BITMAPDATA->m_quality = q;
194 void wxBitmap::SetOk(bool isOk)
197 m_refData = new wxBitmapRefData;
199 M_BITMAPDATA->m_ok = isOk;
202 void wxBitmap::SetPalette(const wxPalette& palette)
205 m_refData = new wxBitmapRefData;
207 M_BITMAPDATA->m_bitmapPalette = palette ;
210 void wxBitmap::SetMask(wxMask *mask)
213 m_refData = new wxBitmapRefData;
215 M_BITMAPDATA->m_bitmapMask = mask ;
218 bool wxBitmap::Ok() const
220 return m_refData && M_BITMAPDATA->m_ok;
223 wxPalette* wxBitmap::GetPalette() const
227 return &M_BITMAPDATA->m_bitmapPalette;
230 wxMask* wxBitmap::GetMask() const
234 return M_BITMAPDATA->m_bitmapMask;
237 int wxBitmap::GetDepth() const
241 return M_BITMAPDATA->m_depth;
244 int wxBitmap::GetWidth() const
248 return M_BITMAPDATA->m_width;
251 int wxBitmap::GetHeight() const
255 return M_BITMAPDATA->m_height;
258 bool wxBitmap::Create(int w, int h, int d)
262 m_refData = new wxBitmapRefData;
264 M_BITMAPDATA->m_width = w;
265 M_BITMAPDATA->m_height = h;
266 M_BITMAPDATA->m_depth = d;
268 /* TODO: create new bitmap */
270 return M_BITMAPDATA->m_ok;
273 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
275 wxAutoNSAutoreleasePool pool;
278 m_refData = new wxBitmapRefData;
280 NSBitmapImageRep *imageRep = [NSBitmapImageRep
281 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
285 M_BITMAPDATA->m_width = [imageRep pixelsWide];
286 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
287 M_BITMAPDATA->m_depth = 24; // FIXME
288 M_BITMAPDATA->m_ok = true;
289 M_BITMAPDATA->m_numColors = 0;
290 M_BITMAPDATA->m_quality = 0;
291 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
292 M_BITMAPDATA->m_bitmapMask = NULL;
296 if(!image.LoadFile(filename,type))
300 *this = wxBitmap(image);
304 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
308 m_refData = new wxBitmapRefData;
313 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
318 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
323 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
328 wxImage wxBitmap::ConvertToImage() const
330 if(!M_BITMAPDATA->m_ok)
331 return wxImage(5,5)/*wxNullImage*/;
332 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
335 bool wxBitmap::CreateFromXpm(const char **xpm)
337 #if wxUSE_IMAGE && wxUSE_XPM
340 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
342 wxXPMDecoder decoder;
343 wxImage img = decoder.ReadData(xpm);
344 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
346 *this = wxBitmap(img);
353 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
357 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
358 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
360 m_refData = new wxBitmapRefData();
362 M_BITMAPDATA->m_width = image.GetWidth();
363 M_BITMAPDATA->m_height = image.GetHeight();
364 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
365 initWithBitmapDataPlanes: NULL
366 pixelsWide: image.GetWidth()
367 pixelsHigh: image.GetHeight()
372 colorSpaceName: NSCalibratedRGBColorSpace
376 const int numBytes = image.GetWidth()*image.GetHeight()*3;
377 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
378 // TODO: Alpha and convert to desired depth
379 M_BITMAPDATA->m_depth = 24;
380 M_BITMAPDATA->m_ok = true;
381 M_BITMAPDATA->m_numColors = 0;
382 M_BITMAPDATA->m_quality = 0;
383 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
384 M_BITMAPDATA->m_bitmapMask = NULL;
388 // ========================================================================
390 // ========================================================================
392 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
401 // Construct a mask from a bitmap and a colour indicating
402 // the transparent area
403 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
408 Create(bitmap, colour);
411 // Construct a mask from a bitmap and a palette index indicating
412 // the transparent area
413 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
419 Create(bitmap, paletteIndex);
422 // Construct a mask from a mono bitmap (copies the bitmap).
423 wxMask::wxMask(const wxBitmap& bitmap)
434 // TODO: delete mask bitmap
437 // Create a mask from a mono bitmap (copies the bitmap).
438 bool wxMask::Create(const wxBitmap& bitmap)
444 // Create a mask from a bitmap and a palette index indicating
445 // the transparent area
446 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
452 // Create a mask from a bitmap and a colour indicating
453 // the transparent area
454 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)