]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bitmap.cpp
Use empty check consistent with other parts.
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose: wxBitmap
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "bitmap.h"
14#endif
15
a8e9860d 16#include "wx/wxprec.h"
b698c8e9 17
e9576ca5
SC
18#include "wx/bitmap.h"
19#include "wx/icon.h"
20#include "wx/log.h"
fec19ea9 21#include "wx/image.h"
20b69855 22#include "wx/metafile.h"
973b0afb 23#include "wx/xpmdecod.h"
e9576ca5 24
55e18dbe
VZ
25#include "wx/rawbmp.h"
26
e9576ca5
SC
27IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
28IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
55e18dbe 29IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )
e9576ca5 30
f5c6eb5c 31#ifdef __DARWIN__
5fde6fcc 32 #include <ApplicationServices/ApplicationServices.h>
03e11df5
GD
33#else
34 #include <PictUtils.h>
35#endif
519cb848 36
31d30995
SC
37#include "wx/mac/uma.h"
38
05d8deda
RN
39#include "wx/dcmemory.h"
40
20b69855
SC
41// Implementation Notes
42// --------------------
43//
44// we are always working with a 32 bit deep pixel buffer
45// under QuickDraw its alpha parts are going to be ignored in the GWorld,
46// therefore we have a separate GWorld there for blitting the mask in
47
48// under Quartz then content is transformed into a CGImageRef representing the same data
49// which can be transferred to the GPU by the OS for fast rendering
50
51// we don't dare premultiplied alpha yet
52#define wxMAC_USE_PREMULTIPLIED_ALPHA 0
53
d45318b8
RN
54#if wxUSE_BMPBUTTON
55
20b69855 56void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
519cb848 57{
20b69855
SC
58 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
59 if ( bitmap.Ok() )
60 {
45cf8535 61 wxBitmapRefData * bmap = bitmap.GetBitmapData() ;
20b69855
SC
62 if ( bmap == NULL )
63 return ;
b28aeea5 64
45cf8535 65 if ( ( bmap->HasNativeSize() && forceType == 0 ) || forceType == kControlContentIconRef )
b28aeea5 66 {
45cf8535
SC
67 wxBitmap scaleBmp ;
68
69 wxBitmapRefData* bmp = bmap ;
70
71 if ( !bmap->HasNativeSize() )
72 {
73 // as PICT conversion will only result in a 16x16 icon, let's attempt
74 // a few scales for better results
75
76 int w = bitmap.GetWidth() ;
77 int h = bitmap.GetHeight() ;
78 int sz = wxMax( w , h ) ;
79 if ( sz == 24 || sz == 64)
80 {
81 scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ;
82 bmp = scaleBmp.GetBitmapData() ;
83 }
84 }
85
b28aeea5 86 info->contentType = kControlContentIconRef ;
45cf8535
SC
87 info->u.iconRef = bmp->GetIconRef() ;
88 AcquireIconRef( info->u.iconRef ) ;
b28aeea5 89 }
86e94923 90#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
45cf8535
SC
91 else if ( forceType == kControlContentCGImageRef )
92 {
93 info->contentType = kControlContentCGImageRef ;
94 info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
95 }
96#endif
b28aeea5
SC
97 else
98 {
99 info->contentType = kControlContentPictHandle ;
100 info->u.picture = bmap->GetPictHandle() ;
101 }
20b69855 102 }
519cb848
SC
103}
104
20b69855 105void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
519cb848 106{
20b69855 107 if ( info->contentType == kControlContentIconRef )
4e9ed364 108 {
45cf8535 109 ReleaseIconRef( info->u.iconRef ) ;
b28aeea5
SC
110 }
111 else if ( info->contentType == kControlContentPictHandle )
112 {
45cf8535 113 // owned by the bitmap, no release here
4e9ed364 114 }
df21986c 115#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
20b69855
SC
116 else if ( info->contentType == kControlContentCGImageRef )
117 {
118 CGImageRelease( info->u.imageRef ) ;
119 }
120#endif
121 else
4e9ed364 122 {
20b69855 123 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
4e9ed364 124 }
03e11df5 125}
519cb848 126
d45318b8
RN
127#endif //wxUSE_BMPBUTTON
128
9cc62fc8 129#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
20b69855
SC
130
131void wxBitmapRefData::Init()
519cb848 132{
20b69855
SC
133 m_width = 0 ;
134 m_height = 0 ;
135 m_depth = 0 ;
136 m_ok = false ;
137 m_bitmapMask = NULL ;
138#if wxMAC_USE_CORE_GRAPHICS
139 m_cgImageRef = NULL ;
71cc158e 140#endif
b28aeea5
SC
141 m_iconRef = NULL ;
142 m_pictHandle = NULL ;
20b69855
SC
143 m_hBitmap = NULL ;
144 m_hMaskBitmap = NULL;
145 m_maskBytesPerRow = NULL ;
71cc158e 146
20b69855
SC
147 m_rawAccessCount = 0 ;
148 m_hasAlpha = false;
519cb848
SC
149}
150
20b69855 151wxBitmapRefData::wxBitmapRefData()
5fde6fcc 152{
20b69855 153 Init() ;
72055702
SC
154}
155
20b69855 156wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
72055702 157{
20b69855
SC
158 Init() ;
159 Create( w , h , d ) ;
160}
55e18dbe 161
20b69855
SC
162bool wxBitmapRefData::Create( int w , int h , int d )
163{
164 m_width = w ;
165 m_height = h ;
166 m_depth = d ;
72055702 167
20b69855
SC
168 m_bytesPerRow = w * 4 ;
169 size_t size = m_bytesPerRow * h ;
170 void* data = m_memBuf.GetWriteBuf(size) ;
171 memset( data , 0 , size) ;
172 m_memBuf.UngetWriteBuf(size) ;
71cc158e 173
20b69855
SC
174 m_hBitmap = NULL ;
175 Rect rect = { 0 , 0 , m_height , m_width } ;
176 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
177 (char*) data , m_bytesPerRow ) ) ;
178 wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ;
179 m_ok = ( m_hBitmap != NULL ) ;
71cc158e 180
20b69855
SC
181 return m_ok ;
182}
72055702 183
20b69855
SC
184void wxBitmapRefData::UseAlpha( bool use )
185{
186 if ( m_hasAlpha == use )
187 return ;
188
189 m_hasAlpha = use ;
20b69855 190 if ( m_hasAlpha )
72055702 191 {
20b69855
SC
192 int width = GetWidth() ;
193 int height = GetHeight() ;
431c82e0 194 m_maskBytesPerRow = ( width * 4 + 3 ) & 0xFFFFFFC ;
20b69855
SC
195 size_t size = height * m_maskBytesPerRow ;
196 unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ;
197 memset( data , 0 , size ) ;
198 wxASSERT( m_hMaskBitmap == NULL ) ;
199 Rect rect = { 0 , 0 , height , width } ;
431c82e0 200 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
20b69855
SC
201 (char*) data , m_maskBytesPerRow ) ) ;
202 wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ;
203 m_maskMemBuf.UngetWriteBuf(size) ;
71cc158e 204#if !wxMAC_USE_CORE_GRAPHICS
20b69855 205 UpdateAlphaMask() ;
71cc158e 206#endif
72055702
SC
207 }
208 else
209 {
20b69855
SC
210 DisposeGWorld( m_hMaskBitmap ) ;
211 m_hMaskBitmap = NULL ;
212 m_maskBytesPerRow = 0 ;
72055702 213 }
72055702
SC
214}
215
20b69855 216void *wxBitmapRefData::GetRawAccess() const
72055702 217{
20b69855
SC
218 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
219 return m_memBuf.GetData() ;
220}
72055702 221
20b69855
SC
222void *wxBitmapRefData::BeginRawAccess()
223{
224 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
225 wxASSERT( m_rawAccessCount == 0 ) ;
226 ++m_rawAccessCount ;
b28aeea5 227 // we must destroy an existing cached image, as
20b69855 228 // the bitmap data may change now
b28aeea5
SC
229 wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
230 wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
231#if wxMAC_USE_CORE_GRAPHICS
20b69855
SC
232 if ( m_cgImageRef )
233 {
234 CGImageRelease( m_cgImageRef ) ;
235 m_cgImageRef = NULL ;
236 }
237#endif
238 return m_memBuf.GetData() ;
239}
55e18dbe 240
20b69855
SC
241void wxBitmapRefData::EndRawAccess()
242{
243 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
244 wxASSERT( m_rawAccessCount == 1 ) ;
245 --m_rawAccessCount ;
246#if !wxMAC_USE_CORE_GRAPHICS
247 UpdateAlphaMask() ;
248#endif
249}
55e18dbe 250
b28aeea5
SC
251bool wxBitmapRefData::HasNativeSize()
252{
253 int w = GetWidth() ;
254 int h = GetHeight() ;
255 int sz = wxMax( w , h ) ;
256
257 if ( sz == 128 || sz == 48 || sz == 32 || sz == 16 )
258 return true ;
259 return false ;
260}
261
262IconRef wxBitmapRefData::GetIconRef()
263{
264 if ( m_iconRef == NULL )
265 {
266 // Create Icon Family Handle
267
268 IconFamilyHandle iconFamily = NULL ;
269
270 iconFamily = (IconFamilyHandle) NewHandle(8) ;
271 (**iconFamily).resourceType = kIconFamilyType ;
272 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
273
274 int w = GetWidth() ;
275 int h = GetHeight() ;
276 int sz = wxMax( w , h ) ;
277
278 OSType dataType = 0 ;
279 OSType maskType = 0 ;
280
281 if ( sz == 128 )
282 {
283 dataType = kThumbnail32BitData ;
284 maskType = kThumbnail8BitMask ;
285 }
286 else if ( sz == 48 )
287 {
288 dataType = kHuge32BitData ;
289 maskType = kHuge8BitMask ;
290 }
291 else if ( sz == 32 )
292 {
293 dataType = kLarge32BitData ;
294 maskType = kLarge8BitMask ;
295 }
296 else if ( sz == 16 )
297 {
298 dataType = kSmall32BitData ;
299 maskType = kSmall8BitMask ;
300 }
301
302 if ( dataType != 0 )
303 {
304 // setup the header properly
305
306 Handle data = NULL ;
307 Handle maskdata = NULL ;
308 unsigned char * maskptr = NULL ;
309 unsigned char * ptr = NULL ;
310 size_t size ;
311 size_t masksize ;
312
313 size = sz * sz * 4 ;
314 data = NewHandle( size) ;
315 HLock( data ) ;
316 ptr = (unsigned char*) *data ;
317 memset( ptr , 0, size ) ;
318
319 masksize = sz * sz ;
320 maskdata = NewHandle( masksize ) ;
321 HLock( maskdata ) ;
322 maskptr = (unsigned char*) *maskdata ;
323 memset( maskptr , 0 , masksize ) ;
324
325 bool hasAlpha = HasAlpha() ;
326 wxMask *mask = m_bitmapMask ;
327 unsigned char * source = (unsigned char*) GetRawAccess() ;
328 unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
329 for ( int y = 0 ; y < h ; ++y )
330 {
331 unsigned char * dest = ptr + y * sz * 4 ;
332 unsigned char * maskdest = maskptr + y * sz ;
333 for ( int x = 0 ; x < w ; ++x )
334 {
335 unsigned char a = *source ++ ;
336 unsigned char r = *source ++ ;
337 unsigned char g = *source ++ ;
338 unsigned char b = *source ++ ;
339
340 *dest++ = 0 ;
341 *dest++ = r ;
342 *dest++ = g ;
343 *dest++ = b ;
344
345 if ( mask )
346 *maskdest++ = *masksource++ ;
347 else if ( hasAlpha )
348 *maskdest++ = a ;
349 else
350 *maskdest++ = 0xFF ;
351 }
352 }
353
354 OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
355 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
356
357 err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
358 wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
359 HUnlock( data ) ;
360 HUnlock( maskdata ) ;
361 DisposeHandle( data ) ;
362 DisposeHandle( maskdata ) ;
363 }
364 else
365 {
366 iconFamily = (IconFamilyHandle) NewHandle(8) ;
367 (**iconFamily).resourceType = kIconFamilyType ;
368 (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
369 PicHandle pic = GetPictHandle() ;
370 SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
371 }
372
373 // transform into IconRef
374
375 static int iconCounter = 2 ;
d45318b8
RN
376#ifdef __WXDEBUG__
377 OSStatus err =
378#endif
379 RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ;
b28aeea5
SC
380 wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
381 // we have to retain a reference, as Unregister will decrement it
382 AcquireIconRef( m_iconRef ) ;
383 UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
384 DisposeHandle( (Handle) iconFamily ) ;
385 ++iconCounter ;
386 }
387 return m_iconRef ;
388}
389
390PicHandle wxBitmapRefData::GetPictHandle()
391{
392 if ( m_pictHandle == NULL )
393 {
394 CGrafPtr origPort = NULL ;
395 GDHandle origDev = NULL ;
b28aeea5
SC
396 GWorldPtr wp = NULL ;
397 GWorldPtr mask = NULL ;
398 int height = GetHeight() ;
399 int width = GetWidth() ;
400
401 Rect rect = { 0 , 0 , height , width } ;
402
403 GetGWorld( &origPort , &origDev ) ;
404
405 wp = GetHBITMAP( &mask ) ;
406
407 RgnHandle clipRgn = NULL ;
408
409 if ( mask )
410 {
411 GWorldPtr monoworld ;
412 clipRgn = NewRgn() ;
413 OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
414 verify_noerr(err) ;
415 LockPixels( GetGWorldPixMap( monoworld ) ) ;
416 LockPixels( GetGWorldPixMap( mask ) ) ;
417 SetGWorld( monoworld , NULL ) ;
418 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
419 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
420 RGBForeColor( &black ) ;
421 RGBBackColor( &white ) ;
422 CopyBits(GetPortBitMapForCopyBits(mask),
423 GetPortBitMapForCopyBits(monoworld),
424 &rect,
425 &rect,
426 srcCopy,NULL);
427 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
428 UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
429 UnlockPixels( GetGWorldPixMap( mask ) ) ;
430 DisposeGWorld( monoworld ) ;
431 }
432
433 SetGWorld( wp , NULL ) ;
434 Rect portRect ;
435 GetPortBounds( wp , &portRect ) ;
436 m_pictHandle = OpenPicture(&portRect);
437
438 if(m_pictHandle)
439 {
440 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
441 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
442 RGBForeColor( &black ) ;
443 RGBBackColor( &white ) ;
444
445 if ( clipRgn )
446 SetClip( clipRgn ) ;
447
448 LockPixels( GetGWorldPixMap( wp ) ) ;
449 CopyBits(GetPortBitMapForCopyBits(wp),
450 GetPortBitMapForCopyBits(wp),
451 &portRect,
452 &portRect,
453 srcCopy,clipRgn);
454 UnlockPixels( GetGWorldPixMap( wp ) ) ;
455 ClosePicture();
456 }
457 SetGWorld( origPort , origDev ) ;
458 if ( clipRgn )
459 DisposeRgn( clipRgn ) ;
460 }
461 return m_pictHandle ;
462}
55e18dbe 463
30e77b5c 464#ifdef __WXMAC_OSX__
71cc158e 465void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
20b69855 466{
71cc158e
SC
467 wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
468 wxASSERT( data == membuf->GetData() ) ;
469 delete membuf ;
5fde6fcc
GD
470}
471
20b69855 472CGImageRef wxBitmapRefData::CGImageCreate() const
be295828 473{
20b69855
SC
474 wxASSERT( m_ok ) ;
475 wxASSERT( m_rawAccessCount >= 0 ) ;
476 CGImageRef image ;
477 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
be295828 478 {
20b69855
SC
479 size_t imageSize = m_width * m_height * 4 ;
480 void * dataBuffer = m_memBuf.GetData() ;
481 int w = m_width ;
482 int h = m_height ;
483 CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
484 wxMemoryBuffer* membuf = NULL ;
485
486 if ( m_bitmapMask )
be295828 487 {
20b69855
SC
488 membuf = new wxMemoryBuffer( imageSize ) ;
489 memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
490 unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
491 int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
492 unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
493 alphaInfo = kCGImageAlphaFirst ;
494 for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
be295828 495 {
20b69855
SC
496 unsigned char *sourcemask = sourcemaskstart ;
497 for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
be295828 498 {
20b69855 499 *destalpha = *sourcemask ;
be295828
SC
500 }
501 }
502 }
20b69855 503 else if ( m_hasAlpha )
e40298d5 504 {
20b69855
SC
505#if wxMAC_USE_PREMULTIPLIED_ALPHA
506 alphaInfo = kCGImageAlphaPremultipliedFirst ;
507#else
508 alphaInfo = kCGImageAlphaFirst ;
509#endif
510 membuf = new wxMemoryBuffer( m_memBuf ) ;
e40298d5 511 }
20b69855
SC
512 else
513 {
514 membuf = new wxMemoryBuffer( m_memBuf ) ;
515 }
516 CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
517 CGDataProviderRef dataProvider =
71cc158e
SC
518 CGDataProviderCreateWithData( membuf , (const void *)membuf->GetData() , imageSize,
519 wxMacMemoryBufferReleaseProc );
20b69855
SC
520 image =
521 ::CGImageCreate( w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo ,
522 dataProvider, NULL , false , kCGRenderingIntentDefault );
523 CGDataProviderRelease( dataProvider);
524 }
525 else
526 {
527 image = m_cgImageRef ;
528 CGImageRetain( image ) ;
be295828 529 }
20b69855
SC
530 if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
531 {
532 // we keep it for later use
533 m_cgImageRef = image ;
534 CGImageRetain( image ) ;
535 }
536 return image ;
be295828 537}
20b69855 538#endif
be295828 539
20b69855
SC
540GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const
541{
542 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
543 if ( mask )
544 {
545 *mask = NULL ;
546 if ( m_bitmapMask )
547 *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ;
548 else if ( m_hasAlpha )
549 {
71cc158e 550#if !wxMAC_USE_CORE_GRAPHICS
20b69855
SC
551 if ( m_rawAccessCount > 0 )
552 UpdateAlphaMask() ;
71cc158e
SC
553#else
554 // this structure is not kept in synch when using CG, so if someone
555 // is really accessing the Graphports, we have to sync it
556 UpdateAlphaMask() ;
557#endif
20b69855
SC
558 *mask = m_hMaskBitmap ;
559 }
560 }
561 return m_hBitmap ;
e9576ca5
SC
562}
563
20b69855 564void wxBitmapRefData::UpdateAlphaMask() const
e9576ca5 565{
20b69855 566 if ( m_hasAlpha )
e40298d5 567 {
20b69855
SC
568 unsigned char *sourcemask = (unsigned char *) GetRawAccess() ;
569 unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ;
570
571 int h = GetHeight() ;
572 int w = GetWidth() ;
573
574 for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow )
575 {
576 unsigned char* destalpha = destalphabase ;
431c82e0 577 for( int x = 0 ; x < w ; ++x , sourcemask += 4 )
e40298d5 578 {
431c82e0
SC
579 // we must have 24 bit depth for non quartz smooth alpha
580 *destalpha++ = 255 ;
581 *destalpha++ = 255 - *sourcemask ;
582 *destalpha++ = 255 - *sourcemask ;
583 *destalpha++ = 255 - *sourcemask ;
e40298d5 584 }
20b69855
SC
585 }
586 }
587}
588
20b69855
SC
589void wxBitmapRefData::Free()
590{
591 wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ;
592
593#if wxMAC_USE_CORE_GRAPHICS
594 if ( m_cgImageRef )
595 {
596 CGImageRelease( m_cgImageRef ) ;
597 m_cgImageRef = NULL ;
e40298d5 598 }
71cc158e 599#endif
b28aeea5
SC
600 if ( m_iconRef )
601 {
602 ReleaseIconRef( m_iconRef ) ;
603 m_iconRef = NULL ;
604 }
605 if ( m_pictHandle )
606 {
607 KillPicture( m_pictHandle ) ;
608 m_pictHandle = NULL ;
609 }
20b69855
SC
610 if ( m_hBitmap )
611 {
612 DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ;
613 m_hBitmap = NULL ;
614 }
615 if ( m_hMaskBitmap )
616 {
617 DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ;
618 m_hMaskBitmap = NULL ;
619 }
55e18dbe 620
20b69855 621 if (m_bitmapMask)
e40298d5 622 {
20b69855
SC
623 delete m_bitmapMask;
624 m_bitmapMask = NULL;
e40298d5 625 }
e9576ca5
SC
626}
627
85f296a3
SC
628wxBitmapRefData::~wxBitmapRefData()
629{
20b69855 630 Free() ;
85f296a3
SC
631}
632
90b959ae
SC
633bool wxBitmap::CopyFromIcon(const wxIcon& icon)
634{
20b69855
SC
635 int w = icon.GetWidth() ;
636 int h = icon.GetHeight() ;
b28aeea5
SC
637
638
20b69855
SC
639 Create( icon.GetWidth() , icon.GetHeight() ) ;
640
b28aeea5
SC
641 bool created = false ;
642
71cc158e 643 if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) )
20b69855
SC
644 {
645 IconFamilyHandle iconFamily = NULL ;
646 Handle imagehandle = NewHandle(0) ;
647 Handle maskhandle = NewHandle(0) ;
71cc158e 648
9cc62fc8
SC
649 OSType maskType = 0;
650 OSType dataType = 0;
651 IconSelectorValue selector = 0 ;
71cc158e
SC
652 if ( w == 128 )
653 {
654 dataType = kThumbnail32BitData ;
655 maskType = kThumbnail8BitMask ;
656 selector = kSelectorAllAvailableData ;
657 }
658 else if ( w == 48 )
659 {
660 dataType = kHuge32BitData ;
661 maskType = kHuge8BitMask ;
662 selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ;
663 }
664 else if ( w == 32 )
665 {
666 dataType = kLarge32BitData ;
667 maskType = kLarge8BitMask ;
668 selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ;
669 }
670 else if ( w == 16 )
671 {
672 dataType = kSmall32BitData ;
673 maskType = kSmall8BitMask ;
674 selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ;
675 }
71cc158e
SC
676
677
678 OSStatus err = ( IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ) ;
679
680 err =( GetIconFamilyData( iconFamily , dataType , imagehandle ) ) ;
681 err =( GetIconFamilyData( iconFamily , maskType , maskhandle ) ) ;
b28aeea5
SC
682 size_t imagehandlesize = GetHandleSize( imagehandle ) ;
683 size_t maskhandlesize = GetHandleSize( maskhandle ) ;
684
685 if ( imagehandlesize != 0 && maskhandlesize != 0 )
20b69855 686 {
b28aeea5
SC
687 wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ;
688 wxASSERT( GetHandleSize( maskhandle ) == w * h ) ;
689 UseAlpha() ;
690 unsigned char *source = (unsigned char *) *imagehandle ;
691 unsigned char *sourcemask = (unsigned char *) *maskhandle ;
692
693 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
694 for ( int y = 0 ; y < h ; ++y )
20b69855 695 {
b28aeea5
SC
696 for ( int x = 0 ; x < w ; ++x )
697 {
698 *destination++ = *sourcemask++ ;
699 source++ ;
700 *destination++ = *source++ ;
701 *destination++ = *source++ ;
702 *destination++ = *source++ ;
703 }
20b69855 704 }
b28aeea5
SC
705 EndRawAccess() ;
706 DisposeHandle( imagehandle ) ;
707 DisposeHandle( maskhandle ) ;
20b69855 708 }
b28aeea5
SC
709 DisposeHandle( (Handle) iconFamily ) ;
710
20b69855 711 }
b28aeea5
SC
712
713 if ( !created )
20b69855
SC
714 {
715 wxMemoryDC dc ;
716 dc.SelectObject( *this ) ;
717 dc.DrawIcon( icon , 0 , 0 ) ;
718 dc.SelectObject( wxNullBitmap ) ;
719 }
125c389e 720 return true;
90b959ae
SC
721}
722
e9576ca5
SC
723wxBitmap::wxBitmap()
724{
e9576ca5
SC
725}
726
727wxBitmap::~wxBitmap()
728{
e9576ca5
SC
729}
730
731wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
732{
20b69855 733 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
e9576ca5 734
d2c6d549
GD
735 if ( no_bits == 1 )
736 {
973b0afb
GD
737 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
738 if ( the_width % (sizeof(unsigned char) * 8) ) {
739 linesize += sizeof(unsigned char);
740 }
20b69855
SC
741 unsigned char* linestart = (unsigned char*) bits ;
742 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
973b0afb
GD
743 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
744 {
745 for ( int x = 0 ; x < the_width ; ++x )
746 {
747 int index = x / 8 ;
748 int bit = x % 8 ;
749 int mask = 1 << bit ;
750 if ( linestart[index] & mask )
751 {
20b69855
SC
752 *destination++ = 0xFF ;
753 *destination++ = 0 ;
754 *destination++ = 0 ;
755 *destination++ = 0 ;
973b0afb
GD
756 }
757 else
758 {
20b69855
SC
759 *destination++ = 0xFF ;
760 *destination++ = 0xFF ;
761 *destination++ = 0xFF ;
762 *destination++ = 0xFF ;
973b0afb
GD
763 }
764 }
765 }
20b69855 766 EndRawAccess() ;
d2c6d549
GD
767 }
768 else
769 {
973b0afb 770 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
d2c6d549 771 }
e9576ca5
SC
772}
773
774wxBitmap::wxBitmap(int w, int h, int d)
775{
776 (void)Create(w, h, d);
e9576ca5
SC
777}
778
a8562f55 779wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
780{
781 (void) Create(data, type, width, height, depth);
e9576ca5
SC
782}
783
a8562f55 784wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
e9576ca5 785{
a8562f55 786 LoadFile(filename, type);
e9576ca5
SC
787}
788
20b69855
SC
789wxBitmap::wxBitmap(const char **bits)
790{
791 (void) CreateFromXpm(bits);
792}
793
794wxBitmap::wxBitmap(char **bits)
795{
796 (void) CreateFromXpm((const char **)bits);
797}
798
799void* wxBitmap::GetRawAccess() const
800{
801 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
802 return M_BITMAPDATA->GetRawAccess() ;
803}
804
805void* wxBitmap::BeginRawAccess()
806{
807 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
808 return M_BITMAPDATA->BeginRawAccess() ;
809}
810
811void wxBitmap::EndRawAccess()
812{
813 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
814 M_BITMAPDATA->EndRawAccess() ;
815}
816
973b0afb 817bool wxBitmap::CreateFromXpm(const char **bits)
e9576ca5 818{
d45318b8 819#if wxUSE_IMAGE
973b0afb
GD
820 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
821 wxXPMDecoder decoder;
822 wxImage img = decoder.ReadData(bits);
55e18dbe
VZ
823 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
824 *this = wxBitmap(img);
973b0afb 825 return TRUE;
d45318b8
RN
826#else
827 return FALSE;
828#endif
973b0afb
GD
829}
830
30e77b5c 831#ifdef __WXMAC_OSX__
20b69855 832WXCGIMAGEREF wxBitmap::CGImageCreate() const
03e11df5 833{
20b69855
SC
834 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
835 return M_BITMAPDATA->CGImageCreate() ;
03e11df5 836}
20b69855 837#endif
03e11df5 838
5fde6fcc
GD
839wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
840{
20b69855 841 wxCHECK_MSG( Ok() &&
5fde6fcc
GD
842 (rect.x >= 0) && (rect.y >= 0) &&
843 (rect.x+rect.width <= GetWidth()) &&
844 (rect.y+rect.height <= GetHeight()),
845 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
846
55e18dbe 847
20b69855
SC
848 wxBitmap ret( rect.width, rect.height, GetDepth() );
849 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
5fde6fcc 850
5fde6fcc 851
20b69855
SC
852 int sourcewidth = GetWidth() ;
853 int destwidth = rect.width ;
854 int destheight = rect.height ;
855 {
856 unsigned char * sourcedata = (unsigned char*) GetRawAccess() ;
857 unsigned char * destdata = (unsigned char*) ret.BeginRawAccess() ;
858 int sourcelinesize = sourcewidth * 4 ;
859 int destlinesize = destwidth * 4 ;
860 unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ;
861 unsigned char *dest = destdata ;
862 for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
863 {
864 memcpy( dest , source , destlinesize ) ;
865 }
866 }
867 ret.EndRawAccess() ;
868
869 if ( M_BITMAPDATA->m_bitmapMask )
870 {
871 wxMemoryBuffer maskbuf ;
872 int rowBytes = ( destwidth + 3 ) & 0xFFFFFFC ;
873 size_t maskbufsize = rowBytes * destheight ;
874 unsigned char * destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ;
875
876 int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ;
877 int destlinesize = rowBytes ;
878 unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ;
879 source += rect.x + rect.y * sourcelinesize ;
880 unsigned char *dest = destdata ;
881
882 for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize)
883 {
884 memcpy( dest , source , destlinesize ) ;
885 }
886 maskbuf.UngetWriteBuf( maskbufsize ) ;
887 ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ;
888 }
889 else if ( HasAlpha() )
890 ret.UseAlpha() ;
5fde6fcc 891
20b69855 892 return ret;
5fde6fcc
GD
893}
894
e9576ca5
SC
895bool wxBitmap::Create(int w, int h, int d)
896{
897 UnRef();
898
20b69855
SC
899 if ( d < 0 )
900 d = wxDisplayDepth() ;
2a391f87 901
20b69855 902 m_refData = new wxBitmapRefData( w , h , d );
55e18dbe 903
20b69855 904 return M_BITMAPDATA->Ok() ;
519cb848
SC
905}
906
a8562f55 907bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
e9576ca5
SC
908{
909 UnRef();
910
e9576ca5
SC
911 wxBitmapHandler *handler = FindHandler(type);
912
a8562f55
GD
913 if ( handler )
914 {
4e9ed364 915 m_refData = new wxBitmapRefData;
e9576ca5 916
a8562f55 917 return handler->LoadFile(this, filename, type, -1, -1);
e9576ca5 918 }
a8562f55
GD
919 else
920 {
d45318b8 921#if wxUSE_IMAGE
a8562f55
GD
922 wxImage loadimage(filename, type);
923 if (loadimage.Ok()) {
924 *this = loadimage;
925 return true;
926 }
d45318b8 927#endif
a8562f55 928 }
427ff662 929 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 930 return false;
e9576ca5
SC
931}
932
a8562f55 933bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
934{
935 UnRef();
936
937 m_refData = new wxBitmapRefData;
938
939 wxBitmapHandler *handler = FindHandler(type);
940
941 if ( handler == NULL ) {
427ff662 942 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
e9576ca5
SC
943
944 return FALSE;
945 }
946
947 return handler->Create(this, data, type, width, height, depth);
948}
949
d45318b8
RN
950#if wxUSE_IMAGE
951
fec19ea9
VS
952wxBitmap::wxBitmap(const wxImage& image, int depth)
953{
954 wxCHECK_RET( image.Ok(), wxT("invalid image") )
fec19ea9 955
fec19ea9
VS
956 // width and height of the device-dependent bitmap
957 int width = image.GetWidth();
958 int height = image.GetHeight();
959
20b69855 960 m_refData = new wxBitmapRefData( width , height , depth ) ;;
55e18dbe 961
20b69855 962 // Create picture
fec19ea9 963
20b69855
SC
964 bool hasAlpha = false ;
965
966 if ( image.HasMask() )
967 {
968 // takes precedence, don't mix with alpha info
969 }
970 else
971 {
972 hasAlpha = image.HasAlpha() ;
973 }
974
975 if ( hasAlpha )
976 UseAlpha() ;
977
978 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
55e18dbe 979
fec19ea9 980 register unsigned char* data = image.GetData();
20b69855 981 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
fec19ea9
VS
982 for (int y = 0; y < height; y++)
983 {
984 for (int x = 0; x < width; x++)
985 {
20b69855
SC
986 if ( hasAlpha )
987 {
988 const unsigned char a = *alpha++;
989 *destination++ = a ;
990#if wxMAC_USE_PREMULTIPLIED_ALPHA
991 *destination++ = ((*data++) * a + 127 ) / 255 ;
992 *destination++ = ((*data++) * a + 127 ) / 255 ;
993 *destination++ = ((*data++) * a + 127 ) / 255 ;
994#else
995 *destination++ = *data++ ;
996 *destination++ = *data++ ;
997 *destination++ = *data++ ;
998#endif
999 }
1000 else
1001 {
1002 *destination++ = 0xFF ;
1003 *destination++ = *data++ ;
1004 *destination++ = *data++ ;
1005 *destination++ = *data++ ;
1006 }
fec19ea9 1007 }
55e18dbe 1008 }
20b69855
SC
1009 EndRawAccess() ;
1010 if ( image.HasMask() )
8dd336cb 1011 {
20b69855 1012 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
8dd336cb 1013 }
fec19ea9
VS
1014}
1015
1016wxImage wxBitmap::ConvertToImage() const
1017{
1018 wxImage image;
55e18dbe 1019
fec19ea9
VS
1020 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
1021
1022 // create an wxImage object
1023 int width = GetWidth();
1024 int height = GetHeight();
1025 image.Create( width, height );
1026
1027 unsigned char *data = image.GetData();
fec19ea9
VS
1028 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
1029
20b69855
SC
1030 unsigned char* source = (unsigned char*) GetRawAccess() ;
1031
1032 bool hasAlpha = false ;
1033 bool hasMask = false ;
7e7f40ed 1034 int maskBytesPerRow = 0 ;
20b69855
SC
1035 unsigned char *alpha = NULL ;
1036 unsigned char *mask = NULL ;
1037 if ( HasAlpha() )
2b5f62a0 1038 {
20b69855 1039 hasAlpha = true ;
2b5f62a0 1040 }
20b69855
SC
1041
1042 if ( GetMask() )
2b5f62a0 1043 {
20b69855
SC
1044 hasMask = true ;
1045 mask = (unsigned char*) GetMask()->GetRawAccess() ;
7e7f40ed 1046 maskBytesPerRow = GetMask()->GetBytesPerRow() ;
e40298d5 1047 }
20b69855
SC
1048
1049 if ( hasAlpha )
e40298d5 1050 {
20b69855
SC
1051 image.SetAlpha() ;
1052 alpha = image.GetAlpha() ;
e40298d5 1053 }
20b69855
SC
1054 int index = 0;
1055
1056 // The following masking algorithm is the same as well in msw/gtk:
1057 // the colour used as transparent one in wxImage and the one it is
1058 // replaced with when it really occurs in the bitmap
1059 static const int MASK_RED = 1;
1060 static const int MASK_GREEN = 2;
1061 static const int MASK_BLUE = 3;
1062 static const int MASK_BLUE_REPLACEMENT = 2;
1063
7e7f40ed 1064 for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow )
fec19ea9 1065 {
7e7f40ed 1066 unsigned char * maskp = mask ;
fec19ea9
VS
1067 for (int xx = 0; xx < width; xx++)
1068 {
20b69855
SC
1069 long color = *((long*) source) ;
1070 unsigned char a = ((color&0xFF000000) >> 24) ;
1071 unsigned char r = ((color&0x00FF0000) >> 16) ;
1072 unsigned char g = ((color&0x0000FF00) >> 8) ;
1073 unsigned char b = (color&0x000000FF);
1074 if ( hasMask )
55e18dbe 1075 {
7e7f40ed 1076 if ( *maskp++ == 0 )
e40298d5 1077 {
20b69855
SC
1078 if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
1079 b = MASK_BLUE_REPLACEMENT ;
53e8c532
SC
1080 else
1081 {
1082 r = MASK_RED ;
1083 g = MASK_GREEN ;
1084 b = MASK_BLUE ;
1085 }
e40298d5 1086 }
fec19ea9 1087 }
20b69855
SC
1088 else if ( hasAlpha )
1089 *alpha++ = a ;
1090
1091 data[index ] = r ;
1092 data[index + 1] = g ;
1093 data[index + 2] = b ;
fec19ea9 1094 index += 3;
20b69855 1095 source += 4 ;
fec19ea9
VS
1096 }
1097 }
20b69855
SC
1098 if ( hasMask )
1099 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
fec19ea9
VS
1100 return image;
1101}
1102
d45318b8 1103#endif //wxUSE_IMAGE
fec19ea9 1104
a8562f55
GD
1105bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
1106 const wxPalette *palette) const
e9576ca5
SC
1107{
1108 wxBitmapHandler *handler = FindHandler(type);
1109
a8562f55
GD
1110 if ( handler )
1111 {
1112 return handler->SaveFile(this, filename, type, palette);
1113 }
1114 else
1115 {
d45318b8 1116#if wxUSE_IMAGE
a8562f55 1117 wxImage image = ConvertToImage();
a8562f55 1118 return image.SaveFile(filename, type);
d45318b8 1119#endif
a8562f55 1120 }
55e18dbe 1121
427ff662 1122 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 1123 return false;
e9576ca5
SC
1124}
1125
5fde6fcc
GD
1126bool wxBitmap::Ok() const
1127{
20b69855 1128 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
5fde6fcc
GD
1129}
1130
1131int wxBitmap::GetHeight() const
1132{
1133 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1134
20b69855 1135 return M_BITMAPDATA->GetHeight();
5fde6fcc
GD
1136}
1137
1138int wxBitmap::GetWidth() const
1139{
1140 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1141
20b69855 1142 return M_BITMAPDATA->GetWidth() ;
5fde6fcc
GD
1143}
1144
1145int wxBitmap::GetDepth() const
1146{
1147 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1148
20b69855 1149 return M_BITMAPDATA->GetDepth();
5fde6fcc
GD
1150}
1151
179e085f
RN
1152#if WXWIN_COMPATIBILITY_2_4
1153
5fde6fcc
GD
1154int wxBitmap::GetQuality() const
1155{
20b69855 1156 return 0;
5fde6fcc
GD
1157}
1158
179e085f
RN
1159#endif
1160
5fde6fcc
GD
1161wxMask *wxBitmap::GetMask() const
1162{
1163 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1164
1165 return M_BITMAPDATA->m_bitmapMask;
1166}
1167
20b69855
SC
1168bool wxBitmap::HasAlpha() const
1169{
1170 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
1171
1172 return M_BITMAPDATA->HasAlpha() ;
1173}
1174
e9576ca5
SC
1175void wxBitmap::SetWidth(int w)
1176{
1177 if (!M_BITMAPDATA)
1178 m_refData = new wxBitmapRefData;
1179
20b69855 1180 M_BITMAPDATA->SetWidth(w);
e9576ca5
SC
1181}
1182
1183void wxBitmap::SetHeight(int h)
1184{
1185 if (!M_BITMAPDATA)
1186 m_refData = new wxBitmapRefData;
1187
20b69855 1188 M_BITMAPDATA->SetHeight(h);
e9576ca5
SC
1189}
1190
1191void wxBitmap::SetDepth(int d)
1192{
1193 if (!M_BITMAPDATA)
1194 m_refData = new wxBitmapRefData;
1195
20b69855 1196 M_BITMAPDATA->SetDepth(d);
e9576ca5
SC
1197}
1198
179e085f
RN
1199#if WXWIN_COMPATIBILITY_2_4
1200
20b69855 1201void wxBitmap::SetQuality(int WXUNUSED(quality))
e9576ca5 1202{
e9576ca5
SC
1203}
1204
179e085f
RN
1205#endif
1206
e9576ca5
SC
1207void wxBitmap::SetOk(bool isOk)
1208{
1209 if (!M_BITMAPDATA)
1210 m_refData = new wxBitmapRefData;
1211
20b69855 1212 M_BITMAPDATA->SetOk(isOk);
e9576ca5
SC
1213}
1214
a6de86fa 1215#if wxUSE_PALETTE
5fde6fcc
GD
1216wxPalette *wxBitmap::GetPalette() const
1217{
1218 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1219
1220 return &M_BITMAPDATA->m_bitmapPalette;
1221}
1222
e9576ca5
SC
1223void wxBitmap::SetPalette(const wxPalette& palette)
1224{
1225 if (!M_BITMAPDATA)
1226 m_refData = new wxBitmapRefData;
1227
1228 M_BITMAPDATA->m_bitmapPalette = palette ;
1229}
a6de86fa 1230#endif // wxUSE_PALETTE
e9576ca5
SC
1231
1232void wxBitmap::SetMask(wxMask *mask)
1233{
1234 if (!M_BITMAPDATA)
1235 m_refData = new wxBitmapRefData;
1236
a8562f55 1237 // Remove existing mask if there is one.
1e74d03b 1238 delete M_BITMAPDATA->m_bitmapMask;
a8562f55 1239
e9576ca5
SC
1240 M_BITMAPDATA->m_bitmapMask = mask ;
1241}
1242
20b69855 1243WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
5fde6fcc 1244{
20b69855 1245 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
5fde6fcc
GD
1246}
1247
20b69855
SC
1248// ----------------------------------------------------------------------------
1249// wxMask
1250// ----------------------------------------------------------------------------
e9576ca5
SC
1251
1252wxMask::wxMask()
1253{
20b69855 1254 Init() ;
e9576ca5
SC
1255}
1256
1257// Construct a mask from a bitmap and a colour indicating
1258// the transparent area
1259wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1260{
20b69855 1261 Init() ;
e9576ca5
SC
1262 Create(bitmap, colour);
1263}
1264
20b69855
SC
1265// Construct a mask from a mono bitmap (copies the bitmap).
1266wxMask::wxMask(const wxBitmap& bitmap)
e9576ca5 1267{
20b69855
SC
1268 Init() ;
1269 Create(bitmap);
e9576ca5
SC
1270}
1271
1272// Construct a mask from a mono bitmap (copies the bitmap).
20b69855 1273wxMask::wxMask(const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
e9576ca5 1274{
20b69855
SC
1275 Init() ;
1276 Create(data, width , height , bytesPerRow );
e9576ca5
SC
1277}
1278
1279wxMask::~wxMask()
1280{
4e9ed364
RR
1281 if ( m_maskBitmap )
1282 {
20b69855 1283 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
4e9ed364
RR
1284 m_maskBitmap = NULL ;
1285 }
e9576ca5
SC
1286}
1287
20b69855 1288void wxMask::Init()
e9576ca5 1289{
20b69855 1290 m_width = m_height = m_bytesPerRow = 0 ;
20b69855 1291 m_maskBitmap = NULL ;
20b69855 1292}
5fde6fcc 1293
20b69855
SC
1294void *wxMask::GetRawAccess() const
1295{
1296 return m_memBuf.GetData() ;
1297}
5fde6fcc 1298
431c82e0
SC
1299// this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white
1300// so no rainbox colors will be created by QD when blitting
1301
20b69855
SC
1302void wxMask::RealizeNative()
1303{
20b69855
SC
1304 if ( m_maskBitmap )
1305 {
1306 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1307 m_maskBitmap = NULL ;
1308 }
1309 Rect rect = { 0 , 0 , m_height , m_width } ;
1310 verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_maskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 ,
1311 (char*) m_memBuf.GetData() , m_bytesPerRow ) ) ;
20b69855 1312}
5fde6fcc 1313
20b69855
SC
1314// Create a mask from a mono bitmap (copies the bitmap).
1315bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow)
1316{
1317 m_memBuf = data ;
1318 m_width = width ;
1319 m_height = height ;
1320 m_bytesPerRow = bytesPerRow ;
1321 wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ;
1322 RealizeNative() ;
1323 return true ;
e9576ca5
SC
1324}
1325
20b69855
SC
1326// Create a mask from a mono bitmap (copies the bitmap).
1327bool wxMask::Create(const wxBitmap& bitmap)
e9576ca5 1328{
20b69855
SC
1329 m_width = bitmap.GetWidth() ;
1330 m_height = bitmap.GetHeight() ;
1331 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1332 size_t size = m_bytesPerRow * m_height ;
1333 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1334 memset( destdatabase , 0 , size ) ;
1335 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1336 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow )
1337 {
1338 unsigned char *destdata= destdatabase ;
1339 for( int x = 0 ; x < m_width ; ++x )
1340 {
1341 srcdata++ ;
1342 unsigned char r = *srcdata++ ;
1343 unsigned char g = *srcdata++ ;
1344 unsigned char b = *srcdata++ ;
1345 if ( ( r + g + b ) > 0x10 )
1346 *destdata++ = 0x00 ;
1347 else
1348 *destdata++ = 0xFF ;
1349 }
1350 }
1351 m_memBuf.UngetWriteBuf( size ) ;
1352 RealizeNative() ;
1353 return TRUE;
e9576ca5
SC
1354}
1355
1356// Create a mask from a bitmap and a colour indicating
1357// the transparent area
1358bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1359{
20b69855
SC
1360 m_width = bitmap.GetWidth() ;
1361 m_height = bitmap.GetHeight() ;
1362 m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ;
1363 size_t size = m_bytesPerRow * m_height ;
1364
1365 unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ;
1366 memset( destdatabase , 0 , size ) ;
1367 unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ;
1368 for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow)
8208e181 1369 {
20b69855
SC
1370 unsigned char *destdata= destdatabase ;
1371 for( int x = 0 ; x < m_width ; ++x )
55e18dbe 1372 {
20b69855
SC
1373 srcdata++ ;
1374 unsigned char r = *srcdata++ ;
1375 unsigned char g = *srcdata++ ;
1376 unsigned char b = *srcdata++ ;
1377 if ( colour == wxColour( r , g , b) )
1378 *destdata++ = 0x00 ;
8208e181 1379 else
20b69855 1380 *destdata++ = 0xFF ;
8208e181
SC
1381 }
1382 }
20b69855
SC
1383 m_memBuf.UngetWriteBuf( size ) ;
1384 RealizeNative() ;
8208e181 1385 return TRUE;
e9576ca5
SC
1386}
1387
20b69855 1388WXHBITMAP wxMask::GetHBITMAP() const
5fde6fcc 1389{
20b69855 1390 return m_maskBitmap ;
5fde6fcc
GD
1391}
1392
20b69855
SC
1393// ----------------------------------------------------------------------------
1394// wxBitmapHandler
1395// ----------------------------------------------------------------------------
e9576ca5 1396
be52b341
GD
1397wxBitmapHandler::~wxBitmapHandler()
1398{
1399}
1400
e9576ca5
SC
1401bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1402{
1403 return FALSE;
1404}
1405
a8562f55 1406bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1407 int desiredWidth, int desiredHeight)
1408{
1409 return FALSE;
1410}
1411
a8562f55 1412bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5
SC
1413{
1414 return FALSE;
1415}
1416
20b69855
SC
1417// ----------------------------------------------------------------------------
1418// Standard Handlers
1419// ----------------------------------------------------------------------------
e9576ca5 1420
519cb848
SC
1421class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1422{
1423 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1424public:
1425 inline wxPICTResourceHandler()
1426 {
3bf2bdfb
GD
1427 SetName(wxT("Macintosh Pict resource"));
1428 SetExtension(wxEmptyString);
1429 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
519cb848
SC
1430 };
1431
1432 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1433 int desiredWidth, int desiredHeight);
1434};
1435IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1436
179e085f 1437
519cb848
SC
1438bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1439 int desiredWidth, int desiredHeight)
1440{
179e085f 1441#if wxUSE_METAFILE
4e9ed364 1442 Str255 theName ;
427ff662 1443 wxMacStringToPascal( name , theName ) ;
55e18dbe 1444
4e9ed364
RR
1445 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1446 if ( thePict )
1447 {
20b69855
SC
1448 wxMetafile mf ;
1449 mf.SetHMETAFILE((WXHMETAFILE) thePict ) ;
1450 bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ;
1451 wxMemoryDC dc ;
1452 dc.SelectObject( *bitmap ) ;
1453 mf.Play( &dc ) ;
1454 dc.SelectObject( wxNullBitmap ) ;
4e9ed364
RR
1455 return TRUE ;
1456 }
179e085f 1457#endif //wxUSE_METAFILE
4e9ed364 1458 return FALSE ;
519cb848
SC
1459}
1460
179e085f 1461
e9576ca5
SC
1462void wxBitmap::InitStandardHandlers()
1463{
973b0afb
GD
1464 AddHandler(new wxPICTResourceHandler) ;
1465 AddHandler(new wxICONResourceHandler) ;
e9576ca5 1466}
55e18dbe
VZ
1467
1468// ----------------------------------------------------------------------------
1469// raw bitmap access support
1470// ----------------------------------------------------------------------------
1471
1472void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1473{
1474 if ( !Ok() )
1475 {
1476 // no bitmap, no data (raw or otherwise)
1477 return NULL;
1478 }
1479
20b69855
SC
1480 data.m_width = GetWidth() ;
1481 data.m_height = GetHeight() ;
1482 data.m_stride = GetWidth() * 4 ;
1483 return GetRawAccess() ;
55e18dbe
VZ
1484}
1485
1e74d03b 1486void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
55e18dbe
VZ
1487{
1488 if ( !Ok() )
1489 return;
1490
20b69855
SC
1491 // TODO : if we have some information about the API we should check
1492 // this code looks strange...
1493
1494 if ( M_BITMAPDATA->HasAlpha() )
1e74d03b
VZ
1495 {
1496 wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;
1497
1498 int w = data.GetWidth(),
1499 h = data.GetHeight();
1500
1501 wxBitmap bmpMask(GetWidth(), GetHeight(), 32);
1502 wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h));
1503 wxAlphaPixelData::Iterator pMask(dataMask),
1504 p(data);
1505 for ( int y = 0; y < h; y++ )
1506 {
1507 wxAlphaPixelData::Iterator rowStartMask = pMask,
1508 rowStart = p;
1509
1510 for ( int x = 0; x < w; x++ )
1511 {
1512 const wxAlphaPixelData::Iterator::ChannelType
1513 alpha = p.Alpha();
1514
1515 pMask.Red() = alpha;
1516 pMask.Green() = alpha;
1517 pMask.Blue() = alpha;
1518
1519 ++p;
1520 ++pMask;
1521 }
1522
1523 p = rowStart;
1524 p.OffsetY(data, 1);
1525
1526 pMask = rowStartMask;
1527 pMask.OffsetY(dataMask, 1);
1528 }
1529
1530 SetMask(new wxMask(bmpMask));
1531 }
55e18dbe
VZ
1532}
1533
1534void wxBitmap::UseAlpha()
1535{
1e74d03b
VZ
1536 // remember that we are using alpha channel, we'll need to create a proper
1537 // mask in UngetRawData()
20b69855 1538 M_BITMAPDATA->UseAlpha( true );
55e18dbe 1539}