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