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