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