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)
101 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
103 m_refData = new wxBitmapRefData;
105 M_BITMAPDATA->m_width = the_width ;
106 M_BITMAPDATA->m_height = the_height ;
107 M_BITMAPDATA->m_depth = no_bits ;
108 M_BITMAPDATA->m_numColors = 0;
110 /* TODO: create the bitmap from data */
113 wxBitmap::wxBitmap(int w, int h, int d)
115 (void)Create(w, h, d);
118 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
120 (void) Create(data, type, width, height, depth);
123 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
125 LoadFile(filename, type);
128 wxObjectRefData *wxBitmap::CreateRefData() const
130 return new wxBitmapRefData;
133 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
135 return new wxBitmapRefData(*(wxBitmapRefData*)data);
138 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
142 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
145 void wxBitmap::SetWidth(int w)
148 m_refData = new wxBitmapRefData;
150 M_BITMAPDATA->m_width = w;
153 void wxBitmap::SetHeight(int h)
156 m_refData = new wxBitmapRefData;
158 M_BITMAPDATA->m_height = h;
161 void wxBitmap::SetDepth(int d)
164 m_refData = new wxBitmapRefData;
166 M_BITMAPDATA->m_depth = d;
169 void wxBitmap::SetQuality(int q)
172 m_refData = new wxBitmapRefData;
174 M_BITMAPDATA->m_quality = q;
177 void wxBitmap::SetOk(bool isOk)
180 m_refData = new wxBitmapRefData;
182 M_BITMAPDATA->m_ok = isOk;
185 void wxBitmap::SetPalette(const wxPalette& palette)
188 m_refData = new wxBitmapRefData;
190 M_BITMAPDATA->m_bitmapPalette = palette ;
193 void wxBitmap::SetMask(wxMask *mask)
196 m_refData = new wxBitmapRefData;
198 M_BITMAPDATA->m_bitmapMask = mask ;
201 bool wxBitmap::Ok() const
203 return m_refData && M_BITMAPDATA->m_ok;
206 wxPalette* wxBitmap::GetPalette() const
210 return &M_BITMAPDATA->m_bitmapPalette;
213 wxMask* wxBitmap::GetMask() const
217 return M_BITMAPDATA->m_bitmapMask;
220 int wxBitmap::GetDepth() const
224 return M_BITMAPDATA->m_depth;
227 int wxBitmap::GetWidth() const
231 return M_BITMAPDATA->m_width;
234 int wxBitmap::GetHeight() const
238 return M_BITMAPDATA->m_height;
241 bool wxBitmap::Create(int w, int h, int d)
245 m_refData = new wxBitmapRefData;
247 M_BITMAPDATA->m_width = w;
248 M_BITMAPDATA->m_height = h;
249 M_BITMAPDATA->m_depth = d;
251 /* TODO: create new bitmap */
253 return M_BITMAPDATA->m_ok;
256 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
258 wxAutoNSAutoreleasePool pool;
261 m_refData = new wxBitmapRefData;
263 NSBitmapImageRep *imageRep = [NSBitmapImageRep
264 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
268 M_BITMAPDATA->m_width = [imageRep pixelsWide];
269 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
270 M_BITMAPDATA->m_depth = 24; // FIXME
271 M_BITMAPDATA->m_ok = true;
272 M_BITMAPDATA->m_numColors = 0;
273 M_BITMAPDATA->m_quality = 0;
274 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
275 M_BITMAPDATA->m_bitmapMask = NULL;
279 if(!image.LoadFile(filename,type))
283 *this = wxBitmap(image);
287 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
291 m_refData = new wxBitmapRefData;
296 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
301 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
306 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
311 wxImage wxBitmap::ConvertToImage() const
313 if(!M_BITMAPDATA->m_ok)
314 return wxImage(5,5)/*wxNullImage*/;
315 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
318 bool wxBitmap::CreateFromXpm(const char **xpm)
320 #if wxUSE_IMAGE && wxUSE_XPM
323 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
325 wxXPMDecoder decoder;
326 wxImage img = decoder.ReadData(xpm);
327 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
329 *this = wxBitmap(img);
336 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
340 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
341 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
343 m_refData = new wxBitmapRefData();
345 M_BITMAPDATA->m_width = image.GetWidth();
346 M_BITMAPDATA->m_height = image.GetHeight();
347 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
348 initWithBitmapDataPlanes: NULL
349 pixelsWide: image.GetWidth()
350 pixelsHigh: image.GetHeight()
355 colorSpaceName: NSCalibratedRGBColorSpace
359 const int numBytes = image.GetWidth()*image.GetHeight()*3;
360 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
361 // TODO: Alpha and convert to desired depth
362 M_BITMAPDATA->m_depth = 24;
363 M_BITMAPDATA->m_ok = true;
364 M_BITMAPDATA->m_numColors = 0;
365 M_BITMAPDATA->m_quality = 0;
366 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
367 M_BITMAPDATA->m_bitmapMask = NULL;
371 // ========================================================================
373 // ========================================================================
375 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
384 // Construct a mask from a bitmap and a colour indicating
385 // the transparent area
386 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
391 Create(bitmap, colour);
394 // Construct a mask from a bitmap and a palette index indicating
395 // the transparent area
396 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
402 Create(bitmap, paletteIndex);
405 // Construct a mask from a mono bitmap (copies the bitmap).
406 wxMask::wxMask(const wxBitmap& bitmap)
417 // TODO: delete mask bitmap
420 // Create a mask from a mono bitmap (copies the bitmap).
421 bool wxMask::Create(const wxBitmap& bitmap)
427 // Create a mask from a bitmap and a palette index indicating
428 // the transparent area
429 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
435 // Create a mask from a bitmap and a colour indicating
436 // the transparent area
437 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)