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 |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a8e9860d | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
e9576ca5 SC |
13 | #pragma implementation "bitmap.h" |
14 | #endif | |
15 | ||
a8e9860d | 16 | #include "wx/wxprec.h" |
b698c8e9 | 17 | |
e9576ca5 SC |
18 | #include "wx/bitmap.h" |
19 | #include "wx/icon.h" | |
20 | #include "wx/log.h" | |
fec19ea9 | 21 | #include "wx/image.h" |
20b69855 | 22 | #include "wx/metafile.h" |
973b0afb | 23 | #include "wx/xpmdecod.h" |
e9576ca5 | 24 | |
55e18dbe VZ |
25 | #include "wx/rawbmp.h" |
26 | ||
e9576ca5 SC |
27 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) |
28 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
55e18dbe | 29 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject ) |
e9576ca5 | 30 | |
f5c6eb5c | 31 | #ifdef __DARWIN__ |
5fde6fcc | 32 | #include <ApplicationServices/ApplicationServices.h> |
03e11df5 GD |
33 | #else |
34 | #include <PictUtils.h> | |
35 | #endif | |
519cb848 | 36 | |
31d30995 SC |
37 | #include "wx/mac/uma.h" |
38 | ||
05d8deda RN |
39 | #include "wx/dcmemory.h" |
40 | ||
20b69855 SC |
41 | // Implementation Notes |
42 | // -------------------- | |
43 | // | |
44 | // we are always working with a 32 bit deep pixel buffer | |
45 | // under QuickDraw its alpha parts are going to be ignored in the GWorld, | |
46 | // therefore we have a separate GWorld there for blitting the mask in | |
47 | ||
48 | // under Quartz then content is transformed into a CGImageRef representing the same data | |
49 | // which can be transferred to the GPU by the OS for fast rendering | |
50 | ||
51 | // we don't dare premultiplied alpha yet | |
52 | #define wxMAC_USE_PREMULTIPLIED_ALPHA 0 | |
53 | ||
d45318b8 RN |
54 | #if wxUSE_BMPBUTTON |
55 | ||
20b69855 | 56 | void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) |
519cb848 | 57 | { |
20b69855 SC |
58 | memset( info , 0 , sizeof(ControlButtonContentInfo) ) ; |
59 | if ( bitmap.Ok() ) | |
60 | { | |
45cf8535 | 61 | wxBitmapRefData * bmap = bitmap.GetBitmapData() ; |
20b69855 SC |
62 | if ( bmap == NULL ) |
63 | return ; | |
b28aeea5 | 64 | |
45cf8535 | 65 | if ( ( bmap->HasNativeSize() && forceType == 0 ) || forceType == kControlContentIconRef ) |
b28aeea5 | 66 | { |
45cf8535 SC |
67 | wxBitmap scaleBmp ; |
68 | ||
69 | wxBitmapRefData* bmp = bmap ; | |
70 | ||
71 | if ( !bmap->HasNativeSize() ) | |
72 | { | |
73 | // as PICT conversion will only result in a 16x16 icon, let's attempt | |
74 | // a few scales for better results | |
75 | ||
76 | int w = bitmap.GetWidth() ; | |
77 | int h = bitmap.GetHeight() ; | |
78 | int sz = wxMax( w , h ) ; | |
79 | if ( sz == 24 || sz == 64) | |
80 | { | |
81 | scaleBmp = wxBitmap( bitmap.ConvertToImage().Scale( w * 2 , h * 2 ) ) ; | |
82 | bmp = scaleBmp.GetBitmapData() ; | |
83 | } | |
84 | } | |
85 | ||
b28aeea5 | 86 | info->contentType = kControlContentIconRef ; |
45cf8535 SC |
87 | info->u.iconRef = bmp->GetIconRef() ; |
88 | AcquireIconRef( info->u.iconRef ) ; | |
b28aeea5 | 89 | } |
86e94923 | 90 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 |
45cf8535 SC |
91 | else if ( forceType == kControlContentCGImageRef ) |
92 | { | |
93 | info->contentType = kControlContentCGImageRef ; | |
94 | info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ; | |
95 | } | |
96 | #endif | |
b28aeea5 SC |
97 | else |
98 | { | |
99 | info->contentType = kControlContentPictHandle ; | |
100 | info->u.picture = bmap->GetPictHandle() ; | |
101 | } | |
20b69855 | 102 | } |
519cb848 SC |
103 | } |
104 | ||
20b69855 | 105 | void wxMacReleaseBitmapButton( ControlButtonContentInfo*info ) |
519cb848 | 106 | { |
20b69855 | 107 | if ( info->contentType == kControlContentIconRef ) |
4e9ed364 | 108 | { |
45cf8535 | 109 | ReleaseIconRef( info->u.iconRef ) ; |
b28aeea5 | 110 | } |
531436ec SC |
111 | else if ( info->contentType == kControlNoContent ) |
112 | { | |
113 | // there's no bitmap at all, fall through silently | |
114 | } | |
b28aeea5 SC |
115 | else if ( info->contentType == kControlContentPictHandle ) |
116 | { | |
45cf8535 | 117 | // owned by the bitmap, no release here |
4e9ed364 | 118 | } |
df21986c | 119 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 |
20b69855 SC |
120 | else if ( info->contentType == kControlContentCGImageRef ) |
121 | { | |
122 | CGImageRelease( info->u.imageRef ) ; | |
123 | } | |
124 | #endif | |
125 | else | |
4e9ed364 | 126 | { |
20b69855 | 127 | wxFAIL_MSG(wxT("Unexpected bitmap type") ) ; |
4e9ed364 | 128 | } |
03e11df5 | 129 | } |
519cb848 | 130 | |
d45318b8 RN |
131 | #endif //wxUSE_BMPBUTTON |
132 | ||
9cc62fc8 | 133 | #define M_BITMAPDATA ((wxBitmapRefData *)m_refData) |
20b69855 SC |
134 | |
135 | void wxBitmapRefData::Init() | |
519cb848 | 136 | { |
20b69855 SC |
137 | m_width = 0 ; |
138 | m_height = 0 ; | |
139 | m_depth = 0 ; | |
140 | m_ok = false ; | |
141 | m_bitmapMask = NULL ; | |
142 | #if wxMAC_USE_CORE_GRAPHICS | |
143 | m_cgImageRef = NULL ; | |
71cc158e | 144 | #endif |
b28aeea5 SC |
145 | m_iconRef = NULL ; |
146 | m_pictHandle = NULL ; | |
20b69855 SC |
147 | m_hBitmap = NULL ; |
148 | m_hMaskBitmap = NULL; | |
149 | m_maskBytesPerRow = NULL ; | |
71cc158e | 150 | |
20b69855 SC |
151 | m_rawAccessCount = 0 ; |
152 | m_hasAlpha = false; | |
519cb848 SC |
153 | } |
154 | ||
20b69855 | 155 | wxBitmapRefData::wxBitmapRefData() |
5fde6fcc | 156 | { |
20b69855 | 157 | Init() ; |
72055702 SC |
158 | } |
159 | ||
20b69855 | 160 | wxBitmapRefData::wxBitmapRefData( int w , int h , int d ) |
72055702 | 161 | { |
20b69855 SC |
162 | Init() ; |
163 | Create( w , h , d ) ; | |
164 | } | |
55e18dbe | 165 | |
20b69855 SC |
166 | bool wxBitmapRefData::Create( int w , int h , int d ) |
167 | { | |
168 | m_width = w ; | |
169 | m_height = h ; | |
170 | m_depth = d ; | |
72055702 | 171 | |
20b69855 SC |
172 | m_bytesPerRow = w * 4 ; |
173 | size_t size = m_bytesPerRow * h ; | |
174 | void* data = m_memBuf.GetWriteBuf(size) ; | |
175 | memset( data , 0 , size) ; | |
176 | m_memBuf.UngetWriteBuf(size) ; | |
71cc158e | 177 | |
20b69855 SC |
178 | m_hBitmap = NULL ; |
179 | Rect rect = { 0 , 0 , m_height , m_width } ; | |
180 | verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 , | |
181 | (char*) data , m_bytesPerRow ) ) ; | |
182 | wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ; | |
183 | m_ok = ( m_hBitmap != NULL ) ; | |
71cc158e | 184 | |
20b69855 SC |
185 | return m_ok ; |
186 | } | |
72055702 | 187 | |
20b69855 SC |
188 | void wxBitmapRefData::UseAlpha( bool use ) |
189 | { | |
190 | if ( m_hasAlpha == use ) | |
191 | return ; | |
192 | ||
193 | m_hasAlpha = use ; | |
20b69855 | 194 | if ( m_hasAlpha ) |
72055702 | 195 | { |
20b69855 SC |
196 | int width = GetWidth() ; |
197 | int height = GetHeight() ; | |
431c82e0 | 198 | m_maskBytesPerRow = ( width * 4 + 3 ) & 0xFFFFFFC ; |
20b69855 SC |
199 | size_t size = height * m_maskBytesPerRow ; |
200 | unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ; | |
201 | memset( data , 0 , size ) ; | |
202 | wxASSERT( m_hMaskBitmap == NULL ) ; | |
203 | Rect rect = { 0 , 0 , height , width } ; | |
431c82e0 | 204 | verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 , |
20b69855 SC |
205 | (char*) data , m_maskBytesPerRow ) ) ; |
206 | wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ; | |
207 | m_maskMemBuf.UngetWriteBuf(size) ; | |
71cc158e | 208 | #if !wxMAC_USE_CORE_GRAPHICS |
20b69855 | 209 | UpdateAlphaMask() ; |
71cc158e | 210 | #endif |
72055702 SC |
211 | } |
212 | else | |
213 | { | |
20b69855 SC |
214 | DisposeGWorld( m_hMaskBitmap ) ; |
215 | m_hMaskBitmap = NULL ; | |
216 | m_maskBytesPerRow = 0 ; | |
72055702 | 217 | } |
72055702 SC |
218 | } |
219 | ||
20b69855 | 220 | void *wxBitmapRefData::GetRawAccess() const |
72055702 | 221 | { |
20b69855 SC |
222 | wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; |
223 | return m_memBuf.GetData() ; | |
224 | } | |
72055702 | 225 | |
20b69855 SC |
226 | void *wxBitmapRefData::BeginRawAccess() |
227 | { | |
228 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ; | |
229 | wxASSERT( m_rawAccessCount == 0 ) ; | |
230 | ++m_rawAccessCount ; | |
b28aeea5 | 231 | // we must destroy an existing cached image, as |
20b69855 | 232 | // the bitmap data may change now |
b28aeea5 SC |
233 | wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL , |
234 | wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; | |
235 | #if wxMAC_USE_CORE_GRAPHICS | |
20b69855 SC |
236 | if ( m_cgImageRef ) |
237 | { | |
238 | CGImageRelease( m_cgImageRef ) ; | |
239 | m_cgImageRef = NULL ; | |
240 | } | |
241 | #endif | |
242 | return m_memBuf.GetData() ; | |
243 | } | |
55e18dbe | 244 | |
20b69855 SC |
245 | void wxBitmapRefData::EndRawAccess() |
246 | { | |
247 | wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ; | |
248 | wxASSERT( m_rawAccessCount == 1 ) ; | |
249 | --m_rawAccessCount ; | |
250 | #if !wxMAC_USE_CORE_GRAPHICS | |
251 | UpdateAlphaMask() ; | |
252 | #endif | |
253 | } | |
55e18dbe | 254 | |
b28aeea5 SC |
255 | bool wxBitmapRefData::HasNativeSize() |
256 | { | |
257 | int w = GetWidth() ; | |
258 | int h = GetHeight() ; | |
259 | int sz = wxMax( w , h ) ; | |
260 | ||
261 | if ( sz == 128 || sz == 48 || sz == 32 || sz == 16 ) | |
262 | return true ; | |
263 | return false ; | |
264 | } | |
265 | ||
266 | IconRef wxBitmapRefData::GetIconRef() | |
267 | { | |
268 | if ( m_iconRef == NULL ) | |
269 | { | |
270 | // Create Icon Family Handle | |
271 | ||
272 | IconFamilyHandle iconFamily = NULL ; | |
273 | ||
274 | iconFamily = (IconFamilyHandle) NewHandle(8) ; | |
275 | (**iconFamily).resourceType = kIconFamilyType ; | |
276 | (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size); | |
277 | ||
278 | int w = GetWidth() ; | |
279 | int h = GetHeight() ; | |
280 | int sz = wxMax( w , h ) ; | |
281 | ||
282 | OSType dataType = 0 ; | |
283 | OSType maskType = 0 ; | |
284 | ||
285 | if ( sz == 128 ) | |
286 | { | |
287 | dataType = kThumbnail32BitData ; | |
288 | maskType = kThumbnail8BitMask ; | |
289 | } | |
290 | else if ( sz == 48 ) | |
291 | { | |
292 | dataType = kHuge32BitData ; | |
293 | maskType = kHuge8BitMask ; | |
294 | } | |
295 | else if ( sz == 32 ) | |
296 | { | |
297 | dataType = kLarge32BitData ; | |
298 | maskType = kLarge8BitMask ; | |
299 | } | |
300 | else if ( sz == 16 ) | |
301 | { | |
302 | dataType = kSmall32BitData ; | |
303 | maskType = kSmall8BitMask ; | |
304 | } | |
305 | ||
306 | if ( dataType != 0 ) | |
307 | { | |
308 | // setup the header properly | |
309 | ||
310 | Handle data = NULL ; | |
311 | Handle maskdata = NULL ; | |
312 | unsigned char * maskptr = NULL ; | |
313 | unsigned char * ptr = NULL ; | |
314 | size_t size ; | |
315 | size_t masksize ; | |
316 | ||
317 | size = sz * sz * 4 ; | |
318 | data = NewHandle( size) ; | |
319 | HLock( data ) ; | |
320 | ptr = (unsigned char*) *data ; | |
321 | memset( ptr , 0, size ) ; | |
322 | ||
323 | masksize = sz * sz ; | |
324 | maskdata = NewHandle( masksize ) ; | |
325 | HLock( maskdata ) ; | |
326 | maskptr = (unsigned char*) *maskdata ; | |
327 | memset( maskptr , 0 , masksize ) ; | |
328 | ||
329 | bool hasAlpha = HasAlpha() ; | |
330 | wxMask *mask = m_bitmapMask ; | |
331 | unsigned char * source = (unsigned char*) GetRawAccess() ; | |
332 | unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; | |
333 | for ( int y = 0 ; y < h ; ++y ) | |
334 | { | |
335 | unsigned char * dest = ptr + y * sz * 4 ; | |
336 | unsigned char * maskdest = maskptr + y * sz ; | |
337 | for ( int x = 0 ; x < w ; ++x ) | |
338 | { | |
339 | unsigned char a = *source ++ ; | |
340 | unsigned char r = *source ++ ; | |
341 | unsigned char g = *source ++ ; | |
342 | unsigned char b = *source ++ ; | |
343 | ||
344 | *dest++ = 0 ; | |
345 | *dest++ = r ; | |
346 | *dest++ = g ; | |
347 | *dest++ = b ; | |
348 | ||
349 | if ( mask ) | |
350 | *maskdest++ = *masksource++ ; | |
351 | else if ( hasAlpha ) | |
352 | *maskdest++ = a ; | |
353 | else | |
354 | *maskdest++ = 0xFF ; | |
355 | } | |
356 | } | |
357 | ||
358 | OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ; | |
359 | wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ; | |
360 | ||
361 | err = SetIconFamilyData( iconFamily, maskType , maskdata ) ; | |
362 | wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ; | |
363 | HUnlock( data ) ; | |
364 | HUnlock( maskdata ) ; | |
365 | DisposeHandle( data ) ; | |
366 | DisposeHandle( maskdata ) ; | |
367 | } | |
368 | else | |
369 | { | |
b28aeea5 SC |
370 | PicHandle pic = GetPictHandle() ; |
371 | SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ; | |
372 | } | |
373 | ||
374 | // transform into IconRef | |
375 | ||
376 | static int iconCounter = 2 ; | |
d45318b8 RN |
377 | #ifdef __WXDEBUG__ |
378 | OSStatus err = | |
379 | #endif | |
380 | RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &m_iconRef ) ; | |
b28aeea5 SC |
381 | wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ; |
382 | // we have to retain a reference, as Unregister will decrement it | |
383 | AcquireIconRef( m_iconRef ) ; | |
384 | UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ; | |
385 | DisposeHandle( (Handle) iconFamily ) ; | |
386 | ++iconCounter ; | |
387 | } | |
388 | return m_iconRef ; | |
389 | } | |
390 | ||
391 | PicHandle wxBitmapRefData::GetPictHandle() | |
392 | { | |
393 | if ( m_pictHandle == NULL ) | |
394 | { | |
395 | CGrafPtr origPort = NULL ; | |
396 | GDHandle origDev = NULL ; | |
b28aeea5 SC |
397 | GWorldPtr wp = NULL ; |
398 | GWorldPtr mask = NULL ; | |
399 | int height = GetHeight() ; | |
400 | int width = GetWidth() ; | |
401 | ||
402 | Rect rect = { 0 , 0 , height , width } ; | |
403 | ||
404 | GetGWorld( &origPort , &origDev ) ; | |
405 | ||
406 | wp = GetHBITMAP( &mask ) ; | |
407 | ||
408 | RgnHandle clipRgn = NULL ; | |
409 | ||
410 | if ( mask ) | |
411 | { | |
412 | GWorldPtr monoworld ; | |
413 | clipRgn = NewRgn() ; | |
414 | OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ; | |
415 | verify_noerr(err) ; | |
416 | LockPixels( GetGWorldPixMap( monoworld ) ) ; | |
417 | LockPixels( GetGWorldPixMap( mask ) ) ; | |
418 | SetGWorld( monoworld , NULL ) ; | |
419 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; | |
420 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; | |
421 | RGBForeColor( &black ) ; | |
422 | RGBBackColor( &white ) ; | |
423 | CopyBits(GetPortBitMapForCopyBits(mask), | |
424 | GetPortBitMapForCopyBits(monoworld), | |
425 | &rect, | |
426 | &rect, | |
427 | srcCopy,NULL); | |
428 | BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ; | |
429 | UnlockPixels( GetGWorldPixMap( monoworld ) ) ; | |
430 | UnlockPixels( GetGWorldPixMap( mask ) ) ; | |
431 | DisposeGWorld( monoworld ) ; | |
432 | } | |
433 | ||
434 | SetGWorld( wp , NULL ) ; | |
435 | Rect portRect ; | |
436 | GetPortBounds( wp , &portRect ) ; | |
437 | m_pictHandle = OpenPicture(&portRect); | |
438 | ||
439 | if(m_pictHandle) | |
440 | { | |
441 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; | |
442 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; | |
443 | RGBForeColor( &black ) ; | |
444 | RGBBackColor( &white ) ; | |
445 | ||
446 | if ( clipRgn ) | |
447 | SetClip( clipRgn ) ; | |
448 | ||
449 | LockPixels( GetGWorldPixMap( wp ) ) ; | |
450 | CopyBits(GetPortBitMapForCopyBits(wp), | |
451 | GetPortBitMapForCopyBits(wp), | |
452 | &portRect, | |
453 | &portRect, | |
454 | srcCopy,clipRgn); | |
455 | UnlockPixels( GetGWorldPixMap( wp ) ) ; | |
456 | ClosePicture(); | |
457 | } | |
458 | SetGWorld( origPort , origDev ) ; | |
459 | if ( clipRgn ) | |
460 | DisposeRgn( clipRgn ) ; | |
461 | } | |
462 | return m_pictHandle ; | |
463 | } | |
55e18dbe | 464 | |
30e77b5c | 465 | #ifdef __WXMAC_OSX__ |
71cc158e | 466 | void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size) |
20b69855 | 467 | { |
71cc158e SC |
468 | wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ; |
469 | wxASSERT( data == membuf->GetData() ) ; | |
470 | delete membuf ; | |
5fde6fcc GD |
471 | } |
472 | ||
20b69855 | 473 | CGImageRef wxBitmapRefData::CGImageCreate() const |
be295828 | 474 | { |
20b69855 SC |
475 | wxASSERT( m_ok ) ; |
476 | wxASSERT( m_rawAccessCount >= 0 ) ; | |
477 | CGImageRef image ; | |
478 | if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) | |
be295828 | 479 | { |
20b69855 SC |
480 | size_t imageSize = m_width * m_height * 4 ; |
481 | void * dataBuffer = m_memBuf.GetData() ; | |
482 | int w = m_width ; | |
483 | int h = m_height ; | |
484 | CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; | |
485 | wxMemoryBuffer* membuf = NULL ; | |
486 | ||
487 | if ( m_bitmapMask ) | |
be295828 | 488 | { |
20b69855 SC |
489 | membuf = new wxMemoryBuffer( imageSize ) ; |
490 | memcpy( membuf->GetData() , dataBuffer , imageSize ) ; | |
491 | unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; | |
492 | int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; | |
493 | unsigned char *destalpha = (unsigned char *) membuf->GetData() ; | |
494 | alphaInfo = kCGImageAlphaFirst ; | |
495 | for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes) | |
be295828 | 496 | { |
20b69855 SC |
497 | unsigned char *sourcemask = sourcemaskstart ; |
498 | for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 ) | |
be295828 | 499 | { |
20b69855 | 500 | *destalpha = *sourcemask ; |
be295828 SC |
501 | } |
502 | } | |
503 | } | |
20b69855 | 504 | else if ( m_hasAlpha ) |
e40298d5 | 505 | { |
20b69855 SC |
506 | #if wxMAC_USE_PREMULTIPLIED_ALPHA |
507 | alphaInfo = kCGImageAlphaPremultipliedFirst ; | |
508 | #else | |
509 | alphaInfo = kCGImageAlphaFirst ; | |
510 | #endif | |
511 | membuf = new wxMemoryBuffer( m_memBuf ) ; | |
e40298d5 | 512 | } |
20b69855 SC |
513 | else |
514 | { | |
515 | membuf = new wxMemoryBuffer( m_memBuf ) ; | |
516 | } | |
517 | CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); | |
518 | CGDataProviderRef dataProvider = | |
71cc158e SC |
519 | CGDataProviderCreateWithData( membuf , (const void *)membuf->GetData() , imageSize, |
520 | wxMacMemoryBufferReleaseProc ); | |
20b69855 SC |
521 | image = |
522 | ::CGImageCreate( w, h, 8 , 32 , 4 * m_width , colorSpace, alphaInfo , | |
523 | dataProvider, NULL , false , kCGRenderingIntentDefault ); | |
524 | CGDataProviderRelease( dataProvider); | |
525 | } | |
526 | else | |
527 | { | |
528 | image = m_cgImageRef ; | |
529 | CGImageRetain( image ) ; | |
be295828 | 530 | } |
20b69855 SC |
531 | if ( m_rawAccessCount == 0 && m_cgImageRef == NULL) |
532 | { | |
533 | // we keep it for later use | |
534 | m_cgImageRef = image ; | |
535 | CGImageRetain( image ) ; | |
536 | } | |
537 | return image ; | |
be295828 | 538 | } |
20b69855 | 539 | #endif |
be295828 | 540 | |
20b69855 SC |
541 | GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const |
542 | { | |
543 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
544 | if ( mask ) | |
545 | { | |
546 | *mask = NULL ; | |
547 | if ( m_bitmapMask ) | |
548 | *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ; | |
549 | else if ( m_hasAlpha ) | |
550 | { | |
71cc158e | 551 | #if !wxMAC_USE_CORE_GRAPHICS |
20b69855 SC |
552 | if ( m_rawAccessCount > 0 ) |
553 | UpdateAlphaMask() ; | |
71cc158e SC |
554 | #else |
555 | // this structure is not kept in synch when using CG, so if someone | |
556 | // is really accessing the Graphports, we have to sync it | |
557 | UpdateAlphaMask() ; | |
558 | #endif | |
20b69855 SC |
559 | *mask = m_hMaskBitmap ; |
560 | } | |
561 | } | |
562 | return m_hBitmap ; | |
e9576ca5 SC |
563 | } |
564 | ||
20b69855 | 565 | void wxBitmapRefData::UpdateAlphaMask() const |
e9576ca5 | 566 | { |
20b69855 | 567 | if ( m_hasAlpha ) |
e40298d5 | 568 | { |
20b69855 SC |
569 | unsigned char *sourcemask = (unsigned char *) GetRawAccess() ; |
570 | unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ; | |
571 | ||
572 | int h = GetHeight() ; | |
573 | int w = GetWidth() ; | |
574 | ||
575 | for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow ) | |
576 | { | |
577 | unsigned char* destalpha = destalphabase ; | |
431c82e0 | 578 | for( int x = 0 ; x < w ; ++x , sourcemask += 4 ) |
e40298d5 | 579 | { |
431c82e0 SC |
580 | // we must have 24 bit depth for non quartz smooth alpha |
581 | *destalpha++ = 255 ; | |
582 | *destalpha++ = 255 - *sourcemask ; | |
583 | *destalpha++ = 255 - *sourcemask ; | |
584 | *destalpha++ = 255 - *sourcemask ; | |
e40298d5 | 585 | } |
20b69855 SC |
586 | } |
587 | } | |
588 | } | |
589 | ||
20b69855 SC |
590 | void wxBitmapRefData::Free() |
591 | { | |
592 | wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; | |
593 | ||
594 | #if wxMAC_USE_CORE_GRAPHICS | |
595 | if ( m_cgImageRef ) | |
596 | { | |
597 | CGImageRelease( m_cgImageRef ) ; | |
598 | m_cgImageRef = NULL ; | |
e40298d5 | 599 | } |
71cc158e | 600 | #endif |
b28aeea5 SC |
601 | if ( m_iconRef ) |
602 | { | |
603 | ReleaseIconRef( m_iconRef ) ; | |
604 | m_iconRef = NULL ; | |
605 | } | |
606 | if ( m_pictHandle ) | |
607 | { | |
608 | KillPicture( m_pictHandle ) ; | |
609 | m_pictHandle = NULL ; | |
610 | } | |
20b69855 SC |
611 | if ( m_hBitmap ) |
612 | { | |
613 | DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ; | |
614 | m_hBitmap = NULL ; | |
615 | } | |
616 | if ( m_hMaskBitmap ) | |
617 | { | |
618 | DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ; | |
619 | m_hMaskBitmap = NULL ; | |
620 | } | |
55e18dbe | 621 | |
20b69855 | 622 | if (m_bitmapMask) |
e40298d5 | 623 | { |
20b69855 SC |
624 | delete m_bitmapMask; |
625 | m_bitmapMask = NULL; | |
e40298d5 | 626 | } |
e9576ca5 SC |
627 | } |
628 | ||
85f296a3 SC |
629 | wxBitmapRefData::~wxBitmapRefData() |
630 | { | |
20b69855 | 631 | Free() ; |
85f296a3 SC |
632 | } |
633 | ||
90b959ae SC |
634 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
635 | { | |
20b69855 SC |
636 | int w = icon.GetWidth() ; |
637 | int h = icon.GetHeight() ; | |
b28aeea5 SC |
638 | |
639 | ||
20b69855 SC |
640 | Create( icon.GetWidth() , icon.GetHeight() ) ; |
641 | ||
b28aeea5 SC |
642 | bool created = false ; |
643 | ||
71cc158e | 644 | if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) ) |
20b69855 SC |
645 | { |
646 | IconFamilyHandle iconFamily = NULL ; | |
647 | Handle imagehandle = NewHandle(0) ; | |
648 | Handle maskhandle = NewHandle(0) ; | |
71cc158e | 649 | |
9cc62fc8 SC |
650 | OSType maskType = 0; |
651 | OSType dataType = 0; | |
652 | IconSelectorValue selector = 0 ; | |
71cc158e SC |
653 | if ( w == 128 ) |
654 | { | |
655 | dataType = kThumbnail32BitData ; | |
656 | maskType = kThumbnail8BitMask ; | |
657 | selector = kSelectorAllAvailableData ; | |
658 | } | |
659 | else if ( w == 48 ) | |
660 | { | |
661 | dataType = kHuge32BitData ; | |
662 | maskType = kHuge8BitMask ; | |
663 | selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ; | |
664 | } | |
665 | else if ( w == 32 ) | |
666 | { | |
667 | dataType = kLarge32BitData ; | |
668 | maskType = kLarge8BitMask ; | |
669 | selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ; | |
670 | } | |
671 | else if ( w == 16 ) | |
672 | { | |
673 | dataType = kSmall32BitData ; | |
674 | maskType = kSmall8BitMask ; | |
675 | selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ; | |
676 | } | |
71cc158e SC |
677 | |
678 | ||
679 | OSStatus err = ( IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ) ; | |
680 | ||
681 | err =( GetIconFamilyData( iconFamily , dataType , imagehandle ) ) ; | |
682 | err =( GetIconFamilyData( iconFamily , maskType , maskhandle ) ) ; | |
b28aeea5 SC |
683 | size_t imagehandlesize = GetHandleSize( imagehandle ) ; |
684 | size_t maskhandlesize = GetHandleSize( maskhandle ) ; | |
685 | ||
686 | if ( imagehandlesize != 0 && maskhandlesize != 0 ) | |
20b69855 | 687 | { |
b28aeea5 SC |
688 | wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ; |
689 | wxASSERT( GetHandleSize( maskhandle ) == w * h ) ; | |
690 | UseAlpha() ; | |
691 | unsigned char *source = (unsigned char *) *imagehandle ; | |
692 | unsigned char *sourcemask = (unsigned char *) *maskhandle ; | |
693 | ||
694 | unsigned char* destination = (unsigned char*) BeginRawAccess() ; | |
695 | for ( int y = 0 ; y < h ; ++y ) | |
20b69855 | 696 | { |
b28aeea5 SC |
697 | for ( int x = 0 ; x < w ; ++x ) |
698 | { | |
699 | *destination++ = *sourcemask++ ; | |
700 | source++ ; | |
701 | *destination++ = *source++ ; | |
702 | *destination++ = *source++ ; | |
703 | *destination++ = *source++ ; | |
704 | } | |
20b69855 | 705 | } |
b28aeea5 SC |
706 | EndRawAccess() ; |
707 | DisposeHandle( imagehandle ) ; | |
708 | DisposeHandle( maskhandle ) ; | |
95d8425f | 709 | created = true ; |
20b69855 | 710 | } |
b28aeea5 SC |
711 | DisposeHandle( (Handle) iconFamily ) ; |
712 | ||
20b69855 | 713 | } |
b28aeea5 SC |
714 | |
715 | if ( !created ) | |
20b69855 SC |
716 | { |
717 | wxMemoryDC dc ; | |
718 | dc.SelectObject( *this ) ; | |
719 | dc.DrawIcon( icon , 0 , 0 ) ; | |
720 | dc.SelectObject( wxNullBitmap ) ; | |
721 | } | |
125c389e | 722 | return true; |
90b959ae SC |
723 | } |
724 | ||
e9576ca5 SC |
725 | wxBitmap::wxBitmap() |
726 | { | |
e9576ca5 SC |
727 | } |
728 | ||
729 | wxBitmap::~wxBitmap() | |
730 | { | |
e9576ca5 SC |
731 | } |
732 | ||
733 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) | |
734 | { | |
20b69855 | 735 | m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ; |
e9576ca5 | 736 | |
d2c6d549 GD |
737 | if ( no_bits == 1 ) |
738 | { | |
973b0afb GD |
739 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; |
740 | if ( the_width % (sizeof(unsigned char) * 8) ) { | |
741 | linesize += sizeof(unsigned char); | |
742 | } | |
20b69855 SC |
743 | unsigned char* linestart = (unsigned char*) bits ; |
744 | unsigned char* destination = (unsigned char*) BeginRawAccess() ; | |
973b0afb GD |
745 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize ) |
746 | { | |
747 | for ( int x = 0 ; x < the_width ; ++x ) | |
748 | { | |
749 | int index = x / 8 ; | |
750 | int bit = x % 8 ; | |
751 | int mask = 1 << bit ; | |
752 | if ( linestart[index] & mask ) | |
753 | { | |
20b69855 SC |
754 | *destination++ = 0xFF ; |
755 | *destination++ = 0 ; | |
756 | *destination++ = 0 ; | |
757 | *destination++ = 0 ; | |
973b0afb GD |
758 | } |
759 | else | |
760 | { | |
20b69855 SC |
761 | *destination++ = 0xFF ; |
762 | *destination++ = 0xFF ; | |
763 | *destination++ = 0xFF ; | |
764 | *destination++ = 0xFF ; | |
973b0afb GD |
765 | } |
766 | } | |
767 | } | |
20b69855 | 768 | EndRawAccess() ; |
d2c6d549 GD |
769 | } |
770 | else | |
771 | { | |
973b0afb | 772 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); |
d2c6d549 | 773 | } |
e9576ca5 SC |
774 | } |
775 | ||
776 | wxBitmap::wxBitmap(int w, int h, int d) | |
777 | { | |
778 | (void)Create(w, h, d); | |
e9576ca5 SC |
779 | } |
780 | ||
a8562f55 | 781 | wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
782 | { |
783 | (void) Create(data, type, width, height, depth); | |
e9576ca5 SC |
784 | } |
785 | ||
a8562f55 | 786 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) |
e9576ca5 | 787 | { |
a8562f55 | 788 | LoadFile(filename, type); |
e9576ca5 SC |
789 | } |
790 | ||
20b69855 SC |
791 | wxBitmap::wxBitmap(const char **bits) |
792 | { | |
793 | (void) CreateFromXpm(bits); | |
794 | } | |
795 | ||
796 | wxBitmap::wxBitmap(char **bits) | |
797 | { | |
798 | (void) CreateFromXpm((const char **)bits); | |
799 | } | |
800 | ||
801 | void* wxBitmap::GetRawAccess() const | |
802 | { | |
803 | wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ; | |
804 | return M_BITMAPDATA->GetRawAccess() ; | |
805 | } | |
806 | ||
807 | void* wxBitmap::BeginRawAccess() | |
808 | { | |
809 | wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ; | |
810 | return M_BITMAPDATA->BeginRawAccess() ; | |
811 | } | |
812 | ||
813 | void wxBitmap::EndRawAccess() | |
814 | { | |
815 | wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ; | |
816 | M_BITMAPDATA->EndRawAccess() ; | |
817 | } | |
818 | ||
973b0afb | 819 | bool wxBitmap::CreateFromXpm(const char **bits) |
e9576ca5 | 820 | { |
d45318b8 | 821 | #if wxUSE_IMAGE |
973b0afb GD |
822 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
823 | wxXPMDecoder decoder; | |
824 | wxImage img = decoder.ReadData(bits); | |
55e18dbe VZ |
825 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) |
826 | *this = wxBitmap(img); | |
973b0afb | 827 | return TRUE; |
d45318b8 RN |
828 | #else |
829 | return FALSE; | |
830 | #endif | |
973b0afb GD |
831 | } |
832 | ||
30e77b5c | 833 | #ifdef __WXMAC_OSX__ |
20b69855 | 834 | WXCGIMAGEREF wxBitmap::CGImageCreate() const |
03e11df5 | 835 | { |
20b69855 SC |
836 | wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; |
837 | return M_BITMAPDATA->CGImageCreate() ; | |
03e11df5 | 838 | } |
20b69855 | 839 | #endif |
03e11df5 | 840 | |
5fde6fcc GD |
841 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const |
842 | { | |
20b69855 | 843 | wxCHECK_MSG( Ok() && |
5fde6fcc GD |
844 | (rect.x >= 0) && (rect.y >= 0) && |
845 | (rect.x+rect.width <= GetWidth()) && | |
846 | (rect.y+rect.height <= GetHeight()), | |
847 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
848 | ||
55e18dbe | 849 | |
20b69855 SC |
850 | wxBitmap ret( rect.width, rect.height, GetDepth() ); |
851 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
5fde6fcc | 852 | |
5fde6fcc | 853 | |
20b69855 SC |
854 | int sourcewidth = GetWidth() ; |
855 | int destwidth = rect.width ; | |
856 | int destheight = rect.height ; | |
857 | { | |
858 | unsigned char * sourcedata = (unsigned char*) GetRawAccess() ; | |
859 | unsigned char * destdata = (unsigned char*) ret.BeginRawAccess() ; | |
860 | int sourcelinesize = sourcewidth * 4 ; | |
861 | int destlinesize = destwidth * 4 ; | |
862 | unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; | |
863 | unsigned char *dest = destdata ; | |
864 | for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) | |
865 | { | |
866 | memcpy( dest , source , destlinesize ) ; | |
867 | } | |
868 | } | |
869 | ret.EndRawAccess() ; | |
870 | ||
871 | if ( M_BITMAPDATA->m_bitmapMask ) | |
872 | { | |
873 | wxMemoryBuffer maskbuf ; | |
874 | int rowBytes = ( destwidth + 3 ) & 0xFFFFFFC ; | |
875 | size_t maskbufsize = rowBytes * destheight ; | |
876 | unsigned char * destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ; | |
877 | ||
878 | int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ; | |
879 | int destlinesize = rowBytes ; | |
880 | unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ; | |
881 | source += rect.x + rect.y * sourcelinesize ; | |
882 | unsigned char *dest = destdata ; | |
883 | ||
884 | for(int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) | |
885 | { | |
886 | memcpy( dest , source , destlinesize ) ; | |
887 | } | |
888 | maskbuf.UngetWriteBuf( maskbufsize ) ; | |
889 | ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ; | |
890 | } | |
891 | else if ( HasAlpha() ) | |
892 | ret.UseAlpha() ; | |
5fde6fcc | 893 | |
20b69855 | 894 | return ret; |
5fde6fcc GD |
895 | } |
896 | ||
e9576ca5 SC |
897 | bool wxBitmap::Create(int w, int h, int d) |
898 | { | |
899 | UnRef(); | |
900 | ||
20b69855 SC |
901 | if ( d < 0 ) |
902 | d = wxDisplayDepth() ; | |
2a391f87 | 903 | |
20b69855 | 904 | m_refData = new wxBitmapRefData( w , h , d ); |
55e18dbe | 905 | |
20b69855 | 906 | return M_BITMAPDATA->Ok() ; |
519cb848 SC |
907 | } |
908 | ||
a8562f55 | 909 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
e9576ca5 SC |
910 | { |
911 | UnRef(); | |
912 | ||
e9576ca5 SC |
913 | wxBitmapHandler *handler = FindHandler(type); |
914 | ||
a8562f55 GD |
915 | if ( handler ) |
916 | { | |
4e9ed364 | 917 | m_refData = new wxBitmapRefData; |
e9576ca5 | 918 | |
a8562f55 | 919 | return handler->LoadFile(this, filename, type, -1, -1); |
e9576ca5 | 920 | } |
a8562f55 GD |
921 | else |
922 | { | |
d45318b8 | 923 | #if wxUSE_IMAGE |
a8562f55 GD |
924 | wxImage loadimage(filename, type); |
925 | if (loadimage.Ok()) { | |
926 | *this = loadimage; | |
927 | return true; | |
928 | } | |
d45318b8 | 929 | #endif |
a8562f55 | 930 | } |
427ff662 | 931 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); |
a8562f55 | 932 | return false; |
e9576ca5 SC |
933 | } |
934 | ||
a8562f55 | 935 | bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
936 | { |
937 | UnRef(); | |
938 | ||
939 | m_refData = new wxBitmapRefData; | |
940 | ||
941 | wxBitmapHandler *handler = FindHandler(type); | |
942 | ||
943 | if ( handler == NULL ) { | |
427ff662 | 944 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); |
e9576ca5 SC |
945 | |
946 | return FALSE; | |
947 | } | |
948 | ||
949 | return handler->Create(this, data, type, width, height, depth); | |
950 | } | |
951 | ||
d45318b8 RN |
952 | #if wxUSE_IMAGE |
953 | ||
fec19ea9 VS |
954 | wxBitmap::wxBitmap(const wxImage& image, int depth) |
955 | { | |
956 | wxCHECK_RET( image.Ok(), wxT("invalid image") ) | |
fec19ea9 | 957 | |
fec19ea9 VS |
958 | // width and height of the device-dependent bitmap |
959 | int width = image.GetWidth(); | |
960 | int height = image.GetHeight(); | |
961 | ||
20b69855 | 962 | m_refData = new wxBitmapRefData( width , height , depth ) ;; |
55e18dbe | 963 | |
20b69855 | 964 | // Create picture |
fec19ea9 | 965 | |
20b69855 SC |
966 | bool hasAlpha = false ; |
967 | ||
968 | if ( image.HasMask() ) | |
969 | { | |
970 | // takes precedence, don't mix with alpha info | |
971 | } | |
972 | else | |
973 | { | |
974 | hasAlpha = image.HasAlpha() ; | |
975 | } | |
976 | ||
977 | if ( hasAlpha ) | |
978 | UseAlpha() ; | |
979 | ||
980 | unsigned char* destination = (unsigned char*) BeginRawAccess() ; | |
55e18dbe | 981 | |
fec19ea9 | 982 | register unsigned char* data = image.GetData(); |
20b69855 | 983 | const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; |
fec19ea9 VS |
984 | for (int y = 0; y < height; y++) |
985 | { | |
986 | for (int x = 0; x < width; x++) | |
987 | { | |
20b69855 SC |
988 | if ( hasAlpha ) |
989 | { | |
990 | const unsigned char a = *alpha++; | |
991 | *destination++ = a ; | |
992 | #if wxMAC_USE_PREMULTIPLIED_ALPHA | |
993 | *destination++ = ((*data++) * a + 127 ) / 255 ; | |
994 | *destination++ = ((*data++) * a + 127 ) / 255 ; | |
995 | *destination++ = ((*data++) * a + 127 ) / 255 ; | |
996 | #else | |
997 | *destination++ = *data++ ; | |
998 | *destination++ = *data++ ; | |
999 | *destination++ = *data++ ; | |
1000 | #endif | |
1001 | } | |
1002 | else | |
1003 | { | |
1004 | *destination++ = 0xFF ; | |
1005 | *destination++ = *data++ ; | |
1006 | *destination++ = *data++ ; | |
1007 | *destination++ = *data++ ; | |
1008 | } | |
fec19ea9 | 1009 | } |
55e18dbe | 1010 | } |
20b69855 SC |
1011 | EndRawAccess() ; |
1012 | if ( image.HasMask() ) | |
8dd336cb | 1013 | { |
20b69855 | 1014 | SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; |
8dd336cb | 1015 | } |
fec19ea9 VS |
1016 | } |
1017 | ||
1018 | wxImage wxBitmap::ConvertToImage() const | |
1019 | { | |
1020 | wxImage image; | |
55e18dbe | 1021 | |
fec19ea9 VS |
1022 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
1023 | ||
1024 | // create an wxImage object | |
1025 | int width = GetWidth(); | |
1026 | int height = GetHeight(); | |
1027 | image.Create( width, height ); | |
1028 | ||
1029 | unsigned char *data = image.GetData(); | |
fec19ea9 VS |
1030 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); |
1031 | ||
20b69855 SC |
1032 | unsigned char* source = (unsigned char*) GetRawAccess() ; |
1033 | ||
1034 | bool hasAlpha = false ; | |
1035 | bool hasMask = false ; | |
7e7f40ed | 1036 | int maskBytesPerRow = 0 ; |
20b69855 SC |
1037 | unsigned char *alpha = NULL ; |
1038 | unsigned char *mask = NULL ; | |
1039 | if ( HasAlpha() ) | |
2b5f62a0 | 1040 | { |
20b69855 | 1041 | hasAlpha = true ; |
2b5f62a0 | 1042 | } |
20b69855 SC |
1043 | |
1044 | if ( GetMask() ) | |
2b5f62a0 | 1045 | { |
20b69855 SC |
1046 | hasMask = true ; |
1047 | mask = (unsigned char*) GetMask()->GetRawAccess() ; | |
7e7f40ed | 1048 | maskBytesPerRow = GetMask()->GetBytesPerRow() ; |
e40298d5 | 1049 | } |
20b69855 SC |
1050 | |
1051 | if ( hasAlpha ) | |
e40298d5 | 1052 | { |
20b69855 SC |
1053 | image.SetAlpha() ; |
1054 | alpha = image.GetAlpha() ; | |
e40298d5 | 1055 | } |
20b69855 SC |
1056 | int index = 0; |
1057 | ||
1058 | // The following masking algorithm is the same as well in msw/gtk: | |
1059 | // the colour used as transparent one in wxImage and the one it is | |
1060 | // replaced with when it really occurs in the bitmap | |
1061 | static const int MASK_RED = 1; | |
1062 | static const int MASK_GREEN = 2; | |
1063 | static const int MASK_BLUE = 3; | |
1064 | static const int MASK_BLUE_REPLACEMENT = 2; | |
1065 | ||
7e7f40ed | 1066 | for (int yy = 0; yy < height; yy++ , mask += maskBytesPerRow ) |
fec19ea9 | 1067 | { |
7e7f40ed | 1068 | unsigned char * maskp = mask ; |
fec19ea9 VS |
1069 | for (int xx = 0; xx < width; xx++) |
1070 | { | |
20b69855 SC |
1071 | long color = *((long*) source) ; |
1072 | unsigned char a = ((color&0xFF000000) >> 24) ; | |
1073 | unsigned char r = ((color&0x00FF0000) >> 16) ; | |
1074 | unsigned char g = ((color&0x0000FF00) >> 8) ; | |
1075 | unsigned char b = (color&0x000000FF); | |
1076 | if ( hasMask ) | |
55e18dbe | 1077 | { |
7e7f40ed | 1078 | if ( *maskp++ == 0 ) |
e40298d5 | 1079 | { |
e59ea2c5 SC |
1080 | r = MASK_RED ; |
1081 | g = MASK_GREEN ; | |
1082 | b = MASK_BLUE ; | |
e40298d5 | 1083 | } |
e59ea2c5 SC |
1084 | else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE ) |
1085 | b = MASK_BLUE_REPLACEMENT ; | |
fec19ea9 | 1086 | } |
20b69855 SC |
1087 | else if ( hasAlpha ) |
1088 | *alpha++ = a ; | |
1089 | ||
1090 | data[index ] = r ; | |
1091 | data[index + 1] = g ; | |
1092 | data[index + 2] = b ; | |
fec19ea9 | 1093 | index += 3; |
20b69855 | 1094 | source += 4 ; |
fec19ea9 VS |
1095 | } |
1096 | } | |
20b69855 SC |
1097 | if ( hasMask ) |
1098 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); | |
fec19ea9 VS |
1099 | return image; |
1100 | } | |
1101 | ||
d45318b8 | 1102 | #endif //wxUSE_IMAGE |
fec19ea9 | 1103 | |
a8562f55 GD |
1104 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, |
1105 | const wxPalette *palette) const | |
e9576ca5 SC |
1106 | { |
1107 | wxBitmapHandler *handler = FindHandler(type); | |
1108 | ||
a8562f55 GD |
1109 | if ( handler ) |
1110 | { | |
1111 | return handler->SaveFile(this, filename, type, palette); | |
1112 | } | |
1113 | else | |
1114 | { | |
d45318b8 | 1115 | #if wxUSE_IMAGE |
a8562f55 | 1116 | wxImage image = ConvertToImage(); |
a8562f55 | 1117 | return image.SaveFile(filename, type); |
d45318b8 | 1118 | #endif |
a8562f55 | 1119 | } |
55e18dbe | 1120 | |
427ff662 | 1121 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); |
a8562f55 | 1122 | return false; |
e9576ca5 SC |
1123 | } |
1124 | ||
5fde6fcc GD |
1125 | bool wxBitmap::Ok() const |
1126 | { | |
20b69855 | 1127 | return (M_BITMAPDATA && M_BITMAPDATA->Ok()); |
5fde6fcc GD |
1128 | } |
1129 | ||
1130 | int wxBitmap::GetHeight() const | |
1131 | { | |
1132 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1133 | ||
20b69855 | 1134 | return M_BITMAPDATA->GetHeight(); |
5fde6fcc GD |
1135 | } |
1136 | ||
1137 | int wxBitmap::GetWidth() const | |
1138 | { | |
1139 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1140 | ||
20b69855 | 1141 | return M_BITMAPDATA->GetWidth() ; |
5fde6fcc GD |
1142 | } |
1143 | ||
1144 | int wxBitmap::GetDepth() const | |
1145 | { | |
1146 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1147 | ||
20b69855 | 1148 | return M_BITMAPDATA->GetDepth(); |
5fde6fcc GD |
1149 | } |
1150 | ||
179e085f RN |
1151 | #if WXWIN_COMPATIBILITY_2_4 |
1152 | ||
5fde6fcc GD |
1153 | int wxBitmap::GetQuality() const |
1154 | { | |
20b69855 | 1155 | return 0; |
5fde6fcc GD |
1156 | } |
1157 | ||
179e085f RN |
1158 | #endif |
1159 | ||
5fde6fcc GD |
1160 | wxMask *wxBitmap::GetMask() const |
1161 | { | |
1162 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
1163 | ||
1164 | return M_BITMAPDATA->m_bitmapMask; | |
1165 | } | |
1166 | ||
20b69855 SC |
1167 | bool wxBitmap::HasAlpha() const |
1168 | { | |
1169 | wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") ); | |
1170 | ||
1171 | return M_BITMAPDATA->HasAlpha() ; | |
1172 | } | |
1173 | ||
e9576ca5 SC |
1174 | void wxBitmap::SetWidth(int w) |
1175 | { | |
1176 | if (!M_BITMAPDATA) | |
1177 | m_refData = new wxBitmapRefData; | |
1178 | ||
20b69855 | 1179 | M_BITMAPDATA->SetWidth(w); |
e9576ca5 SC |
1180 | } |
1181 | ||
1182 | void wxBitmap::SetHeight(int h) | |
1183 | { | |
1184 | if (!M_BITMAPDATA) | |
1185 | m_refData = new wxBitmapRefData; | |
1186 | ||
20b69855 | 1187 | M_BITMAPDATA->SetHeight(h); |
e9576ca5 SC |
1188 | } |
1189 | ||
1190 | void wxBitmap::SetDepth(int d) | |
1191 | { | |
1192 | if (!M_BITMAPDATA) | |
1193 | m_refData = new wxBitmapRefData; | |
1194 | ||
20b69855 | 1195 | M_BITMAPDATA->SetDepth(d); |
e9576ca5 SC |
1196 | } |
1197 | ||
179e085f RN |
1198 | #if WXWIN_COMPATIBILITY_2_4 |
1199 | ||
20b69855 | 1200 | void wxBitmap::SetQuality(int WXUNUSED(quality)) |
e9576ca5 | 1201 | { |
e9576ca5 SC |
1202 | } |
1203 | ||
179e085f RN |
1204 | #endif |
1205 | ||
e9576ca5 SC |
1206 | void wxBitmap::SetOk(bool isOk) |
1207 | { | |
1208 | if (!M_BITMAPDATA) | |
1209 | m_refData = new wxBitmapRefData; | |
1210 | ||
20b69855 | 1211 | M_BITMAPDATA->SetOk(isOk); |
e9576ca5 SC |
1212 | } |
1213 | ||
a6de86fa | 1214 | #if wxUSE_PALETTE |
5fde6fcc GD |
1215 | wxPalette *wxBitmap::GetPalette() const |
1216 | { | |
1217 | wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") ); | |
1218 | ||
1219 | return &M_BITMAPDATA->m_bitmapPalette; | |
1220 | } | |
1221 | ||
e9576ca5 SC |
1222 | void wxBitmap::SetPalette(const wxPalette& palette) |
1223 | { | |
1224 | if (!M_BITMAPDATA) | |
1225 | m_refData = new wxBitmapRefData; | |
1226 | ||
1227 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
1228 | } | |
a6de86fa | 1229 | #endif // wxUSE_PALETTE |
e9576ca5 SC |
1230 | |
1231 | void wxBitmap::SetMask(wxMask *mask) | |
1232 | { | |
1233 | if (!M_BITMAPDATA) | |
1234 | m_refData = new wxBitmapRefData; | |
1235 | ||
a8562f55 | 1236 | // Remove existing mask if there is one. |
1e74d03b | 1237 | delete M_BITMAPDATA->m_bitmapMask; |
a8562f55 | 1238 | |
e9576ca5 SC |
1239 | M_BITMAPDATA->m_bitmapMask = mask ; |
1240 | } | |
1241 | ||
20b69855 | 1242 | WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const |
5fde6fcc | 1243 | { |
20b69855 | 1244 | return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask)); |
5fde6fcc GD |
1245 | } |
1246 | ||
20b69855 SC |
1247 | // ---------------------------------------------------------------------------- |
1248 | // wxMask | |
1249 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
1250 | |
1251 | wxMask::wxMask() | |
1252 | { | |
20b69855 | 1253 | Init() ; |
e9576ca5 SC |
1254 | } |
1255 | ||
1256 | // Construct a mask from a bitmap and a colour indicating | |
1257 | // the transparent area | |
1258 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
1259 | { | |
20b69855 | 1260 | Init() ; |
e9576ca5 SC |
1261 | Create(bitmap, colour); |
1262 | } | |
1263 | ||
20b69855 SC |
1264 | // Construct a mask from a mono bitmap (copies the bitmap). |
1265 | wxMask::wxMask(const wxBitmap& bitmap) | |
e9576ca5 | 1266 | { |
20b69855 SC |
1267 | Init() ; |
1268 | Create(bitmap); | |
e9576ca5 SC |
1269 | } |
1270 | ||
1271 | // Construct a mask from a mono bitmap (copies the bitmap). | |
20b69855 | 1272 | wxMask::wxMask(const wxMemoryBuffer& data, int width , int height , int bytesPerRow ) |
e9576ca5 | 1273 | { |
20b69855 SC |
1274 | Init() ; |
1275 | Create(data, width , height , bytesPerRow ); | |
e9576ca5 SC |
1276 | } |
1277 | ||
1278 | wxMask::~wxMask() | |
1279 | { | |
4e9ed364 RR |
1280 | if ( m_maskBitmap ) |
1281 | { | |
20b69855 | 1282 | DisposeGWorld( (GWorldPtr) m_maskBitmap ) ; |
4e9ed364 RR |
1283 | m_maskBitmap = NULL ; |
1284 | } | |
e9576ca5 SC |
1285 | } |
1286 | ||
20b69855 | 1287 | void wxMask::Init() |
e9576ca5 | 1288 | { |
20b69855 | 1289 | m_width = m_height = m_bytesPerRow = 0 ; |
20b69855 | 1290 | m_maskBitmap = NULL ; |
20b69855 | 1291 | } |
5fde6fcc | 1292 | |
20b69855 SC |
1293 | void *wxMask::GetRawAccess() const |
1294 | { | |
1295 | return m_memBuf.GetData() ; | |
1296 | } | |
5fde6fcc | 1297 | |
431c82e0 SC |
1298 | // this can be a k8IndexedGrayPixelFormat GWorld, because it never stores other values than black or white |
1299 | // so no rainbox colors will be created by QD when blitting | |
1300 | ||
20b69855 SC |
1301 | void wxMask::RealizeNative() |
1302 | { | |
20b69855 SC |
1303 | if ( m_maskBitmap ) |
1304 | { | |
1305 | DisposeGWorld( (GWorldPtr) m_maskBitmap ) ; | |
1306 | m_maskBitmap = NULL ; | |
1307 | } | |
1308 | Rect rect = { 0 , 0 , m_height , m_width } ; | |
1309 | verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_maskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 , | |
1310 | (char*) m_memBuf.GetData() , m_bytesPerRow ) ) ; | |
20b69855 | 1311 | } |
5fde6fcc | 1312 | |
20b69855 SC |
1313 | // Create a mask from a mono bitmap (copies the bitmap). |
1314 | bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow) | |
1315 | { | |
1316 | m_memBuf = data ; | |
1317 | m_width = width ; | |
1318 | m_height = height ; | |
1319 | m_bytesPerRow = bytesPerRow ; | |
1320 | wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ; | |
1321 | RealizeNative() ; | |
1322 | return true ; | |
e9576ca5 SC |
1323 | } |
1324 | ||
20b69855 SC |
1325 | // Create a mask from a mono bitmap (copies the bitmap). |
1326 | bool wxMask::Create(const wxBitmap& bitmap) | |
e9576ca5 | 1327 | { |
20b69855 SC |
1328 | m_width = bitmap.GetWidth() ; |
1329 | m_height = bitmap.GetHeight() ; | |
1330 | m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ; | |
1331 | size_t size = m_bytesPerRow * m_height ; | |
1332 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; | |
1333 | memset( destdatabase , 0 , size ) ; | |
1334 | unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; | |
1335 | for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) | |
1336 | { | |
1337 | unsigned char *destdata= destdatabase ; | |
1338 | for( int x = 0 ; x < m_width ; ++x ) | |
1339 | { | |
1340 | srcdata++ ; | |
1341 | unsigned char r = *srcdata++ ; | |
1342 | unsigned char g = *srcdata++ ; | |
1343 | unsigned char b = *srcdata++ ; | |
1344 | if ( ( r + g + b ) > 0x10 ) | |
1345 | *destdata++ = 0x00 ; | |
1346 | else | |
1347 | *destdata++ = 0xFF ; | |
1348 | } | |
1349 | } | |
1350 | m_memBuf.UngetWriteBuf( size ) ; | |
1351 | RealizeNative() ; | |
1352 | return TRUE; | |
e9576ca5 SC |
1353 | } |
1354 | ||
1355 | // Create a mask from a bitmap and a colour indicating | |
1356 | // the transparent area | |
1357 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1358 | { | |
20b69855 SC |
1359 | m_width = bitmap.GetWidth() ; |
1360 | m_height = bitmap.GetHeight() ; | |
1361 | m_bytesPerRow = ( m_width + 3 ) & 0xFFFFFFC ; | |
1362 | size_t size = m_bytesPerRow * m_height ; | |
1363 | ||
1364 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; | |
1365 | memset( destdatabase , 0 , size ) ; | |
1366 | unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; | |
1367 | for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow) | |
8208e181 | 1368 | { |
20b69855 SC |
1369 | unsigned char *destdata= destdatabase ; |
1370 | for( int x = 0 ; x < m_width ; ++x ) | |
55e18dbe | 1371 | { |
20b69855 SC |
1372 | srcdata++ ; |
1373 | unsigned char r = *srcdata++ ; | |
1374 | unsigned char g = *srcdata++ ; | |
1375 | unsigned char b = *srcdata++ ; | |
1376 | if ( colour == wxColour( r , g , b) ) | |
1377 | *destdata++ = 0x00 ; | |
8208e181 | 1378 | else |
20b69855 | 1379 | *destdata++ = 0xFF ; |
8208e181 SC |
1380 | } |
1381 | } | |
20b69855 SC |
1382 | m_memBuf.UngetWriteBuf( size ) ; |
1383 | RealizeNative() ; | |
8208e181 | 1384 | return TRUE; |
e9576ca5 SC |
1385 | } |
1386 | ||
20b69855 | 1387 | WXHBITMAP wxMask::GetHBITMAP() const |
5fde6fcc | 1388 | { |
20b69855 | 1389 | return m_maskBitmap ; |
5fde6fcc GD |
1390 | } |
1391 | ||
20b69855 SC |
1392 | // ---------------------------------------------------------------------------- |
1393 | // wxBitmapHandler | |
1394 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 1395 | |
be52b341 GD |
1396 | wxBitmapHandler::~wxBitmapHandler() |
1397 | { | |
1398 | } | |
1399 | ||
e9576ca5 SC |
1400 | bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) |
1401 | { | |
1402 | return FALSE; | |
1403 | } | |
1404 | ||
a8562f55 | 1405 | bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
e9576ca5 SC |
1406 | int desiredWidth, int desiredHeight) |
1407 | { | |
1408 | return FALSE; | |
1409 | } | |
1410 | ||
a8562f55 | 1411 | bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) |
e9576ca5 SC |
1412 | { |
1413 | return FALSE; | |
1414 | } | |
1415 | ||
20b69855 SC |
1416 | // ---------------------------------------------------------------------------- |
1417 | // Standard Handlers | |
1418 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 1419 | |
519cb848 SC |
1420 | class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler |
1421 | { | |
1422 | DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) | |
1423 | public: | |
1424 | inline wxPICTResourceHandler() | |
1425 | { | |
3bf2bdfb GD |
1426 | SetName(wxT("Macintosh Pict resource")); |
1427 | SetExtension(wxEmptyString); | |
1428 | SetType(wxBITMAP_TYPE_PICT_RESOURCE); | |
519cb848 SC |
1429 | }; |
1430 | ||
1431 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1432 | int desiredWidth, int desiredHeight); | |
1433 | }; | |
1434 | IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) | |
1435 | ||
179e085f | 1436 | |
519cb848 SC |
1437 | bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
1438 | int desiredWidth, int desiredHeight) | |
1439 | { | |
179e085f | 1440 | #if wxUSE_METAFILE |
4e9ed364 | 1441 | Str255 theName ; |
427ff662 | 1442 | wxMacStringToPascal( name , theName ) ; |
55e18dbe | 1443 | |
4e9ed364 RR |
1444 | PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; |
1445 | if ( thePict ) | |
1446 | { | |
20b69855 SC |
1447 | wxMetafile mf ; |
1448 | mf.SetHMETAFILE((WXHMETAFILE) thePict ) ; | |
1449 | bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ; | |
1450 | wxMemoryDC dc ; | |
1451 | dc.SelectObject( *bitmap ) ; | |
1452 | mf.Play( &dc ) ; | |
1453 | dc.SelectObject( wxNullBitmap ) ; | |
4e9ed364 RR |
1454 | return TRUE ; |
1455 | } | |
179e085f | 1456 | #endif //wxUSE_METAFILE |
4e9ed364 | 1457 | return FALSE ; |
519cb848 SC |
1458 | } |
1459 | ||
179e085f | 1460 | |
e9576ca5 SC |
1461 | void wxBitmap::InitStandardHandlers() |
1462 | { | |
973b0afb GD |
1463 | AddHandler(new wxPICTResourceHandler) ; |
1464 | AddHandler(new wxICONResourceHandler) ; | |
e9576ca5 | 1465 | } |
55e18dbe VZ |
1466 | |
1467 | // ---------------------------------------------------------------------------- | |
1468 | // raw bitmap access support | |
1469 | // ---------------------------------------------------------------------------- | |
1470 | ||
1471 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) | |
1472 | { | |
1473 | if ( !Ok() ) | |
1474 | { | |
1475 | // no bitmap, no data (raw or otherwise) | |
1476 | return NULL; | |
1477 | } | |
1478 | ||
20b69855 SC |
1479 | data.m_width = GetWidth() ; |
1480 | data.m_height = GetHeight() ; | |
1481 | data.m_stride = GetWidth() * 4 ; | |
1482 | return GetRawAccess() ; | |
55e18dbe VZ |
1483 | } |
1484 | ||
1e74d03b | 1485 | void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) |
55e18dbe VZ |
1486 | { |
1487 | if ( !Ok() ) | |
1488 | return; | |
1489 | ||
20b69855 SC |
1490 | // TODO : if we have some information about the API we should check |
1491 | // this code looks strange... | |
1492 | ||
1493 | if ( M_BITMAPDATA->HasAlpha() ) | |
1e74d03b VZ |
1494 | { |
1495 | wxAlphaPixelData& data = (wxAlphaPixelData&)dataBase; | |
1496 | ||
1497 | int w = data.GetWidth(), | |
1498 | h = data.GetHeight(); | |
1499 | ||
1500 | wxBitmap bmpMask(GetWidth(), GetHeight(), 32); | |
1501 | wxAlphaPixelData dataMask(bmpMask, data.GetOrigin(), wxSize(w, h)); | |
1502 | wxAlphaPixelData::Iterator pMask(dataMask), | |
1503 | p(data); | |
1504 | for ( int y = 0; y < h; y++ ) | |
1505 | { | |
1506 | wxAlphaPixelData::Iterator rowStartMask = pMask, | |
1507 | rowStart = p; | |
1508 | ||
1509 | for ( int x = 0; x < w; x++ ) | |
1510 | { | |
1511 | const wxAlphaPixelData::Iterator::ChannelType | |
1512 | alpha = p.Alpha(); | |
1513 | ||
1514 | pMask.Red() = alpha; | |
1515 | pMask.Green() = alpha; | |
1516 | pMask.Blue() = alpha; | |
1517 | ||
1518 | ++p; | |
1519 | ++pMask; | |
1520 | } | |
1521 | ||
1522 | p = rowStart; | |
1523 | p.OffsetY(data, 1); | |
1524 | ||
1525 | pMask = rowStartMask; | |
1526 | pMask.OffsetY(dataMask, 1); | |
1527 | } | |
1528 | ||
1529 | SetMask(new wxMask(bmpMask)); | |
1530 | } | |
55e18dbe VZ |
1531 | } |
1532 | ||
1533 | void wxBitmap::UseAlpha() | |
1534 | { | |
1e74d03b VZ |
1535 | // remember that we are using alpha channel, we'll need to create a proper |
1536 | // mask in UngetRawData() | |
20b69855 | 1537 | M_BITMAPDATA->UseAlpha( true ); |
55e18dbe | 1538 | } |