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