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 // ========================================================================
38 // ========================================================================
39 class wxBitmapRefData: public wxGDIRefData
41 friend class wxBitmap;
44 wxBitmapRefData( const wxBitmapRefData& data );
45 virtual ~wxBitmapRefData();
53 wxPalette m_bitmapPalette;
55 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
56 wxMask *m_bitmapMask; // Optional mask
59 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
61 wxBitmapRefData::wxBitmapRefData()
69 m_cocoaNSBitmapImageRep = nil;
73 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
75 m_width = data.m_width;
76 m_height = data.m_height;
77 m_depth = data.m_depth;
79 m_numColors = data.m_numColors;
80 m_bitmapPalette = data.m_bitmapPalette;
81 m_quality = data.m_quality;
82 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
83 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
86 wxBitmapRefData::~wxBitmapRefData()
88 [m_cocoaNSBitmapImageRep release];
89 m_cocoaNSBitmapImageRep = NULL;
95 // ========================================================================
97 // ========================================================================
98 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
105 wxBitmap::~wxBitmap()
109 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
111 m_refData = new wxBitmapRefData;
113 M_BITMAPDATA->m_width = the_width ;
114 M_BITMAPDATA->m_height = the_height ;
115 M_BITMAPDATA->m_depth = no_bits ;
116 M_BITMAPDATA->m_numColors = 0;
118 /* TODO: create the bitmap from data */
121 wxBitmap::wxBitmap(int w, int h, int d)
123 (void)Create(w, h, d);
126 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
128 (void) Create(data, type, width, height, depth);
131 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
133 LoadFile(filename, type);
136 wxObjectRefData *wxBitmap::CreateRefData() const
138 return new wxBitmapRefData;
141 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
143 return new wxBitmapRefData(*(wxBitmapRefData*)data);
146 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
150 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
153 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
157 NSImage *nsimage = [[[NSImage alloc]
158 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
161 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
162 if(useMask && GetMask())
164 // Show before/after to prove that the bitmap itself is not changed
165 // even though we just composited onto the NSImage
166 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
167 NSImage *maskImage = [[NSImage alloc]
168 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
169 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
171 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
172 [nsimage unlockFocus];
174 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
179 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
183 // NOTE: No checking is done to make sure width/height agree
184 [bitmapImageRep retain];
185 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
186 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
189 void wxBitmap::SetWidth(int w)
192 m_refData = new wxBitmapRefData;
194 M_BITMAPDATA->m_width = w;
197 void wxBitmap::SetHeight(int h)
200 m_refData = new wxBitmapRefData;
202 M_BITMAPDATA->m_height = h;
205 void wxBitmap::SetDepth(int d)
208 m_refData = new wxBitmapRefData;
210 M_BITMAPDATA->m_depth = d;
213 void wxBitmap::SetQuality(int q)
216 m_refData = new wxBitmapRefData;
218 M_BITMAPDATA->m_quality = q;
221 void wxBitmap::SetOk(bool isOk)
224 m_refData = new wxBitmapRefData;
226 M_BITMAPDATA->m_ok = isOk;
229 void wxBitmap::SetPalette(const wxPalette& palette)
232 m_refData = new wxBitmapRefData;
234 M_BITMAPDATA->m_bitmapPalette = palette ;
237 void wxBitmap::SetMask(wxMask *mask)
240 m_refData = new wxBitmapRefData;
242 M_BITMAPDATA->m_bitmapMask = mask ;
245 bool wxBitmap::Ok() const
247 return m_refData && M_BITMAPDATA->m_ok;
250 wxPalette* wxBitmap::GetPalette() const
254 return &M_BITMAPDATA->m_bitmapPalette;
257 wxMask* wxBitmap::GetMask() const
261 return M_BITMAPDATA->m_bitmapMask;
264 int wxBitmap::GetDepth() const
268 return M_BITMAPDATA->m_depth;
271 int wxBitmap::GetWidth() const
275 return M_BITMAPDATA->m_width;
278 int wxBitmap::GetHeight() const
282 return M_BITMAPDATA->m_height;
285 bool wxBitmap::Create(int w, int h, int d)
289 m_refData = new wxBitmapRefData;
291 M_BITMAPDATA->m_width = w;
292 M_BITMAPDATA->m_height = h;
293 M_BITMAPDATA->m_depth = d;
295 /* TODO: create new bitmap */
296 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
297 initWithBitmapDataPlanes: NULL
304 colorSpaceName: NSCalibratedRGBColorSpace
305 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
306 // 10.4 will add padding bytes when 0 is used here
309 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
310 M_BITMAPDATA->m_ok = true;
311 M_BITMAPDATA->m_numColors = 0;
312 M_BITMAPDATA->m_quality = 0;
313 M_BITMAPDATA->m_bitmapMask = NULL;
315 return M_BITMAPDATA->m_ok;
318 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
320 wxAutoNSAutoreleasePool pool;
323 m_refData = new wxBitmapRefData;
325 NSBitmapImageRep *imageRep = [NSBitmapImageRep
326 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
330 M_BITMAPDATA->m_width = [imageRep pixelsWide];
331 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
332 M_BITMAPDATA->m_depth = 24; // FIXME
333 M_BITMAPDATA->m_ok = true;
334 M_BITMAPDATA->m_numColors = 0;
335 M_BITMAPDATA->m_quality = 0;
336 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
337 M_BITMAPDATA->m_bitmapMask = NULL;
341 if(!image.LoadFile(filename,type))
345 *this = wxBitmap(image);
349 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
353 m_refData = new wxBitmapRefData;
358 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
363 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
366 if(!icon.GetNSImage());
367 [icon.GetNSImage() lockFocus];
369 imageRect.origin.x = imageRect.origin.y = 0.0;
370 imageRect.size = [icon.GetNSImage() size];
371 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
372 [icon.GetNSImage() unlockFocus];
375 m_refData = new wxBitmapRefData;
376 M_BITMAPDATA->m_cocoaNSBitmapImageRep = newBitmapRep;
377 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
378 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
379 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
380 M_BITMAPDATA->m_ok = true;
381 M_BITMAPDATA->m_numColors = 0;
382 M_BITMAPDATA->m_quality = 0;
383 M_BITMAPDATA->m_bitmapMask = NULL;
387 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
392 wxImage wxBitmap::ConvertToImage() const
394 wxAutoNSAutoreleasePool pool;
396 return /*wxImage(5,5)*/wxNullImage;
397 NSImage *nsimage = GetNSImage(false /* don't use mask */);
398 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
400 for(int i=0; i < M_BITMAPDATA->m_width; i++)
402 // Don't let the pool get too big as you'll notice we're creating
403 // two autoreleased NSColor objects with every iteration.
404 wxAutoNSAutoreleasePool loopPool;
405 for(int j=0; j < M_BITMAPDATA->m_height; j++)
407 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
408 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
409 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
412 [nsimage unlockFocus];
416 bool wxBitmap::CreateFromXpm(const char **xpm)
418 #if wxUSE_IMAGE && wxUSE_XPM
421 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") );
423 wxXPMDecoder decoder;
424 wxImage img = decoder.ReadData(xpm);
425 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") );
427 *this = wxBitmap(img);
434 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
438 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
439 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
441 m_refData = new wxBitmapRefData();
443 M_BITMAPDATA->m_width = image.GetWidth();
444 M_BITMAPDATA->m_height = image.GetHeight();
445 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
446 initWithBitmapDataPlanes: NULL
447 pixelsWide: image.GetWidth()
448 pixelsHigh: image.GetHeight()
453 colorSpaceName: NSCalibratedRGBColorSpace
454 bytesPerRow: image.GetWidth()*3
457 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
458 // so that the rows are aligned suitably for altivec by the OS (Tiger)
459 const int numBytes = image.GetWidth()*image.GetHeight()*3;
460 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
461 // TODO: Alpha and convert to desired depth
462 M_BITMAPDATA->m_depth = 24;
463 M_BITMAPDATA->m_ok = true;
464 M_BITMAPDATA->m_numColors = 0;
465 M_BITMAPDATA->m_quality = 0;
466 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
467 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
471 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
476 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
480 if([bitmapRep bitsPerPixel]!=bpp)
482 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
485 data.m_width = [bitmapRep pixelsWide];
486 data.m_height = [bitmapRep pixelsHigh];
487 data.m_stride = [bitmapRep bytesPerRow];
488 return [bitmapRep bitmapData];
490 // NOTE: It is up to the user to make sure they used the proper
491 // pixel format class that details what is actually inside the pixels
492 // We can only check to make sure that the total number of bits per
493 // pixel are being iterated over properly
494 // NSBitmapImageRep can contain grayscale or CMYK data and
495 // wxPixelDataBase doesn't really define the color format
498 void wxBitmap::UngetRawData(wxPixelDataBase& data)
502 void wxBitmap::UseAlpha()
506 // ========================================================================
508 // ========================================================================
510 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
514 m_cocoaNSBitmapImageRep = nil;
517 // Construct a mask from a bitmap and a colour indicating
518 // the transparent area
519 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
521 m_cocoaNSBitmapImageRep = nil;
522 Create(bitmap, colour);
525 // Construct a mask from a bitmap and a palette index indicating
526 // the transparent area
527 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
529 m_cocoaNSBitmapImageRep = nil;
531 Create(bitmap, paletteIndex);
534 // Construct a mask from a mono bitmap (copies the bitmap).
535 wxMask::wxMask(const wxBitmap& bitmap)
537 m_cocoaNSBitmapImageRep = nil;
544 [m_cocoaNSBitmapImageRep release];
547 // Create a mask from a mono bitmap (copies the bitmap).
548 bool wxMask::Create(const wxBitmap& bitmap)
554 // Create a mask from a bitmap and a palette index indicating
555 // the transparent area
556 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
562 template <typename PixelData>
563 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
565 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
566 typename PixelData::Iterator p(srcData);
567 const int nRows = srcData.GetHeight();
568 const int nCols = srcData.GetWidth();
569 // Total number of bytes per destination column
570 const int dstRowLength = (nCols+7)/8;
571 // Number of source columns that fit into a byte in the destination
572 const int width_aligned = nCols/8*8;
573 for(int y=0; y<nRows; ++y)
575 typename PixelData::Iterator rowStart(p);
576 unsigned char *dstRow = dstData + y*dstRowLength;
577 for(int x=0; x<width_aligned; x+=8)
579 unsigned char *dstByte = dstRow + x/8;
581 // Take source RGB, compare it with the wxColour
582 for(int j=0; j<8; ++j, ++p)
585 ( p.Red()!=colour.Red()
586 || p.Green()!=colour.Green()
587 || p.Blue()!=colour.Blue()
591 // Handle the remaining 0-7 pixels in the row
592 unsigned char *dstByte = dstRow + width_aligned/8;
595 for(int j=0; j<(nCols%8); ++j, ++p)
598 ( p.Red()!=colour.Red()
599 || p.Green()!=colour.Green()
600 || p.Blue()!=colour.Blue()
604 p.OffsetY(srcData,1);
609 // Create a mask from a bitmap and a colour indicating
610 // the transparent area
611 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
613 wxAutoNSAutoreleasePool pool;
616 int bmpWidth = bitmap.GetWidth();
617 int bmpHeight = bitmap.GetHeight();
618 int dstRowLength = (bmpWidth+7)/8;
620 // Create a bitmap image rep with 1-bit per pixel data representing
621 // the alpha channel padded such that rows end on byte boundaries
622 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
623 // we must have at least one channel of non-alpha data. In order to
624 // make our life easy, we use planar data which results in two
625 // separate arrays. We don't need to touch the first because it
626 // should never be used. The second is the 1-bit "alpha" data.
627 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
628 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
629 pixelsHigh:bmpHeight bitsPerSample:1
630 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
631 colorSpaceName:NSCalibratedWhiteColorSpace
632 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
633 wxCHECK(maskRep,false);
635 // We need the source NSBitmapImageRep to detemine its pixel format
636 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
637 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
639 // Get a pointer to the destination data
640 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
641 [maskRep getBitmapDataPlanes:dstPlanes];
642 unsigned char *dstData = dstPlanes[1];
643 // The wxImage format (which we use whenever we imported from wxImage)
644 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
646 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
647 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
648 false, wxT("Unable to access raw data"));
650 // 32-bpp RGBx (x=throw away, no alpha)
651 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
653 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
654 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
655 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
656 false, wxT("Unable to access raw data"));
659 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
661 wxPixelData<wxBitmap,wxAlphaPixelFormat> 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];