]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/bitmap.mm
Increase the number of visible OS X combo items.
[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 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/bitmap.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/log.h"
17 #include "wx/utils.h"
18 #include "wx/palette.h"
19 #include "wx/icon.h"
20 #include "wx/colour.h"
21 #include "wx/image.h"
22 #endif //WX_PRECOMP
23
24 #include "wx/xpmdecod.h"
25 #include "wx/rawbmp.h"
26
27 #include "wx/cocoa/autorelease.h"
28 #include "wx/cocoa/string.h"
29 #include "wx/cocoa/ObjcRef.h"
30
31 #import <AppKit/NSBitmapImageRep.h>
32 #import <AppKit/NSGraphics.h>
33 #import <AppKit/NSImage.h>
34 #import <AppKit/NSColor.h>
35
36 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
37
38 // ========================================================================
39 // wxBitmapRefData
40 // ========================================================================
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 wxBitmapRefData::wxBitmapRefData()
65 {
66 m_ok = FALSE;
67 m_width = 0;
68 m_height = 0;
69 m_depth = 0;
70 m_quality = 0;
71 m_numColors = 0;
72 m_cocoaNSBitmapImageRep = nil;
73 m_bitmapMask = NULL;
74 }
75
76 wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
77 {
78 wxAutoNSAutoreleasePool pool;
79
80 m_width = data.m_width;
81 m_height = data.m_height;
82 m_depth = data.m_depth;
83 m_ok = data.m_ok;
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;
89 }
90
91 wxBitmapRefData::~wxBitmapRefData()
92 {
93 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
94 m_cocoaNSBitmapImageRep = NULL;
95
96 delete m_bitmapMask;
97 m_bitmapMask = NULL;
98 }
99
100 // ========================================================================
101 // wxBitmap
102 // ========================================================================
103
104 #define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
105
106 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
107
108 wxBitmap::wxBitmap()
109 {
110 m_refData = NULL;
111 }
112
113 wxBitmap::~wxBitmap()
114 {
115 }
116
117 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
118 {
119 m_refData = new wxBitmapRefData;
120
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;
125
126 /* TODO: create the bitmap from data */
127 }
128
129 wxBitmap::wxBitmap(NSImage* cocoaNSImage)
130 {
131 (void) Create(cocoaNSImage);
132 }
133
134 wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
135 {
136 (void) Create(cocoaNSBitmapImageRep);
137 }
138
139 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
140 {
141 (void) Create(data, type, width, height, depth);
142 }
143
144 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
145 {
146 LoadFile(filename, type);
147 }
148
149 wxGDIRefData *wxBitmap::CreateGDIRefData() const
150 {
151 return new wxBitmapRefData;
152 }
153
154 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
155 {
156 return new wxBitmapRefData(*(wxBitmapRefData*)data);
157 }
158
159 WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
160 {
161 if(!M_BITMAPDATA)
162 return NULL;
163 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
164 }
165
166 WX_NSImage wxBitmap::GetNSImage(bool useMask) const
167 {
168 if(!IsOk())
169 return nil;
170 NSImage *nsimage = [[[NSImage alloc]
171 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
172 if(!nsimage)
173 return nil;
174 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
175 if(useMask && GetMask())
176 {
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()];
183 [nsimage lockFocus];
184 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
185 [nsimage unlockFocus];
186 [maskImage release];
187 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
188 }
189 return nsimage;
190 }
191
192 void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
193 {
194 if(!M_BITMAPDATA)
195 return;
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;
200 }
201
202 void wxBitmap::SetWidth(int w)
203 {
204 if (!M_BITMAPDATA)
205 m_refData = new wxBitmapRefData;
206
207 M_BITMAPDATA->m_width = w;
208 }
209
210 void wxBitmap::SetHeight(int h)
211 {
212 if (!M_BITMAPDATA)
213 m_refData = new wxBitmapRefData;
214
215 M_BITMAPDATA->m_height = h;
216 }
217
218 void wxBitmap::SetDepth(int d)
219 {
220 if (!M_BITMAPDATA)
221 m_refData = new wxBitmapRefData;
222
223 M_BITMAPDATA->m_depth = d;
224 }
225
226 void wxBitmap::SetQuality(int q)
227 {
228 if (!M_BITMAPDATA)
229 m_refData = new wxBitmapRefData;
230
231 M_BITMAPDATA->m_quality = q;
232 }
233
234 void wxBitmap::SetOk(bool isOk)
235 {
236 if (!M_BITMAPDATA)
237 m_refData = new wxBitmapRefData;
238
239 M_BITMAPDATA->m_ok = isOk;
240 }
241
242 void wxBitmap::SetPalette(const wxPalette& palette)
243 {
244 if (!M_BITMAPDATA)
245 m_refData = new wxBitmapRefData;
246
247 M_BITMAPDATA->m_bitmapPalette = palette ;
248 }
249
250 void wxBitmap::SetMask(wxMask *mask)
251 {
252 if (!M_BITMAPDATA)
253 m_refData = new wxBitmapRefData;
254
255 M_BITMAPDATA->m_bitmapMask = mask ;
256 }
257
258 wxPalette* wxBitmap::GetPalette() const
259 {
260 if(!m_refData)
261 return NULL;
262 return &M_BITMAPDATA->m_bitmapPalette;
263 }
264
265 wxMask* wxBitmap::GetMask() const
266 {
267 if(!m_refData)
268 return NULL;
269 return M_BITMAPDATA->m_bitmapMask;
270 }
271
272 int wxBitmap::GetDepth() const
273 {
274 if(!m_refData)
275 return 0;
276 return M_BITMAPDATA->m_depth;
277 }
278
279 int wxBitmap::GetWidth() const
280 {
281 if(!m_refData)
282 return 0;
283 return M_BITMAPDATA->m_width;
284 }
285
286 int wxBitmap::GetHeight() const
287 {
288 if(!m_refData)
289 return 0;
290 return M_BITMAPDATA->m_height;
291 }
292
293 bool wxBitmap::Create(int w, int h, int d)
294 {
295 wxAutoNSAutoreleasePool pool;
296
297 UnRef();
298
299 m_refData = new wxBitmapRefData;
300
301 M_BITMAPDATA->m_width = w;
302 M_BITMAPDATA->m_height = h;
303 M_BITMAPDATA->m_depth = d;
304
305 /* TODO: create new bitmap */
306 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
307 initWithBitmapDataPlanes: NULL
308 pixelsWide: w
309 pixelsHigh: h
310 bitsPerSample: 8
311 samplesPerPixel: 3
312 hasAlpha: NO
313 isPlanar: NO
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]);
318
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;
324
325 return M_BITMAPDATA->m_ok;
326 }
327
328 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
329 {
330 wxAutoNSAutoreleasePool pool;
331 UnRef();
332
333 m_refData = new wxBitmapRefData;
334
335 NSBitmapImageRep *imageRep = [NSBitmapImageRep
336 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
337
338 if(imageRep)
339 {
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;
348 return true;
349 }
350 wxImage image;
351 if(!image.LoadFile(filename,type))
352 return false;
353 if(!image.IsOk())
354 return false;
355 *this = wxBitmap(image);
356 return true;
357 }
358
359 bool wxBitmap::Create(NSImage* cocoaNSImage)
360 {
361 wxAutoNSAutoreleasePool pool;
362 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
363 return Create(bitmapImageRep);
364 }
365
366 bool wxBitmap::Create(NSBitmapImageRep *imageRep)
367 {
368 UnRef();
369 m_refData = new wxBitmapRefData;
370 if(imageRep != nil)
371 {
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;
380 return true;
381 }
382 else
383 return false;
384 }
385
386 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
387 {
388 UnRef();
389
390 m_refData = new wxBitmapRefData;
391
392 return false;
393 }
394
395 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
396 {
397 return false;
398 }
399
400 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
401 {
402 // Pool here due to lack of one during wx init phase
403 wxAutoNSAutoreleasePool pool;
404
405 UnRef();
406 if(!icon.GetNSImage());
407 [icon.GetNSImage() lockFocus];
408 NSRect imageRect;
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];
413 if(!newBitmapRep)
414 return false;
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;
424 return true;
425 }
426
427 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
428 {
429 wxAutoNSAutoreleasePool pool;
430 if(!IsOk())
431 return wxNullBitmap;
432 NSImage *nsimage = GetNSImage(false);
433
434 [nsimage lockFocus];
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();
440
441 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
442 [nsimage unlockFocus];
443
444 wxBitmap newBitmap(newBitmapRep);
445
446 return (newBitmap);
447 }
448
449 wxImage wxBitmap::ConvertToImage() const
450 {
451 wxAutoNSAutoreleasePool pool;
452 if(!IsOk())
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);
456 [nsimage lockFocus];
457 for(int i=0; i < M_BITMAPDATA->m_width; i++)
458 {
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++)
463 {
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));
467 }
468 }
469 [nsimage unlockFocus];
470 return newImage;
471 }
472
473 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
474 {
475 wxAutoNSAutoreleasePool pool;
476 UnRef();
477
478 wxCHECK_MSG(image.IsOk(), false, wxT("invalid image"));
479 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
480
481 m_refData = new wxBitmapRefData();
482
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()
489 bitsPerSample: 8
490 samplesPerPixel: 3
491 hasAlpha: NO
492 isPlanar: NO
493 colorSpaceName: NSCalibratedRGBColorSpace
494 bytesPerRow: image.GetWidth()*3
495 bitsPerPixel: 0] autorelease];
496
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()));
508 return true;
509 }
510
511 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
512 {
513 if(!IsOk())
514 return NULL;
515
516 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
517 if(!bitmapRep)
518 return NULL;
519
520 if([bitmapRep bitsPerPixel]!=bpp)
521 {
522 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
523 return NULL;
524 }
525 data.m_width = [bitmapRep pixelsWide];
526 data.m_height = [bitmapRep pixelsHigh];
527 data.m_stride = [bitmapRep bytesPerRow];
528 return [bitmapRep bitmapData];
529
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
536 }
537
538 void wxBitmap::UngetRawData(wxPixelDataBase& data)
539 { // TODO
540 }
541
542 // ========================================================================
543 // wxMask
544 // ========================================================================
545
546 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
547
548 wxMask::wxMask()
549 {
550 m_cocoaNSBitmapImageRep = nil;
551 }
552
553 // Construct a mask from a bitmap and a colour indicating
554 // the transparent area
555 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
556 {
557 m_cocoaNSBitmapImageRep = nil;
558 Create(bitmap, colour);
559 }
560
561 // Construct a mask from a bitmap and a palette index indicating
562 // the transparent area
563 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
564 {
565 m_cocoaNSBitmapImageRep = nil;
566
567 Create(bitmap, paletteIndex);
568 }
569
570 // Construct a mask from a mono bitmap (copies the bitmap).
571 wxMask::wxMask(const wxBitmap& bitmap)
572 {
573 m_cocoaNSBitmapImageRep = nil;
574
575 Create(bitmap);
576 }
577
578 // Copy constructor
579 wxMask::wxMask(const wxMask& src)
580 : wxObject(src)
581 , m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
582 {
583 }
584
585 wxMask::~wxMask()
586 {
587 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
588 }
589
590 // Create a mask from a mono bitmap (copies the bitmap).
591 bool wxMask::Create(const wxBitmap& bitmap)
592 {
593 // TODO
594 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
595 return FALSE;
596 }
597
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)
601 {
602 // TODO
603 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
604 return FALSE;
605 }
606
607 template <typename PixelData>
608 static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
609 {
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)
619 {
620 typename PixelData::Iterator rowStart(p);
621 unsigned char *dstRow = dstData + y*dstRowLength;
622 for(int x=0; x<width_aligned; x+=8)
623 {
624 unsigned char *dstByte = dstRow + x/8;
625 *dstByte = 0;
626 // Take source RGB, compare it with the wxColour
627 for(int j=0; j<8; ++j, ++p)
628 {
629 *dstByte +=
630 ( p.Red()!=colour.Red()
631 || p.Green()!=colour.Green()
632 || p.Blue()!=colour.Blue()
633 ) << (7-j);
634 }
635 }
636 // Handle the remaining 0-7 pixels in the row
637 unsigned char *dstByte = dstRow + width_aligned/8;
638 if(nCols%8>0)
639 *dstByte = 0;
640 for(int j=0; j<(nCols%8); ++j, ++p)
641 {
642 *dstByte +=
643 ( p.Red()!=colour.Red()
644 || p.Green()!=colour.Green()
645 || p.Blue()!=colour.Blue()
646 ) << (7-j);
647 }
648 p = rowStart;
649 p.OffsetY(srcData,1);
650 }
651 return true;
652 }
653
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)
657 {
658 wxAutoNSAutoreleasePool pool;
659 if(!bitmap.IsOk())
660 return false;
661 int bmpWidth = bitmap.GetWidth();
662 int bmpHeight = bitmap.GetHeight();
663 int dstRowLength = (bmpWidth+7)/8;
664
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);
679
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"));
683
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)
690 {
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"));
694 }
695 // 32-bpp RGBx (x=throw away, no alpha)
696 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
697 {
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"));
702 }
703 // 32-bpp RGBA
704 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
705 {
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"));
709 }
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"));
717 }
718 else
719 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
720
721 // maskRep was autoreleased in case we had to exit quickly
722 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);
723 return true;
724 }