]>
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 | { |
4e9ed364 RR |
372 | switch (data->m_bitmapType) |
373 | { | |
374 | case kMacBitmapTypePict : | |
375 | { | |
376 | if ( data->m_hPict ) | |
377 | { | |
76a5e5d2 | 378 | KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ; |
4e9ed364 RR |
379 | data->m_hPict = NULL ; |
380 | } | |
381 | } | |
382 | break ; | |
383 | case kMacBitmapTypeGrafWorld : | |
384 | { | |
385 | if ( data->m_hBitmap ) | |
386 | { | |
76a5e5d2 | 387 | wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ; |
4e9ed364 RR |
388 | data->m_hBitmap = NULL ; |
389 | } | |
390 | } | |
391 | break ; | |
392 | case kMacBitmapTypeIcon : | |
393 | if ( data->m_hIcon ) | |
394 | { | |
76a5e5d2 | 395 | DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ; |
4e9ed364 RR |
396 | data->m_hIcon = NULL ; |
397 | } | |
398 | ||
399 | default : | |
400 | // unkown type ? | |
401 | break ; | |
402 | } | |
403 | ||
85f296a3 | 404 | if (data->m_bitmapMask) |
519cb848 | 405 | { |
85f296a3 SC |
406 | delete data->m_bitmapMask; |
407 | data->m_bitmapMask = NULL; | |
519cb848 | 408 | } |
e9576ca5 SC |
409 | } |
410 | ||
85f296a3 SC |
411 | wxBitmapRefData::~wxBitmapRefData() |
412 | { | |
413 | DisposeBitmapRefData( this ) ; | |
414 | } | |
415 | ||
90b959ae SC |
416 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
417 | { | |
418 | Ref(icon) ; | |
125c389e | 419 | return true; |
90b959ae SC |
420 | } |
421 | ||
e9576ca5 SC |
422 | wxBitmap::wxBitmap() |
423 | { | |
424 | m_refData = NULL; | |
e9576ca5 SC |
425 | } |
426 | ||
427 | wxBitmap::~wxBitmap() | |
428 | { | |
e9576ca5 SC |
429 | } |
430 | ||
431 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) | |
432 | { | |
433 | m_refData = new wxBitmapRefData; | |
434 | ||
435 | M_BITMAPDATA->m_width = the_width ; | |
436 | M_BITMAPDATA->m_height = the_height ; | |
437 | M_BITMAPDATA->m_depth = no_bits ; | |
438 | M_BITMAPDATA->m_numColors = 0; | |
d2c6d549 GD |
439 | if ( no_bits == 1 ) |
440 | { | |
973b0afb | 441 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
76a5e5d2 SC |
442 | MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) = wxMacCreateGWorld( the_width , the_height , no_bits ) ; |
443 | M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ; | |
4e9ed364 RR |
444 | |
445 | CGrafPtr origPort ; | |
446 | GDHandle origDevice ; | |
447 | ||
973b0afb | 448 | GetGWorld( &origPort , &origDevice ) ; |
76a5e5d2 SC |
449 | SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; |
450 | LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ; | |
4e9ed364 | 451 | |
973b0afb | 452 | // bits is a char array |
4e9ed364 | 453 | |
973b0afb GD |
454 | unsigned char* linestart = (unsigned char*) bits ; |
455 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; | |
456 | if ( the_width % (sizeof(unsigned char) * 8) ) { | |
457 | linesize += sizeof(unsigned char); | |
458 | } | |
4e9ed364 | 459 | |
973b0afb GD |
460 | RGBColor colors[2] = { |
461 | { 0xFFFF , 0xFFFF , 0xFFFF } , | |
462 | { 0, 0 , 0 } | |
463 | } ; | |
4e9ed364 | 464 | |
973b0afb GD |
465 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize ) |
466 | { | |
467 | for ( int x = 0 ; x < the_width ; ++x ) | |
468 | { | |
469 | int index = x / 8 ; | |
470 | int bit = x % 8 ; | |
471 | int mask = 1 << bit ; | |
472 | if ( linestart[index] & mask ) | |
473 | { | |
474 | SetCPixel( x , y , &colors[1] ) ; | |
475 | } | |
476 | else | |
477 | { | |
478 | SetCPixel( x , y , &colors[0] ) ; | |
479 | } | |
480 | } | |
481 | } | |
76a5e5d2 | 482 | UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ; |
4e9ed364 | 483 | |
973b0afb | 484 | SetGWorld( origPort , origDevice ) ; |
d2c6d549 GD |
485 | } |
486 | else | |
487 | { | |
973b0afb | 488 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); |
d2c6d549 | 489 | } |
e9576ca5 SC |
490 | } |
491 | ||
492 | wxBitmap::wxBitmap(int w, int h, int d) | |
493 | { | |
494 | (void)Create(w, h, d); | |
e9576ca5 SC |
495 | } |
496 | ||
a8562f55 | 497 | wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
498 | { |
499 | (void) Create(data, type, width, height, depth); | |
e9576ca5 SC |
500 | } |
501 | ||
a8562f55 | 502 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) |
e9576ca5 | 503 | { |
a8562f55 | 504 | LoadFile(filename, type); |
e9576ca5 SC |
505 | } |
506 | ||
973b0afb | 507 | bool wxBitmap::CreateFromXpm(const char **bits) |
e9576ca5 | 508 | { |
973b0afb GD |
509 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
510 | wxXPMDecoder decoder; | |
511 | wxImage img = decoder.ReadData(bits); | |
512 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) | |
513 | *this = wxBitmap(img); | |
973b0afb GD |
514 | return TRUE; |
515 | } | |
516 | ||
517 | wxBitmap::wxBitmap(const char **bits) | |
518 | { | |
973b0afb | 519 | (void) CreateFromXpm(bits); |
e9576ca5 | 520 | } |
e9576ca5 | 521 | |
973b0afb | 522 | wxBitmap::wxBitmap(char **bits) |
03e11df5 | 523 | { |
973b0afb | 524 | (void) CreateFromXpm((const char **)bits); |
03e11df5 GD |
525 | } |
526 | ||
5fde6fcc GD |
527 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const |
528 | { | |
529 | wxCHECK_MSG( Ok() && | |
530 | (rect.x >= 0) && (rect.y >= 0) && | |
531 | (rect.x+rect.width <= GetWidth()) && | |
532 | (rect.y+rect.height <= GetHeight()), | |
533 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
534 | ||
535 | ||
536 | wxBitmap ret( rect.width, rect.height, GetDepth() ); | |
537 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
538 | ||
76a5e5d2 | 539 | GWorldPtr origPort; |
5fde6fcc GD |
540 | GDHandle origDevice; |
541 | ||
542 | GetGWorld( &origPort, &origDevice ); | |
543 | ||
544 | // Update the subbitmaps reference data | |
545 | wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData(); | |
546 | ||
547 | ref->m_numColors = M_BITMAPDATA->m_numColors; | |
548 | ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette; | |
549 | ref->m_bitmapType = M_BITMAPDATA->m_bitmapType; | |
550 | ||
551 | // Copy sub region of this bitmap | |
552 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) | |
553 | { | |
554 | printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n"); | |
555 | } | |
556 | else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld) | |
557 | { | |
558 | // Copy mask | |
559 | if(GetMask()) | |
560 | { | |
76a5e5d2 | 561 | GWorldPtr submask, mask; |
5fde6fcc GD |
562 | RGBColor color; |
563 | ||
76a5e5d2 | 564 | mask = (GWorldPtr) GetMask()->GetMaskBitmap(); |
5fde6fcc GD |
565 | submask = wxMacCreateGWorld(rect.width, rect.height, 1); |
566 | LockPixels(GetGWorldPixMap(mask)); | |
567 | LockPixels(GetGWorldPixMap(submask)); | |
568 | ||
569 | for(int yy = 0; yy < rect.height; yy++) | |
570 | { | |
571 | for(int xx = 0; xx < rect.width; xx++) | |
572 | { | |
573 | SetGWorld(mask, NULL); | |
574 | GetCPixel(rect.x + xx, rect.y + yy, &color); | |
575 | SetGWorld(submask, NULL); | |
576 | SetCPixel(xx,yy, &color); | |
577 | } | |
578 | } | |
579 | UnlockPixels(GetGWorldPixMap(mask)); | |
580 | UnlockPixels(GetGWorldPixMap(submask)); | |
581 | ref->m_bitmapMask = new wxMask; | |
582 | ref->m_bitmapMask->SetMaskBitmap(submask); | |
583 | } | |
584 | ||
585 | // Copy bitmap | |
586 | if(GetHBITMAP()) | |
587 | { | |
76a5e5d2 | 588 | GWorldPtr subbitmap, bitmap; |
5fde6fcc GD |
589 | RGBColor color; |
590 | ||
76a5e5d2 SC |
591 | bitmap = (GWorldPtr) GetHBITMAP(); |
592 | subbitmap = (GWorldPtr) ref->m_hBitmap ; | |
5fde6fcc GD |
593 | LockPixels(GetGWorldPixMap(bitmap)); |
594 | LockPixels(GetGWorldPixMap(subbitmap)); | |
595 | ||
596 | for(int yy = 0; yy < rect.height; yy++) | |
597 | { | |
598 | for(int xx = 0; xx < rect.width; xx++) | |
599 | { | |
600 | SetGWorld(bitmap, NULL); | |
601 | GetCPixel(rect.x + xx, rect.y + yy, &color); | |
602 | SetGWorld(subbitmap, NULL); | |
603 | SetCPixel(xx, yy, &color); | |
604 | } | |
605 | } | |
606 | UnlockPixels(GetGWorldPixMap(bitmap)); | |
607 | UnlockPixels(GetGWorldPixMap(subbitmap)); | |
5fde6fcc GD |
608 | } |
609 | } | |
610 | SetGWorld( origPort, origDevice ); | |
611 | ||
612 | return ret; | |
613 | } | |
614 | ||
e9576ca5 SC |
615 | bool wxBitmap::Create(int w, int h, int d) |
616 | { | |
617 | UnRef(); | |
618 | ||
619 | m_refData = new wxBitmapRefData; | |
620 | ||
621 | M_BITMAPDATA->m_width = w; | |
622 | M_BITMAPDATA->m_height = h; | |
623 | M_BITMAPDATA->m_depth = d; | |
624 | ||
519cb848 SC |
625 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
626 | M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ; | |
76a5e5d2 | 627 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; |
e9576ca5 SC |
628 | return M_BITMAPDATA->m_ok; |
629 | } | |
630 | ||
5fde6fcc GD |
631 | int wxBitmap::GetBitmapType() const |
632 | { | |
633 | wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") ); | |
634 | ||
635 | return M_BITMAPDATA->m_bitmapType; | |
636 | } | |
637 | ||
519cb848 SC |
638 | void wxBitmap::SetHBITMAP(WXHBITMAP bmp) |
639 | { | |
85f296a3 SC |
640 | DisposeBitmapRefData( M_BITMAPDATA ) ; |
641 | ||
519cb848 SC |
642 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
643 | M_BITMAPDATA->m_hBitmap = bmp ; | |
76a5e5d2 | 644 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; |
519cb848 SC |
645 | } |
646 | ||
a8562f55 | 647 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
e9576ca5 SC |
648 | { |
649 | UnRef(); | |
650 | ||
e9576ca5 SC |
651 | wxBitmapHandler *handler = FindHandler(type); |
652 | ||
a8562f55 GD |
653 | if ( handler ) |
654 | { | |
4e9ed364 | 655 | m_refData = new wxBitmapRefData; |
e9576ca5 | 656 | |
a8562f55 | 657 | return handler->LoadFile(this, filename, type, -1, -1); |
e9576ca5 | 658 | } |
a8562f55 GD |
659 | else |
660 | { | |
661 | wxImage loadimage(filename, type); | |
662 | if (loadimage.Ok()) { | |
663 | *this = loadimage; | |
664 | return true; | |
665 | } | |
666 | } | |
667 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
668 | return false; | |
e9576ca5 SC |
669 | } |
670 | ||
a8562f55 | 671 | bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
672 | { |
673 | UnRef(); | |
674 | ||
675 | m_refData = new wxBitmapRefData; | |
676 | ||
677 | wxBitmapHandler *handler = FindHandler(type); | |
678 | ||
679 | if ( handler == NULL ) { | |
680 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
681 | ||
682 | return FALSE; | |
683 | } | |
684 | ||
685 | return handler->Create(this, data, type, width, height, depth); | |
686 | } | |
687 | ||
fec19ea9 VS |
688 | wxBitmap::wxBitmap(const wxImage& image, int depth) |
689 | { | |
690 | wxCHECK_RET( image.Ok(), wxT("invalid image") ) | |
691 | wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") ) | |
692 | ||
693 | m_refData = new wxBitmapRefData(); | |
694 | ||
fec19ea9 VS |
695 | // width and height of the device-dependent bitmap |
696 | int width = image.GetWidth(); | |
697 | int height = image.GetHeight(); | |
698 | ||
699 | // Create picture | |
700 | ||
71789654 | 701 | Create( width , height , 32 ) ; |
fec19ea9 VS |
702 | |
703 | CGrafPtr origPort ; | |
704 | GDHandle origDevice ; | |
705 | ||
76a5e5d2 | 706 | PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ; |
71789654 | 707 | LockPixels( pixMap ); |
fec19ea9 VS |
708 | |
709 | GetGWorld( &origPort , &origDevice ) ; | |
76a5e5d2 | 710 | SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ; |
fec19ea9 VS |
711 | |
712 | // Render image | |
fec19ea9 | 713 | register unsigned char* data = image.GetData(); |
71789654 SC |
714 | char* destinationBase = GetPixBaseAddr( pixMap ); |
715 | register unsigned char* destination = (unsigned char*) destinationBase ; | |
fec19ea9 VS |
716 | for (int y = 0; y < height; y++) |
717 | { | |
718 | for (int x = 0; x < width; x++) | |
719 | { | |
71789654 SC |
720 | *destination++ = 0 ; |
721 | *destination++ = *data++ ; | |
722 | *destination++ = *data++ ; | |
723 | *destination++ = *data++ ; | |
fec19ea9 | 724 | } |
71789654 SC |
725 | destinationBase += ((**pixMap).rowBytes & 0x7fff); |
726 | destination = (unsigned char*) destinationBase ; | |
727 | } | |
728 | if ( image.HasMask() ) | |
729 | { | |
730 | data = image.GetData(); | |
731 | ||
732 | wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue()); | |
733 | RGBColor white = { 0xffff, 0xffff, 0xffff }; | |
734 | RGBColor black = { 0 , 0 , 0 }; | |
735 | wxBitmap maskBitmap ; | |
fec19ea9 | 736 | |
71789654 | 737 | maskBitmap.Create( width, height, 1); |
76a5e5d2 SC |
738 | LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); |
739 | SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL); | |
71789654 SC |
740 | |
741 | for (int y = 0; y < height; y++) | |
742 | { | |
743 | for (int x = 0; x < width; x++) | |
744 | { | |
2af9ef13 | 745 | if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() ) |
71789654 SC |
746 | { |
747 | SetCPixel(x,y, &white); | |
748 | } | |
749 | else { | |
750 | SetCPixel(x,y, &black); | |
751 | } | |
752 | data += 3 ; | |
753 | } | |
754 | } // for height | |
76a5e5d2 | 755 | SetGWorld( (GWorldPtr) GetHBITMAP(), NULL); |
71789654 | 756 | SetMask(new wxMask( maskBitmap )); |
76a5e5d2 | 757 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); |
fec19ea9 VS |
758 | } |
759 | ||
76a5e5d2 | 760 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) ); |
fec19ea9 VS |
761 | SetGWorld( origPort, origDevice ); |
762 | } | |
763 | ||
764 | wxImage wxBitmap::ConvertToImage() const | |
765 | { | |
766 | wxImage image; | |
767 | ||
768 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
769 | ||
770 | // create an wxImage object | |
771 | int width = GetWidth(); | |
772 | int height = GetHeight(); | |
773 | image.Create( width, height ); | |
774 | ||
775 | unsigned char *data = image.GetData(); | |
776 | ||
777 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); | |
778 | ||
76a5e5d2 | 779 | GWorldPtr origPort; |
fec19ea9 | 780 | GDHandle origDevice; |
2b5f62a0 VZ |
781 | RgnHandle maskRgn = NULL ; |
782 | GWorldPtr tempPort = NULL ; | |
fec19ea9 VS |
783 | int index; |
784 | RGBColor color; | |
785 | // background color set to RGB(16,16,16) in consistent with wxGTK | |
786 | unsigned char mask_r=16, mask_g=16, mask_b=16; | |
787 | SInt16 r,g,b; | |
788 | wxMask *mask = GetMask(); | |
789 | ||
790 | GetGWorld( &origPort, &origDevice ); | |
2b5f62a0 VZ |
791 | if ( GetBitmapType() != kMacBitmapTypeGrafWorld ) |
792 | { | |
793 | tempPort = wxMacCreateGWorld( width , height , -1) ; | |
794 | } | |
795 | else | |
796 | { | |
797 | tempPort = (GWorldPtr) GetHBITMAP() ; | |
798 | } | |
799 | LockPixels(GetGWorldPixMap(tempPort)); | |
800 | SetGWorld( tempPort, NULL); | |
801 | if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon ) | |
802 | { | |
803 | Rect bitmaprect = { 0 , 0 , height, width }; | |
804 | if ( GetBitmapType() == kMacBitmapTypeIcon ) | |
805 | { | |
806 | ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ; | |
807 | maskRgn = NewRgn() ; | |
808 | BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ; | |
809 | } | |
810 | else | |
811 | ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ; | |
812 | } | |
fec19ea9 VS |
813 | // Copy data into image |
814 | index = 0; | |
815 | for (int yy = 0; yy < height; yy++) | |
816 | { | |
817 | for (int xx = 0; xx < width; xx++) | |
818 | { | |
819 | GetCPixel(xx,yy, &color); | |
820 | r = ((color.red ) >> 8); | |
821 | g = ((color.green ) >> 8); | |
822 | b = ((color.blue ) >> 8); | |
823 | data[index ] = r; | |
824 | data[index + 1] = g; | |
825 | data[index + 2] = b; | |
2b5f62a0 VZ |
826 | if ( maskRgn ) |
827 | { | |
828 | Point pt ; | |
829 | pt.h = xx ; | |
830 | pt.v = yy ; | |
831 | if ( !PtInRgn( pt , maskRgn ) ) | |
832 | { | |
fec19ea9 VS |
833 | data[index ] = mask_r; |
834 | data[index + 1] = mask_g; | |
835 | data[index + 2] = mask_b; | |
2b5f62a0 VZ |
836 | } |
837 | } | |
838 | else | |
839 | { | |
840 | if (mask) | |
841 | { | |
842 | if (mask->PointMasked(xx,yy)) | |
843 | { | |
844 | data[index ] = mask_r; | |
845 | data[index + 1] = mask_g; | |
846 | data[index + 2] = mask_b; | |
847 | } | |
848 | } | |
fec19ea9 VS |
849 | } |
850 | index += 3; | |
851 | } | |
852 | } | |
2b5f62a0 | 853 | if (mask || maskRgn ) |
fec19ea9 VS |
854 | { |
855 | image.SetMaskColour( mask_r, mask_g, mask_b ); | |
856 | image.SetMask( true ); | |
857 | } | |
858 | ||
859 | // Free resources | |
2b5f62a0 | 860 | UnlockPixels(GetGWorldPixMap( tempPort )); |
fec19ea9 | 861 | SetGWorld(origPort, origDevice); |
2b5f62a0 VZ |
862 | if ( GetBitmapType() != kMacBitmapTypeGrafWorld ) |
863 | { | |
864 | wxMacDestroyGWorld( tempPort ) ; | |
865 | } | |
866 | if ( maskRgn ) | |
867 | { | |
868 | DisposeRgn( maskRgn ) ; | |
869 | } | |
fec19ea9 VS |
870 | |
871 | return image; | |
872 | } | |
873 | ||
874 | ||
a8562f55 GD |
875 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, |
876 | const wxPalette *palette) const | |
e9576ca5 SC |
877 | { |
878 | wxBitmapHandler *handler = FindHandler(type); | |
879 | ||
a8562f55 GD |
880 | if ( handler ) |
881 | { | |
882 | return handler->SaveFile(this, filename, type, palette); | |
883 | } | |
884 | else | |
885 | { | |
886 | wxImage image = ConvertToImage(); | |
e9576ca5 | 887 | |
a8562f55 GD |
888 | return image.SaveFile(filename, type); |
889 | } | |
890 | ||
891 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
892 | return false; | |
e9576ca5 SC |
893 | } |
894 | ||
5fde6fcc GD |
895 | bool wxBitmap::Ok() const |
896 | { | |
897 | return (M_BITMAPDATA && M_BITMAPDATA->m_ok); | |
898 | } | |
899 | ||
900 | int wxBitmap::GetHeight() const | |
901 | { | |
902 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
903 | ||
904 | return M_BITMAPDATA->m_height; | |
905 | } | |
906 | ||
907 | int wxBitmap::GetWidth() const | |
908 | { | |
909 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
910 | ||
911 | return M_BITMAPDATA->m_width; | |
912 | } | |
913 | ||
914 | int wxBitmap::GetDepth() const | |
915 | { | |
916 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
917 | ||
918 | return M_BITMAPDATA->m_depth; | |
919 | } | |
920 | ||
921 | int wxBitmap::GetQuality() const | |
922 | { | |
923 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
924 | ||
925 | return M_BITMAPDATA->m_quality; | |
926 | } | |
927 | ||
928 | wxMask *wxBitmap::GetMask() const | |
929 | { | |
930 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
931 | ||
932 | return M_BITMAPDATA->m_bitmapMask; | |
933 | } | |
934 | ||
e9576ca5 SC |
935 | void wxBitmap::SetWidth(int w) |
936 | { | |
937 | if (!M_BITMAPDATA) | |
938 | m_refData = new wxBitmapRefData; | |
939 | ||
940 | M_BITMAPDATA->m_width = w; | |
941 | } | |
942 | ||
943 | void wxBitmap::SetHeight(int h) | |
944 | { | |
945 | if (!M_BITMAPDATA) | |
946 | m_refData = new wxBitmapRefData; | |
947 | ||
948 | M_BITMAPDATA->m_height = h; | |
949 | } | |
950 | ||
951 | void wxBitmap::SetDepth(int d) | |
952 | { | |
953 | if (!M_BITMAPDATA) | |
954 | m_refData = new wxBitmapRefData; | |
955 | ||
956 | M_BITMAPDATA->m_depth = d; | |
957 | } | |
958 | ||
959 | void wxBitmap::SetQuality(int q) | |
960 | { | |
961 | if (!M_BITMAPDATA) | |
962 | m_refData = new wxBitmapRefData; | |
963 | ||
964 | M_BITMAPDATA->m_quality = q; | |
965 | } | |
966 | ||
967 | void wxBitmap::SetOk(bool isOk) | |
968 | { | |
969 | if (!M_BITMAPDATA) | |
970 | m_refData = new wxBitmapRefData; | |
971 | ||
972 | M_BITMAPDATA->m_ok = isOk; | |
973 | } | |
974 | ||
5fde6fcc GD |
975 | wxPalette *wxBitmap::GetPalette() const |
976 | { | |
977 | wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") ); | |
978 | ||
979 | return &M_BITMAPDATA->m_bitmapPalette; | |
980 | } | |
981 | ||
e9576ca5 SC |
982 | void wxBitmap::SetPalette(const wxPalette& palette) |
983 | { | |
984 | if (!M_BITMAPDATA) | |
985 | m_refData = new wxBitmapRefData; | |
986 | ||
987 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
988 | } | |
989 | ||
990 | void wxBitmap::SetMask(wxMask *mask) | |
991 | { | |
992 | if (!M_BITMAPDATA) | |
993 | m_refData = new wxBitmapRefData; | |
994 | ||
a8562f55 GD |
995 | // Remove existing mask if there is one. |
996 | if (M_BITMAPDATA->m_bitmapMask) | |
997 | delete M_BITMAPDATA->m_bitmapMask; | |
998 | ||
e9576ca5 SC |
999 | M_BITMAPDATA->m_bitmapMask = mask ; |
1000 | } | |
1001 | ||
5fde6fcc GD |
1002 | WXHBITMAP wxBitmap::GetHBITMAP() const |
1003 | { | |
1004 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
1005 | ||
76a5e5d2 | 1006 | return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap); |
5fde6fcc GD |
1007 | } |
1008 | ||
76a5e5d2 | 1009 | WXHMETAFILE wxBitmap::GetPict() const |
5fde6fcc GD |
1010 | { |
1011 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
1012 | ||
1013 | PicHandle picture; // This is the returned picture | |
1014 | ||
1015 | // If bitmap already in Pict format return pointer | |
1016 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { | |
1017 | return M_BITMAPDATA->m_hPict; | |
1018 | } | |
1019 | else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { | |
1020 | // Invalid bitmap | |
1021 | return NULL; | |
1022 | } | |
1023 | ||
1024 | RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; | |
1025 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; | |
1026 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; | |
1027 | CGrafPtr origPort; | |
1028 | GDHandle origDev ; | |
1029 | wxMask *mask; | |
1030 | Rect portRect ; | |
1031 | ||
76a5e5d2 | 1032 | GetPortBounds( (GWorldPtr) GetHBITMAP() , &portRect ) ; |
5fde6fcc GD |
1033 | int width = portRect.right - portRect.left ; |
1034 | int height = portRect.bottom - portRect.top ; | |
1035 | ||
76a5e5d2 | 1036 | LockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; |
5fde6fcc GD |
1037 | GetGWorld( &origPort , &origDev ) ; |
1038 | ||
1039 | mask = GetMask(); | |
1040 | ||
76a5e5d2 | 1041 | SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ; |
5fde6fcc GD |
1042 | |
1043 | picture = OpenPicture(&portRect); // open a picture, this disables drawing | |
1044 | if(!picture) { | |
1045 | return NULL; | |
1046 | } | |
1047 | ||
1048 | if( mask ) | |
1049 | { | |
f5c6eb5c | 1050 | #ifdef __DARWIN__ |
5fde6fcc GD |
1051 | RGBColor trans = white; |
1052 | #else | |
1053 | RGBBackColor( &gray ); | |
1054 | EraseRect( &portRect ); | |
1055 | RGBColor trans = gray; | |
1056 | #endif | |
1057 | RGBForeColor( &black ) ; | |
1058 | RGBBackColor( &white ) ; | |
1059 | PenMode(transparent); | |
1060 | ||
1061 | for ( int y = 0 ; y < height ; ++y ) | |
1062 | { | |
1063 | for( int x = 0 ; x < width ; ++x ) | |
1064 | { | |
1065 | if ( !mask->PointMasked(x,y) ) | |
1066 | { | |
1067 | RGBColor col ; | |
1068 | ||
1069 | GetCPixel( x + portRect.left , y + portRect.top , &col ) ; | |
1070 | SetCPixel( x + portRect.left , y + portRect.top , &col ) ; | |
1071 | } | |
1072 | else { | |
1073 | // With transparency this sets a blank pixel | |
1074 | SetCPixel( x + portRect.left , y + portRect.top , &trans); | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | RGBBackColor( &gray ) ; | |
1082 | EraseRect(&portRect); | |
1083 | RGBForeColor( &black ) ; | |
1084 | RGBBackColor( &white ) ; | |
1085 | ||
76a5e5d2 | 1086 | CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), |
5fde6fcc | 1087 | // src PixMap - we copy image over itself - |
76a5e5d2 | 1088 | GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), |
5fde6fcc GD |
1089 | // dst PixMap - no drawing occurs |
1090 | &portRect, // srcRect - it will be recorded and compressed - | |
1091 | &portRect, // dstRect - into the picture that is open - | |
1092 | srcCopy,NULL); // copyMode and no clip region | |
1093 | } | |
1094 | ClosePicture(); // We are done recording the picture | |
76a5e5d2 | 1095 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; |
5fde6fcc GD |
1096 | SetGWorld( origPort , origDev ) ; |
1097 | ||
1098 | return picture; // return our groovy pict handle | |
1099 | } | |
1100 | ||
e9576ca5 SC |
1101 | /* |
1102 | * wxMask | |
1103 | */ | |
1104 | ||
1105 | wxMask::wxMask() | |
be52b341 | 1106 | : m_maskBitmap(NULL) |
e9576ca5 | 1107 | { |
e9576ca5 SC |
1108 | } |
1109 | ||
1110 | // Construct a mask from a bitmap and a colour indicating | |
1111 | // the transparent area | |
1112 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
be52b341 | 1113 | : m_maskBitmap(NULL) |
e9576ca5 | 1114 | { |
e9576ca5 SC |
1115 | Create(bitmap, colour); |
1116 | } | |
1117 | ||
1118 | // Construct a mask from a bitmap and a palette index indicating | |
1119 | // the transparent area | |
1120 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
be52b341 | 1121 | : m_maskBitmap(NULL) |
e9576ca5 | 1122 | { |
e9576ca5 SC |
1123 | Create(bitmap, paletteIndex); |
1124 | } | |
1125 | ||
1126 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1127 | wxMask::wxMask(const wxBitmap& bitmap) | |
be52b341 | 1128 | : m_maskBitmap(NULL) |
e9576ca5 | 1129 | { |
e9576ca5 SC |
1130 | Create(bitmap); |
1131 | } | |
1132 | ||
1133 | wxMask::~wxMask() | |
1134 | { | |
4e9ed364 RR |
1135 | if ( m_maskBitmap ) |
1136 | { | |
76a5e5d2 | 1137 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; |
4e9ed364 RR |
1138 | m_maskBitmap = NULL ; |
1139 | } | |
e9576ca5 SC |
1140 | } |
1141 | ||
1142 | // Create a mask from a mono bitmap (copies the bitmap). | |
1143 | bool wxMask::Create(const wxBitmap& bitmap) | |
1144 | { | |
5fde6fcc GD |
1145 | if ( m_maskBitmap ) |
1146 | { | |
76a5e5d2 | 1147 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; |
5fde6fcc GD |
1148 | m_maskBitmap = NULL ; |
1149 | } | |
1150 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, | |
1151 | wxT("Cannot create mask from this bitmap type (TODO)")); | |
1152 | // other types would require a temporary bitmap. not yet implemented | |
1153 | ||
1154 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap")); | |
1155 | ||
1156 | wxCHECK_MSG(bitmap.GetDepth() == 1, false, | |
1157 | wxT("Cannot create mask from colour bitmap")); | |
1158 | ||
1159 | m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), 1); | |
1160 | Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() }; | |
1161 | ||
76a5e5d2 SC |
1162 | LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) ); |
1163 | LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) ); | |
1164 | CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()), | |
1165 | GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap), | |
5fde6fcc | 1166 | &rect, &rect, srcCopy, 0); |
76a5e5d2 SC |
1167 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) ); |
1168 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) ); | |
5fde6fcc GD |
1169 | |
1170 | return FALSE; | |
e9576ca5 SC |
1171 | } |
1172 | ||
1173 | // Create a mask from a bitmap and a palette index indicating | |
1174 | // the transparent area | |
1175 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
1176 | { | |
be52b341 GD |
1177 | // TODO |
1178 | wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented")); | |
e9576ca5 SC |
1179 | return FALSE; |
1180 | } | |
1181 | ||
1182 | // Create a mask from a bitmap and a colour indicating | |
1183 | // the transparent area | |
1184 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1185 | { | |
4e9ed364 RR |
1186 | if ( m_maskBitmap ) |
1187 | { | |
76a5e5d2 | 1188 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; |
4e9ed364 RR |
1189 | m_maskBitmap = NULL ; |
1190 | } | |
1191 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, | |
5fde6fcc | 1192 | wxT("Cannot create mask from this bitmap type (TODO)")); |
4e9ed364 RR |
1193 | // other types would require a temporary bitmap. not yet implemented |
1194 | ||
5fde6fcc | 1195 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap")); |
8208e181 | 1196 | |
4e9ed364 | 1197 | m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 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 }}; | |
4e9ed364 RR |
1210 | |
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++) | |
4e9ed364 | 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 |
1236 | bool 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 |
1261 | wxBitmapHandler::~wxBitmapHandler() |
1262 | { | |
1263 | } | |
1264 | ||
e9576ca5 SC |
1265 | bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) |
1266 | { | |
1267 | return FALSE; | |
1268 | } | |
1269 | ||
a8562f55 | 1270 | bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
e9576ca5 SC |
1271 | int desiredWidth, int desiredHeight) |
1272 | { | |
1273 | return FALSE; | |
1274 | } | |
1275 | ||
a8562f55 | 1276 | bool 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 |
1285 | class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler |
1286 | { | |
1287 | DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) | |
1288 | public: | |
1289 | inline wxPICTResourceHandler() | |
1290 | { | |
1291 | m_name = "Macintosh Pict resource"; | |
1292 | m_extension = ""; | |
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 | }; | |
1299 | IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) | |
1300 | ||
1301 | bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1302 | int desiredWidth, int desiredHeight) | |
1303 | { | |
4e9ed364 RR |
1304 | Str255 theName ; |
1305 | ||
03e11df5 | 1306 | #if TARGET_CARBON |
4e9ed364 | 1307 | c2pstrcpy( (StringPtr) theName , name ) ; |
03e11df5 | 1308 | #else |
4e9ed364 RR |
1309 | strcpy( (char *) theName , name ) ; |
1310 | c2pstr( (char *)theName ) ; | |
03e11df5 | 1311 | #endif |
4e9ed364 RR |
1312 | |
1313 | PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; | |
1314 | if ( thePict ) | |
1315 | { | |
1316 | PictInfo theInfo ; | |
1317 | ||
1318 | GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ; | |
1319 | DetachResource( (Handle) thePict ) ; | |
1320 | M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ; | |
1321 | M_BITMAPHANDLERDATA->m_hPict = thePict ; | |
1322 | M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ; | |
1323 | M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ; | |
1324 | ||
1325 | M_BITMAPHANDLERDATA->m_depth = theInfo.depth ; | |
1326 | M_BITMAPHANDLERDATA->m_ok = true ; | |
1327 | M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ; | |
1328 | // M_BITMAPHANDLERDATA->m_bitmapPalette; | |
1329 | // M_BITMAPHANDLERDATA->m_quality; | |
1330 | return TRUE ; | |
1331 | } | |
1332 | return FALSE ; | |
519cb848 SC |
1333 | } |
1334 | ||
e9576ca5 SC |
1335 | void wxBitmap::InitStandardHandlers() |
1336 | { | |
973b0afb GD |
1337 | AddHandler(new wxPICTResourceHandler) ; |
1338 | AddHandler(new wxICONResourceHandler) ; | |
e9576ca5 | 1339 | } |