1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/bitmap.h"
18 #include "wx/palette.h"
20 #include "wx/colour.h"
24 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
27 #include "wx/cocoa/autorelease.h"
28 #include "wx/cocoa/string.h"
29 #include "wx/cocoa/ObjcRef.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 // ========================================================================
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 wxBitmapRefData::wxBitmapRefData()
72 m_cocoaNSBitmapImageRep = nil;
76 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
78 wxAutoNSAutoreleasePool pool;
80 m_width = data.m_width;
81 m_height = data.m_height;
82 m_depth = data.m_depth;
84 m_numColors = data.m_numColors;
85 m_bitmapPalette = data.m_bitmapPalette;
86 m_quality = data.m_quality;
87 m_cocoaNSBitmapImageRep = wxGCSafeRetain([[data.m_cocoaNSBitmapImageRep copyWithZone:nil] autorelease]);
88 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
91 wxBitmapRefData::~wxBitmapRefData()
93 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
94 m_cocoaNSBitmapImageRep = NULL;
100 // ========================================================================
102 // ========================================================================
104 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
106 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
113 wxBitmap::~wxBitmap()
117 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
119 m_refData = new wxBitmapRefData;
121 M_BITMAPDATA->m_width = the_width ;
122 M_BITMAPDATA->m_height = the_height ;
123 M_BITMAPDATA->m_depth = no_bits ;
124 M_BITMAPDATA->m_numColors = 0;
126 /* TODO: create the bitmap from data */
129 wxBitmap::wxBitmap(NSImage* cocoaNSImage)
131 (void) Create(cocoaNSImage);
134 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
136 (void) Create(cocoaNSBitmapImageRep);
139 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
141 (void) Create(data, type, width, height, depth);
144 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
146 LoadFile(filename, type);
149 wxGDIRefData *wxBitmap::CreateGDIRefData() const
151 return new wxBitmapRefData;
154 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
156 return new wxBitmapRefData(*(wxBitmapRefData*)data);
159 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
163 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
166 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
170 NSImage *nsimage = [[[NSImage alloc]
171 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
174 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
175 if(useMask && GetMask())
177 // Show before/after to prove that the bitmap itself is not changed
178 // even though we just composited onto the NSImage
179 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
180 NSImage *maskImage = [[NSImage alloc]
181 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
182 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
184 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
185 [nsimage unlockFocus];
187 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
192 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
196 // NOTE: No checking is done to make sure width/height agree
197 wxGCSafeRetain(bitmapImageRep);
198 wxGCSafeRelease(M_BITMAPDATA->m_cocoaNSBitmapImageRep);
199 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
202 void wxBitmap::SetWidth(int w)
205 m_refData = new wxBitmapRefData;
207 M_BITMAPDATA->m_width = w;
210 void wxBitmap::SetHeight(int h)
213 m_refData = new wxBitmapRefData;
215 M_BITMAPDATA->m_height = h;
218 void wxBitmap::SetDepth(int d)
221 m_refData = new wxBitmapRefData;
223 M_BITMAPDATA->m_depth = d;
226 void wxBitmap::SetQuality(int q)
229 m_refData = new wxBitmapRefData;
231 M_BITMAPDATA->m_quality = q;
234 void wxBitmap::SetOk(bool isOk)
237 m_refData = new wxBitmapRefData;
239 M_BITMAPDATA->m_ok = isOk;
242 void wxBitmap::SetPalette(const wxPalette& palette)
245 m_refData = new wxBitmapRefData;
247 M_BITMAPDATA->m_bitmapPalette = palette ;
250 void wxBitmap::SetMask(wxMask *mask)
253 m_refData = new wxBitmapRefData;
255 M_BITMAPDATA->m_bitmapMask = mask ;
258 wxPalette* wxBitmap::GetPalette() const
262 return &M_BITMAPDATA->m_bitmapPalette;
265 wxMask* wxBitmap::GetMask() const
269 return M_BITMAPDATA->m_bitmapMask;
272 int wxBitmap::GetDepth() const
276 return M_BITMAPDATA->m_depth;
279 int wxBitmap::GetWidth() const
283 return M_BITMAPDATA->m_width;
286 int wxBitmap::GetHeight() const
290 return M_BITMAPDATA->m_height;
293 bool wxBitmap::Create(int w, int h, int d)
295 wxAutoNSAutoreleasePool pool;
299 m_refData = new wxBitmapRefData;
301 M_BITMAPDATA->m_width = w;
302 M_BITMAPDATA->m_height = h;
303 M_BITMAPDATA->m_depth = d;
305 /* TODO: create new bitmap */
306 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
307 initWithBitmapDataPlanes: NULL
314 colorSpaceName: NSCalibratedRGBColorSpace
315 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
316 // 10.4 will add padding bytes when 0 is used here
317 bitsPerPixel: 0] autorelease]);
319 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
320 M_BITMAPDATA->m_ok = true;
321 M_BITMAPDATA->m_numColors = 0;
322 M_BITMAPDATA->m_quality = 0;
323 M_BITMAPDATA->m_bitmapMask = NULL;
325 return M_BITMAPDATA->m_ok;
328 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
330 wxAutoNSAutoreleasePool pool;
333 m_refData = new wxBitmapRefData;
335 NSBitmapImageRep *imageRep = [NSBitmapImageRep
336 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
340 M_BITMAPDATA->m_width = [imageRep pixelsWide];
341 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
342 M_BITMAPDATA->m_depth = 24; // FIXME
343 M_BITMAPDATA->m_ok = true;
344 M_BITMAPDATA->m_numColors = 0;
345 M_BITMAPDATA->m_quality = 0;
346 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
347 M_BITMAPDATA->m_bitmapMask = NULL;
351 if(!image.LoadFile(filename,type))
355 *this = wxBitmap(image);
359 bool wxBitmap::Create(NSImage* cocoaNSImage)
361 wxAutoNSAutoreleasePool pool;
362 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
363 return Create(bitmapImageRep);
366 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
369 m_refData = new wxBitmapRefData;
372 M_BITMAPDATA->m_width = [imageRep pixelsWide];
373 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
374 M_BITMAPDATA->m_depth = [imageRep bitsPerPixel];
375 M_BITMAPDATA->m_ok = true;
376 M_BITMAPDATA->m_numColors = 0;
377 M_BITMAPDATA->m_quality = 0;
378 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
379 M_BITMAPDATA->m_bitmapMask = NULL;
386 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
390 m_refData = new wxBitmapRefData;
395 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
400 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
402 // Pool here due to lack of one during wx init phase
403 wxAutoNSAutoreleasePool pool;
406 if(!icon.GetNSImage());
407 [icon.GetNSImage() lockFocus];
409 imageRect.origin.x = imageRect.origin.y = 0.0;
410 imageRect.size = [icon.GetNSImage() size];
411 NSBitmapImageRep *newBitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease];
412 [icon.GetNSImage() unlockFocus];
415 m_refData = new wxBitmapRefData;
416 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(newBitmapRep);
417 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
418 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
419 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
420 M_BITMAPDATA->m_ok = true;
421 M_BITMAPDATA->m_numColors = 0;
422 M_BITMAPDATA->m_quality = 0;
423 M_BITMAPDATA->m_bitmapMask = NULL;
427 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
429 wxAutoNSAutoreleasePool pool;
432 NSImage *nsimage = GetNSImage(false);
435 NSRect imageRect = {{0,0}, [nsimage size]};
436 imageRect.origin.x = imageRect.size.width * rect.x / GetWidth();
437 imageRect.origin.y = imageRect.size.height * rect.y / GetHeight();
438 imageRect.size.width *= static_cast<CGFloat>(rect.width) / GetWidth();
439 imageRect.size.height *= static_cast<CGFloat>(rect.height) / GetHeight();
441 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
442 [nsimage unlockFocus];
444 wxBitmap newBitmap(newBitmapRep);
449 wxImage wxBitmap::ConvertToImage() const
451 wxAutoNSAutoreleasePool pool;
453 return /*wxImage(5,5)*/wxNullImage;
454 NSImage *nsimage = GetNSImage(false /* don't use mask */);
455 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
457 for(int i=0; i < M_BITMAPDATA->m_width; i++)
459 // Don't let the pool get too big as you'll notice we're creating
460 // two autoreleased NSColor objects with every iteration.
461 wxAutoNSAutoreleasePool loopPool;
462 for(int j=0; j < M_BITMAPDATA->m_height; j++)
464 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
465 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
466 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
469 [nsimage unlockFocus];
473 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
475 wxAutoNSAutoreleasePool pool;
478 wxCHECK_MSG(image.IsOk(), false, wxT("invalid image"));
479 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
481 m_refData = new wxBitmapRefData();
483 M_BITMAPDATA->m_width = image.GetWidth();
484 M_BITMAPDATA->m_height = image.GetHeight();
485 NSBitmapImageRep *bitmapImage = [[[NSBitmapImageRep alloc]
486 initWithBitmapDataPlanes: NULL
487 pixelsWide: image.GetWidth()
488 pixelsHigh: image.GetHeight()
493 colorSpaceName: NSCalibratedRGBColorSpace
494 bytesPerRow: image.GetWidth()*3
495 bitsPerPixel: 0] autorelease];
497 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
498 // so that the rows are aligned suitably for altivec by the OS (Tiger)
499 const int numBytes = image.GetWidth()*image.GetHeight()*3;
500 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
501 // TODO: Alpha and convert to desired depth
502 M_BITMAPDATA->m_depth = 24;
503 M_BITMAPDATA->m_ok = true;
504 M_BITMAPDATA->m_numColors = 0;
505 M_BITMAPDATA->m_quality = 0;
506 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(bitmapImage);
507 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
511 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
516 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
520 if([bitmapRep bitsPerPixel]!=bpp)
522 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
525 data.m_width = [bitmapRep pixelsWide];
526 data.m_height = [bitmapRep pixelsHigh];
527 data.m_stride = [bitmapRep bytesPerRow];
528 return [bitmapRep bitmapData];
530 // NOTE: It is up to the user to make sure they used the proper
531 // pixel format class that details what is actually inside the pixels
532 // We can only check to make sure that the total number of bits per
533 // pixel are being iterated over properly
534 // NSBitmapImageRep can contain grayscale or CMYK data and
535 // wxPixelDataBase doesn't really define the color format
538 void wxBitmap::UngetRawData(wxPixelDataBase& data)
542 // ========================================================================
544 // ========================================================================
546 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
550 m_cocoaNSBitmapImageRep = nil;
553 // Construct a mask from a bitmap and a colour indicating
554 // the transparent area
555 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
557 m_cocoaNSBitmapImageRep = nil;
558 Create(bitmap, colour);
561 // Construct a mask from a bitmap and a palette index indicating
562 // the transparent area
563 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
565 m_cocoaNSBitmapImageRep = nil;
567 Create(bitmap, paletteIndex);
570 // Construct a mask from a mono bitmap (copies the bitmap).
571 wxMask::wxMask(const wxBitmap& bitmap)
573 m_cocoaNSBitmapImageRep = nil;
579 wxMask::wxMask(const wxMask& src)
581 , m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
587 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
590 // Create a mask from a mono bitmap (copies the bitmap).
591 bool wxMask::Create(const wxBitmap& bitmap)
594 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
598 // Create a mask from a bitmap and a palette index indicating
599 // the transparent area
600 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
603 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
607 template <typename PixelData>
608 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
610 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
611 typename PixelData::Iterator p(srcData);
612 const int nRows = srcData.GetHeight();
613 const int nCols = srcData.GetWidth();
614 // Total number of bytes per destination column
615 const int dstRowLength = (nCols+7)/8;
616 // Number of source columns that fit into a byte in the destination
617 const int width_aligned = nCols/8*8;
618 for(int y=0; y<nRows; ++y)
620 typename PixelData::Iterator rowStart(p);
621 unsigned char *dstRow = dstData + y*dstRowLength;
622 for(int x=0; x<width_aligned; x+=8)
624 unsigned char *dstByte = dstRow + x/8;
626 // Take source RGB, compare it with the wxColour
627 for(int j=0; j<8; ++j, ++p)
630 ( p.Red()!=colour.Red()
631 || p.Green()!=colour.Green()
632 || p.Blue()!=colour.Blue()
636 // Handle the remaining 0-7 pixels in the row
637 unsigned char *dstByte = dstRow + width_aligned/8;
640 for(int j=0; j<(nCols%8); ++j, ++p)
643 ( p.Red()!=colour.Red()
644 || p.Green()!=colour.Green()
645 || p.Blue()!=colour.Blue()
649 p.OffsetY(srcData,1);
654 // Create a mask from a bitmap and a colour indicating
655 // the transparent area
656 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
658 wxAutoNSAutoreleasePool pool;
661 int bmpWidth = bitmap.GetWidth();
662 int bmpHeight = bitmap.GetHeight();
663 int dstRowLength = (bmpWidth+7)/8;
665 // Create a bitmap image rep with 1-bit per pixel data representing
666 // the alpha channel padded such that rows end on byte boundaries
667 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
668 // we must have at least one channel of non-alpha data. In order to
669 // make our life easy, we use planar data which results in two
670 // separate arrays. We don't need to touch the first because it
671 // should never be used. The second is the 1-bit "alpha" data.
672 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
673 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
674 pixelsHigh:bmpHeight bitsPerSample:1
675 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
676 colorSpaceName:NSCalibratedWhiteColorSpace
677 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
678 wxCHECK(maskRep,false);
680 // We need the source NSBitmapImageRep to detemine its pixel format
681 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
682 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
684 // Get a pointer to the destination data
685 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
686 [maskRep getBitmapDataPlanes:dstPlanes];
687 unsigned char *dstData = dstPlanes[1];
688 // The wxImage format (which we use whenever we imported from wxImage)
689 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
691 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
692 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
693 false, wxT("Unable to access raw data"));
695 // 32-bpp RGBx (x=throw away, no alpha)
696 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
698 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
699 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
700 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
701 false, wxT("Unable to access raw data"));
704 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
706 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
707 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
708 false, wxT("Unable to access raw data"));
710 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
711 // 8-bpp Grayscale, no alpha
712 { // Force all RGB to access the same grayscale component
713 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
714 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
715 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
716 false, wxT("Unable to access raw data"));
719 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
721 // maskRep was autoreleased in case we had to exit quickly
722 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);