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"
31 #import <AppKit/NSBitmapImageRep.h>
32 #import <AppKit/NSGraphics.h>
33 #import <AppKit/NSImage.h>
34 #import <AppKit/NSColor.h>
36 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
38 // ========================================================================
40 // ========================================================================
41 class wxBitmapRefData: public wxGDIRefData
43 friend class wxBitmap;
46 wxBitmapRefData( const wxBitmapRefData& data );
47 virtual ~wxBitmapRefData();
55 wxPalette m_bitmapPalette;
57 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
58 wxMask *m_bitmapMask; // Optional mask
61 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
63 wxBitmapRefData::wxBitmapRefData()
71 m_cocoaNSBitmapImageRep = nil;
75 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
77 m_width = data.m_width;
78 m_height = data.m_height;
79 m_depth = data.m_depth;
81 m_numColors = data.m_numColors;
82 m_bitmapPalette = data.m_bitmapPalette;
83 m_quality = data.m_quality;
84 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
85 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
88 wxBitmapRefData::~wxBitmapRefData()
90 [m_cocoaNSBitmapImageRep release];
91 m_cocoaNSBitmapImageRep = NULL;
97 // ========================================================================
99 // ========================================================================
100 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
107 wxBitmap::~wxBitmap()
111 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
113 m_refData = new wxBitmapRefData;
115 M_BITMAPDATA->m_width = the_width ;
116 M_BITMAPDATA->m_height = the_height ;
117 M_BITMAPDATA->m_depth = no_bits ;
118 M_BITMAPDATA->m_numColors = 0;
120 /* TODO: create the bitmap from data */
123 wxBitmap::wxBitmap(int w, int h, int d)
125 (void)Create(w, h, d);
128 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
130 (void) Create(data, type, width, height, depth);
133 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
135 LoadFile(filename, type);
138 wxObjectRefData *wxBitmap::CreateRefData() const
140 return new wxBitmapRefData;
143 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
145 return new wxBitmapRefData(*(wxBitmapRefData*)data);
148 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
152 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
155 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
159 NSImage *nsimage = [[[NSImage alloc]
160 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
163 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
164 if(useMask && GetMask())
166 // Show before/after to prove that the bitmap itself is not changed
167 // even though we just composited onto the NSImage
168 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
169 NSImage *maskImage = [[NSImage alloc]
170 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
171 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
173 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
174 [nsimage unlockFocus];
176 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
181 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
185 // NOTE: No checking is done to make sure width/height agree
186 [bitmapImageRep retain];
187 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
188 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
191 void wxBitmap::SetWidth(int w)
194 m_refData = new wxBitmapRefData;
196 M_BITMAPDATA->m_width = w;
199 void wxBitmap::SetHeight(int h)
202 m_refData = new wxBitmapRefData;
204 M_BITMAPDATA->m_height = h;
207 void wxBitmap::SetDepth(int d)
210 m_refData = new wxBitmapRefData;
212 M_BITMAPDATA->m_depth = d;
215 void wxBitmap::SetQuality(int q)
218 m_refData = new wxBitmapRefData;
220 M_BITMAPDATA->m_quality = q;
223 void wxBitmap::SetOk(bool isOk)
226 m_refData = new wxBitmapRefData;
228 M_BITMAPDATA->m_ok = isOk;
231 void wxBitmap::SetPalette(const wxPalette& palette)
234 m_refData = new wxBitmapRefData;
236 M_BITMAPDATA->m_bitmapPalette = palette ;
239 void wxBitmap::SetMask(wxMask *mask)
242 m_refData = new wxBitmapRefData;
244 M_BITMAPDATA->m_bitmapMask = mask ;
247 bool wxBitmap::IsOk() const
249 return m_refData && M_BITMAPDATA->m_ok;
252 wxPalette* wxBitmap::GetPalette() const
256 return &M_BITMAPDATA->m_bitmapPalette;
259 wxMask* wxBitmap::GetMask() const
263 return M_BITMAPDATA->m_bitmapMask;
266 int wxBitmap::GetDepth() const
270 return M_BITMAPDATA->m_depth;
273 int wxBitmap::GetWidth() const
277 return M_BITMAPDATA->m_width;
280 int wxBitmap::GetHeight() const
284 return M_BITMAPDATA->m_height;
287 bool wxBitmap::Create(int w, int h, int d)
291 m_refData = new wxBitmapRefData;
293 M_BITMAPDATA->m_width = w;
294 M_BITMAPDATA->m_height = h;
295 M_BITMAPDATA->m_depth = d;
297 /* TODO: create new bitmap */
298 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
299 initWithBitmapDataPlanes: NULL
306 colorSpaceName: NSCalibratedRGBColorSpace
307 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
308 // 10.4 will add padding bytes when 0 is used here
311 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
312 M_BITMAPDATA->m_ok = true;
313 M_BITMAPDATA->m_numColors = 0;
314 M_BITMAPDATA->m_quality = 0;
315 M_BITMAPDATA->m_bitmapMask = NULL;
317 return M_BITMAPDATA->m_ok;
320 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
322 wxAutoNSAutoreleasePool pool;
325 m_refData = new wxBitmapRefData;
327 NSBitmapImageRep *imageRep = [NSBitmapImageRep
328 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
332 M_BITMAPDATA->m_width = [imageRep pixelsWide];
333 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
334 M_BITMAPDATA->m_depth = 24; // FIXME
335 M_BITMAPDATA->m_ok = true;
336 M_BITMAPDATA->m_numColors = 0;
337 M_BITMAPDATA->m_quality = 0;
338 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
339 M_BITMAPDATA->m_bitmapMask = NULL;
343 if(!image.LoadFile(filename,type))
347 *this = wxBitmap(image);
351 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
355 m_refData = new wxBitmapRefData;
360 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
365 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
367 // Pool here due to lack of one during wx init phase
368 wxAutoNSAutoreleasePool pool;
371 if(!icon.GetNSImage());
372 [icon.GetNSImage() lockFocus];
374 imageRect.origin.x = imageRect.origin.y = 0.0;
375 imageRect.size = [icon.GetNSImage() size];
376 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
377 [icon.GetNSImage() unlockFocus];
380 m_refData = new wxBitmapRefData;
381 M_BITMAPDATA->m_cocoaNSBitmapImageRep = newBitmapRep;
382 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
383 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
384 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
385 M_BITMAPDATA->m_ok = true;
386 M_BITMAPDATA->m_numColors = 0;
387 M_BITMAPDATA->m_quality = 0;
388 M_BITMAPDATA->m_bitmapMask = NULL;
392 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
397 wxImage wxBitmap::ConvertToImage() const
399 wxAutoNSAutoreleasePool pool;
401 return /*wxImage(5,5)*/wxNullImage;
402 NSImage *nsimage = GetNSImage(false /* don't use mask */);
403 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
405 for(int i=0; i < M_BITMAPDATA->m_width; i++)
407 // Don't let the pool get too big as you'll notice we're creating
408 // two autoreleased NSColor objects with every iteration.
409 wxAutoNSAutoreleasePool loopPool;
410 for(int j=0; j < M_BITMAPDATA->m_height; j++)
412 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
413 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
414 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
417 [nsimage unlockFocus];
421 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
425 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
426 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
428 m_refData = new wxBitmapRefData();
430 M_BITMAPDATA->m_width = image.GetWidth();
431 M_BITMAPDATA->m_height = image.GetHeight();
432 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
433 initWithBitmapDataPlanes: NULL
434 pixelsWide: image.GetWidth()
435 pixelsHigh: image.GetHeight()
440 colorSpaceName: NSCalibratedRGBColorSpace
441 bytesPerRow: image.GetWidth()*3
444 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
445 // so that the rows are aligned suitably for altivec by the OS (Tiger)
446 const int numBytes = image.GetWidth()*image.GetHeight()*3;
447 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
448 // TODO: Alpha and convert to desired depth
449 M_BITMAPDATA->m_depth = 24;
450 M_BITMAPDATA->m_ok = true;
451 M_BITMAPDATA->m_numColors = 0;
452 M_BITMAPDATA->m_quality = 0;
453 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
454 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
458 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
463 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
467 if([bitmapRep bitsPerPixel]!=bpp)
469 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
472 data.m_width = [bitmapRep pixelsWide];
473 data.m_height = [bitmapRep pixelsHigh];
474 data.m_stride = [bitmapRep bytesPerRow];
475 return [bitmapRep bitmapData];
477 // NOTE: It is up to the user to make sure they used the proper
478 // pixel format class that details what is actually inside the pixels
479 // We can only check to make sure that the total number of bits per
480 // pixel are being iterated over properly
481 // NSBitmapImageRep can contain grayscale or CMYK data and
482 // wxPixelDataBase doesn't really define the color format
485 void wxBitmap::UngetRawData(wxPixelDataBase& data)
489 // ========================================================================
491 // ========================================================================
493 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
497 m_cocoaNSBitmapImageRep = nil;
500 // Construct a mask from a bitmap and a colour indicating
501 // the transparent area
502 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
504 m_cocoaNSBitmapImageRep = nil;
505 Create(bitmap, colour);
508 // Construct a mask from a bitmap and a palette index indicating
509 // the transparent area
510 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
512 m_cocoaNSBitmapImageRep = nil;
514 Create(bitmap, paletteIndex);
517 // Construct a mask from a mono bitmap (copies the bitmap).
518 wxMask::wxMask(const wxBitmap& bitmap)
520 m_cocoaNSBitmapImageRep = nil;
526 wxMask::wxMask(const wxMask& src)
528 , m_cocoaNSBitmapImageRep([src.m_cocoaNSBitmapImageRep retain])
534 [m_cocoaNSBitmapImageRep release];
537 // Create a mask from a mono bitmap (copies the bitmap).
538 bool wxMask::Create(const wxBitmap& bitmap)
541 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
545 // Create a mask from a bitmap and a palette index indicating
546 // the transparent area
547 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
550 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
554 template <typename PixelData>
555 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
557 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
558 typename PixelData::Iterator p(srcData);
559 const int nRows = srcData.GetHeight();
560 const int nCols = srcData.GetWidth();
561 // Total number of bytes per destination column
562 const int dstRowLength = (nCols+7)/8;
563 // Number of source columns that fit into a byte in the destination
564 const int width_aligned = nCols/8*8;
565 for(int y=0; y<nRows; ++y)
567 typename PixelData::Iterator rowStart(p);
568 unsigned char *dstRow = dstData + y*dstRowLength;
569 for(int x=0; x<width_aligned; x+=8)
571 unsigned char *dstByte = dstRow + x/8;
573 // Take source RGB, compare it with the wxColour
574 for(int j=0; j<8; ++j, ++p)
577 ( p.Red()!=colour.Red()
578 || p.Green()!=colour.Green()
579 || p.Blue()!=colour.Blue()
583 // Handle the remaining 0-7 pixels in the row
584 unsigned char *dstByte = dstRow + width_aligned/8;
587 for(int j=0; j<(nCols%8); ++j, ++p)
590 ( p.Red()!=colour.Red()
591 || p.Green()!=colour.Green()
592 || p.Blue()!=colour.Blue()
596 p.OffsetY(srcData,1);
601 // Create a mask from a bitmap and a colour indicating
602 // the transparent area
603 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
605 wxAutoNSAutoreleasePool pool;
608 int bmpWidth = bitmap.GetWidth();
609 int bmpHeight = bitmap.GetHeight();
610 int dstRowLength = (bmpWidth+7)/8;
612 // Create a bitmap image rep with 1-bit per pixel data representing
613 // the alpha channel padded such that rows end on byte boundaries
614 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
615 // we must have at least one channel of non-alpha data. In order to
616 // make our life easy, we use planar data which results in two
617 // separate arrays. We don't need to touch the first because it
618 // should never be used. The second is the 1-bit "alpha" data.
619 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
620 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
621 pixelsHigh:bmpHeight bitsPerSample:1
622 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
623 colorSpaceName:NSCalibratedWhiteColorSpace
624 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
625 wxCHECK(maskRep,false);
627 // We need the source NSBitmapImageRep to detemine its pixel format
628 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
629 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
631 // Get a pointer to the destination data
632 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
633 [maskRep getBitmapDataPlanes:dstPlanes];
634 unsigned char *dstData = dstPlanes[1];
635 // The wxImage format (which we use whenever we imported from wxImage)
636 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
638 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
639 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
640 false, wxT("Unable to access raw data"));
642 // 32-bpp RGBx (x=throw away, no alpha)
643 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
645 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
646 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
647 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
648 false, wxT("Unable to access raw data"));
651 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
653 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
654 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
655 false, wxT("Unable to access raw data"));
657 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
658 // 8-bpp Grayscale, no alpha
659 { // Force all RGB to access the same grayscale component
660 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
661 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
662 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
663 false, wxT("Unable to access raw data"));
666 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
668 // maskRep was autoreleased in case we had to exit quickly
669 m_cocoaNSBitmapImageRep = [maskRep retain];