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