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