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