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