1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/bitmap.h"
19 #include "wx/palette.h"
21 #include "wx/colour.h"
25 #include "wx/xpmdecod.h"
26 #include "wx/rawbmp.h"
28 #include "wx/cocoa/autorelease.h"
29 #include "wx/cocoa/string.h"
30 #include "wx/cocoa/ObjcRef.h"
32 #import <AppKit/NSBitmapImageRep.h>
33 #import <AppKit/NSGraphics.h>
34 #import <AppKit/NSImage.h>
35 #import <AppKit/NSColor.h>
37 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
39 // ========================================================================
41 // ========================================================================
42 class wxBitmapRefData: public wxGDIRefData
44 friend class wxBitmap;
47 wxBitmapRefData( const wxBitmapRefData& data );
48 virtual ~wxBitmapRefData();
50 virtual bool IsOk() const { return m_ok; }
58 wxPalette m_bitmapPalette;
60 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
61 wxMask *m_bitmapMask; // Optional mask
64 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
66 wxBitmapRefData::wxBitmapRefData()
74 m_cocoaNSBitmapImageRep = nil;
78 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
80 wxAutoNSAutoreleasePool pool;
82 m_width = data.m_width;
83 m_height = data.m_height;
84 m_depth = data.m_depth;
86 m_numColors = data.m_numColors;
87 m_bitmapPalette = data.m_bitmapPalette;
88 m_quality = data.m_quality;
89 m_cocoaNSBitmapImageRep = wxGCSafeRetain([[data.m_cocoaNSBitmapImageRep copyWithZone:nil] autorelease]);
90 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
93 wxBitmapRefData::~wxBitmapRefData()
95 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
96 m_cocoaNSBitmapImageRep = NULL;
102 // ========================================================================
104 // ========================================================================
105 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
112 wxBitmap::~wxBitmap()
116 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
118 m_refData = new wxBitmapRefData;
120 M_BITMAPDATA->m_width = the_width ;
121 M_BITMAPDATA->m_height = the_height ;
122 M_BITMAPDATA->m_depth = no_bits ;
123 M_BITMAPDATA->m_numColors = 0;
125 /* TODO: create the bitmap from data */
128 wxBitmap::wxBitmap(int w, int h, int d)
130 (void)Create(w, h, d);
133 wxBitmap::wxBitmap(NSImage* cocoaNSImage)
135 (void) Create(cocoaNSImage);
138 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
140 (void) Create(cocoaNSBitmapImageRep);
143 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
145 (void) Create(data, type, width, height, depth);
148 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
150 LoadFile(filename, type);
153 wxGDIRefData *wxBitmap::CreateGDIRefData() const
155 return new wxBitmapRefData;
158 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
160 return new wxBitmapRefData(*(wxBitmapRefData*)data);
163 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
167 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
170 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
174 NSImage *nsimage = [[[NSImage alloc]
175 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
178 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
179 if(useMask && GetMask())
181 // Show before/after to prove that the bitmap itself is not changed
182 // even though we just composited onto the NSImage
183 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
184 NSImage *maskImage = [[NSImage alloc]
185 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
186 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
188 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
189 [nsimage unlockFocus];
191 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
196 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
200 // NOTE: No checking is done to make sure width/height agree
201 wxGCSafeRetain(bitmapImageRep);
202 wxGCSafeRelease(M_BITMAPDATA->m_cocoaNSBitmapImageRep);
203 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
206 void wxBitmap::SetWidth(int w)
209 m_refData = new wxBitmapRefData;
211 M_BITMAPDATA->m_width = w;
214 void wxBitmap::SetHeight(int h)
217 m_refData = new wxBitmapRefData;
219 M_BITMAPDATA->m_height = h;
222 void wxBitmap::SetDepth(int d)
225 m_refData = new wxBitmapRefData;
227 M_BITMAPDATA->m_depth = d;
230 void wxBitmap::SetQuality(int q)
233 m_refData = new wxBitmapRefData;
235 M_BITMAPDATA->m_quality = q;
238 void wxBitmap::SetOk(bool isOk)
241 m_refData = new wxBitmapRefData;
243 M_BITMAPDATA->m_ok = isOk;
246 void wxBitmap::SetPalette(const wxPalette& palette)
249 m_refData = new wxBitmapRefData;
251 M_BITMAPDATA->m_bitmapPalette = palette ;
254 void wxBitmap::SetMask(wxMask *mask)
257 m_refData = new wxBitmapRefData;
259 M_BITMAPDATA->m_bitmapMask = mask ;
262 wxPalette* wxBitmap::GetPalette() const
266 return &M_BITMAPDATA->m_bitmapPalette;
269 wxMask* wxBitmap::GetMask() const
273 return M_BITMAPDATA->m_bitmapMask;
276 int wxBitmap::GetDepth() const
280 return M_BITMAPDATA->m_depth;
283 int wxBitmap::GetWidth() const
287 return M_BITMAPDATA->m_width;
290 int wxBitmap::GetHeight() const
294 return M_BITMAPDATA->m_height;
297 bool wxBitmap::Create(int w, int h, int d)
299 wxAutoNSAutoreleasePool pool;
303 m_refData = new wxBitmapRefData;
305 M_BITMAPDATA->m_width = w;
306 M_BITMAPDATA->m_height = h;
307 M_BITMAPDATA->m_depth = d;
309 /* TODO: create new bitmap */
310 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
311 initWithBitmapDataPlanes: NULL
318 colorSpaceName: NSCalibratedRGBColorSpace
319 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
320 // 10.4 will add padding bytes when 0 is used here
321 bitsPerPixel: 0] autorelease]);
323 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
324 M_BITMAPDATA->m_ok = true;
325 M_BITMAPDATA->m_numColors = 0;
326 M_BITMAPDATA->m_quality = 0;
327 M_BITMAPDATA->m_bitmapMask = NULL;
329 return M_BITMAPDATA->m_ok;
332 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
334 wxAutoNSAutoreleasePool pool;
337 m_refData = new wxBitmapRefData;
339 NSBitmapImageRep *imageRep = [NSBitmapImageRep
340 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
344 M_BITMAPDATA->m_width = [imageRep pixelsWide];
345 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
346 M_BITMAPDATA->m_depth = 24; // FIXME
347 M_BITMAPDATA->m_ok = true;
348 M_BITMAPDATA->m_numColors = 0;
349 M_BITMAPDATA->m_quality = 0;
350 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
351 M_BITMAPDATA->m_bitmapMask = NULL;
355 if(!image.LoadFile(filename,type))
359 *this = wxBitmap(image);
363 bool wxBitmap::Create(NSImage* cocoaNSImage)
365 wxAutoNSAutoreleasePool pool;
366 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
367 return Create(bitmapImageRep);
370 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
373 m_refData = new wxBitmapRefData;
376 M_BITMAPDATA->m_width = [imageRep pixelsWide];
377 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
378 M_BITMAPDATA->m_depth = [imageRep bitsPerPixel];
379 M_BITMAPDATA->m_ok = true;
380 M_BITMAPDATA->m_numColors = 0;
381 M_BITMAPDATA->m_quality = 0;
382 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
383 M_BITMAPDATA->m_bitmapMask = NULL;
390 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
394 m_refData = new wxBitmapRefData;
399 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
404 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
406 // Pool here due to lack of one during wx init phase
407 wxAutoNSAutoreleasePool pool;
410 if(!icon.GetNSImage());
411 [icon.GetNSImage() lockFocus];
413 imageRect.origin.x = imageRect.origin.y = 0.0;
414 imageRect.size = [icon.GetNSImage() size];
415 NSBitmapImageRep *newBitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease];
416 [icon.GetNSImage() unlockFocus];
419 m_refData = new wxBitmapRefData;
420 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(newBitmapRep);
421 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
422 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
423 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
424 M_BITMAPDATA->m_ok = true;
425 M_BITMAPDATA->m_numColors = 0;
426 M_BITMAPDATA->m_quality = 0;
427 M_BITMAPDATA->m_bitmapMask = NULL;
431 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
433 wxAutoNSAutoreleasePool pool;
436 NSImage *nsimage = GetNSImage(false);
439 NSRect imageRect = {{0,0}, [nsimage size]};
440 imageRect.origin.x = imageRect.size.width * rect.x / GetWidth();
441 imageRect.origin.y = imageRect.size.height * rect.y / GetHeight();
442 imageRect.size.width *= wx_static_cast(CGFloat, rect.width) / GetWidth();
443 imageRect.size.height *= wx_static_cast(CGFloat, rect.height) / GetHeight();
445 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
446 [nsimage unlockFocus];
448 wxBitmap newBitmap(newBitmapRep);
453 wxImage wxBitmap::ConvertToImage() const
455 wxAutoNSAutoreleasePool pool;
457 return /*wxImage(5,5)*/wxNullImage;
458 NSImage *nsimage = GetNSImage(false /* don't use mask */);
459 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
461 for(int i=0; i < M_BITMAPDATA->m_width; i++)
463 // Don't let the pool get too big as you'll notice we're creating
464 // two autoreleased NSColor objects with every iteration.
465 wxAutoNSAutoreleasePool loopPool;
466 for(int j=0; j < M_BITMAPDATA->m_height; j++)
468 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
469 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
470 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
473 [nsimage unlockFocus];
477 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
479 wxAutoNSAutoreleasePool pool;
482 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
483 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
485 m_refData = new wxBitmapRefData();
487 M_BITMAPDATA->m_width = image.GetWidth();
488 M_BITMAPDATA->m_height = image.GetHeight();
489 NSBitmapImageRep *bitmapImage = [[[NSBitmapImageRep alloc]
490 initWithBitmapDataPlanes: NULL
491 pixelsWide: image.GetWidth()
492 pixelsHigh: image.GetHeight()
497 colorSpaceName: NSCalibratedRGBColorSpace
498 bytesPerRow: image.GetWidth()*3
499 bitsPerPixel: 0] autorelease];
501 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
502 // so that the rows are aligned suitably for altivec by the OS (Tiger)
503 const int numBytes = image.GetWidth()*image.GetHeight()*3;
504 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
505 // TODO: Alpha and convert to desired depth
506 M_BITMAPDATA->m_depth = 24;
507 M_BITMAPDATA->m_ok = true;
508 M_BITMAPDATA->m_numColors = 0;
509 M_BITMAPDATA->m_quality = 0;
510 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(bitmapImage);
511 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
515 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
520 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
524 if([bitmapRep bitsPerPixel]!=bpp)
526 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
529 data.m_width = [bitmapRep pixelsWide];
530 data.m_height = [bitmapRep pixelsHigh];
531 data.m_stride = [bitmapRep bytesPerRow];
532 return [bitmapRep bitmapData];
534 // NOTE: It is up to the user to make sure they used the proper
535 // pixel format class that details what is actually inside the pixels
536 // We can only check to make sure that the total number of bits per
537 // pixel are being iterated over properly
538 // NSBitmapImageRep can contain grayscale or CMYK data and
539 // wxPixelDataBase doesn't really define the color format
542 void wxBitmap::UngetRawData(wxPixelDataBase& data)
546 // ========================================================================
548 // ========================================================================
550 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
554 m_cocoaNSBitmapImageRep = nil;
557 // Construct a mask from a bitmap and a colour indicating
558 // the transparent area
559 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
561 m_cocoaNSBitmapImageRep = nil;
562 Create(bitmap, colour);
565 // Construct a mask from a bitmap and a palette index indicating
566 // the transparent area
567 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
569 m_cocoaNSBitmapImageRep = nil;
571 Create(bitmap, paletteIndex);
574 // Construct a mask from a mono bitmap (copies the bitmap).
575 wxMask::wxMask(const wxBitmap& bitmap)
577 m_cocoaNSBitmapImageRep = nil;
583 wxMask::wxMask(const wxMask& src)
585 , m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
591 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
594 // Create a mask from a mono bitmap (copies the bitmap).
595 bool wxMask::Create(const wxBitmap& bitmap)
598 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
602 // Create a mask from a bitmap and a palette index indicating
603 // the transparent area
604 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
607 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
611 template <typename PixelData>
612 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
614 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
615 typename PixelData::Iterator p(srcData);
616 const int nRows = srcData.GetHeight();
617 const int nCols = srcData.GetWidth();
618 // Total number of bytes per destination column
619 const int dstRowLength = (nCols+7)/8;
620 // Number of source columns that fit into a byte in the destination
621 const int width_aligned = nCols/8*8;
622 for(int y=0; y<nRows; ++y)
624 typename PixelData::Iterator rowStart(p);
625 unsigned char *dstRow = dstData + y*dstRowLength;
626 for(int x=0; x<width_aligned; x+=8)
628 unsigned char *dstByte = dstRow + x/8;
630 // Take source RGB, compare it with the wxColour
631 for(int j=0; j<8; ++j, ++p)
634 ( p.Red()!=colour.Red()
635 || p.Green()!=colour.Green()
636 || p.Blue()!=colour.Blue()
640 // Handle the remaining 0-7 pixels in the row
641 unsigned char *dstByte = dstRow + width_aligned/8;
644 for(int j=0; j<(nCols%8); ++j, ++p)
647 ( p.Red()!=colour.Red()
648 || p.Green()!=colour.Green()
649 || p.Blue()!=colour.Blue()
653 p.OffsetY(srcData,1);
658 // Create a mask from a bitmap and a colour indicating
659 // the transparent area
660 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
662 wxAutoNSAutoreleasePool pool;
665 int bmpWidth = bitmap.GetWidth();
666 int bmpHeight = bitmap.GetHeight();
667 int dstRowLength = (bmpWidth+7)/8;
669 // Create a bitmap image rep with 1-bit per pixel data representing
670 // the alpha channel padded such that rows end on byte boundaries
671 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
672 // we must have at least one channel of non-alpha data. In order to
673 // make our life easy, we use planar data which results in two
674 // separate arrays. We don't need to touch the first because it
675 // should never be used. The second is the 1-bit "alpha" data.
676 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
677 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
678 pixelsHigh:bmpHeight bitsPerSample:1
679 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
680 colorSpaceName:NSCalibratedWhiteColorSpace
681 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
682 wxCHECK(maskRep,false);
684 // We need the source NSBitmapImageRep to detemine its pixel format
685 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
686 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
688 // Get a pointer to the destination data
689 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
690 [maskRep getBitmapDataPlanes:dstPlanes];
691 unsigned char *dstData = dstPlanes[1];
692 // The wxImage format (which we use whenever we imported from wxImage)
693 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
695 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
696 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
697 false, wxT("Unable to access raw data"));
699 // 32-bpp RGBx (x=throw away, no alpha)
700 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
702 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
703 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
704 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
705 false, wxT("Unable to access raw data"));
708 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
710 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
711 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
712 false, wxT("Unable to access raw data"));
714 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
715 // 8-bpp Grayscale, no alpha
716 { // Force all RGB to access the same grayscale component
717 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
718 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
719 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
720 false, wxT("Unable to access raw data"));
723 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
725 // maskRep was autoreleased in case we had to exit quickly
726 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);