]>
Commit | Line | Data |
---|---|---|
0becd470 | 1 | //////////////////////////////////////////////////////////////////////////// |
2bda0e17 KB |
2 | // Name: bitmap.cpp |
3 | // Purpose: wxBitmap | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
10fcf31a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
10fcf31a | 21 | #pragma implementation "bitmap.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
10fcf31a | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
10fcf31a VZ |
32 | #include <stdio.h> |
33 | ||
34 | #include "wx/list.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/app.h" | |
37 | #include "wx/palette.h" | |
38 | #include "wx/dcmemory.h" | |
39 | #include "wx/bitmap.h" | |
40 | #include "wx/icon.h" | |
2bda0e17 KB |
41 | #endif |
42 | ||
43 | #include "wx/msw/private.h" | |
1d792928 VZ |
44 | #include "wx/log.h" |
45 | ||
4676948b | 46 | #if wxUSE_WXDIB |
2bda0e17 | 47 | #include "wx/msw/dib.h" |
04ef50df JS |
48 | #endif |
49 | ||
b75dd496 | 50 | #include "wx/image.h" |
66e23ad2 | 51 | #include "wx/xpmdecod.h" |
2bda0e17 | 52 | |
1e3c12d7 | 53 | #ifdef wxHAVE_RAW_BITMAP |
2cf45d69 | 54 | #include "wx/rawbmp.h" |
1e3c12d7 | 55 | #endif |
2cf45d69 | 56 | |
3c1a88d8 VZ |
57 | // missing from mingw32 header |
58 | #ifndef CLR_INVALID | |
59 | #define CLR_INVALID ((COLORREF)-1) | |
60 | #endif // no CLR_INVALID | |
61 | ||
8bbbae21 VZ |
62 | // ---------------------------------------------------------------------------- |
63 | // Bitmap data | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData | |
67 | { | |
68 | public: | |
69 | wxBitmapRefData(); | |
7ff64980 | 70 | wxBitmapRefData(const wxBitmapRefData& data); |
8bbbae21 VZ |
71 | virtual ~wxBitmapRefData() { Free(); } |
72 | ||
73 | virtual void Free(); | |
74 | ||
75 | // set the mask object to use as the mask, we take ownership of it | |
76 | void SetMask(wxMask *mask) | |
77 | { | |
78 | delete m_bitmapMask; | |
79 | m_bitmapMask = mask; | |
80 | } | |
81 | ||
82 | // set the HBITMAP to use as the mask | |
83 | void SetMask(HBITMAP hbmpMask) | |
84 | { | |
85 | SetMask(new wxMask((WXHBITMAP)hbmpMask)); | |
86 | } | |
87 | ||
88 | // return the mask | |
89 | wxMask *GetMask() const { return m_bitmapMask; } | |
90 | ||
91 | public: | |
8bbbae21 VZ |
92 | #if wxUSE_PALETTE |
93 | wxPalette m_bitmapPalette; | |
94 | #endif // wxUSE_PALETTE | |
95 | ||
96 | // MSW-specific | |
97 | // ------------ | |
98 | ||
acf8e3d2 | 99 | #ifdef __WXDEBUG__ |
8bbbae21 VZ |
100 | // this field is solely for error checking: we detect selecting a bitmap |
101 | // into more than one DC at once or deleting a bitmap still selected into a | |
102 | // DC (both are serious programming errors under Windows) | |
103 | wxDC *m_selectedInto; | |
acf8e3d2 | 104 | #endif // __WXDEBUG__ |
8bbbae21 | 105 | |
4676948b | 106 | #if wxUSE_WXDIB |
fa275e86 VZ |
107 | // when GetRawData() is called for a DDB we need to convert it to a DIB |
108 | // first to be able to provide direct access to it and we cache that DIB | |
109 | // here and convert it back to DDB when UngetRawData() is called | |
110 | wxDIB *m_dib; | |
4676948b | 111 | #endif |
fa275e86 | 112 | |
acf8e3d2 VZ |
113 | // true if we have alpha transparency info and can be drawn using |
114 | // AlphaBlend() | |
115 | bool m_hasAlpha; | |
8bbbae21 | 116 | |
fa275e86 VZ |
117 | // true if our HBITMAP is a DIB section, false if it is a DDB |
118 | bool m_isDIB; | |
119 | ||
8bbbae21 VZ |
120 | private: |
121 | // optional mask for transparent drawing | |
122 | wxMask *m_bitmapMask; | |
123 | ||
7ff64980 VZ |
124 | |
125 | // not implemented | |
126 | wxBitmapRefData& operator=(const wxBitmapRefData&); | |
8bbbae21 VZ |
127 | }; |
128 | ||
10fcf31a VZ |
129 | // ---------------------------------------------------------------------------- |
130 | // macros | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
4b7f2165 VZ |
133 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) |
134 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
6d167489 | 135 | |
4b7f2165 | 136 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) |
2bda0e17 | 137 | |
10fcf31a VZ |
138 | // ============================================================================ |
139 | // implementation | |
140 | // ============================================================================ | |
141 | ||
2cf45d69 VZ |
142 | // ---------------------------------------------------------------------------- |
143 | // helper functions | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | // decide whether we should create a DIB or a DDB for the given parameters | |
be69f971 VZ |
147 | // |
148 | // NB: we always use DIBs under Windows CE as this is much simpler (even if | |
149 | // also less efficient...) and we obviously can't use them if there is no | |
150 | // DIB support compiled in at all | |
151 | #ifdef __WXWINCE__ | |
152 | static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; } | |
153 | ||
154 | #define ALWAYS_USE_DIB | |
155 | #elif !wxUSE_WXDIB | |
156 | // no sense in defining wxShouldCreateDIB() as we can't compile code | |
157 | // executed if it is true, so we have to use #if's anyhow | |
158 | #define NEVER_USE_DIB | |
159 | #else // wxUSE_WXDIB && !__WXWINCE__ | |
160 | static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc) | |
161 | { | |
162 | // here is the logic: | |
163 | // | |
164 | // (a) if hdc is specified, the caller explicitly wants DDB | |
165 | // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp | |
166 | // or less DIBs anyhow) | |
167 | // (c) finally, create DIBs under Win9x even if the depth hasn't been | |
168 | // explicitly specified but the current display depth is 24 or | |
169 | // more and the image is "big", i.e. > 16Mb which is the | |
170 | // theoretical limit for DDBs under Win9x | |
171 | // | |
172 | // consequences (all of which seem to make sense): | |
173 | // | |
174 | // (i) by default, DDBs are created (depth == -1 usually) | |
175 | // (ii) DIBs can be created by explicitly specifying the depth | |
176 | // (iii) using a DC always forces creating a DDB | |
177 | return !hdc && | |
178 | (d >= 24 || | |
179 | (d == -1 && | |
180 | wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024)); | |
181 | } | |
182 | ||
183 | #define SOMETIMES_USE_DIB | |
184 | #endif // different DIB usage scenarious | |
2cf45d69 | 185 | |
10fcf31a VZ |
186 | // ---------------------------------------------------------------------------- |
187 | // wxBitmapRefData | |
188 | // ---------------------------------------------------------------------------- | |
189 | ||
190 | wxBitmapRefData::wxBitmapRefData() | |
2bda0e17 | 191 | { |
3ca22d5e | 192 | #ifdef __WXDEBUG__ |
6d167489 | 193 | m_selectedInto = NULL; |
3ca22d5e | 194 | #endif |
6d167489 | 195 | m_bitmapMask = NULL; |
fa275e86 | 196 | |
340196c0 | 197 | m_hBitmap = (WXHBITMAP) NULL; |
4676948b | 198 | #if wxUSE_WXDIB |
fa275e86 | 199 | m_dib = NULL; |
4676948b | 200 | #endif |
fa275e86 VZ |
201 | |
202 | m_isDIB = | |
acf8e3d2 | 203 | m_hasAlpha = FALSE; |
2bda0e17 KB |
204 | } |
205 | ||
7ff64980 VZ |
206 | wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) |
207 | : wxGDIImageRefData(data) | |
208 | { | |
209 | #ifdef __WXDEBUG__ | |
210 | m_selectedInto = NULL; | |
211 | #endif | |
212 | ||
213 | // can't copy the mask as the other bitmap destroys it | |
214 | m_bitmapMask = NULL; | |
215 | ||
216 | wxASSERT_MSG( !data.m_isDIB, | |
217 | _T("can't copy bitmap locked for raw access!") ); | |
218 | m_isDIB = FALSE; | |
219 | ||
220 | m_hasAlpha = data.m_hasAlpha; | |
221 | } | |
222 | ||
6d167489 | 223 | void wxBitmapRefData::Free() |
2bda0e17 | 224 | { |
d59ceba5 VZ |
225 | wxASSERT_MSG( !m_selectedInto, |
226 | wxT("deleting bitmap still selected into wxMemoryDC") ); | |
2bda0e17 | 227 | |
4676948b | 228 | #if wxUSE_WXDIB |
fa275e86 | 229 | wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") ); |
4676948b | 230 | #endif |
fa275e86 | 231 | |
d59ceba5 | 232 | if ( m_hBitmap) |
6d167489 VZ |
233 | { |
234 | if ( !::DeleteObject((HBITMAP)m_hBitmap) ) | |
235 | { | |
f6bcfd97 | 236 | wxLogLastError(wxT("DeleteObject(hbitmap)")); |
6d167489 VZ |
237 | } |
238 | } | |
e7003166 | 239 | |
6d167489 VZ |
240 | delete m_bitmapMask; |
241 | m_bitmapMask = NULL; | |
2bda0e17 KB |
242 | } |
243 | ||
10fcf31a | 244 | // ---------------------------------------------------------------------------- |
6d167489 | 245 | // wxBitmap creation |
10fcf31a VZ |
246 | // ---------------------------------------------------------------------------- |
247 | ||
4fe5383d VZ |
248 | // this function should be called from all wxBitmap ctors |
249 | void wxBitmap::Init() | |
2bda0e17 | 250 | { |
4fe5383d | 251 | // m_refData = NULL; done in the base class ctor |
4fe5383d VZ |
252 | } |
253 | ||
8bbbae21 VZ |
254 | wxGDIImageRefData *wxBitmap::CreateData() const |
255 | { | |
256 | return new wxBitmapRefData; | |
257 | } | |
258 | ||
7ff64980 VZ |
259 | wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const |
260 | { | |
261 | const wxBitmapRefData * | |
262 | data = wx_static_cast(const wxBitmapRefData *, dataOrig); | |
263 | if ( !data ) | |
264 | return NULL; | |
265 | ||
266 | wxBitmap *self = wx_const_cast(wxBitmap *, this); | |
267 | self->m_refData = new wxBitmapRefData(*data); | |
268 | ||
269 | #if wxUSE_WXDIB | |
270 | // copy the other bitmap | |
271 | if ( data->m_hBitmap ) | |
272 | { | |
273 | wxDIB dib((HBITMAP)(data->m_hBitmap)); | |
274 | self->CopyFromDIB(dib); | |
275 | } | |
276 | #endif // wxUSE_WXDIB | |
277 | ||
278 | return m_refData; | |
279 | } | |
280 | ||
6d167489 VZ |
281 | #ifdef __WIN32__ |
282 | ||
283 | bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) | |
284 | { | |
4676948b | 285 | #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
6d167489 VZ |
286 | // it may be either HICON or HCURSOR |
287 | HICON hicon = (HICON)icon.GetHandle(); | |
288 | ||
289 | ICONINFO iconInfo; | |
290 | if ( !::GetIconInfo(hicon, &iconInfo) ) | |
291 | { | |
f6bcfd97 | 292 | wxLogLastError(wxT("GetIconInfo")); |
6d167489 | 293 | |
7010702f | 294 | return false; |
6d167489 VZ |
295 | } |
296 | ||
297 | wxBitmapRefData *refData = new wxBitmapRefData; | |
298 | m_refData = refData; | |
299 | ||
d9c8e68e VZ |
300 | int w = icon.GetWidth(), |
301 | h = icon.GetHeight(); | |
302 | ||
303 | refData->m_width = w; | |
304 | refData->m_height = h; | |
6d167489 VZ |
305 | refData->m_depth = wxDisplayDepth(); |
306 | ||
307 | refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor; | |
d9c8e68e VZ |
308 | |
309 | // the mask returned by GetIconInfo() is inversed compared to the usual | |
310 | // wxWin convention | |
8bbbae21 | 311 | refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h)); |
6d167489 | 312 | |
68f36a2c VZ |
313 | |
314 | // delete the old one now as we don't need it any more | |
315 | ::DeleteObject(iconInfo.hbmMask); | |
316 | ||
7010702f | 317 | return true; |
04ef50df | 318 | #else |
7010702f WS |
319 | wxUnusedVar(icon); |
320 | return false; | |
04ef50df | 321 | #endif |
6d167489 VZ |
322 | } |
323 | ||
324 | #endif // Win32 | |
325 | ||
326 | bool wxBitmap::CopyFromCursor(const wxCursor& cursor) | |
4fe5383d VZ |
327 | { |
328 | UnRef(); | |
07cf98cb | 329 | |
6d167489 | 330 | if ( !cursor.Ok() ) |
4fe5383d | 331 | return FALSE; |
07cf98cb | 332 | |
6d167489 VZ |
333 | return CopyFromIconOrCursor(cursor); |
334 | } | |
335 | ||
336 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
337 | { | |
338 | UnRef(); | |
07cf98cb | 339 | |
6d167489 VZ |
340 | if ( !icon.Ok() ) |
341 | return FALSE; | |
4fe5383d | 342 | |
6d167489 | 343 | return CopyFromIconOrCursor(icon); |
10fcf31a VZ |
344 | } |
345 | ||
be69f971 VZ |
346 | #ifndef NEVER_USE_DIB |
347 | ||
b0ea5d96 VZ |
348 | bool wxBitmap::CopyFromDIB(const wxDIB& dib) |
349 | { | |
350 | wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") ); | |
351 | ||
be69f971 | 352 | #ifdef SOMETIMES_USE_DIB |
b0ea5d96 VZ |
353 | HBITMAP hbitmap = dib.CreateDDB(); |
354 | if ( !hbitmap ) | |
355 | return FALSE; | |
be69f971 VZ |
356 | #else // ALWAYS_USE_DIB |
357 | HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast | |
358 | #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB | |
b0ea5d96 VZ |
359 | |
360 | UnRef(); | |
361 | ||
362 | wxBitmapRefData *refData = new wxBitmapRefData; | |
363 | m_refData = refData; | |
364 | ||
365 | refData->m_width = dib.GetWidth(); | |
366 | refData->m_height = dib.GetHeight(); | |
367 | refData->m_depth = dib.GetDepth(); | |
368 | ||
369 | refData->m_hBitmap = (WXHBITMAP)hbitmap; | |
370 | ||
371 | #if wxUSE_PALETTE | |
372 | wxPalette *palette = dib.CreatePalette(); | |
373 | if ( palette ) | |
374 | { | |
375 | refData->m_bitmapPalette = *palette; | |
376 | } | |
377 | ||
378 | delete palette; | |
379 | #endif // wxUSE_PALETTE | |
380 | ||
381 | return TRUE; | |
382 | } | |
be69f971 VZ |
383 | |
384 | #endif // NEVER_USE_DIB | |
b0ea5d96 | 385 | |
10fcf31a | 386 | wxBitmap::~wxBitmap() |
2bda0e17 | 387 | { |
2bda0e17 KB |
388 | } |
389 | ||
5bd3a2da | 390 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) |
2bda0e17 | 391 | { |
4fe5383d | 392 | Init(); |
2bda0e17 | 393 | |
04ef50df | 394 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
395 | wxBitmapRefData *refData = new wxBitmapRefData; |
396 | m_refData = refData; | |
2bda0e17 | 397 | |
5bd3a2da VZ |
398 | refData->m_width = width; |
399 | refData->m_height = height; | |
400 | refData->m_depth = depth; | |
2bda0e17 | 401 | |
5bd3a2da VZ |
402 | char *data; |
403 | if ( depth == 1 ) | |
404 | { | |
405 | // we assume that it is in XBM format which is not quite the same as | |
406 | // the format CreateBitmap() wants because the order of bytes in the | |
407 | // line is inversed! | |
4d24ece7 VZ |
408 | const size_t bytesPerLine = (width + 7) / 8; |
409 | const size_t padding = bytesPerLine % 2; | |
410 | const size_t len = height * ( padding + bytesPerLine ); | |
5bd3a2da VZ |
411 | data = (char *)malloc(len); |
412 | const char *src = bits; | |
413 | char *dst = data; | |
414 | ||
415 | for ( int rows = 0; rows < height; rows++ ) | |
416 | { | |
0765adca | 417 | for ( size_t cols = 0; cols < bytesPerLine; cols++ ) |
5bd3a2da | 418 | { |
0765adca VZ |
419 | unsigned char val = *src++; |
420 | unsigned char reversed = 0; | |
421 | ||
422 | for ( int bits = 0; bits < 8; bits++) | |
423 | { | |
424 | reversed <<= 1; | |
425 | reversed |= (val & 0x01); | |
426 | val >>= 1; | |
427 | } | |
428 | *dst++ = reversed; | |
5bd3a2da VZ |
429 | } |
430 | ||
431 | if ( padding ) | |
432 | *dst++ = 0; | |
5bd3a2da VZ |
433 | } |
434 | } | |
435 | else | |
436 | { | |
437 | // bits should already be in Windows standard format | |
438 | data = (char *)bits; // const_cast is harmless | |
439 | } | |
440 | ||
441 | HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data); | |
6d167489 VZ |
442 | if ( !hbmp ) |
443 | { | |
f6bcfd97 | 444 | wxLogLastError(wxT("CreateBitmap")); |
6d167489 | 445 | } |
2bda0e17 | 446 | |
5bd3a2da VZ |
447 | if ( data != bits ) |
448 | { | |
449 | free(data); | |
450 | } | |
451 | ||
6d167489 | 452 | SetHBITMAP((WXHBITMAP)hbmp); |
04ef50df | 453 | #endif |
2bda0e17 KB |
454 | } |
455 | ||
2fd284a4 | 456 | // Create from XPM data |
1904aa72 | 457 | #if wxUSE_IMAGE && wxUSE_XPM |
4b7f2165 | 458 | bool wxBitmap::CreateFromXpm(const char **data) |
1904aa72 DS |
459 | #else |
460 | bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data)) | |
461 | #endif | |
2fd284a4 | 462 | { |
66e23ad2 | 463 | #if wxUSE_IMAGE && wxUSE_XPM |
4fe5383d VZ |
464 | Init(); |
465 | ||
66e23ad2 | 466 | wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") ) |
4d24ece7 | 467 | |
66e23ad2 VS |
468 | wxXPMDecoder decoder; |
469 | wxImage img = decoder.ReadData(data); | |
470 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) | |
4d24ece7 | 471 | |
c59b4b01 | 472 | *this = wxBitmap(img); |
66e23ad2 VS |
473 | return TRUE; |
474 | #else | |
4d24ece7 | 475 | return FALSE; |
66e23ad2 | 476 | #endif |
2fd284a4 JS |
477 | } |
478 | ||
debe6624 | 479 | wxBitmap::wxBitmap(int w, int h, int d) |
2bda0e17 | 480 | { |
4fe5383d | 481 | Init(); |
2bda0e17 | 482 | |
4fe5383d | 483 | (void)Create(w, h, d); |
2bda0e17 KB |
484 | } |
485 | ||
98ea6b7d VZ |
486 | wxBitmap::wxBitmap(int w, int h, const wxDC& dc) |
487 | { | |
488 | Init(); | |
489 | ||
490 | (void)Create(w, h, dc); | |
491 | } | |
492 | ||
debe6624 | 493 | wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) |
2bda0e17 | 494 | { |
4fe5383d | 495 | Init(); |
2bda0e17 | 496 | |
6d167489 | 497 | (void)Create(data, type, width, height, depth); |
2bda0e17 KB |
498 | } |
499 | ||
2aeec9ec | 500 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) |
2bda0e17 | 501 | { |
4fe5383d | 502 | Init(); |
2bda0e17 | 503 | |
4fe5383d | 504 | LoadFile(filename, (int)type); |
2bda0e17 KB |
505 | } |
506 | ||
2cf45d69 VZ |
507 | bool wxBitmap::Create(int width, int height, int depth) |
508 | { | |
509 | return DoCreate(width, height, depth, 0); | |
510 | } | |
511 | ||
512 | bool wxBitmap::Create(int width, int height, const wxDC& dc) | |
513 | { | |
514 | wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") ); | |
515 | ||
516 | return DoCreate(width, height, -1, dc.GetHDC()); | |
517 | } | |
518 | ||
519 | bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) | |
2bda0e17 | 520 | { |
6d167489 VZ |
521 | UnRef(); |
522 | ||
523 | m_refData = new wxBitmapRefData; | |
524 | ||
2cf45d69 VZ |
525 | GetBitmapData()->m_width = w; |
526 | GetBitmapData()->m_height = h; | |
527 | ||
be69f971 | 528 | HBITMAP hbmp wxDUMMY_INITIALIZE(0); |
2cf45d69 | 529 | |
be69f971 | 530 | #ifndef NEVER_USE_DIB |
2cf45d69 | 531 | if ( wxShouldCreateDIB(w, h, d, hdc) ) |
6d167489 | 532 | { |
2cf45d69 VZ |
533 | if ( d == -1 ) |
534 | { | |
535 | // create DIBs without alpha channel by default | |
536 | d = 24; | |
537 | } | |
538 | ||
539 | wxDIB dib(w, h, d); | |
540 | if ( !dib.IsOk() ) | |
0becd470 | 541 | return FALSE; |
2cf45d69 VZ |
542 | |
543 | // don't delete the DIB section in dib object dtor | |
544 | hbmp = dib.Detach(); | |
545 | ||
fa275e86 | 546 | GetBitmapData()->m_isDIB = TRUE; |
2cf45d69 | 547 | GetBitmapData()->m_depth = d; |
6d167489 | 548 | } |
2cf45d69 | 549 | else // create a DDB |
be69f971 | 550 | #endif // NEVER_USE_DIB |
6d167489 | 551 | { |
be69f971 | 552 | #ifndef ALWAYS_USE_DIB |
0becd470 VZ |
553 | #ifndef __WXMICROWIN__ |
554 | if ( d > 0 ) | |
6d167489 | 555 | { |
0becd470 VZ |
556 | hbmp = ::CreateBitmap(w, h, 1, d, NULL); |
557 | if ( !hbmp ) | |
558 | { | |
559 | wxLogLastError(wxT("CreateBitmap")); | |
560 | } | |
be69f971 VZ |
561 | |
562 | GetBitmapData()->m_depth = d; | |
6d167489 | 563 | } |
be69f971 | 564 | else // d == 0, create bitmap compatible with the screen |
0becd470 VZ |
565 | #endif // !__WXMICROWIN__ |
566 | { | |
567 | ScreenHDC dc; | |
568 | hbmp = ::CreateCompatibleBitmap(dc, w, h); | |
569 | if ( !hbmp ) | |
570 | { | |
571 | wxLogLastError(wxT("CreateCompatibleBitmap")); | |
572 | } | |
6d167489 | 573 | |
0becd470 VZ |
574 | GetBitmapData()->m_depth = wxDisplayDepth(); |
575 | } | |
be69f971 | 576 | #endif // !ALWAYS_USE_DIB |
2cf45d69 | 577 | } |
2bda0e17 | 578 | |
2cf45d69 | 579 | SetHBITMAP((WXHBITMAP)hbmp); |
2bda0e17 | 580 | |
6d167489 | 581 | return Ok(); |
2bda0e17 KB |
582 | } |
583 | ||
acf8e3d2 | 584 | #if wxUSE_IMAGE |
0becd470 | 585 | |
6d51f220 | 586 | // ---------------------------------------------------------------------------- |
acf8e3d2 | 587 | // wxImage to/from conversions for Microwin |
6d51f220 VZ |
588 | // ---------------------------------------------------------------------------- |
589 | ||
acf8e3d2 VZ |
590 | // Microwin versions are so different from normal ones that it really doesn't |
591 | // make sense to use #ifdefs inside the function bodies | |
592 | #ifdef __WXMICROWIN__ | |
6d51f220 | 593 | |
2cf45d69 | 594 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) |
fec19ea9 | 595 | { |
acf8e3d2 VZ |
596 | // Set this to 1 to experiment with mask code, |
597 | // which currently doesn't work | |
598 | #define USE_MASKS 0 | |
62e1ba75 | 599 | |
54a96d02 | 600 | m_refData = new wxBitmapRefData(); |
e640f823 JS |
601 | |
602 | // Initial attempt at a simple-minded implementation. | |
603 | // The bitmap will always be created at the screen depth, | |
604 | // so the 'depth' argument is ignored. | |
8e9ff815 | 605 | |
e640f823 JS |
606 | HDC hScreenDC = ::GetDC(NULL); |
607 | int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL); | |
608 | ||
609 | HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight()); | |
62e1ba75 JS |
610 | HBITMAP hMaskBitmap = NULL; |
611 | HBITMAP hOldMaskBitmap = NULL; | |
612 | HDC hMaskDC = NULL; | |
613 | unsigned char maskR = 0; | |
614 | unsigned char maskG = 0; | |
615 | unsigned char maskB = 0; | |
616 | ||
54a96d02 | 617 | // printf("Created bitmap %d\n", (int) hBitmap); |
e640f823 JS |
618 | if (hBitmap == NULL) |
619 | { | |
620 | ::ReleaseDC(NULL, hScreenDC); | |
8e9ff815 | 621 | return FALSE; |
e640f823 JS |
622 | } |
623 | HDC hMemDC = ::CreateCompatibleDC(hScreenDC); | |
e640f823 JS |
624 | |
625 | HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); | |
62e1ba75 JS |
626 | ::ReleaseDC(NULL, hScreenDC); |
627 | ||
628 | // created an mono-bitmap for the possible mask | |
629 | bool hasMask = image.HasMask(); | |
630 | ||
631 | if ( hasMask ) | |
632 | { | |
633 | #if USE_MASKS | |
634 | // FIXME: we should be able to pass bpp = 1, but | |
635 | // GdBlit can't handle a different depth | |
636 | #if 0 | |
637 | hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL ); | |
638 | #else | |
639 | hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight()); | |
640 | #endif | |
641 | maskR = image.GetMaskRed(); | |
642 | maskG = image.GetMaskGreen(); | |
643 | maskB = image.GetMaskBlue(); | |
644 | ||
645 | if (!hMaskBitmap) | |
646 | { | |
647 | hasMask = FALSE; | |
8e9ff815 | 648 | } |
62e1ba75 JS |
649 | else |
650 | { | |
651 | hScreenDC = ::GetDC(NULL); | |
652 | hMaskDC = ::CreateCompatibleDC(hScreenDC); | |
653 | ::ReleaseDC(NULL, hScreenDC); | |
654 | ||
655 | hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap); | |
8e9ff815 | 656 | } |
62e1ba75 JS |
657 | #else |
658 | hasMask = FALSE; | |
659 | #endif | |
660 | } | |
e640f823 JS |
661 | |
662 | int i, j; | |
663 | for (i = 0; i < image.GetWidth(); i++) | |
664 | { | |
8e9ff815 VZ |
665 | for (j = 0; j < image.GetHeight(); j++) |
666 | { | |
667 | unsigned char red = image.GetRed(i, j); | |
668 | unsigned char green = image.GetGreen(i, j); | |
669 | unsigned char blue = image.GetBlue(i, j); | |
e640f823 | 670 | |
8e9ff815 | 671 | ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue)); |
62e1ba75 JS |
672 | |
673 | if (hasMask) | |
674 | { | |
675 | // scan the bitmap for the transparent colour and set the corresponding | |
676 | // pixels in the mask to BLACK and the rest to WHITE | |
677 | if (maskR == red && maskG == green && maskB == blue) | |
678 | ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0)); | |
679 | else | |
680 | ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255)); | |
8e9ff815 VZ |
681 | } |
682 | } | |
e640f823 JS |
683 | } |
684 | ||
685 | ::SelectObject(hMemDC, hOldBitmap); | |
686 | ::DeleteDC(hMemDC); | |
62e1ba75 JS |
687 | if (hasMask) |
688 | { | |
689 | ::SelectObject(hMaskDC, hOldMaskBitmap); | |
8e9ff815 | 690 | ::DeleteDC(hMaskDC); |
62e1ba75 | 691 | |
8bbbae21 | 692 | ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap); |
62e1ba75 | 693 | } |
8e9ff815 | 694 | |
e640f823 JS |
695 | SetWidth(image.GetWidth()); |
696 | SetHeight(image.GetHeight()); | |
697 | SetDepth(screenDepth); | |
698 | SetHBITMAP( (WXHBITMAP) hBitmap ); | |
8e9ff815 | 699 | |
e640f823 JS |
700 | #if wxUSE_PALETTE |
701 | // Copy the palette from the source image | |
702 | SetPalette(image.GetPalette()); | |
703 | #endif // wxUSE_PALETTE | |
704 | ||
705 | return TRUE; | |
fec19ea9 VS |
706 | } |
707 | ||
708 | wxImage wxBitmap::ConvertToImage() const | |
709 | { | |
e640f823 JS |
710 | // Initial attempt at a simple-minded implementation. |
711 | // The bitmap will always be created at the screen depth, | |
712 | // so the 'depth' argument is ignored. | |
713 | // TODO: transparency (create a mask image) | |
714 | ||
715 | if (!Ok()) | |
716 | { | |
717 | wxFAIL_MSG( wxT("bitmap is invalid") ); | |
8e9ff815 | 718 | return wxNullImage; |
e640f823 JS |
719 | } |
720 | ||
721 | wxImage image; | |
722 | ||
723 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
724 | ||
725 | // create an wxImage object | |
726 | int width = GetWidth(); | |
727 | int height = GetHeight(); | |
728 | image.Create( width, height ); | |
729 | unsigned char *data = image.GetData(); | |
730 | if( !data ) | |
731 | { | |
732 | wxFAIL_MSG( wxT("could not allocate data for image") ); | |
733 | return wxNullImage; | |
734 | } | |
8e9ff815 | 735 | |
e640f823 JS |
736 | HDC hScreenDC = ::GetDC(NULL); |
737 | ||
738 | HDC hMemDC = ::CreateCompatibleDC(hScreenDC); | |
739 | ::ReleaseDC(NULL, hScreenDC); | |
740 | ||
741 | HBITMAP hBitmap = (HBITMAP) GetHBITMAP(); | |
8e9ff815 | 742 | |
e640f823 JS |
743 | HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); |
744 | ||
745 | int i, j; | |
746 | for (i = 0; i < GetWidth(); i++) | |
747 | { | |
8e9ff815 VZ |
748 | for (j = 0; j < GetHeight(); j++) |
749 | { | |
750 | COLORREF color = ::GetPixel(hMemDC, i, j); | |
751 | unsigned char red = GetRValue(color); | |
752 | unsigned char green = GetGValue(color); | |
753 | unsigned char blue = GetBValue(color); | |
754 | ||
755 | image.SetRGB(i, j, red, green, blue); | |
756 | } | |
e640f823 JS |
757 | } |
758 | ||
759 | ::SelectObject(hMemDC, hOldBitmap); | |
760 | ::DeleteDC(hMemDC); | |
8e9ff815 | 761 | |
e640f823 JS |
762 | #if wxUSE_PALETTE |
763 | // Copy the palette from the source image | |
764 | if (GetPalette()) | |
765 | image.SetPalette(* GetPalette()); | |
766 | #endif // wxUSE_PALETTE | |
767 | ||
768 | return image; | |
acf8e3d2 VZ |
769 | } |
770 | ||
771 | #endif // __WXMICROWIN__ | |
772 | ||
773 | // ---------------------------------------------------------------------------- | |
774 | // wxImage to/from conversions | |
775 | // ---------------------------------------------------------------------------- | |
776 | ||
be69f971 VZ |
777 | #if wxUSE_WXDIB |
778 | ||
2cf45d69 VZ |
779 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
780 | { | |
781 | return CreateFromImage(image, depth, 0); | |
782 | } | |
783 | ||
784 | bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) | |
785 | { | |
786 | wxCHECK_MSG( dc.Ok(), FALSE, | |
787 | _T("invalid HDC in wxBitmap::CreateFromImage()") ); | |
788 | ||
789 | return CreateFromImage(image, -1, dc.GetHDC()); | |
790 | } | |
791 | ||
be69f971 | 792 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) |
acf8e3d2 VZ |
793 | { |
794 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); | |
795 | ||
796 | UnRef(); | |
797 | ||
798 | // first convert the image to DIB | |
799 | const int h = image.GetHeight(); | |
800 | const int w = image.GetWidth(); | |
801 | ||
802 | wxDIB dib(image); | |
803 | if ( !dib.IsOk() ) | |
804 | return FALSE; | |
805 | ||
806 | ||
807 | // store the bitmap parameters | |
808 | wxBitmapRefData *refData = new wxBitmapRefData; | |
809 | refData->m_width = w; | |
810 | refData->m_height = h; | |
acf8e3d2 VZ |
811 | refData->m_hasAlpha = image.HasAlpha(); |
812 | ||
813 | m_refData = refData; | |
814 | ||
815 | ||
816 | // next either store DIB as is or create a DDB from it | |
be69f971 | 817 | HBITMAP hbitmap wxDUMMY_INITIALIZE(0); |
acf8e3d2 | 818 | |
2cf45d69 | 819 | // are we going to use DIB? |
4b8ab2f9 VZ |
820 | // |
821 | // NB: DDBs don't support alpha so if we have alpha channel we must use DIB | |
822 | if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) ) | |
2cf45d69 VZ |
823 | { |
824 | // don't delete the DIB section in dib object dtor | |
825 | hbitmap = dib.Detach(); | |
826 | ||
fa275e86 | 827 | refData->m_isDIB = TRUE; |
2cf45d69 VZ |
828 | refData->m_depth = dib.GetDepth(); |
829 | } | |
be69f971 | 830 | #ifndef ALWAYS_USE_DIB |
2cf45d69 | 831 | else // we need to convert DIB to DDB |
acf8e3d2 | 832 | { |
b0ea5d96 | 833 | hbitmap = dib.CreateDDB((HDC)hdc); |
2cf45d69 | 834 | |
3cfd077f | 835 | refData->m_depth = depth == -1 ? dib.GetDepth() : depth; |
acf8e3d2 | 836 | } |
be69f971 | 837 | #endif // !ALWAYS_USE_DIB |
acf8e3d2 VZ |
838 | |
839 | // validate this object | |
840 | SetHBITMAP((WXHBITMAP)hbitmap); | |
841 | ||
acf8e3d2 VZ |
842 | // finally also set the mask if we have one |
843 | if ( image.HasMask() ) | |
844 | { | |
845 | SetMask(new wxMask(*this, wxColour(image.GetMaskRed(), | |
846 | image.GetMaskGreen(), | |
847 | image.GetMaskBlue()))); | |
848 | } | |
849 | ||
850 | return TRUE; | |
851 | } | |
852 | ||
853 | wxImage wxBitmap::ConvertToImage() const | |
854 | { | |
6faac21b | 855 | // convert DDB to DIB |
be69f971 | 856 | wxDIB dib(*this); |
c8688869 | 857 | |
003e6c07 DS |
858 | if ( !dib.IsOk() ) |
859 | { | |
860 | return wxNullImage; | |
861 | } | |
862 | ||
6faac21b | 863 | // and then DIB to our wxImage |
003e6c07 DS |
864 | wxImage image = dib.ConvertToImage(); |
865 | if ( !image.Ok() ) | |
866 | { | |
867 | return wxNullImage; | |
868 | } | |
869 | ||
6faac21b VZ |
870 | // now do the same for the mask, if we have any |
871 | HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL; | |
872 | if ( hbmpMask ) | |
fec19ea9 | 873 | { |
6faac21b VZ |
874 | wxDIB dibMask(hbmpMask); |
875 | if ( dibMask.IsOk() ) | |
003e6c07 | 876 | { |
6faac21b VZ |
877 | // TODO: use wxRawBitmap to iterate over DIB |
878 | ||
879 | // we hard code the mask colour for now but we could also make an | |
880 | // effort (and waste time) to choose a colour not present in the | |
881 | // image already to avoid having to fudge the pixels below -- | |
882 | // whether it's worth to do it is unclear however | |
883 | static const int MASK_RED = 1; | |
884 | static const int MASK_GREEN = 2; | |
885 | static const int MASK_BLUE = 3; | |
886 | static const int MASK_BLUE_REPLACEMENT = 2; | |
887 | ||
888 | const int h = dibMask.GetHeight(); | |
889 | const int w = dibMask.GetWidth(); | |
890 | const int bpp = dibMask.GetDepth(); | |
891 | const int maskBytesPerPixel = bpp >> 3; | |
892 | const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp); | |
893 | unsigned char *data = image.GetData(); | |
894 | ||
895 | // remember that DIBs are stored in bottom to top order | |
896 | unsigned char * | |
897 | maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine); | |
898 | ||
899 | for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine ) | |
003e6c07 | 900 | { |
6faac21b VZ |
901 | // traverse one mask DIB line |
902 | unsigned char *mask = maskLineStart; | |
903 | for ( int x = 0; x < w; x++, mask += maskBytesPerPixel ) | |
003e6c07 | 904 | { |
6faac21b VZ |
905 | // should this pixel be transparent? |
906 | if ( *mask ) | |
003e6c07 | 907 | { |
6faac21b VZ |
908 | // no, check that it isn't transparent by accident |
909 | if ( (data[0] == MASK_RED) && | |
910 | (data[1] == MASK_GREEN) && | |
911 | (data[2] == MASK_BLUE) ) | |
912 | { | |
913 | // we have to fudge the colour a bit to prevent | |
914 | // this pixel from appearing transparent | |
915 | data[2] = MASK_BLUE_REPLACEMENT; | |
916 | } | |
917 | ||
918 | data += 3; | |
919 | } | |
920 | else // yes, transparent pixel | |
921 | { | |
922 | *data++ = MASK_RED; | |
923 | *data++ = MASK_GREEN; | |
924 | *data++ = MASK_BLUE; | |
003e6c07 | 925 | } |
003e6c07 | 926 | } |
003e6c07 | 927 | } |
003e6c07 | 928 | |
6faac21b VZ |
929 | image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE); |
930 | } | |
fec19ea9 | 931 | } |
fec19ea9 VS |
932 | |
933 | return image; | |
934 | } | |
935 | ||
be69f971 VZ |
936 | #endif // wxUSE_WXDIB |
937 | ||
6d51f220 VZ |
938 | #endif // wxUSE_IMAGE |
939 | ||
acf8e3d2 VZ |
940 | // ---------------------------------------------------------------------------- |
941 | // loading and saving bitmaps | |
942 | // ---------------------------------------------------------------------------- | |
943 | ||
debe6624 | 944 | bool wxBitmap::LoadFile(const wxString& filename, long type) |
2bda0e17 | 945 | { |
6d167489 | 946 | UnRef(); |
2bda0e17 | 947 | |
6d167489 | 948 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 949 | |
6d167489 VZ |
950 | if ( handler ) |
951 | { | |
952 | m_refData = new wxBitmapRefData; | |
2bda0e17 | 953 | |
6d167489 VZ |
954 | return handler->LoadFile(this, filename, type, -1, -1); |
955 | } | |
6d51f220 | 956 | #if wxUSE_IMAGE |
be69f971 | 957 | else // no bitmap handler found |
b75dd496 | 958 | { |
6d167489 | 959 | wxImage image; |
6d51f220 VZ |
960 | if ( image.LoadFile( filename, type ) && image.Ok() ) |
961 | { | |
368d59f0 | 962 | *this = wxBitmap(image); |
6d167489 | 963 | |
6d51f220 VZ |
964 | return TRUE; |
965 | } | |
b75dd496 | 966 | } |
6d51f220 VZ |
967 | #endif // wxUSE_IMAGE |
968 | ||
969 | return FALSE; | |
2bda0e17 KB |
970 | } |
971 | ||
debe6624 | 972 | bool wxBitmap::Create(void *data, long type, int width, int height, int depth) |
2bda0e17 | 973 | { |
6d167489 | 974 | UnRef(); |
2bda0e17 | 975 | |
6d167489 | 976 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 977 | |
6d167489 VZ |
978 | if ( !handler ) |
979 | { | |
9b601c24 | 980 | wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type); |
2bda0e17 | 981 | |
6d167489 VZ |
982 | return FALSE; |
983 | } | |
1d792928 | 984 | |
6d167489 | 985 | m_refData = new wxBitmapRefData; |
1d792928 | 986 | |
6d167489 | 987 | return handler->Create(this, data, type, width, height, depth); |
2bda0e17 KB |
988 | } |
989 | ||
d275c7eb VZ |
990 | bool wxBitmap::SaveFile(const wxString& filename, |
991 | int type, | |
992 | const wxPalette *palette) | |
2bda0e17 | 993 | { |
6d167489 | 994 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 995 | |
6d167489 VZ |
996 | if ( handler ) |
997 | { | |
998 | return handler->SaveFile(this, filename, type, palette); | |
999 | } | |
6d51f220 | 1000 | #if wxUSE_IMAGE |
be69f971 | 1001 | else // no bitmap handler found |
6d167489 VZ |
1002 | { |
1003 | // FIXME what about palette? shouldn't we use it? | |
368d59f0 | 1004 | wxImage image = ConvertToImage(); |
6d51f220 VZ |
1005 | if ( image.Ok() ) |
1006 | { | |
1007 | return image.SaveFile(filename, type); | |
1008 | } | |
6d167489 | 1009 | } |
6d51f220 VZ |
1010 | #endif // wxUSE_IMAGE |
1011 | ||
1012 | return FALSE; | |
2bda0e17 KB |
1013 | } |
1014 | ||
4b7f2165 VZ |
1015 | // ---------------------------------------------------------------------------- |
1016 | // sub bitmap extraction | |
1017 | // ---------------------------------------------------------------------------- | |
1018 | ||
1019 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const | |
1020 | { | |
1021 | wxCHECK_MSG( Ok() && | |
1022 | (rect.x >= 0) && (rect.y >= 0) && | |
1023 | (rect.x+rect.width <= GetWidth()) && | |
1024 | (rect.y+rect.height <= GetHeight()), | |
1025 | wxNullBitmap, wxT("Invalid bitmap or bitmap region") ); | |
1026 | ||
acf8e3d2 | 1027 | wxBitmap ret( rect.width, rect.height ); |
4b7f2165 VZ |
1028 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); |
1029 | ||
8e9ff815 | 1030 | #ifndef __WXMICROWIN__ |
acf8e3d2 VZ |
1031 | // TODO: copy alpha channel data if any |
1032 | ||
4b7f2165 | 1033 | // copy bitmap data |
acf8e3d2 VZ |
1034 | MemoryHDC dcSrc, |
1035 | dcDst; | |
8e9ff815 VZ |
1036 | |
1037 | { | |
1038 | SelectInHDC selectSrc(dcSrc, GetHbitmap()), | |
1039 | selectDst(dcDst, GetHbitmapOf(ret)); | |
1040 | ||
acf8e3d2 VZ |
1041 | if ( !selectSrc || !selectDst ) |
1042 | { | |
1043 | wxLogLastError(_T("SelectObjct(hBitmap)")); | |
1044 | } | |
1045 | ||
8e9ff815 VZ |
1046 | if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height, |
1047 | dcSrc, rect.x, rect.y, SRCCOPY) ) | |
1048 | { | |
1049 | wxLogLastError(_T("BitBlt")); | |
1050 | } | |
1051 | } | |
4b7f2165 VZ |
1052 | |
1053 | // copy mask if there is one | |
8e9ff815 | 1054 | if ( GetMask() ) |
4b7f2165 VZ |
1055 | { |
1056 | HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0); | |
1057 | ||
8e9ff815 VZ |
1058 | SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()), |
1059 | selectDst(dcDst, hbmpMask); | |
1060 | ||
1061 | if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height, | |
1062 | dcSrc, rect.x, rect.y, SRCCOPY) ) | |
1063 | { | |
1064 | wxLogLastError(_T("BitBlt")); | |
1065 | } | |
4b7f2165 VZ |
1066 | |
1067 | wxMask *mask = new wxMask((WXHBITMAP) hbmpMask); | |
1068 | ret.SetMask(mask); | |
1069 | } | |
8e9ff815 | 1070 | #endif // !__WXMICROWIN__ |
4b7f2165 VZ |
1071 | |
1072 | return ret; | |
1073 | } | |
1074 | ||
6d167489 VZ |
1075 | // ---------------------------------------------------------------------------- |
1076 | // wxBitmap accessors | |
1077 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1078 | |
adf9e099 | 1079 | #if wxUSE_PALETTE |
8bbbae21 | 1080 | wxPalette* wxBitmap::GetPalette() const |
2bda0e17 | 1081 | { |
8bbbae21 VZ |
1082 | return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette |
1083 | : (wxPalette *) NULL; | |
1084 | } | |
adf9e099 | 1085 | #endif |
8bbbae21 VZ |
1086 | |
1087 | wxMask *wxBitmap::GetMask() const | |
1088 | { | |
1089 | return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL; | |
1090 | } | |
2bda0e17 | 1091 | |
3ca22d5e MB |
1092 | #ifdef __WXDEBUG__ |
1093 | ||
8bbbae21 VZ |
1094 | wxDC *wxBitmap::GetSelectedInto() const |
1095 | { | |
1096 | return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL; | |
2bda0e17 KB |
1097 | } |
1098 | ||
3ca22d5e MB |
1099 | #endif |
1100 | ||
8bbbae21 VZ |
1101 | #if WXWIN_COMPATIBILITY_2_4 |
1102 | ||
1103 | int wxBitmap::GetQuality() const | |
1104 | { | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | #endif // WXWIN_COMPATIBILITY_2_4 | |
1109 | ||
10b41b53 VZ |
1110 | void wxBitmap::UseAlpha() |
1111 | { | |
1112 | if ( GetBitmapData() ) | |
1113 | GetBitmapData()->m_hasAlpha = true; | |
1114 | } | |
1115 | ||
acf8e3d2 VZ |
1116 | bool wxBitmap::HasAlpha() const |
1117 | { | |
1118 | return GetBitmapData() && GetBitmapData()->m_hasAlpha; | |
1119 | } | |
1120 | ||
8bbbae21 VZ |
1121 | // ---------------------------------------------------------------------------- |
1122 | // wxBitmap setters | |
1123 | // ---------------------------------------------------------------------------- | |
1124 | ||
3ca22d5e MB |
1125 | #ifdef __WXDEBUG__ |
1126 | ||
8bbbae21 VZ |
1127 | void wxBitmap::SetSelectedInto(wxDC *dc) |
1128 | { | |
1129 | if ( GetBitmapData() ) | |
1130 | GetBitmapData()->m_selectedInto = dc; | |
2bda0e17 KB |
1131 | } |
1132 | ||
3ca22d5e MB |
1133 | #endif |
1134 | ||
d275c7eb VZ |
1135 | #if wxUSE_PALETTE |
1136 | ||
2bda0e17 KB |
1137 | void wxBitmap::SetPalette(const wxPalette& palette) |
1138 | { | |
7ff64980 | 1139 | AllocExclusive(); |
2bda0e17 | 1140 | |
6d167489 | 1141 | GetBitmapData()->m_bitmapPalette = palette; |
2bda0e17 KB |
1142 | } |
1143 | ||
d275c7eb VZ |
1144 | #endif // wxUSE_PALETTE |
1145 | ||
2bda0e17 KB |
1146 | void wxBitmap::SetMask(wxMask *mask) |
1147 | { | |
7ff64980 | 1148 | AllocExclusive(); |
2bda0e17 | 1149 | |
8bbbae21 VZ |
1150 | GetBitmapData()->SetMask(mask); |
1151 | } | |
1152 | ||
8bbbae21 VZ |
1153 | #if WXWIN_COMPATIBILITY_2_4 |
1154 | ||
1155 | void wxBitmap::SetQuality(int WXUNUSED(quality)) | |
1156 | { | |
2bda0e17 KB |
1157 | } |
1158 | ||
8bbbae21 VZ |
1159 | #endif // WXWIN_COMPATIBILITY_2_4 |
1160 | ||
2cf45d69 VZ |
1161 | // ---------------------------------------------------------------------------- |
1162 | // raw bitmap access support | |
1163 | // ---------------------------------------------------------------------------- | |
1164 | ||
1e3c12d7 | 1165 | #ifdef wxHAVE_RAW_BITMAP |
b9bcaf11 | 1166 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
2cf45d69 | 1167 | { |
4676948b | 1168 | #if wxUSE_WXDIB |
2cf45d69 VZ |
1169 | if ( !Ok() ) |
1170 | { | |
1171 | // no bitmap, no data (raw or otherwise) | |
b9bcaf11 | 1172 | return NULL; |
2cf45d69 VZ |
1173 | } |
1174 | ||
fa275e86 VZ |
1175 | // if we're already a DIB we can access our data directly, but if not we |
1176 | // need to convert this DDB to a DIB section and use it for raw access and | |
1177 | // then convert it back | |
1178 | HBITMAP hDIB; | |
1179 | if ( !GetBitmapData()->m_isDIB ) | |
1180 | { | |
1181 | wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE, | |
1182 | _T("GetRawData() may be called only once") ); | |
1183 | ||
1184 | wxDIB *dib = new wxDIB(*this); | |
1185 | if ( !dib->IsOk() ) | |
1186 | { | |
1187 | delete dib; | |
1188 | ||
b9bcaf11 | 1189 | return NULL; |
fa275e86 VZ |
1190 | } |
1191 | ||
1192 | // we'll free it in UngetRawData() | |
1193 | GetBitmapData()->m_dib = dib; | |
1194 | ||
1195 | hDIB = dib->GetHandle(); | |
1196 | } | |
1197 | else // we're a DIB | |
1198 | { | |
1199 | hDIB = GetHbitmap(); | |
1200 | } | |
1201 | ||
2cf45d69 | 1202 | DIBSECTION ds; |
fa275e86 | 1203 | if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) ) |
2cf45d69 | 1204 | { |
fa275e86 VZ |
1205 | wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") ); |
1206 | ||
b9bcaf11 VZ |
1207 | return NULL; |
1208 | } | |
1209 | ||
1210 | // check that the bitmap is in correct format | |
1211 | if ( ds.dsBm.bmBitsPixel != bpp ) | |
1212 | { | |
1213 | wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") ); | |
1214 | ||
1215 | return NULL; | |
2cf45d69 VZ |
1216 | } |
1217 | ||
b9bcaf11 | 1218 | // ok, store the relevant info in wxPixelDataBase |
2cf45d69 VZ |
1219 | const LONG h = ds.dsBm.bmHeight; |
1220 | ||
b9bcaf11 VZ |
1221 | data.m_width = ds.dsBm.bmWidth; |
1222 | data.m_height = h; | |
2cf45d69 VZ |
1223 | |
1224 | // remember that DIBs are stored in top to bottom order! | |
1225 | const LONG bytesPerRow = ds.dsBm.bmWidthBytes; | |
b9bcaf11 VZ |
1226 | data.m_stride = -bytesPerRow; |
1227 | ||
1228 | char *bits = (char *)ds.dsBm.bmBits; | |
2cf45d69 VZ |
1229 | if ( h > 1 ) |
1230 | { | |
b9bcaf11 | 1231 | bits += (h - 1)*bytesPerRow; |
2cf45d69 VZ |
1232 | } |
1233 | ||
b9bcaf11 | 1234 | return bits; |
4676948b JS |
1235 | #else |
1236 | return NULL; | |
1237 | #endif | |
2cf45d69 VZ |
1238 | } |
1239 | ||
b9bcaf11 | 1240 | void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) |
13c13ecb | 1241 | { |
4676948b | 1242 | #if wxUSE_WXDIB |
fa275e86 VZ |
1243 | if ( !Ok() ) |
1244 | return; | |
1245 | ||
b9bcaf11 VZ |
1246 | // the cast is ugly but we can't do without it and without making this |
1247 | // function template (and hence inline) unfortunately | |
1248 | typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> PixelData; | |
1249 | PixelData& data = (PixelData &)dataBase; | |
1250 | ||
1251 | if ( !data ) | |
7af64aac VZ |
1252 | { |
1253 | // invalid data, don't crash -- but don't assert neither as we're | |
b9bcaf11 | 1254 | // called automatically from wxPixelDataBase dtor and so there is no |
7af64aac VZ |
1255 | // way to prevent this from happening |
1256 | return; | |
1257 | } | |
1258 | ||
b9bcaf11 | 1259 | if ( GetBitmapData()->m_hasAlpha ) |
13c13ecb | 1260 | { |
b9bcaf11 VZ |
1261 | // AlphaBlend() wants to have premultiplied source alpha but |
1262 | // wxRawBitmap API uses normal, not premultiplied, colours, so adjust | |
1263 | // them here now | |
1264 | PixelData::Iterator p(data); | |
106c33bb | 1265 | |
b9bcaf11 VZ |
1266 | const int w = data.GetWidth(); |
1267 | const int h = data.GetHeight(); | |
1268 | ||
1269 | for ( int y = 0; y < h; y++ ) | |
13c13ecb | 1270 | { |
b9bcaf11 | 1271 | PixelData::Iterator rowStart = p; |
106c33bb | 1272 | |
b9bcaf11 VZ |
1273 | for ( int x = 0; x < w; x++ ) |
1274 | { | |
1275 | const unsigned alpha = p.Alpha(); | |
106c33bb | 1276 | |
b9bcaf11 VZ |
1277 | p.Red() = (p.Red() * alpha + 127) / 255; |
1278 | p.Blue() = (p.Blue() * alpha + 127) / 255; | |
1279 | p.Green() = (p.Green() * alpha + 127) / 255; | |
106c33bb | 1280 | |
b9bcaf11 VZ |
1281 | ++p; |
1282 | } | |
fa275e86 | 1283 | |
b9bcaf11 VZ |
1284 | p = rowStart; |
1285 | p.OffsetY(data, 1); | |
1286 | } | |
289307fa | 1287 | } |
b9bcaf11 | 1288 | |
289307fa VZ |
1289 | // if we're a DDB we need to convert DIB back to DDB now to make the |
1290 | // changes made via raw bitmap access effective | |
1291 | if ( !GetBitmapData()->m_isDIB ) | |
1292 | { | |
1293 | wxDIB *dib = GetBitmapData()->m_dib; | |
1294 | GetBitmapData()->m_dib = NULL; | |
fa275e86 | 1295 | |
289307fa | 1296 | // TODO: convert |
fa275e86 | 1297 | |
289307fa | 1298 | delete dib; |
fa275e86 | 1299 | } |
289307fa | 1300 | #endif // wxUSE_WXDIB |
13c13ecb | 1301 | } |
1e3c12d7 | 1302 | #endif // #ifdef wxHAVE_RAW_BITMAP |
13c13ecb | 1303 | |
6d167489 VZ |
1304 | // ---------------------------------------------------------------------------- |
1305 | // wxMask | |
1306 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1307 | |
10fcf31a | 1308 | wxMask::wxMask() |
2bda0e17 KB |
1309 | { |
1310 | m_maskBitmap = 0; | |
1311 | } | |
1312 | ||
1313 | // Construct a mask from a bitmap and a colour indicating | |
1314 | // the transparent area | |
1315 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
1316 | { | |
1317 | m_maskBitmap = 0; | |
6d167489 | 1318 | Create(bitmap, colour); |
2bda0e17 KB |
1319 | } |
1320 | ||
1321 | // Construct a mask from a bitmap and a palette index indicating | |
1322 | // the transparent area | |
debe6624 | 1323 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
1324 | { |
1325 | m_maskBitmap = 0; | |
6d167489 | 1326 | Create(bitmap, paletteIndex); |
2bda0e17 KB |
1327 | } |
1328 | ||
1329 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1330 | wxMask::wxMask(const wxBitmap& bitmap) | |
1331 | { | |
1332 | m_maskBitmap = 0; | |
6d167489 | 1333 | Create(bitmap); |
2bda0e17 KB |
1334 | } |
1335 | ||
10fcf31a | 1336 | wxMask::~wxMask() |
2bda0e17 KB |
1337 | { |
1338 | if ( m_maskBitmap ) | |
1339 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
1340 | } | |
1341 | ||
1342 | // Create a mask from a mono bitmap (copies the bitmap). | |
1343 | bool wxMask::Create(const wxBitmap& bitmap) | |
1344 | { | |
04ef50df | 1345 | #ifndef __WXMICROWIN__ |
a58a12e9 VZ |
1346 | wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE, |
1347 | _T("can't create mask from invalid or not monochrome bitmap") ); | |
1348 | ||
2bda0e17 | 1349 | if ( m_maskBitmap ) |
6d167489 VZ |
1350 | { |
1351 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
1352 | m_maskBitmap = 0; | |
1353 | } | |
a58a12e9 | 1354 | |
6d167489 VZ |
1355 | m_maskBitmap = (WXHBITMAP) CreateBitmap( |
1356 | bitmap.GetWidth(), | |
1357 | bitmap.GetHeight(), | |
1358 | 1, 1, 0 | |
1359 | ); | |
1360 | HDC srcDC = CreateCompatibleDC(0); | |
1361 | SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
1362 | HDC destDC = CreateCompatibleDC(0); | |
1363 | SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
1364 | BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY); | |
1365 | SelectObject(srcDC, 0); | |
1366 | DeleteDC(srcDC); | |
1367 | SelectObject(destDC, 0); | |
1368 | DeleteDC(destDC); | |
1369 | return TRUE; | |
04ef50df JS |
1370 | #else |
1371 | return FALSE; | |
1372 | #endif | |
2bda0e17 KB |
1373 | } |
1374 | ||
1375 | // Create a mask from a bitmap and a palette index indicating | |
1376 | // the transparent area | |
debe6624 | 1377 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
1378 | { |
1379 | if ( m_maskBitmap ) | |
1d792928 | 1380 | { |
6d167489 VZ |
1381 | ::DeleteObject((HBITMAP) m_maskBitmap); |
1382 | m_maskBitmap = 0; | |
1d792928 | 1383 | } |
d275c7eb VZ |
1384 | |
1385 | #if wxUSE_PALETTE | |
6d167489 VZ |
1386 | if (bitmap.Ok() && bitmap.GetPalette()->Ok()) |
1387 | { | |
1388 | unsigned char red, green, blue; | |
1389 | if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue)) | |
1390 | { | |
1391 | wxColour transparentColour(red, green, blue); | |
1392 | return Create(bitmap, transparentColour); | |
1393 | } | |
1394 | } | |
d275c7eb VZ |
1395 | #endif // wxUSE_PALETTE |
1396 | ||
6d167489 | 1397 | return FALSE; |
2bda0e17 KB |
1398 | } |
1399 | ||
1400 | // Create a mask from a bitmap and a colour indicating | |
1401 | // the transparent area | |
1402 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1403 | { | |
04ef50df | 1404 | #ifndef __WXMICROWIN__ |
4b7f2165 VZ |
1405 | wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") ); |
1406 | ||
2bda0e17 | 1407 | if ( m_maskBitmap ) |
1d792928 | 1408 | { |
6d167489 VZ |
1409 | ::DeleteObject((HBITMAP) m_maskBitmap); |
1410 | m_maskBitmap = 0; | |
1411 | } | |
4b7f2165 VZ |
1412 | |
1413 | int width = bitmap.GetWidth(), | |
1414 | height = bitmap.GetHeight(); | |
1415 | ||
1416 | // scan the bitmap for the transparent colour and set the corresponding | |
1417 | // pixels in the mask to BLACK and the rest to WHITE | |
f423f4db | 1418 | COLORREF maskColour = wxColourToPalRGB(colour); |
4b7f2165 VZ |
1419 | m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0); |
1420 | ||
1421 | HDC srcDC = ::CreateCompatibleDC(NULL); | |
1422 | HDC destDC = ::CreateCompatibleDC(NULL); | |
1423 | if ( !srcDC || !destDC ) | |
1424 | { | |
f6bcfd97 | 1425 | wxLogLastError(wxT("CreateCompatibleDC")); |
4b7f2165 VZ |
1426 | } |
1427 | ||
0bafad0c VZ |
1428 | bool ok = TRUE; |
1429 | ||
1e6feb95 VZ |
1430 | // SelectObject() will fail |
1431 | wxASSERT_MSG( !bitmap.GetSelectedInto(), | |
1432 | _T("bitmap can't be selected in another DC") ); | |
1433 | ||
0bafad0c VZ |
1434 | HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap)); |
1435 | if ( !hbmpSrcOld ) | |
6d167489 | 1436 | { |
f6bcfd97 | 1437 | wxLogLastError(wxT("SelectObject")); |
0bafad0c VZ |
1438 | |
1439 | ok = FALSE; | |
4b7f2165 | 1440 | } |
0bafad0c VZ |
1441 | |
1442 | HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap); | |
1443 | if ( !hbmpDstOld ) | |
4b7f2165 | 1444 | { |
f6bcfd97 | 1445 | wxLogLastError(wxT("SelectObject")); |
0bafad0c VZ |
1446 | |
1447 | ok = FALSE; | |
1d792928 | 1448 | } |
2bda0e17 | 1449 | |
59ff46cb | 1450 | if ( ok ) |
2bda0e17 | 1451 | { |
59ff46cb VZ |
1452 | // this will create a monochrome bitmap with 0 points for the pixels |
1453 | // which have the same value as the background colour and 1 for the | |
1454 | // others | |
1455 | ::SetBkColor(srcDC, maskColour); | |
1456 | ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY); | |
2bda0e17 | 1457 | } |
4b7f2165 | 1458 | |
0bafad0c | 1459 | ::SelectObject(srcDC, hbmpSrcOld); |
6d167489 | 1460 | ::DeleteDC(srcDC); |
0bafad0c | 1461 | ::SelectObject(destDC, hbmpDstOld); |
6d167489 | 1462 | ::DeleteDC(destDC); |
4b7f2165 | 1463 | |
0bafad0c | 1464 | return ok; |
59ff46cb | 1465 | #else // __WXMICROWIN__ |
04ef50df | 1466 | return FALSE; |
59ff46cb | 1467 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
6d167489 VZ |
1468 | } |
1469 | ||
1470 | // ---------------------------------------------------------------------------- | |
1471 | // wxBitmapHandler | |
1472 | // ---------------------------------------------------------------------------- | |
1d792928 | 1473 | |
6d167489 VZ |
1474 | bool wxBitmapHandler::Create(wxGDIImage *image, |
1475 | void *data, | |
1476 | long flags, | |
1477 | int width, int height, int depth) | |
1478 | { | |
1479 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); | |
1d792928 | 1480 | |
8c1375b9 | 1481 | return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE; |
2bda0e17 KB |
1482 | } |
1483 | ||
6d167489 VZ |
1484 | bool wxBitmapHandler::Load(wxGDIImage *image, |
1485 | const wxString& name, | |
1486 | long flags, | |
1487 | int width, int height) | |
2bda0e17 | 1488 | { |
6d167489 | 1489 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
2bda0e17 | 1490 | |
6d167489 VZ |
1491 | return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE; |
1492 | } | |
2bda0e17 | 1493 | |
6d167489 VZ |
1494 | bool wxBitmapHandler::Save(wxGDIImage *image, |
1495 | const wxString& name, | |
1496 | int type) | |
2bda0e17 | 1497 | { |
6d167489 VZ |
1498 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
1499 | ||
1500 | return bitmap ? SaveFile(bitmap, name, type) : FALSE; | |
2bda0e17 KB |
1501 | } |
1502 | ||
6d167489 VZ |
1503 | bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), |
1504 | void *WXUNUSED(data), | |
1505 | long WXUNUSED(type), | |
1506 | int WXUNUSED(width), | |
1507 | int WXUNUSED(height), | |
1508 | int WXUNUSED(depth)) | |
2bda0e17 | 1509 | { |
6d167489 | 1510 | return FALSE; |
2bda0e17 KB |
1511 | } |
1512 | ||
6d167489 VZ |
1513 | bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), |
1514 | const wxString& WXUNUSED(name), | |
1515 | long WXUNUSED(type), | |
1516 | int WXUNUSED(desiredWidth), | |
1517 | int WXUNUSED(desiredHeight)) | |
2bda0e17 | 1518 | { |
6d167489 | 1519 | return FALSE; |
2bda0e17 KB |
1520 | } |
1521 | ||
6d167489 VZ |
1522 | bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), |
1523 | const wxString& WXUNUSED(name), | |
1524 | int WXUNUSED(type), | |
1525 | const wxPalette *WXUNUSED(palette)) | |
2bda0e17 | 1526 | { |
6d167489 | 1527 | return FALSE; |
7b46ecac JS |
1528 | } |
1529 | ||
6d167489 VZ |
1530 | // ---------------------------------------------------------------------------- |
1531 | // DIB functions | |
1532 | // ---------------------------------------------------------------------------- | |
1533 | ||
04ef50df | 1534 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
1535 | bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, |
1536 | HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) | |
7b46ecac JS |
1537 | { |
1538 | unsigned long i, headerSize; | |
7b46ecac JS |
1539 | |
1540 | // Allocate space for a DIB header | |
1541 | headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); | |
999836aa VZ |
1542 | LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize); |
1543 | LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); | |
7b46ecac JS |
1544 | |
1545 | GetPaletteEntries(hPal, 0, 256, lpPe); | |
1546 | ||
7b46ecac JS |
1547 | memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER)); |
1548 | ||
7b46ecac JS |
1549 | // Fill in the static parts of the DIB header |
1550 | lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
1551 | lpDIBheader->bmiHeader.biWidth = xSize; | |
1552 | lpDIBheader->bmiHeader.biHeight = ySize; | |
1553 | lpDIBheader->bmiHeader.biPlanes = 1; | |
1554 | ||
1555 | // this value must be 1, 4, 8 or 24 so PixelDepth can only be | |
1556 | lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); | |
1557 | lpDIBheader->bmiHeader.biCompression = BI_RGB; | |
6d167489 | 1558 | lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3; |
7b46ecac JS |
1559 | lpDIBheader->bmiHeader.biClrUsed = 256; |
1560 | ||
1561 | ||
1562 | // Initialize the DIB palette | |
1563 | for (i = 0; i < 256; i++) { | |
1564 | lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags; | |
1565 | lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed; | |
1566 | lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen; | |
1567 | lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue; | |
1568 | } | |
1569 | ||
1570 | *lpDIBHeader = lpDIBheader; | |
1571 | ||
6d167489 | 1572 | return TRUE; |
7b46ecac JS |
1573 | } |
1574 | ||
6d167489 | 1575 | void wxFreeDIB(LPBITMAPINFO lpDIBHeader) |
7b46ecac | 1576 | { |
6d167489 | 1577 | free(lpDIBHeader); |
7b46ecac | 1578 | } |
04ef50df | 1579 | #endif |
7b46ecac | 1580 | |
4b7f2165 | 1581 | // ---------------------------------------------------------------------------- |
211b54b1 | 1582 | // global helper functions implemented here |
4b7f2165 VZ |
1583 | // ---------------------------------------------------------------------------- |
1584 | ||
211b54b1 VZ |
1585 | // helper of wxBitmapToHICON/HCURSOR |
1586 | static | |
1587 | HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, | |
1588 | bool iconWanted, | |
1589 | int hotSpotX, | |
1590 | int hotSpotY) | |
1591 | { | |
1592 | if ( !bmp.Ok() ) | |
1593 | { | |
1594 | // we can't create an icon/cursor form nothing | |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | wxMask *mask = bmp.GetMask(); | |
1599 | if ( !mask ) | |
1600 | { | |
1601 | // we must have a mask for an icon, so even if it's probably incorrect, | |
1602 | // do create it (grey is the "standard" transparent colour) | |
1603 | mask = new wxMask(bmp, *wxLIGHT_GREY); | |
1604 | } | |
1605 | ||
1606 | ICONINFO iconInfo; | |
4104d2b3 | 1607 | wxZeroMemory(iconInfo); |
211b54b1 VZ |
1608 | iconInfo.fIcon = iconWanted; // do we want an icon or a cursor? |
1609 | if ( !iconWanted ) | |
1610 | { | |
1611 | iconInfo.xHotspot = hotSpotX; | |
1612 | iconInfo.yHotspot = hotSpotY; | |
1613 | } | |
1614 | ||
1615 | iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap()); | |
1616 | iconInfo.hbmColor = GetHbitmapOf(bmp); | |
1617 | ||
1618 | // black out the transparent area to preserve background colour, because | |
1619 | // Windows blits the original bitmap using SRCINVERT (XOR) after applying | |
1620 | // the mask to the dest rect. | |
1621 | { | |
1622 | MemoryHDC dcSrc, dcDst; | |
1623 | SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()), | |
1624 | selectBitmap(dcDst, iconInfo.hbmColor); | |
1625 | ||
1626 | if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(), | |
1627 | dcSrc, 0, 0, SRCAND) ) | |
1628 | { | |
1629 | wxLogLastError(_T("BitBlt")); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | HICON hicon = ::CreateIconIndirect(&iconInfo); | |
1634 | ||
1635 | if ( !bmp.GetMask() ) | |
1636 | { | |
1637 | // we created the mask, now delete it | |
1638 | delete mask; | |
1639 | } | |
1640 | ||
1641 | // delete the inverted mask bitmap we created as well | |
1642 | ::DeleteObject(iconInfo.hbmMask); | |
1643 | ||
1644 | return hicon; | |
1645 | } | |
1646 | ||
1647 | HICON wxBitmapToHICON(const wxBitmap& bmp) | |
1648 | { | |
1649 | return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0); | |
1650 | } | |
1651 | ||
1652 | HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY) | |
1653 | { | |
1654 | return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY); | |
1655 | } | |
1656 | ||
1657 | HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) | |
4b7f2165 | 1658 | { |
04ef50df | 1659 | #ifndef __WXMICROWIN__ |
4b7f2165 VZ |
1660 | wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") ); |
1661 | ||
1662 | // get width/height from the bitmap if not given | |
1663 | if ( !w || !h ) | |
1664 | { | |
1665 | BITMAP bm; | |
1666 | ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm); | |
1667 | w = bm.bmWidth; | |
1668 | h = bm.bmHeight; | |
1669 | } | |
1670 | ||
1671 | HDC hdcSrc = ::CreateCompatibleDC(NULL); | |
1672 | HDC hdcDst = ::CreateCompatibleDC(NULL); | |
1673 | if ( !hdcSrc || !hdcDst ) | |
1674 | { | |
f6bcfd97 | 1675 | wxLogLastError(wxT("CreateCompatibleDC")); |
4b7f2165 VZ |
1676 | } |
1677 | ||
1678 | HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0); | |
1679 | if ( !hbmpInvMask ) | |
1680 | { | |
f6bcfd97 | 1681 | wxLogLastError(wxT("CreateBitmap")); |
4b7f2165 VZ |
1682 | } |
1683 | ||
e7222d65 JS |
1684 | HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask); |
1685 | HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask); | |
4b7f2165 VZ |
1686 | if ( !::BitBlt(hdcDst, 0, 0, w, h, |
1687 | hdcSrc, 0, 0, | |
1688 | NOTSRCCOPY) ) | |
1689 | { | |
f6bcfd97 | 1690 | wxLogLastError(wxT("BitBlt")); |
4b7f2165 | 1691 | } |
7b46ecac | 1692 | |
e7222d65 JS |
1693 | // Deselect objects |
1694 | SelectObject(hdcSrc,srcTmp); | |
1695 | SelectObject(hdcDst,dstTmp); | |
1696 | ||
4b7f2165 VZ |
1697 | ::DeleteDC(hdcSrc); |
1698 | ::DeleteDC(hdcDst); | |
1699 | ||
1700 | return hbmpInvMask; | |
04ef50df JS |
1701 | #else |
1702 | return 0; | |
1703 | #endif | |
4b7f2165 | 1704 | } |