]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
fixed variable shadowing icc warnings
[wxWidgets.git] / src / cocoa / bitmap.mm
CommitLineData
a24aff65 1/////////////////////////////////////////////////////////////////////////////
d135b1a5 2// Name: src/cocoa/bitmap.cpp
a24aff65 3// Purpose: wxBitmap
d135b1a5 4// Author: David Elliott
a24aff65 5// Modified by:
d135b1a5 6// Created: 2003/07/19
a24aff65 7// RCS-ID: $Id$
d135b1a5 8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
a24aff65
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/utils.h"
16 #include "wx/palette.h"
17 #include "wx/icon.h"
016b0643 18 #include "wx/colour.h"
449c5673 19#endif //WX_PRECOMP
a24aff65 20#include "wx/bitmap.h"
a24aff65 21#include "wx/image.h"
d135b1a5 22#include "wx/xpmdecod.h"
b60c6e97 23#include "wx/rawbmp.h"
a24aff65 24
d135b1a5
DE
25#include "wx/cocoa/autorelease.h"
26#include "wx/cocoa/string.h"
27
28#import <AppKit/NSBitmapImageRep.h>
29#import <AppKit/NSGraphics.h>
9c54e4ae 30#import <AppKit/NSImage.h>
2a8db3cd 31#import <AppKit/NSColor.h>
d135b1a5
DE
32
33// ========================================================================
34// wxBitmapRefData
35// ========================================================================
36class wxBitmapRefData: public wxGDIRefData
37{
38 friend class wxBitmap;
39public:
40 wxBitmapRefData();
41 wxBitmapRefData( const wxBitmapRefData& data );
42 virtual ~wxBitmapRefData();
43
44protected:
45 int m_width;
46 int m_height;
47 int m_depth;
48 bool m_ok;
49 int m_numColors;
50 wxPalette m_bitmapPalette;
51 int m_quality;
52 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
53 wxMask *m_bitmapMask; // Optional mask
54};
55
56#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
a24aff65
DE
57
58wxBitmapRefData::wxBitmapRefData()
59{
60 m_ok = FALSE;
61 m_width = 0;
62 m_height = 0;
63 m_depth = 0;
64 m_quality = 0;
65 m_numColors = 0;
d135b1a5 66 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
67 m_bitmapMask = NULL;
68}
69
d135b1a5
DE
70wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
71{
72 m_width = data.m_width;
73 m_height = data.m_height;
74 m_depth = data.m_depth;
75 m_ok = data.m_ok;
76 m_numColors = data.m_numColors;
77 m_bitmapPalette = data.m_bitmapPalette;
78 m_quality = data.m_quality;
ab13160e 79 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
d135b1a5
DE
80 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
81}
82
a24aff65
DE
83wxBitmapRefData::~wxBitmapRefData()
84{
d135b1a5
DE
85 [m_cocoaNSBitmapImageRep release];
86 m_cocoaNSBitmapImageRep = NULL;
a24aff65 87
d135b1a5 88 delete m_bitmapMask;
a24aff65
DE
89 m_bitmapMask = NULL;
90}
91
d135b1a5
DE
92// ========================================================================
93// wxBitmap
94// ========================================================================
95IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
96
a24aff65
DE
97wxBitmap::wxBitmap()
98{
99 m_refData = NULL;
a24aff65
DE
100}
101
102wxBitmap::~wxBitmap()
103{
a24aff65
DE
104}
105
106wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
107{
108 m_refData = new wxBitmapRefData;
109
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;
114
115 /* TODO: create the bitmap from data */
a24aff65
DE
116}
117
118wxBitmap::wxBitmap(int w, int h, int d)
119{
120 (void)Create(w, h, d);
a24aff65
DE
121}
122
123wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
124{
125 (void) Create(data, type, width, height, depth);
a24aff65
DE
126}
127
128wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
129{
130 LoadFile(filename, type);
a24aff65
DE
131}
132
d135b1a5 133wxObjectRefData *wxBitmap::CreateRefData() const
a24aff65 134{
d135b1a5 135 return new wxBitmapRefData;
a24aff65
DE
136}
137
d135b1a5 138wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
a24aff65 139{
d135b1a5 140 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
141}
142
d135b1a5 143WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 144{
d135b1a5
DE
145 if(!M_BITMAPDATA)
146 return NULL;
147 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
148}
149
9c54e4ae
DE
150WX_NSImage wxBitmap::GetNSImage(bool useMask) const
151{
152 if(!Ok())
153 return nil;
154 NSImage *nsimage = [[[NSImage alloc]
155 initWithSize:NSMakeSize(GetWidth(), GetHeight())] autorelease];
156 if(!nsimage)
157 return nil;
158 [nsimage addRepresentation: M_BITMAPDATA->m_cocoaNSBitmapImageRep];
159 if(useMask && GetMask())
160 {
9a6e13a6
DE
161 // Show before/after to prove that the bitmap itself is not changed
162 // even though we just composited onto the NSImage
a6d27d48 163 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
9c54e4ae
DE
164 NSImage *maskImage = [[NSImage alloc]
165 initWithSize:NSMakeSize(GetWidth(), GetHeight())];
166 [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()];
167 [nsimage lockFocus];
168 [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn];
169 [nsimage unlockFocus];
9a6e13a6 170 [maskImage release];
a6d27d48 171 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
9c54e4ae
DE
172 }
173 return nsimage;
174}
175
ab13160e
DE
176void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
177{
178 if(!M_BITMAPDATA)
179 return;
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;
184}
185
a24aff65
DE
186void wxBitmap::SetWidth(int w)
187{
188 if (!M_BITMAPDATA)
189 m_refData = new wxBitmapRefData;
190
191 M_BITMAPDATA->m_width = w;
192}
193
194void wxBitmap::SetHeight(int h)
195{
196 if (!M_BITMAPDATA)
197 m_refData = new wxBitmapRefData;
198
199 M_BITMAPDATA->m_height = h;
200}
201
202void wxBitmap::SetDepth(int d)
203{
204 if (!M_BITMAPDATA)
205 m_refData = new wxBitmapRefData;
206
207 M_BITMAPDATA->m_depth = d;
208}
209
210void wxBitmap::SetQuality(int q)
211{
212 if (!M_BITMAPDATA)
213 m_refData = new wxBitmapRefData;
214
215 M_BITMAPDATA->m_quality = q;
216}
217
218void wxBitmap::SetOk(bool isOk)
219{
220 if (!M_BITMAPDATA)
221 m_refData = new wxBitmapRefData;
222
223 M_BITMAPDATA->m_ok = isOk;
224}
225
226void wxBitmap::SetPalette(const wxPalette& palette)
227{
228 if (!M_BITMAPDATA)
229 m_refData = new wxBitmapRefData;
230
231 M_BITMAPDATA->m_bitmapPalette = palette ;
232}
233
234void wxBitmap::SetMask(wxMask *mask)
235{
236 if (!M_BITMAPDATA)
237 m_refData = new wxBitmapRefData;
238
239 M_BITMAPDATA->m_bitmapMask = mask ;
240}
241
d135b1a5 242bool wxBitmap::Ok() const
a24aff65 243{
d135b1a5 244 return m_refData && M_BITMAPDATA->m_ok;
a24aff65
DE
245}
246
d135b1a5 247wxPalette* wxBitmap::GetPalette() const
a24aff65 248{
d135b1a5
DE
249 if(!m_refData)
250 return NULL;
251 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
252}
253
d135b1a5 254wxMask* wxBitmap::GetMask() const
a24aff65 255{
d135b1a5
DE
256 if(!m_refData)
257 return NULL;
258 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
259}
260
d135b1a5 261int wxBitmap::GetDepth() const
a24aff65 262{
d135b1a5
DE
263 if(!m_refData)
264 return 0;
265 return M_BITMAPDATA->m_depth;
a24aff65
DE
266}
267
d135b1a5 268int wxBitmap::GetWidth() const
a24aff65 269{
d135b1a5
DE
270 if(!m_refData)
271 return 0;
272 return M_BITMAPDATA->m_width;
a24aff65
DE
273}
274
d135b1a5 275int wxBitmap::GetHeight() const
a24aff65 276{
d135b1a5
DE
277 if(!m_refData)
278 return 0;
279 return M_BITMAPDATA->m_height;
a24aff65
DE
280}
281
d135b1a5 282bool wxBitmap::Create(int w, int h, int d)
a24aff65 283{
d135b1a5
DE
284 UnRef();
285
286 m_refData = new wxBitmapRefData;
287
288 M_BITMAPDATA->m_width = w;
289 M_BITMAPDATA->m_height = h;
290 M_BITMAPDATA->m_depth = d;
291
292 /* TODO: create new bitmap */
ab13160e
DE
293 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
294 initWithBitmapDataPlanes: NULL
295 pixelsWide: w
296 pixelsHigh: h
297 bitsPerSample: 8
298 samplesPerPixel: 3
299 hasAlpha: NO
300 isPlanar: NO
301 colorSpaceName: NSCalibratedRGBColorSpace
efe85495
DE
302 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
303 // 10.4 will add padding bytes when 0 is used here
ab13160e
DE
304 bitsPerPixel: 0];
305
48580976 306 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
ab13160e
DE
307 M_BITMAPDATA->m_ok = true;
308 M_BITMAPDATA->m_numColors = 0;
309 M_BITMAPDATA->m_quality = 0;
310 M_BITMAPDATA->m_bitmapMask = NULL;
d135b1a5
DE
311
312 return M_BITMAPDATA->m_ok;
a24aff65
DE
313}
314
d135b1a5 315bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 316{
d135b1a5
DE
317 wxAutoNSAutoreleasePool pool;
318 UnRef();
a24aff65 319
d135b1a5 320 m_refData = new wxBitmapRefData;
a24aff65 321
d135b1a5
DE
322 NSBitmapImageRep *imageRep = [NSBitmapImageRep
323 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 324
d135b1a5
DE
325 if(imageRep)
326 {
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;
335 return true;
336 }
337 wxImage image;
338 if(!image.LoadFile(filename,type))
339 return false;
340 if(!image.Ok())
341 return false;
342 *this = wxBitmap(image);
343 return true;
a24aff65
DE
344}
345
d135b1a5 346bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
a24aff65 347{
d135b1a5
DE
348 UnRef();
349
350 m_refData = new wxBitmapRefData;
351
352 return false;
a24aff65
DE
353}
354
d135b1a5 355bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 356{
d135b1a5 357 return false;
a24aff65
DE
358}
359
68a8c89b 360bool wxBitmap::CopyFromIcon(const wxIcon& icon)
a24aff65 361{
68a8c89b
DE
362 UnRef();
363 if(!icon.GetNSImage());
364 [icon.GetNSImage() lockFocus];
365 NSRect imageRect;
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];
370 if(!newBitmapRep)
371 return false;
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];
b4918747 376 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
68a8c89b
DE
377 M_BITMAPDATA->m_ok = true;
378 M_BITMAPDATA->m_numColors = 0;
379 M_BITMAPDATA->m_quality = 0;
380 M_BITMAPDATA->m_bitmapMask = NULL;
381 return true;
a24aff65
DE
382}
383
d135b1a5 384wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
a24aff65 385{
d135b1a5
DE
386 return wxNullBitmap;
387}
a24aff65 388
d135b1a5 389wxImage wxBitmap::ConvertToImage() const
a24aff65 390{
2a8db3cd 391 wxAutoNSAutoreleasePool pool;
f8c10ed8
DE
392 if(!Ok())
393 return /*wxImage(5,5)*/wxNullImage;
2a8db3cd
DE
394 NSImage *nsimage = GetNSImage(false /* don't use mask */);
395 wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
396 [nsimage lockFocus];
397 for(int i=0; i < M_BITMAPDATA->m_width; i++)
398 {
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++)
403 {
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));
407 }
408 }
409 [nsimage unlockFocus];
410 return newImage;
a24aff65
DE
411}
412
d135b1a5 413bool wxBitmap::CreateFromXpm(const char **xpm)
a24aff65 414{
d135b1a5
DE
415#if wxUSE_IMAGE && wxUSE_XPM
416 UnRef();
417
418 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
419
420 wxXPMDecoder decoder;
421 wxImage img = decoder.ReadData(xpm);
422 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
423
424 *this = wxBitmap(img);
425 return true;
426#else
a24aff65 427 return false;
d135b1a5 428#endif
a24aff65
DE
429}
430
d135b1a5 431bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 432{
d135b1a5
DE
433 UnRef();
434
435 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
436 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
437
438 m_refData = new wxBitmapRefData();
439
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()
446 bitsPerSample: 8
447 samplesPerPixel: 3
448 hasAlpha: NO
449 isPlanar: NO
450 colorSpaceName: NSCalibratedRGBColorSpace
efe85495 451 bytesPerRow: image.GetWidth()*3
d135b1a5
DE
452 bitsPerPixel: 0];
453
efe85495
DE
454 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
455 // so that the rows are aligned suitably for altivec by the OS (Tiger)
d135b1a5
DE
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;
016b0643 464 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
d135b1a5 465 return true;
a24aff65
DE
466}
467
b60c6e97
DE
468void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
469{
470 if(!Ok())
471 return NULL;
472
473 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
474 if(!bitmapRep)
475 return NULL;
476
477 if([bitmapRep bitsPerPixel]!=bpp)
478 {
479 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
480 return NULL;
481 }
482 data.m_width = [bitmapRep pixelsWide];
483 data.m_height = [bitmapRep pixelsHigh];
484 data.m_stride = [bitmapRep bytesPerRow];
485 return [bitmapRep bitmapData];
486
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
493}
494
495void wxBitmap::UngetRawData(wxPixelDataBase& data)
496{ // TODO
497}
498
4170c465
DE
499void wxBitmap::UseAlpha()
500{ // TODO
501}
502
d135b1a5
DE
503// ========================================================================
504// wxMask
505// ========================================================================
506
507IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
508
509wxMask::wxMask()
a24aff65 510{
016b0643 511 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
512}
513
d135b1a5
DE
514// Construct a mask from a bitmap and a colour indicating
515// the transparent area
516wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 517{
016b0643 518 m_cocoaNSBitmapImageRep = nil;
d135b1a5 519 Create(bitmap, colour);
a24aff65
DE
520}
521
d135b1a5
DE
522// Construct a mask from a bitmap and a palette index indicating
523// the transparent area
524wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 525{
016b0643 526 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
527
528 Create(bitmap, paletteIndex);
a24aff65
DE
529}
530
d135b1a5
DE
531// Construct a mask from a mono bitmap (copies the bitmap).
532wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 533{
016b0643 534 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
535
536 Create(bitmap);
a24aff65
DE
537}
538
d135b1a5 539wxMask::~wxMask()
a24aff65 540{
016b0643 541 [m_cocoaNSBitmapImageRep release];
a24aff65
DE
542}
543
d135b1a5
DE
544// Create a mask from a mono bitmap (copies the bitmap).
545bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 546{
d135b1a5
DE
547// TODO
548 return FALSE;
a24aff65
DE
549}
550
d135b1a5
DE
551// Create a mask from a bitmap and a palette index indicating
552// the transparent area
553bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 554{
d135b1a5
DE
555// TODO
556 return FALSE;
a24aff65
DE
557}
558
360f4262 559template <typename PixelData>
016b0643
DE
560static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
561{
2b030203 562 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
360f4262 563 typename PixelData::Iterator p(srcData);
016b0643
DE
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)
571 {
360f4262 572 typename PixelData::Iterator rowStart(p);
016b0643
DE
573 unsigned char *dstRow = dstData + y*dstRowLength;
574 for(int x=0; x<width_aligned; x+=8)
575 {
576 unsigned char *dstByte = dstRow + x/8;
577 *dstByte = 0;
578 // Take source RGB, compare it with the wxColour
579 for(int j=0; j<8; ++j, ++p)
580 {
581 *dstByte +=
582 ( p.Red()!=colour.Red()
583 || p.Green()!=colour.Green()
584 || p.Blue()!=colour.Blue()
585 ) << (7-j);
586 }
587 }
588 // Handle the remaining 0-7 pixels in the row
589 unsigned char *dstByte = dstRow + width_aligned/8;
9a6e13a6
DE
590 if(nCols%8>0)
591 *dstByte = 0;
016b0643
DE
592 for(int j=0; j<(nCols%8); ++j, ++p)
593 {
594 *dstByte +=
595 ( p.Red()!=colour.Red()
596 || p.Green()!=colour.Green()
597 || p.Blue()!=colour.Blue()
598 ) << (7-j);
599 }
600 p = rowStart;
601 p.OffsetY(srcData,1);
602 }
603 return true;
604}
605
d135b1a5
DE
606// Create a mask from a bitmap and a colour indicating
607// the transparent area
608bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 609{
016b0643
DE
610 wxAutoNSAutoreleasePool pool;
611 if(!bitmap.Ok())
612 return false;
613 int bmpWidth = bitmap.GetWidth();
614 int bmpHeight = bitmap.GetHeight();
615 int dstRowLength = (bmpWidth+7)/8;
616
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);
631
632 // We need the source NSBitmapImageRep to detemine its pixel format
ce20d822 633 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
2b030203 634 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
016b0643
DE
635
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];
ea3d4caf 640 // The wxImage format (which we use whenever we imported from wxImage)
016b0643
DE
641 if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
642 {
643 wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
644 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
2b030203 645 false, wxT("Unable to access raw data"));
016b0643 646 }
ea3d4caf
DE
647 // 32-bpp RGBx (x=throw away, no alpha)
648 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO)
649 {
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),
2b030203 653 false, wxT("Unable to access raw data"));
ea3d4caf
DE
654 }
655 // 32-bpp RGBA
016b0643
DE
656 else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES)
657 {
658 wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap));
659 wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData),
2b030203 660 false, wxT("Unable to access raw data"));
016b0643
DE
661 }
662 else
2b030203 663 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
016b0643
DE
664
665 // maskRep was autoreleased in case we had to exit quickly
666 m_cocoaNSBitmapImageRep = [maskRep retain];
667 return true;
a24aff65
DE
668}
669