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