]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/bitmap.cpp
added wx/defs.h include to correct compilation issues under Mac OS X
[wxWidgets.git] / src / mac / carbon / bitmap.cpp
CommitLineData
e9576ca5
SC
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__
b698c8e9 13#pragma implementation "bitmapbase.h"
e9576ca5
SC
14#pragma implementation "bitmap.h"
15#endif
16
b698c8e9
GD
17#include "wx/defs.h"
18
e9576ca5
SC
19#include "wx/bitmap.h"
20#include "wx/icon.h"
21#include "wx/log.h"
fec19ea9 22#include "wx/image.h"
973b0afb 23#include "wx/xpmdecod.h"
e9576ca5 24
2f1ae414 25#if !USE_SHARED_LIBRARIES
e9576ca5
SC
26IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
27IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
90b959ae
SC
28IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase , wxGDIObject )
29IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase, wxObject )
2f1ae414 30#endif
e9576ca5 31
f5c6eb5c 32#ifdef __DARWIN__
5fde6fcc 33 #include <ApplicationServices/ApplicationServices.h>
03e11df5
GD
34#else
35 #include <PictUtils.h>
36#endif
519cb848 37
31d30995
SC
38#include "wx/mac/uma.h"
39
519cb848
SC
40CTabHandle wxMacCreateColorTable( int numColors )
41{
42 CTabHandle newColors; /* Handle to the new color table */
03e11df5 43
519cb848
SC
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
58void wxMacDestroyColorTable( CTabHandle colors )
59{
60 DisposeHandle( (Handle) colors ) ;
61}
62
63void 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
03e11df5 71GWorldPtr wxMacCreateGWorld( int width , int height , int depth )
519cb848
SC
72{
73 OSErr err = noErr ;
74 GWorldPtr port ;
03e11df5 75 Rect rect = { 0 , 0 , height , width } ;
519cb848
SC
76
77 if ( depth < 0 )
78 {
2f1ae414 79 depth = wxDisplayDepth() ;
519cb848
SC
80 }
81
82 err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ;
83 if ( err == noErr )
84 {
85 return port ;
86 }
87 return NULL ;
03e11df5 88}
519cb848
SC
89
90void wxMacDestroyGWorld( GWorldPtr gw )
91{
92 if ( gw )
93 DisposeGWorld( gw ) ;
94}
95
5fde6fcc
GD
96PicHandle 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
e9576ca5
SC
187wxBitmapRefData::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;
973b0afb
GD
196 m_hBitmap = NULL ;
197 m_hPict = NULL ;
3dec57ad 198 m_hIcon = NULL ;
973b0afb 199 m_bitmapType = kMacBitmapTypeUnknownType ;
e9576ca5
SC
200}
201
202wxBitmapRefData::~wxBitmapRefData()
203{
519cb848
SC
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 ;
3dec57ad
SC
224 case kMacBitmapTypeIcon :
225 if ( m_hIcon )
226 {
227 DisposeCIcon( m_hIcon ) ;
228 m_hIcon = NULL ;
229 }
230
519cb848
SC
231 default :
232 // unkown type ?
233 break ;
03e11df5 234 }
519cb848
SC
235
236 if (m_bitmapMask)
237 {
238 delete m_bitmapMask;
e9576ca5 239 m_bitmapMask = NULL;
519cb848 240 }
e9576ca5
SC
241}
242
b698c8e9 243wxList wxBitmapBase::sm_handlers;
e9576ca5 244
90b959ae
SC
245
246bool wxBitmap::CopyFromIcon(const wxIcon& icon)
247{
248 Ref(icon) ;
125c389e 249 return true;
90b959ae
SC
250}
251
e9576ca5
SC
252wxBitmap::wxBitmap()
253{
254 m_refData = NULL;
255
256 if ( wxTheBitmapList )
257 wxTheBitmapList->AddBitmap(this);
258}
259
260wxBitmap::~wxBitmap()
261{
262 if (wxTheBitmapList)
263 wxTheBitmapList->DeleteObject(this);
264}
265
266wxBitmap::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;
d2c6d549
GD
274 if ( no_bits == 1 )
275 {
973b0afb
GD
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 ) ;
519cb848 279
973b0afb
GD
280 CGrafPtr origPort ;
281 GDHandle origDevice ;
519cb848 282
973b0afb
GD
283 GetGWorld( &origPort , &origDevice ) ;
284 SetGWorld( M_BITMAPDATA->m_hBitmap , NULL ) ;
285 LockPixels( GetGWorldPixMap( M_BITMAPDATA->m_hBitmap ) ) ;
d2c6d549 286
973b0afb 287 // bits is a char array
d2c6d549 288
973b0afb
GD
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 }
d2c6d549 294
973b0afb
GD
295 RGBColor colors[2] = {
296 { 0xFFFF , 0xFFFF , 0xFFFF } ,
297 { 0, 0 , 0 }
298 } ;
d2c6d549 299
973b0afb
GD
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 ) ) ;
d2c6d549 318
973b0afb 319 SetGWorld( origPort , origDevice ) ;
d2c6d549
GD
320 }
321 else
322 {
973b0afb 323 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
d2c6d549
GD
324 }
325
326 if ( wxTheBitmapList ) {
e9576ca5 327 wxTheBitmapList->AddBitmap(this);
d2c6d549 328 }
e9576ca5
SC
329}
330
331wxBitmap::wxBitmap(int w, int h, int d)
332{
333 (void)Create(w, h, d);
334
335 if ( wxTheBitmapList )
336 wxTheBitmapList->AddBitmap(this);
337}
338
a8562f55 339wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
340{
341 (void) Create(data, type, width, height, depth);
342
343 if ( wxTheBitmapList )
344 wxTheBitmapList->AddBitmap(this);
345}
346
a8562f55 347wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
e9576ca5 348{
a8562f55 349 LoadFile(filename, type);
e9576ca5
SC
350
351 if ( wxTheBitmapList )
352 wxTheBitmapList->AddBitmap(this);
353}
354
973b0afb 355bool wxBitmap::CreateFromXpm(const char **bits)
e9576ca5 356{
973b0afb
GD
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
366wxBitmap::wxBitmap(const char **bits)
367{
973b0afb 368 (void) CreateFromXpm(bits);
e9576ca5 369}
e9576ca5 370
973b0afb 371wxBitmap::wxBitmap(char **bits)
03e11df5 372{
973b0afb 373 (void) CreateFromXpm((const char **)bits);
03e11df5
GD
374}
375
5fde6fcc
GD
376wxBitmap 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
e9576ca5
SC
465bool 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
519cb848
SC
475 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
476 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
a8562f55 477 M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ;
e9576ca5
SC
478 return M_BITMAPDATA->m_ok;
479}
480
5fde6fcc
GD
481int wxBitmap::GetBitmapType() const
482{
483 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") );
484
485 return M_BITMAPDATA->m_bitmapType;
486}
487
519cb848
SC
488void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
489{
490 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
491 M_BITMAPDATA->m_hBitmap = bmp ;
3dec57ad 492 M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ;
519cb848
SC
493}
494
a8562f55 495bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
e9576ca5
SC
496{
497 UnRef();
498
e9576ca5
SC
499 wxBitmapHandler *handler = FindHandler(type);
500
a8562f55
GD
501 if ( handler )
502 {
503 m_refData = new wxBitmapRefData;
e9576ca5 504
a8562f55 505 return handler->LoadFile(this, filename, type, -1, -1);
e9576ca5 506 }
a8562f55
GD
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;
e9576ca5
SC
517}
518
a8562f55 519bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
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
fec19ea9
VS
536wxBitmap::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
71789654 551 Create( width , height , 32 ) ;
fec19ea9
VS
552
553 CGrafPtr origPort ;
554 GDHandle origDevice ;
555
71789654
SC
556 PixMapHandle pixMap = GetGWorldPixMap(GetHBITMAP()) ;
557 LockPixels( pixMap );
fec19ea9
VS
558
559 GetGWorld( &origPort , &origDevice ) ;
560 SetGWorld( GetHBITMAP() , NULL ) ;
561
562 // Render image
71789654 563 RGBColor colorRGB ;
fec19ea9
VS
564
565 register unsigned char* data = image.GetData();
71789654
SC
566 char* destinationBase = GetPixBaseAddr( pixMap );
567 register unsigned char* destination = (unsigned char*) destinationBase ;
fec19ea9
VS
568 for (int y = 0; y < height; y++)
569 {
570 for (int x = 0; x < width; x++)
571 {
71789654
SC
572 *destination++ = 0 ;
573 *destination++ = *data++ ;
574 *destination++ = *data++ ;
575 *destination++ = *data++ ;
fec19ea9 576 }
71789654
SC
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 ;
fec19ea9 588
71789654
SC
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 {
2af9ef13 597 if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
71789654
SC
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()) );
fec19ea9
VS
610 }
611
612 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
fec19ea9
VS
613 SetGWorld( origPort, origDevice );
614}
615
616wxImage 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
a8562f55
GD
683bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
684 const wxPalette *palette) const
e9576ca5
SC
685{
686 wxBitmapHandler *handler = FindHandler(type);
687
a8562f55
GD
688 if ( handler )
689 {
690 return handler->SaveFile(this, filename, type, palette);
691 }
692 else
693 {
694 wxImage image = ConvertToImage();
e9576ca5 695
a8562f55
GD
696 return image.SaveFile(filename, type);
697 }
698
699 wxLogWarning("no bitmap handler for type %d defined.", type);
700 return false;
e9576ca5
SC
701}
702
5fde6fcc
GD
703bool wxBitmap::Ok() const
704{
705 return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
706}
707
708int wxBitmap::GetHeight() const
709{
710 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
711
712 return M_BITMAPDATA->m_height;
713}
714
715int wxBitmap::GetWidth() const
716{
717 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
718
719 return M_BITMAPDATA->m_width;
720}
721
722int wxBitmap::GetDepth() const
723{
724 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
725
726 return M_BITMAPDATA->m_depth;
727}
728
729int wxBitmap::GetQuality() const
730{
731 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
732
733 return M_BITMAPDATA->m_quality;
734}
735
736wxMask *wxBitmap::GetMask() const
737{
738 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
739
740 return M_BITMAPDATA->m_bitmapMask;
741}
742
e9576ca5
SC
743void wxBitmap::SetWidth(int w)
744{
745 if (!M_BITMAPDATA)
746 m_refData = new wxBitmapRefData;
747
748 M_BITMAPDATA->m_width = w;
749}
750
751void wxBitmap::SetHeight(int h)
752{
753 if (!M_BITMAPDATA)
754 m_refData = new wxBitmapRefData;
755
756 M_BITMAPDATA->m_height = h;
757}
758
759void wxBitmap::SetDepth(int d)
760{
761 if (!M_BITMAPDATA)
762 m_refData = new wxBitmapRefData;
763
764 M_BITMAPDATA->m_depth = d;
765}
766
767void wxBitmap::SetQuality(int q)
768{
769 if (!M_BITMAPDATA)
770 m_refData = new wxBitmapRefData;
771
772 M_BITMAPDATA->m_quality = q;
773}
774
775void wxBitmap::SetOk(bool isOk)
776{
777 if (!M_BITMAPDATA)
778 m_refData = new wxBitmapRefData;
779
780 M_BITMAPDATA->m_ok = isOk;
781}
782
5fde6fcc
GD
783wxPalette *wxBitmap::GetPalette() const
784{
785 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
786
787 return &M_BITMAPDATA->m_bitmapPalette;
788}
789
e9576ca5
SC
790void wxBitmap::SetPalette(const wxPalette& palette)
791{
792 if (!M_BITMAPDATA)
793 m_refData = new wxBitmapRefData;
794
795 M_BITMAPDATA->m_bitmapPalette = palette ;
796}
797
798void wxBitmap::SetMask(wxMask *mask)
799{
800 if (!M_BITMAPDATA)
801 m_refData = new wxBitmapRefData;
802
a8562f55
GD
803 // Remove existing mask if there is one.
804 if (M_BITMAPDATA->m_bitmapMask)
805 delete M_BITMAPDATA->m_bitmapMask;
806
e9576ca5
SC
807 M_BITMAPDATA->m_bitmapMask = mask ;
808}
809
5fde6fcc
GD
810WXHBITMAP wxBitmap::GetHBITMAP() const
811{
812 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
813
814 return M_BITMAPDATA->m_hBitmap;
815}
816
817PicHandle 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 {
f5c6eb5c 858#ifdef __DARWIN__
5fde6fcc
GD
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
e9576ca5
SC
909void wxBitmap::AddHandler(wxBitmapHandler *handler)
910{
911 sm_handlers.Append(handler);
912}
913
914void wxBitmap::InsertHandler(wxBitmapHandler *handler)
915{
916 sm_handlers.Insert(handler);
917}
918
919bool 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
931wxBitmapHandler *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
a8562f55 944wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, wxBitmapType type)
e9576ca5
SC
945{
946 wxNode *node = sm_handlers.First();
947 while ( node )
948 {
949 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
950 if ( handler->GetExtension() == extension &&
a8562f55 951 (type == -1 || handler->GetType() == type) )
e9576ca5
SC
952 return handler;
953 node = node->Next();
954 }
955 return NULL;
956}
957
a8562f55 958wxBitmapHandler *wxBitmap::FindHandler(wxBitmapType type)
e9576ca5
SC
959{
960 wxNode *node = sm_handlers.First();
961 while ( node )
962 {
963 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
a8562f55 964 if (handler->GetType() == type)
e9576ca5
SC
965 return handler;
966 node = node->Next();
967 }
968 return NULL;
969}
970
971/*
972 * wxMask
973 */
974
975wxMask::wxMask()
976{
e9576ca5 977 m_maskBitmap = 0;
e9576ca5
SC
978}
979
980// Construct a mask from a bitmap and a colour indicating
981// the transparent area
982wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
983{
e9576ca5 984 m_maskBitmap = 0;
e9576ca5
SC
985 Create(bitmap, colour);
986}
987
988// Construct a mask from a bitmap and a palette index indicating
989// the transparent area
990wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
991{
e9576ca5 992 m_maskBitmap = 0;
e9576ca5
SC
993 Create(bitmap, paletteIndex);
994}
995
996// Construct a mask from a mono bitmap (copies the bitmap).
997wxMask::wxMask(const wxBitmap& bitmap)
998{
e9576ca5 999 m_maskBitmap = 0;
e9576ca5
SC
1000 Create(bitmap);
1001}
1002
1003wxMask::~wxMask()
1004{
8208e181
SC
1005 if ( m_maskBitmap )
1006 {
1007 wxMacDestroyGWorld( m_maskBitmap ) ;
1008 m_maskBitmap = NULL ;
1009 }
e9576ca5
SC
1010}
1011
1012// Create a mask from a mono bitmap (copies the bitmap).
1013bool wxMask::Create(const wxBitmap& bitmap)
1014{
5fde6fcc
GD
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;
e9576ca5
SC
1041}
1042
1043// Create a mask from a bitmap and a palette index indicating
1044// the transparent area
1045bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1046{
1047// TODO
5fde6fcc 1048 wxCHECK_MSG( 0, false, wxT("Not implemented"));
e9576ca5
SC
1049 return FALSE;
1050}
1051
1052// Create a mask from a bitmap and a colour indicating
1053// the transparent area
1054bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1055{
8208e181
SC
1056 if ( m_maskBitmap )
1057 {
1058 wxMacDestroyGWorld( m_maskBitmap ) ;
1059 m_maskBitmap = NULL ;
1060 }
5fde6fcc
GD
1061 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1062 wxT("Cannot create mask from this bitmap type (TODO)"));
8208e181
SC
1063 // other types would require a temporary bitmap. not yet implemented
1064
5fde6fcc 1065 wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));
8208e181 1066
5fde6fcc
GD
1067 m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
1068 LockPixels( GetGWorldPixMap( m_maskBitmap ) );
1069 LockPixels( GetGWorldPixMap( bitmap.GetHBITMAP() ) );
1070 RGBColor maskColor = colour.GetPixel();
8208e181
SC
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 ;
5fde6fcc
GD
1076 RGBColor col;
1077 RGBColor colors[2] = {
1078 { 0xFFFF, 0xFFFF, 0xFFFF },
1079 { 0, 0, 0 }};
8208e181
SC
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 {
5fde6fcc 1086 SetGWorld( bitmap.GetHBITMAP(), NULL ) ;
8208e181
SC
1087 GetCPixel( w , h , &col ) ;
1088 SetGWorld( m_maskBitmap , NULL ) ;
5fde6fcc 1089 if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
8208e181
SC
1090 {
1091 SetCPixel( w , h , &colors[0] ) ;
1092 }
1093 else
1094 {
1095 SetCPixel( w , h , &colors[1] ) ;
1096 }
1097 }
1098 }
2f1ae414 1099 UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
5fde6fcc 1100 UnlockPixels( GetGWorldPixMap( bitmap.GetHBITMAP() ) ) ;
8208e181
SC
1101 SetGWorld( origPort , origDevice ) ;
1102
1103 return TRUE;
e9576ca5
SC
1104}
1105
5fde6fcc
GD
1106bool 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
e9576ca5
SC
1127/*
1128 * wxBitmapHandler
1129 */
1130
1131IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
1132
1133bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1134{
1135 return FALSE;
1136}
1137
a8562f55 1138bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1139 int desiredWidth, int desiredHeight)
1140{
1141 return FALSE;
1142}
1143
a8562f55 1144bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5
SC
1145{
1146 return FALSE;
1147}
1148
1149/*
1150 * Standard handlers
1151 */
1152
519cb848
SC
1153class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1154{
1155 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1156public:
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};
1167IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1168
1169bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1170 int desiredWidth, int desiredHeight)
1171{
1172 Str255 theName ;
1173
03e11df5
GD
1174#if TARGET_CARBON
1175 c2pstrcpy( (StringPtr) theName , name ) ;
1176#else
1177 strcpy( (char *) theName , name ) ;
1178 c2pstr( (char *)theName ) ;
1179#endif
519cb848
SC
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
a8562f55 1203#if 0 // The following is an example for creating a bitmap handler
519cb848 1204
a8562f55 1205// TODO: bitmap handlers, a bit like this:
519cb848
SC
1206class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
1207{
1208 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
1209public:
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};
519cb848
SC
1220IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
1221
519cb848 1222#endif
519cb848 1223
e9576ca5
SC
1224void 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
1237void wxBitmap::InitStandardHandlers()
1238{
973b0afb
GD
1239 AddHandler(new wxPICTResourceHandler) ;
1240 AddHandler(new wxICONResourceHandler) ;
e9576ca5 1241}