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