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