]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
Reflect changes in stc.cpp in stc.cpp.in from which it's generated.
[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
d135b1a5 7// Copyright: (c) 2003 David Elliott
526954c5 8// Licence: wxWindows licence
a24aff65
DE
9/////////////////////////////////////////////////////////////////////////////
10
449c5673 11#include "wx/wxprec.h"
0bca0373
WS
12
13#include "wx/bitmap.h"
14
449c5673
DE
15#ifndef WX_PRECOMP
16 #include "wx/log.h"
17 #include "wx/utils.h"
18 #include "wx/palette.h"
19 #include "wx/icon.h"
016b0643 20 #include "wx/colour.h"
155ecd4c 21 #include "wx/image.h"
449c5673 22#endif //WX_PRECOMP
0bca0373 23
d135b1a5 24#include "wx/xpmdecod.h"
b60c6e97 25#include "wx/rawbmp.h"
a24aff65 26
d135b1a5
DE
27#include "wx/cocoa/autorelease.h"
28#include "wx/cocoa/string.h"
6a5c31c2 29#include "wx/cocoa/ObjcRef.h"
d135b1a5
DE
30
31#import <AppKit/NSBitmapImageRep.h>
32#import <AppKit/NSGraphics.h>
9c54e4ae 33#import <AppKit/NSImage.h>
2a8db3cd 34#import <AppKit/NSColor.h>
d135b1a5 35
452418c4
PC
36IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
37
d135b1a5
DE
38// ========================================================================
39// wxBitmapRefData
40// ========================================================================
732d8c74 41
d135b1a5
DE
42class wxBitmapRefData: public wxGDIRefData
43{
44 friend class wxBitmap;
45public:
46 wxBitmapRefData();
47 wxBitmapRefData( const wxBitmapRefData& data );
48 virtual ~wxBitmapRefData();
49
8f884a0d
VZ
50 virtual bool IsOk() const { return m_ok; }
51
d135b1a5
DE
52protected:
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
a24aff65
DE
64wxBitmapRefData::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;
d135b1a5 72 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
73 m_bitmapMask = NULL;
74}
75
d135b1a5
DE
76wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
77{
6a5c31c2
DE
78 wxAutoNSAutoreleasePool pool;
79
d135b1a5
DE
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;
6a5c31c2 87 m_cocoaNSBitmapImageRep = wxGCSafeRetain([[data.m_cocoaNSBitmapImageRep copyWithZone:nil] autorelease]);
d135b1a5
DE
88 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
89}
90
a24aff65
DE
91wxBitmapRefData::~wxBitmapRefData()
92{
6a5c31c2 93 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
d135b1a5 94 m_cocoaNSBitmapImageRep = NULL;
a24aff65 95
d135b1a5 96 delete m_bitmapMask;
a24aff65
DE
97 m_bitmapMask = NULL;
98}
99
d135b1a5
DE
100// ========================================================================
101// wxBitmap
102// ========================================================================
732d8c74
FM
103
104#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
105
d135b1a5
DE
106IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
107
a24aff65
DE
108wxBitmap::wxBitmap()
109{
110 m_refData = NULL;
a24aff65
DE
111}
112
113wxBitmap::~wxBitmap()
114{
a24aff65
DE
115}
116
117wxBitmap::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 */
a24aff65
DE
127}
128
d381b7df
DE
129wxBitmap::wxBitmap(NSImage* cocoaNSImage)
130{
131 (void) Create(cocoaNSImage);
132}
133
134wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep)
135{
136 (void) Create(cocoaNSBitmapImageRep);
137}
138
452418c4 139wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
a24aff65
DE
140{
141 (void) Create(data, type, width, height, depth);
a24aff65
DE
142}
143
144wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
145{
146 LoadFile(filename, type);
a24aff65
DE
147}
148
8f884a0d 149wxGDIRefData *wxBitmap::CreateGDIRefData() const
a24aff65 150{
d135b1a5 151 return new wxBitmapRefData;
a24aff65
DE
152}
153
8f884a0d 154wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
a24aff65 155{
d135b1a5 156 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
157}
158
d135b1a5 159WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 160{
d135b1a5
DE
161 if(!M_BITMAPDATA)
162 return NULL;
163 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
164}
165
9c54e4ae
DE
166WX_NSImage wxBitmap::GetNSImage(bool useMask) const
167{
a1b806b9 168 if(!IsOk())
9c54e4ae
DE
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 {
9a6e13a6
DE
177 // Show before/after to prove that the bitmap itself is not changed
178 // even though we just composited onto the NSImage
a6d27d48 179 wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
9c54e4ae
DE
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];
9a6e13a6 186 [maskImage release];
a6d27d48 187 wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]);
9c54e4ae
DE
188 }
189 return nsimage;
190}
191
ab13160e
DE
192void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
193{
194 if(!M_BITMAPDATA)
195 return;
196 // NOTE: No checking is done to make sure width/height agree
6a5c31c2
DE
197 wxGCSafeRetain(bitmapImageRep);
198 wxGCSafeRelease(M_BITMAPDATA->m_cocoaNSBitmapImageRep);
ab13160e
DE
199 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
200}
201
a24aff65
DE
202void wxBitmap::SetWidth(int w)
203{
204 if (!M_BITMAPDATA)
205 m_refData = new wxBitmapRefData;
206
207 M_BITMAPDATA->m_width = w;
208}
209
210void wxBitmap::SetHeight(int h)
211{
212 if (!M_BITMAPDATA)
213 m_refData = new wxBitmapRefData;
214
215 M_BITMAPDATA->m_height = h;
216}
217
218void wxBitmap::SetDepth(int d)
219{
220 if (!M_BITMAPDATA)
221 m_refData = new wxBitmapRefData;
222
223 M_BITMAPDATA->m_depth = d;
224}
225
226void wxBitmap::SetQuality(int q)
227{
228 if (!M_BITMAPDATA)
229 m_refData = new wxBitmapRefData;
230
231 M_BITMAPDATA->m_quality = q;
232}
233
234void wxBitmap::SetOk(bool isOk)
235{
236 if (!M_BITMAPDATA)
237 m_refData = new wxBitmapRefData;
238
239 M_BITMAPDATA->m_ok = isOk;
240}
241
242void wxBitmap::SetPalette(const wxPalette& palette)
243{
244 if (!M_BITMAPDATA)
245 m_refData = new wxBitmapRefData;
246
247 M_BITMAPDATA->m_bitmapPalette = palette ;
248}
249
250void wxBitmap::SetMask(wxMask *mask)
251{
252 if (!M_BITMAPDATA)
253 m_refData = new wxBitmapRefData;
254
255 M_BITMAPDATA->m_bitmapMask = mask ;
256}
257
d135b1a5 258wxPalette* wxBitmap::GetPalette() const
a24aff65 259{
d135b1a5
DE
260 if(!m_refData)
261 return NULL;
262 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
263}
264
d135b1a5 265wxMask* wxBitmap::GetMask() const
a24aff65 266{
d135b1a5
DE
267 if(!m_refData)
268 return NULL;
269 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
270}
271
d135b1a5 272int wxBitmap::GetDepth() const
a24aff65 273{
d135b1a5
DE
274 if(!m_refData)
275 return 0;
276 return M_BITMAPDATA->m_depth;
a24aff65
DE
277}
278
d135b1a5 279int wxBitmap::GetWidth() const
a24aff65 280{
d135b1a5
DE
281 if(!m_refData)
282 return 0;
283 return M_BITMAPDATA->m_width;
a24aff65
DE
284}
285
d135b1a5 286int wxBitmap::GetHeight() const
a24aff65 287{
d135b1a5
DE
288 if(!m_refData)
289 return 0;
290 return M_BITMAPDATA->m_height;
a24aff65
DE
291}
292
d135b1a5 293bool wxBitmap::Create(int w, int h, int d)
a24aff65 294{
6a5c31c2
DE
295 wxAutoNSAutoreleasePool pool;
296
d135b1a5
DE
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 */
6a5c31c2 306 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain([[[NSBitmapImageRep alloc]
ab13160e
DE
307 initWithBitmapDataPlanes: NULL
308 pixelsWide: w
309 pixelsHigh: h
310 bitsPerSample: 8
311 samplesPerPixel: 3
312 hasAlpha: NO
313 isPlanar: NO
314 colorSpaceName: NSCalibratedRGBColorSpace
efe85495
DE
315 bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS
316 // 10.4 will add padding bytes when 0 is used here
6a5c31c2 317 bitsPerPixel: 0] autorelease]);
ab13160e 318
48580976 319 wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
ab13160e
DE
320 M_BITMAPDATA->m_ok = true;
321 M_BITMAPDATA->m_numColors = 0;
322 M_BITMAPDATA->m_quality = 0;
323 M_BITMAPDATA->m_bitmapMask = NULL;
d135b1a5
DE
324
325 return M_BITMAPDATA->m_ok;
a24aff65
DE
326}
327
d135b1a5 328bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 329{
d135b1a5
DE
330 wxAutoNSAutoreleasePool pool;
331 UnRef();
a24aff65 332
d135b1a5 333 m_refData = new wxBitmapRefData;
a24aff65 334
d135b1a5
DE
335 NSBitmapImageRep *imageRep = [NSBitmapImageRep
336 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 337
d135b1a5
DE
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;
6a5c31c2 346 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
d135b1a5
DE
347 M_BITMAPDATA->m_bitmapMask = NULL;
348 return true;
349 }
350 wxImage image;
351 if(!image.LoadFile(filename,type))
352 return false;
a1b806b9 353 if(!image.IsOk())
d135b1a5
DE
354 return false;
355 *this = wxBitmap(image);
356 return true;
a24aff65
DE
357}
358
d381b7df
DE
359bool wxBitmap::Create(NSImage* cocoaNSImage)
360{
361 wxAutoNSAutoreleasePool pool;
362 NSBitmapImageRep *bitmapImageRep = [NSBitmapImageRep imageRepWithData:[cocoaNSImage TIFFRepresentation]];
363 return Create(bitmapImageRep);
364}
365
366bool 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;
6a5c31c2 378 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(imageRep);
d381b7df
DE
379 M_BITMAPDATA->m_bitmapMask = NULL;
380 return true;
381 }
382 else
383 return false;
384}
385
452418c4 386bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
a24aff65 387{
d135b1a5
DE
388 UnRef();
389
390 m_refData = new wxBitmapRefData;
391
392 return false;
a24aff65
DE
393}
394
d135b1a5 395bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 396{
d135b1a5 397 return false;
a24aff65
DE
398}
399
68a8c89b 400bool wxBitmap::CopyFromIcon(const wxIcon& icon)
a24aff65 401{
99635c99
DE
402 // Pool here due to lack of one during wx init phase
403 wxAutoNSAutoreleasePool pool;
404
68a8c89b
DE
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];
6a5c31c2 411 NSBitmapImageRep *newBitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease];
68a8c89b
DE
412 [icon.GetNSImage() unlockFocus];
413 if(!newBitmapRep)
414 return false;
415 m_refData = new wxBitmapRefData;
6a5c31c2 416 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(newBitmapRep);
68a8c89b
DE
417 M_BITMAPDATA->m_width = [newBitmapRep pixelsWide];
418 M_BITMAPDATA->m_height = [newBitmapRep pixelsHigh];
b4918747 419 M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel];
68a8c89b
DE
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;
a24aff65
DE
425}
426
4706bee7 427wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
a24aff65 428{
4706bee7 429 wxAutoNSAutoreleasePool pool;
a1b806b9 430 if(!IsOk())
4706bee7
DE
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();
5c33522f
VZ
438 imageRect.size.width *= static_cast<CGFloat>(rect.width) / GetWidth();
439 imageRect.size.height *= static_cast<CGFloat>(rect.height) / GetHeight();
4706bee7
DE
440
441 NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
442 [nsimage unlockFocus];
443
444 wxBitmap newBitmap(newBitmapRep);
445
446 return (newBitmap);
d135b1a5 447}
a24aff65 448
d135b1a5 449wxImage wxBitmap::ConvertToImage() const
a24aff65 450{
2a8db3cd 451 wxAutoNSAutoreleasePool pool;
a1b806b9 452 if(!IsOk())
f8c10ed8 453 return /*wxImage(5,5)*/wxNullImage;
2a8db3cd
DE
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;
a24aff65
DE
471}
472
d135b1a5 473bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 474{
6a5c31c2 475 wxAutoNSAutoreleasePool pool;
d135b1a5
DE
476 UnRef();
477
a1b806b9 478 wxCHECK_MSG(image.IsOk(), false, wxT("invalid image"));
d135b1a5
DE
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();
6a5c31c2 485 NSBitmapImageRep *bitmapImage = [[[NSBitmapImageRep alloc]
d135b1a5
DE
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
efe85495 494 bytesPerRow: image.GetWidth()*3
6a5c31c2 495 bitsPerPixel: 0] autorelease];
d135b1a5 496
efe85495
DE
497 // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow]
498 // so that the rows are aligned suitably for altivec by the OS (Tiger)
d135b1a5
DE
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;
6a5c31c2 506 M_BITMAPDATA->m_cocoaNSBitmapImageRep = wxGCSafeRetain(bitmapImage);
016b0643 507 M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue()));
d135b1a5 508 return true;
a24aff65
DE
509}
510
b60c6e97
DE
511void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
512{
a1b806b9 513 if(!IsOk())
b60c6e97
DE
514 return NULL;
515
516 NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep;
517 if(!bitmapRep)
518 return NULL;
519
520 if([bitmapRep bitsPerPixel]!=bpp)
521 {
9a83f860 522 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
b60c6e97
DE
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
538void wxBitmap::UngetRawData(wxPixelDataBase& data)
539{ // TODO
540}
541
d135b1a5
DE
542// ========================================================================
543// wxMask
544// ========================================================================
545
546IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
547
548wxMask::wxMask()
a24aff65 549{
016b0643 550 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
551}
552
d135b1a5
DE
553// Construct a mask from a bitmap and a colour indicating
554// the transparent area
555wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 556{
016b0643 557 m_cocoaNSBitmapImageRep = nil;
d135b1a5 558 Create(bitmap, colour);
a24aff65
DE
559}
560
d135b1a5
DE
561// Construct a mask from a bitmap and a palette index indicating
562// the transparent area
563wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 564{
016b0643 565 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
566
567 Create(bitmap, paletteIndex);
a24aff65
DE
568}
569
d135b1a5
DE
570// Construct a mask from a mono bitmap (copies the bitmap).
571wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 572{
016b0643 573 m_cocoaNSBitmapImageRep = nil;
d135b1a5
DE
574
575 Create(bitmap);
a24aff65
DE
576}
577
319fe103
DE
578// Copy constructor
579wxMask::wxMask(const wxMask& src)
580: wxObject(src)
6a5c31c2 581, m_cocoaNSBitmapImageRep(wxGCSafeRetain(src.m_cocoaNSBitmapImageRep))
319fe103
DE
582{
583}
584
d135b1a5 585wxMask::~wxMask()
a24aff65 586{
6a5c31c2 587 wxGCSafeRelease(m_cocoaNSBitmapImageRep);
a24aff65
DE
588}
589
d135b1a5
DE
590// Create a mask from a mono bitmap (copies the bitmap).
591bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 592{
d135b1a5 593// TODO
148af7c5 594 wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap"));
d135b1a5 595 return FALSE;
a24aff65
DE
596}
597
d135b1a5
DE
598// Create a mask from a bitmap and a palette index indicating
599// the transparent area
600bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 601{
d135b1a5 602// TODO
148af7c5 603 wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap"));
d135b1a5 604 return FALSE;
a24aff65
DE
605}
606
360f4262 607template <typename PixelData>
016b0643
DE
608static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData)
609{
2b030203 610 wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data"));
360f4262 611 typename PixelData::Iterator p(srcData);
016b0643
DE
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 {
360f4262 620 typename PixelData::Iterator rowStart(p);
016b0643
DE
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;
9a6e13a6
DE
638 if(nCols%8>0)
639 *dstByte = 0;
016b0643
DE
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
d135b1a5
DE
654// Create a mask from a bitmap and a colour indicating
655// the transparent area
656bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 657{
016b0643 658 wxAutoNSAutoreleasePool pool;
a1b806b9 659 if(!bitmap.IsOk())
016b0643
DE
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
ce20d822 681 NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep();
2b030203 682 wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap"));
016b0643
DE
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];
ea3d4caf 688 // The wxImage format (which we use whenever we imported from wxImage)
016b0643
DE
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),
2b030203 693 false, wxT("Unable to access raw data"));
016b0643 694 }
ea3d4caf
DE
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),
2b030203 701 false, wxT("Unable to access raw data"));
ea3d4caf
DE
702 }
703 // 32-bpp RGBA
016b0643
DE
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),
2b030203 708 false, wxT("Unable to access raw data"));
016b0643 709 }
319fe103
DE
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 }
016b0643 718 else
2b030203 719 { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); }
016b0643
DE
720
721 // maskRep was autoreleased in case we had to exit quickly
6a5c31c2 722 m_cocoaNSBitmapImageRep = wxGCSafeRetain(maskRep);
016b0643 723 return true;
a24aff65 724}