]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/bitmap.mm
fix typo in last commit
[wxWidgets.git] / src / cocoa / bitmap.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/bitmap.mm
3 // Purpose: wxBitmap
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/07/19
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/bitmap.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/utils.h"
19 #include "wx/palette.h"
20 #include "wx/icon.h"
21 #include "wx/colour.h"
22 #include "wx/image.h"
23 #endif //WX_PRECOMP
24
25 #include "wx/xpmdecod.h"
26 #include "wx/rawbmp.h"
27
28 #include "wx/cocoa/autorelease.h"
29 #include "wx/cocoa/string.h"
30 #include "wx/cocoa/ObjcRef.h"
31
32 #import <AppKit/NSBitmapImageRep.h>
33 #import <AppKit/NSGraphics.h>
34 #import <AppKit/NSImage.h>
35 #import <AppKit/NSColor.h>
36
37 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
38
39 // ========================================================================
40 // wxBitmapRefData
41 // ========================================================================
42 class wxBitmapRefData: public wxGDIRefData
43 {
44 friend class wxBitmap;
45 public:
46 wxBitmapRefData();
47 wxBitmapRefData( const wxBitmapRefData& data );
48 virtual ~wxBitmapRefData();
49
50 virtual bool IsOk() const { return m_ok; }
51
52 protected:
53 int m_width;
54 int m_height;
55 int m_depth;
56 bool m_ok;
57 int m_numColors;
58 wxPalette m_bitmapPalette;
59 int m_quality;
60 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
61 wxMask *m_bitmapMask; // Optional mask
62 };
63
64 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
65
66 wxBitmapRefData::wxBitmapRefData()
67 {
68 m_ok = FALSE;
69 m_width = 0;
70 m_height = 0;
71 m_depth = 0;
72 m_quality = 0;
73 m_numColors = 0;
74 m_cocoaNSBitmapImageRep = nil;
75 m_bitmapMask = NULL;
76 }
77
78 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
79 {
80 wxAutoNSAutoreleasePool pool;
81
82 m_width = data.m_width;
83 m_height = data.m_height;
84 m_depth = data.m_depth;
85 m_ok = data.m_ok;
86 m_numColors = data.m_numColors;
87 m_bitmapPalette = data.m_bitmapPalette;
88 m_quality = data.m_quality;
89 m_cocoaNSBitmapImageRep = wxGCSafeRetain([[data.m_cocoaNSBitmapImageRep copyWithZone:nil] autorelease]);
90 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
91 }
92
93 wxBitmapRefData::~wxBitmapRefData()
94 {
95 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
96 m_cocoaNSBitmapImageRep = NULL;
97
98 delete m_bitmapMask;
99 m_bitmapMask = NULL;
100 }
101
102 // ========================================================================
103 // wxBitmap
104 // ========================================================================
105 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
106
107 wxBitmap::wxBitmap()
108 {
109 m_refData = NULL;
110 }
111
112 wxBitmap::~wxBitmap()
113 {
114 }
115
116 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
117 {
118 m_refData = new wxBitmapRefData;
119
120 M_BITMAPDATA->m_width = the_width ;
121 M_BITMAPDATA->m_height = the_height ;
122 M_BITMAPDATA->m_depth = no_bits ;
123 M_BITMAPDATA->m_numColors = 0;
124
125 /* TODO: create the bitmap from data */
126 }
127
128 wxBitmap::wxBitmap(int w, int h, int d)
129 {
130 (void)Create(w, h, d);
131 }
132
133 wxBitmap::wxBitmap(NSImage* cocoaNSImage)
134 {
135 (void) Create(cocoaNSImage);
136 }
137
138 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
139 {
140 (void) Create(cocoaNSBitmapImageRep);
141 }
142
143 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
144 {
145 (void) Create(data, type, width, height, depth);
146 }
147
148 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
149 {
150 LoadFile(filename, type);
151 }
152
153 wxGDIRefData *wxBitmap::CreateGDIRefData() const
154 {
155 return new wxBitmapRefData;
156 }
157
158 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
159 {
160 return new wxBitmapRefData(*(wxBitmapRefData*)data);
161 }
162
163 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
164 {
165 if(!M_BITMAPDATA)
166 return NULL;
167 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
168 }
169
170 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
171 {
172 if(!Ok())
173 return nil;
174 NSImage *nsimage = [[[NSImage alloc]
175 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
176 if(!nsimage)
177 return nil;
178 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
179 if(useMask && GetMask())
180 {
181 // Show before/after to prove that the bitmap itself is not changed
182 // even though we just composited onto the NSImage
183 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
184 NSImage *maskImage = [[NSImage alloc]
185 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
186 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
187 [nsimage lockFocus];
188 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
189 [nsimage unlockFocus];
190 [maskImage release];
191 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
192 }
193 return nsimage;
194 }
195
196 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
197 {
198 if(!M_BITMAPDATA)
199 return;
200 // NOTE: No checking is done to make sure width/height agree
201 wxGCSafeRetain(bitmapImageRep);
202 wxGCSafeRelease(M_BITMAPDATA->m_cocoaNSBitmapImageRep);
203 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
204 }
205
206 void wxBitmap::SetWidth(int w)
207 {
208 if (!M_BITMAPDATA)
209 m_refData = new wxBitmapRefData;
210
211 M_BITMAPDATA->m_width = w;
212 }
213
214 void wxBitmap::SetHeight(int h)
215 {
216 if (!M_BITMAPDATA)
217 m_refData = new wxBitmapRefData;
218
219 M_BITMAPDATA->m_height = h;
220 }
221
222 void wxBitmap::SetDepth(int d)
223 {
224 if (!M_BITMAPDATA)
225 m_refData = new wxBitmapRefData;
226
227 M_BITMAPDATA->m_depth = d;
228 }
229
230 void wxBitmap::SetQuality(int q)
231 {
232 if (!M_BITMAPDATA)
233 m_refData = new wxBitmapRefData;
234
235 M_BITMAPDATA->m_quality = q;
236 }
237
238 void wxBitmap::SetOk(bool isOk)
239 {
240 if (!M_BITMAPDATA)
241 m_refData = new wxBitmapRefData;
242
243 M_BITMAPDATA->m_ok = isOk;
244 }
245
246 void wxBitmap::SetPalette(const wxPalette& palette)
247 {
248 if (!M_BITMAPDATA)
249 m_refData = new wxBitmapRefData;
250
251 M_BITMAPDATA->m_bitmapPalette = palette ;
252 }
253
254 void wxBitmap::SetMask(wxMask *mask)
255 {
256 if (!M_BITMAPDATA)
257 m_refData = new wxBitmapRefData;
258
259 M_BITMAPDATA->m_bitmapMask = mask ;
260 }
261
262 wxPalette* wxBitmap::GetPalette() const
263 {
264 if(!m_refData)
265 return NULL;
266 return &M_BITMAPDATA->m_bitmapPalette;
267 }
268
269 wxMask* wxBitmap::GetMask() const
270 {
271 if(!m_refData)
272 return NULL;
273 return M_BITMAPDATA->m_bitmapMask;
274 }
275
276 int wxBitmap::GetDepth() const
277 {
278 if(!m_refData)
279 return 0;
280 return M_BITMAPDATA->m_depth;
281 }
282
283 int wxBitmap::GetWidth() const
284 {
285 if(!m_refData)
286 return 0;
287 return M_BITMAPDATA->m_width;
288 }
289
290 int wxBitmap::GetHeight() const
291 {
292 if(!m_refData)
293 return 0;
294 return M_BITMAPDATA->m_height;
295 }
296
297 bool wxBitmap::Create(int w, int h, int d)
298 {
299 wxAutoNSAutoreleasePool pool;
300
301 UnRef();
302
303 m_refData = new wxBitmapRefData;
304
305 M_BITMAPDATA->m_width = w;
306 M_BITMAPDATA->m_height = h;
307 M_BITMAPDATA->m_depth = d;
308
309 /* TODO: create new bitmap */
310 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
311 initWithBitmapDataPlanes: NULL
312 pixelsWide: w
313 pixelsHigh: h
314 bitsPerSample: 8
315 samplesPerPixel: 3
316 hasAlpha: NO
317 isPlanar: NO
318 colorSpaceName: NSCalibratedRGBColorSpace
319 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
320 // 10.4 will add padding bytes when 0 is used here
321 bitsPerPixel: 0] autorelease]);
322
323 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
324 M_BITMAPDATA->m_ok = true;
325 M_BITMAPDATA->m_numColors = 0;
326 M_BITMAPDATA->m_quality = 0;
327 M_BITMAPDATA->m_bitmapMask = NULL;
328
329 return M_BITMAPDATA->m_ok;
330 }
331
332 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
333 {
334 wxAutoNSAutoreleasePool pool;
335 UnRef();
336
337 m_refData = new wxBitmapRefData;
338
339 NSBitmapImageRep *imageRep = [NSBitmapImageRep
340 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
341
342 if(imageRep)
343 {
344 M_BITMAPDATA->m_width = [imageRep pixelsWide];
345 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
346 M_BITMAPDATA->m_depth = 24; // FIXME
347 M_BITMAPDATA->m_ok = true;
348 M_BITMAPDATA->m_numColors = 0;
349 M_BITMAPDATA->m_quality = 0;
350 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
351 M_BITMAPDATA->m_bitmapMask = NULL;
352 return true;
353 }
354 wxImage image;
355 if(!image.LoadFile(filename,type))
356 return false;
357 if(!image.Ok())
358 return false;
359 *this = wxBitmap(image);
360 return true;
361 }
362
363 bool wxBitmap::Create(NSImage* cocoaNSImage)
364 {
365 wxAutoNSAutoreleasePool pool;
366 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
367 return Create(bitmapImageRep);
368 }
369
370 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
371 {
372 UnRef();
373 m_refData = new wxBitmapRefData;
374 if(imageRep != nil)
375 {
376 M_BITMAPDATA->m_width = [imageRep pixelsWide];
377 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
378 M_BITMAPDATA->m_depth = [imageRep bitsPerPixel];
379 M_BITMAPDATA->m_ok = true;
380 M_BITMAPDATA->m_numColors = 0;
381 M_BITMAPDATA->m_quality = 0;
382 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
383 M_BITMAPDATA->m_bitmapMask = NULL;
384 return true;
385 }
386 else
387 return false;
388 }
389
390 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
391 {
392 UnRef();
393
394 m_refData = new wxBitmapRefData;
395
396 return false;
397 }
398
399 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
400 {
401 return false;
402 }
403
404 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
405 {
406 // Pool here due to lack of one during wx init phase
407 wxAutoNSAutoreleasePool pool;
408
409 UnRef();
410 if(!icon.GetNSImage());
411 [icon.GetNSImage() lockFocus];
412 NSRect imageRect;
413 imageRect.origin.x = imageRect.origin.y = 0.0;
414 imageRect.size = [icon.GetNSImage() size];
415 NSBitmapImageRep *newBitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease];
416 [icon.GetNSImage() unlockFocus];
417 if(!newBitmapRep)
418 return false;
419 m_refData = new wxBitmapRefData;
420 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(newBitmapRep);
421 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
422 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
423 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
424 M_BITMAPDATA->m_ok = true;
425 M_BITMAPDATA->m_numColors = 0;
426 M_BITMAPDATA->m_quality = 0;
427 M_BITMAPDATA->m_bitmapMask = NULL;
428 return true;
429 }
430
431 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
432 {
433 wxAutoNSAutoreleasePool pool;
434 if(!Ok())
435 return wxNullBitmap;
436 NSImage *nsimage = GetNSImage(false);
437
438 [nsimage lockFocus];
439 NSRect imageRect = {{0,0}, [nsimage size]};
440 imageRect.origin.x = imageRect.size.width * rect.x / GetWidth();
441 imageRect.origin.y = imageRect.size.height * rect.y / GetHeight();
442 imageRect.size.width *= static_cast<CGFloat>(rect.width) / GetWidth();
443 imageRect.size.height *= static_cast<CGFloat>(rect.height) / GetHeight();
444
445 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
446 [nsimage unlockFocus];
447
448 wxBitmap newBitmap(newBitmapRep);
449
450 return (newBitmap);
451 }
452
453 wxImage wxBitmap::ConvertToImage() const
454 {
455 wxAutoNSAutoreleasePool pool;
456 if(!Ok())
457 return /*wxImage(5,5)*/wxNullImage;
458 NSImage *nsimage = GetNSImage(false /* don't use mask */);
459 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
460 [nsimage lockFocus];
461 for(int i=0; i < M_BITMAPDATA->m_width; i++)
462 {
463 // Don't let the pool get too big as you'll notice we're creating
464 // two autoreleased NSColor objects with every iteration.
465 wxAutoNSAutoreleasePool loopPool;
466 for(int j=0; j < M_BITMAPDATA->m_height; j++)
467 {
468 NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1));
469 NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
470 newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0));
471 }
472 }
473 [nsimage unlockFocus];
474 return newImage;
475 }
476
477 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
478 {
479 wxAutoNSAutoreleasePool pool;
480 UnRef();
481
482 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
483 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
484
485 m_refData = new wxBitmapRefData();
486
487 M_BITMAPDATA->m_width = image.GetWidth();
488 M_BITMAPDATA->m_height = image.GetHeight();
489 NSBitmapImageRep *bitmapImage = [[[NSBitmapImageRep alloc]
490 initWithBitmapDataPlanes: NULL
491 pixelsWide: image.GetWidth()
492 pixelsHigh: image.GetHeight()
493 bitsPerSample: 8
494 samplesPerPixel: 3
495 hasAlpha: NO
496 isPlanar: NO
497 colorSpaceName: NSCalibratedRGBColorSpace
498 bytesPerRow: image.GetWidth()*3
499 bitsPerPixel: 0] autorelease];
500
501 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
502 // so that the rows are aligned suitably for altivec by the OS (Tiger)
503 const int numBytes = image.GetWidth()*image.GetHeight()*3;
504 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
505 // TODO: Alpha and convert to desired depth
506 M_BITMAPDATA->m_depth = 24;
507 M_BITMAPDATA->m_ok = true;
508 M_BITMAPDATA->m_numColors = 0;
509 M_BITMAPDATA->m_quality = 0;
510 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(bitmapImage);
511 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
512 return true;
513 }
514
515 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
516 {
517 if(!Ok())
518 return NULL;
519
520 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
521 if(!bitmapRep)
522 return NULL;
523
524 if([bitmapRep bitsPerPixel]!=bpp)
525 {
526 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
527 return NULL;
528 }
529 data.m_width = [bitmapRep pixelsWide];
530 data.m_height = [bitmapRep pixelsHigh];
531 data.m_stride = [bitmapRep bytesPerRow];
532 return [bitmapRep bitmapData];
533
534 // NOTE: It is up to the user to make sure they used the proper
535 // pixel format class that details what is actually inside the pixels
536 // We can only check to make sure that the total number of bits per
537 // pixel are being iterated over properly
538 // NSBitmapImageRep can contain grayscale or CMYK data and
539 // wxPixelDataBase doesn't really define the color format
540 }
541
542 void wxBitmap::UngetRawData(wxPixelDataBase& data)
543 { // TODO
544 }
545
546 // ========================================================================
547 // wxMask
548 // ========================================================================
549
550 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
551
552 wxMask::wxMask()
553 {
554 m_cocoaNSBitmapImageRep = nil;
555 }
556
557 // Construct a mask from a bitmap and a colour indicating
558 // the transparent area
559 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
560 {
561 m_cocoaNSBitmapImageRep = nil;
562 Create(bitmap, colour);
563 }
564
565 // Construct a mask from a bitmap and a palette index indicating
566 // the transparent area
567 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
568 {
569 m_cocoaNSBitmapImageRep = nil;
570
571 Create(bitmap, paletteIndex);
572 }
573
574 // Construct a mask from a mono bitmap (copies the bitmap).
575 wxMask::wxMask(const wxBitmap& bitmap)
576 {
577 m_cocoaNSBitmapImageRep = nil;
578
579 Create(bitmap);
580 }
581
582 // Copy constructor
583 wxMask::wxMask(const wxMask& src)
584 : wxObject(src)
585 , m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
586 {
587 }
588
589 wxMask::~wxMask()
590 {
591 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
592 }
593
594 // Create a mask from a mono bitmap (copies the bitmap).
595 bool wxMask::Create(const wxBitmap& bitmap)
596 {
597 // TODO
598 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
599 return FALSE;
600 }
601
602 // Create a mask from a bitmap and a palette index indicating
603 // the transparent area
604 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
605 {
606 // TODO
607 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
608 return FALSE;
609 }
610
611 template <typename PixelData>
612 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
613 {
614 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
615 typename PixelData::Iterator p(srcData);
616 const int nRows = srcData.GetHeight();
617 const int nCols = srcData.GetWidth();
618 // Total number of bytes per destination column
619 const int dstRowLength = (nCols+7)/8;
620 // Number of source columns that fit into a byte in the destination
621 const int width_aligned = nCols/8*8;
622 for(int y=0; y<nRows; ++y)
623 {
624 typename PixelData::Iterator rowStart(p);
625 unsigned char *dstRow = dstData + y*dstRowLength;
626 for(int x=0; x<width_aligned; x+=8)
627 {
628 unsigned char *dstByte = dstRow + x/8;
629 *dstByte = 0;
630 // Take source RGB, compare it with the wxColour
631 for(int j=0; j<8; ++j, ++p)
632 {
633 *dstByte +=
634 ( p.Red()!=colour.Red()
635 || p.Green()!=colour.Green()
636 || p.Blue()!=colour.Blue()
637 ) << (7-j);
638 }
639 }
640 // Handle the remaining 0-7 pixels in the row
641 unsigned char *dstByte = dstRow + width_aligned/8;
642 if(nCols%8>0)
643 *dstByte = 0;
644 for(int j=0; j<(nCols%8); ++j, ++p)
645 {
646 *dstByte +=
647 ( p.Red()!=colour.Red()
648 || p.Green()!=colour.Green()
649 || p.Blue()!=colour.Blue()
650 ) << (7-j);
651 }
652 p = rowStart;
653 p.OffsetY(srcData,1);
654 }
655 return true;
656 }
657
658 // Create a mask from a bitmap and a colour indicating
659 // the transparent area
660 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
661 {
662 wxAutoNSAutoreleasePool pool;
663 if(!bitmap.Ok())
664 return false;
665 int bmpWidth = bitmap.GetWidth();
666 int bmpHeight = bitmap.GetHeight();
667 int dstRowLength = (bmpWidth+7)/8;
668
669 // Create a bitmap image rep with 1-bit per pixel data representing
670 // the alpha channel padded such that rows end on byte boundaries
671 // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace
672 // we must have at least one channel of non-alpha data. In order to
673 // make our life easy, we use planar data which results in two
674 // separate arrays. We don't need to touch the first because it
675 // should never be used. The second is the 1-bit "alpha" data.
676 NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc]
677 initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth
678 pixelsHigh:bmpHeight bitsPerSample:1
679 samplesPerPixel:2 hasAlpha:YES isPlanar:YES
680 colorSpaceName:NSCalibratedWhiteColorSpace
681 bytesPerRow:dstRowLength bitsPerPixel:1] autorelease];
682 wxCHECK(maskRep,false);
683
684 // We need the source NSBitmapImageRep to detemine its pixel format
685 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
686 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
687
688 // Get a pointer to the destination data
689 unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL};
690 [maskRep getBitmapDataPlanes:dstPlanes];
691 unsigned char *dstData = dstPlanes[1];
692 // The wxImage format (which we use whenever we imported from wxImage)
693 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
694 {
695 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
696 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
697 false, wxT("Unable to access raw data"));
698 }
699 // 32-bpp RGBx (x=throw away, no alpha)
700 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
701 {
702 typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat;
703 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
704 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
705 false, wxT("Unable to access raw data"));
706 }
707 // 32-bpp RGBA
708 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
709 {
710 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
711 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
712 false, wxT("Unable to access raw data"));
713 }
714 else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO)
715 // 8-bpp Grayscale, no alpha
716 { // Force all RGB to access the same grayscale component
717 typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat;
718 wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
719 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
720 false, wxT("Unable to access raw data"));
721 }
722 else
723 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
724
725 // maskRep was autoreleased in case we had to exit quickly
726 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);
727 return true;
728 }