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(const wxRect& rect) const
431 wxAutoNSAutoreleasePool pool;
434 NSImage *nsimage = GetNSImage(false);
437 NSRect imageRect = {{0,0}, [nsimage size]};
438 imageRect.origin.x = imageRect.size.width * rect.x / GetWidth();
439 imageRect.origin.y = imageRect.size.height * rect.y / GetHeight();
440 imageRect.size.width *= wx_static_cast(CGFloat, rect.width) / GetWidth();
441 imageRect.size.height *= wx_static_cast(CGFloat, rect.height) / GetHeight();
443 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
444 [nsimage unlockFocus];
446 wxBitmap newBitmap(newBitmapRep);
451 wxImage wxBitmap::ConvertToImage() const
453 wxAutoNSAutoreleasePool pool;
455 return /*wxImage(5,5)*/wxNullImage;
456 NSImage *nsimage = GetNSImage(false /* don't use mask */);
457 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
459 for(int i=0; i < M_BITMAPDATA->m_width; i++)
461 // Don't let the pool get too big as you'll notice we're creating
462 // two autoreleased NSColor objects with every iteration.
463 wxAutoNSAutoreleasePool loopPool;
464 for(int j=0; j < M_BITMAPDATA->m_height; j++)
466 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
467 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
468 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
471 [nsimage unlockFocus];
475 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
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
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 = 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( _T("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([src.m_cocoaNSBitmapImageRep retain])
588 [m_cocoaNSBitmapImageRep release];
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 = [maskRep retain];