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