]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/bitmap.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
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
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
29 IMPLEMENT_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 // 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
52 IconRef 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 )
64 {
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 ;
104 }
105
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 ;
170 }
171
172 void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
173 {
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 }
192 }
193
194 void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
195 {
196 if ( info->contentType == kControlContentIconRef )
197 {
198 ReleaseIconRef(info->u.iconRef) ;
199 }
200 #if wxMAC_USE_CORE_GRAPHICS
201 else if ( info->contentType == kControlContentCGImageRef )
202 {
203 CGImageRelease( info->u.imageRef ) ;
204 }
205 #endif
206 else
207 {
208 wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
209 }
210 }
211
212
213 void wxBitmapRefData::Init()
214 {
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;
229 }
230
231 wxBitmapRefData::wxBitmapRefData()
232 {
233 Init() ;
234 }
235
236 wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
237 {
238 Init() ;
239 Create( w , h , d ) ;
240 }
241
242 bool wxBitmapRefData::Create( int w , int h , int d )
243 {
244 m_width = w ;
245 m_height = h ;
246 m_depth = d ;
247
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 }
265
266 void 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 )
274 {
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() ;
288 }
289 else
290 {
291 DisposeGWorld( m_hMaskBitmap ) ;
292 m_hMaskBitmap = NULL ;
293 m_maskBytesPerRow = 0 ;
294 }
295 #endif
296 }
297
298 void *wxBitmapRefData::GetRawAccess() const
299 {
300 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
301 return m_memBuf.GetData() ;
302 }
303
304 void *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 }
320
321 void 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 }
330
331
332 #if wxMAC_USE_CORE_GRAPHICS
333 static void FreeImageMemoryBufferInstance(void *info, const void *data, size_t size)
334 {
335 delete ((wxMemoryBuffer*)info) ;
336 }
337
338 CGImageRef wxBitmapRefData::CGImageCreate() const
339 {
340 wxASSERT( m_ok ) ;
341 wxASSERT( m_rawAccessCount >= 0 ) ;
342 CGImageRef image ;
343 if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
344 {
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 )
353 {
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)
361 {
362 unsigned char *sourcemask = sourcemaskstart ;
363 for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
364 {
365 *destalpha = *sourcemask ;
366 }
367 }
368 }
369 else if ( m_hasAlpha )
370 {
371 #if wxMAC_USE_PREMULTIPLIED_ALPHA
372 alphaInfo = kCGImageAlphaPremultipliedFirst ;
373 #else
374 alphaInfo = kCGImageAlphaFirst ;
375 #endif
376 membuf = new wxMemoryBuffer( m_memBuf ) ;
377 }
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 ) ;
394 }
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 ;
402 }
403 #endif
404
405 #if !wxMAC_USE_CORE_GRAPHICS
406 GWorldPtr 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 ;
422 }
423
424 void wxBitmapRefData::UpdateAlphaMask() const
425 {
426 if ( m_hasAlpha )
427 {
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 )
438 {
439 *destalpha = *sourcemask ;
440 }
441 }
442 }
443 }
444
445 #endif
446
447 void 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 ;
456 }
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
469
470 if (m_bitmapMask)
471 {
472 delete m_bitmapMask;
473 m_bitmapMask = NULL;
474 }
475 }
476
477 wxBitmapRefData::~wxBitmapRefData()
478 {
479 Free() ;
480 }
481
482 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
483 {
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 }
525 return true;
526 }
527
528 wxBitmap::wxBitmap()
529 {
530 }
531
532 wxBitmap::~wxBitmap()
533 {
534 }
535
536 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
537 {
538 m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ;
539
540 if ( no_bits == 1 )
541 {
542 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
543 if ( the_width % (sizeof(unsigned char) * 8) ) {
544 linesize += sizeof(unsigned char);
545 }
546 unsigned char* linestart = (unsigned char*) bits ;
547 unsigned char* destination = (unsigned char*) BeginRawAccess() ;
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 {
557 *destination++ = 0xFF ;
558 *destination++ = 0 ;
559 *destination++ = 0 ;
560 *destination++ = 0 ;
561 }
562 else
563 {
564 *destination++ = 0xFF ;
565 *destination++ = 0xFF ;
566 *destination++ = 0xFF ;
567 *destination++ = 0xFF ;
568 }
569 }
570 }
571 EndRawAccess() ;
572 }
573 else
574 {
575 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
576 }
577 }
578
579 wxBitmap::wxBitmap(int w, int h, int d)
580 {
581 (void)Create(w, h, d);
582 }
583
584 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
585 {
586 (void) Create(data, type, width, height, depth);
587 }
588
589 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
590 {
591 LoadFile(filename, type);
592 }
593
594 wxBitmap::wxBitmap(const char **bits)
595 {
596 (void) CreateFromXpm(bits);
597 }
598
599 wxBitmap::wxBitmap(char **bits)
600 {
601 (void) CreateFromXpm((const char **)bits);
602 }
603
604 void* wxBitmap::GetRawAccess() const
605 {
606 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
607 return M_BITMAPDATA->GetRawAccess() ;
608 }
609
610 void* wxBitmap::BeginRawAccess()
611 {
612 wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ;
613 return M_BITMAPDATA->BeginRawAccess() ;
614 }
615
616 void wxBitmap::EndRawAccess()
617 {
618 wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
619 M_BITMAPDATA->EndRawAccess() ;
620 }
621
622 bool wxBitmap::CreateFromXpm(const char **bits)
623 {
624 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
625 wxXPMDecoder decoder;
626 wxImage img = decoder.ReadData(bits);
627 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
628 *this = wxBitmap(img);
629 return TRUE;
630 }
631
632 #if wxMAC_USE_CORE_GRAPHICS
633 WXCGIMAGEREF wxBitmap::CGImageCreate() const
634 {
635 wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
636 return M_BITMAPDATA->CGImageCreate() ;
637 }
638 #endif
639
640 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
641 {
642 wxCHECK_MSG( Ok() &&
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
648
649 wxBitmap ret( rect.width, rect.height, GetDepth() );
650 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
651
652
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() ;
692
693 return ret;
694 }
695
696 bool wxBitmap::Create(int w, int h, int d)
697 {
698 UnRef();
699
700 if ( d < 0 )
701 d = wxDisplayDepth() ;
702
703 m_refData = new wxBitmapRefData( w , h , d );
704
705 return M_BITMAPDATA->Ok() ;
706 }
707
708 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
709 {
710 UnRef();
711
712 wxBitmapHandler *handler = FindHandler(type);
713
714 if ( handler )
715 {
716 m_refData = new wxBitmapRefData;
717
718 return handler->LoadFile(this, filename, type, -1, -1);
719 }
720 else
721 {
722 wxImage loadimage(filename, type);
723 if (loadimage.Ok()) {
724 *this = loadimage;
725 return true;
726 }
727 }
728 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
729 return false;
730 }
731
732 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
733 {
734 UnRef();
735
736 m_refData = new wxBitmapRefData;
737
738 wxBitmapHandler *handler = FindHandler(type);
739
740 if ( handler == NULL ) {
741 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
742
743 return FALSE;
744 }
745
746 return handler->Create(this, data, type, width, height, depth);
747 }
748
749 wxBitmap::wxBitmap(const wxImage& image, int depth)
750 {
751 wxCHECK_RET( image.Ok(), wxT("invalid image") )
752
753 // width and height of the device-dependent bitmap
754 int width = image.GetWidth();
755 int height = image.GetHeight();
756
757 m_refData = new wxBitmapRefData( width , height , depth ) ;;
758
759 // Create picture
760
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() ;
776
777 register unsigned char* data = image.GetData();
778 const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ;
779 for (int y = 0; y < height; y++)
780 {
781 for (int x = 0; x < width; x++)
782 {
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 }
804 }
805 }
806 EndRawAccess() ;
807 if ( image.HasMask() )
808 {
809 SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ;
810 }
811 }
812
813 wxImage wxBitmap::ConvertToImage() const
814 {
815 wxImage image;
816
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();
825 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
826
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() )
834 {
835 hasAlpha = true ;
836 }
837
838 if ( GetMask() )
839 {
840 hasMask = true ;
841 mask = (unsigned char*) GetMask()->GetRawAccess() ;
842 }
843
844 if ( hasAlpha )
845 {
846 image.SetAlpha() ;
847 alpha = image.GetAlpha() ;
848 }
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
859 for (int yy = 0; yy < height; yy++)
860 {
861 for (int xx = 0; xx < width; xx++)
862 {
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 )
869 {
870 if ( *mask++ == 0 )
871 {
872 if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE )
873 b = MASK_BLUE_REPLACEMENT ;
874 }
875 else
876 {
877 r = MASK_RED ;
878 g = MASK_GREEN ;
879 b = MASK_BLUE ;
880 }
881 }
882 else if ( hasAlpha )
883 *alpha++ = a ;
884
885 data[index ] = r ;
886 data[index + 1] = g ;
887 data[index + 2] = b ;
888 index += 3;
889 source += 4 ;
890 }
891 }
892 if ( hasMask )
893 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
894 return image;
895 }
896
897
898 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
899 const wxPalette *palette) const
900 {
901 wxBitmapHandler *handler = FindHandler(type);
902
903 if ( handler )
904 {
905 return handler->SaveFile(this, filename, type, palette);
906 }
907 else
908 {
909 wxImage image = ConvertToImage();
910
911 return image.SaveFile(filename, type);
912 }
913
914 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
915 return false;
916 }
917
918 bool wxBitmap::Ok() const
919 {
920 return (M_BITMAPDATA && M_BITMAPDATA->Ok());
921 }
922
923 int wxBitmap::GetHeight() const
924 {
925 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
926
927 return M_BITMAPDATA->GetHeight();
928 }
929
930 int wxBitmap::GetWidth() const
931 {
932 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
933
934 return M_BITMAPDATA->GetWidth() ;
935 }
936
937 int wxBitmap::GetDepth() const
938 {
939 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
940
941 return M_BITMAPDATA->GetDepth();
942 }
943
944 int wxBitmap::GetQuality() const
945 {
946 return 0;
947 }
948
949 wxMask *wxBitmap::GetMask() const
950 {
951 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
952
953 return M_BITMAPDATA->m_bitmapMask;
954 }
955
956 bool wxBitmap::HasAlpha() const
957 {
958 wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") );
959
960 return M_BITMAPDATA->HasAlpha() ;
961 }
962
963 void wxBitmap::SetWidth(int w)
964 {
965 if (!M_BITMAPDATA)
966 m_refData = new wxBitmapRefData;
967
968 M_BITMAPDATA->SetWidth(w);
969 }
970
971 void wxBitmap::SetHeight(int h)
972 {
973 if (!M_BITMAPDATA)
974 m_refData = new wxBitmapRefData;
975
976 M_BITMAPDATA->SetHeight(h);
977 }
978
979 void wxBitmap::SetDepth(int d)
980 {
981 if (!M_BITMAPDATA)
982 m_refData = new wxBitmapRefData;
983
984 M_BITMAPDATA->SetDepth(d);
985 }
986
987 void wxBitmap::SetQuality(int WXUNUSED(quality))
988 {
989 }
990
991 void wxBitmap::SetOk(bool isOk)
992 {
993 if (!M_BITMAPDATA)
994 m_refData = new wxBitmapRefData;
995
996 M_BITMAPDATA->SetOk(isOk);
997 }
998
999 #if wxUSE_PALETTE
1000 wxPalette *wxBitmap::GetPalette() const
1001 {
1002 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1003
1004 return &M_BITMAPDATA->m_bitmapPalette;
1005 }
1006
1007 void wxBitmap::SetPalette(const wxPalette& palette)
1008 {
1009 if (!M_BITMAPDATA)
1010 m_refData = new wxBitmapRefData;
1011
1012 M_BITMAPDATA->m_bitmapPalette = palette ;
1013 }
1014 #endif // wxUSE_PALETTE
1015
1016 void wxBitmap::SetMask(wxMask *mask)
1017 {
1018 if (!M_BITMAPDATA)
1019 m_refData = new wxBitmapRefData;
1020
1021 // Remove existing mask if there is one.
1022 delete M_BITMAPDATA->m_bitmapMask;
1023
1024 M_BITMAPDATA->m_bitmapMask = mask ;
1025 }
1026
1027 #if !wxMAC_USE_CORE_GRAPHICS
1028 WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const
1029 {
1030 return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask));
1031 }
1032 #endif
1033
1034 // ----------------------------------------------------------------------------
1035 // wxMask
1036 // ----------------------------------------------------------------------------
1037
1038 wxMask::wxMask()
1039 {
1040 Init() ;
1041 }
1042
1043 // Construct a mask from a bitmap and a colour indicating
1044 // the transparent area
1045 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1046 {
1047 Init() ;
1048 Create(bitmap, colour);
1049 }
1050
1051 // Construct a mask from a mono bitmap (copies the bitmap).
1052 wxMask::wxMask(const wxBitmap& bitmap)
1053 {
1054 Init() ;
1055 Create(bitmap);
1056 }
1057
1058 // Construct a mask from a mono bitmap (copies the bitmap).
1059 wxMask::wxMask(const wxMemoryBuffer& data, int width , int height , int bytesPerRow )
1060 {
1061 Init() ;
1062 Create(data, width , height , bytesPerRow );
1063 }
1064
1065 wxMask::~wxMask()
1066 {
1067 #if !wxMAC_USE_CORE_GRAPHICS
1068 if ( m_maskBitmap )
1069 {
1070 DisposeGWorld( (GWorldPtr) m_maskBitmap ) ;
1071 m_maskBitmap = NULL ;
1072 }
1073 #endif
1074 }
1075
1076 void wxMask::Init()
1077 {
1078 m_width = m_height = m_bytesPerRow = 0 ;
1079 #if !wxMAC_USE_CORE_GRAPHICS
1080 m_maskBitmap = NULL ;
1081 #endif
1082
1083 }
1084
1085 void *wxMask::GetRawAccess() const
1086 {
1087 return m_memBuf.GetData() ;
1088 }
1089
1090 void 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 }
1103
1104 // Create a mask from a mono bitmap (copies the bitmap).
1105 bool 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 ;
1114 }
1115
1116 // Create a mask from a mono bitmap (copies the bitmap).
1117 bool wxMask::Create(const wxBitmap& bitmap)
1118 {
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;
1144 }
1145
1146 // Create a mask from a bitmap and a colour indicating
1147 // the transparent area
1148 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1149 {
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)
1159 {
1160 unsigned char *destdata= destdatabase ;
1161 for( int x = 0 ; x < m_width ; ++x )
1162 {
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 ;
1169 else
1170 *destdata++ = 0xFF ;
1171 }
1172 }
1173 m_memBuf.UngetWriteBuf( size ) ;
1174 RealizeNative() ;
1175 return TRUE;
1176 }
1177
1178 #if !wxMAC_USE_CORE_GRAPHICS
1179 WXHBITMAP wxMask::GetHBITMAP() const
1180 {
1181 return m_maskBitmap ;
1182 }
1183 #endif
1184
1185 // ----------------------------------------------------------------------------
1186 // wxBitmapHandler
1187 // ----------------------------------------------------------------------------
1188
1189 wxBitmapHandler::~wxBitmapHandler()
1190 {
1191 }
1192
1193 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1194 {
1195 return FALSE;
1196 }
1197
1198 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1199 int desiredWidth, int desiredHeight)
1200 {
1201 return FALSE;
1202 }
1203
1204 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1205 {
1206 return FALSE;
1207 }
1208
1209 // ----------------------------------------------------------------------------
1210 // Standard Handlers
1211 // ----------------------------------------------------------------------------
1212
1213 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1214 {
1215 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1216 public:
1217 inline wxPICTResourceHandler()
1218 {
1219 SetName(wxT("Macintosh Pict resource"));
1220 SetExtension(wxEmptyString);
1221 SetType(wxBITMAP_TYPE_PICT_RESOURCE);
1222 };
1223
1224 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1225 int desiredWidth, int desiredHeight);
1226 };
1227 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1228
1229 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1230 int desiredWidth, int desiredHeight)
1231 {
1232 Str255 theName ;
1233 wxMacStringToPascal( name , theName ) ;
1234
1235 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1236 if ( thePict )
1237 {
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 ) ;
1245 return TRUE ;
1246 }
1247 return FALSE ;
1248 }
1249
1250 void wxBitmap::InitStandardHandlers()
1251 {
1252 AddHandler(new wxPICTResourceHandler) ;
1253 AddHandler(new wxICONResourceHandler) ;
1254 }
1255
1256 // ----------------------------------------------------------------------------
1257 // raw bitmap access support
1258 // ----------------------------------------------------------------------------
1259
1260 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1261 {
1262 if ( !Ok() )
1263 {
1264 // no bitmap, no data (raw or otherwise)
1265 return NULL;
1266 }
1267
1268 data.m_width = GetWidth() ;
1269 data.m_height = GetHeight() ;
1270 data.m_stride = GetWidth() * 4 ;
1271 return GetRawAccess() ;
1272 }
1273
1274 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1275 {
1276 if ( !Ok() )
1277 return;
1278
1279 // TODO : if we have some information about the API we should check
1280 // this code looks strange...
1281
1282 if ( M_BITMAPDATA->HasAlpha() )
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 }
1320 }
1321
1322 void wxBitmap::UseAlpha()
1323 {
1324 // remember that we are using alpha channel, we'll need to create a proper
1325 // mask in UngetRawData()
1326 M_BITMAPDATA->UseAlpha( true );
1327 }