]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/bitmap.cpp
avoiding warning because of missing define
[wxWidgets.git] / src / osx / carbon / bitmap.cpp
CommitLineData
489468fe
SC
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
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
134#define wxMAC_USE_PREMULTIPLIED_ALPHA 1
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 {
481#if wxMAC_USE_PREMULTIPLIED_ALPHA
482 // this must be non-premultiplied data
483 if ( a != 0xFF && a!= 0 )
484 {
485 r = r * 255 / a;
486 g = g * 255 / a;
487 b = b * 255 / a;
488 }
489#endif
490 }
491 *dest++ = a ;
492 *dest++ = r ;
493 *dest++ = g ;
494 *dest++ = b ;
495
496 }
497 }
498 HUnlock( data );
499 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 {
679#if wxMAC_USE_PREMULTIPLIED_ALPHA
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
794 Create( icon.GetWidth() , icon.GetHeight() ) ;
795#ifndef __WXOSX_IPHONE__
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++ ;
861#if wxMAC_USE_PREMULTIPLIED_ALPHA
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
1024wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
1025{
1026 wxCHECK_MSG( Ok() &&
1027 (rect.x >= 0) && (rect.y >= 0) &&
1028 (rect.x+rect.width <= GetWidth()) &&
1029 (rect.y+rect.height <= GetHeight()),
1030 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1031
1032 wxBitmap ret( rect.width, rect.height, GetDepth() );
1033 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1034
1035 int destwidth = rect.width ;
1036 int destheight = rect.height ;
1037
1038 {
1039 unsigned char *sourcedata = (unsigned char*) GetRawAccess() ;
1040 unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ;
1041 wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ;
1042
1043 int sourcelinesize = GetBitmapData()->GetBytesPerRow() ;
1044 int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ;
1045 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
1046 unsigned char *dest = destdata ;
1047
1048 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
1049 {
1050 memcpy( dest , source , destlinesize ) ;
1051 }
1052 }
1053
1054 ret.EndRawAccess() ;
1055
1056 if ( M_BITMAPDATA->m_bitmapMask )
1057 {
1058 wxMemoryBuffer maskbuf ;
1059 int rowBytes = GetBestBytesPerRow( destwidth * kMaskBytesPerPixel );
1060 size_t maskbufsize = rowBytes * destheight ;
1061
1062 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
1063 int destlinesize = rowBytes ;
1064
1065 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
1066 unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
1067 wxASSERT( (source != NULL) && (destdata != NULL) ) ;
1068
1069 source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ;
1070 unsigned char *dest = destdata ;
1071
1072 for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
1073 {
1074 memcpy( dest , source , destlinesize ) ;
1075 }
1076
1077 maskbuf.UngetWriteBuf( maskbufsize ) ;
1078 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
1079 }
1080 else if ( HasAlpha() )
1081 ret.UseAlpha() ;
1082
1083 return ret;
1084}
1085
1086bool wxBitmap::Create(int w, int h, int d)
1087{
1088 UnRef();
1089
1090 if ( d < 0 )
1091 d = wxDisplayDepth() ;
1092
1093 m_refData = new wxBitmapRefData( w , h , d );
1094
1095 return M_BITMAPDATA->IsOk() ;
1096}
1097
1098bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
1099{
1100 UnRef();
1101
1102 wxBitmapHandler *handler = FindHandler(type);
1103
1104 if ( handler )
1105 {
1106 m_refData = new wxBitmapRefData;
1107
1108 return handler->LoadFile(this, filename, type, -1, -1);
1109 }
1110 else
1111 {
1112#if wxUSE_IMAGE
1113 wxImage loadimage(filename, type);
1114 if (loadimage.Ok())
1115 {
1116 *this = loadimage;
1117
1118 return true;
1119 }
1120#endif
1121 }
1122
1123 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1124
1125 return false;
1126}
1127
1128bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
1129{
1130 UnRef();
1131
1132 m_refData = new wxBitmapRefData;
1133
1134 wxBitmapHandler *handler = FindHandler(type);
1135
1136 if ( handler == NULL )
1137 {
1138 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1139
1140 return false;
1141 }
1142
1143 return handler->Create(this, data, type, width, height, depth);
1144}
1145
1146#if wxUSE_IMAGE
1147
1148wxBitmap::wxBitmap(const wxImage& image, int depth)
1149{
1150 wxCHECK_RET( image.Ok(), wxT("invalid image") );
1151
1152 // width and height of the device-dependent bitmap
1153 int width = image.GetWidth();
1154 int height = image.GetHeight();
1155
1156 wxBitmapRefData* bitmapRefData;
1157
1158 m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ;
1159
1160 if ( bitmapRefData->IsOk())
1161 {
1162 // Create picture
1163
1164 bool hasAlpha = false ;
1165
1166 if ( image.HasMask() )
1167 {
1168 // takes precedence, don't mix with alpha info
1169 }
1170 else
1171 {
1172 hasAlpha = image.HasAlpha() ;
1173 }
1174
1175 if ( hasAlpha )
1176 UseAlpha() ;
1177
1178 unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ;
1179 register unsigned char* data = image.GetData();
1180 if ( destinationstart != NULL && data != NULL )
1181 {
1182 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
1183 for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++)
1184 {
1185 unsigned char * destination = destinationstart;
1186 for (int x = 0; x < width; x++)
1187 {
1188 if ( hasAlpha )
1189 {
1190 const unsigned char a = *alpha++;
1191 *destination++ = a ;
1192
1193 #if wxMAC_USE_PREMULTIPLIED_ALPHA
1194 *destination++ = ((*data++) * a + 127) / 255 ;
1195 *destination++ = ((*data++) * a + 127) / 255 ;
1196 *destination++ = ((*data++) * a + 127) / 255 ;
1197 #else
1198 *destination++ = *data++ ;
1199 *destination++ = *data++ ;
1200 *destination++ = *data++ ;
1201 #endif
1202 }
1203 else
1204 {
1205 *destination++ = 0xFF ;
1206 *destination++ = *data++ ;
1207 *destination++ = *data++ ;
1208 *destination++ = *data++ ;
1209 }
1210 }
1211 }
1212
1213 EndRawAccess() ;
1214 }
1215 if ( image.HasMask() )
1216 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
1217 } /* bitmapRefData->IsOk() */
1218}
1219
1220wxImage wxBitmap::ConvertToImage() const
1221{
1222 wxImage image;
1223
1224 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1225
1226 // create an wxImage object
1227 int width = GetWidth();
1228 int height = GetHeight();
1229 image.Create( width, height );
1230
1231 unsigned char *data = image.GetData();
1232 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1233
1234 unsigned char* sourcestart = (unsigned char*) GetRawAccess() ;
1235
1236 bool hasAlpha = false ;
1237 bool hasMask = false ;
1238 int maskBytesPerRow = 0 ;
1239 unsigned char *alpha = NULL ;
1240 unsigned char *mask = NULL ;
1241
1242 if ( HasAlpha() )
1243 hasAlpha = true ;
1244
1245 if ( GetMask() )
1246 {
1247 hasMask = true ;
1248 mask = (unsigned char*) GetMask()->GetRawAccess() ;
1249 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
1250 }
1251
1252 if ( hasAlpha )
1253 {
1254 image.SetAlpha() ;
1255 alpha = image.GetAlpha() ;
1256 }
1257
1258 int index = 0;
1259
1260 // The following masking algorithm is the same as well in msw/gtk:
1261 // the colour used as transparent one in wxImage and the one it is
1262 // replaced with when it actually occurs in the bitmap
1263 static const int MASK_RED = 1;
1264 static const int MASK_GREEN = 2;
1265 static const int MASK_BLUE = 3;
1266 static const int MASK_BLUE_REPLACEMENT = 2;
1267
1268 for (int yy = 0; yy < height; yy++ , sourcestart += M_BITMAPDATA->GetBytesPerRow() , mask += maskBytesPerRow )
1269 {
1270 unsigned char * maskp = mask ;
1271 unsigned char * source = sourcestart;
1272 unsigned char a, r, g, b;
1273 long color;
1274
1275 for (int xx = 0; xx < width; xx++)
1276 {
1277 color = *((long*) source) ;
1278#ifdef WORDS_BIGENDIAN
1279 a = ((color&0xFF000000) >> 24) ;
1280 r = ((color&0x00FF0000) >> 16) ;
1281 g = ((color&0x0000FF00) >> 8) ;
1282 b = (color&0x000000FF);
1283#else
1284 b = ((color&0xFF000000) >> 24) ;
1285 g = ((color&0x00FF0000) >> 16) ;
1286 r = ((color&0x0000FF00) >> 8) ;
1287 a = (color&0x000000FF);
1288#endif
1289 if ( hasMask )
1290 {
1291 if ( *maskp++ == 0xFF )
1292 {
1293 r = MASK_RED ;
1294 g = MASK_GREEN ;
1295 b = MASK_BLUE ;
1296 }
1297 else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1298 b = MASK_BLUE_REPLACEMENT ;
1299 }
1300 else if ( hasAlpha )
1301 {
1302 *alpha++ = a ;
1303#if wxMAC_USE_PREMULTIPLIED_ALPHA
1304 // this must be non-premultiplied data
1305 if ( a != 0xFF && a!= 0 )
1306 {
1307 r = r * 255 / a;
1308 g = g * 255 / a;
1309 b = b * 255 / a;
1310 }
1311#endif
1312 }
1313
1314 data[index ] = r ;
1315 data[index + 1] = g ;
1316 data[index + 2] = b ;
1317
1318 index += 3;
1319 source += 4 ;
1320 }
1321 }
1322
1323 if ( hasMask )
1324 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1325
1326 return image;
1327}
1328
1329#endif //wxUSE_IMAGE
1330
1331bool wxBitmap::SaveFile( const wxString& filename,
1332 wxBitmapType type, const wxPalette *palette ) const
1333{
1334 bool success = false;
1335 wxBitmapHandler *handler = FindHandler(type);
1336
1337 if ( handler )
1338 {
1339 success = handler->SaveFile(this, filename, type, palette);
1340 }
1341 else
1342 {
1343#if wxUSE_IMAGE
1344 wxImage image = ConvertToImage();
1345 success = image.SaveFile(filename, type);
1346#else
1347 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
1348#endif
1349 }
1350
1351 return success;
1352}
1353
1354int wxBitmap::GetHeight() const
1355{
1356 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1357
1358 return M_BITMAPDATA->GetHeight();
1359}
1360
1361int wxBitmap::GetWidth() const
1362{
1363 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1364
1365 return M_BITMAPDATA->GetWidth() ;
1366}
1367
1368int wxBitmap::GetDepth() const
1369{
1370 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1371
1372 return M_BITMAPDATA->GetDepth();
1373}
1374
1375wxMask *wxBitmap::GetMask() const
1376{
1377 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1378
1379 return M_BITMAPDATA->m_bitmapMask;
1380}
1381
1382bool wxBitmap::HasAlpha() const
1383{
1384 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1385
1386 return M_BITMAPDATA->HasAlpha() ;
1387}
1388
1389void wxBitmap::SetWidth(int w)
1390{
1391 AllocExclusive();
1392 M_BITMAPDATA->SetWidth(w);
1393}
1394
1395void wxBitmap::SetHeight(int h)
1396{
1397 AllocExclusive();
1398 M_BITMAPDATA->SetHeight(h);
1399}
1400
1401void wxBitmap::SetDepth(int d)
1402{
1403 AllocExclusive();
1404 M_BITMAPDATA->SetDepth(d);
1405}
1406
1407void wxBitmap::SetOk(bool isOk)
1408{
1409 AllocExclusive();
1410 M_BITMAPDATA->SetOk(isOk);
1411}
1412
1413#if wxUSE_PALETTE
1414wxPalette *wxBitmap::GetPalette() const
1415{
1416 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1417
1418 return &M_BITMAPDATA->m_bitmapPalette;
1419}
1420
1421void wxBitmap::SetPalette(const wxPalette& palette)
1422{
1423 AllocExclusive();
1424 M_BITMAPDATA->m_bitmapPalette = palette ;
1425}
1426#endif // wxUSE_PALETTE
1427
1428void wxBitmap::SetMask(wxMask *mask)
1429{
1430 AllocExclusive();
1431 // Remove existing mask if there is one.
1432 delete M_BITMAPDATA->m_bitmapMask;
1433
1434 M_BITMAPDATA->m_bitmapMask = mask ;
1435}
1436
1437WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1438{
1439 wxUnusedVar(mask);
1440
1441 return WXHBITMAP(M_BITMAPDATA->GetBitmapContext());
1442}
1443
1444// ----------------------------------------------------------------------------
1445// wxMask
1446// ----------------------------------------------------------------------------
1447
1448wxMask::wxMask()
1449{
1450 Init() ;
1451}
1452
1453wxMask::wxMask(const wxMask &tocopy)
1454{
1455 Init();
1456
1457 m_bytesPerRow = tocopy.m_bytesPerRow;
1458 m_width = tocopy.m_width;
1459 m_height = tocopy.m_height;
1460
1461 size_t size = m_bytesPerRow * m_height;
1462 unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size );
1463 unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData();
1464 memcpy( dest, source, size );
1465 m_memBuf.UngetWriteBuf( size ) ;
1466 RealizeNative() ;
1467}
1468
1469// Construct a mask from a bitmap and a colour indicating
1470// the transparent area
1471wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
1472{
1473 Init() ;
1474 Create( bitmap, colour );
1475}
1476
1477// Construct a mask from a mono bitmap (copies the bitmap).
1478wxMask::wxMask( const wxBitmap& bitmap )
1479{
1480 Init() ;
1481 Create( bitmap );
1482}
1483
1484// Construct a mask from a mono bitmap (copies the bitmap).
1485
1486wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1487{
1488 Init() ;
1489 Create( data, width , height , bytesPerRow );
1490}
1491
1492wxMask::~wxMask()
1493{
1494 if ( m_maskBitmap )
1495 {
1496 CGContextRelease( (CGContextRef) m_maskBitmap );
1497 m_maskBitmap = NULL ;
1498 }
1499}
1500
1501void wxMask::Init()
1502{
1503 m_width = m_height = m_bytesPerRow = 0 ;
1504 m_maskBitmap = NULL ;
1505}
1506
1507void *wxMask::GetRawAccess() const
1508{
1509 return m_memBuf.GetData() ;
1510}
1511
1512// The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed
1513// bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well
1514
1515void wxMask::RealizeNative()
1516{
1517 if ( m_maskBitmap )
1518 {
1519 CGContextRelease( (CGContextRef) m_maskBitmap );
1520 m_maskBitmap = NULL ;
1521 }
1522
1523 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
1524 // from MouseTracking sample :
1525 // Ironically, due to a bug in CGImageCreateWithMask, you cannot use
1526 // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point!
1527
1528 m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace,
1529 kCGImageAlphaNone );
1530 CGColorSpaceRelease( colorspace );
1531 wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ;
1532}
1533
1534// Create a mask from a mono bitmap (copies the bitmap).
1535
1536bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1537{
1538 m_memBuf = data ;
1539 m_width = width ;
1540 m_height = height ;
1541 m_bytesPerRow = bytesPerRow ;
1542
1543 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1544
1545 RealizeNative() ;
1546
1547 return true ;
1548}
1549
1550// Create a mask from a mono bitmap (copies the bitmap).
1551bool wxMask::Create(const wxBitmap& bitmap)
1552{
1553 m_width = bitmap.GetWidth() ;
1554 m_height = bitmap.GetHeight() ;
1555 m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ;
1556
1557 size_t size = m_bytesPerRow * m_height ;
1558 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1559 wxASSERT( destdatabase != NULL ) ;
1560
1561 memset( destdatabase , 0 , size ) ;
1562 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1563
1564 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1565 {
1566 unsigned char *destdata = destdatabase ;
1567 unsigned char r, g, b;
1568
1569 for ( int x = 0 ; x < m_width ; ++x )
1570 {
1571 srcdata++ ;
1572 r = *srcdata++ ;
1573 g = *srcdata++ ;
1574 b = *srcdata++ ;
1575
1576 if ( ( r + g + b ) > 0x10 )
1577 *destdata++ = 0xFF ;
1578 else
1579 *destdata++ = 0x00 ;
1580 }
1581 }
1582
1583 m_memBuf.UngetWriteBuf( size ) ;
1584 RealizeNative() ;
1585
1586 return true;
1587}
1588
1589// Create a mask from a bitmap and a colour indicating
1590// the transparent area
1591bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1592{
1593 m_width = bitmap.GetWidth() ;
1594 m_height = bitmap.GetHeight() ;
1595 m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ;
1596
1597 size_t size = m_bytesPerRow * m_height ;
1598 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1599 wxASSERT( destdatabase != NULL ) ;
1600
1601 memset( destdatabase , 0 , size ) ;
1602 unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ;
1603 size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow();
1604
1605 for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow)
1606 {
1607 unsigned char *srcdata = srcdatabase ;
1608 unsigned char *destdata = destdatabase ;
1609 unsigned char r, g, b;
1610
1611 for ( int x = 0 ; x < m_width ; ++x )
1612 {
1613 srcdata++ ;
1614 r = *srcdata++ ;
1615 g = *srcdata++ ;
1616 b = *srcdata++ ;
1617
1618 if ( colour == wxColour( r , g , b ) )
1619 *destdata++ = 0xFF ;
1620 else
1621 *destdata++ = 0x00 ;
1622 }
1623 }
1624
1625 m_memBuf.UngetWriteBuf( size ) ;
1626 RealizeNative() ;
1627
1628 return true;
1629}
1630
1631WXHBITMAP wxMask::GetHBITMAP() const
1632{
1633 return m_maskBitmap ;
1634}
1635
1636// ----------------------------------------------------------------------------
1637// Standard Handlers
1638// ----------------------------------------------------------------------------
1639
1640#if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__)
1641
1642class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1643{
1644 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1645
1646public:
1647 inline wxPICTResourceHandler()
1648 {
1649 SetName(wxT("Macintosh Pict resource"));
1650 SetExtension(wxEmptyString);
1651 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
1652 };
1653
1654 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1655 int desiredWidth, int desiredHeight);
1656};
1657
1658IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1659
1660
1661bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap,
1662 const wxString& name,
1663 long WXUNUSED(flags),
1664 int WXUNUSED(desiredWidth),
1665 int WXUNUSED(desiredHeight))
1666{
1667#if wxUSE_METAFILE
1668 Str255 theName ;
1669 wxMacStringToPascal( name , theName ) ;
1670
1671 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1672 if ( thePict )
1673 {
1674 wxMetafile mf ;
1675
1676 mf.SetPICT( thePict ) ;
1677 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1678 wxMemoryDC dc ;
1679 dc.SelectObject( *bitmap ) ;
1680 mf.Play( &dc ) ;
1681 dc.SelectObject( wxNullBitmap ) ;
1682
1683 return true ;
1684 }
1685#endif
1686
1687 return false ;
1688}
1689#endif
1690
1691void wxBitmap::InitStandardHandlers()
1692{
1693#if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__)
1694 AddHandler( new wxPICTResourceHandler ) ;
1695#endif
b2680ced 1696#if wxOSX_USE_COCOA_OR_CARBON
489468fe 1697 AddHandler( new wxICONResourceHandler ) ;
b2680ced 1698#endif
489468fe
SC
1699}
1700
1701// ----------------------------------------------------------------------------
1702// raw bitmap access support
1703// ----------------------------------------------------------------------------
1704
1705void *wxBitmap::GetRawData(wxPixelDataBase& data, int WXUNUSED(bpp))
1706{
1707 if ( !Ok() )
1708 // no bitmap, no data (raw or otherwise)
1709 return NULL;
1710
1711 data.m_width = GetWidth() ;
1712 data.m_height = GetHeight() ;
1713 data.m_stride = GetBitmapData()->GetBytesPerRow() ;
1714
1715 return BeginRawAccess() ;
1716}
1717
1718void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase))
1719{
1720 EndRawAccess() ;
1721}
1722
1723void wxBitmap::UseAlpha()
1724{
1725 // remember that we are using alpha channel:
1726 // we'll need to create a proper mask in UngetRawData()
1727 M_BITMAPDATA->UseAlpha( true );
1728}