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