1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows 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 // ========================================================================
43 class wxBitmapRefData: public wxGDIRefData
45 friend class wxBitmap;
48 wxBitmapRefData( const wxBitmapRefData& data );
49 virtual ~wxBitmapRefData();
51 virtual bool IsOk() const { return m_ok; }
59 wxPalette m_bitmapPalette;
61 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
62 wxMask *m_bitmapMask; // Optional mask
65 wxBitmapRefData::wxBitmapRefData()
73 m_cocoaNSBitmapImageRep = nil;
77 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
79 wxAutoNSAutoreleasePool pool;
81 m_width = data.m_width;
82 m_height = data.m_height;
83 m_depth = data.m_depth;
85 m_numColors = data.m_numColors;
86 m_bitmapPalette = data.m_bitmapPalette;
87 m_quality = data.m_quality;
88 m_cocoaNSBitmapImageRep = wxGCSafeRetain([[data.m_cocoaNSBitmapImageRep copyWithZone:nil] autorelease]);
89 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
92 wxBitmapRefData::~wxBitmapRefData()
94 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
95 m_cocoaNSBitmapImageRep = NULL;
101 // ========================================================================
103 // ========================================================================
105 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
107 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
114 wxBitmap::~wxBitmap()
118 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
120 m_refData = new wxBitmapRefData;
122 M_BITMAPDATA->m_width = the_width ;
123 M_BITMAPDATA->m_height = the_height ;
124 M_BITMAPDATA->m_depth = no_bits ;
125 M_BITMAPDATA->m_numColors = 0;
127 /* TODO: create the bitmap from data */
130 wxBitmap::wxBitmap(NSImage* cocoaNSImage)
132 (void) Create(cocoaNSImage);
135 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
137 (void) Create(cocoaNSBitmapImageRep);
140 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
142 (void) Create(data, type, width, height, depth);
145 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
147 LoadFile(filename, type);
150 wxGDIRefData *wxBitmap::CreateGDIRefData() const
152 return new wxBitmapRefData;
155 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
157 return new wxBitmapRefData(*(wxBitmapRefData*)data);
160 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
164 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
167 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
171 NSImage *nsimage = [[[NSImage alloc]
172 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
175 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
176 if(useMask && GetMask())
178 // Show before/after to prove that the bitmap itself is not changed
179 // even though we just composited onto the NSImage
180 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
181 NSImage *maskImage = [[NSImage alloc]
182 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
183 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
185 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
186 [nsimage unlockFocus];
188 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
193 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
197 // NOTE: No checking is done to make sure width/height agree
198 wxGCSafeRetain(bitmapImageRep);
199 wxGCSafeRelease(M_BITMAPDATA->m_cocoaNSBitmapImageRep);
200 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
203 void wxBitmap::SetWidth(int w)
206 m_refData = new wxBitmapRefData;
208 M_BITMAPDATA->m_width = w;
211 void wxBitmap::SetHeight(int h)
214 m_refData = new wxBitmapRefData;
216 M_BITMAPDATA->m_height = h;
219 void wxBitmap::SetDepth(int d)
222 m_refData = new wxBitmapRefData;
224 M_BITMAPDATA->m_depth = d;
227 void wxBitmap::SetQuality(int q)
230 m_refData = new wxBitmapRefData;
232 M_BITMAPDATA->m_quality = q;
235 void wxBitmap::SetOk(bool isOk)
238 m_refData = new wxBitmapRefData;
240 M_BITMAPDATA->m_ok = isOk;
243 void wxBitmap::SetPalette(const wxPalette& palette)
246 m_refData = new wxBitmapRefData;
248 M_BITMAPDATA->m_bitmapPalette = palette ;
251 void wxBitmap::SetMask(wxMask *mask)
254 m_refData = new wxBitmapRefData;
256 M_BITMAPDATA->m_bitmapMask = mask ;
259 wxPalette* wxBitmap::GetPalette() const
263 return &M_BITMAPDATA->m_bitmapPalette;
266 wxMask* wxBitmap::GetMask() const
270 return M_BITMAPDATA->m_bitmapMask;
273 int wxBitmap::GetDepth() const
277 return M_BITMAPDATA->m_depth;
280 int wxBitmap::GetWidth() const
284 return M_BITMAPDATA->m_width;
287 int wxBitmap::GetHeight() const
291 return M_BITMAPDATA->m_height;
294 bool wxBitmap::Create(int w, int h, int d)
296 wxAutoNSAutoreleasePool pool;
300 m_refData = new wxBitmapRefData;
302 M_BITMAPDATA->m_width = w;
303 M_BITMAPDATA->m_height = h;
304 M_BITMAPDATA->m_depth = d;
306 /* TODO: create new bitmap */
307 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
308 initWithBitmapDataPlanes: NULL
315 colorSpaceName: NSCalibratedRGBColorSpace
316 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
317 // 10.4 will add padding bytes when 0 is used here
318 bitsPerPixel: 0] autorelease]);
320 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
321 M_BITMAPDATA->m_ok = true;
322 M_BITMAPDATA->m_numColors = 0;
323 M_BITMAPDATA->m_quality = 0;
324 M_BITMAPDATA->m_bitmapMask = NULL;
326 return M_BITMAPDATA->m_ok;
329 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
331 wxAutoNSAutoreleasePool pool;
334 m_refData = new wxBitmapRefData;
336 NSBitmapImageRep *imageRep = [NSBitmapImageRep
337 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
341 M_BITMAPDATA->m_width = [imageRep pixelsWide];
342 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
343 M_BITMAPDATA->m_depth = 24; // FIXME
344 M_BITMAPDATA->m_ok = true;
345 M_BITMAPDATA->m_numColors = 0;
346 M_BITMAPDATA->m_quality = 0;
347 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
348 M_BITMAPDATA->m_bitmapMask = NULL;
352 if(!image.LoadFile(filename,type))
356 *this = wxBitmap(image);
360 bool wxBitmap::Create(NSImage* cocoaNSImage)
362 wxAutoNSAutoreleasePool pool;
363 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
364 return Create(bitmapImageRep);
367 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
370 m_refData = new wxBitmapRefData;
373 M_BITMAPDATA->m_width = [imageRep pixelsWide];
374 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
375 M_BITMAPDATA->m_depth = [imageRep bitsPerPixel];
376 M_BITMAPDATA->m_ok = true;
377 M_BITMAPDATA->m_numColors = 0;
378 M_BITMAPDATA->m_quality = 0;
379 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
380 M_BITMAPDATA->m_bitmapMask = NULL;
387 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
391 m_refData = new wxBitmapRefData;
396 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
401 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
403 // Pool here due to lack of one during wx init phase
404 wxAutoNSAutoreleasePool pool;
407 if(!icon.GetNSImage());
408 [icon.GetNSImage() lockFocus];
410 imageRect.origin.x = imageRect.origin.y = 0.0;
411 imageRect.size = [icon.GetNSImage() size];
412 NSBitmapImageRep *newBitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease];
413 [icon.GetNSImage() unlockFocus];
416 m_refData = new wxBitmapRefData;
417 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(newBitmapRep);
418 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
419 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
420 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
421 M_BITMAPDATA->m_ok = true;
422 M_BITMAPDATA->m_numColors = 0;
423 M_BITMAPDATA->m_quality = 0;
424 M_BITMAPDATA->m_bitmapMask = NULL;
428 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
430 wxAutoNSAutoreleasePool pool;
433 NSImage *nsimage = GetNSImage(false);
436 NSRect imageRect = {{0,0}, [nsimage size]};
437 imageRect.origin.x = imageRect.size.width * rect.x / GetWidth();
438 imageRect.origin.y = imageRect.size.height * rect.y / GetHeight();
439 imageRect.size.width *= static_cast<CGFloat>(rect.width) / GetWidth();
440 imageRect.size.height *= static_cast<CGFloat>(rect.height) / GetHeight();
442 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
443 [nsimage unlockFocus];
445 wxBitmap newBitmap(newBitmapRep);
450 wxImage wxBitmap::ConvertToImage() const
452 wxAutoNSAutoreleasePool pool;
454 return /*wxImage(5,5)*/wxNullImage;
455 NSImage *nsimage = GetNSImage(false /* don't use mask */);
456 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
458 for(int i=0; i < M_BITMAPDATA->m_width; i++)
460 // Don't let the pool get too big as you'll notice we're creating
461 // two autoreleased NSColor objects with every iteration.
462 wxAutoNSAutoreleasePool loopPool;
463 for(int j=0; j < M_BITMAPDATA->m_height; j++)
465 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
466 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
467 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
470 [nsimage unlockFocus];
474 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
476 wxAutoNSAutoreleasePool pool;
479 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
480 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
482 m_refData = new wxBitmapRefData();
484 M_BITMAPDATA->m_width = image.GetWidth();
485 M_BITMAPDATA->m_height = image.GetHeight();
486 NSBitmapImageRep *bitmapImage = [[[NSBitmapImageRep alloc]
487 initWithBitmapDataPlanes: NULL
488 pixelsWide: image.GetWidth()
489 pixelsHigh: image.GetHeight()
494 colorSpaceName: NSCalibratedRGBColorSpace
495 bytesPerRow: image.GetWidth()*3
496 bitsPerPixel: 0] autorelease];
498 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
499 // so that the rows are aligned suitably for altivec by the OS (Tiger)
500 const int numBytes = image.GetWidth()*image.GetHeight()*3;
501 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
502 // TODO: Alpha and convert to desired depth
503 M_BITMAPDATA->m_depth = 24;
504 M_BITMAPDATA->m_ok = true;
505 M_BITMAPDATA->m_numColors = 0;
506 M_BITMAPDATA->m_quality = 0;
507 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(bitmapImage);
508 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
512 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
517 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
521 if([bitmapRep bitsPerPixel]!=bpp)
523 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
526 data.m_width = [bitmapRep pixelsWide];
527 data.m_height = [bitmapRep pixelsHigh];
528 data.m_stride = [bitmapRep bytesPerRow];
529 return [bitmapRep bitmapData];
531 // NOTE: It is up to the user to make sure they used the proper
532 // pixel format class that details what is actually inside the pixels
533 // We can only check to make sure that the total number of bits per
534 // pixel are being iterated over properly
535 // NSBitmapImageRep can contain grayscale or CMYK data and
536 // wxPixelDataBase doesn't really define the color format
539 void wxBitmap::UngetRawData(wxPixelDataBase& data)
543 // ========================================================================
545 // ========================================================================
547 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
551 m_cocoaNSBitmapImageRep = nil;
554 // Construct a mask from a bitmap and a colour indicating
555 // the transparent area
556 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
558 m_cocoaNSBitmapImageRep = nil;
559 Create(bitmap, colour);
562 // Construct a mask from a bitmap and a palette index indicating
563 // the transparent area
564 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
566 m_cocoaNSBitmapImageRep = nil;
568 Create(bitmap, paletteIndex);
571 // Construct a mask from a mono bitmap (copies the bitmap).
572 wxMask::wxMask(const wxBitmap& bitmap)
574 m_cocoaNSBitmapImageRep = nil;
580 wxMask::wxMask(const wxMask& src)
582 , m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
588 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
591 // Create a mask from a mono bitmap (copies the bitmap).
592 bool wxMask::Create(const wxBitmap& bitmap)
595 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
599 // Create a mask from a bitmap and a palette index indicating
600 // the transparent area
601 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
604 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
608 template <typename PixelData>
609 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
611 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
612 typename PixelData::Iterator p(srcData);
613 const int nRows = srcData.GetHeight();
614 const int nCols = srcData.GetWidth();
615 // Total number of bytes per destination column
616 const int dstRowLength = (nCols+7)/8;
617 // Number of source columns that fit into a byte in the destination
618 const int width_aligned = nCols/8*8;
619 for(int y=0; y<nRows; ++y)
621 typename PixelData::Iterator rowStart(p);
622 unsigned char *dstRow = dstData + y*dstRowLength;
623 for(int x=0; x<width_aligned; x+=8)
625 unsigned char *dstByte = dstRow + x/8;
627 // Take source RGB, compare it with the wxColour
628 for(int j=0; j<8; ++j, ++p)
631 ( p.Red()!=colour.Red()
632 || p.Green()!=colour.Green()
633 || p.Blue()!=colour.Blue()
637 // Handle the remaining 0-7 pixels in the row
638 unsigned char *dstByte = dstRow + width_aligned/8;
641 for(int j=0; j<(nCols%8); ++j, ++p)
644 ( p.Red()!=colour.Red()
645 || p.Green()!=colour.Green()
646 || p.Blue()!=colour.Blue()
650 p.OffsetY(srcData,1);
655 // Create a mask from a bitmap and a colour indicating
656 // the transparent area
657 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
659 wxAutoNSAutoreleasePool pool;
662 int bmpWidth = bitmap.GetWidth();
663 int bmpHeight = bitmap.GetHeight();
664 int dstRowLength = (bmpWidth+7)/8;
666 // Create a bitmap image rep with 1-bit per pixel data representing
667 // the alpha channel padded such that rows end on byte boundaries
668 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
669 // we must have at least one channel of non-alpha data. In order to
670 // make our life easy, we use planar data which results in two
671 // separate arrays. We don't need to touch the first because it
672 // should never be used. The second is the 1-bit "alpha" data.
673 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
674 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
675 pixelsHigh:bmpHeight bitsPerSample:1
676 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
677 colorSpaceName:NSCalibratedWhiteColorSpace
678 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
679 wxCHECK(maskRep,false);
681 // We need the source NSBitmapImageRep to detemine its pixel format
682 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
683 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
685 // Get a pointer to the destination data
686 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
687 [maskRep getBitmapDataPlanes:dstPlanes];
688 unsigned char *dstData = dstPlanes[1];
689 // The wxImage format (which we use whenever we imported from wxImage)
690 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
692 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
693 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
694 false, wxT("Unable to access raw data"));
696 // 32-bpp RGBx (x=throw away, no alpha)
697 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
699 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
700 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
701 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
702 false, wxT("Unable to access raw data"));
705 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
707 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
708 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
709 false, wxT("Unable to access raw data"));
711 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
712 // 8-bpp Grayscale, no alpha
713 { // Force all RGB to access the same grayscale component
714 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
715 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
716 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
717 false, wxT("Unable to access raw data"));
720 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
722 // maskRep was autoreleased in case we had to exit quickly
723 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);