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