]>
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 |
a24aff65 DE |
9 | // Licence: wxWindows licence |
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 | { | |
160 | NSImage *maskImage = [[NSImage alloc] | |
161 | initWithSize:NSMakeSize(GetWidth(), GetHeight())]; | |
162 | [maskImage addRepresentation: GetMask()->GetNSBitmapImageRep()]; | |
163 | [nsimage lockFocus]; | |
164 | [maskImage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationIn]; | |
165 | [nsimage unlockFocus]; | |
166 | } | |
167 | return nsimage; | |
168 | } | |
169 | ||
ab13160e DE |
170 | void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep) |
171 | { | |
172 | if(!M_BITMAPDATA) | |
173 | return; | |
174 | // NOTE: No checking is done to make sure width/height agree | |
175 | [bitmapImageRep retain]; | |
176 | [M_BITMAPDATA->m_cocoaNSBitmapImageRep release]; | |
177 | M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep; | |
178 | } | |
179 | ||
a24aff65 DE |
180 | void wxBitmap::SetWidth(int w) |
181 | { | |
182 | if (!M_BITMAPDATA) | |
183 | m_refData = new wxBitmapRefData; | |
184 | ||
185 | M_BITMAPDATA->m_width = w; | |
186 | } | |
187 | ||
188 | void wxBitmap::SetHeight(int h) | |
189 | { | |
190 | if (!M_BITMAPDATA) | |
191 | m_refData = new wxBitmapRefData; | |
192 | ||
193 | M_BITMAPDATA->m_height = h; | |
194 | } | |
195 | ||
196 | void wxBitmap::SetDepth(int d) | |
197 | { | |
198 | if (!M_BITMAPDATA) | |
199 | m_refData = new wxBitmapRefData; | |
200 | ||
201 | M_BITMAPDATA->m_depth = d; | |
202 | } | |
203 | ||
204 | void wxBitmap::SetQuality(int q) | |
205 | { | |
206 | if (!M_BITMAPDATA) | |
207 | m_refData = new wxBitmapRefData; | |
208 | ||
209 | M_BITMAPDATA->m_quality = q; | |
210 | } | |
211 | ||
212 | void wxBitmap::SetOk(bool isOk) | |
213 | { | |
214 | if (!M_BITMAPDATA) | |
215 | m_refData = new wxBitmapRefData; | |
216 | ||
217 | M_BITMAPDATA->m_ok = isOk; | |
218 | } | |
219 | ||
220 | void wxBitmap::SetPalette(const wxPalette& palette) | |
221 | { | |
222 | if (!M_BITMAPDATA) | |
223 | m_refData = new wxBitmapRefData; | |
224 | ||
225 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
226 | } | |
227 | ||
228 | void wxBitmap::SetMask(wxMask *mask) | |
229 | { | |
230 | if (!M_BITMAPDATA) | |
231 | m_refData = new wxBitmapRefData; | |
232 | ||
233 | M_BITMAPDATA->m_bitmapMask = mask ; | |
234 | } | |
235 | ||
d135b1a5 | 236 | bool wxBitmap::Ok() const |
a24aff65 | 237 | { |
d135b1a5 | 238 | return m_refData && M_BITMAPDATA->m_ok; |
a24aff65 DE |
239 | } |
240 | ||
d135b1a5 | 241 | wxPalette* wxBitmap::GetPalette() const |
a24aff65 | 242 | { |
d135b1a5 DE |
243 | if(!m_refData) |
244 | return NULL; | |
245 | return &M_BITMAPDATA->m_bitmapPalette; | |
a24aff65 DE |
246 | } |
247 | ||
d135b1a5 | 248 | wxMask* wxBitmap::GetMask() const |
a24aff65 | 249 | { |
d135b1a5 DE |
250 | if(!m_refData) |
251 | return NULL; | |
252 | return M_BITMAPDATA->m_bitmapMask; | |
a24aff65 DE |
253 | } |
254 | ||
d135b1a5 | 255 | int wxBitmap::GetDepth() const |
a24aff65 | 256 | { |
d135b1a5 DE |
257 | if(!m_refData) |
258 | return 0; | |
259 | return M_BITMAPDATA->m_depth; | |
a24aff65 DE |
260 | } |
261 | ||
d135b1a5 | 262 | int wxBitmap::GetWidth() const |
a24aff65 | 263 | { |
d135b1a5 DE |
264 | if(!m_refData) |
265 | return 0; | |
266 | return M_BITMAPDATA->m_width; | |
a24aff65 DE |
267 | } |
268 | ||
d135b1a5 | 269 | int wxBitmap::GetHeight() const |
a24aff65 | 270 | { |
d135b1a5 DE |
271 | if(!m_refData) |
272 | return 0; | |
273 | return M_BITMAPDATA->m_height; | |
a24aff65 DE |
274 | } |
275 | ||
d135b1a5 | 276 | bool wxBitmap::Create(int w, int h, int d) |
a24aff65 | 277 | { |
d135b1a5 DE |
278 | UnRef(); |
279 | ||
280 | m_refData = new wxBitmapRefData; | |
281 | ||
282 | M_BITMAPDATA->m_width = w; | |
283 | M_BITMAPDATA->m_height = h; | |
284 | M_BITMAPDATA->m_depth = d; | |
285 | ||
286 | /* TODO: create new bitmap */ | |
ab13160e DE |
287 | M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc] |
288 | initWithBitmapDataPlanes: NULL | |
289 | pixelsWide: w | |
290 | pixelsHigh: h | |
291 | bitsPerSample: 8 | |
292 | samplesPerPixel: 3 | |
293 | hasAlpha: NO | |
294 | isPlanar: NO | |
295 | colorSpaceName: NSCalibratedRGBColorSpace | |
296 | bytesPerRow: 0 | |
297 | bitsPerPixel: 0]; | |
298 | ||
2b030203 | 299 | wxLogDebug(wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]); |
ab13160e DE |
300 | M_BITMAPDATA->m_ok = true; |
301 | M_BITMAPDATA->m_numColors = 0; | |
302 | M_BITMAPDATA->m_quality = 0; | |
303 | M_BITMAPDATA->m_bitmapMask = NULL; | |
d135b1a5 DE |
304 | |
305 | return M_BITMAPDATA->m_ok; | |
a24aff65 DE |
306 | } |
307 | ||
d135b1a5 | 308 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
a24aff65 | 309 | { |
d135b1a5 DE |
310 | wxAutoNSAutoreleasePool pool; |
311 | UnRef(); | |
a24aff65 | 312 | |
d135b1a5 | 313 | m_refData = new wxBitmapRefData; |
a24aff65 | 314 | |
d135b1a5 DE |
315 | NSBitmapImageRep *imageRep = [NSBitmapImageRep |
316 | imageRepWithContentsOfFile:wxNSStringWithWxString(filename)]; | |
a24aff65 | 317 | |
d135b1a5 DE |
318 | if(imageRep) |
319 | { | |
320 | M_BITMAPDATA->m_width = [imageRep pixelsWide]; | |
321 | M_BITMAPDATA->m_height = [imageRep pixelsHigh]; | |
322 | M_BITMAPDATA->m_depth = 24; // FIXME | |
323 | M_BITMAPDATA->m_ok = true; | |
324 | M_BITMAPDATA->m_numColors = 0; | |
325 | M_BITMAPDATA->m_quality = 0; | |
326 | M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain]; | |
327 | M_BITMAPDATA->m_bitmapMask = NULL; | |
328 | return true; | |
329 | } | |
330 | wxImage image; | |
331 | if(!image.LoadFile(filename,type)) | |
332 | return false; | |
333 | if(!image.Ok()) | |
334 | return false; | |
335 | *this = wxBitmap(image); | |
336 | return true; | |
a24aff65 DE |
337 | } |
338 | ||
d135b1a5 | 339 | bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth) |
a24aff65 | 340 | { |
d135b1a5 DE |
341 | UnRef(); |
342 | ||
343 | m_refData = new wxBitmapRefData; | |
344 | ||
345 | return false; | |
a24aff65 DE |
346 | } |
347 | ||
d135b1a5 | 348 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const |
a24aff65 | 349 | { |
d135b1a5 | 350 | return false; |
a24aff65 DE |
351 | } |
352 | ||
d135b1a5 | 353 | bool wxBitmap::CopyFromIcon(const wxIcon& icno) |
a24aff65 | 354 | { |
d135b1a5 | 355 | return false; |
a24aff65 DE |
356 | } |
357 | ||
d135b1a5 | 358 | wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const |
a24aff65 | 359 | { |
d135b1a5 DE |
360 | return wxNullBitmap; |
361 | } | |
a24aff65 | 362 | |
d135b1a5 | 363 | wxImage wxBitmap::ConvertToImage() const |
a24aff65 | 364 | { |
f8c10ed8 DE |
365 | if(!Ok()) |
366 | return /*wxImage(5,5)*/wxNullImage; | |
d135b1a5 | 367 | return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height); |
a24aff65 DE |
368 | } |
369 | ||
d135b1a5 | 370 | bool wxBitmap::CreateFromXpm(const char **xpm) |
a24aff65 | 371 | { |
d135b1a5 DE |
372 | #if wxUSE_IMAGE && wxUSE_XPM |
373 | UnRef(); | |
374 | ||
375 | wxCHECK_MSG( xpm, false, wxT("invalid XPM data") ) | |
376 | ||
377 | wxXPMDecoder decoder; | |
378 | wxImage img = decoder.ReadData(xpm); | |
379 | wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") ) | |
380 | ||
381 | *this = wxBitmap(img); | |
382 | return true; | |
383 | #else | |
a24aff65 | 384 | return false; |
d135b1a5 | 385 | #endif |
a24aff65 DE |
386 | } |
387 | ||
d135b1a5 | 388 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
a24aff65 | 389 | { |
d135b1a5 DE |
390 | UnRef(); |
391 | ||
392 | wxCHECK_MSG(image.Ok(), false, wxT("invalid image")); | |
393 | wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth")); | |
394 | ||
395 | m_refData = new wxBitmapRefData(); | |
396 | ||
397 | M_BITMAPDATA->m_width = image.GetWidth(); | |
398 | M_BITMAPDATA->m_height = image.GetHeight(); | |
399 | NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc] | |
400 | initWithBitmapDataPlanes: NULL | |
401 | pixelsWide: image.GetWidth() | |
402 | pixelsHigh: image.GetHeight() | |
403 | bitsPerSample: 8 | |
404 | samplesPerPixel: 3 | |
405 | hasAlpha: NO | |
406 | isPlanar: NO | |
407 | colorSpaceName: NSCalibratedRGBColorSpace | |
408 | bytesPerRow: 0 | |
409 | bitsPerPixel: 0]; | |
410 | ||
411 | const int numBytes = image.GetWidth()*image.GetHeight()*3; | |
412 | memcpy([bitmapImage bitmapData], image.GetData(), numBytes); | |
413 | // TODO: Alpha and convert to desired depth | |
414 | M_BITMAPDATA->m_depth = 24; | |
415 | M_BITMAPDATA->m_ok = true; | |
416 | M_BITMAPDATA->m_numColors = 0; | |
417 | M_BITMAPDATA->m_quality = 0; | |
418 | M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage; | |
016b0643 | 419 | M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue())); |
d135b1a5 | 420 | return true; |
a24aff65 DE |
421 | } |
422 | ||
b60c6e97 DE |
423 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
424 | { | |
425 | if(!Ok()) | |
426 | return NULL; | |
427 | ||
428 | NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep; | |
429 | if(!bitmapRep) | |
430 | return NULL; | |
431 | ||
432 | if([bitmapRep bitsPerPixel]!=bpp) | |
433 | { | |
434 | wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") ); | |
435 | return NULL; | |
436 | } | |
437 | data.m_width = [bitmapRep pixelsWide]; | |
438 | data.m_height = [bitmapRep pixelsHigh]; | |
439 | data.m_stride = [bitmapRep bytesPerRow]; | |
440 | return [bitmapRep bitmapData]; | |
441 | ||
442 | // NOTE: It is up to the user to make sure they used the proper | |
443 | // pixel format class that details what is actually inside the pixels | |
444 | // We can only check to make sure that the total number of bits per | |
445 | // pixel are being iterated over properly | |
446 | // NSBitmapImageRep can contain grayscale or CMYK data and | |
447 | // wxPixelDataBase doesn't really define the color format | |
448 | } | |
449 | ||
450 | void wxBitmap::UngetRawData(wxPixelDataBase& data) | |
451 | { // TODO | |
452 | } | |
453 | ||
d135b1a5 DE |
454 | // ======================================================================== |
455 | // wxMask | |
456 | // ======================================================================== | |
457 | ||
458 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
459 | ||
460 | wxMask::wxMask() | |
a24aff65 | 461 | { |
016b0643 | 462 | m_cocoaNSBitmapImageRep = nil; |
a24aff65 DE |
463 | } |
464 | ||
d135b1a5 DE |
465 | // Construct a mask from a bitmap and a colour indicating |
466 | // the transparent area | |
467 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
a24aff65 | 468 | { |
016b0643 | 469 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 | 470 | Create(bitmap, colour); |
a24aff65 DE |
471 | } |
472 | ||
d135b1a5 DE |
473 | // Construct a mask from a bitmap and a palette index indicating |
474 | // the transparent area | |
475 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
a24aff65 | 476 | { |
016b0643 | 477 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 DE |
478 | |
479 | Create(bitmap, paletteIndex); | |
a24aff65 DE |
480 | } |
481 | ||
d135b1a5 DE |
482 | // Construct a mask from a mono bitmap (copies the bitmap). |
483 | wxMask::wxMask(const wxBitmap& bitmap) | |
a24aff65 | 484 | { |
016b0643 | 485 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 DE |
486 | |
487 | Create(bitmap); | |
a24aff65 DE |
488 | } |
489 | ||
d135b1a5 | 490 | wxMask::~wxMask() |
a24aff65 | 491 | { |
016b0643 | 492 | [m_cocoaNSBitmapImageRep release]; |
a24aff65 DE |
493 | } |
494 | ||
d135b1a5 DE |
495 | // Create a mask from a mono bitmap (copies the bitmap). |
496 | bool wxMask::Create(const wxBitmap& bitmap) | |
a24aff65 | 497 | { |
d135b1a5 DE |
498 | // TODO |
499 | return FALSE; | |
a24aff65 DE |
500 | } |
501 | ||
d135b1a5 DE |
502 | // Create a mask from a bitmap and a palette index indicating |
503 | // the transparent area | |
504 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
a24aff65 | 505 | { |
d135b1a5 DE |
506 | // TODO |
507 | return FALSE; | |
a24aff65 DE |
508 | } |
509 | ||
016b0643 DE |
510 | template <class PixelData> |
511 | static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData) | |
512 | { | |
2b030203 | 513 | wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data")); |
016b0643 DE |
514 | class PixelData::Iterator p(srcData); |
515 | const int nRows = srcData.GetHeight(); | |
516 | const int nCols = srcData.GetWidth(); | |
517 | // Total number of bytes per destination column | |
518 | const int dstRowLength = (nCols+7)/8; | |
519 | // Number of source columns that fit into a byte in the destination | |
520 | const int width_aligned = nCols/8*8; | |
521 | for(int y=0; y<nRows; ++y) | |
522 | { | |
523 | class PixelData::Iterator rowStart(p); | |
524 | unsigned char *dstRow = dstData + y*dstRowLength; | |
525 | for(int x=0; x<width_aligned; x+=8) | |
526 | { | |
527 | unsigned char *dstByte = dstRow + x/8; | |
528 | *dstByte = 0; | |
529 | // Take source RGB, compare it with the wxColour | |
530 | for(int j=0; j<8; ++j, ++p) | |
531 | { | |
532 | *dstByte += | |
533 | ( p.Red()!=colour.Red() | |
534 | || p.Green()!=colour.Green() | |
535 | || p.Blue()!=colour.Blue() | |
536 | ) << (7-j); | |
537 | } | |
538 | } | |
539 | // Handle the remaining 0-7 pixels in the row | |
540 | unsigned char *dstByte = dstRow + width_aligned/8; | |
541 | *dstByte = 0; | |
542 | for(int j=0; j<(nCols%8); ++j, ++p) | |
543 | { | |
544 | *dstByte += | |
545 | ( p.Red()!=colour.Red() | |
546 | || p.Green()!=colour.Green() | |
547 | || p.Blue()!=colour.Blue() | |
548 | ) << (7-j); | |
549 | } | |
550 | p = rowStart; | |
551 | p.OffsetY(srcData,1); | |
552 | } | |
553 | return true; | |
554 | } | |
555 | ||
d135b1a5 DE |
556 | // Create a mask from a bitmap and a colour indicating |
557 | // the transparent area | |
558 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
a24aff65 | 559 | { |
016b0643 DE |
560 | wxAutoNSAutoreleasePool pool; |
561 | if(!bitmap.Ok()) | |
562 | return false; | |
563 | int bmpWidth = bitmap.GetWidth(); | |
564 | int bmpHeight = bitmap.GetHeight(); | |
565 | int dstRowLength = (bmpWidth+7)/8; | |
566 | ||
567 | // Create a bitmap image rep with 1-bit per pixel data representing | |
568 | // the alpha channel padded such that rows end on byte boundaries | |
569 | // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace | |
570 | // we must have at least one channel of non-alpha data. In order to | |
571 | // make our life easy, we use planar data which results in two | |
572 | // separate arrays. We don't need to touch the first because it | |
573 | // should never be used. The second is the 1-bit "alpha" data. | |
574 | NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc] | |
575 | initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth | |
576 | pixelsHigh:bmpHeight bitsPerSample:1 | |
577 | samplesPerPixel:2 hasAlpha:YES isPlanar:YES | |
578 | colorSpaceName:NSCalibratedWhiteColorSpace | |
579 | bytesPerRow:dstRowLength bitsPerPixel:1] autorelease]; | |
580 | wxCHECK(maskRep,false); | |
581 | ||
582 | // We need the source NSBitmapImageRep to detemine its pixel format | |
ce20d822 | 583 | NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep(); |
2b030203 | 584 | wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap")); |
016b0643 DE |
585 | |
586 | // Get a pointer to the destination data | |
587 | unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL}; | |
588 | [maskRep getBitmapDataPlanes:dstPlanes]; | |
589 | unsigned char *dstData = dstPlanes[1]; | |
ea3d4caf | 590 | // The wxImage format (which we use whenever we imported from wxImage) |
016b0643 DE |
591 | if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO) |
592 | { | |
593 | wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
594 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 595 | false, wxT("Unable to access raw data")); |
016b0643 | 596 | } |
ea3d4caf DE |
597 | // 32-bpp RGBx (x=throw away, no alpha) |
598 | else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO) | |
599 | { | |
600 | typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat; | |
601 | wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
602 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 603 | false, wxT("Unable to access raw data")); |
ea3d4caf DE |
604 | } |
605 | // 32-bpp RGBA | |
016b0643 DE |
606 | else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES) |
607 | { | |
608 | wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
609 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 610 | false, wxT("Unable to access raw data")); |
016b0643 DE |
611 | } |
612 | else | |
2b030203 | 613 | { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); } |
016b0643 DE |
614 | |
615 | // maskRep was autoreleased in case we had to exit quickly | |
616 | m_cocoaNSBitmapImageRep = [maskRep retain]; | |
617 | return true; | |
a24aff65 DE |
618 | } |
619 |