]>
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); | |
568 | wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") ); | |
c881da96 | 569 | DisposeHandle( (Handle) iconFamily ) ; |
b28aeea5 | 570 | } |
1cb97a54 | 571 | |
b28aeea5 SC |
572 | return m_iconRef ; |
573 | } | |
574 | ||
575 | PicHandle wxBitmapRefData::GetPictHandle() | |
576 | { | |
577 | if ( m_pictHandle == NULL ) | |
578 | { | |
6239ee05 SC |
579 | #ifndef __LP64__ |
580 | GraphicsExportComponent exporter = 0; | |
581 | OSStatus err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypePicture, &exporter); | |
582 | if (noErr == err) | |
583 | { | |
584 | m_pictHandle = (PicHandle) NewHandle(0); | |
585 | if ( m_pictHandle ) | |
586 | { | |
587 | // QT does not correctly export the mask | |
588 | // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands | |
968c951f | 589 | CGImageRef imageRef = CreateCGImage(); |
6239ee05 SC |
590 | err = GraphicsExportSetInputCGImage( exporter, imageRef ); |
591 | err = GraphicsExportSetOutputHandle(exporter, (Handle)m_pictHandle); | |
592 | err = GraphicsExportDoExport(exporter, NULL); | |
593 | CGImageRelease( imageRef ); | |
594 | ||
595 | size_t handleSize = GetHandleSize( (Handle) m_pictHandle ); | |
596 | // the 512 bytes header is only needed for pict files, but not in memory | |
597 | if ( handleSize >= 512 ) | |
598 | { | |
599 | memmove( *m_pictHandle , (char*)(*m_pictHandle)+512, handleSize - 512 ); | |
600 | SetHandleSize( (Handle) m_pictHandle, handleSize - 512 ); | |
601 | } | |
602 | } | |
603 | CloseComponent( exporter ); | |
604 | } | |
4f74e0d1 | 605 | #endif |
b28aeea5 | 606 | } |
1cb97a54 | 607 | |
b28aeea5 SC |
608 | return m_pictHandle ; |
609 | } | |
55e18dbe | 610 | |
968c951f | 611 | CGImageRef wxBitmapRefData::CreateCGImage() const |
be295828 | 612 | { |
20b69855 SC |
613 | wxASSERT( m_ok ) ; |
614 | wxASSERT( m_rawAccessCount >= 0 ) ; | |
615 | CGImageRef image ; | |
616 | if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) | |
be295828 | 617 | { |
e1673e52 | 618 | if ( m_depth != 1 && m_bitmapMask == NULL ) |
be295828 | 619 | { |
6239ee05 | 620 | if ( m_bitmapMask ) |
be295828 | 621 | { |
6239ee05 SC |
622 | CGImageRef tempImage = CGBitmapContextCreateImage( m_hBitmap ); |
623 | CGImageRef tempMask = CGBitmapContextCreateImage((CGContextRef) m_bitmapMask->GetHBITMAP() ); | |
624 | image = CGImageCreateWithMask( tempImage, tempMask ); | |
625 | CGImageRelease(tempMask); | |
626 | CGImageRelease(tempImage); | |
be295828 | 627 | } |
6239ee05 SC |
628 | else |
629 | image = CGBitmapContextCreateImage( m_hBitmap ); | |
be295828 | 630 | } |
1cb97a54 | 631 | else |
e40298d5 | 632 | { |
6239ee05 SC |
633 | size_t imageSize = m_height * m_bytesPerRow ; |
634 | void * dataBuffer = m_memBuf.GetData() ; | |
635 | int w = m_width ; | |
636 | int h = m_height ; | |
637 | CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; | |
fb728ebb | 638 | wxMemoryBuffer membuf; |
8f884a0d | 639 | |
6239ee05 | 640 | if ( m_bitmapMask ) |
1cb97a54 | 641 | { |
6239ee05 | 642 | alphaInfo = kCGImageAlphaFirst ; |
fb728ebb SC |
643 | unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ; |
644 | memcpy( destalphastart , dataBuffer , imageSize ) ; | |
6239ee05 SC |
645 | unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; |
646 | int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; | |
6239ee05 SC |
647 | for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes) |
648 | { | |
649 | unsigned char *sourcemask = sourcemaskstart ; | |
650 | unsigned char *destalpha = destalphastart ; | |
651 | for ( int x = 0 ; x < w ; ++x , sourcemask += kMaskBytesPerPixel , destalpha += 4 ) | |
652 | { | |
653 | *destalpha = 0xFF - *sourcemask ; | |
654 | } | |
655 | } | |
fb728ebb | 656 | membuf.UngetWriteBuf( imageSize ); |
6239ee05 SC |
657 | } |
658 | else | |
659 | { | |
660 | if ( m_hasAlpha ) | |
661 | { | |
20b69855 | 662 | #if wxMAC_USE_PREMULTIPLIED_ALPHA |
6239ee05 | 663 | alphaInfo = kCGImageAlphaPremultipliedFirst ; |
20b69855 | 664 | #else |
6239ee05 | 665 | alphaInfo = kCGImageAlphaFirst ; |
20b69855 | 666 | #endif |
6239ee05 | 667 | } |
8f884a0d | 668 | |
fb728ebb | 669 | membuf = m_memBuf; |
1cb97a54 | 670 | } |
8f884a0d | 671 | |
6239ee05 SC |
672 | CGDataProviderRef dataProvider = NULL ; |
673 | if ( m_depth == 1 ) | |
674 | { | |
fb728ebb SC |
675 | // TODO CHECK ALIGNMENT |
676 | wxMemoryBuffer maskBuf; | |
677 | unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( m_width * m_height ); | |
678 | unsigned char * bufData = (unsigned char *) membuf.GetData() ; | |
6239ee05 | 679 | // copy one color component |
fb728ebb SC |
680 | size_t i = 0; |
681 | for( int y = 0 ; y < m_height ; bufData+= m_bytesPerRow, ++y ) | |
682 | { | |
683 | unsigned char *bufDataIter = bufData+3; | |
684 | for ( int x = 0 ; x < m_width ; bufDataIter += 4, ++x, ++i ) | |
685 | { | |
686 | maskBufData[i] = *bufDataIter; | |
687 | } | |
688 | } | |
689 | maskBuf.UngetWriteBuf( m_width * m_height ); | |
8f884a0d | 690 | |
6239ee05 | 691 | dataProvider = |
fb728ebb | 692 | wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); |
8f884a0d | 693 | |
6239ee05 SC |
694 | image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false ); |
695 | } | |
696 | else | |
697 | { | |
698 | CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); | |
fb728ebb | 699 | dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf ); |
6239ee05 SC |
700 | image = |
701 | ::CGImageCreate( | |
702 | w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo , | |
703 | dataProvider, NULL , false , kCGRenderingIntentDefault ); | |
704 | } | |
705 | CGDataProviderRelease( dataProvider); | |
b083987d | 706 | } |
20b69855 SC |
707 | } |
708 | else | |
709 | { | |
710 | image = m_cgImageRef ; | |
711 | CGImageRetain( image ) ; | |
be295828 | 712 | } |
1cb97a54 | 713 | |
20b69855 SC |
714 | if ( m_rawAccessCount == 0 && m_cgImageRef == NULL) |
715 | { | |
716 | // we keep it for later use | |
717 | m_cgImageRef = image ; | |
718 | CGImageRetain( image ) ; | |
902725ee | 719 | } |
1cb97a54 | 720 | |
20b69855 | 721 | return image ; |
be295828 SC |
722 | } |
723 | ||
6239ee05 SC |
724 | CGContextRef wxBitmapRefData::GetBitmapContext() const |
725 | { | |
726 | return m_hBitmap; | |
727 | } | |
20b69855 | 728 | |
20b69855 SC |
729 | void wxBitmapRefData::Free() |
730 | { | |
731 | wxASSERT_MSG( m_rawAccessCount == 0 , wxT("Bitmap still selected when destroyed") ) ; | |
732 | ||
20b69855 SC |
733 | if ( m_cgImageRef ) |
734 | { | |
735 | CGImageRelease( m_cgImageRef ) ; | |
736 | m_cgImageRef = NULL ; | |
e40298d5 | 737 | } |
1cb97a54 | 738 | |
b28aeea5 SC |
739 | if ( m_iconRef ) |
740 | { | |
741 | ReleaseIconRef( m_iconRef ) ; | |
742 | m_iconRef = NULL ; | |
743 | } | |
1cb97a54 | 744 | |
4f74e0d1 | 745 | #ifndef __LP64__ |
b28aeea5 SC |
746 | if ( m_pictHandle ) |
747 | { | |
748 | KillPicture( m_pictHandle ) ; | |
749 | m_pictHandle = NULL ; | |
750 | } | |
6239ee05 | 751 | #endif |
1cb97a54 | 752 | |
20b69855 SC |
753 | if ( m_hBitmap ) |
754 | { | |
6239ee05 | 755 | CGContextRelease(m_hBitmap); |
20b69855 SC |
756 | m_hBitmap = NULL ; |
757 | } | |
1cb97a54 | 758 | |
20b69855 | 759 | if (m_bitmapMask) |
e40298d5 | 760 | { |
20b69855 SC |
761 | delete m_bitmapMask; |
762 | m_bitmapMask = NULL; | |
e40298d5 | 763 | } |
e9576ca5 SC |
764 | } |
765 | ||
85f296a3 SC |
766 | wxBitmapRefData::~wxBitmapRefData() |
767 | { | |
20b69855 | 768 | Free() ; |
85f296a3 SC |
769 | } |
770 | ||
90b959ae SC |
771 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
772 | { | |
1cb97a54 | 773 | bool created = false ; |
20b69855 SC |
774 | int w = icon.GetWidth() ; |
775 | int h = icon.GetHeight() ; | |
b28aeea5 | 776 | |
20b69855 SC |
777 | Create( icon.GetWidth() , icon.GetHeight() ) ; |
778 | ||
71cc158e | 779 | if ( w == h && ( w == 16 || w == 32 || w == 48 || w == 128 ) ) |
20b69855 SC |
780 | { |
781 | IconFamilyHandle iconFamily = NULL ; | |
1cb97a54 DS |
782 | Handle imagehandle = NewHandle( 0 ) ; |
783 | Handle maskhandle = NewHandle( 0 ) ; | |
902725ee | 784 | |
9cc62fc8 SC |
785 | OSType maskType = 0; |
786 | OSType dataType = 0; | |
902725ee | 787 | IconSelectorValue selector = 0 ; |
1cb97a54 DS |
788 | |
789 | switch (w) | |
71cc158e | 790 | { |
1cb97a54 DS |
791 | case 128: |
792 | dataType = kThumbnail32BitData ; | |
793 | maskType = kThumbnail8BitMask ; | |
794 | selector = kSelectorAllAvailableData ; | |
795 | break; | |
796 | ||
797 | case 48: | |
798 | dataType = kHuge32BitData ; | |
799 | maskType = kHuge8BitMask ; | |
800 | selector = kSelectorHuge32Bit | kSelectorHuge8BitMask ; | |
801 | break; | |
802 | ||
803 | case 32: | |
804 | dataType = kLarge32BitData ; | |
805 | maskType = kLarge8BitMask ; | |
806 | selector = kSelectorLarge32Bit | kSelectorLarge8BitMask ; | |
807 | break; | |
808 | ||
809 | case 16: | |
810 | dataType = kSmall32BitData ; | |
811 | maskType = kSmall8BitMask ; | |
812 | selector = kSelectorSmall32Bit | kSelectorSmall8BitMask ; | |
813 | break; | |
814 | ||
815 | default: | |
816 | break; | |
71cc158e | 817 | } |
71cc158e | 818 | |
1cb97a54 | 819 | OSStatus err = IconRefToIconFamily( MAC_WXHICON(icon.GetHICON()) , selector , &iconFamily ) ; |
71cc158e | 820 | |
1cb97a54 DS |
821 | err = GetIconFamilyData( iconFamily , dataType , imagehandle ) ; |
822 | err = GetIconFamilyData( iconFamily , maskType , maskhandle ) ; | |
b28aeea5 SC |
823 | size_t imagehandlesize = GetHandleSize( imagehandle ) ; |
824 | size_t maskhandlesize = GetHandleSize( maskhandle ) ; | |
902725ee | 825 | |
b28aeea5 | 826 | if ( imagehandlesize != 0 && maskhandlesize != 0 ) |
20b69855 | 827 | { |
b28aeea5 SC |
828 | wxASSERT( GetHandleSize( imagehandle ) == w * 4 * h ) ; |
829 | wxASSERT( GetHandleSize( maskhandle ) == w * h ) ; | |
1cb97a54 | 830 | |
b28aeea5 | 831 | UseAlpha() ; |
1cb97a54 | 832 | |
b28aeea5 SC |
833 | unsigned char *source = (unsigned char *) *imagehandle ; |
834 | unsigned char *sourcemask = (unsigned char *) *maskhandle ; | |
b28aeea5 | 835 | unsigned char* destination = (unsigned char*) BeginRawAccess() ; |
1cb97a54 | 836 | |
b28aeea5 | 837 | for ( int y = 0 ; y < h ; ++y ) |
20b69855 | 838 | { |
b28aeea5 SC |
839 | for ( int x = 0 ; x < w ; ++x ) |
840 | { | |
6239ee05 SC |
841 | unsigned char a = *sourcemask++; |
842 | *destination++ = a; | |
b28aeea5 | 843 | source++ ; |
6239ee05 SC |
844 | #if wxMAC_USE_PREMULTIPLIED_ALPHA |
845 | *destination++ = ( (*source++) * a + 127 ) / 255; | |
846 | *destination++ = ( (*source++) * a + 127 ) / 255; | |
847 | *destination++ = ( (*source++) * a + 127 ) / 255; | |
8f884a0d | 848 | #else |
b28aeea5 SC |
849 | *destination++ = *source++ ; |
850 | *destination++ = *source++ ; | |
851 | *destination++ = *source++ ; | |
6239ee05 | 852 | #endif |
b28aeea5 | 853 | } |
20b69855 | 854 | } |
1cb97a54 | 855 | |
b28aeea5 SC |
856 | EndRawAccess() ; |
857 | DisposeHandle( imagehandle ) ; | |
858 | DisposeHandle( maskhandle ) ; | |
95d8425f | 859 | created = true ; |
20b69855 | 860 | } |
902725ee | 861 | |
1cb97a54 | 862 | DisposeHandle( (Handle) iconFamily ) ; |
20b69855 | 863 | } |
902725ee | 864 | |
b28aeea5 | 865 | if ( !created ) |
902725ee | 866 | { |
20b69855 SC |
867 | wxMemoryDC dc ; |
868 | dc.SelectObject( *this ) ; | |
869 | dc.DrawIcon( icon , 0 , 0 ) ; | |
870 | dc.SelectObject( wxNullBitmap ) ; | |
871 | } | |
1cb97a54 | 872 | |
125c389e | 873 | return true; |
90b959ae SC |
874 | } |
875 | ||
e9576ca5 SC |
876 | wxBitmap::wxBitmap() |
877 | { | |
e9576ca5 SC |
878 | } |
879 | ||
880 | wxBitmap::~wxBitmap() | |
881 | { | |
e9576ca5 SC |
882 | } |
883 | ||
884 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) | |
885 | { | |
0d0b1695 | 886 | wxBitmapRefData* bitmapRefData; |
e9576ca5 | 887 | |
0d0b1695 | 888 | m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ; |
1cb97a54 | 889 | |
0d0b1695 SC |
890 | if (bitmapRefData->IsOk()) |
891 | { | |
892 | if ( no_bits == 1 ) | |
973b0afb | 893 | { |
0d0b1695 SC |
894 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; |
895 | if ( the_width % (sizeof(unsigned char) * 8) ) | |
896 | linesize += sizeof(unsigned char); | |
897 | ||
898 | unsigned char* linestart = (unsigned char*) bits ; | |
899 | unsigned char* destptr = (unsigned char*) BeginRawAccess() ; | |
1cb97a54 | 900 | |
0d0b1695 | 901 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) |
973b0afb | 902 | { |
0d0b1695 SC |
903 | unsigned char* destination = destptr; |
904 | int index, bit, mask; | |
1cb97a54 | 905 | |
0d0b1695 | 906 | for ( int x = 0 ; x < the_width ; ++x ) |
973b0afb | 907 | { |
0d0b1695 SC |
908 | index = x / 8 ; |
909 | bit = x % 8 ; | |
910 | mask = 1 << bit ; | |
911 | ||
912 | if ( linestart[index] & mask ) | |
913 | { | |
914 | *destination++ = 0xFF ; | |
915 | *destination++ = 0 ; | |
916 | *destination++ = 0 ; | |
917 | *destination++ = 0 ; | |
918 | } | |
919 | else | |
920 | { | |
921 | *destination++ = 0xFF ; | |
922 | *destination++ = 0xFF ; | |
923 | *destination++ = 0xFF ; | |
924 | *destination++ = 0xFF ; | |
925 | } | |
973b0afb GD |
926 | } |
927 | } | |
1cb97a54 | 928 | |
0d0b1695 SC |
929 | EndRawAccess() ; |
930 | } | |
931 | else | |
932 | { | |
933 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); | |
934 | } | |
935 | } /* bitmapRefData->IsOk() */ | |
e9576ca5 SC |
936 | } |
937 | ||
938 | wxBitmap::wxBitmap(int w, int h, int d) | |
939 | { | |
940 | (void)Create(w, h, d); | |
e9576ca5 SC |
941 | } |
942 | ||
452418c4 | 943 | wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
944 | { |
945 | (void) Create(data, type, width, height, depth); | |
e9576ca5 SC |
946 | } |
947 | ||
a8562f55 | 948 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) |
e9576ca5 | 949 | { |
a8562f55 | 950 | LoadFile(filename, type); |
e9576ca5 SC |
951 | } |
952 | ||
8f884a0d | 953 | wxGDIRefData* wxBitmap::CreateGDIRefData() const |
2bf8f4c0 RD |
954 | { |
955 | return new wxBitmapRefData; | |
956 | } | |
957 | ||
8f884a0d | 958 | wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const |
2bf8f4c0 RD |
959 | { |
960 | return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData *, data)); | |
961 | } | |
962 | ||
1cb97a54 | 963 | void * wxBitmap::GetRawAccess() const |
20b69855 SC |
964 | { |
965 | wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ; | |
1cb97a54 | 966 | |
20b69855 SC |
967 | return M_BITMAPDATA->GetRawAccess() ; |
968 | } | |
969 | ||
1cb97a54 | 970 | void * wxBitmap::BeginRawAccess() |
20b69855 SC |
971 | { |
972 | wxCHECK_MSG( Ok() , NULL , wxT("invalid bitmap") ) ; | |
1cb97a54 | 973 | |
20b69855 SC |
974 | return M_BITMAPDATA->BeginRawAccess() ; |
975 | } | |
976 | ||
977 | void wxBitmap::EndRawAccess() | |
978 | { | |
979 | wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ; | |
1cb97a54 | 980 | |
20b69855 SC |
981 | M_BITMAPDATA->EndRawAccess() ; |
982 | } | |
983 | ||
968c951f | 984 | CGImageRef wxBitmap::CreateCGImage() const |
03e11df5 | 985 | { |
20b69855 | 986 | wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; |
1cb97a54 | 987 | |
968c951f SC |
988 | return M_BITMAPDATA->CreateCGImage() ; |
989 | } | |
990 | ||
991 | IconRef wxBitmap::GetIconRef() const | |
992 | { | |
993 | wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; | |
8f884a0d | 994 | |
968c951f SC |
995 | return M_BITMAPDATA->GetIconRef() ; |
996 | } | |
997 | ||
998 | IconRef wxBitmap::CreateIconRef() const | |
999 | { | |
1000 | IconRef icon = GetIconRef(); | |
1001 | verify_noerr( AcquireIconRef(icon) ); | |
1002 | return icon; | |
03e11df5 GD |
1003 | } |
1004 | ||
5fde6fcc GD |
1005 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const |
1006 | { | |
20b69855 | 1007 | wxCHECK_MSG( Ok() && |
5fde6fcc GD |
1008 | (rect.x >= 0) && (rect.y >= 0) && |
1009 | (rect.x+rect.width <= GetWidth()) && | |
1010 | (rect.y+rect.height <= GetHeight()), | |
1011 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
1012 | ||
20b69855 SC |
1013 | wxBitmap ret( rect.width, rect.height, GetDepth() ); |
1014 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
5fde6fcc | 1015 | |
20b69855 SC |
1016 | int destwidth = rect.width ; |
1017 | int destheight = rect.height ; | |
1cb97a54 | 1018 | |
20b69855 | 1019 | { |
1cb97a54 DS |
1020 | unsigned char *sourcedata = (unsigned char*) GetRawAccess() ; |
1021 | unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ; | |
1022 | wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ; | |
1023 | ||
6239ee05 SC |
1024 | int sourcelinesize = GetBitmapData()->GetBytesPerRow() ; |
1025 | int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ; | |
20b69855 SC |
1026 | unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; |
1027 | unsigned char *dest = destdata ; | |
1cb97a54 DS |
1028 | |
1029 | for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) | |
20b69855 SC |
1030 | { |
1031 | memcpy( dest , source , destlinesize ) ; | |
1032 | } | |
1033 | } | |
1cb97a54 | 1034 | |
20b69855 | 1035 | ret.EndRawAccess() ; |
902725ee | 1036 | |
20b69855 SC |
1037 | if ( M_BITMAPDATA->m_bitmapMask ) |
1038 | { | |
1039 | wxMemoryBuffer maskbuf ; | |
6239ee05 | 1040 | int rowBytes = GetBestBytesPerRow( destwidth * kMaskBytesPerPixel ); |
20b69855 | 1041 | size_t maskbufsize = rowBytes * destheight ; |
20b69855 | 1042 | |
7fe44dee | 1043 | int sourcelinesize = M_BITMAPDATA->m_bitmapMask->GetBytesPerRow() ; |
20b69855 | 1044 | int destlinesize = rowBytes ; |
1cb97a54 | 1045 | |
20b69855 | 1046 | unsigned char *source = (unsigned char *) M_BITMAPDATA->m_bitmapMask->GetRawAccess() ; |
1cb97a54 DS |
1047 | unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ; |
1048 | wxASSERT( (source != NULL) && (destdata != NULL) ) ; | |
1049 | ||
6239ee05 | 1050 | source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ; |
20b69855 SC |
1051 | unsigned char *dest = destdata ; |
1052 | ||
1cb97a54 | 1053 | for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) |
20b69855 | 1054 | { |
392e179f | 1055 | memcpy( dest , source , destlinesize ) ; |
20b69855 | 1056 | } |
1cb97a54 | 1057 | |
20b69855 SC |
1058 | maskbuf.UngetWriteBuf( maskbufsize ) ; |
1059 | ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ; | |
1060 | } | |
1061 | else if ( HasAlpha() ) | |
1062 | ret.UseAlpha() ; | |
5fde6fcc | 1063 | |
20b69855 | 1064 | return ret; |
5fde6fcc GD |
1065 | } |
1066 | ||
e9576ca5 SC |
1067 | bool wxBitmap::Create(int w, int h, int d) |
1068 | { | |
1069 | UnRef(); | |
1070 | ||
20b69855 SC |
1071 | if ( d < 0 ) |
1072 | d = wxDisplayDepth() ; | |
2a391f87 | 1073 | |
20b69855 | 1074 | m_refData = new wxBitmapRefData( w , h , d ); |
55e18dbe | 1075 | |
8f884a0d | 1076 | return M_BITMAPDATA->IsOk() ; |
519cb848 SC |
1077 | } |
1078 | ||
a8562f55 | 1079 | bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) |
e9576ca5 SC |
1080 | { |
1081 | UnRef(); | |
1082 | ||
e9576ca5 SC |
1083 | wxBitmapHandler *handler = FindHandler(type); |
1084 | ||
a8562f55 GD |
1085 | if ( handler ) |
1086 | { | |
4e9ed364 | 1087 | m_refData = new wxBitmapRefData; |
e9576ca5 | 1088 | |
a8562f55 | 1089 | return handler->LoadFile(this, filename, type, -1, -1); |
e9576ca5 | 1090 | } |
a8562f55 GD |
1091 | else |
1092 | { | |
d45318b8 | 1093 | #if wxUSE_IMAGE |
a8562f55 | 1094 | wxImage loadimage(filename, type); |
1cb97a54 DS |
1095 | if (loadimage.Ok()) |
1096 | { | |
a8562f55 | 1097 | *this = loadimage; |
1cb97a54 | 1098 | |
a8562f55 GD |
1099 | return true; |
1100 | } | |
d45318b8 | 1101 | #endif |
a8562f55 | 1102 | } |
1cb97a54 | 1103 | |
427ff662 | 1104 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); |
1cb97a54 | 1105 | |
a8562f55 | 1106 | return false; |
e9576ca5 SC |
1107 | } |
1108 | ||
452418c4 | 1109 | bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth) |
e9576ca5 SC |
1110 | { |
1111 | UnRef(); | |
1112 | ||
1113 | m_refData = new wxBitmapRefData; | |
1114 | ||
1115 | wxBitmapHandler *handler = FindHandler(type); | |
1116 | ||
1cb97a54 DS |
1117 | if ( handler == NULL ) |
1118 | { | |
427ff662 | 1119 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); |
e9576ca5 | 1120 | |
902725ee | 1121 | return false; |
e9576ca5 SC |
1122 | } |
1123 | ||
1124 | return handler->Create(this, data, type, width, height, depth); | |
1125 | } | |
1126 | ||
d45318b8 RN |
1127 | #if wxUSE_IMAGE |
1128 | ||
fec19ea9 VS |
1129 | wxBitmap::wxBitmap(const wxImage& image, int depth) |
1130 | { | |
637b7e4f | 1131 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); |
8f884a0d | 1132 | |
fec19ea9 VS |
1133 | // width and height of the device-dependent bitmap |
1134 | int width = image.GetWidth(); | |
1135 | int height = image.GetHeight(); | |
8f884a0d | 1136 | |
0d0b1695 SC |
1137 | wxBitmapRefData* bitmapRefData; |
1138 | ||
1139 | m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ; | |
8f884a0d | 1140 | |
0d0b1695 SC |
1141 | if ( bitmapRefData->IsOk()) |
1142 | { | |
1143 | // Create picture | |
8f884a0d | 1144 | |
0d0b1695 | 1145 | bool hasAlpha = false ; |
8f884a0d | 1146 | |
0d0b1695 SC |
1147 | if ( image.HasMask() ) |
1148 | { | |
1149 | // takes precedence, don't mix with alpha info | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | hasAlpha = image.HasAlpha() ; | |
1154 | } | |
8f884a0d | 1155 | |
0d0b1695 SC |
1156 | if ( hasAlpha ) |
1157 | UseAlpha() ; | |
8f884a0d | 1158 | |
0d0b1695 SC |
1159 | unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; |
1160 | register unsigned char* data = image.GetData(); | |
1161 | if ( destinationstart != NULL && data != NULL ) | |
fec19ea9 | 1162 | { |
0d0b1695 SC |
1163 | const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; |
1164 | for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) | |
20b69855 | 1165 | { |
0d0b1695 SC |
1166 | unsigned char * destination = destinationstart; |
1167 | for (int x = 0; x < width; x++) | |
6239ee05 | 1168 | { |
0d0b1695 SC |
1169 | if ( hasAlpha ) |
1170 | { | |
1171 | const unsigned char a = *alpha++; | |
1172 | *destination++ = a ; | |
1173 | ||
1174 | #if wxMAC_USE_PREMULTIPLIED_ALPHA | |
1175 | *destination++ = ((*data++) * a + 127) / 255 ; | |
1176 | *destination++ = ((*data++) * a + 127) / 255 ; | |
1177 | *destination++ = ((*data++) * a + 127) / 255 ; | |
1178 | #else | |
1179 | *destination++ = *data++ ; | |
1180 | *destination++ = *data++ ; | |
1181 | *destination++ = *data++ ; | |
1182 | #endif | |
1183 | } | |
1184 | else | |
1185 | { | |
1186 | *destination++ = 0xFF ; | |
1187 | *destination++ = *data++ ; | |
1188 | *destination++ = *data++ ; | |
1189 | *destination++ = *data++ ; | |
1190 | } | |
6239ee05 | 1191 | } |
20b69855 | 1192 | } |
8f884a0d | 1193 | |
0d0b1695 SC |
1194 | EndRawAccess() ; |
1195 | } | |
1196 | if ( image.HasMask() ) | |
1197 | SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; | |
1198 | } /* bitmapRefData->IsOk() */ | |
fec19ea9 VS |
1199 | } |
1200 | ||
1201 | wxImage wxBitmap::ConvertToImage() const | |
1202 | { | |
1203 | wxImage image; | |
55e18dbe | 1204 | |
fec19ea9 VS |
1205 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
1206 | ||
1207 | // create an wxImage object | |
1208 | int width = GetWidth(); | |
1209 | int height = GetHeight(); | |
1210 | image.Create( width, height ); | |
1211 | ||
1212 | unsigned char *data = image.GetData(); | |
fec19ea9 VS |
1213 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); |
1214 | ||
6239ee05 | 1215 | unsigned char* sourcestart = (unsigned char*) GetRawAccess() ; |
20b69855 SC |
1216 | |
1217 | bool hasAlpha = false ; | |
1218 | bool hasMask = false ; | |
7e7f40ed | 1219 | int maskBytesPerRow = 0 ; |
20b69855 SC |
1220 | unsigned char *alpha = NULL ; |
1221 | unsigned char *mask = NULL ; | |
1cb97a54 | 1222 | |
20b69855 | 1223 | if ( HasAlpha() ) |
20b69855 | 1224 | hasAlpha = true ; |
20b69855 SC |
1225 | |
1226 | if ( GetMask() ) | |
2b5f62a0 | 1227 | { |
20b69855 SC |
1228 | hasMask = true ; |
1229 | mask = (unsigned char*) GetMask()->GetRawAccess() ; | |
7e7f40ed | 1230 | maskBytesPerRow = GetMask()->GetBytesPerRow() ; |
e40298d5 | 1231 | } |
20b69855 SC |
1232 | |
1233 | if ( hasAlpha ) | |
e40298d5 | 1234 | { |
20b69855 SC |
1235 | image.SetAlpha() ; |
1236 | alpha = image.GetAlpha() ; | |
e40298d5 | 1237 | } |
1cb97a54 | 1238 | |
20b69855 | 1239 | int index = 0; |
902725ee | 1240 | |
20b69855 SC |
1241 | // The following masking algorithm is the same as well in msw/gtk: |
1242 | // the colour used as transparent one in wxImage and the one it is | |
7fe44dee | 1243 | // replaced with when it actually occurs in the bitmap |
20b69855 SC |
1244 | static const int MASK_RED = 1; |
1245 | static const int MASK_GREEN = 2; | |
1246 | static const int MASK_BLUE = 3; | |
1247 | static const int MASK_BLUE_REPLACEMENT = 2; | |
1248 | ||
6239ee05 | 1249 | for (int yy = 0; yy < height; yy++ , sourcestart += M_BITMAPDATA->GetBytesPerRow() , mask += maskBytesPerRow ) |
fec19ea9 | 1250 | { |
7e7f40ed | 1251 | unsigned char * maskp = mask ; |
6239ee05 | 1252 | unsigned char * source = sourcestart; |
1cb97a54 DS |
1253 | unsigned char a, r, g, b; |
1254 | long color; | |
1255 | ||
fec19ea9 VS |
1256 | for (int xx = 0; xx < width; xx++) |
1257 | { | |
1cb97a54 | 1258 | color = *((long*) source) ; |
f288c87b | 1259 | #ifdef WORDS_BIGENDIAN |
1cb97a54 DS |
1260 | a = ((color&0xFF000000) >> 24) ; |
1261 | r = ((color&0x00FF0000) >> 16) ; | |
1262 | g = ((color&0x0000FF00) >> 8) ; | |
1263 | b = (color&0x000000FF); | |
f288c87b SC |
1264 | #else |
1265 | b = ((color&0xFF000000) >> 24) ; | |
1266 | g = ((color&0x00FF0000) >> 16) ; | |
1267 | r = ((color&0x0000FF00) >> 8) ; | |
1268 | a = (color&0x000000FF); | |
1269 | #endif | |
20b69855 | 1270 | if ( hasMask ) |
55e18dbe | 1271 | { |
93a2b888 | 1272 | if ( *maskp++ == 0xFF ) |
e40298d5 | 1273 | { |
e59ea2c5 SC |
1274 | r = MASK_RED ; |
1275 | g = MASK_GREEN ; | |
1276 | b = MASK_BLUE ; | |
e40298d5 | 1277 | } |
e59ea2c5 SC |
1278 | else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE ) |
1279 | b = MASK_BLUE_REPLACEMENT ; | |
fec19ea9 | 1280 | } |
20b69855 | 1281 | else if ( hasAlpha ) |
6239ee05 | 1282 | { |
20b69855 | 1283 | *alpha++ = a ; |
6239ee05 SC |
1284 | #if wxMAC_USE_PREMULTIPLIED_ALPHA |
1285 | // this must be non-premultiplied data | |
1286 | if ( a != 0xFF && a!= 0 ) | |
1287 | { | |
1288 | r = r * 255 / a; | |
1289 | g = g * 255 / a; | |
1290 | b = b * 255 / a; | |
1291 | } | |
1292 | #endif | |
1293 | } | |
20b69855 SC |
1294 | |
1295 | data[index ] = r ; | |
1296 | data[index + 1] = g ; | |
1297 | data[index + 2] = b ; | |
1cb97a54 | 1298 | |
fec19ea9 | 1299 | index += 3; |
20b69855 | 1300 | source += 4 ; |
fec19ea9 VS |
1301 | } |
1302 | } | |
1cb97a54 | 1303 | |
20b69855 SC |
1304 | if ( hasMask ) |
1305 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); | |
1cb97a54 | 1306 | |
fec19ea9 VS |
1307 | return image; |
1308 | } | |
1309 | ||
d45318b8 | 1310 | #endif //wxUSE_IMAGE |
fec19ea9 | 1311 | |
7fe44dee DS |
1312 | bool wxBitmap::SaveFile( const wxString& filename, |
1313 | wxBitmapType type, const wxPalette *palette ) const | |
e9576ca5 | 1314 | { |
902725ee | 1315 | bool success = false; |
e9576ca5 SC |
1316 | wxBitmapHandler *handler = FindHandler(type); |
1317 | ||
a8562f55 GD |
1318 | if ( handler ) |
1319 | { | |
902725ee | 1320 | success = handler->SaveFile(this, filename, type, palette); |
a8562f55 GD |
1321 | } |
1322 | else | |
1323 | { | |
d45318b8 | 1324 | #if wxUSE_IMAGE |
a8562f55 | 1325 | wxImage image = ConvertToImage(); |
902725ee WS |
1326 | success = image.SaveFile(filename, type); |
1327 | #else | |
1328 | wxLogWarning(wxT("no bitmap handler for type %d defined."), type); | |
d45318b8 | 1329 | #endif |
a8562f55 | 1330 | } |
55e18dbe | 1331 | |
902725ee | 1332 | return success; |
e9576ca5 SC |
1333 | } |
1334 | ||
5fde6fcc GD |
1335 | int wxBitmap::GetHeight() const |
1336 | { | |
1337 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1338 | ||
20b69855 | 1339 | return M_BITMAPDATA->GetHeight(); |
5fde6fcc GD |
1340 | } |
1341 | ||
1342 | int wxBitmap::GetWidth() const | |
1343 | { | |
1344 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1345 | ||
20b69855 | 1346 | return M_BITMAPDATA->GetWidth() ; |
5fde6fcc GD |
1347 | } |
1348 | ||
1349 | int wxBitmap::GetDepth() const | |
1350 | { | |
1351 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
1352 | ||
20b69855 | 1353 | return M_BITMAPDATA->GetDepth(); |
5fde6fcc GD |
1354 | } |
1355 | ||
5fde6fcc GD |
1356 | wxMask *wxBitmap::GetMask() const |
1357 | { | |
1358 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
1359 | ||
1360 | return M_BITMAPDATA->m_bitmapMask; | |
1361 | } | |
1362 | ||
20b69855 SC |
1363 | bool wxBitmap::HasAlpha() const |
1364 | { | |
1365 | wxCHECK_MSG( Ok(), false , wxT("invalid bitmap") ); | |
1366 | ||
1367 | return M_BITMAPDATA->HasAlpha() ; | |
1368 | } | |
1369 | ||
e9576ca5 SC |
1370 | void wxBitmap::SetWidth(int w) |
1371 | { | |
55ccdb93 | 1372 | AllocExclusive(); |
20b69855 | 1373 | M_BITMAPDATA->SetWidth(w); |
e9576ca5 SC |
1374 | } |
1375 | ||
1376 | void wxBitmap::SetHeight(int h) | |
1377 | { | |
55ccdb93 | 1378 | AllocExclusive(); |
20b69855 | 1379 | M_BITMAPDATA->SetHeight(h); |
e9576ca5 SC |
1380 | } |
1381 | ||
1382 | void wxBitmap::SetDepth(int d) | |
1383 | { | |
55ccdb93 | 1384 | AllocExclusive(); |
20b69855 | 1385 | M_BITMAPDATA->SetDepth(d); |
e9576ca5 SC |
1386 | } |
1387 | ||
e9576ca5 SC |
1388 | void wxBitmap::SetOk(bool isOk) |
1389 | { | |
55ccdb93 | 1390 | AllocExclusive(); |
20b69855 | 1391 | M_BITMAPDATA->SetOk(isOk); |
e9576ca5 SC |
1392 | } |
1393 | ||
a6de86fa | 1394 | #if wxUSE_PALETTE |
5fde6fcc GD |
1395 | wxPalette *wxBitmap::GetPalette() const |
1396 | { | |
1397 | wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") ); | |
1398 | ||
1399 | return &M_BITMAPDATA->m_bitmapPalette; | |
1400 | } | |
1401 | ||
e9576ca5 SC |
1402 | void wxBitmap::SetPalette(const wxPalette& palette) |
1403 | { | |
55ccdb93 | 1404 | AllocExclusive(); |
e9576ca5 SC |
1405 | M_BITMAPDATA->m_bitmapPalette = palette ; |
1406 | } | |
a6de86fa | 1407 | #endif // wxUSE_PALETTE |
e9576ca5 SC |
1408 | |
1409 | void wxBitmap::SetMask(wxMask *mask) | |
1410 | { | |
55ccdb93 | 1411 | AllocExclusive(); |
a8562f55 | 1412 | // Remove existing mask if there is one. |
1e74d03b | 1413 | delete M_BITMAPDATA->m_bitmapMask; |
a8562f55 | 1414 | |
e9576ca5 SC |
1415 | M_BITMAPDATA->m_bitmapMask = mask ; |
1416 | } | |
1417 | ||
20b69855 | 1418 | WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const |
5fde6fcc | 1419 | { |
31c1cbc7 VZ |
1420 | wxUnusedVar(mask); |
1421 | ||
6239ee05 | 1422 | return WXHBITMAP(M_BITMAPDATA->GetBitmapContext()); |
5fde6fcc GD |
1423 | } |
1424 | ||
20b69855 SC |
1425 | // ---------------------------------------------------------------------------- |
1426 | // wxMask | |
1427 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
1428 | |
1429 | wxMask::wxMask() | |
1430 | { | |
20b69855 | 1431 | Init() ; |
e9576ca5 SC |
1432 | } |
1433 | ||
2bf8f4c0 RD |
1434 | wxMask::wxMask(const wxMask &tocopy) |
1435 | { | |
1436 | Init(); | |
1437 | ||
1438 | m_bytesPerRow = tocopy.m_bytesPerRow; | |
1439 | m_width = tocopy.m_width; | |
1440 | m_height = tocopy.m_height; | |
1441 | ||
1442 | size_t size = m_bytesPerRow * m_height; | |
1443 | unsigned char* dest = (unsigned char*)m_memBuf.GetWriteBuf( size ); | |
1444 | unsigned char* source = (unsigned char*)tocopy.m_memBuf.GetData(); | |
6239ee05 | 1445 | memcpy( dest, source, size ); |
2bf8f4c0 RD |
1446 | m_memBuf.UngetWriteBuf( size ) ; |
1447 | RealizeNative() ; | |
1448 | } | |
1449 | ||
e9576ca5 SC |
1450 | // Construct a mask from a bitmap and a colour indicating |
1451 | // the transparent area | |
7fe44dee | 1452 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
e9576ca5 | 1453 | { |
20b69855 | 1454 | Init() ; |
7fe44dee | 1455 | Create( bitmap, colour ); |
e9576ca5 SC |
1456 | } |
1457 | ||
20b69855 | 1458 | // Construct a mask from a mono bitmap (copies the bitmap). |
7fe44dee | 1459 | wxMask::wxMask( const wxBitmap& bitmap ) |
e9576ca5 | 1460 | { |
20b69855 | 1461 | Init() ; |
7fe44dee | 1462 | Create( bitmap ); |
e9576ca5 SC |
1463 | } |
1464 | ||
1465 | // Construct a mask from a mono bitmap (copies the bitmap). | |
392e179f | 1466 | |
1cb97a54 | 1467 | wxMask::wxMask( const wxMemoryBuffer& data, int width , int height , int bytesPerRow ) |
e9576ca5 | 1468 | { |
20b69855 | 1469 | Init() ; |
7fe44dee | 1470 | Create( data, width , height , bytesPerRow ); |
e9576ca5 SC |
1471 | } |
1472 | ||
1473 | wxMask::~wxMask() | |
1474 | { | |
4e9ed364 RR |
1475 | if ( m_maskBitmap ) |
1476 | { | |
6239ee05 | 1477 | CGContextRelease( (CGContextRef) m_maskBitmap ); |
4e9ed364 RR |
1478 | m_maskBitmap = NULL ; |
1479 | } | |
e9576ca5 SC |
1480 | } |
1481 | ||
902725ee | 1482 | void wxMask::Init() |
e9576ca5 | 1483 | { |
20b69855 | 1484 | m_width = m_height = m_bytesPerRow = 0 ; |
20b69855 | 1485 | m_maskBitmap = NULL ; |
20b69855 | 1486 | } |
5fde6fcc | 1487 | |
20b69855 SC |
1488 | void *wxMask::GetRawAccess() const |
1489 | { | |
1490 | return m_memBuf.GetData() ; | |
1491 | } | |
5fde6fcc | 1492 | |
7fe44dee DS |
1493 | // The default ColorTable for k8IndexedGrayPixelFormat in Intel appears to be broken, so we'll use an non-indexed |
1494 | // bitmap mask instead; in order to keep the code simple, the change applies to PowerPC implementations as well | |
431c82e0 | 1495 | |
902725ee | 1496 | void wxMask::RealizeNative() |
20b69855 | 1497 | { |
20b69855 SC |
1498 | if ( m_maskBitmap ) |
1499 | { | |
6239ee05 | 1500 | CGContextRelease( (CGContextRef) m_maskBitmap ); |
20b69855 SC |
1501 | m_maskBitmap = NULL ; |
1502 | } | |
1cb97a54 | 1503 | |
6239ee05 SC |
1504 | CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); |
1505 | // from MouseTracking sample : | |
8f884a0d | 1506 | // Ironically, due to a bug in CGImageCreateWithMask, you cannot use |
6239ee05 | 1507 | // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point! |
8f884a0d VZ |
1508 | |
1509 | m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, | |
6239ee05 SC |
1510 | kCGImageAlphaNone ); |
1511 | CGColorSpaceRelease( colorspace ); | |
1512 | wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ; | |
20b69855 | 1513 | } |
5fde6fcc | 1514 | |
20b69855 | 1515 | // Create a mask from a mono bitmap (copies the bitmap). |
392e179f | 1516 | |
20b69855 SC |
1517 | bool wxMask::Create(const wxMemoryBuffer& data,int width , int height , int bytesPerRow) |
1518 | { | |
1519 | m_memBuf = data ; | |
1520 | m_width = width ; | |
1521 | m_height = height ; | |
1522 | m_bytesPerRow = bytesPerRow ; | |
1cb97a54 | 1523 | |
20b69855 | 1524 | wxASSERT( data.GetDataLen() == (size_t)(height * bytesPerRow) ) ; |
1cb97a54 | 1525 | |
20b69855 | 1526 | RealizeNative() ; |
1cb97a54 | 1527 | |
20b69855 | 1528 | return true ; |
e9576ca5 SC |
1529 | } |
1530 | ||
20b69855 SC |
1531 | // Create a mask from a mono bitmap (copies the bitmap). |
1532 | bool wxMask::Create(const wxBitmap& bitmap) | |
e9576ca5 | 1533 | { |
20b69855 SC |
1534 | m_width = bitmap.GetWidth() ; |
1535 | m_height = bitmap.GetHeight() ; | |
6239ee05 | 1536 | m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; |
1cb97a54 | 1537 | |
20b69855 SC |
1538 | size_t size = m_bytesPerRow * m_height ; |
1539 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; | |
1cb97a54 DS |
1540 | wxASSERT( destdatabase != NULL ) ; |
1541 | ||
20b69855 SC |
1542 | memset( destdatabase , 0 , size ) ; |
1543 | unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; | |
1cb97a54 | 1544 | |
20b69855 SC |
1545 | for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) |
1546 | { | |
1cb97a54 DS |
1547 | unsigned char *destdata = destdatabase ; |
1548 | unsigned char r, g, b; | |
1549 | ||
1550 | for ( int x = 0 ; x < m_width ; ++x ) | |
20b69855 SC |
1551 | { |
1552 | srcdata++ ; | |
1cb97a54 DS |
1553 | r = *srcdata++ ; |
1554 | g = *srcdata++ ; | |
1555 | b = *srcdata++ ; | |
1556 | ||
20b69855 | 1557 | if ( ( r + g + b ) > 0x10 ) |
20b69855 | 1558 | *destdata++ = 0xFF ; |
93a2b888 | 1559 | else |
392e179f | 1560 | *destdata++ = 0x00 ; |
20b69855 SC |
1561 | } |
1562 | } | |
1cb97a54 | 1563 | |
20b69855 SC |
1564 | m_memBuf.UngetWriteBuf( size ) ; |
1565 | RealizeNative() ; | |
1cb97a54 | 1566 | |
902725ee | 1567 | return true; |
e9576ca5 SC |
1568 | } |
1569 | ||
1570 | // Create a mask from a bitmap and a colour indicating | |
1571 | // the transparent area | |
1572 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1573 | { | |
20b69855 SC |
1574 | m_width = bitmap.GetWidth() ; |
1575 | m_height = bitmap.GetHeight() ; | |
6239ee05 | 1576 | m_bytesPerRow = GetBestBytesPerRow( m_width * kMaskBytesPerPixel ) ; |
20b69855 | 1577 | |
1cb97a54 | 1578 | size_t size = m_bytesPerRow * m_height ; |
20b69855 | 1579 | unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; |
1cb97a54 DS |
1580 | wxASSERT( destdatabase != NULL ) ; |
1581 | ||
20b69855 | 1582 | memset( destdatabase , 0 , size ) ; |
6239ee05 SC |
1583 | unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ; |
1584 | size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow(); | |
1cb97a54 | 1585 | |
6239ee05 | 1586 | for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow) |
8208e181 | 1587 | { |
6239ee05 | 1588 | unsigned char *srcdata = srcdatabase ; |
1cb97a54 DS |
1589 | unsigned char *destdata = destdatabase ; |
1590 | unsigned char r, g, b; | |
1591 | ||
1592 | for ( int x = 0 ; x < m_width ; ++x ) | |
55e18dbe | 1593 | { |
20b69855 | 1594 | srcdata++ ; |
1cb97a54 DS |
1595 | r = *srcdata++ ; |
1596 | g = *srcdata++ ; | |
1597 | b = *srcdata++ ; | |
1598 | ||
7fe44dee | 1599 | if ( colour == wxColour( r , g , b ) ) |
93a2b888 | 1600 | *destdata++ = 0xFF ; |
93a2b888 | 1601 | else |
93a2b888 | 1602 | *destdata++ = 0x00 ; |
8208e181 SC |
1603 | } |
1604 | } | |
1cb97a54 | 1605 | |
20b69855 SC |
1606 | m_memBuf.UngetWriteBuf( size ) ; |
1607 | RealizeNative() ; | |
1cb97a54 | 1608 | |
902725ee | 1609 | return true; |
e9576ca5 SC |
1610 | } |
1611 | ||
20b69855 | 1612 | WXHBITMAP wxMask::GetHBITMAP() const |
5fde6fcc | 1613 | { |
20b69855 | 1614 | return m_maskBitmap ; |
5fde6fcc GD |
1615 | } |
1616 | ||
20b69855 SC |
1617 | // ---------------------------------------------------------------------------- |
1618 | // wxBitmapHandler | |
1619 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 1620 | |
452418c4 | 1621 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase) |
e9576ca5 | 1622 | |
20b69855 SC |
1623 | // ---------------------------------------------------------------------------- |
1624 | // Standard Handlers | |
1625 | // ---------------------------------------------------------------------------- | |
e9576ca5 | 1626 | |
2078e2f1 SC |
1627 | #ifndef __LP64__ |
1628 | ||
519cb848 SC |
1629 | class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler |
1630 | { | |
1631 | DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) | |
1cb97a54 | 1632 | |
519cb848 SC |
1633 | public: |
1634 | inline wxPICTResourceHandler() | |
1635 | { | |
3bf2bdfb GD |
1636 | SetName(wxT("Macintosh Pict resource")); |
1637 | SetExtension(wxEmptyString); | |
1638 | SetType(wxBITMAP_TYPE_PICT_RESOURCE); | |
519cb848 SC |
1639 | }; |
1640 | ||
1641 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
1642 | int desiredWidth, int desiredHeight); | |
1643 | }; | |
7fe44dee | 1644 | |
519cb848 SC |
1645 | IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) |
1646 | ||
179e085f | 1647 | |
89954433 VZ |
1648 | bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, |
1649 | const wxString& name, | |
1650 | long WXUNUSED(flags), | |
1651 | int WXUNUSED(desiredWidth), | |
1652 | int WXUNUSED(desiredHeight)) | |
519cb848 | 1653 | { |
179e085f | 1654 | #if wxUSE_METAFILE |
4e9ed364 | 1655 | Str255 theName ; |
427ff662 | 1656 | wxMacStringToPascal( name , theName ) ; |
55e18dbe | 1657 | |
4e9ed364 RR |
1658 | PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; |
1659 | if ( thePict ) | |
1660 | { | |
20b69855 | 1661 | wxMetafile mf ; |
7fe44dee | 1662 | |
6239ee05 | 1663 | mf.SetPICT( thePict ) ; |
20b69855 SC |
1664 | bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ; |
1665 | wxMemoryDC dc ; | |
1666 | dc.SelectObject( *bitmap ) ; | |
1667 | mf.Play( &dc ) ; | |
1668 | dc.SelectObject( wxNullBitmap ) ; | |
1cb97a54 | 1669 | |
902725ee | 1670 | return true ; |
4e9ed364 | 1671 | } |
7fe44dee | 1672 | #endif |
1cb97a54 | 1673 | |
902725ee | 1674 | return false ; |
519cb848 | 1675 | } |
2078e2f1 | 1676 | #endif |
519cb848 | 1677 | |
e9576ca5 SC |
1678 | void wxBitmap::InitStandardHandlers() |
1679 | { | |
2078e2f1 | 1680 | #ifndef __LP64__ |
1cb97a54 | 1681 | AddHandler( new wxPICTResourceHandler ) ; |
2078e2f1 | 1682 | #endif |
1cb97a54 | 1683 | AddHandler( new wxICONResourceHandler ) ; |
e9576ca5 | 1684 | } |
55e18dbe VZ |
1685 | |
1686 | // ---------------------------------------------------------------------------- | |
1687 | // raw bitmap access support | |
1688 | // ---------------------------------------------------------------------------- | |
1689 | ||
89954433 | 1690 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int WXUNUSED(bpp)) |
55e18dbe VZ |
1691 | { |
1692 | if ( !Ok() ) | |
55e18dbe VZ |
1693 | // no bitmap, no data (raw or otherwise) |
1694 | return NULL; | |
55e18dbe | 1695 | |
20b69855 SC |
1696 | data.m_width = GetWidth() ; |
1697 | data.m_height = GetHeight() ; | |
6239ee05 | 1698 | data.m_stride = GetBitmapData()->GetBytesPerRow() ; |
1cb97a54 | 1699 | |
6945b587 | 1700 | return BeginRawAccess() ; |
55e18dbe VZ |
1701 | } |
1702 | ||
89954433 | 1703 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase)) |
55e18dbe | 1704 | { |
6945b587 | 1705 | EndRawAccess() ; |
55e18dbe VZ |
1706 | } |
1707 | ||
1708 | void wxBitmap::UseAlpha() | |
1709 | { | |
1cb97a54 DS |
1710 | // remember that we are using alpha channel: |
1711 | // we'll need to create a proper mask in UngetRawData() | |
20b69855 | 1712 | M_BITMAPDATA->UseAlpha( true ); |
55e18dbe | 1713 | } |