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