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