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(NSImage* cocoaNSImage)
130 (void) Create(cocoaNSImage);
133 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
135 (void) Create(cocoaNSBitmapImageRep);
138 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
140 (void) Create(data, type, width, height, depth);
143 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
145 LoadFile(filename, type);
148 wxObjectRefData *wxBitmap::CreateRefData() const
150 return new wxBitmapRefData;
153 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
155 return new wxBitmapRefData(*(wxBitmapRefData*)data);
158 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
162 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
165 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
169 NSImage *nsimage = [[[NSImage alloc]
170 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
173 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
174 if(useMask && GetMask())
176 // Show before/after to prove that the bitmap itself is not changed
177 // even though we just composited onto the NSImage
178 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
179 NSImage *maskImage = [[NSImage alloc]
180 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
181 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
183 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
184 [nsimage unlockFocus];
186 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
191 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
195 // NOTE: No checking is done to make sure width/height agree
196 [bitmapImageRep retain];
197 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
198 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
201 void wxBitmap::SetWidth(int w)
204 m_refData = new wxBitmapRefData;
206 M_BITMAPDATA->m_width = w;
209 void wxBitmap::SetHeight(int h)
212 m_refData = new wxBitmapRefData;
214 M_BITMAPDATA->m_height = h;
217 void wxBitmap::SetDepth(int d)
220 m_refData = new wxBitmapRefData;
222 M_BITMAPDATA->m_depth = d;
225 void wxBitmap::SetQuality(int q)
228 m_refData = new wxBitmapRefData;
230 M_BITMAPDATA->m_quality = q;
233 void wxBitmap::SetOk(bool isOk)
236 m_refData = new wxBitmapRefData;
238 M_BITMAPDATA->m_ok = isOk;
241 void wxBitmap::SetPalette(const wxPalette& palette)
244 m_refData = new wxBitmapRefData;
246 M_BITMAPDATA->m_bitmapPalette = palette ;
249 void wxBitmap::SetMask(wxMask *mask)
252 m_refData = new wxBitmapRefData;
254 M_BITMAPDATA->m_bitmapMask = mask ;
257 bool wxBitmap::IsOk() const
259 return m_refData && M_BITMAPDATA->m_ok;
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)
301 m_refData = new wxBitmapRefData;
303 M_BITMAPDATA->m_width = w;
304 M_BITMAPDATA->m_height = h;
305 M_BITMAPDATA->m_depth = d;
307 /* TODO: create new bitmap */
308 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
309 initWithBitmapDataPlanes: NULL
316 colorSpaceName: NSCalibratedRGBColorSpace
317 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
318 // 10.4 will add padding bytes when 0 is used here
321 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
322 M_BITMAPDATA->m_ok = true;
323 M_BITMAPDATA->m_numColors = 0;
324 M_BITMAPDATA->m_quality = 0;
325 M_BITMAPDATA->m_bitmapMask = NULL;
327 return M_BITMAPDATA->m_ok;
330 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
332 wxAutoNSAutoreleasePool pool;
335 m_refData = new wxBitmapRefData;
337 NSBitmapImageRep *imageRep = [NSBitmapImageRep
338 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
342 M_BITMAPDATA->m_width = [imageRep pixelsWide];
343 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
344 M_BITMAPDATA->m_depth = 24; // FIXME
345 M_BITMAPDATA->m_ok = true;
346 M_BITMAPDATA->m_numColors = 0;
347 M_BITMAPDATA->m_quality = 0;
348 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
349 M_BITMAPDATA->m_bitmapMask = NULL;
353 if(!image.LoadFile(filename,type))
357 *this = wxBitmap(image);
361 bool wxBitmap::Create(NSImage* cocoaNSImage)
363 wxAutoNSAutoreleasePool pool;
364 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
365 return Create(bitmapImageRep);
368 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
371 m_refData = new wxBitmapRefData;
374 M_BITMAPDATA->m_width = [imageRep pixelsWide];
375 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
376 M_BITMAPDATA->m_depth = [imageRep bitsPerPixel];
377 M_BITMAPDATA->m_ok = true;
378 M_BITMAPDATA->m_numColors = 0;
379 M_BITMAPDATA->m_quality = 0;
380 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
381 M_BITMAPDATA->m_bitmapMask = NULL;
388 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
392 m_refData = new wxBitmapRefData;
397 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
402 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
404 // Pool here due to lack of one during wx init phase
405 wxAutoNSAutoreleasePool pool;
408 if(!icon.GetNSImage());
409 [icon.GetNSImage() lockFocus];
411 imageRect.origin.x = imageRect.origin.y = 0.0;
412 imageRect.size = [icon.GetNSImage() size];
413 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
414 [icon.GetNSImage() unlockFocus];
417 m_refData = new wxBitmapRefData;
418 M_BITMAPDATA->m_cocoaNSBitmapImageRep = newBitmapRep;
419 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
420 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
421 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
422 M_BITMAPDATA->m_ok = true;
423 M_BITMAPDATA->m_numColors = 0;
424 M_BITMAPDATA->m_quality = 0;
425 M_BITMAPDATA->m_bitmapMask = NULL;
429 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
434 wxImage wxBitmap::ConvertToImage() const
436 wxAutoNSAutoreleasePool pool;
438 return /*wxImage(5,5)*/wxNullImage;
439 NSImage *nsimage = GetNSImage(false /* don't use mask */);
440 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
442 for(int i=0; i < M_BITMAPDATA->m_width; i++)
444 // Don't let the pool get too big as you'll notice we're creating
445 // two autoreleased NSColor objects with every iteration.
446 wxAutoNSAutoreleasePool loopPool;
447 for(int j=0; j < M_BITMAPDATA->m_height; j++)
449 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
450 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
451 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
454 [nsimage unlockFocus];
458 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
462 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
463 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
465 m_refData = new wxBitmapRefData();
467 M_BITMAPDATA->m_width = image.GetWidth();
468 M_BITMAPDATA->m_height = image.GetHeight();
469 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
470 initWithBitmapDataPlanes: NULL
471 pixelsWide: image.GetWidth()
472 pixelsHigh: image.GetHeight()
477 colorSpaceName: NSCalibratedRGBColorSpace
478 bytesPerRow: image.GetWidth()*3
481 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
482 // so that the rows are aligned suitably for altivec by the OS (Tiger)
483 const int numBytes = image.GetWidth()*image.GetHeight()*3;
484 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
485 // TODO: Alpha and convert to desired depth
486 M_BITMAPDATA->m_depth = 24;
487 M_BITMAPDATA->m_ok = true;
488 M_BITMAPDATA->m_numColors = 0;
489 M_BITMAPDATA->m_quality = 0;
490 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
491 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
495 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
500 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
504 if([bitmapRep bitsPerPixel]!=bpp)
506 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
509 data.m_width = [bitmapRep pixelsWide];
510 data.m_height = [bitmapRep pixelsHigh];
511 data.m_stride = [bitmapRep bytesPerRow];
512 return [bitmapRep bitmapData];
514 // NOTE: It is up to the user to make sure they used the proper
515 // pixel format class that details what is actually inside the pixels
516 // We can only check to make sure that the total number of bits per
517 // pixel are being iterated over properly
518 // NSBitmapImageRep can contain grayscale or CMYK data and
519 // wxPixelDataBase doesn't really define the color format
522 void wxBitmap::UngetRawData(wxPixelDataBase& data)
526 // ========================================================================
528 // ========================================================================
530 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
534 m_cocoaNSBitmapImageRep = nil;
537 // Construct a mask from a bitmap and a colour indicating
538 // the transparent area
539 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
541 m_cocoaNSBitmapImageRep = nil;
542 Create(bitmap, colour);
545 // Construct a mask from a bitmap and a palette index indicating
546 // the transparent area
547 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
549 m_cocoaNSBitmapImageRep = nil;
551 Create(bitmap, paletteIndex);
554 // Construct a mask from a mono bitmap (copies the bitmap).
555 wxMask::wxMask(const wxBitmap& bitmap)
557 m_cocoaNSBitmapImageRep = nil;
563 wxMask::wxMask(const wxMask& src)
565 , m_cocoaNSBitmapImageRep([src.m_cocoaNSBitmapImageRep retain])
571 [m_cocoaNSBitmapImageRep release];
574 // Create a mask from a mono bitmap (copies the bitmap).
575 bool wxMask::Create(const wxBitmap& bitmap)
578 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
582 // Create a mask from a bitmap and a palette index indicating
583 // the transparent area
584 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
587 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
591 template <typename PixelData>
592 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
594 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
595 typename PixelData::Iterator p(srcData);
596 const int nRows = srcData.GetHeight();
597 const int nCols = srcData.GetWidth();
598 // Total number of bytes per destination column
599 const int dstRowLength = (nCols+7)/8;
600 // Number of source columns that fit into a byte in the destination
601 const int width_aligned = nCols/8*8;
602 for(int y=0; y<nRows; ++y)
604 typename PixelData::Iterator rowStart(p);
605 unsigned char *dstRow = dstData + y*dstRowLength;
606 for(int x=0; x<width_aligned; x+=8)
608 unsigned char *dstByte = dstRow + x/8;
610 // Take source RGB, compare it with the wxColour
611 for(int j=0; j<8; ++j, ++p)
614 ( p.Red()!=colour.Red()
615 || p.Green()!=colour.Green()
616 || p.Blue()!=colour.Blue()
620 // Handle the remaining 0-7 pixels in the row
621 unsigned char *dstByte = dstRow + width_aligned/8;
624 for(int j=0; j<(nCols%8); ++j, ++p)
627 ( p.Red()!=colour.Red()
628 || p.Green()!=colour.Green()
629 || p.Blue()!=colour.Blue()
633 p.OffsetY(srcData,1);
638 // Create a mask from a bitmap and a colour indicating
639 // the transparent area
640 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
642 wxAutoNSAutoreleasePool pool;
645 int bmpWidth = bitmap.GetWidth();
646 int bmpHeight = bitmap.GetHeight();
647 int dstRowLength = (bmpWidth+7)/8;
649 // Create a bitmap image rep with 1-bit per pixel data representing
650 // the alpha channel padded such that rows end on byte boundaries
651 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
652 // we must have at least one channel of non-alpha data. In order to
653 // make our life easy, we use planar data which results in two
654 // separate arrays. We don't need to touch the first because it
655 // should never be used. The second is the 1-bit "alpha" data.
656 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
657 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
658 pixelsHigh:bmpHeight bitsPerSample:1
659 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
660 colorSpaceName:NSCalibratedWhiteColorSpace
661 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
662 wxCHECK(maskRep,false);
664 // We need the source NSBitmapImageRep to detemine its pixel format
665 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
666 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
668 // Get a pointer to the destination data
669 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
670 [maskRep getBitmapDataPlanes:dstPlanes];
671 unsigned char *dstData = dstPlanes[1];
672 // The wxImage format (which we use whenever we imported from wxImage)
673 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
675 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
676 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
677 false, wxT("Unable to access raw data"));
679 // 32-bpp RGBx (x=throw away, no alpha)
680 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
682 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
683 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
684 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
685 false, wxT("Unable to access raw data"));
688 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
690 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
691 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
692 false, wxT("Unable to access raw data"));
694 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
695 // 8-bpp Grayscale, no alpha
696 { // Force all RGB to access the same grayscale component
697 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
698 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
699 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
700 false, wxT("Unable to access raw data"));
703 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
705 // maskRep was autoreleased in case we had to exit quickly
706 m_cocoaNSBitmapImageRep = [maskRep retain];