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