]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
96dabe43 | 2 | // Name: src/osx/core/bitmap.cpp |
489468fe SC |
3 | // Purpose: wxBitmap |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/bitmap.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/log.h" | |
18 | #include "wx/dcmemory.h" | |
19 | #include "wx/icon.h" | |
20 | #include "wx/image.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/metafile.h" | |
acc255e9 PC |
24 | #include "wx/xpmdecod.h" |
25 | ||
489468fe SC |
26 | #include "wx/rawbmp.h" |
27 | ||
28 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) | |
29 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
30 | ||
b2680ced | 31 | #if wxOSX_USE_CARBON |
b96ad102 | 32 | #include "wx/osx/uma.h" |
b2680ced SC |
33 | #else |
34 | #include "wx/osx/private.h" | |
35 | #endif | |
36 | ||
489468fe SC |
37 | #ifndef __WXOSX_IPHONE__ |
38 | #include <QuickTime/QuickTime.h> | |
39 | #endif | |
40 | ||
41 | CGColorSpaceRef wxMacGetGenericRGBColorSpace(); | |
42 | CGDataProviderRef wxMacCGDataProviderCreateWithMemoryBuffer( const wxMemoryBuffer& buf ); | |
43 | ||
44 | // Implementation Notes | |
45 | // -------------------- | |
46 | // | |
47 | // we are always working with a 32 bit deep pixel buffer | |
48 | // under QuickDraw its alpha parts are going to be ignored in the GWorld, | |
49 | // therefore we have a separate GWorld there for blitting the mask in | |
50 | ||
51 | // under Quartz then content is transformed into a CGImageRef representing the same data | |
52 | // which can be transferred to the GPU by the OS for fast rendering | |
53 | ||
54 | class WXDLLEXPORT wxBitmapRefData: public wxGDIRefData | |
55 | { | |
56 | friend class WXDLLIMPEXP_FWD_CORE wxIcon; | |
57 | friend class WXDLLIMPEXP_FWD_CORE wxCursor; | |
58 | public: | |
59 | wxBitmapRefData(int width , int height , int depth); | |
aaa7d63c | 60 | wxBitmapRefData(CGImageRef image); |
489468fe SC |
61 | wxBitmapRefData(); |
62 | wxBitmapRefData(const wxBitmapRefData &tocopy); | |
63 | ||
64 | virtual ~wxBitmapRefData(); | |
65 | ||
66 | virtual bool IsOk() const { return m_ok; } | |
67 | ||
68 | void Free(); | |
69 | void SetOk( bool isOk) { m_ok = isOk; } | |
70 | ||
71 | void SetWidth( int width ) { m_width = width; } | |
72 | void SetHeight( int height ) { m_height = height; } | |
73 | void SetDepth( int depth ) { m_depth = depth; } | |
74 | ||
75 | int GetWidth() const { return m_width; } | |
76 | int GetHeight() const { return m_height; } | |
77 | int GetDepth() const { return m_depth; } | |
78 | ||
79 | void *GetRawAccess() const; | |
80 | void *BeginRawAccess(); | |
81 | void EndRawAccess(); | |
82 | ||
83 | bool HasAlpha() const { return m_hasAlpha; } | |
84 | void UseAlpha( bool useAlpha ); | |
85 | ||
86 | public: | |
87 | #if wxUSE_PALETTE | |
88 | wxPalette m_bitmapPalette; | |
89 | #endif // wxUSE_PALETTE | |
90 | ||
91 | wxMask * m_bitmapMask; // Optional mask | |
92 | CGImageRef CreateCGImage() const; | |
93 | ||
94 | // returns true if the bitmap has a size that | |
95 | // can be natively transferred into a true icon | |
96 | // if no is returned GetIconRef will still produce | |
97 | // an icon but it will be generated via a PICT and | |
98 | // rescaled to 16 x 16 | |
99 | bool HasNativeSize(); | |
100 | ||
101 | // caller should increase ref count if needed longer | |
102 | // than the bitmap exists | |
103 | IconRef GetIconRef(); | |
104 | ||
105 | #ifndef __WXOSX_IPHONE__ | |
106 | // returns a Pict from the bitmap content | |
107 | PicHandle GetPictHandle(); | |
108 | #endif | |
109 | ||
110 | CGContextRef GetBitmapContext() const; | |
111 | ||
112 | int GetBytesPerRow() const { return m_bytesPerRow; } | |
acc255e9 | 113 | private : |
489468fe | 114 | bool Create(int width , int height , int depth); |
aaa7d63c | 115 | bool Create( CGImageRef image ); |
489468fe SC |
116 | void Init(); |
117 | ||
118 | int m_width; | |
119 | int m_height; | |
120 | int m_bytesPerRow; | |
121 | int m_depth; | |
122 | bool m_hasAlpha; | |
123 | wxMemoryBuffer m_memBuf; | |
124 | int m_rawAccessCount; | |
125 | bool m_ok; | |
126 | mutable CGImageRef m_cgImageRef; | |
127 | ||
128 | IconRef m_iconRef; | |
129 | #ifndef __WXOSX_IPHONE__ | |
130 | PicHandle m_pictHandle; | |
131 | #endif | |
132 | CGContextRef m_hBitmap; | |
133 | }; | |
134 | ||
135 | ||
292e5e1f | 136 | #define wxOSX_USE_PREMULTIPLIED_ALPHA 1 |
489468fe SC |
137 | static const int kBestByteAlignement = 16; |
138 | static const int kMaskBytesPerPixel = 1; | |
139 | ||
140 | static int GetBestBytesPerRow( int rawBytes ) | |
141 | { | |
142 | return (((rawBytes)+kBestByteAlignement-1) & ~(kBestByteAlignement-1) ); | |
143 | } | |
144 | ||
145 | #if wxUSE_GUI && !defined(__WXOSX_IPHONE__) | |
146 | ||
147 | // this is used for more controls than just the wxBitmap button, also for notebooks etc | |
148 | ||
149 | void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) | |
150 | { | |
151 | memset( info , 0 , sizeof(ControlButtonContentInfo) ) ; | |
a1b806b9 | 152 | if ( bitmap.IsOk() ) |
489468fe SC |
153 | { |
154 | wxBitmapRefData * bmap = bitmap.GetBitmapData() ; | |
155 | if ( bmap == NULL ) | |
156 | return ; | |
157 | ||
158 | if ( forceType == 0 ) | |
159 | { | |
160 | forceType = kControlContentCGImageRef; | |
161 | } | |
162 | ||
163 | if ( forceType == kControlContentIconRef ) | |
164 | { | |
165 | wxBitmap scaleBmp ; | |
166 | wxBitmapRefData* bmp = bmap ; | |
167 | ||
168 | if ( !bmap->HasNativeSize() ) | |
169 | { | |
170 | // as PICT conversion will only result in a 16x16 icon, let's attempt | |
171 | // a few scales for better results | |
172 | ||
173 | int w = bitmap.GetWidth() ; | |
174 | int h = bitmap.GetHeight() ; | |
175 | int sz = wxMax( w , h ) ; | |
176 | if ( sz == 24 || sz == 64 ) | |
177 | { | |
178 | scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ; | |
179 | bmp = scaleBmp.GetBitmapData() ; | |
180 | } | |
181 | } | |
182 | ||
183 | info->contentType = kControlContentIconRef ; | |
184 | info->u.iconRef = bmp->GetIconRef() ; | |
185 | AcquireIconRef( info->u.iconRef ) ; | |
186 | } | |
187 | else if ( forceType == kControlContentCGImageRef ) | |
188 | { | |
189 | info->contentType = kControlContentCGImageRef ; | |
190 | info->u.imageRef = (CGImageRef) bmap->CreateCGImage() ; | |
191 | } | |
192 | else | |
193 | { | |
194 | #ifndef __LP64__ | |
195 | info->contentType = kControlContentPictHandle ; | |
196 | info->u.picture = bmap->GetPictHandle() ; | |
197 | #endif | |
198 | } | |
199 | } | |
200 | } | |
201 | ||
202 | CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) | |
203 | { | |
204 | wxBitmapRefData * bmap = bitmap.GetBitmapData() ; | |
205 | if ( bmap == NULL ) | |
206 | return NULL ; | |
207 | return (CGImageRef) bmap->CreateCGImage(); | |
208 | } | |
209 | ||
210 | void wxMacReleaseBitmapButton( ControlButtonContentInfo*info ) | |
211 | { | |
212 | if ( info->contentType == kControlContentIconRef ) | |
213 | { | |
214 | ReleaseIconRef( info->u.iconRef ) ; | |
215 | } | |
216 | else if ( info->contentType == kControlNoContent ) | |
217 | { | |
218 | // there's no bitmap at all, fall through silently | |
219 | } | |
220 | else if ( info->contentType == kControlContentPictHandle ) | |
221 | { | |
222 | // owned by the bitmap, no release here | |
223 | } | |
224 | else if ( info->contentType == kControlContentCGImageRef ) | |
225 | { | |
226 | CGImageRelease( info->u.imageRef ) ; | |
227 | } | |
228 | else | |
229 | { | |
230 | wxFAIL_MSG(wxT("Unexpected bitmap type") ) ; | |
231 | } | |
232 | } | |
233 | ||
234 | #endif //wxUSE_BMPBUTTON | |
235 | ||
236 | #define M_BITMAPDATA ((wxBitmapRefData *)m_refData) | |
237 | ||
238 | void wxBitmapRefData::Init() | |
239 | { | |
240 | m_width = 0 ; | |
241 | m_height = 0 ; | |
242 | m_depth = 0 ; | |
243 | m_bytesPerRow = 0; | |
244 | m_ok = false ; | |
245 | m_bitmapMask = NULL ; | |
246 | m_cgImageRef = NULL ; | |
247 | ||
248 | #ifndef __WXOSX_IPHONE__ | |
249 | m_iconRef = NULL ; | |
250 | m_pictHandle = NULL ; | |
251 | #endif | |
252 | m_hBitmap = NULL ; | |
253 | ||
254 | m_rawAccessCount = 0 ; | |
255 | m_hasAlpha = false; | |
256 | } | |
257 | ||
ea2807a4 | 258 | wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData &tocopy) : wxGDIRefData() |
489468fe SC |
259 | { |
260 | Init(); | |
261 | Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); | |
262 | ||
263 | if (tocopy.m_bitmapMask) | |
264 | m_bitmapMask = new wxMask(*tocopy.m_bitmapMask); | |
265 | else if (tocopy.m_hasAlpha) | |
266 | UseAlpha(true); | |
267 | ||
268 | unsigned char* dest = (unsigned char*)GetRawAccess(); | |
269 | unsigned char* source = (unsigned char*)tocopy.GetRawAccess(); | |
270 | size_t numbytes = m_bytesPerRow * m_height; | |
271 | memcpy( dest, source, numbytes ); | |
272 | } | |
273 | ||
274 | wxBitmapRefData::wxBitmapRefData() | |
275 | { | |
276 | Init() ; | |
277 | } | |
278 | ||
279 | wxBitmapRefData::wxBitmapRefData( int w , int h , int d ) | |
280 | { | |
281 | Init() ; | |
282 | Create( w , h , d ) ; | |
283 | } | |
284 | ||
aaa7d63c SC |
285 | wxBitmapRefData::wxBitmapRefData(CGImageRef image) |
286 | { | |
287 | Init(); | |
288 | Create( image ); | |
289 | } | |
290 | // code from Technical Q&A QA1509 | |
291 | ||
292 | bool wxBitmapRefData::Create(CGImageRef image) | |
293 | { | |
554b2800 | 294 | if ( image != NULL ) |
aaa7d63c | 295 | { |
554b2800 SC |
296 | m_width = CGImageGetWidth(image); |
297 | m_height = CGImageGetHeight(image); | |
298 | m_depth = 32; | |
299 | m_hBitmap = NULL; | |
aaa7d63c | 300 | |
554b2800 SC |
301 | m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; |
302 | size_t size = m_bytesPerRow * m_height ; | |
303 | void* data = m_memBuf.GetWriteBuf( size ) ; | |
304 | if ( data != NULL ) | |
305 | { | |
306 | memset( data , 0 , size ) ; | |
307 | m_memBuf.UngetWriteBuf( size ) ; | |
308 | CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image); | |
4fbb421b | 309 | if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) |
554b2800 SC |
310 | { |
311 | m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); | |
312 | } | |
313 | else | |
314 | { | |
315 | m_hasAlpha = true; | |
316 | m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst ); | |
317 | } | |
5f35207b | 318 | CGRect rect = CGRectMake(0,0,m_width,m_height); |
554b2800 SC |
319 | CGContextDrawImage(m_hBitmap, rect, image); |
320 | ||
321 | wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; | |
322 | CGContextTranslateCTM( m_hBitmap, 0, m_height ); | |
323 | CGContextScaleCTM( m_hBitmap, 1, -1 ); | |
324 | } /* data != NULL */ | |
325 | } | |
aaa7d63c SC |
326 | m_ok = ( m_hBitmap != NULL ) ; |
327 | ||
328 | return m_ok ; | |
329 | ||
330 | } | |
331 | ||
489468fe SC |
332 | bool wxBitmapRefData::Create( int w , int h , int d ) |
333 | { | |
334 | m_width = wxMax(1, w); | |
335 | m_height = wxMax(1, h); | |
336 | m_depth = d ; | |
337 | m_hBitmap = NULL ; | |
338 | ||
795160b7 SC |
339 | m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ; |
340 | size_t size = m_bytesPerRow * m_height ; | |
489468fe SC |
341 | void* data = m_memBuf.GetWriteBuf( size ) ; |
342 | if ( data != NULL ) | |
343 | { | |
344 | memset( data , 0 , size ) ; | |
345 | m_memBuf.UngetWriteBuf( size ) ; | |
346 | ||
347 | m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); | |
348 | wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; | |
349 | CGContextTranslateCTM( m_hBitmap, 0, m_height ); | |
350 | CGContextScaleCTM( m_hBitmap, 1, -1 ); | |
351 | } /* data != NULL */ | |
352 | m_ok = ( m_hBitmap != NULL ) ; | |
353 | ||
354 | return m_ok ; | |
355 | } | |
356 | ||
357 | void wxBitmapRefData::UseAlpha( bool use ) | |
358 | { | |
359 | if ( m_hasAlpha == use ) | |
360 | return ; | |
361 | ||
362 | m_hasAlpha = use ; | |
363 | ||
364 | CGContextRelease( m_hBitmap ); | |
365 | m_hBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), m_hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); | |
366 | wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; | |
367 | CGContextTranslateCTM( m_hBitmap, 0, m_height ); | |
368 | CGContextScaleCTM( m_hBitmap, 1, -1 ); | |
369 | } | |
370 | ||
371 | void *wxBitmapRefData::GetRawAccess() const | |
372 | { | |
373 | wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; | |
374 | return m_memBuf.GetData() ; | |
375 | } | |
376 | ||
377 | void *wxBitmapRefData::BeginRawAccess() | |
378 | { | |
379 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ; | |
380 | wxASSERT( m_rawAccessCount == 0 ) ; | |
381 | #ifndef __WXOSX_IPHONE__ | |
382 | wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL , | |
383 | wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; | |
384 | #endif | |
385 | ++m_rawAccessCount ; | |
386 | ||
387 | // we must destroy an existing cached image, as | |
388 | // the bitmap data may change now | |
389 | if ( m_cgImageRef ) | |
390 | { | |
391 | CGImageRelease( m_cgImageRef ) ; | |
392 | m_cgImageRef = NULL ; | |
393 | } | |
394 | ||
395 | return m_memBuf.GetData() ; | |
396 | } | |
397 | ||
398 | void wxBitmapRefData::EndRawAccess() | |
399 | { | |
400 | wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; | |
401 | wxASSERT( m_rawAccessCount == 1 ) ; | |
402 | ||
403 | --m_rawAccessCount ; | |
404 | } | |
405 | ||
406 | bool wxBitmapRefData::HasNativeSize() | |
407 | { | |
408 | int w = GetWidth() ; | |
409 | int h = GetHeight() ; | |
410 | int sz = wxMax( w , h ) ; | |
411 | ||
412 | return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 ); | |
413 | } | |
414 | ||
415 | #ifndef __WXOSX_IPHONE__ | |
416 | IconRef wxBitmapRefData::GetIconRef() | |
417 | { | |
418 | if ( m_iconRef == NULL ) | |
419 | { | |
420 | // Create Icon Family Handle | |
421 | ||
422 | IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle( 0 ); | |
423 | ||
424 | int w = GetWidth() ; | |
425 | int h = GetHeight() ; | |
426 | int sz = wxMax( w , h ) ; | |
427 | ||
428 | OSType dataType = 0 ; | |
429 | OSType maskType = 0 ; | |
430 | ||
431 | switch (sz) | |
432 | { | |
433 | case 128: | |
29c829ba | 434 | dataType = kIconServices128PixelDataARGB ; |
489468fe SC |
435 | break; |
436 | ||
437 | case 48: | |
29c829ba | 438 | dataType = kIconServices48PixelDataARGB ; |
489468fe SC |
439 | break; |
440 | ||
441 | case 32: | |
29c829ba | 442 | dataType = kIconServices32PixelDataARGB ; |
489468fe SC |
443 | break; |
444 | ||
445 | case 16: | |
29c829ba | 446 | dataType = kIconServices16PixelDataARGB ; |
489468fe SC |
447 | break; |
448 | ||
449 | default: | |
450 | break; | |
451 | } | |
452 | ||
453 | if ( dataType != 0 ) | |
454 | { | |
29c829ba | 455 | if ( maskType == 0 ) |
489468fe SC |
456 | { |
457 | size_t datasize = sz * sz * 4 ; | |
458 | Handle data = NewHandle( datasize ) ; | |
459 | HLock( data ) ; | |
460 | unsigned char* ptr = (unsigned char*) *data ; | |
461 | memset( ptr, 0, datasize ); | |
462 | bool hasAlpha = HasAlpha() ; | |
463 | wxMask *mask = m_bitmapMask ; | |
464 | unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; | |
465 | unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; | |
466 | ||
467 | for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) | |
468 | { | |
469 | unsigned char * source = sourcePtr; | |
470 | unsigned char * masksource = masksourcePtr; | |
471 | unsigned char * dest = ptr + y * sz * 4 ; | |
472 | unsigned char a, r, g, b; | |
473 | ||
474 | for ( int x = 0 ; x < w ; ++x ) | |
475 | { | |
476 | a = *source ++ ; | |
477 | r = *source ++ ; | |
478 | g = *source ++ ; | |
479 | b = *source ++ ; | |
480 | ||
481 | if ( mask ) | |
482 | { | |
483 | a = 0xFF - *masksource++ ; | |
484 | } | |
485 | else if ( !hasAlpha ) | |
486 | a = 0xFF ; | |
487 | else | |
488 | { | |
292e5e1f | 489 | #if wxOSX_USE_PREMULTIPLIED_ALPHA |
489468fe SC |
490 | // this must be non-premultiplied data |
491 | if ( a != 0xFF && a!= 0 ) | |
492 | { | |
493 | r = r * 255 / a; | |
494 | g = g * 255 / a; | |
495 | b = b * 255 / a; | |
496 | } | |
497 | #endif | |
498 | } | |
499 | *dest++ = a ; | |
500 | *dest++ = r ; | |
501 | *dest++ = g ; | |
502 | *dest++ = b ; | |
503 | ||
504 | } | |
505 | } | |
506 | HUnlock( data ); | |
83c23edb | 507 | |
489468fe | 508 | OSStatus err = SetIconFamilyData( iconFamily, dataType , data ); |
83c23edb VZ |
509 | if ( err != noErr ) |
510 | { | |
511 | wxFAIL_MSG("Error when adding bitmap"); | |
512 | } | |
513 | ||
489468fe SC |
514 | DisposeHandle( data ); |
515 | } | |
516 | else | |
489468fe SC |
517 | { |
518 | // setup the header properly | |
519 | ||
acc255e9 PC |
520 | Handle data = NULL ; |
521 | Handle maskdata = NULL ; | |
522 | unsigned char * maskptr = NULL ; | |
523 | unsigned char * ptr = NULL ; | |
489468fe SC |
524 | size_t datasize, masksize ; |
525 | ||
526 | datasize = sz * sz * 4 ; | |
527 | data = NewHandle( datasize ) ; | |
528 | HLock( data ) ; | |
529 | ptr = (unsigned char*) *data ; | |
530 | memset( ptr , 0, datasize ) ; | |
531 | ||
532 | masksize = sz * sz ; | |
533 | maskdata = NewHandle( masksize ) ; | |
534 | HLock( maskdata ) ; | |
535 | maskptr = (unsigned char*) *maskdata ; | |
536 | memset( maskptr , 0 , masksize ) ; | |
537 | ||
538 | bool hasAlpha = HasAlpha() ; | |
539 | wxMask *mask = m_bitmapMask ; | |
540 | unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; | |
541 | unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; | |
542 | ||
543 | for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) | |
544 | { | |
545 | unsigned char * source = sourcePtr; | |
546 | unsigned char * masksource = masksourcePtr; | |
547 | unsigned char * dest = ptr + y * sz * 4 ; | |
548 | unsigned char * maskdest = maskptr + y * sz ; | |
549 | unsigned char a, r, g, b; | |
550 | ||
551 | for ( int x = 0 ; x < w ; ++x ) | |
552 | { | |
553 | a = *source ++ ; | |
554 | r = *source ++ ; | |
555 | g = *source ++ ; | |
556 | b = *source ++ ; | |
557 | ||
558 | *dest++ = 0 ; | |
559 | *dest++ = r ; | |
560 | *dest++ = g ; | |
561 | *dest++ = b ; | |
562 | ||
563 | if ( mask ) | |
564 | *maskdest++ = 0xFF - *masksource++ ; | |
565 | else if ( hasAlpha ) | |
566 | *maskdest++ = a ; | |
567 | else | |
568 | *maskdest++ = 0xFF ; | |
569 | } | |
570 | } | |
571 | ||
572 | OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ; | |
573 | wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ; | |
574 | ||
575 | err = SetIconFamilyData( iconFamily, maskType , maskdata ) ; | |
576 | wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ; | |
577 | ||
578 | HUnlock( data ) ; | |
579 | HUnlock( maskdata ) ; | |
580 | DisposeHandle( data ) ; | |
581 | DisposeHandle( maskdata ) ; | |
582 | } | |
583 | } | |
584 | else | |
585 | { | |
586 | PicHandle pic = GetPictHandle() ; | |
587 | SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ; | |
588 | } | |
589 | // transform into IconRef | |
590 | ||
591 | // cleaner version existing from 10.3 upwards | |
592 | HLock((Handle) iconFamily); | |
593 | OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &m_iconRef ); | |
594 | HUnlock((Handle) iconFamily); | |
595 | DisposeHandle( (Handle) iconFamily ) ; | |
596 | ||
597 | wxCHECK_MSG( err == noErr, NULL, wxT("Error when constructing icon ref") ); | |
598 | } | |
599 | ||
600 | return m_iconRef ; | |
601 | } | |
602 | ||
603 | PicHandle wxBitmapRefData::GetPictHandle() | |
604 | { | |
489468fe SC |
605 | return m_pictHandle ; |
606 | } | |
607 | #endif | |
608 | ||
609 | CGImageRef wxBitmapRefData::CreateCGImage() const | |
610 | { | |
611 | wxASSERT( m_ok ) ; | |
612 | wxASSERT( m_rawAccessCount >= 0 ) ; | |
613 | CGImageRef image ; | |
614 | if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) | |
615 | { | |
616 | if ( m_depth != 1 && m_bitmapMask == NULL ) | |
617 | { | |
0cc67fd5 SC |
618 | #if 0 |
619 | // in order for this code to work properly, wxMask would have to invert black and white | |
620 | // in the native bitmap | |
489468fe SC |
621 | if ( m_bitmapMask ) |
622 | { | |
623 | CGImageRef tempImage = CGBitmapContextCreateImage( m_hBitmap ); | |
624 | CGImageRef tempMask = CGBitmapContextCreateImage((CGContextRef) m_bitmapMask->GetHBITMAP() ); | |
625 | image = CGImageCreateWithMask( tempImage, tempMask ); | |
626 | CGImageRelease(tempMask); | |
627 | CGImageRelease(tempImage); | |
628 | } | |
629 | else | |
0cc67fd5 | 630 | #endif |
489468fe SC |
631 | image = CGBitmapContextCreateImage( m_hBitmap ); |
632 | } | |
633 | else | |
634 | { | |
635 | size_t imageSize = m_height * m_bytesPerRow ; | |
636 | void * dataBuffer = m_memBuf.GetData() ; | |
637 | int w = m_width ; | |
638 | int h = m_height ; | |
639 | CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; | |
640 | wxMemoryBuffer membuf; | |
641 | ||
642 | if ( m_bitmapMask ) | |
643 | { | |
644 | alphaInfo = kCGImageAlphaFirst ; | |
645 | unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ; | |
646 | memcpy( destalphastart , dataBuffer , imageSize ) ; | |
647 | unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; | |
648 | int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; | |
649 | for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes) | |
650 | { | |
651 | unsigned char *sourcemask = sourcemaskstart ; | |
652 | unsigned char *destalpha = destalphastart ; | |
653 | for ( int x = 0 ; x < w ; ++x , sourcemask += kMaskBytesPerPixel , destalpha += 4 ) | |
654 | { | |
655 | *destalpha = 0xFF - *sourcemask ; | |
656 | } | |
657 | } | |
658 | membuf.UngetWriteBuf( imageSize ); | |
659 | } | |
660 | else | |
661 | { | |
662 | if ( m_hasAlpha ) | |
663 | { | |
292e5e1f | 664 | #if wxOSX_USE_PREMULTIPLIED_ALPHA |
489468fe SC |
665 | alphaInfo = kCGImageAlphaPremultipliedFirst ; |
666 | #else | |
667 | alphaInfo = kCGImageAlphaFirst ; | |
668 | #endif | |
669 | } | |
670 | ||
671 | membuf = m_memBuf; | |
672 | } | |
673 | ||
674 | CGDataProviderRef dataProvider = NULL ; | |
675 | if ( m_depth == 1 ) | |
676 | { | |
677 | // TODO CHECK ALIGNMENT | |
678 | wxMemoryBuffer maskBuf; | |
679 | unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( m_width * m_height ); | |
680 | unsigned char * bufData = (unsigned char *) membuf.GetData() ; | |
681 | // copy one color component | |
682 | size_t i = 0; | |
683 | for( int y = 0 ; y < m_height ; bufData+= m_bytesPerRow, ++y ) | |
684 | { | |
685 | unsigned char *bufDataIter = bufData+3; | |
686 | for ( int x = 0 ; x < m_width ; bufDataIter += 4, ++x, ++i ) | |
687 | { | |
688 | maskBufData[i] = *bufDataIter; | |
689 | } | |
690 | } | |
691 | maskBuf.UngetWriteBuf( m_width * m_height ); | |
692 | ||
693 | dataProvider = | |
694 | wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); | |
695 | ||
696 | image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false ); | |
697 | } | |
698 | else | |
699 | { | |
700 | CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); | |
701 | dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf ); | |
702 | image = | |
703 | ::CGImageCreate( | |
704 | w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo , | |
705 | dataProvider, NULL , false , kCGRenderingIntentDefault ); | |
706 | } | |
707 | CGDataProviderRelease( dataProvider); | |
708 | } | |
709 | } | |
710 | else | |
711 | { | |
712 | image = m_cgImageRef ; | |
713 | CGImageRetain( image ) ; | |
714 | } | |
715 | ||
716 | if ( m_rawAccessCount == 0 && m_cgImageRef == NULL) | |
717 | { | |
718 | // we keep it for later use | |
719 | m_cgImageRef = image ; | |
720 | CGImageRetain( image ) ; | |
721 | } | |
722 | ||
723 | return image ; | |
724 | } | |
725 | ||
726 | CGContextRef wxBitmapRefData::GetBitmapContext() const | |
727 | { | |
728 | return m_hBitmap; | |
729 | } | |
730 | ||
731 | void wxBitmapRefData::Free() | |
732 | { | |
733 | wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; | |
734 | ||
735 | if ( m_cgImageRef ) | |
736 | { | |
737 | CGImageRelease( m_cgImageRef ) ; | |
738 | m_cgImageRef = NULL ; | |
739 | } | |
740 | #ifndef __WXOSX_IPHONE__ | |
741 | if ( m_iconRef ) | |
742 | { | |
743 | ReleaseIconRef( m_iconRef ) ; | |
744 | m_iconRef = NULL ; | |
745 | } | |
746 | ||
747 | #ifndef __LP64__ | |
748 | if ( m_pictHandle ) | |
749 | { | |
489468fe SC |
750 | m_pictHandle = NULL ; |
751 | } | |
752 | #endif | |
753 | #endif | |
754 | if ( m_hBitmap ) | |
755 | { | |
756 | CGContextRelease(m_hBitmap); | |
757 | m_hBitmap = NULL ; | |
758 | } | |
759 | ||
5276b0a5 | 760 | wxDELETE(m_bitmapMask); |
489468fe SC |
761 | } |
762 | ||
763 | wxBitmapRefData::~wxBitmapRefData() | |
764 | { | |
765 | Free() ; | |
766 | } | |
767 | ||
732d8c74 FM |
768 | |
769 | ||
770 | // ---------------------------------------------------------------------------- | |
771 | // wxBitmap | |
772 | // ---------------------------------------------------------------------------- | |
773 | ||
489468fe SC |
774 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
775 | { | |
776 | bool created = false ; | |
777 | int w = icon.GetWidth() ; | |
778 | int h = icon.GetHeight() ; | |
779 | ||
524c47aa SC |
780 | Create( w , h ) ; |
781 | #ifdef __WXOSX_CARBON__ | |
489468fe SC |
782 | if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) ) |
783 | { | |
784 | IconFamilyHandle iconFamily = NULL ; | |
785 | Handle imagehandle = NewHandle( 0 ) ; | |
786 | Handle maskhandle = NewHandle( 0 ) ; | |
787 | ||
788 | OSType maskType = 0; | |
789 | OSType dataType = 0; | |
790 | IconSelectorValue selector = 0 ; | |
791 | ||
792 | switch (w) | |
793 | { | |
794 | case 128: | |
795 | dataType = kThumbnail32BitData ; | |
796 | maskType = kThumbnail8BitMask ; | |
797 | selector = kSelectorAllAvailableData ; | |
798 | break; | |
799 | ||
800 | case 48: | |
801 | dataType = kHuge32BitData ; | |
802 | maskType = kHuge8BitMask ; | |
803 | selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ; | |
804 | break; | |
805 | ||
806 | case 32: | |
807 | dataType = kLarge32BitData ; | |
808 | maskType = kLarge8BitMask ; | |
809 | selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ; | |
810 | break; | |
811 | ||
812 | case 16: | |
813 | dataType = kSmall32BitData ; | |
814 | maskType = kSmall8BitMask ; | |
815 | selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ; | |
816 | break; | |
817 | ||
818 | default: | |
819 | break; | |
820 | } | |
821 | ||
822 | OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ; | |
823 | ||
824 | err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ; | |
825 | err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ; | |
826 | size_t imagehandlesize = GetHandleSize( imagehandle ) ; | |
827 | size_t maskhandlesize = GetHandleSize( maskhandle ) ; | |
828 | ||
829 | if ( imagehandlesize != 0 && maskhandlesize != 0 ) | |
830 | { | |
831 | wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ; | |
832 | wxASSERT( GetHandleSize( maskhandle ) == w * h ) ; | |
833 | ||
834 | UseAlpha() ; | |
835 | ||
836 | unsigned char *source = (unsigned char *) *imagehandle ; | |
837 | unsigned char *sourcemask = (unsigned char *) *maskhandle ; | |
838 | unsigned char* destination = (unsigned char*) BeginRawAccess() ; | |
839 | ||
840 | for ( int y = 0 ; y < h ; ++y ) | |
841 | { | |
842 | for ( int x = 0 ; x < w ; ++x ) | |
843 | { | |
844 | unsigned char a = *sourcemask++; | |
845 | *destination++ = a; | |
846 | source++ ; | |
292e5e1f | 847 | #if wxOSX_USE_PREMULTIPLIED_ALPHA |
489468fe SC |
848 | *destination++ = ( (*source++) * a + 127 ) / 255; |
849 | *destination++ = ( (*source++) * a + 127 ) / 255; | |
850 | *destination++ = ( (*source++) * a + 127 ) / 255; | |
851 | #else | |
852 | *destination++ = *source++ ; | |
853 | *destination++ = *source++ ; | |
854 | *destination++ = *source++ ; | |
855 | #endif | |
856 | } | |
857 | } | |
858 | ||
859 | EndRawAccess() ; | |
860 | DisposeHandle( imagehandle ) ; | |
861 | DisposeHandle( maskhandle ) ; | |
862 | created = true ; | |
863 | } | |
864 | ||
865 | DisposeHandle( (Handle) iconFamily ) ; | |
866 | } | |
867 | #endif | |
868 | if ( !created ) | |
869 | { | |
870 | wxMemoryDC dc ; | |
871 | dc.SelectObject( *this ) ; | |
872 | dc.DrawIcon( icon , 0 , 0 ) ; | |
873 | dc.SelectObject( wxNullBitmap ) ; | |
874 | } | |
875 | ||
876 | return true; | |
877 | } | |
878 | ||
489468fe SC |
879 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) |
880 | { | |
881 | wxBitmapRefData* bitmapRefData; | |
882 | ||
883 | m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ; | |
884 | ||
885 | if (bitmapRefData->IsOk()) | |
886 | { | |
887 | if ( no_bits == 1 ) | |
888 | { | |
889 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; | |
890 | if ( the_width % (sizeof(unsigned char) * 8) ) | |
891 | linesize += sizeof(unsigned char); | |
892 | ||
893 | unsigned char* linestart = (unsigned char*) bits ; | |
894 | unsigned char* destptr = (unsigned char*) BeginRawAccess() ; | |
895 | ||
896 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) | |
897 | { | |
898 | unsigned char* destination = destptr; | |
899 | int index, bit, mask; | |
900 | ||
901 | for ( int x = 0 ; x < the_width ; ++x ) | |
902 | { | |
903 | index = x / 8 ; | |
904 | bit = x % 8 ; | |
905 | mask = 1 << bit ; | |
906 | ||
907 | if ( linestart[index] & mask ) | |
908 | { | |
909 | *destination++ = 0xFF ; | |
910 | *destination++ = 0 ; | |
911 | *destination++ = 0 ; | |
912 | *destination++ = 0 ; | |
913 | } | |
914 | else | |
915 | { | |
916 | *destination++ = 0xFF ; | |
917 | *destination++ = 0xFF ; | |
918 | *destination++ = 0xFF ; | |
919 | *destination++ = 0xFF ; | |
920 | } | |
921 | } | |
922 | } | |
923 | ||
924 | EndRawAccess() ; | |
925 | } | |
926 | else | |
927 | { | |
928 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); | |
929 | } | |
930 | } /* bitmapRefData->IsOk() */ | |
931 | } | |
932 | ||
489468fe SC |
933 | wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth) |
934 | { | |
935 | (void) Create(data, type, width, height, depth); | |
936 | } | |
937 | ||
938 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) | |
939 | { | |
940 | LoadFile(filename, type); | |
941 | } | |
942 | ||
bf7470fe SC |
943 | wxBitmap::wxBitmap(CGImageRef image) |
944 | { | |
945 | (void) Create(image); | |
946 | } | |
947 | ||
489468fe SC |
948 | wxGDIRefData* wxBitmap::CreateGDIRefData() const |
949 | { | |
950 | return new wxBitmapRefData; | |
951 | } | |
952 | ||
953 | wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const | |
954 | { | |
5c33522f | 955 | return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data)); |
489468fe SC |
956 | } |
957 | ||
958 | void * wxBitmap::GetRawAccess() const | |
959 | { | |
a1b806b9 | 960 | wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ; |
489468fe SC |
961 | |
962 | return M_BITMAPDATA->GetRawAccess() ; | |
963 | } | |
964 | ||
965 | void * wxBitmap::BeginRawAccess() | |
966 | { | |
a1b806b9 | 967 | wxCHECK_MSG( IsOk() , NULL , wxT("invalid bitmap") ) ; |
489468fe SC |
968 | |
969 | return M_BITMAPDATA->BeginRawAccess() ; | |
970 | } | |
971 | ||
972 | void wxBitmap::EndRawAccess() | |
973 | { | |
a1b806b9 | 974 | wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; |
489468fe SC |
975 | |
976 | M_BITMAPDATA->EndRawAccess() ; | |
977 | } | |
978 | ||
979 | CGImageRef wxBitmap::CreateCGImage() const | |
980 | { | |
a1b806b9 | 981 | wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; |
489468fe SC |
982 | |
983 | return M_BITMAPDATA->CreateCGImage() ; | |
984 | } | |
985 | ||
986 | #ifndef __WXOSX_IPHONE__ | |
987 | IconRef wxBitmap::GetIconRef() const | |
988 | { | |
a1b806b9 | 989 | wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; |
489468fe SC |
990 | |
991 | return M_BITMAPDATA->GetIconRef() ; | |
992 | } | |
993 | ||
994 | IconRef wxBitmap::CreateIconRef() const | |
995 | { | |
996 | IconRef icon = GetIconRef(); | |
997 | verify_noerr( AcquireIconRef(icon) ); | |
998 | return icon; | |
999 | } | |
1000 | #endif | |
1001 | ||
4bea628d | 1002 | #if wxOSX_USE_COCOA |
524c47aa SC |
1003 | |
1004 | WX_NSImage wxBitmap::GetNSImage() const | |
1005 | { | |
1006 | wxCFRef< CGImageRef > cgimage(CreateCGImage()); | |
59d866ad | 1007 | return wxOSXGetNSImageFromCGImage( cgimage ); |
524c47aa SC |
1008 | } |
1009 | ||
1010 | #endif | |
1011 | ||
4bea628d SC |
1012 | #if wxOSX_USE_IPHONE |
1013 | ||
1014 | WX_UIImage wxBitmap::GetUIImage() const | |
1015 | { | |
1016 | wxCFRef< CGImageRef > cgimage(CreateCGImage()); | |
59d866ad | 1017 | return wxOSXGetUIImageFromCGImage( cgimage ); |
4bea628d SC |
1018 | } |
1019 | ||
1020 | #endif | |
489468fe SC |
1021 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const |
1022 | { | |
a1b806b9 | 1023 | wxCHECK_MSG( IsOk() && |
489468fe SC |
1024 | (rect.x >= 0) && (rect.y >= 0) && |
1025 | (rect.x+rect.width <= GetWidth()) && | |
1026 | (rect.y+rect.height <= GetHeight()), | |
1027 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
1028 | ||
1029 | wxBitmap ret( rect.width, rect.height, GetDepth() ); | |
a1b806b9 | 1030 | wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") ); |
489468fe SC |
1031 | |
1032 | int destwidth = rect.width ; | |
1033 | int destheight = rect.height ; | |
1034 | ||
1035 | { | |
1036 | unsigned char *sourcedata = (unsigned char*) GetRawAccess() ; | |
1037 | unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ; | |
f5fac447 | 1038 | wxASSERT((sourcedata != NULL) && (destdata != NULL)); |
489468fe | 1039 | |
f5fac447 | 1040 | if ( (sourcedata != NULL) && (destdata != NULL) ) |
489468fe | 1041 | { |
f5fac447 SC |
1042 | int sourcelinesize = GetBitmapData()->GetBytesPerRow() ; |
1043 | int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ; | |
1044 | unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; | |
1045 | unsigned char *dest = destdata ; | |
1046 | ||
1047 | for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) | |
1048 | { | |
1049 | memcpy( dest , source , destlinesize ) ; | |
1050 | } | |
489468fe | 1051 | } |
f5fac447 | 1052 | ret.EndRawAccess() ; |
489468fe SC |
1053 | } |
1054 | ||
489468fe SC |
1055 | |
1056 | if ( M_BITMAPDATA->m_bitmapMask ) | |
1057 | { | |
1058 | wxMemoryBuffer maskbuf ; | |
1059 | int rowBytes = GetBestBytesPerRow( destwidth * kMaskBytesPerPixel ); | |
1060 | size_t maskbufsize = rowBytes * destheight ; | |
1061 | ||
1062 | int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ; | |
1063 | int destlinesize = rowBytes ; | |
1064 | ||
1065 | unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ; | |
1066 | unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ; | |
1067 | wxASSERT( (source != NULL) && (destdata != NULL) ) ; | |
1068 | ||
f5fac447 | 1069 | if ( (source != NULL) && (destdata != NULL) ) |
489468fe | 1070 | { |
f5fac447 SC |
1071 | source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ; |
1072 | unsigned char *dest = destdata ; | |
1073 | ||
1074 | for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) | |
1075 | { | |
1076 | memcpy( dest , source , destlinesize ) ; | |
1077 | } | |
489468fe | 1078 | |
f5fac447 SC |
1079 | maskbuf.UngetWriteBuf( maskbufsize ) ; |
1080 | } | |
489468fe SC |
1081 | ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ; |
1082 | } | |
1083 | else if ( HasAlpha() ) | |
1084 | ret.UseAlpha() ; | |
1085 | ||
1086 | return ret; | |
1087 | } | |
1088 | ||
1089 | bool wxBitmap::Create(int w, int h, int d) | |
1090 | { | |
1091 | UnRef(); | |
1092 | ||
1093 | if ( d < 0 ) | |
1094 | d = wxDisplayDepth() ; | |
1095 | ||
1096 | m_refData = new wxBitmapRefData( w , h , d ); | |
1097 | ||
1098 | return M_BITMAPDATA->IsOk() ; | |
1099 | } | |
1100 | ||
aaa7d63c SC |
1101 | |
1102 | bool wxBitmap::Create(CGImageRef image) | |
1103 | { | |
1104 | UnRef(); | |
1105 | ||
1106 | m_refData = new wxBitmapRefData( image ); | |
1107 | ||
1108 | return M_BITMAPDATA->IsOk() ; | |
1109 | } | |
1110 | ||
489468fe SC |
1111 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
1112 | { | |
1113 | UnRef(); | |
1114 | ||
1115 | wxBitmapHandler *handler = FindHandler(type); | |
1116 | ||
1117 | if ( handler ) | |
1118 | { | |
1119 | m_refData = new wxBitmapRefData; | |
1120 | ||
1121 | return handler->LoadFile(this, filename, type, -1, -1); | |
1122 | } | |
1123 | else | |
1124 | { | |
1125 | #if wxUSE_IMAGE | |
1126 | wxImage loadimage(filename, type); | |
a1b806b9 | 1127 | if (loadimage.IsOk()) |
489468fe SC |
1128 | { |
1129 | *this = loadimage; | |
1130 | ||
1131 | return true; | |
1132 | } | |
1133 | #endif | |
1134 | } | |
1135 | ||
1136 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
1137 | ||
1138 | return false; | |
1139 | } | |
1140 | ||
1141 | bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth) | |
1142 | { | |
1143 | UnRef(); | |
1144 | ||
1145 | m_refData = new wxBitmapRefData; | |
1146 | ||
1147 | wxBitmapHandler *handler = FindHandler(type); | |
1148 | ||
1149 | if ( handler == NULL ) | |
1150 | { | |
1151 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
1152 | ||
1153 | return false; | |
1154 | } | |
1155 | ||
1156 | return handler->Create(this, data, type, width, height, depth); | |
1157 | } | |
1158 | ||
1159 | #if wxUSE_IMAGE | |
1160 | ||
1161 | wxBitmap::wxBitmap(const wxImage& image, int depth) | |
1162 | { | |
a1b806b9 | 1163 | wxCHECK_RET( image.IsOk(), wxT("invalid image") ); |
489468fe SC |
1164 | |
1165 | // width and height of the device-dependent bitmap | |
1166 | int width = image.GetWidth(); | |
1167 | int height = image.GetHeight(); | |
1168 | ||
1169 | wxBitmapRefData* bitmapRefData; | |
03647350 | 1170 | |
489468fe SC |
1171 | m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ; |
1172 | ||
1173 | if ( bitmapRefData->IsOk()) | |
1174 | { | |
1175 | // Create picture | |
1176 | ||
1177 | bool hasAlpha = false ; | |
1178 | ||
1179 | if ( image.HasMask() ) | |
1180 | { | |
1181 | // takes precedence, don't mix with alpha info | |
1182 | } | |
1183 | else | |
1184 | { | |
1185 | hasAlpha = image.HasAlpha() ; | |
1186 | } | |
1187 | ||
1188 | if ( hasAlpha ) | |
1189 | UseAlpha() ; | |
1190 | ||
1191 | unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; | |
1192 | register unsigned char* data = image.GetData(); | |
1193 | if ( destinationstart != NULL && data != NULL ) | |
1194 | { | |
1195 | const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; | |
1196 | for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) | |
1197 | { | |
1198 | unsigned char * destination = destinationstart; | |
1199 | for (int x = 0; x < width; x++) | |
1200 | { | |
1201 | if ( hasAlpha ) | |
1202 | { | |
1203 | const unsigned char a = *alpha++; | |
1204 | *destination++ = a ; | |
1205 | ||
292e5e1f | 1206 | #if wxOSX_USE_PREMULTIPLIED_ALPHA |
489468fe SC |
1207 | *destination++ = ((*data++) * a + 127) / 255 ; |
1208 | *destination++ = ((*data++) * a + 127) / 255 ; | |
1209 | *destination++ = ((*data++) * a + 127) / 255 ; | |
1210 | #else | |
1211 | *destination++ = *data++ ; | |
1212 | *destination++ = *data++ ; | |
1213 | *destination++ = *data++ ; | |
1214 | #endif | |
1215 | } | |
1216 | else | |
1217 | { | |
1218 | *destination++ = 0xFF ; | |
1219 | *destination++ = *data++ ; | |
1220 | *destination++ = *data++ ; | |
1221 | *destination++ = *data++ ; | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | EndRawAccess() ; | |
1227 | } | |
1228 | if ( image.HasMask() ) | |
1229 | SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; | |
1230 | } /* bitmapRefData->IsOk() */ | |
1231 | } | |
1232 | ||
1233 | wxImage wxBitmap::ConvertToImage() const | |
1234 | { | |
1235 | wxImage image; | |
1236 | ||
a1b806b9 | 1237 | wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") ); |
489468fe SC |
1238 | |
1239 | // create an wxImage object | |
1240 | int width = GetWidth(); | |
1241 | int height = GetHeight(); | |
1242 | image.Create( width, height ); | |
1243 | ||
1244 | unsigned char *data = image.GetData(); | |
1245 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); | |
1246 | ||
1247 | unsigned char* sourcestart = (unsigned char*) GetRawAccess() ; | |
1248 | ||
1249 | bool hasAlpha = false ; | |
1250 | bool hasMask = false ; | |
1251 | int maskBytesPerRow = 0 ; | |
1252 | unsigned char *alpha = NULL ; | |
1253 | unsigned char *mask = NULL ; | |
1254 | ||
1255 | if ( HasAlpha() ) | |
1256 | hasAlpha = true ; | |
1257 | ||
1258 | if ( GetMask() ) | |
1259 | { | |
1260 | hasMask = true ; | |
1261 | mask = (unsigned char*) GetMask()->GetRawAccess() ; | |
1262 | maskBytesPerRow = GetMask()->GetBytesPerRow() ; | |
1263 | } | |
1264 | ||
1265 | if ( hasAlpha ) | |
1266 | { | |
1267 | image.SetAlpha() ; | |
1268 | alpha = image.GetAlpha() ; | |
1269 | } | |
1270 | ||
1271 | int index = 0; | |
1272 | ||
1273 | // The following masking algorithm is the same as well in msw/gtk: | |
1274 | // the colour used as transparent one in wxImage and the one it is | |
1275 | // replaced with when it actually occurs in the bitmap | |
1276 | static const int MASK_RED = 1; | |
1277 | static const int MASK_GREEN = 2; | |
1278 | static const int MASK_BLUE = 3; | |
1279 | static const int MASK_BLUE_REPLACEMENT = 2; | |
1280 | ||
1281 | for (int yy = 0; yy < height; yy++ , sourcestart += M_BITMAPDATA->GetBytesPerRow() , mask += maskBytesPerRow ) | |
1282 | { | |
1283 | unsigned char * maskp = mask ; | |
1284 | unsigned char * source = sourcestart; | |
1285 | unsigned char a, r, g, b; | |
1286 | long color; | |
1287 | ||
1288 | for (int xx = 0; xx < width; xx++) | |
1289 | { | |
1290 | color = *((long*) source) ; | |
1291 | #ifdef WORDS_BIGENDIAN | |
1292 | a = ((color&0xFF000000) >> 24) ; | |
1293 | r = ((color&0x00FF0000) >> 16) ; | |
1294 | g = ((color&0x0000FF00) >> 8) ; | |
1295 | b = (color&0x000000FF); | |
1296 | #else | |
1297 | b = ((color&0xFF000000) >> 24) ; | |
1298 | g = ((color&0x00FF0000) >> 16) ; | |
1299 | r = ((color&0x0000FF00) >> 8) ; | |
1300 | a = (color&0x000000FF); | |
1301 | #endif | |
1302 | if ( hasMask ) | |
1303 | { | |
1304 | if ( *maskp++ == 0xFF ) | |
1305 | { | |
1306 | r = MASK_RED ; | |
1307 | g = MASK_GREEN ; | |
1308 | b = MASK_BLUE ; | |
1309 | } | |
1310 | else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE ) | |
1311 | b = MASK_BLUE_REPLACEMENT ; | |
1312 | } | |
1313 | else if ( hasAlpha ) | |
1314 | { | |
1315 | *alpha++ = a ; | |
292e5e1f | 1316 | #if wxOSX_USE_PREMULTIPLIED_ALPHA |
489468fe SC |
1317 | // this must be non-premultiplied data |
1318 | if ( a != 0xFF && a!= 0 ) | |
1319 | { | |
1320 | r = r * 255 / a; | |
1321 | g = g * 255 / a; | |
1322 | b = b * 255 / a; | |
1323 | } | |
1324 | #endif | |
1325 | } | |
1326 | ||
1327 | data[index ] = r ; | |
1328 | data[index + 1] = g ; | |
1329 | data[index + 2] = b ; | |
1330 | ||
1331 | index += 3; | |
1332 | source += 4 ; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | if ( hasMask ) | |
1337 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); | |
1338 | ||
1339 | return image; | |
1340 | } | |
1341 | ||
1342 | #endif //wxUSE_IMAGE | |
1343 | ||
1344 | bool wxBitmap::SaveFile( const wxString& filename, | |
1345 | wxBitmapType type, const wxPalette *palette ) const | |
1346 | { | |
1347 | bool success = false; | |
1348 | wxBitmapHandler *handler = FindHandler(type); | |
1349 | ||
1350 | if ( handler ) | |
1351 | { | |
1352 | success = handler->SaveFile(this, filename, type, palette); | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | #if wxUSE_IMAGE | |
1357 | wxImage image = ConvertToImage(); | |
1358 | success = image.SaveFile(filename, type); | |
1359 | #else | |
1360 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
1361 | #endif | |
1362 | } | |
1363 | ||
1364 | return success; | |
1365 | } | |
1366 | ||
1367 | int wxBitmap::GetHeight() const | |
1368 | { | |
a1b806b9 | 1369 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
489468fe SC |
1370 | |
1371 | return M_BITMAPDATA->GetHeight(); | |
1372 | } | |
1373 | ||
1374 | int wxBitmap::GetWidth() const | |
1375 | { | |
a1b806b9 | 1376 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
489468fe SC |
1377 | |
1378 | return M_BITMAPDATA->GetWidth() ; | |
1379 | } | |
1380 | ||
1381 | int wxBitmap::GetDepth() const | |
1382 | { | |
a1b806b9 | 1383 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
489468fe SC |
1384 | |
1385 | return M_BITMAPDATA->GetDepth(); | |
1386 | } | |
1387 | ||
1388 | wxMask *wxBitmap::GetMask() const | |
1389 | { | |
a1b806b9 | 1390 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
489468fe SC |
1391 | |
1392 | return M_BITMAPDATA->m_bitmapMask; | |
1393 | } | |
1394 | ||
1395 | bool wxBitmap::HasAlpha() const | |
1396 | { | |
a1b806b9 | 1397 | wxCHECK_MSG( IsOk(), false , wxT("invalid bitmap") ); |
489468fe SC |
1398 | |
1399 | return M_BITMAPDATA->HasAlpha() ; | |
1400 | } | |
1401 | ||
1402 | void wxBitmap::SetWidth(int w) | |
1403 | { | |
1404 | AllocExclusive(); | |
1405 | M_BITMAPDATA->SetWidth(w); | |
1406 | } | |
1407 | ||
1408 | void wxBitmap::SetHeight(int h) | |
1409 | { | |
1410 | AllocExclusive(); | |
1411 | M_BITMAPDATA->SetHeight(h); | |
1412 | } | |
1413 | ||
1414 | void wxBitmap::SetDepth(int d) | |
1415 | { | |
1416 | AllocExclusive(); | |
1417 | M_BITMAPDATA->SetDepth(d); | |
1418 | } | |
1419 | ||
1420 | void wxBitmap::SetOk(bool isOk) | |
1421 | { | |
1422 | AllocExclusive(); | |
1423 | M_BITMAPDATA->SetOk(isOk); | |
1424 | } | |
1425 | ||
1426 | #if wxUSE_PALETTE | |
1427 | wxPalette *wxBitmap::GetPalette() const | |
1428 | { | |
a1b806b9 | 1429 | wxCHECK_MSG( IsOk(), NULL, wxT("Invalid bitmap GetPalette()") ); |
489468fe SC |
1430 | |
1431 | return &M_BITMAPDATA->m_bitmapPalette; | |
1432 | } | |
1433 | ||
1434 | void wxBitmap::SetPalette(const wxPalette& palette) | |
1435 | { | |
1436 | AllocExclusive(); | |
1437 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
1438 | } | |
1439 | #endif // wxUSE_PALETTE | |
1440 | ||
1441 | void wxBitmap::SetMask(wxMask *mask) | |
1442 | { | |
1443 | AllocExclusive(); | |
1444 | // Remove existing mask if there is one. | |
1445 | delete M_BITMAPDATA->m_bitmapMask; | |
1446 | ||
1447 | M_BITMAPDATA->m_bitmapMask = mask ; | |
1448 | } | |
1449 | ||
1450 | WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const | |
1451 | { | |
1452 | wxUnusedVar(mask); | |
1453 | ||
1454 | return WXHBITMAP(M_BITMAPDATA->GetBitmapContext()); | |
1455 | } | |
1456 | ||
1457 | // ---------------------------------------------------------------------------- | |
1458 | // wxMask | |
1459 | // ---------------------------------------------------------------------------- | |
1460 | ||
1461 | wxMask::wxMask() | |
1462 | { | |
1463 | Init() ; | |
1464 | } | |
1465 | ||
ea2807a4 | 1466 | wxMask::wxMask(const wxMask &tocopy) : wxObject() |
489468fe SC |
1467 | { |
1468 | Init(); | |
1469 | ||
1470 | m_bytesPerRow = tocopy.m_bytesPerRow; | |
1471 | m_width = tocopy.m_width; | |
1472 | m_height = tocopy.m_height; | |
1473 | ||
1474 | size_t size = m_bytesPerRow * m_height; | |
1475 | unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size ); | |
1476 | unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData(); | |
1477 | memcpy( dest, source, size ); | |
1478 | m_memBuf.UngetWriteBuf( size ) ; | |
1479 | RealizeNative() ; | |
1480 | } | |
1481 | ||
1482 | // Construct a mask from a bitmap and a colour indicating | |
1483 | // the transparent area | |
1484 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) | |
1485 | { | |
1486 | Init() ; | |
1487 | Create( bitmap, colour ); | |
1488 | } | |
1489 | ||
1490 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1491 | wxMask::wxMask( const wxBitmap& bitmap ) | |
1492 | { | |
1493 | Init() ; | |
1494 | Create( bitmap ); | |
1495 | } | |
1496 | ||
1497 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1498 | ||
1499 | wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow ) | |
1500 | { | |
1501 | Init() ; | |
1502 | Create( data, width , height , bytesPerRow ); | |
1503 | } | |
1504 | ||
1505 | wxMask::~wxMask() | |
1506 | { | |
1507 | if ( m_maskBitmap ) | |
1508 | { | |
1509 | CGContextRelease( (CGContextRef) m_maskBitmap ); | |
1510 | m_maskBitmap = NULL ; | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | void wxMask::Init() | |
1515 | { | |
1516 | m_width = m_height = m_bytesPerRow = 0 ; | |
1517 | m_maskBitmap = NULL ; | |
1518 | } | |
1519 | ||
1520 | void *wxMask::GetRawAccess() const | |
1521 | { | |
1522 | return m_memBuf.GetData() ; | |
1523 | } | |
1524 | ||
1525 | // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed | |
1526 | // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well | |
1527 | ||
1528 | void wxMask::RealizeNative() | |
1529 | { | |
1530 | if ( m_maskBitmap ) | |
1531 | { | |
1532 | CGContextRelease( (CGContextRef) m_maskBitmap ); | |
1533 | m_maskBitmap = NULL ; | |
1534 | } | |
1535 | ||
1536 | CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); | |
1537 | // from MouseTracking sample : | |
1538 | // Ironically, due to a bug in CGImageCreateWithMask, you cannot use | |
1539 | // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point! | |
1540 | ||
1541 | m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, | |
1542 | kCGImageAlphaNone ); | |
1543 | CGColorSpaceRelease( colorspace ); | |
1544 | wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ; | |
1545 | } | |
1546 | ||
1547 | // Create a mask from a mono bitmap (copies the bitmap). | |
1548 | ||
1549 | bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow) | |
1550 | { | |
1551 | m_memBuf = data ; | |
1552 | m_width = width ; | |
1553 | m_height = height ; | |
1554 | m_bytesPerRow = bytesPerRow ; | |
1555 | ||
1556 | wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ; | |
1557 | ||
1558 | RealizeNative() ; | |
1559 | ||
1560 | return true ; | |
1561 | } | |
1562 | ||
1563 | // Create a mask from a mono bitmap (copies the bitmap). | |
1564 | bool wxMask::Create(const wxBitmap& bitmap) | |
1565 | { | |
1566 | m_width = bitmap.GetWidth() ; | |
1567 | m_height = bitmap.GetHeight() ; | |
1568 | m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; | |
1569 | ||
1570 | size_t size = m_bytesPerRow * m_height ; | |
1571 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; | |
1572 | wxASSERT( destdatabase != NULL ) ; | |
974c115d SC |
1573 | |
1574 | if ( destdatabase ) | |
489468fe | 1575 | { |
974c115d SC |
1576 | memset( destdatabase , 0 , size ) ; |
1577 | unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; | |
489468fe | 1578 | |
974c115d | 1579 | for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) |
489468fe | 1580 | { |
974c115d SC |
1581 | unsigned char *destdata = destdatabase ; |
1582 | unsigned char r, g, b; | |
489468fe | 1583 | |
974c115d SC |
1584 | for ( int x = 0 ; x < m_width ; ++x ) |
1585 | { | |
1586 | srcdata++ ; | |
1587 | r = *srcdata++ ; | |
1588 | g = *srcdata++ ; | |
1589 | b = *srcdata++ ; | |
1590 | ||
1591 | if ( ( r + g + b ) > 0x10 ) | |
1592 | *destdata++ = 0xFF ; | |
1593 | else | |
1594 | *destdata++ = 0x00 ; | |
1595 | } | |
489468fe SC |
1596 | } |
1597 | } | |
1598 | ||
1599 | m_memBuf.UngetWriteBuf( size ) ; | |
1600 | RealizeNative() ; | |
1601 | ||
1602 | return true; | |
1603 | } | |
1604 | ||
1605 | // Create a mask from a bitmap and a colour indicating | |
1606 | // the transparent area | |
1607 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1608 | { | |
1609 | m_width = bitmap.GetWidth() ; | |
1610 | m_height = bitmap.GetHeight() ; | |
1611 | m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; | |
1612 | ||
1613 | size_t size = m_bytesPerRow * m_height ; | |
1614 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; | |
1615 | wxASSERT( destdatabase != NULL ) ; | |
f5fac447 | 1616 | if ( destdatabase != NULL) |
489468fe | 1617 | { |
f5fac447 SC |
1618 | memset( destdatabase , 0 , size ) ; |
1619 | unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ; | |
1620 | size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow(); | |
489468fe | 1621 | |
f5fac447 | 1622 | for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow) |
489468fe | 1623 | { |
f5fac447 SC |
1624 | unsigned char *srcdata = srcdatabase ; |
1625 | unsigned char *destdata = destdatabase ; | |
1626 | unsigned char r, g, b; | |
489468fe | 1627 | |
f5fac447 SC |
1628 | for ( int x = 0 ; x < m_width ; ++x ) |
1629 | { | |
1630 | srcdata++ ; | |
1631 | r = *srcdata++ ; | |
1632 | g = *srcdata++ ; | |
1633 | b = *srcdata++ ; | |
1634 | ||
1635 | if ( colour == wxColour( r , g , b ) ) | |
1636 | *destdata++ = 0xFF ; | |
1637 | else | |
1638 | *destdata++ = 0x00 ; | |
1639 | } | |
489468fe SC |
1640 | } |
1641 | } | |
489468fe SC |
1642 | m_memBuf.UngetWriteBuf( size ) ; |
1643 | RealizeNative() ; | |
1644 | ||
1645 | return true; | |
1646 | } | |
1647 | ||
1648 | WXHBITMAP wxMask::GetHBITMAP() const | |
1649 | { | |
1650 | return m_maskBitmap ; | |
1651 | } | |
1652 | ||
1653 | // ---------------------------------------------------------------------------- | |
1654 | // Standard Handlers | |
1655 | // ---------------------------------------------------------------------------- | |
1656 | ||
aaa7d63c SC |
1657 | class WXDLLEXPORT wxBundleResourceHandler: public wxBitmapHandler |
1658 | { | |
fb228105 | 1659 | DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler) |
aaa7d63c SC |
1660 | |
1661 | public: | |
1662 | inline wxBundleResourceHandler() | |
1663 | { | |
1664 | }; | |
1665 | ||
1666 | virtual bool LoadFile(wxBitmap *bitmap, | |
1667 | const wxString& name, | |
1668 | wxBitmapType type, | |
1669 | int desiredWidth, | |
1670 | int desiredHeight); | |
1671 | }; | |
1672 | ||
1673 | IMPLEMENT_ABSTRACT_CLASS(wxBundleResourceHandler, wxBitmapHandler); | |
1674 | ||
1675 | class WXDLLEXPORT wxPNGResourceHandler: public wxBundleResourceHandler | |
1676 | { | |
1677 | DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler) | |
1678 | ||
1679 | public: | |
1680 | inline wxPNGResourceHandler() | |
1681 | { | |
1682 | SetName(wxT("PNG resource")); | |
1683 | SetExtension("PNG"); | |
1684 | SetType(wxBITMAP_TYPE_PNG_RESOURCE); | |
1685 | }; | |
1686 | }; | |
1687 | ||
1688 | IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler, wxBundleResourceHandler) | |
1689 | ||
1690 | class WXDLLEXPORT wxJPEGResourceHandler: public wxBundleResourceHandler | |
1691 | { | |
1692 | DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler) | |
1693 | ||
1694 | public: | |
1695 | inline wxJPEGResourceHandler() | |
1696 | { | |
1697 | SetName(wxT("JPEG resource")); | |
1698 | SetExtension("JPEG"); | |
1699 | SetType(wxBITMAP_TYPE_JPEG_RESOURCE); | |
1700 | }; | |
1701 | }; | |
1702 | ||
1703 | IMPLEMENT_DYNAMIC_CLASS(wxJPEGResourceHandler, wxBundleResourceHandler) | |
1704 | ||
1705 | bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, | |
1706 | const wxString& name, | |
1707 | wxBitmapType WXUNUSED(type), | |
1708 | int WXUNUSED(desiredWidth), | |
1709 | int WXUNUSED(desiredHeight)) | |
1710 | { | |
1711 | wxString ext = GetExtension().Lower(); | |
1712 | wxCFStringRef resname(name); | |
1713 | wxCFStringRef restype(ext); | |
1714 | ||
1715 | wxCFRef<CFURLRef> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL)); | |
1716 | ||
1717 | if ( imageURL.get() != NULL ) | |
1718 | { | |
1719 | // Create the data provider object | |
1720 | wxCFRef<CGDataProviderRef> provider(CGDataProviderCreateWithURL (imageURL) ); | |
1721 | CGImageRef image = NULL; | |
1722 | ||
1723 | if ( ext == "jpeg" ) | |
1724 | image = CGImageCreateWithJPEGDataProvider (provider, NULL, true, | |
1725 | kCGRenderingIntentDefault); | |
1726 | else if ( ext == "png" ) | |
1727 | image = CGImageCreateWithPNGDataProvider (provider, NULL, true, | |
1728 | kCGRenderingIntentDefault); | |
1729 | if ( image != NULL ) | |
1730 | { | |
1731 | bitmap->Create(image); | |
1732 | CGImageRelease(image); | |
1733 | } | |
1734 | } | |
1735 | ||
1736 | return false ; | |
1737 | } | |
1738 | ||
489468fe SC |
1739 | void wxBitmap::InitStandardHandlers() |
1740 | { | |
aaa7d63c | 1741 | #if wxOSX_USE_COCOA_OR_CARBON |
489468fe | 1742 | AddHandler( new wxICONResourceHandler ) ; |
b2680ced | 1743 | #endif |
aaa7d63c SC |
1744 | AddHandler( new wxPNGResourceHandler ); |
1745 | AddHandler( new wxJPEGResourceHandler ); | |
489468fe SC |
1746 | } |
1747 | ||
1748 | // ---------------------------------------------------------------------------- | |
1749 | // raw bitmap access support | |
1750 | // ---------------------------------------------------------------------------- | |
1751 | ||
1752 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int WXUNUSED(bpp)) | |
1753 | { | |
a1b806b9 | 1754 | if ( !IsOk() ) |
489468fe SC |
1755 | // no bitmap, no data (raw or otherwise) |
1756 | return NULL; | |
1757 | ||
1758 | data.m_width = GetWidth() ; | |
1759 | data.m_height = GetHeight() ; | |
1760 | data.m_stride = GetBitmapData()->GetBytesPerRow() ; | |
1761 | ||
1762 | return BeginRawAccess() ; | |
1763 | } | |
1764 | ||
1765 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase)) | |
1766 | { | |
1767 | EndRawAccess() ; | |
1768 | } | |
1769 | ||
1770 | void wxBitmap::UseAlpha() | |
1771 | { | |
1772 | // remember that we are using alpha channel: | |
1773 | // we'll need to create a proper mask in UngetRawData() | |
1774 | M_BITMAPDATA->UseAlpha( true ); | |
1775 | } |