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