]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/bitmap.cpp
minor tab height bug fix
[wxWidgets.git] / src / mac / classic / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/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 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/bitmap.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/log.h"
22 #include "wx/icon.h"
23 #include "wx/image.h"
24 #endif
25
26 #include "wx/xpmdecod.h"
27
28 #include "wx/rawbmp.h"
29
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
31 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
32 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )
33
34 #ifdef __DARWIN__
35 #include <ApplicationServices/ApplicationServices.h>
36 #else
37 #include <PictUtils.h>
38 #endif
39
40 #include "wx/mac/uma.h"
41
42 CTabHandle wxMacCreateColorTable( int numColors )
43 {
44 CTabHandle newColors; /* Handle to the new color table */
45
46 /* Allocate memory for the color table */
47 newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) +
48 sizeof (ColorSpec) * (numColors - 1) );
49 if (newColors != nil)
50 {
51 /* Initialize the fields */
52 (**newColors).ctSeed = GetCTSeed();
53 (**newColors).ctFlags = 0;
54 (**newColors).ctSize = numColors - 1;
55 /* Initialize the table of colors */
56 }
57 return newColors ;
58 }
59
60 void wxMacDestroyColorTable( CTabHandle colors )
61 {
62 DisposeHandle( (Handle) colors ) ;
63 }
64
65 void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue )
66 {
67 (**newColors).ctTable[index].value = index;
68 (**newColors).ctTable[index].rgb.red = red ; // someRedValue;
69 (**newColors).ctTable[index].rgb.green = green ; // someGreenValue;
70 (**newColors).ctTable[index].rgb.blue = blue ; // someBlueValue;
71 }
72
73 GWorldPtr wxMacCreateGWorld( int width , int height , int depth )
74 {
75 OSErr err = noErr ;
76 GWorldPtr port ;
77 Rect rect = { 0 , 0 , height , width } ;
78
79 if ( depth < 0 )
80 {
81 depth = wxDisplayDepth() ;
82 }
83
84 err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ;
85 if ( err == noErr )
86 {
87 return port ;
88 }
89 return NULL ;
90 }
91
92 void wxMacDestroyGWorld( GWorldPtr gw )
93 {
94 if ( gw )
95 DisposeGWorld( gw ) ;
96 }
97
98 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
99
100 OSErr SetupCIconHandlePixMap( CIconHandle icon , short depth , Rect *bounds , CTabHandle colors )
101 {
102 CTabHandle newColors; /* Color table used for the off-screen PixMap */
103 Ptr offBaseAddr; /* Pointer to the off-screen pixel image */
104 OSErr error; /* Returns error code */
105 short bytesPerRow; /* Number of bytes per row in the PixMap */
106
107
108 error = noErr;
109 newColors = nil;
110 offBaseAddr = nil;
111
112 bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4;
113
114 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
115 if (depth <= 8)
116 {
117 newColors = colors;
118 error = HandToHand((Handle *) &newColors);
119 }
120 else
121 {
122 newColors = (CTabHandle) NewHandle(sizeof(ColorTable) -
123 sizeof(CSpecArray));
124 error = MemError();
125 }
126 if (error == noErr)
127 {
128 /* Allocate pixel image; long integer multiplication avoids overflow */
129 (**icon).iconData = NewHandle((unsigned long) bytesPerRow * (bounds->bottom -
130 bounds->top));
131 if ((**icon).iconData != nil)
132 {
133 /* Initialize fields common to indexed and direct PixMaps */
134 (**icon).iconPMap.baseAddr = 0; /* Point to image */
135 (**icon).iconPMap.rowBytes = bytesPerRow | /* MSB set for PixMap */
136 0x8000;
137 (**icon).iconPMap.bounds = *bounds; /* Use given bounds */
138 (**icon).iconPMap.pmVersion = 0; /* No special stuff */
139 (**icon).iconPMap.packType = 0; /* Default PICT pack */
140 (**icon).iconPMap.packSize = 0; /* Always zero in mem */
141 (**icon).iconPMap.hRes = kDefaultRes; /* 72 DPI default res */
142 (**icon).iconPMap.vRes = kDefaultRes; /* 72 DPI default res */
143 (**icon).iconPMap.pixelSize = depth; /* Set # bits/pixel */
144
145 /* Initialize fields specific to indexed and direct PixMaps */
146 if (depth <= 8)
147 {
148 /* PixMap is indexed */
149 (**icon).iconPMap.pixelType = 0; /* Indicates indexed */
150 (**icon).iconPMap.cmpCount = 1; /* Have 1 component */
151 (**icon).iconPMap.cmpSize = depth; /* Component size=depth */
152 (**icon).iconPMap.pmTable = newColors; /* Handle to CLUT */
153 }
154 else
155 {
156 /* PixMap is direct */
157 (**icon).iconPMap.pixelType = RGBDirect; /* Indicates direct */
158 (**icon).iconPMap.cmpCount = 3; /* Have 3 components */
159 if (depth == 16)
160 (**icon).iconPMap.cmpSize = 5; /* 5 bits/component */
161 else
162 (**icon).iconPMap.cmpSize = 8; /* 8 bits/component */
163 (**newColors).ctSeed = 3 * (**icon).iconPMap.cmpSize;
164 (**newColors).ctFlags = 0;
165 (**newColors).ctSize = 0;
166 (**icon).iconPMap.pmTable = newColors;
167 }
168 }
169 else
170 error = MemError();
171 }
172 else
173 newColors = nil;
174
175 /* If no errors occurred, return a handle to the new off-screen PixMap */
176 if (error != noErr)
177 {
178 if (newColors != nil)
179 DisposeCTable(newColors);
180 }
181
182 /* Return the error code */
183 return error;
184 }
185
186 CIconHandle wxMacCreateCIcon(GWorldPtr image , GWorldPtr mask , short dstDepth , short iconSize )
187 {
188 GWorldPtr saveWorld;
189 GDHandle saveHandle;
190
191 GetGWorld(&saveWorld,&saveHandle); // save Graphics env state
192 SetGWorld(image,nil);
193
194 Rect frame = { 0 , 0 , iconSize , iconSize } ;
195 Rect imageBounds = frame ;
196 GetPortBounds( image , &imageBounds ) ;
197
198 int bwSize = iconSize / 8 * iconSize ;
199 CIconHandle icon = (CIconHandle) NewHandleClear( sizeof ( CIcon ) + 2 * bwSize) ;
200 HLock((Handle)icon) ;
201 SetupCIconHandlePixMap( icon , dstDepth , &frame,GetCTable(dstDepth)) ;
202 HLock( (**icon).iconData ) ;
203 (**icon).iconPMap.baseAddr = *(**icon).iconData ;
204
205 LockPixels(GetGWorldPixMap(image));
206
207 CopyBits(GetPortBitMapForCopyBits(image),
208 (BitMapPtr)&((**icon).iconPMap),
209 &imageBounds,
210 &imageBounds,
211 srcCopy | ditherCopy, nil);
212
213
214 UnlockPixels(GetGWorldPixMap(image));
215 HUnlock( (**icon).iconData ) ;
216
217 (**icon).iconMask.rowBytes = iconSize / 8 ;
218 (**icon).iconMask.bounds = frame ;
219
220 (**icon).iconBMap.rowBytes = iconSize / 8 ;
221 (**icon).iconBMap.bounds = frame ;
222 (**icon).iconMask.baseAddr = (char*) &(**icon).iconMaskData ;
223 (**icon).iconBMap.baseAddr = (char*) &(**icon).iconMaskData + bwSize ;
224
225 if ( mask )
226 {
227 Rect r ;
228 GetPortBounds( image , &r ) ;
229 LockPixels(GetGWorldPixMap(mask) ) ;
230 CopyBits(GetPortBitMapForCopyBits(mask) ,
231 &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
232 CopyBits(GetPortBitMapForCopyBits(mask) ,
233 &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
234 UnlockPixels(GetGWorldPixMap( mask ) ) ;
235 }
236 else
237 {
238 Rect r ;
239 GetPortBounds( image , &r ) ;
240 LockPixels(GetGWorldPixMap(image));
241 CopyBits(GetPortBitMapForCopyBits(image) ,
242 &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
243 CopyBits(GetPortBitMapForCopyBits(image) ,
244 &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
245 UnlockPixels(GetGWorldPixMap(image));
246 }
247
248 (**icon).iconMask.baseAddr = NULL ;
249 (**icon).iconBMap.baseAddr = NULL ;
250 (**icon).iconPMap.baseAddr = NULL ;
251 HUnlock((Handle)icon) ;
252 SetGWorld(saveWorld,saveHandle);
253
254 return icon;
255 }
256
257 PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask)
258 {
259 CGrafPtr origPort ;
260 GDHandle origDev ;
261
262 PicHandle pict;
263
264 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
265 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
266
267 GetGWorld( &origPort , &origDev ) ;
268
269 RgnHandle clipRgn = NULL ;
270
271 if ( mask )
272 {
273 clipRgn = NewRgn() ;
274 LockPixels( GetGWorldPixMap( mask ) ) ;
275 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ;
276 UnlockPixels( GetGWorldPixMap( mask ) ) ;
277 }
278
279 SetGWorld( wp , NULL ) ;
280 Rect portRect ;
281 if ( clipRgn )
282 GetRegionBounds( clipRgn , &portRect ) ;
283 else
284 GetPortBounds( wp , &portRect ) ;
285 pict = OpenPicture(&portRect);
286 if(pict)
287 {
288 RGBForeColor( &black ) ;
289 RGBBackColor( &white ) ;
290
291 if ( clipRgn )
292 SetClip( clipRgn ) ;
293
294 LockPixels( GetGWorldPixMap( wp ) ) ;
295 CopyBits(GetPortBitMapForCopyBits(wp),
296 GetPortBitMapForCopyBits(wp),
297 &portRect,
298 &portRect,
299 srcCopy,clipRgn);
300 UnlockPixels( GetGWorldPixMap( wp ) ) ;
301 ClosePicture();
302 }
303 SetGWorld( origPort , origDev ) ;
304 if ( clipRgn )
305 DisposeRgn( clipRgn ) ;
306 return pict;
307 }
308
309 void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
310 {
311 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
312 if ( bitmap.Ok() )
313 {
314 wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ;
315 if ( bmap == NULL )
316 return ;
317
318 if ( bmap->m_bitmapType == kMacBitmapTypePict )
319 {
320 info->contentType = kControlContentPictHandle ;
321 info->u.picture = MAC_WXHMETAFILE(bmap->m_hPict) ;
322 }
323 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
324 {
325 if ( (forceType == kControlContentCIconHandle || ( bmap->m_width == bmap->m_height && forceType != kControlContentPictHandle ) ) && ((bmap->m_width & 0x3) == 0) )
326 {
327 info->contentType = kControlContentCIconHandle ;
328 if ( bitmap.GetMask() )
329 {
330 info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap()) ,
331 8 , bmap->m_width ) ;
332 }
333 else
334 {
335 info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ,
336 8 , bmap->m_width ) ;
337 }
338 }
339 else
340 {
341 info->contentType = kControlContentPictHandle ;
342 if ( bitmap.GetMask() )
343 {
344 info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap() ) ) ;
345 }
346 else
347 {
348 info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ) ;
349 }
350 }
351 }
352 else if ( bmap->m_bitmapType == kMacBitmapTypeIcon )
353 {
354 info->contentType = kControlContentCIconHandle ;
355 info->u.cIconHandle = MAC_WXHICON(bmap->m_hIcon) ;
356 }
357 }
358 }
359
360 wxBitmapRefData::wxBitmapRefData()
361 : m_width(0)
362 , m_height(0)
363 , m_depth(0)
364 , m_ok(false)
365 , m_numColors(0)
366 , m_quality(0)
367 {
368 m_bitmapMask = NULL;
369 m_hBitmap = NULL ;
370 m_hPict = NULL ;
371 m_hIcon = NULL ;
372 m_bitmapType = kMacBitmapTypeUnknownType ;
373 m_hasAlpha = false;
374 }
375
376 // TODO move this to a public function of Bitmap Ref
377 static void DisposeBitmapRefData(wxBitmapRefData *data)
378 {
379 if ( !data )
380 return ;
381
382 switch (data->m_bitmapType)
383 {
384 case kMacBitmapTypePict :
385 {
386 if ( data->m_hPict )
387 {
388 KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ;
389 data->m_hPict = NULL ;
390 }
391 }
392 break ;
393 case kMacBitmapTypeGrafWorld :
394 {
395 if ( data->m_hBitmap )
396 {
397 wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ;
398 data->m_hBitmap = NULL ;
399 }
400 }
401 break ;
402 case kMacBitmapTypeIcon :
403 if ( data->m_hIcon )
404 {
405 DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ;
406 data->m_hIcon = NULL ;
407 }
408
409 default :
410 // unkown type ?
411 break ;
412 }
413
414 if (data->m_bitmapMask)
415 {
416 delete data->m_bitmapMask;
417 data->m_bitmapMask = NULL;
418 }
419 }
420
421 wxBitmapRefData::~wxBitmapRefData()
422 {
423 DisposeBitmapRefData( this ) ;
424 }
425
426 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
427 {
428 Ref(icon) ;
429 return true;
430 }
431
432 wxBitmap::wxBitmap()
433 {
434 m_refData = NULL;
435 }
436
437 wxBitmap::~wxBitmap()
438 {
439 }
440
441 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
442 {
443 m_refData = new wxBitmapRefData;
444
445 M_BITMAPDATA->m_width = the_width ;
446 M_BITMAPDATA->m_height = the_height ;
447 M_BITMAPDATA->m_depth = no_bits ;
448 M_BITMAPDATA->m_numColors = 0;
449 if ( no_bits == 1 )
450 {
451 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
452 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( the_width , the_height , no_bits ) ;
453 M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ;
454
455 CGrafPtr origPort ;
456 GDHandle origDevice ;
457
458 GetGWorld( &origPort , &origDevice ) ;
459 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
460 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
461
462 // bits is a char array
463
464 unsigned char* linestart = (unsigned char*) bits ;
465 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
466 if ( the_width % (sizeof(unsigned char) * 8) ) {
467 linesize += sizeof(unsigned char);
468 }
469
470 RGBColor colors[2] = {
471 { 0xFFFF , 0xFFFF , 0xFFFF } ,
472 { 0, 0 , 0 }
473 } ;
474
475 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
476 {
477 for ( int x = 0 ; x < the_width ; ++x )
478 {
479 int index = x / 8 ;
480 int bit = x % 8 ;
481 int mask = 1 << bit ;
482 if ( linestart[index] & mask )
483 {
484 SetCPixel( x , y , &colors[1] ) ;
485 }
486 else
487 {
488 SetCPixel( x , y , &colors[0] ) ;
489 }
490 }
491 }
492 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
493
494 SetGWorld( origPort , origDevice ) ;
495 }
496 else
497 {
498 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
499 }
500 }
501
502 wxBitmap::wxBitmap(int w, int h, int d)
503 {
504 (void)Create(w, h, d);
505 }
506
507 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
508 {
509 (void) Create(data, type, width, height, depth);
510 }
511
512 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
513 {
514 LoadFile(filename, type);
515 }
516
517 bool wxBitmap::CreateFromXpm(const char **bits)
518 {
519 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
520 wxXPMDecoder decoder;
521 wxImage img = decoder.ReadData(bits);
522 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
523 *this = wxBitmap(img);
524 return true;
525 }
526
527 wxBitmap::wxBitmap(const char **bits)
528 {
529 (void) CreateFromXpm(bits);
530 }
531
532 wxBitmap::wxBitmap(char **bits)
533 {
534 (void) CreateFromXpm((const char **)bits);
535 }
536
537 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
538 {
539 wxCHECK_MSG( Ok() &&
540 (rect.x >= 0) && (rect.y >= 0) &&
541 (rect.x+rect.width <= GetWidth()) &&
542 (rect.y+rect.height <= GetHeight()),
543 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
544
545
546 wxBitmap ret( rect.width, rect.height, GetDepth() );
547 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
548
549 GWorldPtr origPort;
550 GDHandle origDevice;
551
552 GetGWorld( &origPort, &origDevice );
553
554 // Update the subbitmaps reference data
555 wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData();
556
557 ref->m_numColors = M_BITMAPDATA->m_numColors;
558 #if wxUSE_PALETTE
559 ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette;
560 #endif // wxUSE_PALETTE
561 ref->m_bitmapType = M_BITMAPDATA->m_bitmapType;
562
563 // Copy sub region of this bitmap
564 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict)
565 {
566 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
567 }
568 else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld)
569 {
570 // Copy mask
571 if(GetMask())
572 {
573 GWorldPtr submask, mask;
574 RGBColor color;
575
576 mask = (GWorldPtr) GetMask()->GetMaskBitmap();
577 submask = wxMacCreateGWorld(rect.width, rect.height, GetMask()->GetDepth() );
578 LockPixels(GetGWorldPixMap(mask));
579 LockPixels(GetGWorldPixMap(submask));
580
581 for(int yy = 0; yy < rect.height; yy++)
582 {
583 for(int xx = 0; xx < rect.width; xx++)
584 {
585 SetGWorld(mask, NULL);
586 GetCPixel(rect.x + xx, rect.y + yy, &color);
587 SetGWorld(submask, NULL);
588 SetCPixel(xx,yy, &color);
589 }
590 }
591 UnlockPixels(GetGWorldPixMap(mask));
592 UnlockPixels(GetGWorldPixMap(submask));
593 ref->m_bitmapMask = new wxMask;
594 ref->m_bitmapMask->SetMaskBitmap(submask);
595 }
596
597 // Copy bitmap
598 if(GetHBITMAP())
599 {
600 GWorldPtr subbitmap, bitmap;
601 RGBColor color;
602
603 bitmap = (GWorldPtr) GetHBITMAP();
604 subbitmap = (GWorldPtr) ref->m_hBitmap ;
605 LockPixels(GetGWorldPixMap(bitmap));
606 LockPixels(GetGWorldPixMap(subbitmap));
607
608 for(int yy = 0; yy < rect.height; yy++)
609 {
610 for(int xx = 0; xx < rect.width; xx++)
611 {
612 SetGWorld(bitmap, NULL);
613 GetCPixel(rect.x + xx, rect.y + yy, &color);
614 SetGWorld(subbitmap, NULL);
615 SetCPixel(xx, yy, &color);
616 }
617 }
618 UnlockPixels(GetGWorldPixMap(bitmap));
619 UnlockPixels(GetGWorldPixMap(subbitmap));
620 }
621 }
622 SetGWorld( origPort, origDevice );
623
624 return ret;
625 }
626
627 bool wxBitmap::Create(int w, int h, int d)
628 {
629 UnRef();
630
631 m_refData = new wxBitmapRefData;
632
633 M_BITMAPDATA->m_width = w;
634 M_BITMAPDATA->m_height = h;
635 M_BITMAPDATA->m_depth = d;
636
637 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
638 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
639 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
640 return M_BITMAPDATA->m_ok;
641 }
642
643 int wxBitmap::GetBitmapType() const
644 {
645 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") );
646
647 return M_BITMAPDATA->m_bitmapType;
648 }
649
650 void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
651 {
652 if (!M_BITMAPDATA)
653 m_refData = new wxBitmapRefData;
654 else
655 DisposeBitmapRefData( M_BITMAPDATA ) ;
656
657 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
658 M_BITMAPDATA->m_hBitmap = bmp ;
659 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
660 }
661
662 void wxBitmap::SetHICON(WXHICON ico)
663 {
664 if (!M_BITMAPDATA)
665 m_refData = new wxBitmapRefData;
666 else
667 DisposeBitmapRefData( M_BITMAPDATA ) ;
668
669 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ;
670 M_BITMAPDATA->m_hIcon = ico ;
671 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ;
672 }
673
674 void wxBitmap::SetPict(WXHMETAFILE pict)
675 {
676 if (!M_BITMAPDATA)
677 m_refData = new wxBitmapRefData;
678 else
679 DisposeBitmapRefData( M_BITMAPDATA ) ;
680
681 M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ;
682 M_BITMAPDATA->m_hPict = pict ;
683 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ;
684 }
685
686 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
687 {
688 UnRef();
689
690 wxBitmapHandler *handler = FindHandler(type);
691
692 if ( handler )
693 {
694 m_refData = new wxBitmapRefData;
695
696 return handler->LoadFile(this, filename, type, -1, -1);
697 }
698 else
699 {
700 wxImage loadimage(filename, type);
701 if (loadimage.Ok()) {
702 *this = loadimage;
703 return true;
704 }
705 }
706 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
707 return false;
708 }
709
710 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
711 {
712 UnRef();
713
714 m_refData = new wxBitmapRefData;
715
716 wxBitmapHandler *handler = FindHandler(type);
717
718 if ( handler == NULL ) {
719 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
720
721 return false;
722 }
723
724 return handler->Create(this, data, type, width, height, depth);
725 }
726
727 wxBitmap::wxBitmap(const wxImage& image, int depth)
728 {
729 wxCHECK_RET( image.Ok(), wxT("invalid image") );
730 wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") );
731
732 m_refData = new wxBitmapRefData();
733
734 // width and height of the device-dependent bitmap
735 int width = image.GetWidth();
736 int height = image.GetHeight();
737
738 // Create picture
739
740 Create( width , height , 32 ) ;
741
742 CGrafPtr origPort ;
743 GDHandle origDevice ;
744
745 PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ;
746 LockPixels( pixMap );
747
748 GetGWorld( &origPort , &origDevice ) ;
749 SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
750
751 // Render image
752 register unsigned char* data = image.GetData();
753 char* destinationBase = GetPixBaseAddr( pixMap );
754 register unsigned char* destination = (unsigned char*) destinationBase ;
755 for (int y = 0; y < height; y++)
756 {
757 for (int x = 0; x < width; x++)
758 {
759 *destination++ = 0 ;
760 *destination++ = *data++ ;
761 *destination++ = *data++ ;
762 *destination++ = *data++ ;
763 }
764 destinationBase += ((**pixMap).rowBytes & 0x7fff);
765 destination = (unsigned char*) destinationBase ;
766 }
767 if ( image.HasAlpha() )
768 {
769 unsigned char *alpha = image.GetAlpha();
770
771 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
772 RGBColor color ;
773 wxBitmap maskBitmap ;
774
775 maskBitmap.Create( width, height, 24);
776 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
777 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
778
779 for (int y = 0; y < height; y++)
780 {
781 for (int x = 0; x < width; x++)
782 {
783 memset( &color , 255 - *alpha , sizeof( color ) );
784 SetCPixel(x,y, &color);
785
786 alpha += 1 ;
787 }
788 } // for height
789 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
790 SetMask(new wxMask( maskBitmap ));
791 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
792 }
793 else if ( image.HasMask() )
794 {
795 data = image.GetData();
796
797 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
798 RGBColor white = { 0xffff, 0xffff, 0xffff };
799 RGBColor black = { 0 , 0 , 0 };
800 wxBitmap maskBitmap ;
801
802 maskBitmap.Create( width, height, 1);
803 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
804 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
805
806 for (int y = 0; y < height; y++)
807 {
808 for (int x = 0; x < width; x++)
809 {
810 if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
811 {
812 SetCPixel(x,y, &white);
813 }
814 else {
815 SetCPixel(x,y, &black);
816 }
817 data += 3 ;
818 }
819 } // for height
820 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
821 SetMask(new wxMask( maskBitmap ));
822 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
823 }
824
825 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
826 SetGWorld( origPort, origDevice );
827 }
828
829 wxImage wxBitmap::ConvertToImage() const
830 {
831 wxImage image;
832
833 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
834
835 // create an wxImage object
836 int width = GetWidth();
837 int height = GetHeight();
838 image.Create( width, height );
839
840 unsigned char *data = image.GetData();
841
842 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
843
844 GWorldPtr origPort;
845 GDHandle origDevice;
846 RgnHandle maskRgn = NULL ;
847 GWorldPtr tempPort = NULL ;
848 int index;
849 RGBColor color;
850 // background color set to RGB(16,16,16) in consistent with wxGTK
851 unsigned char mask_r=16, mask_g=16, mask_b=16;
852 SInt16 r,g,b;
853 wxMask *mask = GetMask();
854
855 GetGWorld( &origPort, &origDevice );
856 if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
857 {
858 tempPort = wxMacCreateGWorld( width , height , -1) ;
859 }
860 else
861 {
862 tempPort = (GWorldPtr) GetHBITMAP() ;
863 }
864 LockPixels(GetGWorldPixMap(tempPort));
865 SetGWorld( tempPort, NULL);
866 if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon )
867 {
868 Rect bitmaprect = { 0 , 0 , height, width };
869 if ( GetBitmapType() == kMacBitmapTypeIcon )
870 {
871 ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ;
872 maskRgn = NewRgn() ;
873 BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ;
874 }
875 else
876 ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ;
877 }
878 // Copy data into image
879 index = 0;
880 for (int yy = 0; yy < height; yy++)
881 {
882 for (int xx = 0; xx < width; xx++)
883 {
884 GetCPixel(xx,yy, &color);
885 r = ((color.red ) >> 8);
886 g = ((color.green ) >> 8);
887 b = ((color.blue ) >> 8);
888 data[index ] = r;
889 data[index + 1] = g;
890 data[index + 2] = b;
891 if ( maskRgn )
892 {
893 Point pt ;
894 pt.h = xx ;
895 pt.v = yy ;
896 if ( !PtInRgn( pt , maskRgn ) )
897 {
898 data[index ] = mask_r;
899 data[index + 1] = mask_g;
900 data[index + 2] = mask_b;
901 }
902 }
903 else
904 {
905 if (mask)
906 {
907 if (mask->PointMasked(xx,yy))
908 {
909 data[index ] = mask_r;
910 data[index + 1] = mask_g;
911 data[index + 2] = mask_b;
912 }
913 }
914 }
915 index += 3;
916 }
917 }
918 if (mask || maskRgn )
919 {
920 image.SetMaskColour( mask_r, mask_g, mask_b );
921 image.SetMask( true );
922 }
923
924 // Free resources
925 UnlockPixels(GetGWorldPixMap( tempPort ));
926 SetGWorld(origPort, origDevice);
927 if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
928 {
929 wxMacDestroyGWorld( tempPort ) ;
930 }
931 if ( maskRgn )
932 {
933 DisposeRgn( maskRgn ) ;
934 }
935
936 return image;
937 }
938
939
940 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
941 const wxPalette *palette) const
942 {
943 wxBitmapHandler *handler = FindHandler(type);
944
945 if ( handler )
946 {
947 return handler->SaveFile(this, filename, type, palette);
948 }
949 else
950 {
951 wxImage image = ConvertToImage();
952
953 return image.SaveFile(filename, type);
954 }
955
956 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
957 return false;
958 }
959
960 bool wxBitmap::IsOk() const
961 {
962 return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
963 }
964
965 int wxBitmap::GetHeight() const
966 {
967 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
968
969 return M_BITMAPDATA->m_height;
970 }
971
972 int wxBitmap::GetWidth() const
973 {
974 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
975
976 return M_BITMAPDATA->m_width;
977 }
978
979 int wxBitmap::GetDepth() const
980 {
981 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
982
983 return M_BITMAPDATA->m_depth;
984 }
985
986 int wxBitmap::GetQuality() const
987 {
988 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
989
990 return M_BITMAPDATA->m_quality;
991 }
992
993 wxMask *wxBitmap::GetMask() const
994 {
995 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
996
997 return M_BITMAPDATA->m_bitmapMask;
998 }
999
1000 void wxBitmap::SetWidth(int w)
1001 {
1002 AllocExclusive();
1003 M_BITMAPDATA->m_width = w;
1004 }
1005
1006 void wxBitmap::SetHeight(int h)
1007 {
1008 AllocExclusive();
1009 M_BITMAPDATA->m_height = h;
1010 }
1011
1012 void wxBitmap::SetDepth(int d)
1013 {
1014 AllocExclusive();
1015 M_BITMAPDATA->m_depth = d;
1016 }
1017
1018 void wxBitmap::SetQuality(int q)
1019 {
1020 if (!M_BITMAPDATA)
1021 m_refData = new wxBitmapRefData;
1022
1023 M_BITMAPDATA->m_quality = q;
1024 }
1025
1026 void wxBitmap::SetOk(bool isOk)
1027 {
1028 if (!M_BITMAPDATA)
1029 m_refData = new wxBitmapRefData;
1030
1031 M_BITMAPDATA->m_ok = isOk;
1032 }
1033
1034 #if wxUSE_PALETTE
1035 wxPalette *wxBitmap::GetPalette() const
1036 {
1037 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1038
1039 return &M_BITMAPDATA->m_bitmapPalette;
1040 }
1041
1042 void wxBitmap::SetPalette(const wxPalette& palette)
1043 {
1044 if (!M_BITMAPDATA)
1045 m_refData = new wxBitmapRefData;
1046
1047 M_BITMAPDATA->m_bitmapPalette = palette ;
1048 }
1049 #endif // wxUSE_PALETTE
1050
1051 void wxBitmap::SetMask(wxMask *mask)
1052 {
1053 if (!M_BITMAPDATA)
1054 m_refData = new wxBitmapRefData;
1055
1056 // Remove existing mask if there is one.
1057 delete M_BITMAPDATA->m_bitmapMask;
1058
1059 M_BITMAPDATA->m_bitmapMask = mask ;
1060 }
1061
1062 WXHBITMAP wxBitmap::GetHBITMAP() const
1063 {
1064 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1065
1066 return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
1067 }
1068
1069 WXHMETAFILE wxBitmap::GetPict( bool *created ) const
1070 {
1071 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1072
1073 PicHandle picture = NULL ; // This is the returned picture
1074 if ( created )
1075 (*created) = false ;
1076 // If bitmap already in Pict format return pointer
1077 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) {
1078 return M_BITMAPDATA->m_hPict;
1079 }
1080 else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) {
1081 // Invalid bitmap
1082 return NULL;
1083 }
1084 else
1085 {
1086 if ( GetMask() )
1087 {
1088 picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1089 }
1090 else
1091 {
1092 picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
1093 }
1094 if ( created && picture )
1095 (*created) = true ;
1096 }
1097 return picture ;
1098 }
1099
1100 /*
1101 * wxMask
1102 */
1103
1104 wxMask::wxMask()
1105 : m_maskBitmap(NULL)
1106 {
1107 }
1108
1109 // Construct a mask from a bitmap and a colour indicating
1110 // the transparent area
1111 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1112 : m_maskBitmap(NULL)
1113 {
1114 Create(bitmap, colour);
1115 }
1116
1117 // Construct a mask from a bitmap and a palette index indicating
1118 // the transparent area
1119 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1120 : m_maskBitmap(NULL)
1121 {
1122 Create(bitmap, paletteIndex);
1123 }
1124
1125 // Construct a mask from a mono bitmap (copies the bitmap).
1126 wxMask::wxMask(const wxBitmap& bitmap)
1127 : m_maskBitmap(NULL)
1128 {
1129 Create(bitmap);
1130 }
1131
1132 wxMask::~wxMask()
1133 {
1134 if ( m_maskBitmap )
1135 {
1136 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1137 m_maskBitmap = NULL ;
1138 }
1139 }
1140
1141 // Create a mask from a mono bitmap (copies the bitmap).
1142 bool wxMask::Create(const wxBitmap& bitmap)
1143 {
1144 if ( m_maskBitmap )
1145 {
1146 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1147 m_maskBitmap = NULL ;
1148 }
1149 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1150 wxT("Cannot create mask from this bitmap type (TODO)"));
1151 // other types would require a temporary bitmap. not yet implemented
1152
1153 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap"));
1154
1155 m_depth = bitmap.GetDepth() ;
1156 m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), bitmap.GetDepth() );
1157 Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() };
1158
1159 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1160 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1161 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()),
1162 GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap),
1163 &rect, &rect, srcCopy, 0);
1164 UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1165 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1166
1167 return false;
1168 }
1169
1170 // Create a mask from a bitmap and a palette index indicating
1171 // the transparent area
1172 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1173 {
1174 // TODO
1175 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1176 return false;
1177 }
1178
1179 // Create a mask from a bitmap and a colour indicating
1180 // the transparent area
1181 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1182 {
1183 if ( m_maskBitmap )
1184 {
1185 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1186 m_maskBitmap = NULL ;
1187 }
1188 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1189 wxT("Cannot create mask from this bitmap type (TODO)"));
1190 // other types would require a temporary bitmap. not yet implemented
1191
1192 wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));
1193
1194 m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
1195 m_depth = 1 ;
1196 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap ) );
1197 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) );
1198 RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel());
1199
1200 // this is not very efficient, but I can't think
1201 // of a better way of doing it
1202 CGrafPtr origPort ;
1203 GDHandle origDevice ;
1204 RGBColor col;
1205 RGBColor colors[2] = {
1206 { 0xFFFF, 0xFFFF, 0xFFFF },
1207 { 0, 0, 0 }};
1208
1209 GetGWorld( &origPort , &origDevice ) ;
1210 for (int w = 0; w < bitmap.GetWidth(); w++)
1211 {
1212 for (int h = 0; h < bitmap.GetHeight(); h++)
1213 {
1214 SetGWorld( (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ;
1215 GetCPixel( w , h , &col ) ;
1216 SetGWorld( (GWorldPtr) m_maskBitmap , NULL ) ;
1217 if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
1218 {
1219 SetCPixel( w , h , &colors[0] ) ;
1220 }
1221 else
1222 {
1223 SetCPixel( w , h , &colors[1] ) ;
1224 }
1225 }
1226 }
1227 UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
1228 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ) ;
1229 SetGWorld( origPort , origDevice ) ;
1230
1231 return true;
1232 }
1233
1234 bool wxMask::PointMasked(int x, int y)
1235 {
1236 GWorldPtr origPort;
1237 GDHandle origDevice;
1238 RGBColor color;
1239 bool masked = true;
1240
1241 GetGWorld( &origPort, &origDevice);
1242
1243 //Set port to mask and see if it masked (1) or not ( 0 )
1244 SetGWorld( (GWorldPtr) m_maskBitmap, NULL);
1245 LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1246 GetCPixel(x,y, &color);
1247 masked = !(color.red == 0 && color.green == 0 && color.blue == 0);
1248 UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1249
1250 SetGWorld( origPort, origDevice);
1251
1252 return masked;
1253 }
1254
1255 /*
1256 * wxBitmapHandler
1257 */
1258
1259 wxBitmapHandler::~wxBitmapHandler()
1260 {
1261 }
1262
1263 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1264 {
1265 return false;
1266 }
1267
1268 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1269 int desiredWidth, int desiredHeight)
1270 {
1271 return false;
1272 }
1273
1274 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1275 {
1276 return false;
1277 }
1278
1279 /*
1280 * Standard handlers
1281 */
1282
1283 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1284 {
1285 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1286 public:
1287 inline wxPICTResourceHandler()
1288 {
1289 m_name = wxT("Macintosh Pict resource");
1290 m_extension = wxEmptyString;
1291 m_type = wxBITMAP_TYPE_PICT_RESOURCE;
1292 };
1293
1294 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1295 int desiredWidth, int desiredHeight);
1296 };
1297 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1298
1299 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1300 int desiredWidth, int desiredHeight)
1301 {
1302 Str255 theName ;
1303 wxMacStringToPascal( name , theName ) ;
1304
1305 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1306 if ( thePict )
1307 {
1308 PictInfo theInfo ;
1309
1310 GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
1311 DetachResource( (Handle) thePict ) ;
1312 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
1313 M_BITMAPHANDLERDATA->m_hPict = thePict ;
1314 M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ;
1315 M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;
1316
1317 M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
1318 M_BITMAPHANDLERDATA->m_ok = true ;
1319 M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
1320 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1321 // M_BITMAPHANDLERDATA->m_quality;
1322 return true ;
1323 }
1324 return false ;
1325 }
1326
1327 void wxBitmap::InitStandardHandlers()
1328 {
1329 AddHandler(new wxPICTResourceHandler) ;
1330 AddHandler(new wxICONResourceHandler) ;
1331 }
1332
1333 // ----------------------------------------------------------------------------
1334 // raw bitmap access support
1335 // ----------------------------------------------------------------------------
1336
1337 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1338 {
1339 if ( !Ok() )
1340 {
1341 // no bitmap, no data (raw or otherwise)
1342 return NULL;
1343 }
1344
1345 if ( M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld )
1346 {
1347 wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") );
1348
1349 return NULL;
1350 }
1351
1352 GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
1353 PixMapHandle hPixMap = GetGWorldPixMap(gworld);
1354 wxCHECK_MSG( hPixMap && *hPixMap, NULL,
1355 _T("GetRawData(): failed to get PixMap from GWorld?") );
1356
1357 wxCHECK_MSG( (*hPixMap)->pixelSize == bpp, NULL,
1358 _T("GetRawData(): pixel format mismatch") );
1359
1360 if ( !LockPixels(hPixMap) )
1361 {
1362 wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") );
1363
1364 return NULL;
1365 }
1366
1367 data.m_width = GetWidth();
1368 data.m_height = GetHeight();
1369 data.m_stride = (*hPixMap)->rowBytes & 0x7fff;
1370
1371 M_BITMAPDATA->m_hasAlpha = false;
1372
1373 return GetPixBaseAddr(hPixMap);
1374 }
1375
1376 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1377 {
1378 if ( !Ok() )
1379 return;
1380
1381 if ( M_BITMAPDATA->m_hasAlpha )
1382 {
1383 wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase;
1384
1385 int w = data.GetWidth(),
1386 h = data.GetHeight();
1387
1388 wxBitmap bmpMask(GetWidth(), GetHeight(), 32);
1389 wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h));
1390 wxAlphaPixelData::Iterator pMask(dataMask),
1391 p(data);
1392 for ( int y = 0; y < h; y++ )
1393 {
1394 wxAlphaPixelData::Iterator rowStartMask = pMask,
1395 rowStart = p;
1396
1397 for ( int x = 0; x < w; x++ )
1398 {
1399 const wxAlphaPixelData::Iterator::ChannelType
1400 alpha = p.Alpha();
1401
1402 pMask.Red() = alpha;
1403 pMask.Green() = alpha;
1404 pMask.Blue() = alpha;
1405
1406 ++p;
1407 ++pMask;
1408 }
1409
1410 p = rowStart;
1411 p.OffsetY(data, 1);
1412
1413 pMask = rowStartMask;
1414 pMask.OffsetY(dataMask, 1);
1415 }
1416
1417 SetMask(new wxMask(bmpMask));
1418 }
1419
1420 GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
1421 PixMapHandle hPixMap = GetGWorldPixMap(gworld);
1422 if ( hPixMap )
1423 {
1424 UnlockPixels(hPixMap);
1425 }
1426 }
1427
1428 void wxBitmap::UseAlpha()
1429 {
1430 // remember that we are using alpha channel, we'll need to create a proper
1431 // mask in UngetRawData()
1432 M_BITMAPDATA->m_hasAlpha = true;
1433 }