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