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