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