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