]> git.saurik.com Git - wxWidgets.git/blob - src/mac/bitmap.cpp
fixed inheritance info
[wxWidgets.git] / src / mac / 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 = 0 ;// someRedValue;
63 (**newColors).ctTable[index].rgb.green = 0 ; // someGreenValue;
64 (**newColors).ctTable[index].rgb.blue = 0 ; // 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 LockPixels(GetGWorldPixMap(mask) ) ;
222 CopyBits(GetPortBitMapForCopyBits(mask) ,
223 &(**icon).iconBMap , &imageBounds , &imageBounds, srcCopy , nil ) ;
224 CopyBits(GetPortBitMapForCopyBits(mask) ,
225 &(**icon).iconMask , &imageBounds , &imageBounds, srcCopy , nil ) ;
226 UnlockPixels(GetGWorldPixMap( mask ) ) ;
227 }
228 else
229 {
230 LockPixels(GetGWorldPixMap(image));
231 CopyBits(GetPortBitMapForCopyBits(image) ,
232 &(**icon).iconBMap , &imageBounds , &imageBounds, srcCopy , nil ) ;
233 CopyBits(GetPortBitMapForCopyBits(image) ,
234 &(**icon).iconMask , &imageBounds , &imageBounds, srcCopy , nil ) ;
235 UnlockPixels(GetGWorldPixMap(image));
236 }
237
238 (**icon).iconMask.baseAddr = NULL ;
239 (**icon).iconBMap.baseAddr = NULL ;
240 (**icon).iconPMap.baseAddr = NULL ;
241 HUnlock((Handle)icon) ;
242 SetGWorld(saveWorld,saveHandle);
243
244 return icon;
245 }
246
247 PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask)
248 {
249 CGrafPtr origPort ;
250 GDHandle origDev ;
251
252 PicHandle pict;
253
254 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
255 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
256
257 GetGWorld( &origPort , &origDev ) ;
258
259 RgnHandle clipRgn = NULL ;
260
261 if ( mask )
262 {
263 clipRgn = NewRgn() ;
264 LockPixels( GetGWorldPixMap( mask ) ) ;
265 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ;
266 UnlockPixels( GetGWorldPixMap( mask ) ) ;
267 }
268
269 SetGWorld( wp , NULL ) ;
270 Rect portRect ;
271 GetPortBounds( wp , &portRect ) ;
272
273 pict = OpenPicture(&portRect);
274 if(pict)
275 {
276 RGBForeColor( &black ) ;
277 RGBBackColor( &white ) ;
278
279 LockPixels( GetGWorldPixMap( wp ) ) ;
280 CopyBits(GetPortBitMapForCopyBits(wp),
281 GetPortBitMapForCopyBits(wp),
282 &portRect,
283 &portRect,
284 srcCopy,clipRgn);
285 UnlockPixels( GetGWorldPixMap( wp ) ) ;
286 ClosePicture();
287 }
288 SetGWorld( origPort , origDev ) ;
289 return pict;
290 }
291
292 wxBitmapRefData::wxBitmapRefData()
293 {
294 m_ok = FALSE;
295 m_width = 0;
296 m_height = 0;
297 m_depth = 0;
298 m_quality = 0;
299 m_numColors = 0;
300 m_bitmapMask = NULL;
301 m_hBitmap = NULL ;
302 m_hPict = NULL ;
303 m_hIcon = NULL ;
304 m_bitmapType = kMacBitmapTypeUnknownType ;
305 }
306
307 // TODO move this do a public function of Bitmap Ref
308 static void DisposeBitmapRefData(wxBitmapRefData *data)
309 {
310 switch (data->m_bitmapType)
311 {
312 case kMacBitmapTypePict :
313 {
314 if ( data->m_hPict )
315 {
316 KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ;
317 data->m_hPict = NULL ;
318 }
319 }
320 break ;
321 case kMacBitmapTypeGrafWorld :
322 {
323 if ( data->m_hBitmap )
324 {
325 wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ;
326 data->m_hBitmap = NULL ;
327 }
328 }
329 break ;
330 case kMacBitmapTypeIcon :
331 if ( data->m_hIcon )
332 {
333 DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ;
334 data->m_hIcon = NULL ;
335 }
336
337 default :
338 // unkown type ?
339 break ;
340 }
341
342 if (data->m_bitmapMask)
343 {
344 delete data->m_bitmapMask;
345 data->m_bitmapMask = NULL;
346 }
347 }
348
349 wxBitmapRefData::~wxBitmapRefData()
350 {
351 DisposeBitmapRefData( this ) ;
352 }
353
354 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
355 {
356 Ref(icon) ;
357 return true;
358 }
359
360 wxBitmap::wxBitmap()
361 {
362 m_refData = NULL;
363 }
364
365 wxBitmap::~wxBitmap()
366 {
367 }
368
369 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
370 {
371 m_refData = new wxBitmapRefData;
372
373 M_BITMAPDATA->m_width = the_width ;
374 M_BITMAPDATA->m_height = the_height ;
375 M_BITMAPDATA->m_depth = no_bits ;
376 M_BITMAPDATA->m_numColors = 0;
377 if ( no_bits == 1 )
378 {
379 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
380 MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) = wxMacCreateGWorld( the_width , the_height , no_bits ) ;
381 M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ;
382
383 CGrafPtr origPort ;
384 GDHandle origDevice ;
385
386 GetGWorld( &origPort , &origDevice ) ;
387 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
388 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
389
390 // bits is a char array
391
392 unsigned char* linestart = (unsigned char*) bits ;
393 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
394 if ( the_width % (sizeof(unsigned char) * 8) ) {
395 linesize += sizeof(unsigned char);
396 }
397
398 RGBColor colors[2] = {
399 { 0xFFFF , 0xFFFF , 0xFFFF } ,
400 { 0, 0 , 0 }
401 } ;
402
403 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
404 {
405 for ( int x = 0 ; x < the_width ; ++x )
406 {
407 int index = x / 8 ;
408 int bit = x % 8 ;
409 int mask = 1 << bit ;
410 if ( linestart[index] & mask )
411 {
412 SetCPixel( x , y , &colors[1] ) ;
413 }
414 else
415 {
416 SetCPixel( x , y , &colors[0] ) ;
417 }
418 }
419 }
420 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
421
422 SetGWorld( origPort , origDevice ) ;
423 }
424 else
425 {
426 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
427 }
428 }
429
430 wxBitmap::wxBitmap(int w, int h, int d)
431 {
432 (void)Create(w, h, d);
433 }
434
435 wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
436 {
437 (void) Create(data, type, width, height, depth);
438 }
439
440 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
441 {
442 LoadFile(filename, type);
443 }
444
445 bool wxBitmap::CreateFromXpm(const char **bits)
446 {
447 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
448 wxXPMDecoder decoder;
449 wxImage img = decoder.ReadData(bits);
450 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
451 *this = wxBitmap(img);
452 return TRUE;
453 }
454
455 wxBitmap::wxBitmap(const char **bits)
456 {
457 (void) CreateFromXpm(bits);
458 }
459
460 wxBitmap::wxBitmap(char **bits)
461 {
462 (void) CreateFromXpm((const char **)bits);
463 }
464
465 wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
466 {
467 wxCHECK_MSG( Ok() &&
468 (rect.x >= 0) && (rect.y >= 0) &&
469 (rect.x+rect.width <= GetWidth()) &&
470 (rect.y+rect.height <= GetHeight()),
471 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
472
473
474 wxBitmap ret( rect.width, rect.height, GetDepth() );
475 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
476
477 GWorldPtr origPort;
478 GDHandle origDevice;
479
480 GetGWorld( &origPort, &origDevice );
481
482 // Update the subbitmaps reference data
483 wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData();
484
485 ref->m_numColors = M_BITMAPDATA->m_numColors;
486 ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette;
487 ref->m_bitmapType = M_BITMAPDATA->m_bitmapType;
488
489 // Copy sub region of this bitmap
490 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict)
491 {
492 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
493 }
494 else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld)
495 {
496 // Copy mask
497 if(GetMask())
498 {
499 GWorldPtr submask, mask;
500 RGBColor color;
501
502 mask = (GWorldPtr) GetMask()->GetMaskBitmap();
503 submask = wxMacCreateGWorld(rect.width, rect.height, 1);
504 LockPixels(GetGWorldPixMap(mask));
505 LockPixels(GetGWorldPixMap(submask));
506
507 for(int yy = 0; yy < rect.height; yy++)
508 {
509 for(int xx = 0; xx < rect.width; xx++)
510 {
511 SetGWorld(mask, NULL);
512 GetCPixel(rect.x + xx, rect.y + yy, &color);
513 SetGWorld(submask, NULL);
514 SetCPixel(xx,yy, &color);
515 }
516 }
517 UnlockPixels(GetGWorldPixMap(mask));
518 UnlockPixels(GetGWorldPixMap(submask));
519 ref->m_bitmapMask = new wxMask;
520 ref->m_bitmapMask->SetMaskBitmap(submask);
521 }
522
523 // Copy bitmap
524 if(GetHBITMAP())
525 {
526 GWorldPtr subbitmap, bitmap;
527 RGBColor color;
528
529 bitmap = (GWorldPtr) GetHBITMAP();
530 subbitmap = (GWorldPtr) ref->m_hBitmap ;
531 LockPixels(GetGWorldPixMap(bitmap));
532 LockPixels(GetGWorldPixMap(subbitmap));
533
534 for(int yy = 0; yy < rect.height; yy++)
535 {
536 for(int xx = 0; xx < rect.width; xx++)
537 {
538 SetGWorld(bitmap, NULL);
539 GetCPixel(rect.x + xx, rect.y + yy, &color);
540 SetGWorld(subbitmap, NULL);
541 SetCPixel(xx, yy, &color);
542 }
543 }
544 UnlockPixels(GetGWorldPixMap(bitmap));
545 UnlockPixels(GetGWorldPixMap(subbitmap));
546 }
547 }
548 SetGWorld( origPort, origDevice );
549
550 return ret;
551 }
552
553 bool wxBitmap::Create(int w, int h, int d)
554 {
555 UnRef();
556
557 m_refData = new wxBitmapRefData;
558
559 M_BITMAPDATA->m_width = w;
560 M_BITMAPDATA->m_height = h;
561 M_BITMAPDATA->m_depth = d;
562
563 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
564 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
565 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
566 return M_BITMAPDATA->m_ok;
567 }
568
569 int wxBitmap::GetBitmapType() const
570 {
571 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") );
572
573 return M_BITMAPDATA->m_bitmapType;
574 }
575
576 void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
577 {
578 DisposeBitmapRefData( M_BITMAPDATA ) ;
579
580 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
581 M_BITMAPDATA->m_hBitmap = bmp ;
582 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
583 }
584
585 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
586 {
587 UnRef();
588
589 wxBitmapHandler *handler = FindHandler(type);
590
591 if ( handler )
592 {
593 m_refData = new wxBitmapRefData;
594
595 return handler->LoadFile(this, filename, type, -1, -1);
596 }
597 else
598 {
599 wxImage loadimage(filename, type);
600 if (loadimage.Ok()) {
601 *this = loadimage;
602 return true;
603 }
604 }
605 wxLogWarning("no bitmap handler for type %d defined.", type);
606 return false;
607 }
608
609 bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
610 {
611 UnRef();
612
613 m_refData = new wxBitmapRefData;
614
615 wxBitmapHandler *handler = FindHandler(type);
616
617 if ( handler == NULL ) {
618 wxLogWarning("no bitmap handler for type %d defined.", type);
619
620 return FALSE;
621 }
622
623 return handler->Create(this, data, type, width, height, depth);
624 }
625
626 wxBitmap::wxBitmap(const wxImage& image, int depth)
627 {
628 wxCHECK_RET( image.Ok(), wxT("invalid image") )
629 wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") )
630
631 m_refData = new wxBitmapRefData();
632
633 // width and height of the device-dependent bitmap
634 int width = image.GetWidth();
635 int height = image.GetHeight();
636
637 // Create picture
638
639 Create( width , height , 32 ) ;
640
641 CGrafPtr origPort ;
642 GDHandle origDevice ;
643
644 PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ;
645 LockPixels( pixMap );
646
647 GetGWorld( &origPort , &origDevice ) ;
648 SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
649
650 // Render image
651 RGBColor colorRGB ;
652
653 register unsigned char* data = image.GetData();
654 char* destinationBase = GetPixBaseAddr( pixMap );
655 register unsigned char* destination = (unsigned char*) destinationBase ;
656 for (int y = 0; y < height; y++)
657 {
658 for (int x = 0; x < width; x++)
659 {
660 *destination++ = 0 ;
661 *destination++ = *data++ ;
662 *destination++ = *data++ ;
663 *destination++ = *data++ ;
664 }
665 destinationBase += ((**pixMap).rowBytes & 0x7fff);
666 destination = (unsigned char*) destinationBase ;
667 }
668 if ( image.HasMask() )
669 {
670 data = image.GetData();
671
672 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
673 RGBColor white = { 0xffff, 0xffff, 0xffff };
674 RGBColor black = { 0 , 0 , 0 };
675 wxBitmap maskBitmap ;
676
677 maskBitmap.Create( width, height, 1);
678 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
679 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
680
681 for (int y = 0; y < height; y++)
682 {
683 for (int x = 0; x < width; x++)
684 {
685 if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
686 {
687 SetCPixel(x,y, &white);
688 }
689 else {
690 SetCPixel(x,y, &black);
691 }
692 data += 3 ;
693 }
694 } // for height
695 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
696 SetMask(new wxMask( maskBitmap ));
697 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
698 }
699
700 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
701 SetGWorld( origPort, origDevice );
702 }
703
704 wxImage wxBitmap::ConvertToImage() const
705 {
706 wxImage image;
707
708 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
709
710 // create an wxImage object
711 int width = GetWidth();
712 int height = GetHeight();
713 image.Create( width, height );
714
715 unsigned char *data = image.GetData();
716
717 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
718
719 GWorldPtr origPort;
720 GDHandle origDevice;
721 int index;
722 RGBColor color;
723 // background color set to RGB(16,16,16) in consistent with wxGTK
724 unsigned char mask_r=16, mask_g=16, mask_b=16;
725 SInt16 r,g,b;
726 wxMask *mask = GetMask();
727
728 GetGWorld( &origPort, &origDevice );
729 LockPixels(GetGWorldPixMap( (GWorldPtr) GetHBITMAP()));
730 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
731
732 // Copy data into image
733 index = 0;
734 for (int yy = 0; yy < height; yy++)
735 {
736 for (int xx = 0; xx < width; xx++)
737 {
738 GetCPixel(xx,yy, &color);
739 r = ((color.red ) >> 8);
740 g = ((color.green ) >> 8);
741 b = ((color.blue ) >> 8);
742 data[index ] = r;
743 data[index + 1] = g;
744 data[index + 2] = b;
745 if (mask)
746 {
747 if (mask->PointMasked(xx,yy))
748 {
749 data[index ] = mask_r;
750 data[index + 1] = mask_g;
751 data[index + 2] = mask_b;
752 }
753 }
754 index += 3;
755 }
756 }
757 if (mask)
758 {
759 image.SetMaskColour( mask_r, mask_g, mask_b );
760 image.SetMask( true );
761 }
762
763 // Free resources
764 UnlockPixels(GetGWorldPixMap( (GWorldPtr) GetHBITMAP()));
765 SetGWorld(origPort, origDevice);
766
767 return image;
768 }
769
770
771 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
772 const wxPalette *palette) const
773 {
774 wxBitmapHandler *handler = FindHandler(type);
775
776 if ( handler )
777 {
778 return handler->SaveFile(this, filename, type, palette);
779 }
780 else
781 {
782 wxImage image = ConvertToImage();
783
784 return image.SaveFile(filename, type);
785 }
786
787 wxLogWarning("no bitmap handler for type %d defined.", type);
788 return false;
789 }
790
791 bool wxBitmap::Ok() const
792 {
793 return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
794 }
795
796 int wxBitmap::GetHeight() const
797 {
798 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
799
800 return M_BITMAPDATA->m_height;
801 }
802
803 int wxBitmap::GetWidth() const
804 {
805 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
806
807 return M_BITMAPDATA->m_width;
808 }
809
810 int wxBitmap::GetDepth() const
811 {
812 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
813
814 return M_BITMAPDATA->m_depth;
815 }
816
817 int wxBitmap::GetQuality() const
818 {
819 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
820
821 return M_BITMAPDATA->m_quality;
822 }
823
824 wxMask *wxBitmap::GetMask() const
825 {
826 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
827
828 return M_BITMAPDATA->m_bitmapMask;
829 }
830
831 void wxBitmap::SetWidth(int w)
832 {
833 if (!M_BITMAPDATA)
834 m_refData = new wxBitmapRefData;
835
836 M_BITMAPDATA->m_width = w;
837 }
838
839 void wxBitmap::SetHeight(int h)
840 {
841 if (!M_BITMAPDATA)
842 m_refData = new wxBitmapRefData;
843
844 M_BITMAPDATA->m_height = h;
845 }
846
847 void wxBitmap::SetDepth(int d)
848 {
849 if (!M_BITMAPDATA)
850 m_refData = new wxBitmapRefData;
851
852 M_BITMAPDATA->m_depth = d;
853 }
854
855 void wxBitmap::SetQuality(int q)
856 {
857 if (!M_BITMAPDATA)
858 m_refData = new wxBitmapRefData;
859
860 M_BITMAPDATA->m_quality = q;
861 }
862
863 void wxBitmap::SetOk(bool isOk)
864 {
865 if (!M_BITMAPDATA)
866 m_refData = new wxBitmapRefData;
867
868 M_BITMAPDATA->m_ok = isOk;
869 }
870
871 wxPalette *wxBitmap::GetPalette() const
872 {
873 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
874
875 return &M_BITMAPDATA->m_bitmapPalette;
876 }
877
878 void wxBitmap::SetPalette(const wxPalette& palette)
879 {
880 if (!M_BITMAPDATA)
881 m_refData = new wxBitmapRefData;
882
883 M_BITMAPDATA->m_bitmapPalette = palette ;
884 }
885
886 void wxBitmap::SetMask(wxMask *mask)
887 {
888 if (!M_BITMAPDATA)
889 m_refData = new wxBitmapRefData;
890
891 // Remove existing mask if there is one.
892 if (M_BITMAPDATA->m_bitmapMask)
893 delete M_BITMAPDATA->m_bitmapMask;
894
895 M_BITMAPDATA->m_bitmapMask = mask ;
896 }
897
898 WXHBITMAP wxBitmap::GetHBITMAP() const
899 {
900 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
901
902 return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
903 }
904
905 WXHMETAFILE wxBitmap::GetPict() const
906 {
907 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
908
909 PicHandle picture; // This is the returned picture
910
911 // If bitmap already in Pict format return pointer
912 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) {
913 return M_BITMAPDATA->m_hPict;
914 }
915 else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) {
916 // Invalid bitmap
917 return NULL;
918 }
919
920 RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
921 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
922 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
923 CGrafPtr origPort;
924 GDHandle origDev ;
925 wxMask *mask;
926 Rect portRect ;
927
928 GetPortBounds( (GWorldPtr) GetHBITMAP() , &portRect ) ;
929 int width = portRect.right - portRect.left ;
930 int height = portRect.bottom - portRect.top ;
931
932 LockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ;
933 GetGWorld( &origPort , &origDev ) ;
934
935 mask = GetMask();
936
937 SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
938
939 picture = OpenPicture(&portRect); // open a picture, this disables drawing
940 if(!picture) {
941 return NULL;
942 }
943
944 if( mask )
945 {
946 #ifdef __DARWIN__
947 RGBColor trans = white;
948 #else
949 RGBBackColor( &gray );
950 EraseRect( &portRect );
951 RGBColor trans = gray;
952 #endif
953 RGBForeColor( &black ) ;
954 RGBBackColor( &white ) ;
955 PenMode(transparent);
956
957 for ( int y = 0 ; y < height ; ++y )
958 {
959 for( int x = 0 ; x < width ; ++x )
960 {
961 if ( !mask->PointMasked(x,y) )
962 {
963 RGBColor col ;
964
965 GetCPixel( x + portRect.left , y + portRect.top , &col ) ;
966 SetCPixel( x + portRect.left , y + portRect.top , &col ) ;
967 }
968 else {
969 // With transparency this sets a blank pixel
970 SetCPixel( x + portRect.left , y + portRect.top , &trans);
971 }
972 }
973 }
974 }
975 else
976 {
977 RGBBackColor( &gray ) ;
978 EraseRect(&portRect);
979 RGBForeColor( &black ) ;
980 RGBBackColor( &white ) ;
981
982 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()),
983 // src PixMap - we copy image over itself -
984 GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()),
985 // dst PixMap - no drawing occurs
986 &portRect, // srcRect - it will be recorded and compressed -
987 &portRect, // dstRect - into the picture that is open -
988 srcCopy,NULL); // copyMode and no clip region
989 }
990 ClosePicture(); // We are done recording the picture
991 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ;
992 SetGWorld( origPort , origDev ) ;
993
994 return picture; // return our groovy pict handle
995 }
996
997 /*
998 * wxMask
999 */
1000
1001 wxMask::wxMask()
1002 {
1003 m_maskBitmap = 0;
1004 }
1005
1006 // Construct a mask from a bitmap and a colour indicating
1007 // the transparent area
1008 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1009 {
1010 m_maskBitmap = 0;
1011 Create(bitmap, colour);
1012 }
1013
1014 // Construct a mask from a bitmap and a palette index indicating
1015 // the transparent area
1016 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1017 {
1018 m_maskBitmap = 0;
1019 Create(bitmap, paletteIndex);
1020 }
1021
1022 // Construct a mask from a mono bitmap (copies the bitmap).
1023 wxMask::wxMask(const wxBitmap& bitmap)
1024 {
1025 m_maskBitmap = 0;
1026 Create(bitmap);
1027 }
1028
1029 wxMask::~wxMask()
1030 {
1031 if ( m_maskBitmap )
1032 {
1033 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1034 m_maskBitmap = NULL ;
1035 }
1036 }
1037
1038 // Create a mask from a mono bitmap (copies the bitmap).
1039 bool wxMask::Create(const wxBitmap& bitmap)
1040 {
1041 if ( m_maskBitmap )
1042 {
1043 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1044 m_maskBitmap = NULL ;
1045 }
1046 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1047 wxT("Cannot create mask from this bitmap type (TODO)"));
1048 // other types would require a temporary bitmap. not yet implemented
1049
1050 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap"));
1051
1052 wxCHECK_MSG(bitmap.GetDepth() == 1, false,
1053 wxT("Cannot create mask from colour bitmap"));
1054
1055 m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), 1);
1056 Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() };
1057
1058 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1059 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1060 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()),
1061 GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap),
1062 &rect, &rect, srcCopy, 0);
1063 UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1064 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1065
1066 return FALSE;
1067 }
1068
1069 // Create a mask from a bitmap and a palette index indicating
1070 // the transparent area
1071 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1072 {
1073 // TODO
1074 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1075 return FALSE;
1076 }
1077
1078 // Create a mask from a bitmap and a colour indicating
1079 // the transparent area
1080 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1081 {
1082 if ( m_maskBitmap )
1083 {
1084 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1085 m_maskBitmap = NULL ;
1086 }
1087 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1088 wxT("Cannot create mask from this bitmap type (TODO)"));
1089 // other types would require a temporary bitmap. not yet implemented
1090
1091 wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));
1092
1093 m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
1094 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap ) );
1095 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) );
1096 RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel());
1097
1098 // this is not very efficient, but I can't think
1099 // of a better way of doing it
1100 CGrafPtr origPort ;
1101 GDHandle origDevice ;
1102 RGBColor col;
1103 RGBColor colors[2] = {
1104 { 0xFFFF, 0xFFFF, 0xFFFF },
1105 { 0, 0, 0 }};
1106
1107 GetGWorld( &origPort , &origDevice ) ;
1108 for (int w = 0; w < bitmap.GetWidth(); w++)
1109 {
1110 for (int h = 0; h < bitmap.GetHeight(); h++)
1111 {
1112 SetGWorld( (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ;
1113 GetCPixel( w , h , &col ) ;
1114 SetGWorld( (GWorldPtr) m_maskBitmap , NULL ) ;
1115 if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
1116 {
1117 SetCPixel( w , h , &colors[0] ) ;
1118 }
1119 else
1120 {
1121 SetCPixel( w , h , &colors[1] ) ;
1122 }
1123 }
1124 }
1125 UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
1126 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ) ;
1127 SetGWorld( origPort , origDevice ) ;
1128
1129 return TRUE;
1130 }
1131
1132 bool wxMask::PointMasked(int x, int y)
1133 {
1134 GWorldPtr origPort;
1135 GDHandle origDevice;
1136 RGBColor color;
1137 bool masked = true;
1138
1139 GetGWorld( &origPort, &origDevice);
1140
1141 //Set port to mask and see if it masked (1) or not ( 0 )
1142 SetGWorld( (GWorldPtr) m_maskBitmap, NULL);
1143 LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1144 GetCPixel(x,y, &color);
1145 masked = !(color.red == 0 && color.green == 0 && color.blue == 0);
1146 UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1147
1148 SetGWorld( origPort, origDevice);
1149
1150 return masked;
1151 }
1152
1153 /*
1154 * wxBitmapHandler
1155 */
1156
1157 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1158 {
1159 return FALSE;
1160 }
1161
1162 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1163 int desiredWidth, int desiredHeight)
1164 {
1165 return FALSE;
1166 }
1167
1168 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1169 {
1170 return FALSE;
1171 }
1172
1173 /*
1174 * Standard handlers
1175 */
1176
1177 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1178 {
1179 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1180 public:
1181 inline wxPICTResourceHandler()
1182 {
1183 m_name = "Macintosh Pict resource";
1184 m_extension = "";
1185 m_type = wxBITMAP_TYPE_PICT_RESOURCE;
1186 };
1187
1188 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1189 int desiredWidth, int desiredHeight);
1190 };
1191 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1192
1193 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1194 int desiredWidth, int desiredHeight)
1195 {
1196 Str255 theName ;
1197
1198 #if TARGET_CARBON
1199 c2pstrcpy( (StringPtr) theName , name ) ;
1200 #else
1201 strcpy( (char *) theName , name ) ;
1202 c2pstr( (char *)theName ) ;
1203 #endif
1204
1205 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1206 if ( thePict )
1207 {
1208 PictInfo theInfo ;
1209
1210 GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
1211 DetachResource( (Handle) thePict ) ;
1212 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
1213 M_BITMAPHANDLERDATA->m_hPict = thePict ;
1214 M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ;
1215 M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;
1216
1217 M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
1218 M_BITMAPHANDLERDATA->m_ok = true ;
1219 M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
1220 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1221 // M_BITMAPHANDLERDATA->m_quality;
1222 return TRUE ;
1223 }
1224 return FALSE ;
1225 }
1226
1227 void wxBitmap::InitStandardHandlers()
1228 {
1229 AddHandler(new wxPICTResourceHandler) ;
1230 AddHandler(new wxICONResourceHandler) ;
1231 }