+ wxAutoNSAutoreleasePool pool;
+ if(!bitmap.Ok())
+ return false;
+ int bmpWidth = bitmap.GetWidth();
+ int bmpHeight = bitmap.GetHeight();
+ int dstRowLength = (bmpWidth+7)/8;
+
+ // Create a bitmap image rep with 1-bit per pixel data representing
+ // the alpha channel padded such that rows end on byte boundaries
+ // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
+ // we must have at least one channel of non-alpha data. In order to
+ // make our life easy, we use planar data which results in two
+ // separate arrays. We don't need to touch the first because it
+ // should never be used. The second is the 1-bit "alpha" data.
+ NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
+ initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
+ pixelsHigh:bmpHeight bitsPerSample:1
+ samplesPerPixel:2 hasAlpha:YES isPlanar:YES
+ colorSpaceName:NSCalibratedWhiteColorSpace
+ bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
+ wxCHECK(maskRep,false);
+
+ // We need the source NSBitmapImageRep to detemine its pixel format
+ NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
+ wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
+
+ // Get a pointer to the destination data
+ unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
+ [maskRep getBitmapDataPlanes:dstPlanes];
+ unsigned char *dstData = dstPlanes[1];
+ // The wxImage format (which we use whenever we imported from wxImage)
+ if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
+ {
+ wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
+ wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
+ false, wxT("Unable to access raw data"));
+ }
+ // 32-bpp RGBx (x=throw away, no alpha)
+ else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
+ {
+ typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
+ wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
+ wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
+ false, wxT("Unable to access raw data"));
+ }
+ // 32-bpp RGBA
+ else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
+ {
+ wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
+ wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
+ false, wxT("Unable to access raw data"));
+ }
+ else
+ { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
+
+ // maskRep was autoreleased in case we had to exit quickly
+ m_cocoaNSBitmapImageRep = [maskRep retain];
+ return true;