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