]>
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 | ||
d381b7df DE |
128 | wxBitmap::wxBitmap(NSImage* cocoaNSImage) |
129 | { | |
130 | (void) Create(cocoaNSImage); | |
131 | } | |
132 | ||
133 | wxBitmap::wxBitmap(NSBitmapImageRep* cocoaNSBitmapImageRep) | |
134 | { | |
135 | (void) Create(cocoaNSBitmapImageRep); | |
136 | } | |
137 | ||
452418c4 | 138 | wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth) |
a24aff65 DE |
139 | { |
140 | (void) Create(data, type, width, height, depth); | |
a24aff65 DE |
141 | } |
142 | ||
143 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) | |
144 | { | |
145 | LoadFile(filename, type); | |
a24aff65 DE |
146 | } |
147 | ||
d135b1a5 | 148 | wxObjectRefData *wxBitmap::CreateRefData() const |
a24aff65 | 149 | { |
d135b1a5 | 150 | return new wxBitmapRefData; |
a24aff65 DE |
151 | } |
152 | ||
d135b1a5 | 153 | wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const |
a24aff65 | 154 | { |
d135b1a5 | 155 | return new wxBitmapRefData(*(wxBitmapRefData*)data); |
a24aff65 DE |
156 | } |
157 | ||
d135b1a5 | 158 | WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep() |
a24aff65 | 159 | { |
d135b1a5 DE |
160 | if(!M_BITMAPDATA) |
161 | return NULL; | |
162 | return M_BITMAPDATA->m_cocoaNSBitmapImageRep; | |
a24aff65 DE |
163 | } |
164 | ||
9c54e4ae DE |
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 | { | |
9a6e13a6 DE |
176 | // Show before/after to prove that the bitmap itself is not changed |
177 | // even though we just composited onto the NSImage | |
a6d27d48 | 178 | wxLogTrace(wxTRACE_COCOA,wxT("Before: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]); |
9c54e4ae DE |
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]; | |
9a6e13a6 | 185 | [maskImage release]; |
a6d27d48 | 186 | wxLogTrace(wxTRACE_COCOA,wxT("After: bpp=%d"),[M_BITMAPDATA->m_cocoaNSBitmapImageRep bitsPerPixel]); |
9c54e4ae DE |
187 | } |
188 | return nsimage; | |
189 | } | |
190 | ||
ab13160e DE |
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 | ||
a24aff65 DE |
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 | ||
b7cacb43 | 257 | bool wxBitmap::IsOk() const |
a24aff65 | 258 | { |
d135b1a5 | 259 | return m_refData && M_BITMAPDATA->m_ok; |
a24aff65 DE |
260 | } |
261 | ||
d135b1a5 | 262 | wxPalette* wxBitmap::GetPalette() const |
a24aff65 | 263 | { |
d135b1a5 DE |
264 | if(!m_refData) |
265 | return NULL; | |
266 | return &M_BITMAPDATA->m_bitmapPalette; | |
a24aff65 DE |
267 | } |
268 | ||
d135b1a5 | 269 | wxMask* wxBitmap::GetMask() const |
a24aff65 | 270 | { |
d135b1a5 DE |
271 | if(!m_refData) |
272 | return NULL; | |
273 | return M_BITMAPDATA->m_bitmapMask; | |
a24aff65 DE |
274 | } |
275 | ||
d135b1a5 | 276 | int wxBitmap::GetDepth() const |
a24aff65 | 277 | { |
d135b1a5 DE |
278 | if(!m_refData) |
279 | return 0; | |
280 | return M_BITMAPDATA->m_depth; | |
a24aff65 DE |
281 | } |
282 | ||
d135b1a5 | 283 | int wxBitmap::GetWidth() const |
a24aff65 | 284 | { |
d135b1a5 DE |
285 | if(!m_refData) |
286 | return 0; | |
287 | return M_BITMAPDATA->m_width; | |
a24aff65 DE |
288 | } |
289 | ||
d135b1a5 | 290 | int wxBitmap::GetHeight() const |
a24aff65 | 291 | { |
d135b1a5 DE |
292 | if(!m_refData) |
293 | return 0; | |
294 | return M_BITMAPDATA->m_height; | |
a24aff65 DE |
295 | } |
296 | ||
d135b1a5 | 297 | bool wxBitmap::Create(int w, int h, int d) |
a24aff65 | 298 | { |
d135b1a5 DE |
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 */ | |
ab13160e DE |
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 | |
efe85495 DE |
317 | bytesPerRow: 0 // NOTE: Contrary to Apple documentation Mac OS |
318 | // 10.4 will add padding bytes when 0 is used here | |
ab13160e DE |
319 | bitsPerPixel: 0]; |
320 | ||
48580976 | 321 | wxLogTrace(wxTRACE_COCOA,wxT("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p"), M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]); |
ab13160e DE |
322 | M_BITMAPDATA->m_ok = true; |
323 | M_BITMAPDATA->m_numColors = 0; | |
324 | M_BITMAPDATA->m_quality = 0; | |
325 | M_BITMAPDATA->m_bitmapMask = NULL; | |
d135b1a5 DE |
326 | |
327 | return M_BITMAPDATA->m_ok; | |
a24aff65 DE |
328 | } |
329 | ||
d135b1a5 | 330 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
a24aff65 | 331 | { |
d135b1a5 DE |
332 | wxAutoNSAutoreleasePool pool; |
333 | UnRef(); | |
a24aff65 | 334 | |
d135b1a5 | 335 | m_refData = new wxBitmapRefData; |
a24aff65 | 336 | |
d135b1a5 DE |
337 | NSBitmapImageRep *imageRep = [NSBitmapImageRep |
338 | imageRepWithContentsOfFile:wxNSStringWithWxString(filename)]; | |
a24aff65 | 339 | |
d135b1a5 DE |
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; | |
a24aff65 DE |
359 | } |
360 | ||
d381b7df DE |
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 | ||
452418c4 | 388 | bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth) |
a24aff65 | 389 | { |
d135b1a5 DE |
390 | UnRef(); |
391 | ||
392 | m_refData = new wxBitmapRefData; | |
393 | ||
394 | return false; | |
a24aff65 DE |
395 | } |
396 | ||
d135b1a5 | 397 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const |
a24aff65 | 398 | { |
d135b1a5 | 399 | return false; |
a24aff65 DE |
400 | } |
401 | ||
68a8c89b | 402 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
a24aff65 | 403 | { |
99635c99 DE |
404 | // Pool here due to lack of one during wx init phase |
405 | wxAutoNSAutoreleasePool pool; | |
406 | ||
68a8c89b DE |
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]; | |
b4918747 | 421 | M_BITMAPDATA->m_depth = [newBitmapRep bitsPerSample]*[newBitmapRep samplesPerPixel]; |
68a8c89b DE |
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; | |
a24aff65 DE |
427 | } |
428 | ||
4706bee7 | 429 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const |
a24aff65 | 430 | { |
4706bee7 DE |
431 | wxAutoNSAutoreleasePool pool; |
432 | if(!Ok()) | |
433 | return wxNullBitmap; | |
434 | NSImage *nsimage = GetNSImage(false); | |
435 | ||
436 | [nsimage lockFocus]; | |
437 | NSRect imageRect = {{0,0}, [nsimage size]}; | |
438 | imageRect.origin.x = imageRect.size.width * rect.x / GetWidth(); | |
439 | imageRect.origin.y = imageRect.size.height * rect.y / GetHeight(); | |
440 | imageRect.size.width *= wx_static_cast(CGFloat, rect.width) / GetWidth(); | |
441 | imageRect.size.height *= wx_static_cast(CGFloat, rect.height) / GetHeight(); | |
442 | ||
443 | NSBitmapImageRep *newBitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect]; | |
444 | [nsimage unlockFocus]; | |
445 | ||
446 | wxBitmap newBitmap(newBitmapRep); | |
447 | ||
448 | return (newBitmap); | |
d135b1a5 | 449 | } |
a24aff65 | 450 | |
d135b1a5 | 451 | wxImage wxBitmap::ConvertToImage() const |
a24aff65 | 452 | { |
2a8db3cd | 453 | wxAutoNSAutoreleasePool pool; |
f8c10ed8 DE |
454 | if(!Ok()) |
455 | return /*wxImage(5,5)*/wxNullImage; | |
2a8db3cd DE |
456 | NSImage *nsimage = GetNSImage(false /* don't use mask */); |
457 | wxImage newImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height); | |
458 | [nsimage lockFocus]; | |
459 | for(int i=0; i < M_BITMAPDATA->m_width; i++) | |
460 | { | |
461 | // Don't let the pool get too big as you'll notice we're creating | |
462 | // two autoreleased NSColor objects with every iteration. | |
463 | wxAutoNSAutoreleasePool loopPool; | |
464 | for(int j=0; j < M_BITMAPDATA->m_height; j++) | |
465 | { | |
466 | NSColor *pixelColor = NSReadPixel(NSMakePoint(i,M_BITMAPDATA->m_height - j - 1)); | |
467 | NSColor *color = [pixelColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; | |
468 | newImage.SetRGB(i,j,int([color redComponent]*255.0), int([color greenComponent]*255.0), int([color blueComponent]*255.0)); | |
469 | } | |
470 | } | |
471 | [nsimage unlockFocus]; | |
472 | return newImage; | |
a24aff65 DE |
473 | } |
474 | ||
d135b1a5 | 475 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
a24aff65 | 476 | { |
d135b1a5 DE |
477 | UnRef(); |
478 | ||
479 | wxCHECK_MSG(image.Ok(), false, wxT("invalid image")); | |
480 | wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth")); | |
481 | ||
482 | m_refData = new wxBitmapRefData(); | |
483 | ||
484 | M_BITMAPDATA->m_width = image.GetWidth(); | |
485 | M_BITMAPDATA->m_height = image.GetHeight(); | |
486 | NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc] | |
487 | initWithBitmapDataPlanes: NULL | |
488 | pixelsWide: image.GetWidth() | |
489 | pixelsHigh: image.GetHeight() | |
490 | bitsPerSample: 8 | |
491 | samplesPerPixel: 3 | |
492 | hasAlpha: NO | |
493 | isPlanar: NO | |
494 | colorSpaceName: NSCalibratedRGBColorSpace | |
efe85495 | 495 | bytesPerRow: image.GetWidth()*3 |
d135b1a5 DE |
496 | bitsPerPixel: 0]; |
497 | ||
efe85495 DE |
498 | // TODO: Specify bytesPerRow:0 and then use [bitmapImage bytesPerRow] |
499 | // so that the rows are aligned suitably for altivec by the OS (Tiger) | |
d135b1a5 DE |
500 | const int numBytes = image.GetWidth()*image.GetHeight()*3; |
501 | memcpy([bitmapImage bitmapData], image.GetData(), numBytes); | |
502 | // TODO: Alpha and convert to desired depth | |
503 | M_BITMAPDATA->m_depth = 24; | |
504 | M_BITMAPDATA->m_ok = true; | |
505 | M_BITMAPDATA->m_numColors = 0; | |
506 | M_BITMAPDATA->m_quality = 0; | |
507 | M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage; | |
016b0643 | 508 | M_BITMAPDATA->m_bitmapMask = new wxMask(*this,wxColour(image.GetMaskRed(),image.GetMaskGreen(),image.GetMaskBlue())); |
d135b1a5 | 509 | return true; |
a24aff65 DE |
510 | } |
511 | ||
b60c6e97 DE |
512 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
513 | { | |
514 | if(!Ok()) | |
515 | return NULL; | |
516 | ||
517 | NSBitmapImageRep *bitmapRep = M_BITMAPDATA->m_cocoaNSBitmapImageRep; | |
518 | if(!bitmapRep) | |
519 | return NULL; | |
520 | ||
521 | if([bitmapRep bitsPerPixel]!=bpp) | |
522 | { | |
523 | wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") ); | |
524 | return NULL; | |
525 | } | |
526 | data.m_width = [bitmapRep pixelsWide]; | |
527 | data.m_height = [bitmapRep pixelsHigh]; | |
528 | data.m_stride = [bitmapRep bytesPerRow]; | |
529 | return [bitmapRep bitmapData]; | |
530 | ||
531 | // NOTE: It is up to the user to make sure they used the proper | |
532 | // pixel format class that details what is actually inside the pixels | |
533 | // We can only check to make sure that the total number of bits per | |
534 | // pixel are being iterated over properly | |
535 | // NSBitmapImageRep can contain grayscale or CMYK data and | |
536 | // wxPixelDataBase doesn't really define the color format | |
537 | } | |
538 | ||
539 | void wxBitmap::UngetRawData(wxPixelDataBase& data) | |
540 | { // TODO | |
541 | } | |
542 | ||
d135b1a5 DE |
543 | // ======================================================================== |
544 | // wxMask | |
545 | // ======================================================================== | |
546 | ||
547 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
548 | ||
549 | wxMask::wxMask() | |
a24aff65 | 550 | { |
016b0643 | 551 | m_cocoaNSBitmapImageRep = nil; |
a24aff65 DE |
552 | } |
553 | ||
d135b1a5 DE |
554 | // Construct a mask from a bitmap and a colour indicating |
555 | // the transparent area | |
556 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
a24aff65 | 557 | { |
016b0643 | 558 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 | 559 | Create(bitmap, colour); |
a24aff65 DE |
560 | } |
561 | ||
d135b1a5 DE |
562 | // Construct a mask from a bitmap and a palette index indicating |
563 | // the transparent area | |
564 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
a24aff65 | 565 | { |
016b0643 | 566 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 DE |
567 | |
568 | Create(bitmap, paletteIndex); | |
a24aff65 DE |
569 | } |
570 | ||
d135b1a5 DE |
571 | // Construct a mask from a mono bitmap (copies the bitmap). |
572 | wxMask::wxMask(const wxBitmap& bitmap) | |
a24aff65 | 573 | { |
016b0643 | 574 | m_cocoaNSBitmapImageRep = nil; |
d135b1a5 DE |
575 | |
576 | Create(bitmap); | |
a24aff65 DE |
577 | } |
578 | ||
319fe103 DE |
579 | // Copy constructor |
580 | wxMask::wxMask(const wxMask& src) | |
581 | : wxObject(src) | |
582 | , m_cocoaNSBitmapImageRep([src.m_cocoaNSBitmapImageRep retain]) | |
583 | { | |
584 | } | |
585 | ||
d135b1a5 | 586 | wxMask::~wxMask() |
a24aff65 | 587 | { |
016b0643 | 588 | [m_cocoaNSBitmapImageRep release]; |
a24aff65 DE |
589 | } |
590 | ||
d135b1a5 DE |
591 | // Create a mask from a mono bitmap (copies the bitmap). |
592 | bool wxMask::Create(const wxBitmap& bitmap) | |
a24aff65 | 593 | { |
d135b1a5 | 594 | // TODO |
148af7c5 | 595 | wxLogDebug(wxT("Cannot yet create a mask from a mono bitmap")); |
d135b1a5 | 596 | return FALSE; |
a24aff65 DE |
597 | } |
598 | ||
d135b1a5 DE |
599 | // Create a mask from a bitmap and a palette index indicating |
600 | // the transparent area | |
601 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
a24aff65 | 602 | { |
d135b1a5 | 603 | // TODO |
148af7c5 | 604 | wxLogDebug(wxT("Cannot yet create a mask from a palette bitmap")); |
d135b1a5 | 605 | return FALSE; |
a24aff65 DE |
606 | } |
607 | ||
360f4262 | 608 | template <typename PixelData> |
016b0643 DE |
609 | static bool wxMask_CreateFromBitmapData(PixelData srcData, const wxColour& colour, unsigned char *dstData) |
610 | { | |
2b030203 | 611 | wxCHECK_MSG(dstData,false,wxT("Couldn't access mask data")); |
360f4262 | 612 | typename PixelData::Iterator p(srcData); |
016b0643 DE |
613 | const int nRows = srcData.GetHeight(); |
614 | const int nCols = srcData.GetWidth(); | |
615 | // Total number of bytes per destination column | |
616 | const int dstRowLength = (nCols+7)/8; | |
617 | // Number of source columns that fit into a byte in the destination | |
618 | const int width_aligned = nCols/8*8; | |
619 | for(int y=0; y<nRows; ++y) | |
620 | { | |
360f4262 | 621 | typename PixelData::Iterator rowStart(p); |
016b0643 DE |
622 | unsigned char *dstRow = dstData + y*dstRowLength; |
623 | for(int x=0; x<width_aligned; x+=8) | |
624 | { | |
625 | unsigned char *dstByte = dstRow + x/8; | |
626 | *dstByte = 0; | |
627 | // Take source RGB, compare it with the wxColour | |
628 | for(int j=0; j<8; ++j, ++p) | |
629 | { | |
630 | *dstByte += | |
631 | ( p.Red()!=colour.Red() | |
632 | || p.Green()!=colour.Green() | |
633 | || p.Blue()!=colour.Blue() | |
634 | ) << (7-j); | |
635 | } | |
636 | } | |
637 | // Handle the remaining 0-7 pixels in the row | |
638 | unsigned char *dstByte = dstRow + width_aligned/8; | |
9a6e13a6 DE |
639 | if(nCols%8>0) |
640 | *dstByte = 0; | |
016b0643 DE |
641 | for(int j=0; j<(nCols%8); ++j, ++p) |
642 | { | |
643 | *dstByte += | |
644 | ( p.Red()!=colour.Red() | |
645 | || p.Green()!=colour.Green() | |
646 | || p.Blue()!=colour.Blue() | |
647 | ) << (7-j); | |
648 | } | |
649 | p = rowStart; | |
650 | p.OffsetY(srcData,1); | |
651 | } | |
652 | return true; | |
653 | } | |
654 | ||
d135b1a5 DE |
655 | // Create a mask from a bitmap and a colour indicating |
656 | // the transparent area | |
657 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
a24aff65 | 658 | { |
016b0643 DE |
659 | wxAutoNSAutoreleasePool pool; |
660 | if(!bitmap.Ok()) | |
661 | return false; | |
662 | int bmpWidth = bitmap.GetWidth(); | |
663 | int bmpHeight = bitmap.GetHeight(); | |
664 | int dstRowLength = (bmpWidth+7)/8; | |
665 | ||
666 | // Create a bitmap image rep with 1-bit per pixel data representing | |
667 | // the alpha channel padded such that rows end on byte boundaries | |
668 | // Since NSBitmapImageRep doesn't have any sort of NSNullColorSpace | |
669 | // we must have at least one channel of non-alpha data. In order to | |
670 | // make our life easy, we use planar data which results in two | |
671 | // separate arrays. We don't need to touch the first because it | |
672 | // should never be used. The second is the 1-bit "alpha" data. | |
673 | NSBitmapImageRep *maskRep = [[[NSBitmapImageRep alloc] | |
674 | initWithBitmapDataPlanes:NULL pixelsWide:bmpWidth | |
675 | pixelsHigh:bmpHeight bitsPerSample:1 | |
676 | samplesPerPixel:2 hasAlpha:YES isPlanar:YES | |
677 | colorSpaceName:NSCalibratedWhiteColorSpace | |
678 | bytesPerRow:dstRowLength bitsPerPixel:1] autorelease]; | |
679 | wxCHECK(maskRep,false); | |
680 | ||
681 | // We need the source NSBitmapImageRep to detemine its pixel format | |
ce20d822 | 682 | NSBitmapImageRep *srcBitmapRep = const_cast<wxBitmap&>(bitmap).GetNSBitmapImageRep(); |
2b030203 | 683 | wxCHECK_MSG(srcBitmapRep,false,wxT("Can't create mask for an uninitialized bitmap")); |
016b0643 DE |
684 | |
685 | // Get a pointer to the destination data | |
686 | unsigned char *dstPlanes[5] = {NULL,NULL,NULL,NULL,NULL}; | |
687 | [maskRep getBitmapDataPlanes:dstPlanes]; | |
688 | unsigned char *dstData = dstPlanes[1]; | |
ea3d4caf | 689 | // The wxImage format (which we use whenever we imported from wxImage) |
016b0643 DE |
690 | if([srcBitmapRep bitsPerPixel]==24 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO) |
691 | { | |
692 | wxPixelData<wxBitmap,wxNativePixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
693 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 694 | false, wxT("Unable to access raw data")); |
016b0643 | 695 | } |
ea3d4caf DE |
696 | // 32-bpp RGBx (x=throw away, no alpha) |
697 | else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==3 && [srcBitmapRep hasAlpha]==NO) | |
698 | { | |
699 | typedef wxPixelFormat<unsigned char,32,0,1,2> PixelFormat; | |
700 | wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
701 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 702 | false, wxT("Unable to access raw data")); |
ea3d4caf DE |
703 | } |
704 | // 32-bpp RGBA | |
016b0643 DE |
705 | else if([srcBitmapRep bitsPerPixel]==32 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==4 && [srcBitmapRep hasAlpha]==YES) |
706 | { | |
707 | wxPixelData<wxBitmap,wxAlphaPixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
708 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
2b030203 | 709 | false, wxT("Unable to access raw data")); |
016b0643 | 710 | } |
319fe103 DE |
711 | else if([srcBitmapRep bitsPerPixel]==8 && [srcBitmapRep bitsPerSample]==8 && [srcBitmapRep samplesPerPixel]==1 && [srcBitmapRep hasAlpha]==NO) |
712 | // 8-bpp Grayscale, no alpha | |
713 | { // Force all RGB to access the same grayscale component | |
714 | typedef wxPixelFormat<unsigned char,8,0,0,0> PixelFormat; | |
715 | wxPixelData<wxBitmap,PixelFormat> pixelData(const_cast<wxBitmap&>(bitmap)); | |
716 | wxCHECK_MSG(wxMask_CreateFromBitmapData(pixelData, colour, dstData), | |
717 | false, wxT("Unable to access raw data")); | |
718 | } | |
016b0643 | 719 | else |
2b030203 | 720 | { wxCHECK_MSG(false,false,wxT("Unimplemented pixel format")); } |
016b0643 DE |
721 | |
722 | // maskRep was autoreleased in case we had to exit quickly | |
723 | m_cocoaNSBitmapImageRep = [maskRep retain]; | |
724 | return true; | |
a24aff65 | 725 | } |