1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.cpp
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/palette.h"
18 #include "wx/colour.h"
20 #include "wx/bitmap.h"
22 #include "wx/xpmdecod.h"
23 #include "wx/rawbmp.h"
25 #include "wx/cocoa/autorelease.h"
26 #include "wx/cocoa/string.h"
28 #import <AppKit/NSBitmapImageRep.h>
29 #import <AppKit/NSGraphics.h>
30 #import <AppKit/NSImage.h>
31 #import <AppKit/NSColor.h>
33 // ========================================================================
35 // ========================================================================
36 class wxBitmapRefData: public wxGDIRefData
38 friend class wxBitmap;
41 wxBitmapRefData( const wxBitmapRefData& data );
42 virtual ~wxBitmapRefData();
50 wxPalette m_bitmapPalette;
52 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
53 wxMask *m_bitmapMask; // Optional mask
56 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
58 wxBitmapRefData::wxBitmapRefData()
66 m_cocoaNSBitmapImageRep = nil;
70 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
72 m_width = data.m_width;
73 m_height = data.m_height;
74 m_depth = data.m_depth;
76 m_numColors = data.m_numColors;
77 m_bitmapPalette = data.m_bitmapPalette;
78 m_quality = data.m_quality;
79 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
80 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
83 wxBitmapRefData::~wxBitmapRefData()
85 [m_cocoaNSBitmapImageRep release];
86 m_cocoaNSBitmapImageRep = NULL;
92 // ========================================================================
94 // ========================================================================
95 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
102 wxBitmap::~wxBitmap()
106 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
108 m_refData = new wxBitmapRefData;
110 M_BITMAPDATA->m_width = the_width ;
111 M_BITMAPDATA->m_height = the_height ;
112 M_BITMAPDATA->m_depth = no_bits ;
113 M_BITMAPDATA->m_numColors = 0;
115 /* TODO: create the bitmap from data */
118 wxBitmap::wxBitmap(int w, int h, int d)
120 (void)Create(w, h, d);
123 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
125 (void) Create(data, type, width, height, depth);
128 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
130 LoadFile(filename, type);
133 wxObjectRefData *wxBitmap::CreateRefData() const
135 return new wxBitmapRefData;
138 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
140 return new wxBitmapRefData(*(wxBitmapRefData*)data);
143 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
147 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
150 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
154 NSImage *nsimage = [[[NSImage alloc]
155 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
158 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
159 if(useMask && GetMask())
161 // Show before/after to prove that the bitmap itself is not changed
162 // even though we just composited onto the NSImage
163 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
164 NSImage *maskImage = [[NSImage alloc]
165 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
166 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
168 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
169 [nsimage unlockFocus];
171 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
176 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
180 // NOTE: No checking is done to make sure width/height agree
181 [bitmapImageRep retain];
182 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
183 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
186 void wxBitmap::SetWidth(int w)
189 m_refData = new wxBitmapRefData;
191 M_BITMAPDATA->m_width = w;
194 void wxBitmap::SetHeight(int h)
197 m_refData = new wxBitmapRefData;
199 M_BITMAPDATA->m_height = h;
202 void wxBitmap::SetDepth(int d)
205 m_refData = new wxBitmapRefData;
207 M_BITMAPDATA->m_depth = d;
210 void wxBitmap::SetQuality(int q)
213 m_refData = new wxBitmapRefData;
215 M_BITMAPDATA->m_quality = q;
218 void wxBitmap::SetOk(bool isOk)
221 m_refData = new wxBitmapRefData;
223 M_BITMAPDATA->m_ok = isOk;
226 void wxBitmap::SetPalette(const wxPalette& palette)
229 m_refData = new wxBitmapRefData;
231 M_BITMAPDATA->m_bitmapPalette = palette ;
234 void wxBitmap::SetMask(wxMask *mask)
237 m_refData = new wxBitmapRefData;
239 M_BITMAPDATA->m_bitmapMask = mask ;
242 bool wxBitmap::Ok() const
244 return m_refData && M_BITMAPDATA->m_ok;
247 wxPalette* wxBitmap::GetPalette() const
251 return &M_BITMAPDATA->m_bitmapPalette;
254 wxMask* wxBitmap::GetMask() const
258 return M_BITMAPDATA->m_bitmapMask;
261 int wxBitmap::GetDepth() const
265 return M_BITMAPDATA->m_depth;
268 int wxBitmap::GetWidth() const
272 return M_BITMAPDATA->m_width;
275 int wxBitmap::GetHeight() const
279 return M_BITMAPDATA->m_height;
282 bool wxBitmap::Create(int w, int h, int d)
286 m_refData = new wxBitmapRefData;
288 M_BITMAPDATA->m_width = w;
289 M_BITMAPDATA->m_height = h;
290 M_BITMAPDATA->m_depth = d;
292 /* TODO: create new bitmap */
293 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
294 initWithBitmapDataPlanes: NULL
301 colorSpaceName: NSCalibratedRGBColorSpace
302 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
303 // 10.4 will add padding bytes when 0 is used here
306 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
307 M_BITMAPDATA->m_ok = true;
308 M_BITMAPDATA->m_numColors = 0;
309 M_BITMAPDATA->m_quality = 0;
310 M_BITMAPDATA->m_bitmapMask = NULL;
312 return M_BITMAPDATA->m_ok;
315 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
317 wxAutoNSAutoreleasePool pool;
320 m_refData = new wxBitmapRefData;
322 NSBitmapImageRep *imageRep = [NSBitmapImageRep
323 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
327 M_BITMAPDATA->m_width = [imageRep pixelsWide];
328 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
329 M_BITMAPDATA->m_depth = 24; // FIXME
330 M_BITMAPDATA->m_ok = true;
331 M_BITMAPDATA->m_numColors = 0;
332 M_BITMAPDATA->m_quality = 0;
333 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
334 M_BITMAPDATA->m_bitmapMask = NULL;
338 if(!image.LoadFile(filename,type))
342 *this = wxBitmap(image);
346 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
350 m_refData = new wxBitmapRefData;
355 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
360 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
363 if(!icon.GetNSImage());
364 [icon.GetNSImage() lockFocus];
366 imageRect.origin.x = imageRect.origin.y = 0.0;
367 imageRect.size = [icon.GetNSImage() size];
368 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
369 [icon.GetNSImage() unlockFocus];
372 m_refData = new wxBitmapRefData;
373 M_BITMAPDATA->m_cocoaNSBitmapImageRep = newBitmapRep;
374 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
375 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
376 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
377 M_BITMAPDATA->m_ok = true;
378 M_BITMAPDATA->m_numColors = 0;
379 M_BITMAPDATA->m_quality = 0;
380 M_BITMAPDATA->m_bitmapMask = NULL;
384 wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
389 wxImage wxBitmap::ConvertToImage() const
391 wxAutoNSAutoreleasePool pool;
393 return /*wxImage(5,5)*/wxNullImage;
394 NSImage *nsimage = GetNSImage(false /* don't use mask */);
395 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
397 for(int i=0; i < M_BITMAPDATA->m_width; i++)
399 // Don't let the pool get too big as you'll notice we're creating
400 // two autoreleased NSColor objects with every iteration.
401 wxAutoNSAutoreleasePool loopPool;
402 for(int j=0; j < M_BITMAPDATA->m_height; j++)
404 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
405 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
406 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
409 [nsimage unlockFocus];
413 bool wxBitmap::CreateFromXpm(const char **xpm)
415 #if wxUSE_IMAGE && wxUSE_XPM
418 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") );
420 wxXPMDecoder decoder;
421 wxImage img = decoder.ReadData(xpm);
422 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") );
424 *this = wxBitmap(img);
431 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
435 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
436 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
438 m_refData = new wxBitmapRefData();
440 M_BITMAPDATA->m_width = image.GetWidth();
441 M_BITMAPDATA->m_height = image.GetHeight();
442 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
443 initWithBitmapDataPlanes: NULL
444 pixelsWide: image.GetWidth()
445 pixelsHigh: image.GetHeight()
450 colorSpaceName: NSCalibratedRGBColorSpace
451 bytesPerRow: image.GetWidth()*3
454 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
455 // so that the rows are aligned suitably for altivec by the OS (Tiger)
456 const int numBytes = image.GetWidth()*image.GetHeight()*3;
457 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
458 // TODO: Alpha and convert to desired depth
459 M_BITMAPDATA->m_depth = 24;
460 M_BITMAPDATA->m_ok = true;
461 M_BITMAPDATA->m_numColors = 0;
462 M_BITMAPDATA->m_quality = 0;
463 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
464 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
468 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
473 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
477 if([bitmapRep bitsPerPixel]!=bpp)
479 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
482 data.m_width = [bitmapRep pixelsWide];
483 data.m_height = [bitmapRep pixelsHigh];
484 data.m_stride = [bitmapRep bytesPerRow];
485 return [bitmapRep bitmapData];
487 // NOTE: It is up to the user to make sure they used the proper
488 // pixel format class that details what is actually inside the pixels
489 // We can only check to make sure that the total number of bits per
490 // pixel are being iterated over properly
491 // NSBitmapImageRep can contain grayscale or CMYK data and
492 // wxPixelDataBase doesn't really define the color format
495 void wxBitmap::UngetRawData(wxPixelDataBase& data)
499 void wxBitmap::UseAlpha()
503 // ========================================================================
505 // ========================================================================
507 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
511 m_cocoaNSBitmapImageRep = nil;
514 // Construct a mask from a bitmap and a colour indicating
515 // the transparent area
516 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
518 m_cocoaNSBitmapImageRep = nil;
519 Create(bitmap, colour);
522 // Construct a mask from a bitmap and a palette index indicating
523 // the transparent area
524 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
526 m_cocoaNSBitmapImageRep = nil;
528 Create(bitmap, paletteIndex);
531 // Construct a mask from a mono bitmap (copies the bitmap).
532 wxMask::wxMask(const wxBitmap& bitmap)
534 m_cocoaNSBitmapImageRep = nil;
541 [m_cocoaNSBitmapImageRep release];
544 // Create a mask from a mono bitmap (copies the bitmap).
545 bool wxMask::Create(const wxBitmap& bitmap)
551 // Create a mask from a bitmap and a palette index indicating
552 // the transparent area
553 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
559 template <typename PixelData>
560 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
562 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
563 typename PixelData::Iterator p(srcData);
564 const int nRows = srcData.GetHeight();
565 const int nCols = srcData.GetWidth();
566 // Total number of bytes per destination column
567 const int dstRowLength = (nCols+7)/8;
568 // Number of source columns that fit into a byte in the destination
569 const int width_aligned = nCols/8*8;
570 for(int y=0; y<nRows; ++y)
572 typename PixelData::Iterator rowStart(p);
573 unsigned char *dstRow = dstData + y*dstRowLength;
574 for(int x=0; x<width_aligned; x+=8)
576 unsigned char *dstByte = dstRow + x/8;
578 // Take source RGB, compare it with the wxColour
579 for(int j=0; j<8; ++j, ++p)
582 ( p.Red()!=colour.Red()
583 || p.Green()!=colour.Green()
584 || p.Blue()!=colour.Blue()
588 // Handle the remaining 0-7 pixels in the row
589 unsigned char *dstByte = dstRow + width_aligned/8;
592 for(int j=0; j<(nCols%8); ++j, ++p)
595 ( p.Red()!=colour.Red()
596 || p.Green()!=colour.Green()
597 || p.Blue()!=colour.Blue()
601 p.OffsetY(srcData,1);
606 // Create a mask from a bitmap and a colour indicating
607 // the transparent area
608 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
610 wxAutoNSAutoreleasePool pool;
613 int bmpWidth = bitmap.GetWidth();
614 int bmpHeight = bitmap.GetHeight();
615 int dstRowLength = (bmpWidth+7)/8;
617 // Create a bitmap image rep with 1-bit per pixel data representing
618 // the alpha channel padded such that rows end on byte boundaries
619 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
620 // we must have at least one channel of non-alpha data. In order to
621 // make our life easy, we use planar data which results in two
622 // separate arrays. We don't need to touch the first because it
623 // should never be used. The second is the 1-bit "alpha" data.
624 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
625 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
626 pixelsHigh:bmpHeight bitsPerSample:1
627 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
628 colorSpaceName:NSCalibratedWhiteColorSpace
629 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
630 wxCHECK(maskRep,false);
632 // We need the source NSBitmapImageRep to detemine its pixel format
633 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
634 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
636 // Get a pointer to the destination data
637 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
638 [maskRep getBitmapDataPlanes:dstPlanes];
639 unsigned char *dstData = dstPlanes[1];
640 // The wxImage format (which we use whenever we imported from wxImage)
641 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
643 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
644 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
645 false, wxT("Unable to access raw data"));
647 // 32-bpp RGBx (x=throw away, no alpha)
648 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
650 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
651 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
652 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
653 false, wxT("Unable to access raw data"));
656 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
658 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
659 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
660 false, wxT("Unable to access raw data"));
663 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
665 // maskRep was autoreleased in case we had to exit quickly
666 m_cocoaNSBitmapImageRep = [maskRep retain];