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