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