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