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