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