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