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