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