]> git.saurik.com Git - wxWidgets.git/blob - src/mac/bitmap.cpp
mingw/cygwin compilation fix (patch 556157)
[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 = 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 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 : m_width(0)
345 , m_height(0)
346 , m_depth(0)
347 , m_ok(FALSE)
348 , m_numColors(0)
349 , m_quality(0)
350 {
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 to 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 register unsigned char* data = image.GetData();
703 char* destinationBase = GetPixBaseAddr( pixMap );
704 register unsigned char* destination = (unsigned char*) destinationBase ;
705 for (int y = 0; y < height; y++)
706 {
707 for (int x = 0; x < width; x++)
708 {
709 *destination++ = 0 ;
710 *destination++ = *data++ ;
711 *destination++ = *data++ ;
712 *destination++ = *data++ ;
713 }
714 destinationBase += ((**pixMap).rowBytes & 0x7fff);
715 destination = (unsigned char*) destinationBase ;
716 }
717 if ( image.HasMask() )
718 {
719 data = image.GetData();
720
721 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
722 RGBColor white = { 0xffff, 0xffff, 0xffff };
723 RGBColor black = { 0 , 0 , 0 };
724 wxBitmap maskBitmap ;
725
726 maskBitmap.Create( width, height, 1);
727 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
728 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
729
730 for (int y = 0; y < height; y++)
731 {
732 for (int x = 0; x < width; x++)
733 {
734 if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
735 {
736 SetCPixel(x,y, &white);
737 }
738 else {
739 SetCPixel(x,y, &black);
740 }
741 data += 3 ;
742 }
743 } // for height
744 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
745 SetMask(new wxMask( maskBitmap ));
746 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
747 }
748
749 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
750 SetGWorld( origPort, origDevice );
751 }
752
753 wxImage wxBitmap::ConvertToImage() const
754 {
755 wxImage image;
756
757 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
758
759 // create an wxImage object
760 int width = GetWidth();
761 int height = GetHeight();
762 image.Create( width, height );
763
764 unsigned char *data = image.GetData();
765
766 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
767
768 GWorldPtr origPort;
769 GDHandle origDevice;
770 int index;
771 RGBColor color;
772 // background color set to RGB(16,16,16) in consistent with wxGTK
773 unsigned char mask_r=16, mask_g=16, mask_b=16;
774 SInt16 r,g,b;
775 wxMask *mask = GetMask();
776
777 GetGWorld( &origPort, &origDevice );
778 LockPixels(GetGWorldPixMap( (GWorldPtr) GetHBITMAP()));
779 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
780
781 // Copy data into image
782 index = 0;
783 for (int yy = 0; yy < height; yy++)
784 {
785 for (int xx = 0; xx < width; xx++)
786 {
787 GetCPixel(xx,yy, &color);
788 r = ((color.red ) >> 8);
789 g = ((color.green ) >> 8);
790 b = ((color.blue ) >> 8);
791 data[index ] = r;
792 data[index + 1] = g;
793 data[index + 2] = b;
794 if (mask)
795 {
796 if (mask->PointMasked(xx,yy))
797 {
798 data[index ] = mask_r;
799 data[index + 1] = mask_g;
800 data[index + 2] = mask_b;
801 }
802 }
803 index += 3;
804 }
805 }
806 if (mask)
807 {
808 image.SetMaskColour( mask_r, mask_g, mask_b );
809 image.SetMask( true );
810 }
811
812 // Free resources
813 UnlockPixels(GetGWorldPixMap( (GWorldPtr) GetHBITMAP()));
814 SetGWorld(origPort, origDevice);
815
816 return image;
817 }
818
819
820 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
821 const wxPalette *palette) const
822 {
823 wxBitmapHandler *handler = FindHandler(type);
824
825 if ( handler )
826 {
827 return handler->SaveFile(this, filename, type, palette);
828 }
829 else
830 {
831 wxImage image = ConvertToImage();
832
833 return image.SaveFile(filename, type);
834 }
835
836 wxLogWarning("no bitmap handler for type %d defined.", type);
837 return false;
838 }
839
840 bool wxBitmap::Ok() const
841 {
842 return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
843 }
844
845 int wxBitmap::GetHeight() const
846 {
847 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
848
849 return M_BITMAPDATA->m_height;
850 }
851
852 int wxBitmap::GetWidth() const
853 {
854 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
855
856 return M_BITMAPDATA->m_width;
857 }
858
859 int wxBitmap::GetDepth() const
860 {
861 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
862
863 return M_BITMAPDATA->m_depth;
864 }
865
866 int wxBitmap::GetQuality() const
867 {
868 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
869
870 return M_BITMAPDATA->m_quality;
871 }
872
873 wxMask *wxBitmap::GetMask() const
874 {
875 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
876
877 return M_BITMAPDATA->m_bitmapMask;
878 }
879
880 void wxBitmap::SetWidth(int w)
881 {
882 if (!M_BITMAPDATA)
883 m_refData = new wxBitmapRefData;
884
885 M_BITMAPDATA->m_width = w;
886 }
887
888 void wxBitmap::SetHeight(int h)
889 {
890 if (!M_BITMAPDATA)
891 m_refData = new wxBitmapRefData;
892
893 M_BITMAPDATA->m_height = h;
894 }
895
896 void wxBitmap::SetDepth(int d)
897 {
898 if (!M_BITMAPDATA)
899 m_refData = new wxBitmapRefData;
900
901 M_BITMAPDATA->m_depth = d;
902 }
903
904 void wxBitmap::SetQuality(int q)
905 {
906 if (!M_BITMAPDATA)
907 m_refData = new wxBitmapRefData;
908
909 M_BITMAPDATA->m_quality = q;
910 }
911
912 void wxBitmap::SetOk(bool isOk)
913 {
914 if (!M_BITMAPDATA)
915 m_refData = new wxBitmapRefData;
916
917 M_BITMAPDATA->m_ok = isOk;
918 }
919
920 wxPalette *wxBitmap::GetPalette() const
921 {
922 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
923
924 return &M_BITMAPDATA->m_bitmapPalette;
925 }
926
927 void wxBitmap::SetPalette(const wxPalette& palette)
928 {
929 if (!M_BITMAPDATA)
930 m_refData = new wxBitmapRefData;
931
932 M_BITMAPDATA->m_bitmapPalette = palette ;
933 }
934
935 void wxBitmap::SetMask(wxMask *mask)
936 {
937 if (!M_BITMAPDATA)
938 m_refData = new wxBitmapRefData;
939
940 // Remove existing mask if there is one.
941 if (M_BITMAPDATA->m_bitmapMask)
942 delete M_BITMAPDATA->m_bitmapMask;
943
944 M_BITMAPDATA->m_bitmapMask = mask ;
945 }
946
947 WXHBITMAP wxBitmap::GetHBITMAP() const
948 {
949 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
950
951 return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
952 }
953
954 WXHMETAFILE wxBitmap::GetPict() const
955 {
956 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
957
958 PicHandle picture; // This is the returned picture
959
960 // If bitmap already in Pict format return pointer
961 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) {
962 return M_BITMAPDATA->m_hPict;
963 }
964 else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) {
965 // Invalid bitmap
966 return NULL;
967 }
968
969 RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
970 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
971 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
972 CGrafPtr origPort;
973 GDHandle origDev ;
974 wxMask *mask;
975 Rect portRect ;
976
977 GetPortBounds( (GWorldPtr) GetHBITMAP() , &portRect ) ;
978 int width = portRect.right - portRect.left ;
979 int height = portRect.bottom - portRect.top ;
980
981 LockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ;
982 GetGWorld( &origPort , &origDev ) ;
983
984 mask = GetMask();
985
986 SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
987
988 picture = OpenPicture(&portRect); // open a picture, this disables drawing
989 if(!picture) {
990 return NULL;
991 }
992
993 if( mask )
994 {
995 #ifdef __DARWIN__
996 RGBColor trans = white;
997 #else
998 RGBBackColor( &gray );
999 EraseRect( &portRect );
1000 RGBColor trans = gray;
1001 #endif
1002 RGBForeColor( &black ) ;
1003 RGBBackColor( &white ) ;
1004 PenMode(transparent);
1005
1006 for ( int y = 0 ; y < height ; ++y )
1007 {
1008 for( int x = 0 ; x < width ; ++x )
1009 {
1010 if ( !mask->PointMasked(x,y) )
1011 {
1012 RGBColor col ;
1013
1014 GetCPixel( x + portRect.left , y + portRect.top , &col ) ;
1015 SetCPixel( x + portRect.left , y + portRect.top , &col ) ;
1016 }
1017 else {
1018 // With transparency this sets a blank pixel
1019 SetCPixel( x + portRect.left , y + portRect.top , &trans);
1020 }
1021 }
1022 }
1023 }
1024 else
1025 {
1026 RGBBackColor( &gray ) ;
1027 EraseRect(&portRect);
1028 RGBForeColor( &black ) ;
1029 RGBBackColor( &white ) ;
1030
1031 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()),
1032 // src PixMap - we copy image over itself -
1033 GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()),
1034 // dst PixMap - no drawing occurs
1035 &portRect, // srcRect - it will be recorded and compressed -
1036 &portRect, // dstRect - into the picture that is open -
1037 srcCopy,NULL); // copyMode and no clip region
1038 }
1039 ClosePicture(); // We are done recording the picture
1040 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ;
1041 SetGWorld( origPort , origDev ) ;
1042
1043 return picture; // return our groovy pict handle
1044 }
1045
1046 /*
1047 * wxMask
1048 */
1049
1050 wxMask::wxMask()
1051 : m_maskBitmap(NULL)
1052 {
1053 }
1054
1055 // Construct a mask from a bitmap and a colour indicating
1056 // the transparent area
1057 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1058 : m_maskBitmap(NULL)
1059 {
1060 Create(bitmap, colour);
1061 }
1062
1063 // Construct a mask from a bitmap and a palette index indicating
1064 // the transparent area
1065 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1066 : m_maskBitmap(NULL)
1067 {
1068 Create(bitmap, paletteIndex);
1069 }
1070
1071 // Construct a mask from a mono bitmap (copies the bitmap).
1072 wxMask::wxMask(const wxBitmap& bitmap)
1073 : m_maskBitmap(NULL)
1074 {
1075 Create(bitmap);
1076 }
1077
1078 wxMask::~wxMask()
1079 {
1080 if ( m_maskBitmap )
1081 {
1082 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1083 m_maskBitmap = NULL ;
1084 }
1085 }
1086
1087 // Create a mask from a mono bitmap (copies the bitmap).
1088 bool wxMask::Create(const wxBitmap& bitmap)
1089 {
1090 if ( m_maskBitmap )
1091 {
1092 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1093 m_maskBitmap = NULL ;
1094 }
1095 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1096 wxT("Cannot create mask from this bitmap type (TODO)"));
1097 // other types would require a temporary bitmap. not yet implemented
1098
1099 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap"));
1100
1101 wxCHECK_MSG(bitmap.GetDepth() == 1, false,
1102 wxT("Cannot create mask from colour bitmap"));
1103
1104 m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), 1);
1105 Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() };
1106
1107 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1108 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1109 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()),
1110 GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap),
1111 &rect, &rect, srcCopy, 0);
1112 UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1113 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1114
1115 return FALSE;
1116 }
1117
1118 // Create a mask from a bitmap and a palette index indicating
1119 // the transparent area
1120 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1121 {
1122 // TODO
1123 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1124 return FALSE;
1125 }
1126
1127 // Create a mask from a bitmap and a colour indicating
1128 // the transparent area
1129 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1130 {
1131 if ( m_maskBitmap )
1132 {
1133 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
1134 m_maskBitmap = NULL ;
1135 }
1136 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1137 wxT("Cannot create mask from this bitmap type (TODO)"));
1138 // other types would require a temporary bitmap. not yet implemented
1139
1140 wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));
1141
1142 m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
1143 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap ) );
1144 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) );
1145 RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel());
1146
1147 // this is not very efficient, but I can't think
1148 // of a better way of doing it
1149 CGrafPtr origPort ;
1150 GDHandle origDevice ;
1151 RGBColor col;
1152 RGBColor colors[2] = {
1153 { 0xFFFF, 0xFFFF, 0xFFFF },
1154 { 0, 0, 0 }};
1155
1156 GetGWorld( &origPort , &origDevice ) ;
1157 for (int w = 0; w < bitmap.GetWidth(); w++)
1158 {
1159 for (int h = 0; h < bitmap.GetHeight(); h++)
1160 {
1161 SetGWorld( (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ;
1162 GetCPixel( w , h , &col ) ;
1163 SetGWorld( (GWorldPtr) m_maskBitmap , NULL ) ;
1164 if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
1165 {
1166 SetCPixel( w , h , &colors[0] ) ;
1167 }
1168 else
1169 {
1170 SetCPixel( w , h , &colors[1] ) ;
1171 }
1172 }
1173 }
1174 UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
1175 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ) ;
1176 SetGWorld( origPort , origDevice ) ;
1177
1178 return TRUE;
1179 }
1180
1181 bool wxMask::PointMasked(int x, int y)
1182 {
1183 GWorldPtr origPort;
1184 GDHandle origDevice;
1185 RGBColor color;
1186 bool masked = true;
1187
1188 GetGWorld( &origPort, &origDevice);
1189
1190 //Set port to mask and see if it masked (1) or not ( 0 )
1191 SetGWorld( (GWorldPtr) m_maskBitmap, NULL);
1192 LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1193 GetCPixel(x,y, &color);
1194 masked = !(color.red == 0 && color.green == 0 && color.blue == 0);
1195 UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
1196
1197 SetGWorld( origPort, origDevice);
1198
1199 return masked;
1200 }
1201
1202 /*
1203 * wxBitmapHandler
1204 */
1205
1206 wxBitmapHandler::~wxBitmapHandler()
1207 {
1208 }
1209
1210 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1211 {
1212 return FALSE;
1213 }
1214
1215 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1216 int desiredWidth, int desiredHeight)
1217 {
1218 return FALSE;
1219 }
1220
1221 bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
1222 {
1223 return FALSE;
1224 }
1225
1226 /*
1227 * Standard handlers
1228 */
1229
1230 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1231 {
1232 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1233 public:
1234 inline wxPICTResourceHandler()
1235 {
1236 m_name = "Macintosh Pict resource";
1237 m_extension = "";
1238 m_type = wxBITMAP_TYPE_PICT_RESOURCE;
1239 };
1240
1241 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1242 int desiredWidth, int desiredHeight);
1243 };
1244 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1245
1246 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1247 int desiredWidth, int desiredHeight)
1248 {
1249 Str255 theName ;
1250
1251 #if TARGET_CARBON
1252 c2pstrcpy( (StringPtr) theName , name ) ;
1253 #else
1254 strcpy( (char *) theName , name ) ;
1255 c2pstr( (char *)theName ) ;
1256 #endif
1257
1258 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1259 if ( thePict )
1260 {
1261 PictInfo theInfo ;
1262
1263 GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
1264 DetachResource( (Handle) thePict ) ;
1265 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
1266 M_BITMAPHANDLERDATA->m_hPict = thePict ;
1267 M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ;
1268 M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;
1269
1270 M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
1271 M_BITMAPHANDLERDATA->m_ok = true ;
1272 M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
1273 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1274 // M_BITMAPHANDLERDATA->m_quality;
1275 return TRUE ;
1276 }
1277 return FALSE ;
1278 }
1279
1280 void wxBitmap::InitStandardHandlers()
1281 {
1282 AddHandler(new wxPICTResourceHandler) ;
1283 AddHandler(new wxICONResourceHandler) ;
1284 }