]>
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 | #include "wx/defs.h" | |
13 | ||
14 | #include "wx/bitmap.h" | |
15 | #include "wx/icon.h" | |
16 | #include "wx/log.h" | |
17 | #include "wx/image.h" | |
18 | #include "wx/xpmdecod.h" | |
19 | ||
20 | #include "wx/rawbmp.h" | |
21 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) | |
23 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
24 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject ) | |
25 | ||
26 | #ifdef __DARWIN__ | |
27 | #include <ApplicationServices/ApplicationServices.h> | |
28 | #else | |
29 | #include <PictUtils.h> | |
30 | #endif | |
31 | ||
32 | #include "wx/mac/uma.h" | |
33 | ||
34 | CTabHandle wxMacCreateColorTable( int numColors ) | |
35 | { | |
36 | CTabHandle newColors; /* Handle to the new color table */ | |
37 | ||
38 | /* Allocate memory for the color table */ | |
39 | newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) + | |
40 | sizeof (ColorSpec) * (numColors - 1) ); | |
41 | if (newColors != nil) | |
42 | { | |
43 | /* Initialize the fields */ | |
44 | (**newColors).ctSeed = GetCTSeed(); | |
45 | (**newColors).ctFlags = 0; | |
46 | (**newColors).ctSize = numColors - 1; | |
47 | /* Initialize the table of colors */ | |
48 | } | |
49 | return newColors ; | |
50 | } | |
51 | ||
52 | void wxMacDestroyColorTable( CTabHandle colors ) | |
53 | { | |
54 | DisposeHandle( (Handle) colors ) ; | |
55 | } | |
56 | ||
57 | void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue ) | |
58 | { | |
59 | (**newColors).ctTable[index].value = index; | |
60 | (**newColors).ctTable[index].rgb.red = red ; // someRedValue; | |
61 | (**newColors).ctTable[index].rgb.green = green ; // someGreenValue; | |
62 | (**newColors).ctTable[index].rgb.blue = blue ; // someBlueValue; | |
63 | } | |
64 | ||
65 | GWorldPtr wxMacCreateGWorld( int width , int height , int depth ) | |
66 | { | |
67 | OSErr err = noErr ; | |
68 | GWorldPtr port ; | |
69 | Rect rect = { 0 , 0 , height , width } ; | |
70 | ||
71 | if ( depth < 0 ) | |
72 | { | |
73 | depth = wxDisplayDepth() ; | |
74 | } | |
75 | ||
76 | err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ; | |
77 | if ( err == noErr ) | |
78 | { | |
79 | return port ; | |
80 | } | |
81 | return NULL ; | |
82 | } | |
83 | ||
84 | void wxMacDestroyGWorld( GWorldPtr gw ) | |
85 | { | |
86 | if ( gw ) | |
87 | DisposeGWorld( gw ) ; | |
88 | } | |
89 | ||
90 | #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */ | |
91 | ||
92 | OSErr SetupCIconHandlePixMap( CIconHandle icon , short depth , Rect *bounds , CTabHandle colors ) | |
93 | { | |
94 | CTabHandle newColors; /* Color table used for the off-screen PixMap */ | |
95 | Ptr offBaseAddr; /* Pointer to the off-screen pixel image */ | |
96 | OSErr error; /* Returns error code */ | |
97 | short bytesPerRow; /* Number of bytes per row in the PixMap */ | |
98 | ||
99 | ||
100 | error = noErr; | |
101 | newColors = nil; | |
102 | offBaseAddr = nil; | |
103 | ||
104 | bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4; | |
105 | ||
106 | /* Clone the clut if indexed color; allocate a dummy clut if direct color*/ | |
107 | if (depth <= 8) | |
108 | { | |
109 | newColors = colors; | |
110 | error = HandToHand((Handle *) &newColors); | |
111 | } | |
112 | else | |
113 | { | |
114 | newColors = (CTabHandle) NewHandle(sizeof(ColorTable) - | |
115 | sizeof(CSpecArray)); | |
116 | error = MemError(); | |
117 | } | |
118 | if (error == noErr) | |
119 | { | |
120 | /* Allocate pixel image; long integer multiplication avoids overflow */ | |
121 | (**icon).iconData = NewHandle((unsigned long) bytesPerRow * (bounds->bottom - | |
122 | bounds->top)); | |
123 | if ((**icon).iconData != nil) | |
124 | { | |
125 | /* Initialize fields common to indexed and direct PixMaps */ | |
126 | (**icon).iconPMap.baseAddr = 0; /* Point to image */ | |
127 | (**icon).iconPMap.rowBytes = bytesPerRow | /* MSB set for PixMap */ | |
128 | 0x8000; | |
129 | (**icon).iconPMap.bounds = *bounds; /* Use given bounds */ | |
130 | (**icon).iconPMap.pmVersion = 0; /* No special stuff */ | |
131 | (**icon).iconPMap.packType = 0; /* Default PICT pack */ | |
132 | (**icon).iconPMap.packSize = 0; /* Always zero in mem */ | |
133 | (**icon).iconPMap.hRes = kDefaultRes; /* 72 DPI default res */ | |
134 | (**icon).iconPMap.vRes = kDefaultRes; /* 72 DPI default res */ | |
135 | (**icon).iconPMap.pixelSize = depth; /* Set # bits/pixel */ | |
136 | ||
137 | /* Initialize fields specific to indexed and direct PixMaps */ | |
138 | if (depth <= 8) | |
139 | { | |
140 | /* PixMap is indexed */ | |
141 | (**icon).iconPMap.pixelType = 0; /* Indicates indexed */ | |
142 | (**icon).iconPMap.cmpCount = 1; /* Have 1 component */ | |
143 | (**icon).iconPMap.cmpSize = depth; /* Component size=depth */ | |
144 | (**icon).iconPMap.pmTable = newColors; /* Handle to CLUT */ | |
145 | } | |
146 | else | |
147 | { | |
148 | /* PixMap is direct */ | |
149 | (**icon).iconPMap.pixelType = RGBDirect; /* Indicates direct */ | |
150 | (**icon).iconPMap.cmpCount = 3; /* Have 3 components */ | |
151 | if (depth == 16) | |
152 | (**icon).iconPMap.cmpSize = 5; /* 5 bits/component */ | |
153 | else | |
154 | (**icon).iconPMap.cmpSize = 8; /* 8 bits/component */ | |
155 | (**newColors).ctSeed = 3 * (**icon).iconPMap.cmpSize; | |
156 | (**newColors).ctFlags = 0; | |
157 | (**newColors).ctSize = 0; | |
158 | (**icon).iconPMap.pmTable = newColors; | |
159 | } | |
160 | } | |
161 | else | |
162 | error = MemError(); | |
163 | } | |
164 | else | |
165 | newColors = nil; | |
166 | ||
167 | /* If no errors occurred, return a handle to the new off-screen PixMap */ | |
168 | if (error != noErr) | |
169 | { | |
170 | if (newColors != nil) | |
171 | DisposeCTable(newColors); | |
172 | } | |
173 | ||
174 | /* Return the error code */ | |
175 | return error; | |
176 | } | |
177 | ||
178 | CIconHandle wxMacCreateCIcon(GWorldPtr image , GWorldPtr mask , short dstDepth , short iconSize ) | |
179 | { | |
180 | GWorldPtr saveWorld; | |
181 | GDHandle saveHandle; | |
182 | ||
183 | GetGWorld(&saveWorld,&saveHandle); // save Graphics env state | |
184 | SetGWorld(image,nil); | |
185 | ||
186 | Rect frame = { 0 , 0 , iconSize , iconSize } ; | |
187 | Rect imageBounds = frame ; | |
188 | GetPortBounds( image , &imageBounds ) ; | |
189 | ||
190 | int bwSize = iconSize / 8 * iconSize ; | |
191 | CIconHandle icon = (CIconHandle) NewHandleClear( sizeof ( CIcon ) + 2 * bwSize) ; | |
192 | HLock((Handle)icon) ; | |
193 | SetupCIconHandlePixMap( icon , dstDepth , &frame,GetCTable(dstDepth)) ; | |
194 | HLock( (**icon).iconData ) ; | |
195 | (**icon).iconPMap.baseAddr = *(**icon).iconData ; | |
196 | ||
197 | LockPixels(GetGWorldPixMap(image)); | |
198 | ||
199 | CopyBits(GetPortBitMapForCopyBits(image), | |
200 | (BitMapPtr)&((**icon).iconPMap), | |
201 | &imageBounds, | |
202 | &imageBounds, | |
203 | srcCopy | ditherCopy, nil); | |
204 | ||
205 | ||
206 | UnlockPixels(GetGWorldPixMap(image)); | |
207 | HUnlock( (**icon).iconData ) ; | |
208 | ||
209 | (**icon).iconMask.rowBytes = iconSize / 8 ; | |
210 | (**icon).iconMask.bounds = frame ; | |
211 | ||
212 | (**icon).iconBMap.rowBytes = iconSize / 8 ; | |
213 | (**icon).iconBMap.bounds = frame ; | |
214 | (**icon).iconMask.baseAddr = (char*) &(**icon).iconMaskData ; | |
215 | (**icon).iconBMap.baseAddr = (char*) &(**icon).iconMaskData + bwSize ; | |
216 | ||
217 | if ( mask ) | |
218 | { | |
219 | Rect r ; | |
220 | GetPortBounds( image , &r ) ; | |
221 | LockPixels(GetGWorldPixMap(mask) ) ; | |
222 | CopyBits(GetPortBitMapForCopyBits(mask) , | |
223 | &(**icon).iconBMap , &r , &r, srcCopy , nil ) ; | |
224 | CopyBits(GetPortBitMapForCopyBits(mask) , | |
225 | &(**icon).iconMask , &r , &r, srcCopy , nil ) ; | |
226 | UnlockPixels(GetGWorldPixMap( mask ) ) ; | |
227 | } | |
228 | else | |
229 | { | |
230 | Rect r ; | |
231 | GetPortBounds( image , &r ) ; | |
232 | LockPixels(GetGWorldPixMap(image)); | |
233 | CopyBits(GetPortBitMapForCopyBits(image) , | |
234 | &(**icon).iconBMap , &r , &r, srcCopy , nil ) ; | |
235 | CopyBits(GetPortBitMapForCopyBits(image) , | |
236 | &(**icon).iconMask , &r , &r, srcCopy , nil ) ; | |
237 | UnlockPixels(GetGWorldPixMap(image)); | |
238 | } | |
239 | ||
240 | (**icon).iconMask.baseAddr = NULL ; | |
241 | (**icon).iconBMap.baseAddr = NULL ; | |
242 | (**icon).iconPMap.baseAddr = NULL ; | |
243 | HUnlock((Handle)icon) ; | |
244 | SetGWorld(saveWorld,saveHandle); | |
245 | ||
246 | return icon; | |
247 | } | |
248 | ||
249 | PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask) | |
250 | { | |
251 | CGrafPtr origPort ; | |
252 | GDHandle origDev ; | |
253 | ||
254 | PicHandle pict; | |
255 | ||
256 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; | |
257 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; | |
258 | ||
259 | GetGWorld( &origPort , &origDev ) ; | |
260 | ||
261 | RgnHandle clipRgn = NULL ; | |
262 | ||
263 | if ( mask ) | |
264 | { | |
265 | clipRgn = NewRgn() ; | |
266 | LockPixels( GetGWorldPixMap( mask ) ) ; | |
267 | BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ; | |
268 | UnlockPixels( GetGWorldPixMap( mask ) ) ; | |
269 | } | |
270 | ||
271 | SetGWorld( wp , NULL ) ; | |
272 | Rect portRect ; | |
273 | if ( clipRgn ) | |
274 | GetRegionBounds( clipRgn , &portRect ) ; | |
275 | else | |
276 | GetPortBounds( wp , &portRect ) ; | |
277 | pict = OpenPicture(&portRect); | |
278 | if(pict) | |
279 | { | |
280 | RGBForeColor( &black ) ; | |
281 | RGBBackColor( &white ) ; | |
282 | ||
283 | if ( clipRgn ) | |
284 | SetClip( clipRgn ) ; | |
285 | ||
286 | LockPixels( GetGWorldPixMap( wp ) ) ; | |
287 | CopyBits(GetPortBitMapForCopyBits(wp), | |
288 | GetPortBitMapForCopyBits(wp), | |
289 | &portRect, | |
290 | &portRect, | |
291 | srcCopy,clipRgn); | |
292 | UnlockPixels( GetGWorldPixMap( wp ) ) ; | |
293 | ClosePicture(); | |
294 | } | |
295 | SetGWorld( origPort , origDev ) ; | |
296 | if ( clipRgn ) | |
297 | DisposeRgn( clipRgn ) ; | |
298 | return pict; | |
299 | } | |
300 | ||
301 | void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) | |
302 | { | |
303 | memset( info , 0 , sizeof(ControlButtonContentInfo) ) ; | |
304 | if ( bitmap.Ok() ) | |
305 | { | |
306 | wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ; | |
307 | if ( bmap == NULL ) | |
308 | return ; | |
309 | ||
310 | if ( bmap->m_bitmapType == kMacBitmapTypePict ) | |
311 | { | |
312 | info->contentType = kControlContentPictHandle ; | |
313 | info->u.picture = MAC_WXHMETAFILE(bmap->m_hPict) ; | |
314 | } | |
315 | else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld ) | |
316 | { | |
317 | if ( (forceType == kControlContentCIconHandle || ( bmap->m_width == bmap->m_height && forceType != kControlContentPictHandle ) ) && ((bmap->m_width & 0x3) == 0) ) | |
318 | { | |
319 | info->contentType = kControlContentCIconHandle ; | |
320 | if ( bitmap.GetMask() ) | |
321 | { | |
322 | info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap()) , | |
323 | 8 , bmap->m_width ) ; | |
324 | } | |
325 | else | |
326 | { | |
327 | info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL , | |
328 | 8 , bmap->m_width ) ; | |
329 | } | |
330 | } | |
331 | else | |
332 | { | |
333 | info->contentType = kControlContentPictHandle ; | |
334 | if ( bitmap.GetMask() ) | |
335 | { | |
336 | info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap() ) ) ; | |
337 | } | |
338 | else | |
339 | { | |
340 | info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ) ; | |
341 | } | |
342 | } | |
343 | } | |
344 | else if ( bmap->m_bitmapType == kMacBitmapTypeIcon ) | |
345 | { | |
346 | info->contentType = kControlContentCIconHandle ; | |
347 | info->u.cIconHandle = MAC_WXHICON(bmap->m_hIcon) ; | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | wxBitmapRefData::wxBitmapRefData() | |
353 | : m_width(0) | |
354 | , m_height(0) | |
355 | , m_depth(0) | |
356 | , m_ok(FALSE) | |
357 | , m_numColors(0) | |
358 | , m_quality(0) | |
359 | { | |
360 | m_bitmapMask = NULL; | |
361 | m_hBitmap = NULL ; | |
362 | m_hPict = NULL ; | |
363 | m_hIcon = NULL ; | |
364 | m_bitmapType = kMacBitmapTypeUnknownType ; | |
365 | m_hasAlpha = false; | |
366 | } | |
367 | ||
368 | // TODO move this to a public function of Bitmap Ref | |
369 | static void DisposeBitmapRefData(wxBitmapRefData *data) | |
370 | { | |
371 | if ( !data ) | |
372 | return ; | |
373 | ||
374 | switch (data->m_bitmapType) | |
375 | { | |
376 | case kMacBitmapTypePict : | |
377 | { | |
378 | if ( data->m_hPict ) | |
379 | { | |
380 | KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ; | |
381 | data->m_hPict = NULL ; | |
382 | } | |
383 | } | |
384 | break ; | |
385 | case kMacBitmapTypeGrafWorld : | |
386 | { | |
387 | if ( data->m_hBitmap ) | |
388 | { | |
389 | wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ; | |
390 | data->m_hBitmap = NULL ; | |
391 | } | |
392 | } | |
393 | break ; | |
394 | case kMacBitmapTypeIcon : | |
395 | if ( data->m_hIcon ) | |
396 | { | |
397 | DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ; | |
398 | data->m_hIcon = NULL ; | |
399 | } | |
400 | ||
401 | default : | |
402 | // unkown type ? | |
403 | break ; | |
404 | } | |
405 | ||
406 | if (data->m_bitmapMask) | |
407 | { | |
408 | delete data->m_bitmapMask; | |
409 | data->m_bitmapMask = NULL; | |
410 | } | |
411 | } | |
412 | ||
413 | wxBitmapRefData::~wxBitmapRefData() | |
414 | { | |
415 | DisposeBitmapRefData( this ) ; | |
416 | } | |
417 | ||
418 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
419 | { | |
420 | Ref(icon) ; | |
421 | return true; | |
422 | } | |
423 | ||
424 | wxBitmap::wxBitmap() | |
425 | { | |
426 | m_refData = NULL; | |
427 | } | |
428 | ||
429 | wxBitmap::~wxBitmap() | |
430 | { | |
431 | } | |
432 | ||
433 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) | |
434 | { | |
435 | m_refData = new wxBitmapRefData; | |
436 | ||
437 | M_BITMAPDATA->m_width = the_width ; | |
438 | M_BITMAPDATA->m_height = the_height ; | |
439 | M_BITMAPDATA->m_depth = no_bits ; | |
440 | M_BITMAPDATA->m_numColors = 0; | |
441 | if ( no_bits == 1 ) | |
442 | { | |
443 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; | |
444 | M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( the_width , the_height , no_bits ) ; | |
445 | M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ; | |
446 | ||
447 | CGrafPtr origPort ; | |
448 | GDHandle origDevice ; | |
449 | ||
450 | GetGWorld( &origPort , &origDevice ) ; | |
451 | SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; | |
452 | LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ; | |
453 | ||
454 | // bits is a char array | |
455 | ||
456 | unsigned char* linestart = (unsigned char*) bits ; | |
457 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; | |
458 | if ( the_width % (sizeof(unsigned char) * 8) ) { | |
459 | linesize += sizeof(unsigned char); | |
460 | } | |
461 | ||
462 | RGBColor colors[2] = { | |
463 | { 0xFFFF , 0xFFFF , 0xFFFF } , | |
464 | { 0, 0 , 0 } | |
465 | } ; | |
466 | ||
467 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize ) | |
468 | { | |
469 | for ( int x = 0 ; x < the_width ; ++x ) | |
470 | { | |
471 | int index = x / 8 ; | |
472 | int bit = x % 8 ; | |
473 | int mask = 1 << bit ; | |
474 | if ( linestart[index] & mask ) | |
475 | { | |
476 | SetCPixel( x , y , &colors[1] ) ; | |
477 | } | |
478 | else | |
479 | { | |
480 | SetCPixel( x , y , &colors[0] ) ; | |
481 | } | |
482 | } | |
483 | } | |
484 | UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ; | |
485 | ||
486 | SetGWorld( origPort , origDevice ) ; | |
487 | } | |
488 | else | |
489 | { | |
490 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); | |
491 | } | |
492 | } | |
493 | ||
494 | wxBitmap::wxBitmap(int w, int h, int d) | |
495 | { | |
496 | (void)Create(w, h, d); | |
497 | } | |
498 | ||
499 | wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth) | |
500 | { | |
501 | (void) Create(data, type, width, height, depth); | |
502 | } | |
503 | ||
504 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) | |
505 | { | |
506 | LoadFile(filename, type); | |
507 | } | |
508 | ||
509 | bool wxBitmap::CreateFromXpm(const char **bits) | |
510 | { | |
511 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) | |
512 | wxXPMDecoder decoder; | |
513 | wxImage img = decoder.ReadData(bits); | |
514 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) | |
515 | *this = wxBitmap(img); | |
516 | return TRUE; | |
517 | } | |
518 | ||
519 | wxBitmap::wxBitmap(const char **bits) | |
520 | { | |
521 | (void) CreateFromXpm(bits); | |
522 | } | |
523 | ||
524 | wxBitmap::wxBitmap(char **bits) | |
525 | { | |
526 | (void) CreateFromXpm((const char **)bits); | |
527 | } | |
528 | ||
529 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const | |
530 | { | |
531 | wxCHECK_MSG( Ok() && | |
532 | (rect.x >= 0) && (rect.y >= 0) && | |
533 | (rect.x+rect.width <= GetWidth()) && | |
534 | (rect.y+rect.height <= GetHeight()), | |
535 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
536 | ||
537 | ||
538 | wxBitmap ret( rect.width, rect.height, GetDepth() ); | |
539 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
540 | ||
541 | GWorldPtr origPort; | |
542 | GDHandle origDevice; | |
543 | ||
544 | GetGWorld( &origPort, &origDevice ); | |
545 | ||
546 | // Update the subbitmaps reference data | |
547 | wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData(); | |
548 | ||
549 | ref->m_numColors = M_BITMAPDATA->m_numColors; | |
550 | #if wxUSE_PALETTE | |
551 | ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette; | |
552 | #endif // wxUSE_PALETTE | |
553 | ref->m_bitmapType = M_BITMAPDATA->m_bitmapType; | |
554 | ||
555 | // Copy sub region of this bitmap | |
556 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) | |
557 | { | |
558 | printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n"); | |
559 | } | |
560 | else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld) | |
561 | { | |
562 | // Copy mask | |
563 | if(GetMask()) | |
564 | { | |
565 | GWorldPtr submask, mask; | |
566 | RGBColor color; | |
567 | ||
568 | mask = (GWorldPtr) GetMask()->GetMaskBitmap(); | |
569 | submask = wxMacCreateGWorld(rect.width, rect.height, GetMask()->GetDepth() ); | |
570 | LockPixels(GetGWorldPixMap(mask)); | |
571 | LockPixels(GetGWorldPixMap(submask)); | |
572 | ||
573 | for(int yy = 0; yy < rect.height; yy++) | |
574 | { | |
575 | for(int xx = 0; xx < rect.width; xx++) | |
576 | { | |
577 | SetGWorld(mask, NULL); | |
578 | GetCPixel(rect.x + xx, rect.y + yy, &color); | |
579 | SetGWorld(submask, NULL); | |
580 | SetCPixel(xx,yy, &color); | |
581 | } | |
582 | } | |
583 | UnlockPixels(GetGWorldPixMap(mask)); | |
584 | UnlockPixels(GetGWorldPixMap(submask)); | |
585 | ref->m_bitmapMask = new wxMask; | |
586 | ref->m_bitmapMask->SetMaskBitmap(submask); | |
587 | } | |
588 | ||
589 | // Copy bitmap | |
590 | if(GetHBITMAP()) | |
591 | { | |
592 | GWorldPtr subbitmap, bitmap; | |
593 | RGBColor color; | |
594 | ||
595 | bitmap = (GWorldPtr) GetHBITMAP(); | |
596 | subbitmap = (GWorldPtr) ref->m_hBitmap ; | |
597 | LockPixels(GetGWorldPixMap(bitmap)); | |
598 | LockPixels(GetGWorldPixMap(subbitmap)); | |
599 | ||
600 | for(int yy = 0; yy < rect.height; yy++) | |
601 | { | |
602 | for(int xx = 0; xx < rect.width; xx++) | |
603 | { | |
604 | SetGWorld(bitmap, NULL); | |
605 | GetCPixel(rect.x + xx, rect.y + yy, &color); | |
606 | SetGWorld(subbitmap, NULL); | |
607 | SetCPixel(xx, yy, &color); | |
608 | } | |
609 | } | |
610 | UnlockPixels(GetGWorldPixMap(bitmap)); | |
611 | UnlockPixels(GetGWorldPixMap(subbitmap)); | |
612 | } | |
613 | } | |
614 | SetGWorld( origPort, origDevice ); | |
615 | ||
616 | return ret; | |
617 | } | |
618 | ||
619 | bool wxBitmap::Create(int w, int h, int d) | |
620 | { | |
621 | UnRef(); | |
622 | ||
623 | m_refData = new wxBitmapRefData; | |
624 | ||
625 | M_BITMAPDATA->m_width = w; | |
626 | M_BITMAPDATA->m_height = h; | |
627 | M_BITMAPDATA->m_depth = d; | |
628 | ||
629 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; | |
630 | M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ; | |
631 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; | |
632 | return M_BITMAPDATA->m_ok; | |
633 | } | |
634 | ||
635 | int wxBitmap::GetBitmapType() const | |
636 | { | |
637 | wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") ); | |
638 | ||
639 | return M_BITMAPDATA->m_bitmapType; | |
640 | } | |
641 | ||
642 | void wxBitmap::SetHBITMAP(WXHBITMAP bmp) | |
643 | { | |
644 | if (!M_BITMAPDATA) | |
645 | m_refData = new wxBitmapRefData; | |
646 | else | |
647 | DisposeBitmapRefData( M_BITMAPDATA ) ; | |
648 | ||
649 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; | |
650 | M_BITMAPDATA->m_hBitmap = bmp ; | |
651 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; | |
652 | } | |
653 | ||
654 | void wxBitmap::SetHICON(WXHICON ico) | |
655 | { | |
656 | if (!M_BITMAPDATA) | |
657 | m_refData = new wxBitmapRefData; | |
658 | else | |
659 | DisposeBitmapRefData( M_BITMAPDATA ) ; | |
660 | ||
661 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ; | |
662 | M_BITMAPDATA->m_hIcon = ico ; | |
663 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ; | |
664 | } | |
665 | ||
666 | void wxBitmap::SetPict(WXHMETAFILE pict) | |
667 | { | |
668 | if (!M_BITMAPDATA) | |
669 | m_refData = new wxBitmapRefData; | |
670 | else | |
671 | DisposeBitmapRefData( M_BITMAPDATA ) ; | |
672 | ||
673 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ; | |
674 | M_BITMAPDATA->m_hPict = pict ; | |
675 | M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ; | |
676 | } | |
677 | ||
678 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) | |
679 | { | |
680 | UnRef(); | |
681 | ||
682 | wxBitmapHandler *handler = FindHandler(type); | |
683 | ||
684 | if ( handler ) | |
685 | { | |
686 | m_refData = new wxBitmapRefData; | |
687 | ||
688 | return handler->LoadFile(this, filename, type, -1, -1); | |
689 | } | |
690 | else | |
691 | { | |
692 | wxImage loadimage(filename, type); | |
693 | if (loadimage.Ok()) { | |
694 | *this = loadimage; | |
695 | return true; | |
696 | } | |
697 | } | |
698 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
699 | return false; | |
700 | } | |
701 | ||
702 | bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth) | |
703 | { | |
704 | UnRef(); | |
705 | ||
706 | m_refData = new wxBitmapRefData; | |
707 | ||
708 | wxBitmapHandler *handler = FindHandler(type); | |
709 | ||
710 | if ( handler == NULL ) { | |
711 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
712 | ||
713 | return FALSE; | |
714 | } | |
715 | ||
716 | return handler->Create(this, data, type, width, height, depth); | |
717 | } | |
718 | ||
719 | wxBitmap::wxBitmap(const wxImage& image, int depth) | |
720 | { | |
721 | wxCHECK_RET( image.Ok(), wxT("invalid image") ) | |
722 | wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") ) | |
723 | ||
724 | m_refData = new wxBitmapRefData(); | |
725 | ||
726 | // width and height of the device-dependent bitmap | |
727 | int width = image.GetWidth(); | |
728 | int height = image.GetHeight(); | |
729 | ||
730 | // Create picture | |
731 | ||
732 | Create( width , height , 32 ) ; | |
733 | ||
734 | CGrafPtr origPort ; | |
735 | GDHandle origDevice ; | |
736 | ||
737 | PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ; | |
738 | LockPixels( pixMap ); | |
739 | ||
740 | GetGWorld( &origPort , &origDevice ) ; | |
741 | SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ; | |
742 | ||
743 | // Render image | |
744 | register unsigned char* data = image.GetData(); | |
745 | char* destinationBase = GetPixBaseAddr( pixMap ); | |
746 | register unsigned char* destination = (unsigned char*) destinationBase ; | |
747 | for (int y = 0; y < height; y++) | |
748 | { | |
749 | for (int x = 0; x < width; x++) | |
750 | { | |
751 | *destination++ = 0 ; | |
752 | *destination++ = *data++ ; | |
753 | *destination++ = *data++ ; | |
754 | *destination++ = *data++ ; | |
755 | } | |
756 | destinationBase += ((**pixMap).rowBytes & 0x7fff); | |
757 | destination = (unsigned char*) destinationBase ; | |
758 | } | |
759 | if ( image.HasAlpha() ) | |
760 | { | |
761 | unsigned char *alpha = image.GetAlpha(); | |
762 | ||
763 | wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue()); | |
764 | RGBColor color ; | |
765 | wxBitmap maskBitmap ; | |
766 | ||
767 | maskBitmap.Create( width, height, 24); | |
768 | LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); | |
769 | SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL); | |
770 | ||
771 | for (int y = 0; y < height; y++) | |
772 | { | |
773 | for (int x = 0; x < width; x++) | |
774 | { | |
775 | memset( &color , 255 - *alpha , sizeof( color ) ); | |
776 | SetCPixel(x,y, &color); | |
777 | ||
778 | alpha += 1 ; | |
779 | } | |
780 | } // for height | |
781 | SetGWorld( (GWorldPtr) GetHBITMAP(), NULL); | |
782 | SetMask(new wxMask( maskBitmap )); | |
783 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); | |
784 | } | |
785 | else if ( image.HasMask() ) | |
786 | { | |
787 | data = image.GetData(); | |
788 | ||
789 | wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue()); | |
790 | RGBColor white = { 0xffff, 0xffff, 0xffff }; | |
791 | RGBColor black = { 0 , 0 , 0 }; | |
792 | wxBitmap maskBitmap ; | |
793 | ||
794 | maskBitmap.Create( width, height, 1); | |
795 | LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); | |
796 | SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL); | |
797 | ||
798 | for (int y = 0; y < height; y++) | |
799 | { | |
800 | for (int x = 0; x < width; x++) | |
801 | { | |
802 | if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() ) | |
803 | { | |
804 | SetCPixel(x,y, &white); | |
805 | } | |
806 | else { | |
807 | SetCPixel(x,y, &black); | |
808 | } | |
809 | data += 3 ; | |
810 | } | |
811 | } // for height | |
812 | SetGWorld( (GWorldPtr) GetHBITMAP(), NULL); | |
813 | SetMask(new wxMask( maskBitmap )); | |
814 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) ); | |
815 | } | |
816 | ||
817 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) ); | |
818 | SetGWorld( origPort, origDevice ); | |
819 | } | |
820 | ||
821 | wxImage wxBitmap::ConvertToImage() const | |
822 | { | |
823 | wxImage image; | |
824 | ||
825 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
826 | ||
827 | // create an wxImage object | |
828 | int width = GetWidth(); | |
829 | int height = GetHeight(); | |
830 | image.Create( width, height ); | |
831 | ||
832 | unsigned char *data = image.GetData(); | |
833 | ||
834 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); | |
835 | ||
836 | GWorldPtr origPort; | |
837 | GDHandle origDevice; | |
838 | RgnHandle maskRgn = NULL ; | |
839 | GWorldPtr tempPort = NULL ; | |
840 | int index; | |
841 | RGBColor color; | |
842 | // background color set to RGB(16,16,16) in consistent with wxGTK | |
843 | unsigned char mask_r=16, mask_g=16, mask_b=16; | |
844 | SInt16 r,g,b; | |
845 | wxMask *mask = GetMask(); | |
846 | ||
847 | GetGWorld( &origPort, &origDevice ); | |
848 | if ( GetBitmapType() != kMacBitmapTypeGrafWorld ) | |
849 | { | |
850 | tempPort = wxMacCreateGWorld( width , height , -1) ; | |
851 | } | |
852 | else | |
853 | { | |
854 | tempPort = (GWorldPtr) GetHBITMAP() ; | |
855 | } | |
856 | LockPixels(GetGWorldPixMap(tempPort)); | |
857 | SetGWorld( tempPort, NULL); | |
858 | if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon ) | |
859 | { | |
860 | Rect bitmaprect = { 0 , 0 , height, width }; | |
861 | if ( GetBitmapType() == kMacBitmapTypeIcon ) | |
862 | { | |
863 | ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ; | |
864 | maskRgn = NewRgn() ; | |
865 | BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ; | |
866 | } | |
867 | else | |
868 | ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ; | |
869 | } | |
870 | // Copy data into image | |
871 | index = 0; | |
872 | for (int yy = 0; yy < height; yy++) | |
873 | { | |
874 | for (int xx = 0; xx < width; xx++) | |
875 | { | |
876 | GetCPixel(xx,yy, &color); | |
877 | r = ((color.red ) >> 8); | |
878 | g = ((color.green ) >> 8); | |
879 | b = ((color.blue ) >> 8); | |
880 | data[index ] = r; | |
881 | data[index + 1] = g; | |
882 | data[index + 2] = b; | |
883 | if ( maskRgn ) | |
884 | { | |
885 | Point pt ; | |
886 | pt.h = xx ; | |
887 | pt.v = yy ; | |
888 | if ( !PtInRgn( pt , maskRgn ) ) | |
889 | { | |
890 | data[index ] = mask_r; | |
891 | data[index + 1] = mask_g; | |
892 | data[index + 2] = mask_b; | |
893 | } | |
894 | } | |
895 | else | |
896 | { | |
897 | if (mask) | |
898 | { | |
899 | if (mask->PointMasked(xx,yy)) | |
900 | { | |
901 | data[index ] = mask_r; | |
902 | data[index + 1] = mask_g; | |
903 | data[index + 2] = mask_b; | |
904 | } | |
905 | } | |
906 | } | |
907 | index += 3; | |
908 | } | |
909 | } | |
910 | if (mask || maskRgn ) | |
911 | { | |
912 | image.SetMaskColour( mask_r, mask_g, mask_b ); | |
913 | image.SetMask( true ); | |
914 | } | |
915 | ||
916 | // Free resources | |
917 | UnlockPixels(GetGWorldPixMap( tempPort )); | |
918 | SetGWorld(origPort, origDevice); | |
919 | if ( GetBitmapType() != kMacBitmapTypeGrafWorld ) | |
920 | { | |
921 | wxMacDestroyGWorld( tempPort ) ; | |
922 | } | |
923 | if ( maskRgn ) | |
924 | { | |
925 | DisposeRgn( maskRgn ) ; | |
926 | } | |
927 | ||
928 | return image; | |
929 | } | |
930 | ||
931 | ||
932 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, | |
933 | const wxPalette *palette) const | |
934 | { | |
935 | wxBitmapHandler *handler = FindHandler(type); | |
936 | ||
937 | if ( handler ) | |
938 | { | |
939 | return handler->SaveFile(this, filename, type, palette); | |
940 | } | |
941 | else | |
942 | { | |
943 | wxImage image = ConvertToImage(); | |
944 | ||
945 | return image.SaveFile(filename, type); | |
946 | } | |
947 | ||
948 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
949 | return false; | |
950 | } | |
951 | ||
952 | bool wxBitmap::Ok() const | |
953 | { | |
954 | return (M_BITMAPDATA && M_BITMAPDATA->m_ok); | |
955 | } | |
956 | ||
957 | int wxBitmap::GetHeight() const | |
958 | { | |
959 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
960 | ||
961 | return M_BITMAPDATA->m_height; | |
962 | } | |
963 | ||
964 | int wxBitmap::GetWidth() const | |
965 | { | |
966 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
967 | ||
968 | return M_BITMAPDATA->m_width; | |
969 | } | |
970 | ||
971 | int wxBitmap::GetDepth() const | |
972 | { | |
973 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
974 | ||
975 | return M_BITMAPDATA->m_depth; | |
976 | } | |
977 | ||
978 | int wxBitmap::GetQuality() const | |
979 | { | |
980 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
981 | ||
982 | return M_BITMAPDATA->m_quality; | |
983 | } | |
984 | ||
985 | wxMask *wxBitmap::GetMask() const | |
986 | { | |
987 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
988 | ||
989 | return M_BITMAPDATA->m_bitmapMask; | |
990 | } | |
991 | ||
992 | void wxBitmap::SetWidth(int w) | |
993 | { | |
994 | if (!M_BITMAPDATA) | |
995 | m_refData = new wxBitmapRefData; | |
996 | ||
997 | M_BITMAPDATA->m_width = w; | |
998 | } | |
999 | ||
1000 | void wxBitmap::SetHeight(int h) | |
1001 | { | |
1002 | if (!M_BITMAPDATA) | |
1003 | m_refData = new wxBitmapRefData; | |
1004 | ||
1005 | M_BITMAPDATA->m_height = h; | |
1006 | } | |
1007 | ||
1008 | void wxBitmap::SetDepth(int d) | |
1009 | { | |
1010 | if (!M_BITMAPDATA) | |
1011 | m_refData = new wxBitmapRefData; | |
1012 | ||
1013 | M_BITMAPDATA->m_depth = d; | |
1014 | } | |
1015 | ||
1016 | void wxBitmap::SetQuality(int q) | |
1017 | { | |
1018 | if (!M_BITMAPDATA) | |
1019 | m_refData = new wxBitmapRefData; | |
1020 | ||
1021 | M_BITMAPDATA->m_quality = q; | |
1022 | } | |
1023 | ||
1024 | void wxBitmap::SetOk(bool isOk) | |
1025 | { | |
1026 | if (!M_BITMAPDATA) | |
1027 | m_refData = new wxBitmapRefData; | |
1028 | ||
1029 | M_BITMAPDATA->m_ok = isOk; | |
1030 | } | |
1031 | ||
1032 | #if wxUSE_PALETTE | |
1033 | wxPalette *wxBitmap::GetPalette() const | |
1034 | { | |
1035 | wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") ); | |
1036 | ||
1037 | return &M_BITMAPDATA->m_bitmapPalette; | |
1038 | } | |
1039 | ||
1040 | void wxBitmap::SetPalette(const wxPalette& palette) | |
1041 | { | |
1042 | if (!M_BITMAPDATA) | |
1043 | m_refData = new wxBitmapRefData; | |
1044 | ||
1045 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
1046 | } | |
1047 | #endif // wxUSE_PALETTE | |
1048 | ||
1049 | void wxBitmap::SetMask(wxMask *mask) | |
1050 | { | |
1051 | if (!M_BITMAPDATA) | |
1052 | m_refData = new wxBitmapRefData; | |
1053 | ||
1054 | // Remove existing mask if there is one. | |
1055 | delete M_BITMAPDATA->m_bitmapMask; | |
1056 | ||
1057 | M_BITMAPDATA->m_bitmapMask = mask ; | |
1058 | } | |
1059 | ||
1060 | WXHBITMAP wxBitmap::GetHBITMAP() const | |
1061 | { | |
1062 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
1063 | ||
1064 | return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap); | |
1065 | } | |
1066 | ||
1067 | WXHMETAFILE wxBitmap::GetPict( bool *created ) const | |
1068 | { | |
1069 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
1070 | ||
1071 | PicHandle picture = NULL ; // This is the returned picture | |
1072 | if ( created ) | |
1073 | (*created) = false ; | |
1074 | // If bitmap already in Pict format return pointer | |
1075 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { | |
1076 | return M_BITMAPDATA->m_hPict; | |
1077 | } | |
1078 | else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { | |
1079 | // Invalid bitmap | |
1080 | return NULL; | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | if ( GetMask() ) | |
1085 | { | |
1086 | picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ; | |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; | |
1091 | } | |
1092 | if ( created && picture ) | |
1093 | (*created) = true ; | |
1094 | } | |
1095 | return picture ; | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * wxMask | |
1100 | */ | |
1101 | ||
1102 | wxMask::wxMask() | |
1103 | : m_maskBitmap(NULL) | |
1104 | { | |
1105 | } | |
1106 | ||
1107 | // Construct a mask from a bitmap and a colour indicating | |
1108 | // the transparent area | |
1109 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
1110 | : m_maskBitmap(NULL) | |
1111 | { | |
1112 | Create(bitmap, colour); | |
1113 | } | |
1114 | ||
1115 | // Construct a mask from a bitmap and a palette index indicating | |
1116 | // the transparent area | |
1117 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
1118 | : m_maskBitmap(NULL) | |
1119 | { | |
1120 | Create(bitmap, paletteIndex); | |
1121 | } | |
1122 | ||
1123 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1124 | wxMask::wxMask(const wxBitmap& bitmap) | |
1125 | : m_maskBitmap(NULL) | |
1126 | { | |
1127 | Create(bitmap); | |
1128 | } | |
1129 | ||
1130 | wxMask::~wxMask() | |
1131 | { | |
1132 | if ( m_maskBitmap ) | |
1133 | { | |
1134 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; | |
1135 | m_maskBitmap = NULL ; | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | // Create a mask from a mono bitmap (copies the bitmap). | |
1140 | bool wxMask::Create(const wxBitmap& bitmap) | |
1141 | { | |
1142 | if ( m_maskBitmap ) | |
1143 | { | |
1144 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; | |
1145 | m_maskBitmap = NULL ; | |
1146 | } | |
1147 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, | |
1148 | wxT("Cannot create mask from this bitmap type (TODO)")); | |
1149 | // other types would require a temporary bitmap. not yet implemented | |
1150 | ||
1151 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap")); | |
1152 | ||
1153 | m_depth = bitmap.GetDepth() ; | |
1154 | m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), bitmap.GetDepth() ); | |
1155 | Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() }; | |
1156 | ||
1157 | LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) ); | |
1158 | LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) ); | |
1159 | CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()), | |
1160 | GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap), | |
1161 | &rect, &rect, srcCopy, 0); | |
1162 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) ); | |
1163 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) ); | |
1164 | ||
1165 | return FALSE; | |
1166 | } | |
1167 | ||
1168 | // Create a mask from a bitmap and a palette index indicating | |
1169 | // the transparent area | |
1170 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
1171 | { | |
1172 | // TODO | |
1173 | wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented")); | |
1174 | return FALSE; | |
1175 | } | |
1176 | ||
1177 | // Create a mask from a bitmap and a colour indicating | |
1178 | // the transparent area | |
1179 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1180 | { | |
1181 | if ( m_maskBitmap ) | |
1182 | { | |
1183 | wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ; | |
1184 | m_maskBitmap = NULL ; | |
1185 | } | |
1186 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, | |
1187 | wxT("Cannot create mask from this bitmap type (TODO)")); | |
1188 | // other types would require a temporary bitmap. not yet implemented | |
1189 | ||
1190 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap")); | |
1191 | ||
1192 | m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 ); | |
1193 | m_depth = 1 ; | |
1194 | LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap ) ); | |
1195 | LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ); | |
1196 | RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel()); | |
1197 | ||
1198 | // this is not very efficient, but I can't think | |
1199 | // of a better way of doing it | |
1200 | CGrafPtr origPort ; | |
1201 | GDHandle origDevice ; | |
1202 | RGBColor col; | |
1203 | RGBColor colors[2] = { | |
1204 | { 0xFFFF, 0xFFFF, 0xFFFF }, | |
1205 | { 0, 0, 0 }}; | |
1206 | ||
1207 | GetGWorld( &origPort , &origDevice ) ; | |
1208 | for (int w = 0; w < bitmap.GetWidth(); w++) | |
1209 | { | |
1210 | for (int h = 0; h < bitmap.GetHeight(); h++) | |
1211 | { | |
1212 | SetGWorld( (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ; | |
1213 | GetCPixel( w , h , &col ) ; | |
1214 | SetGWorld( (GWorldPtr) m_maskBitmap , NULL ) ; | |
1215 | if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue) | |
1216 | { | |
1217 | SetCPixel( w , h , &colors[0] ) ; | |
1218 | } | |
1219 | else | |
1220 | { | |
1221 | SetCPixel( w , h , &colors[1] ) ; | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ; | |
1226 | UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ) ; | |
1227 | SetGWorld( origPort , origDevice ) ; | |
1228 | ||
1229 | return TRUE; | |
1230 | } | |
1231 | ||
1232 | bool wxMask::PointMasked(int x, int y) | |
1233 | { | |
1234 | GWorldPtr origPort; | |
1235 | GDHandle origDevice; | |
1236 | RGBColor color; | |
1237 | bool masked = true; | |
1238 | ||
1239 | GetGWorld( &origPort, &origDevice); | |
1240 | ||
1241 | //Set port to mask and see if it masked (1) or not ( 0 ) | |
1242 | SetGWorld( (GWorldPtr) m_maskBitmap, NULL); | |
1243 | LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap)); | |
1244 | GetCPixel(x,y, &color); | |
1245 | masked = !(color.red == 0 && color.green == 0 && color.blue == 0); | |
1246 | UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap)); | |
1247 | ||
1248 | SetGWorld( origPort, origDevice); | |
1249 | ||
1250 | return masked; | |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * wxBitmapHandler | |
1255 | */ | |
1256 | ||
1257 | wxBitmapHandler::~wxBitmapHandler() | |
1258 | { | |
1259 | } | |
1260 | ||
1261 | bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) | |
1262 | { | |
1263 | return FALSE; | |
1264 | } | |
1265 | ||
1266 | bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1267 | int desiredWidth, int desiredHeight) | |
1268 | { | |
1269 | return FALSE; | |
1270 | } | |
1271 | ||
1272 | bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) | |
1273 | { | |
1274 | return FALSE; | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * Standard handlers | |
1279 | */ | |
1280 | ||
1281 | class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler | |
1282 | { | |
1283 | DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) | |
1284 | public: | |
1285 | inline wxPICTResourceHandler() | |
1286 | { | |
1287 | m_name = wxT("Macintosh Pict resource"); | |
1288 | m_extension = wxEmptyString; | |
1289 | m_type = wxBITMAP_TYPE_PICT_RESOURCE; | |
1290 | }; | |
1291 | ||
1292 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1293 | int desiredWidth, int desiredHeight); | |
1294 | }; | |
1295 | IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) | |
1296 | ||
1297 | bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1298 | int desiredWidth, int desiredHeight) | |
1299 | { | |
1300 | Str255 theName ; | |
1301 | wxMacStringToPascal( name , theName ) ; | |
1302 | ||
1303 | PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; | |
1304 | if ( thePict ) | |
1305 | { | |
1306 | PictInfo theInfo ; | |
1307 | ||
1308 | GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ; | |
1309 | DetachResource( (Handle) thePict ) ; | |
1310 | M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ; | |
1311 | M_BITMAPHANDLERDATA->m_hPict = thePict ; | |
1312 | M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ; | |
1313 | M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ; | |
1314 | ||
1315 | M_BITMAPHANDLERDATA->m_depth = theInfo.depth ; | |
1316 | M_BITMAPHANDLERDATA->m_ok = true ; | |
1317 | M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ; | |
1318 | // M_BITMAPHANDLERDATA->m_bitmapPalette; | |
1319 | // M_BITMAPHANDLERDATA->m_quality; | |
1320 | return TRUE ; | |
1321 | } | |
1322 | return FALSE ; | |
1323 | } | |
1324 | ||
1325 | void wxBitmap::InitStandardHandlers() | |
1326 | { | |
1327 | AddHandler(new wxPICTResourceHandler) ; | |
1328 | AddHandler(new wxICONResourceHandler) ; | |
1329 | } | |
1330 | ||
1331 | // ---------------------------------------------------------------------------- | |
1332 | // raw bitmap access support | |
1333 | // ---------------------------------------------------------------------------- | |
1334 | ||
1335 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) | |
1336 | { | |
1337 | if ( !Ok() ) | |
1338 | { | |
1339 | // no bitmap, no data (raw or otherwise) | |
1340 | return NULL; | |
1341 | } | |
1342 | ||
1343 | if ( M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld ) | |
1344 | { | |
1345 | wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") ); | |
1346 | ||
1347 | return NULL; | |
1348 | } | |
1349 | ||
1350 | GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap); | |
1351 | PixMapHandle hPixMap = GetGWorldPixMap(gworld); | |
1352 | wxCHECK_MSG( hPixMap && *hPixMap, NULL, | |
1353 | _T("GetRawData(): failed to get PixMap from GWorld?") ); | |
1354 | ||
1355 | wxCHECK_MSG( (*hPixMap)->pixelSize == bpp, NULL, | |
1356 | _T("GetRawData(): pixel format mismatch") ); | |
1357 | ||
1358 | if ( !LockPixels(hPixMap) ) | |
1359 | { | |
1360 | wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") ); | |
1361 | ||
1362 | return NULL; | |
1363 | } | |
1364 | ||
1365 | data.m_width = GetWidth(); | |
1366 | data.m_height = GetHeight(); | |
1367 | data.m_stride = (*hPixMap)->rowBytes & 0x7fff; | |
1368 | ||
1369 | M_BITMAPDATA->m_hasAlpha = false; | |
1370 | ||
1371 | return GetPixBaseAddr(hPixMap); | |
1372 | } | |
1373 | ||
1374 | void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) | |
1375 | { | |
1376 | if ( !Ok() ) | |
1377 | return; | |
1378 | ||
1379 | if ( M_BITMAPDATA->m_hasAlpha ) | |
1380 | { | |
1381 | wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase; | |
1382 | ||
1383 | int w = data.GetWidth(), | |
1384 | h = data.GetHeight(); | |
1385 | ||
1386 | wxBitmap bmpMask(GetWidth(), GetHeight(), 32); | |
1387 | wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h)); | |
1388 | wxAlphaPixelData::Iterator pMask(dataMask), | |
1389 | p(data); | |
1390 | for ( int y = 0; y < h; y++ ) | |
1391 | { | |
1392 | wxAlphaPixelData::Iterator rowStartMask = pMask, | |
1393 | rowStart = p; | |
1394 | ||
1395 | for ( int x = 0; x < w; x++ ) | |
1396 | { | |
1397 | const wxAlphaPixelData::Iterator::ChannelType | |
1398 | alpha = p.Alpha(); | |
1399 | ||
1400 | pMask.Red() = alpha; | |
1401 | pMask.Green() = alpha; | |
1402 | pMask.Blue() = alpha; | |
1403 | ||
1404 | ++p; | |
1405 | ++pMask; | |
1406 | } | |
1407 | ||
1408 | p = rowStart; | |
1409 | p.OffsetY(data, 1); | |
1410 | ||
1411 | pMask = rowStartMask; | |
1412 | pMask.OffsetY(dataMask, 1); | |
1413 | } | |
1414 | ||
1415 | SetMask(new wxMask(bmpMask)); | |
1416 | } | |
1417 | ||
1418 | GWorldPtr gworld = MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap); | |
1419 | PixMapHandle hPixMap = GetGWorldPixMap(gworld); | |
1420 | if ( hPixMap ) | |
1421 | { | |
1422 | UnlockPixels(hPixMap); | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | void wxBitmap::UseAlpha() | |
1427 | { | |
1428 | // remember that we are using alpha channel, we'll need to create a proper | |
1429 | // mask in UngetRawData() | |
1430 | M_BITMAPDATA->m_hasAlpha = true; | |
1431 | } | |
1432 |