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