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