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