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