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