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