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