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