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