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