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