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"
18 #include "wx/colour.h"
20 #include "wx/bitmap.h"
22 #include "wx/xpmdecod.h"
23 #include "wx/rawbmp.h"
25 #include "wx/cocoa/autorelease.h"
26 #include "wx/cocoa/string.h"
28 #import <AppKit/NSBitmapImageRep.h>
29 #import <AppKit/NSGraphics.h>
31 // ========================================================================
33 // ========================================================================
34 class wxBitmapRefData: public wxGDIRefData
36 friend class wxBitmap;
39 wxBitmapRefData( const wxBitmapRefData& data );
40 virtual ~wxBitmapRefData();
48 wxPalette m_bitmapPalette;
50 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
51 wxMask *m_bitmapMask; // Optional mask
54 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
56 wxBitmapRefData::wxBitmapRefData()
64 m_cocoaNSBitmapImageRep = nil;
68 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
70 m_width = data.m_width;
71 m_height = data.m_height;
72 m_depth = data.m_depth;
74 m_numColors = data.m_numColors;
75 m_bitmapPalette = data.m_bitmapPalette;
76 m_quality = data.m_quality;
77 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
78 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
81 wxBitmapRefData::~wxBitmapRefData()
83 [m_cocoaNSBitmapImageRep release];
84 m_cocoaNSBitmapImageRep = NULL;
90 // ========================================================================
92 // ========================================================================
93 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
100 wxBitmap::~wxBitmap()
104 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
106 m_refData = new wxBitmapRefData;
108 M_BITMAPDATA->m_width = the_width ;
109 M_BITMAPDATA->m_height = the_height ;
110 M_BITMAPDATA->m_depth = no_bits ;
111 M_BITMAPDATA->m_numColors = 0;
113 /* TODO: create the bitmap from data */
116 wxBitmap::wxBitmap(int w, int h, int d)
118 (void)Create(w, h, d);
121 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
123 (void) Create(data, type, width, height, depth);
126 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
128 LoadFile(filename, type);
131 wxObjectRefData *wxBitmap::CreateRefData() const
133 return new wxBitmapRefData;
136 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
138 return new wxBitmapRefData(*(wxBitmapRefData*)data);
141 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
145 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
148 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
152 // NOTE: No checking is done to make sure width/height agree
153 [bitmapImageRep retain];
154 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
155 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
158 void wxBitmap::SetWidth(int w)
161 m_refData = new wxBitmapRefData;
163 M_BITMAPDATA->m_width = w;
166 void wxBitmap::SetHeight(int h)
169 m_refData = new wxBitmapRefData;
171 M_BITMAPDATA->m_height = h;
174 void wxBitmap::SetDepth(int d)
177 m_refData = new wxBitmapRefData;
179 M_BITMAPDATA->m_depth = d;
182 void wxBitmap::SetQuality(int q)
185 m_refData = new wxBitmapRefData;
187 M_BITMAPDATA->m_quality = q;
190 void wxBitmap::SetOk(bool isOk)
193 m_refData = new wxBitmapRefData;
195 M_BITMAPDATA->m_ok = isOk;
198 void wxBitmap::SetPalette(const wxPalette& palette)
201 m_refData = new wxBitmapRefData;
203 M_BITMAPDATA->m_bitmapPalette = palette ;
206 void wxBitmap::SetMask(wxMask *mask)
209 m_refData = new wxBitmapRefData;
211 M_BITMAPDATA->m_bitmapMask = mask ;
214 bool wxBitmap::Ok() const
216 return m_refData && M_BITMAPDATA->m_ok;
219 wxPalette* wxBitmap::GetPalette() const
223 return &M_BITMAPDATA->m_bitmapPalette;
226 wxMask* wxBitmap::GetMask() const
230 return M_BITMAPDATA->m_bitmapMask;
233 int wxBitmap::GetDepth() const
237 return M_BITMAPDATA->m_depth;
240 int wxBitmap::GetWidth() const
244 return M_BITMAPDATA->m_width;
247 int wxBitmap::GetHeight() const
251 return M_BITMAPDATA->m_height;
254 bool wxBitmap::Create(int w, int h, int d)
258 m_refData = new wxBitmapRefData;
260 M_BITMAPDATA->m_width = w;
261 M_BITMAPDATA->m_height = h;
262 M_BITMAPDATA->m_depth = d;
264 /* TODO: create new bitmap */
265 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
266 initWithBitmapDataPlanes: NULL
273 colorSpaceName: NSCalibratedRGBColorSpace
277 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
278 M_BITMAPDATA->m_ok = true;
279 M_BITMAPDATA->m_numColors = 0;
280 M_BITMAPDATA->m_quality = 0;
281 M_BITMAPDATA->m_bitmapMask = NULL;
283 return M_BITMAPDATA->m_ok;
286 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
288 wxAutoNSAutoreleasePool pool;
291 m_refData = new wxBitmapRefData;
293 NSBitmapImageRep *imageRep = [NSBitmapImageRep
294 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
298 M_BITMAPDATA->m_width = [imageRep pixelsWide];
299 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
300 M_BITMAPDATA->m_depth = 24; // FIXME
301 M_BITMAPDATA->m_ok = true;
302 M_BITMAPDATA->m_numColors = 0;
303 M_BITMAPDATA->m_quality = 0;
304 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
305 M_BITMAPDATA->m_bitmapMask = NULL;
309 if(!image.LoadFile(filename,type))
313 *this = wxBitmap(image);
317 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
321 m_refData = new wxBitmapRefData;
326 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
331 bool wxBitmap::CopyFromIcon(const wxIcon& icno)
336 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
341 wxImage wxBitmap::ConvertToImage() const
343 if(!M_BITMAPDATA->m_ok)
344 return wxImage(5,5)/*wxNullImage*/;
345 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
348 bool wxBitmap::CreateFromXpm(const char **xpm)
350 #if wxUSE_IMAGE && wxUSE_XPM
353 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
355 wxXPMDecoder decoder;
356 wxImage img = decoder.ReadData(xpm);
357 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
359 *this = wxBitmap(img);
366 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
370 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
371 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
373 m_refData = new wxBitmapRefData();
375 M_BITMAPDATA->m_width = image.GetWidth();
376 M_BITMAPDATA->m_height = image.GetHeight();
377 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
378 initWithBitmapDataPlanes: NULL
379 pixelsWide: image.GetWidth()
380 pixelsHigh: image.GetHeight()
385 colorSpaceName: NSCalibratedRGBColorSpace
389 const int numBytes = image.GetWidth()*image.GetHeight()*3;
390 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
391 // TODO: Alpha and convert to desired depth
392 M_BITMAPDATA->m_depth = 24;
393 M_BITMAPDATA->m_ok = true;
394 M_BITMAPDATA->m_numColors = 0;
395 M_BITMAPDATA->m_quality = 0;
396 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
397 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
401 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
406 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
410 if([bitmapRep bitsPerPixel]!=bpp)
412 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
415 data.m_width = [bitmapRep pixelsWide];
416 data.m_height = [bitmapRep pixelsHigh];
417 data.m_stride = [bitmapRep bytesPerRow];
418 return [bitmapRep bitmapData];
420 // NOTE: It is up to the user to make sure they used the proper
421 // pixel format class that details what is actually inside the pixels
422 // We can only check to make sure that the total number of bits per
423 // pixel are being iterated over properly
424 // NSBitmapImageRep can contain grayscale or CMYK data and
425 // wxPixelDataBase doesn't really define the color format
428 void wxBitmap::UngetRawData(wxPixelDataBase& data)
432 // ========================================================================
434 // ========================================================================
436 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
440 m_cocoaNSBitmapImageRep = nil;
443 // Construct a mask from a bitmap and a colour indicating
444 // the transparent area
445 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
447 m_cocoaNSBitmapImageRep = nil;
448 Create(bitmap, colour);
451 // Construct a mask from a bitmap and a palette index indicating
452 // the transparent area
453 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
455 m_cocoaNSBitmapImageRep = nil;
457 Create(bitmap, paletteIndex);
460 // Construct a mask from a mono bitmap (copies the bitmap).
461 wxMask::wxMask(const wxBitmap& bitmap)
463 m_cocoaNSBitmapImageRep = nil;
470 [m_cocoaNSBitmapImageRep release];
473 // Create a mask from a mono bitmap (copies the bitmap).
474 bool wxMask::Create(const wxBitmap& bitmap)
480 // Create a mask from a bitmap and a palette index indicating
481 // the transparent area
482 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
488 template <class PixelData>
489 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
491 wxCHECK_MSG(dstData,false,"Couldn't access mask data");
492 class PixelData::Iterator p(srcData);
493 const int nRows = srcData.GetHeight();
494 const int nCols = srcData.GetWidth();
495 // Total number of bytes per destination column
496 const int dstRowLength = (nCols+7)/8;
497 // Number of source columns that fit into a byte in the destination
498 const int width_aligned = nCols/8*8;
499 for(int y=0; y<nRows; ++y)
501 class PixelData::Iterator rowStart(p);
502 unsigned char *dstRow = dstData + y*dstRowLength;
503 for(int x=0; x<width_aligned; x+=8)
505 unsigned char *dstByte = dstRow + x/8;
507 // Take source RGB, compare it with the wxColour
508 for(int j=0; j<8; ++j, ++p)
511 ( p.Red()!=colour.Red()
512 || p.Green()!=colour.Green()
513 || p.Blue()!=colour.Blue()
517 // Handle the remaining 0-7 pixels in the row
518 unsigned char *dstByte = dstRow + width_aligned/8;
520 for(int j=0; j<(nCols%8); ++j, ++p)
523 ( p.Red()!=colour.Red()
524 || p.Green()!=colour.Green()
525 || p.Blue()!=colour.Blue()
529 p.OffsetY(srcData,1);
534 // Create a mask from a bitmap and a colour indicating
535 // the transparent area
536 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
538 wxAutoNSAutoreleasePool pool;
541 int bmpWidth = bitmap.GetWidth();
542 int bmpHeight = bitmap.GetHeight();
543 int dstRowLength = (bmpWidth+7)/8;
545 // Create a bitmap image rep with 1-bit per pixel data representing
546 // the alpha channel padded such that rows end on byte boundaries
547 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
548 // we must have at least one channel of non-alpha data. In order to
549 // make our life easy, we use planar data which results in two
550 // separate arrays. We don't need to touch the first because it
551 // should never be used. The second is the 1-bit "alpha" data.
552 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
553 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
554 pixelsHigh:bmpHeight bitsPerSample:1
555 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
556 colorSpaceName:NSCalibratedWhiteColorSpace
557 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
558 wxCHECK(maskRep,false);
560 // We need the source NSBitmapImageRep to detemine its pixel format
561 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
562 wxCHECK_MSG(srcBitmapRep,false,"Can't create mask for an uninitialized bitmap");
564 // Get a pointer to the destination data
565 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
566 [maskRep getBitmapDataPlanes:dstPlanes];
567 unsigned char *dstData = dstPlanes[1];
568 // The wxImage format (which we use whenever we imported from wxImage)
569 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
571 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
572 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
573 false, "Unable to access raw data");
575 // 32-bpp RGBx (x=throw away, no alpha)
576 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
578 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
579 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
580 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
581 false, "Unable to access raw data");
584 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
586 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
587 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
588 false, "Unable to access raw data");
591 { wxCHECK_MSG(false,false,"Unimplemented pixel format"); }
593 // maskRep was autoreleased in case we had to exit quickly
594 m_cocoaNSBitmapImageRep = [maskRep retain];