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