]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
3 | // Purpose: wxBitmap | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
1d792928 | 9 | // Licence: wxWindows license |
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 | ||
62e1ba75 JS |
43 | //#include "device.h" |
44 | ||
2bda0e17 | 45 | #include "wx/msw/private.h" |
1d792928 VZ |
46 | #include "wx/log.h" |
47 | ||
04ef50df | 48 | #if !defined(__WXMICROWIN__) |
2bda0e17 | 49 | #include "wx/msw/dib.h" |
04ef50df JS |
50 | #endif |
51 | ||
b75dd496 | 52 | #include "wx/image.h" |
66e23ad2 | 53 | #include "wx/xpmdecod.h" |
2bda0e17 | 54 | |
3c1a88d8 VZ |
55 | // missing from mingw32 header |
56 | #ifndef CLR_INVALID | |
57 | #define CLR_INVALID ((COLORREF)-1) | |
58 | #endif // no CLR_INVALID | |
59 | ||
10fcf31a VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // macros | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
4b7f2165 VZ |
64 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) |
65 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
6d167489 | 66 | |
4b7f2165 | 67 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) |
2bda0e17 | 68 | |
10fcf31a VZ |
69 | // ============================================================================ |
70 | // implementation | |
71 | // ============================================================================ | |
72 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // wxBitmapRefData | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | wxBitmapRefData::wxBitmapRefData() | |
2bda0e17 | 78 | { |
6d167489 VZ |
79 | m_quality = 0; |
80 | m_selectedInto = NULL; | |
81 | m_numColors = 0; | |
82 | m_bitmapMask = NULL; | |
340196c0 | 83 | m_hBitmap = (WXHBITMAP) NULL; |
2bda0e17 KB |
84 | } |
85 | ||
6d167489 | 86 | void wxBitmapRefData::Free() |
2bda0e17 | 87 | { |
d59ceba5 VZ |
88 | wxASSERT_MSG( !m_selectedInto, |
89 | wxT("deleting bitmap still selected into wxMemoryDC") ); | |
2bda0e17 | 90 | |
d59ceba5 | 91 | if ( m_hBitmap) |
6d167489 | 92 | { |
54a96d02 | 93 | // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap); |
62e1ba75 | 94 | #if 1 |
6d167489 VZ |
95 | if ( !::DeleteObject((HBITMAP)m_hBitmap) ) |
96 | { | |
f6bcfd97 | 97 | wxLogLastError(wxT("DeleteObject(hbitmap)")); |
6d167489 | 98 | } |
62e1ba75 | 99 | #endif |
6d167489 | 100 | } |
e7003166 | 101 | |
6d167489 VZ |
102 | delete m_bitmapMask; |
103 | m_bitmapMask = NULL; | |
2bda0e17 KB |
104 | } |
105 | ||
10fcf31a | 106 | // ---------------------------------------------------------------------------- |
6d167489 | 107 | // wxBitmap creation |
10fcf31a VZ |
108 | // ---------------------------------------------------------------------------- |
109 | ||
4fe5383d VZ |
110 | // this function should be called from all wxBitmap ctors |
111 | void wxBitmap::Init() | |
2bda0e17 | 112 | { |
4fe5383d | 113 | // m_refData = NULL; done in the base class ctor |
2bda0e17 | 114 | |
4fe5383d VZ |
115 | } |
116 | ||
6d167489 VZ |
117 | #ifdef __WIN32__ |
118 | ||
119 | bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) | |
120 | { | |
04ef50df | 121 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
122 | // it may be either HICON or HCURSOR |
123 | HICON hicon = (HICON)icon.GetHandle(); | |
124 | ||
125 | ICONINFO iconInfo; | |
126 | if ( !::GetIconInfo(hicon, &iconInfo) ) | |
127 | { | |
f6bcfd97 | 128 | wxLogLastError(wxT("GetIconInfo")); |
6d167489 VZ |
129 | |
130 | return FALSE; | |
131 | } | |
132 | ||
133 | wxBitmapRefData *refData = new wxBitmapRefData; | |
134 | m_refData = refData; | |
135 | ||
d9c8e68e VZ |
136 | int w = icon.GetWidth(), |
137 | h = icon.GetHeight(); | |
138 | ||
139 | refData->m_width = w; | |
140 | refData->m_height = h; | |
6d167489 VZ |
141 | refData->m_depth = wxDisplayDepth(); |
142 | ||
143 | refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor; | |
d9c8e68e VZ |
144 | |
145 | // the mask returned by GetIconInfo() is inversed compared to the usual | |
146 | // wxWin convention | |
4b7f2165 VZ |
147 | refData->m_bitmapMask = new wxMask((WXHBITMAP) |
148 | wxInvertMask(iconInfo.hbmMask, w, h)); | |
6d167489 | 149 | |
68f36a2c VZ |
150 | |
151 | // delete the old one now as we don't need it any more | |
152 | ::DeleteObject(iconInfo.hbmMask); | |
153 | ||
6d167489 VZ |
154 | #if WXWIN_COMPATIBILITY_2 |
155 | refData->m_ok = TRUE; | |
156 | #endif // WXWIN_COMPATIBILITY_2 | |
157 | ||
158 | return TRUE; | |
04ef50df JS |
159 | #else |
160 | return FALSE; | |
161 | #endif | |
6d167489 VZ |
162 | } |
163 | ||
164 | #endif // Win32 | |
165 | ||
166 | bool wxBitmap::CopyFromCursor(const wxCursor& cursor) | |
4fe5383d VZ |
167 | { |
168 | UnRef(); | |
07cf98cb | 169 | |
6d167489 | 170 | if ( !cursor.Ok() ) |
4fe5383d | 171 | return FALSE; |
07cf98cb | 172 | |
6d167489 VZ |
173 | #ifdef __WIN16__ |
174 | wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") ); | |
175 | ||
176 | return FALSE; | |
8f177c8e | 177 | #else |
6d167489 | 178 | return CopyFromIconOrCursor(cursor); |
8f177c8e | 179 | #endif // Win16 |
6d167489 VZ |
180 | } |
181 | ||
182 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
183 | { | |
184 | UnRef(); | |
07cf98cb | 185 | |
6d167489 VZ |
186 | if ( !icon.Ok() ) |
187 | return FALSE; | |
4fe5383d VZ |
188 | |
189 | // GetIconInfo() doesn't exist under Win16 and I don't know any other way | |
190 | // to create a bitmap from icon there - but using this way we won't have | |
191 | // the mask (FIXME) | |
192 | #ifdef __WIN16__ | |
6d167489 VZ |
193 | int width = icon.GetWidth(), |
194 | height = icon.GetHeight(); | |
195 | ||
4fe5383d | 196 | // copy the icon to the bitmap |
6d167489 | 197 | ScreenHDC hdcScreen; |
4fe5383d VZ |
198 | HDC hdc = ::CreateCompatibleDC(hdcScreen); |
199 | HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height); | |
07cf98cb VZ |
200 | HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap); |
201 | ||
6d167489 | 202 | ::DrawIcon(hdc, 0, 0, GetHiconOf(icon)); |
07cf98cb VZ |
203 | |
204 | ::SelectObject(hdc, hbmpOld); | |
205 | ::DeleteDC(hdc); | |
4fe5383d | 206 | |
6d167489 VZ |
207 | wxBitmapRefData *refData = new wxBitmapRefData; |
208 | m_refData = refData; | |
4fe5383d | 209 | |
6d167489 VZ |
210 | refData->m_width = width; |
211 | refData->m_height = height; | |
212 | refData->m_depth = wxDisplayDepth(); | |
07cf98cb | 213 | |
6d167489 | 214 | refData->m_hBitmap = (WXHBITMAP)hbitmap; |
07cf98cb | 215 | |
6d167489 VZ |
216 | #if WXWIN_COMPATIBILITY_2 |
217 | refData->m_ok = TRUE; | |
218 | #endif // WXWIN_COMPATIBILITY_2 | |
222594ea | 219 | |
4fe5383d | 220 | return TRUE; |
6d167489 VZ |
221 | #else // Win32 |
222 | return CopyFromIconOrCursor(icon); | |
223 | #endif // Win16/Win32 | |
10fcf31a VZ |
224 | } |
225 | ||
226 | wxBitmap::~wxBitmap() | |
2bda0e17 | 227 | { |
2bda0e17 KB |
228 | } |
229 | ||
5bd3a2da | 230 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) |
2bda0e17 | 231 | { |
4fe5383d | 232 | Init(); |
2bda0e17 | 233 | |
04ef50df | 234 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
235 | wxBitmapRefData *refData = new wxBitmapRefData; |
236 | m_refData = refData; | |
2bda0e17 | 237 | |
5bd3a2da VZ |
238 | refData->m_width = width; |
239 | refData->m_height = height; | |
240 | refData->m_depth = depth; | |
6d167489 VZ |
241 | refData->m_numColors = 0; |
242 | refData->m_selectedInto = NULL; | |
2bda0e17 | 243 | |
5bd3a2da VZ |
244 | char *data; |
245 | if ( depth == 1 ) | |
246 | { | |
247 | // we assume that it is in XBM format which is not quite the same as | |
248 | // the format CreateBitmap() wants because the order of bytes in the | |
249 | // line is inversed! | |
4d24ece7 VZ |
250 | const size_t bytesPerLine = (width + 7) / 8; |
251 | const size_t padding = bytesPerLine % 2; | |
252 | const size_t len = height * ( padding + bytesPerLine ); | |
5bd3a2da VZ |
253 | data = (char *)malloc(len); |
254 | const char *src = bits; | |
255 | char *dst = data; | |
256 | ||
257 | for ( int rows = 0; rows < height; rows++ ) | |
258 | { | |
0765adca | 259 | for ( size_t cols = 0; cols < bytesPerLine; cols++ ) |
5bd3a2da | 260 | { |
0765adca VZ |
261 | unsigned char val = *src++; |
262 | unsigned char reversed = 0; | |
263 | ||
264 | for ( int bits = 0; bits < 8; bits++) | |
265 | { | |
266 | reversed <<= 1; | |
267 | reversed |= (val & 0x01); | |
268 | val >>= 1; | |
269 | } | |
270 | *dst++ = reversed; | |
5bd3a2da VZ |
271 | } |
272 | ||
273 | if ( padding ) | |
274 | *dst++ = 0; | |
5bd3a2da VZ |
275 | } |
276 | } | |
277 | else | |
278 | { | |
279 | // bits should already be in Windows standard format | |
280 | data = (char *)bits; // const_cast is harmless | |
281 | } | |
282 | ||
283 | HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data); | |
6d167489 VZ |
284 | if ( !hbmp ) |
285 | { | |
f6bcfd97 | 286 | wxLogLastError(wxT("CreateBitmap")); |
6d167489 | 287 | } |
2bda0e17 | 288 | |
5bd3a2da VZ |
289 | if ( data != bits ) |
290 | { | |
291 | free(data); | |
292 | } | |
293 | ||
6d167489 | 294 | SetHBITMAP((WXHBITMAP)hbmp); |
04ef50df | 295 | #endif |
2bda0e17 KB |
296 | } |
297 | ||
2fd284a4 | 298 | // Create from XPM data |
4b7f2165 | 299 | bool wxBitmap::CreateFromXpm(const char **data) |
2fd284a4 | 300 | { |
66e23ad2 | 301 | #if wxUSE_IMAGE && wxUSE_XPM |
4fe5383d VZ |
302 | Init(); |
303 | ||
66e23ad2 | 304 | wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") ) |
4d24ece7 | 305 | |
66e23ad2 VS |
306 | wxXPMDecoder decoder; |
307 | wxImage img = decoder.ReadData(data); | |
308 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) | |
4d24ece7 | 309 | |
c59b4b01 | 310 | *this = wxBitmap(img); |
66e23ad2 VS |
311 | return TRUE; |
312 | #else | |
4d24ece7 | 313 | return FALSE; |
66e23ad2 | 314 | #endif |
2fd284a4 JS |
315 | } |
316 | ||
debe6624 | 317 | wxBitmap::wxBitmap(int w, int h, int d) |
2bda0e17 | 318 | { |
4fe5383d | 319 | Init(); |
2bda0e17 | 320 | |
4fe5383d | 321 | (void)Create(w, h, d); |
2bda0e17 KB |
322 | } |
323 | ||
debe6624 | 324 | wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) |
2bda0e17 | 325 | { |
4fe5383d | 326 | Init(); |
2bda0e17 | 327 | |
6d167489 | 328 | (void)Create(data, type, width, height, depth); |
2bda0e17 KB |
329 | } |
330 | ||
2aeec9ec | 331 | wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) |
2bda0e17 | 332 | { |
4fe5383d | 333 | Init(); |
2bda0e17 | 334 | |
4fe5383d | 335 | LoadFile(filename, (int)type); |
2bda0e17 KB |
336 | } |
337 | ||
debe6624 | 338 | bool wxBitmap::Create(int w, int h, int d) |
2bda0e17 | 339 | { |
6d167489 VZ |
340 | UnRef(); |
341 | ||
342 | m_refData = new wxBitmapRefData; | |
343 | ||
344 | GetBitmapData()->m_width = w; | |
345 | GetBitmapData()->m_height = h; | |
346 | GetBitmapData()->m_depth = d; | |
347 | ||
348 | HBITMAP hbmp; | |
e640f823 | 349 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
350 | if ( d > 0 ) |
351 | { | |
352 | hbmp = ::CreateBitmap(w, h, 1, d, NULL); | |
353 | if ( !hbmp ) | |
354 | { | |
f6bcfd97 | 355 | wxLogLastError(wxT("CreateBitmap")); |
6d167489 VZ |
356 | } |
357 | } | |
358 | else | |
e640f823 | 359 | #endif |
6d167489 VZ |
360 | { |
361 | ScreenHDC dc; | |
362 | hbmp = ::CreateCompatibleBitmap(dc, w, h); | |
363 | if ( !hbmp ) | |
364 | { | |
f6bcfd97 | 365 | wxLogLastError(wxT("CreateCompatibleBitmap")); |
6d167489 VZ |
366 | } |
367 | ||
368 | GetBitmapData()->m_depth = wxDisplayDepth(); | |
369 | } | |
2bda0e17 | 370 | |
6d167489 | 371 | SetHBITMAP((WXHBITMAP)hbmp); |
2bda0e17 | 372 | |
6d167489 VZ |
373 | #if WXWIN_COMPATIBILITY_2 |
374 | GetBitmapData()->m_ok = hbmp != 0; | |
375 | #endif // WXWIN_COMPATIBILITY_2 | |
6d167489 | 376 | return Ok(); |
2bda0e17 KB |
377 | } |
378 | ||
6d51f220 VZ |
379 | // ---------------------------------------------------------------------------- |
380 | // wxImage to/from conversions | |
381 | // ---------------------------------------------------------------------------- | |
382 | ||
383 | #if wxUSE_IMAGE | |
384 | ||
fec19ea9 VS |
385 | bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) |
386 | { | |
8cb172b4 | 387 | #ifdef __WXMICROWIN__ |
62e1ba75 JS |
388 | |
389 | // Set this to 1 to experiment with mask code, | |
390 | // which currently doesn't work | |
391 | #define USE_MASKS 0 | |
392 | ||
54a96d02 | 393 | m_refData = new wxBitmapRefData(); |
e640f823 JS |
394 | |
395 | // Initial attempt at a simple-minded implementation. | |
396 | // The bitmap will always be created at the screen depth, | |
397 | // so the 'depth' argument is ignored. | |
e640f823 JS |
398 | |
399 | HDC hScreenDC = ::GetDC(NULL); | |
62e1ba75 | 400 | // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp); |
e640f823 JS |
401 | int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL); |
402 | ||
403 | HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight()); | |
62e1ba75 JS |
404 | HBITMAP hMaskBitmap = NULL; |
405 | HBITMAP hOldMaskBitmap = NULL; | |
406 | HDC hMaskDC = NULL; | |
407 | unsigned char maskR = 0; | |
408 | unsigned char maskG = 0; | |
409 | unsigned char maskB = 0; | |
410 | ||
54a96d02 | 411 | // printf("Created bitmap %d\n", (int) hBitmap); |
e640f823 JS |
412 | if (hBitmap == NULL) |
413 | { | |
414 | ::ReleaseDC(NULL, hScreenDC); | |
415 | return FALSE; | |
416 | } | |
417 | HDC hMemDC = ::CreateCompatibleDC(hScreenDC); | |
e640f823 JS |
418 | |
419 | HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); | |
62e1ba75 JS |
420 | ::ReleaseDC(NULL, hScreenDC); |
421 | ||
422 | // created an mono-bitmap for the possible mask | |
423 | bool hasMask = image.HasMask(); | |
424 | ||
425 | if ( hasMask ) | |
426 | { | |
427 | #if USE_MASKS | |
428 | // FIXME: we should be able to pass bpp = 1, but | |
429 | // GdBlit can't handle a different depth | |
430 | #if 0 | |
431 | hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL ); | |
432 | #else | |
433 | hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight()); | |
434 | #endif | |
435 | maskR = image.GetMaskRed(); | |
436 | maskG = image.GetMaskGreen(); | |
437 | maskB = image.GetMaskBlue(); | |
438 | ||
439 | if (!hMaskBitmap) | |
440 | { | |
441 | hasMask = FALSE; | |
442 | } | |
443 | else | |
444 | { | |
445 | hScreenDC = ::GetDC(NULL); | |
446 | hMaskDC = ::CreateCompatibleDC(hScreenDC); | |
447 | ::ReleaseDC(NULL, hScreenDC); | |
448 | ||
449 | hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap); | |
450 | } | |
451 | #else | |
452 | hasMask = FALSE; | |
453 | #endif | |
454 | } | |
e640f823 JS |
455 | |
456 | int i, j; | |
457 | for (i = 0; i < image.GetWidth(); i++) | |
458 | { | |
459 | for (j = 0; j < image.GetHeight(); j++) | |
460 | { | |
461 | unsigned char red = image.GetRed(i, j); | |
462 | unsigned char green = image.GetGreen(i, j); | |
463 | unsigned char blue = image.GetBlue(i, j); | |
464 | ||
465 | ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue)); | |
62e1ba75 JS |
466 | |
467 | if (hasMask) | |
468 | { | |
469 | // scan the bitmap for the transparent colour and set the corresponding | |
470 | // pixels in the mask to BLACK and the rest to WHITE | |
471 | if (maskR == red && maskG == green && maskB == blue) | |
472 | ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0)); | |
473 | else | |
474 | ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255)); | |
475 | } | |
e640f823 JS |
476 | } |
477 | } | |
478 | ||
479 | ::SelectObject(hMemDC, hOldBitmap); | |
480 | ::DeleteDC(hMemDC); | |
62e1ba75 JS |
481 | if (hasMask) |
482 | { | |
483 | ::SelectObject(hMaskDC, hOldMaskBitmap); | |
484 | ::DeleteDC(hMaskDC); | |
485 | ||
486 | ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap); | |
487 | } | |
e640f823 | 488 | |
e640f823 JS |
489 | SetWidth(image.GetWidth()); |
490 | SetHeight(image.GetHeight()); | |
491 | SetDepth(screenDepth); | |
492 | SetHBITMAP( (WXHBITMAP) hBitmap ); | |
493 | ||
494 | #if wxUSE_PALETTE | |
495 | // Copy the palette from the source image | |
496 | SetPalette(image.GetPalette()); | |
497 | #endif // wxUSE_PALETTE | |
498 | ||
54a96d02 JS |
499 | #if WXWIN_COMPATIBILITY_2 |
500 | // check the wxBitmap object | |
501 | GetBitmapData()->SetOk(); | |
502 | #endif // WXWIN_COMPATIBILITY_2 | |
503 | ||
e640f823 JS |
504 | return TRUE; |
505 | ||
8cb172b4 | 506 | #else |
fec19ea9 VS |
507 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ) |
508 | ||
509 | m_refData = new wxBitmapRefData(); | |
510 | ||
511 | // sizeLimit is the MS upper limit for the DIB size | |
512 | #ifdef WIN32 | |
513 | int sizeLimit = 1024*768*3; | |
514 | #else | |
515 | int sizeLimit = 0x7fff ; | |
516 | #endif | |
517 | ||
518 | // width and height of the device-dependent bitmap | |
519 | int width = image.GetWidth(); | |
520 | int bmpHeight = image.GetHeight(); | |
521 | ||
522 | // calc the number of bytes per scanline and padding | |
523 | int bytePerLine = width*3; | |
524 | int sizeDWORD = sizeof( DWORD ); | |
525 | int lineBoundary = bytePerLine % sizeDWORD; | |
526 | int padding = 0; | |
527 | if( lineBoundary > 0 ) | |
528 | { | |
529 | padding = sizeDWORD - lineBoundary; | |
530 | bytePerLine += padding; | |
531 | } | |
532 | // calc the number of DIBs and heights of DIBs | |
533 | int numDIB = 1; | |
534 | int hRemain = 0; | |
535 | int height = sizeLimit/bytePerLine; | |
536 | if( height >= bmpHeight ) | |
537 | height = bmpHeight; | |
538 | else | |
539 | { | |
540 | numDIB = bmpHeight / height; | |
541 | hRemain = bmpHeight % height; | |
542 | if( hRemain >0 ) numDIB++; | |
543 | } | |
544 | ||
545 | // set bitmap parameters | |
c447ce17 | 546 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); |
fec19ea9 VS |
547 | SetWidth( width ); |
548 | SetHeight( bmpHeight ); | |
549 | if (depth == -1) depth = wxDisplayDepth(); | |
550 | SetDepth( depth ); | |
551 | ||
e22c13fe | 552 | #if wxUSE_PALETTE |
19193a2c KB |
553 | // Copy the palette from the source image |
554 | SetPalette(image.GetPalette()); | |
e22c13fe | 555 | #endif // wxUSE_PALETTE |
19193a2c | 556 | |
fec19ea9 VS |
557 | // create a DIB header |
558 | int headersize = sizeof(BITMAPINFOHEADER); | |
559 | BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); | |
097aeb99 | 560 | wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") ); |
fec19ea9 VS |
561 | // Fill in the DIB header |
562 | lpDIBh->bmiHeader.biSize = headersize; | |
563 | lpDIBh->bmiHeader.biWidth = (DWORD)width; | |
564 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
565 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
566 | // the general formula for biSizeImage: | |
567 | // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height; | |
568 | lpDIBh->bmiHeader.biPlanes = 1; | |
569 | lpDIBh->bmiHeader.biBitCount = 24; | |
570 | lpDIBh->bmiHeader.biCompression = BI_RGB; | |
571 | lpDIBh->bmiHeader.biClrUsed = 0; | |
572 | // These seem not really needed for our purpose here. | |
573 | lpDIBh->bmiHeader.biClrImportant = 0; | |
574 | lpDIBh->bmiHeader.biXPelsPerMeter = 0; | |
575 | lpDIBh->bmiHeader.biYPelsPerMeter = 0; | |
576 | // memory for DIB data | |
577 | unsigned char *lpBits; | |
578 | lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage ); | |
579 | if( !lpBits ) | |
580 | { | |
581 | wxFAIL_MSG( wxT("could not allocate memory for DIB") ); | |
582 | free( lpDIBh ); | |
583 | return FALSE; | |
584 | } | |
585 | ||
586 | // create and set the device-dependent bitmap | |
587 | HDC hdc = ::GetDC(NULL); | |
588 | HDC memdc = ::CreateCompatibleDC( hdc ); | |
589 | HBITMAP hbitmap; | |
590 | hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight ); | |
591 | ::SelectObject( memdc, hbitmap); | |
592 | ||
d275c7eb | 593 | #if wxUSE_PALETTE |
fec19ea9 VS |
594 | HPALETTE hOldPalette = 0; |
595 | if (image.GetPalette().Ok()) | |
596 | { | |
597 | hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE); | |
598 | ::RealizePalette(memdc); | |
599 | } | |
d275c7eb | 600 | #endif // wxUSE_PALETTE |
fec19ea9 VS |
601 | |
602 | // copy image data into DIB data and then into DDB (in a loop) | |
603 | unsigned char *data = image.GetData(); | |
604 | int i, j, n; | |
605 | int origin = 0; | |
606 | unsigned char *ptdata = data; | |
607 | unsigned char *ptbits; | |
608 | ||
609 | for( n=0; n<numDIB; n++ ) | |
610 | { | |
611 | if( numDIB > 1 && n == numDIB-1 && hRemain > 0 ) | |
612 | { | |
613 | // redefine height and size of the (possibly) last smaller DIB | |
614 | // memory is not reallocated | |
615 | height = hRemain; | |
616 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
617 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
618 | } | |
619 | ptbits = lpBits; | |
620 | ||
621 | for( j=0; j<height; j++ ) | |
622 | { | |
623 | for( i=0; i<width; i++ ) | |
624 | { | |
625 | *(ptbits++) = *(ptdata+2); | |
626 | *(ptbits++) = *(ptdata+1); | |
627 | *(ptbits++) = *(ptdata ); | |
628 | ptdata += 3; | |
629 | } | |
630 | for( i=0; i< padding; i++ ) *(ptbits++) = 0; | |
631 | } | |
632 | ::StretchDIBits( memdc, 0, origin, width, height,\ | |
633 | 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY); | |
634 | origin += height; | |
635 | // if numDIB = 1, lines below can also be used | |
636 | // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
637 | // The above line is equivalent to the following two lines. | |
638 | // hbitmap = ::CreateCompatibleBitmap( hdc, width, height ); | |
639 | // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS); | |
640 | // or the following lines | |
641 | // hbitmap = ::CreateCompatibleBitmap( hdc, width, height ); | |
642 | // HDC memdc = ::CreateCompatibleDC( hdc ); | |
643 | // ::SelectObject( memdc, hbitmap); | |
644 | // ::SetDIBitsToDevice( memdc, 0, 0, width, height, | |
645 | // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS); | |
646 | // ::SelectObject( memdc, 0 ); | |
647 | // ::DeleteDC( memdc ); | |
648 | } | |
649 | SetHBITMAP( (WXHBITMAP) hbitmap ); | |
650 | ||
d275c7eb | 651 | #if wxUSE_PALETTE |
fec19ea9 VS |
652 | if (hOldPalette) |
653 | SelectPalette(memdc, hOldPalette, FALSE); | |
d275c7eb | 654 | #endif // wxUSE_PALETTE |
fec19ea9 VS |
655 | |
656 | // similarly, created an mono-bitmap for the possible mask | |
657 | if( image.HasMask() ) | |
658 | { | |
659 | hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL ); | |
660 | HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap); | |
661 | if( numDIB == 1 ) height = bmpHeight; | |
662 | else height = sizeLimit/bytePerLine; | |
663 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
664 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
665 | origin = 0; | |
666 | unsigned char r = image.GetMaskRed(); | |
667 | unsigned char g = image.GetMaskGreen(); | |
668 | unsigned char b = image.GetMaskBlue(); | |
669 | unsigned char zero = 0, one = 255; | |
670 | ptdata = data; | |
671 | for( n=0; n<numDIB; n++ ) | |
672 | { | |
673 | if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 ) | |
674 | { | |
675 | // redefine height and size of the (possibly) last smaller DIB | |
676 | // memory is not reallocated | |
677 | height = hRemain; | |
678 | lpDIBh->bmiHeader.biHeight = (DWORD)(-height); | |
679 | lpDIBh->bmiHeader.biSizeImage = bytePerLine*height; | |
680 | } | |
681 | ptbits = lpBits; | |
682 | for( int j=0; j<height; j++ ) | |
683 | { | |
684 | for(i=0; i<width; i++ ) | |
685 | { | |
686 | // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) ) | |
687 | unsigned char cr = (*(ptdata++)) ; | |
688 | unsigned char cg = (*(ptdata++)) ; | |
689 | unsigned char cb = (*(ptdata++)) ; | |
690 | ||
691 | if( ( cr !=r) || (cg!=g) || (cb!=b) ) | |
692 | { | |
693 | *(ptbits++) = one; | |
694 | *(ptbits++) = one; | |
695 | *(ptbits++) = one; | |
696 | } | |
697 | else | |
698 | { | |
699 | *(ptbits++) = zero; | |
700 | *(ptbits++) = zero; | |
701 | *(ptbits++) = zero; | |
702 | } | |
703 | } | |
704 | for( i=0; i< padding; i++ ) *(ptbits++) = zero; | |
705 | } | |
706 | ::StretchDIBits( memdc, 0, origin, width, height,\ | |
707 | 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY); | |
708 | origin += height; | |
709 | } | |
710 | // create a wxMask object | |
711 | wxMask *mask = new wxMask(); | |
712 | mask->SetMaskBitmap( (WXHBITMAP) hbitmap ); | |
713 | SetMask( mask ); | |
714 | // It will be deleted when the wxBitmap object is deleted (as of 01/1999) | |
715 | /* The following can also be used but is slow to run | |
716 | wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue()); | |
717 | wxMask *mask = new wxMask( *this, colour ); | |
718 | SetMask( mask ); | |
719 | */ | |
720 | ||
721 | ::SelectObject( memdc, hbmpOld ); | |
722 | } | |
723 | ||
724 | // free allocated resources | |
725 | ::DeleteDC( memdc ); | |
726 | ::ReleaseDC(NULL, hdc); | |
727 | free(lpDIBh); | |
728 | free(lpBits); | |
729 | ||
730 | #if WXWIN_COMPATIBILITY_2 | |
731 | // check the wxBitmap object | |
732 | GetBitmapData()->SetOk(); | |
733 | #endif // WXWIN_COMPATIBILITY_2 | |
6d51f220 | 734 | |
fec19ea9 | 735 | return TRUE; |
8cb172b4 | 736 | #endif |
fec19ea9 VS |
737 | } |
738 | ||
739 | wxImage wxBitmap::ConvertToImage() const | |
740 | { | |
8cb172b4 | 741 | #ifdef __WXMICROWIN__ |
e640f823 JS |
742 | // Initial attempt at a simple-minded implementation. |
743 | // The bitmap will always be created at the screen depth, | |
744 | // so the 'depth' argument is ignored. | |
745 | // TODO: transparency (create a mask image) | |
746 | ||
747 | if (!Ok()) | |
748 | { | |
749 | wxFAIL_MSG( wxT("bitmap is invalid") ); | |
750 | return wxNullImage; | |
751 | } | |
752 | ||
753 | wxImage image; | |
754 | ||
755 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
756 | ||
757 | // create an wxImage object | |
758 | int width = GetWidth(); | |
759 | int height = GetHeight(); | |
760 | image.Create( width, height ); | |
761 | unsigned char *data = image.GetData(); | |
762 | if( !data ) | |
763 | { | |
764 | wxFAIL_MSG( wxT("could not allocate data for image") ); | |
765 | return wxNullImage; | |
766 | } | |
767 | ||
768 | HDC hScreenDC = ::GetDC(NULL); | |
769 | ||
770 | HDC hMemDC = ::CreateCompatibleDC(hScreenDC); | |
771 | ::ReleaseDC(NULL, hScreenDC); | |
772 | ||
773 | HBITMAP hBitmap = (HBITMAP) GetHBITMAP(); | |
774 | ||
775 | HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); | |
776 | ||
777 | int i, j; | |
778 | for (i = 0; i < GetWidth(); i++) | |
779 | { | |
780 | for (j = 0; j < GetHeight(); j++) | |
781 | { | |
782 | COLORREF color = ::GetPixel(hMemDC, i, j); | |
783 | unsigned char red = GetRValue(color); | |
784 | unsigned char green = GetGValue(color); | |
785 | unsigned char blue = GetBValue(color); | |
786 | ||
787 | image.SetRGB(i, j, red, green, blue); | |
788 | } | |
789 | } | |
790 | ||
791 | ::SelectObject(hMemDC, hOldBitmap); | |
792 | ::DeleteDC(hMemDC); | |
793 | ||
794 | #if wxUSE_PALETTE | |
795 | // Copy the palette from the source image | |
796 | if (GetPalette()) | |
797 | image.SetPalette(* GetPalette()); | |
798 | #endif // wxUSE_PALETTE | |
799 | ||
800 | return image; | |
801 | ||
802 | #else // __MICROWIN__ | |
803 | ||
fec19ea9 | 804 | wxImage image; |
6d51f220 | 805 | |
fec19ea9 VS |
806 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
807 | ||
808 | // create an wxImage object | |
809 | int width = GetWidth(); | |
810 | int height = GetHeight(); | |
811 | image.Create( width, height ); | |
812 | unsigned char *data = image.GetData(); | |
813 | if( !data ) | |
814 | { | |
815 | wxFAIL_MSG( wxT("could not allocate data for image") ); | |
816 | return wxNullImage; | |
817 | } | |
818 | ||
819 | // calc the number of bytes per scanline and padding in the DIB | |
820 | int bytePerLine = width*3; | |
821 | int sizeDWORD = sizeof( DWORD ); | |
822 | int lineBoundary = bytePerLine % sizeDWORD; | |
823 | int padding = 0; | |
824 | if( lineBoundary > 0 ) | |
825 | { | |
826 | padding = sizeDWORD - lineBoundary; | |
827 | bytePerLine += padding; | |
828 | } | |
829 | ||
830 | // create a DIB header | |
831 | int headersize = sizeof(BITMAPINFOHEADER); | |
832 | BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); | |
833 | if( !lpDIBh ) | |
834 | { | |
835 | wxFAIL_MSG( wxT("could not allocate data for DIB header") ); | |
836 | free( data ); | |
837 | return wxNullImage; | |
838 | } | |
839 | // Fill in the DIB header | |
840 | lpDIBh->bmiHeader.biSize = headersize; | |
841 | lpDIBh->bmiHeader.biWidth = width; | |
842 | lpDIBh->bmiHeader.biHeight = -height; | |
843 | lpDIBh->bmiHeader.biSizeImage = bytePerLine * height; | |
844 | lpDIBh->bmiHeader.biPlanes = 1; | |
845 | lpDIBh->bmiHeader.biBitCount = 24; | |
846 | lpDIBh->bmiHeader.biCompression = BI_RGB; | |
847 | lpDIBh->bmiHeader.biClrUsed = 0; | |
848 | // These seem not really needed for our purpose here. | |
849 | lpDIBh->bmiHeader.biClrImportant = 0; | |
850 | lpDIBh->bmiHeader.biXPelsPerMeter = 0; | |
851 | lpDIBh->bmiHeader.biYPelsPerMeter = 0; | |
852 | // memory for DIB data | |
853 | unsigned char *lpBits; | |
854 | lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage ); | |
855 | if( !lpBits ) | |
856 | { | |
857 | wxFAIL_MSG( wxT("could not allocate data for DIB") ); | |
858 | free( data ); | |
859 | free( lpDIBh ); | |
860 | return wxNullImage; | |
861 | } | |
862 | ||
863 | // copy data from the device-dependent bitmap to the DIB | |
864 | HDC hdc = ::GetDC(NULL); | |
865 | HBITMAP hbitmap; | |
866 | hbitmap = (HBITMAP) GetHBITMAP(); | |
867 | ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
868 | ||
869 | // copy DIB data into the wxImage object | |
870 | int i, j; | |
871 | unsigned char *ptdata = data; | |
872 | unsigned char *ptbits = lpBits; | |
873 | for( i=0; i<height; i++ ) | |
874 | { | |
875 | for( j=0; j<width; j++ ) | |
876 | { | |
877 | *(ptdata++) = *(ptbits+2); | |
878 | *(ptdata++) = *(ptbits+1); | |
879 | *(ptdata++) = *(ptbits ); | |
880 | ptbits += 3; | |
881 | } | |
882 | ptbits += padding; | |
883 | } | |
884 | ||
885 | // similarly, set data according to the possible mask bitmap | |
886 | if( GetMask() && GetMask()->GetMaskBitmap() ) | |
887 | { | |
888 | hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); | |
889 | // memory DC created, color set, data copied, and memory DC deleted | |
890 | HDC memdc = ::CreateCompatibleDC( hdc ); | |
891 | ::SetTextColor( memdc, RGB( 0, 0, 0 ) ); | |
892 | ::SetBkColor( memdc, RGB( 255, 255, 255 ) ); | |
893 | ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); | |
894 | ::DeleteDC( memdc ); | |
895 | // background color set to RGB(16,16,16) in consistent with wxGTK | |
896 | unsigned char r=16, g=16, b=16; | |
897 | ptdata = data; | |
898 | ptbits = lpBits; | |
899 | for( i=0; i<height; i++ ) | |
900 | { | |
901 | for( j=0; j<width; j++ ) | |
902 | { | |
903 | if( *ptbits != 0 ) | |
904 | ptdata += 3; | |
905 | else | |
906 | { | |
907 | *(ptdata++) = r; | |
908 | *(ptdata++) = g; | |
909 | *(ptdata++) = b; | |
910 | } | |
911 | ptbits += 3; | |
912 | } | |
913 | ptbits += padding; | |
914 | } | |
915 | image.SetMaskColour( r, g, b ); | |
916 | image.SetMask( TRUE ); | |
917 | } | |
918 | else | |
919 | { | |
920 | image.SetMask( FALSE ); | |
921 | } | |
922 | // free allocated resources | |
923 | ::ReleaseDC(NULL, hdc); | |
924 | free(lpDIBh); | |
925 | free(lpBits); | |
926 | ||
927 | return image; | |
8cb172b4 | 928 | #endif |
fec19ea9 VS |
929 | } |
930 | ||
6d51f220 VZ |
931 | #endif // wxUSE_IMAGE |
932 | ||
debe6624 | 933 | bool wxBitmap::LoadFile(const wxString& filename, long type) |
2bda0e17 | 934 | { |
6d167489 | 935 | UnRef(); |
2bda0e17 | 936 | |
6d167489 | 937 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 938 | |
6d167489 VZ |
939 | if ( handler ) |
940 | { | |
941 | m_refData = new wxBitmapRefData; | |
2bda0e17 | 942 | |
6d167489 VZ |
943 | return handler->LoadFile(this, filename, type, -1, -1); |
944 | } | |
6d51f220 | 945 | #if wxUSE_IMAGE |
6d167489 | 946 | else |
b75dd496 | 947 | { |
6d167489 | 948 | wxImage image; |
6d51f220 VZ |
949 | if ( image.LoadFile( filename, type ) && image.Ok() ) |
950 | { | |
951 | *this = image.ConvertToBitmap(); | |
6d167489 | 952 | |
6d51f220 VZ |
953 | return TRUE; |
954 | } | |
b75dd496 | 955 | } |
6d51f220 VZ |
956 | #endif // wxUSE_IMAGE |
957 | ||
958 | return FALSE; | |
2bda0e17 KB |
959 | } |
960 | ||
debe6624 | 961 | bool wxBitmap::Create(void *data, long type, int width, int height, int depth) |
2bda0e17 | 962 | { |
6d167489 | 963 | UnRef(); |
2bda0e17 | 964 | |
6d167489 | 965 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 966 | |
6d167489 VZ |
967 | if ( !handler ) |
968 | { | |
f6bcfd97 | 969 | wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type); |
2bda0e17 | 970 | |
6d167489 VZ |
971 | return FALSE; |
972 | } | |
1d792928 | 973 | |
6d167489 | 974 | m_refData = new wxBitmapRefData; |
1d792928 | 975 | |
6d167489 | 976 | return handler->Create(this, data, type, width, height, depth); |
2bda0e17 KB |
977 | } |
978 | ||
d275c7eb VZ |
979 | bool wxBitmap::SaveFile(const wxString& filename, |
980 | int type, | |
981 | const wxPalette *palette) | |
2bda0e17 | 982 | { |
6d167489 | 983 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 984 | |
6d167489 VZ |
985 | if ( handler ) |
986 | { | |
987 | return handler->SaveFile(this, filename, type, palette); | |
988 | } | |
6d51f220 | 989 | #if wxUSE_IMAGE |
6d167489 VZ |
990 | else |
991 | { | |
992 | // FIXME what about palette? shouldn't we use it? | |
993 | wxImage image( *this ); | |
6d51f220 VZ |
994 | if ( image.Ok() ) |
995 | { | |
996 | return image.SaveFile(filename, type); | |
997 | } | |
6d167489 | 998 | } |
6d51f220 VZ |
999 | #endif // wxUSE_IMAGE |
1000 | ||
1001 | return FALSE; | |
2bda0e17 KB |
1002 | } |
1003 | ||
4b7f2165 VZ |
1004 | // ---------------------------------------------------------------------------- |
1005 | // sub bitmap extraction | |
1006 | // ---------------------------------------------------------------------------- | |
1007 | ||
1008 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const | |
1009 | { | |
04ef50df | 1010 | #ifndef __WXMICROWIN__ |
4b7f2165 VZ |
1011 | wxCHECK_MSG( Ok() && |
1012 | (rect.x >= 0) && (rect.y >= 0) && | |
1013 | (rect.x+rect.width <= GetWidth()) && | |
1014 | (rect.y+rect.height <= GetHeight()), | |
1015 | wxNullBitmap, wxT("Invalid bitmap or bitmap region") ); | |
1016 | ||
1017 | wxBitmap ret( rect.width, rect.height, GetDepth() ); | |
1018 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
1019 | ||
1020 | // copy bitmap data | |
1021 | HDC dcSrc = ::CreateCompatibleDC(NULL); | |
1022 | HDC dcDst = ::CreateCompatibleDC(NULL); | |
1023 | SelectObject(dcSrc, (HBITMAP) GetHBITMAP()); | |
1024 | SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP()); | |
1025 | BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY); | |
1026 | ||
1027 | // copy mask if there is one | |
1028 | if (GetMask()) | |
1029 | { | |
1030 | HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0); | |
1031 | ||
1032 | SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()); | |
1033 | SelectObject(dcDst, (HBITMAP) hbmpMask); | |
1034 | BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY); | |
1035 | ||
1036 | wxMask *mask = new wxMask((WXHBITMAP) hbmpMask); | |
1037 | ret.SetMask(mask); | |
1038 | } | |
1039 | ||
1040 | SelectObject(dcDst, NULL); | |
1041 | SelectObject(dcSrc, NULL); | |
1042 | DeleteDC(dcDst); | |
1043 | DeleteDC(dcSrc); | |
1044 | ||
1045 | return ret; | |
04ef50df JS |
1046 | #else |
1047 | return wxBitmap(); | |
1048 | #endif | |
4b7f2165 VZ |
1049 | } |
1050 | ||
6d167489 VZ |
1051 | // ---------------------------------------------------------------------------- |
1052 | // wxBitmap accessors | |
1053 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
1054 | |
1055 | void wxBitmap::SetQuality(int q) | |
1056 | { | |
6d167489 | 1057 | EnsureHasData(); |
2bda0e17 | 1058 | |
6d167489 | 1059 | GetBitmapData()->m_quality = q; |
2bda0e17 KB |
1060 | } |
1061 | ||
6d167489 | 1062 | #if WXWIN_COMPATIBILITY_2 |
2bda0e17 KB |
1063 | void wxBitmap::SetOk(bool isOk) |
1064 | { | |
6d167489 | 1065 | EnsureHasData(); |
2bda0e17 | 1066 | |
6d167489 | 1067 | GetBitmapData()->m_ok = isOk; |
2bda0e17 | 1068 | } |
6d167489 | 1069 | #endif // WXWIN_COMPATIBILITY_2 |
2bda0e17 | 1070 | |
d275c7eb VZ |
1071 | #if wxUSE_PALETTE |
1072 | ||
2bda0e17 KB |
1073 | void wxBitmap::SetPalette(const wxPalette& palette) |
1074 | { | |
6d167489 | 1075 | EnsureHasData(); |
2bda0e17 | 1076 | |
6d167489 | 1077 | GetBitmapData()->m_bitmapPalette = palette; |
2bda0e17 KB |
1078 | } |
1079 | ||
d275c7eb VZ |
1080 | #endif // wxUSE_PALETTE |
1081 | ||
2bda0e17 KB |
1082 | void wxBitmap::SetMask(wxMask *mask) |
1083 | { | |
6d167489 | 1084 | EnsureHasData(); |
2bda0e17 | 1085 | |
6d167489 | 1086 | GetBitmapData()->m_bitmapMask = mask; |
2bda0e17 KB |
1087 | } |
1088 | ||
7b46ecac JS |
1089 | // Creates a bitmap that matches the device context, from |
1090 | // an arbitray bitmap. At present, the original bitmap must have an | |
1091 | // associated palette. TODO: use a default palette if no palette exists. | |
1092 | // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com> | |
1093 | wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const | |
1094 | { | |
04ef50df | 1095 | #ifdef __WXMICROWIN__ |
54a96d02 | 1096 | return *this; |
04ef50df | 1097 | #else |
7b46ecac | 1098 | wxMemoryDC memDC; |
4b7f2165 | 1099 | wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth()); |
57c208c5 | 1100 | HPALETTE hPal = (HPALETTE) NULL; |
7b46ecac | 1101 | LPBITMAPINFO lpDib; |
57c208c5 | 1102 | void *lpBits = (void*) NULL; |
7b46ecac | 1103 | |
d275c7eb | 1104 | #if wxUSE_PALETTE |
6d167489 | 1105 | if( GetPalette() && GetPalette()->Ok() ) |
a367b9b3 | 1106 | { |
6d167489 | 1107 | tmpBitmap.SetPalette(*GetPalette()); |
a367b9b3 | 1108 | memDC.SelectObject(tmpBitmap); |
6d167489 VZ |
1109 | memDC.SetPalette(*GetPalette()); |
1110 | hPal = (HPALETTE)GetPalette()->GetHPALETTE(); | |
a367b9b3 JS |
1111 | } |
1112 | else | |
1113 | { | |
1114 | hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); | |
1115 | wxPalette palette; | |
1116 | palette.SetHPALETTE( (WXHPALETTE)hPal ); | |
1117 | tmpBitmap.SetPalette( palette ); | |
1118 | memDC.SelectObject(tmpBitmap); | |
1119 | memDC.SetPalette( palette ); | |
1120 | } | |
d275c7eb VZ |
1121 | #else // !wxUSE_PALETTE |
1122 | hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); | |
1123 | #endif // wxUSE_PALETTE/!wxUSE_PALETTE | |
7b46ecac | 1124 | |
6d167489 VZ |
1125 | // set the height negative because in a DIB the order of the lines is |
1126 | // reversed | |
1127 | if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) ) | |
1128 | { | |
1129 | return wxNullBitmap; | |
1130 | } | |
7b46ecac JS |
1131 | |
1132 | lpBits = malloc(lpDib->bmiHeader.biSizeImage); | |
1133 | ||
6d167489 | 1134 | ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits); |
7b46ecac | 1135 | |
6d167489 VZ |
1136 | ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0, |
1137 | GetWidth(), GetHeight(), | |
1138 | 0, 0, 0, GetHeight(), | |
1139 | lpBits, lpDib, DIB_RGB_COLORS); | |
7b46ecac JS |
1140 | |
1141 | free(lpBits); | |
1142 | ||
6d167489 VZ |
1143 | wxFreeDIB(lpDib); |
1144 | ||
1145 | return tmpBitmap; | |
04ef50df | 1146 | #endif |
7b46ecac JS |
1147 | } |
1148 | ||
6d167489 VZ |
1149 | // ---------------------------------------------------------------------------- |
1150 | // wxMask | |
1151 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1152 | |
10fcf31a | 1153 | wxMask::wxMask() |
2bda0e17 KB |
1154 | { |
1155 | m_maskBitmap = 0; | |
1156 | } | |
1157 | ||
1158 | // Construct a mask from a bitmap and a colour indicating | |
1159 | // the transparent area | |
1160 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
1161 | { | |
1162 | m_maskBitmap = 0; | |
6d167489 | 1163 | Create(bitmap, colour); |
2bda0e17 KB |
1164 | } |
1165 | ||
1166 | // Construct a mask from a bitmap and a palette index indicating | |
1167 | // the transparent area | |
debe6624 | 1168 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
1169 | { |
1170 | m_maskBitmap = 0; | |
6d167489 | 1171 | Create(bitmap, paletteIndex); |
2bda0e17 KB |
1172 | } |
1173 | ||
1174 | // Construct a mask from a mono bitmap (copies the bitmap). | |
1175 | wxMask::wxMask(const wxBitmap& bitmap) | |
1176 | { | |
1177 | m_maskBitmap = 0; | |
6d167489 | 1178 | Create(bitmap); |
2bda0e17 KB |
1179 | } |
1180 | ||
10fcf31a | 1181 | wxMask::~wxMask() |
2bda0e17 KB |
1182 | { |
1183 | if ( m_maskBitmap ) | |
1184 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
1185 | } | |
1186 | ||
1187 | // Create a mask from a mono bitmap (copies the bitmap). | |
1188 | bool wxMask::Create(const wxBitmap& bitmap) | |
1189 | { | |
04ef50df | 1190 | #ifndef __WXMICROWIN__ |
a58a12e9 VZ |
1191 | wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE, |
1192 | _T("can't create mask from invalid or not monochrome bitmap") ); | |
1193 | ||
2bda0e17 | 1194 | if ( m_maskBitmap ) |
6d167489 VZ |
1195 | { |
1196 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
1197 | m_maskBitmap = 0; | |
1198 | } | |
a58a12e9 | 1199 | |
6d167489 VZ |
1200 | m_maskBitmap = (WXHBITMAP) CreateBitmap( |
1201 | bitmap.GetWidth(), | |
1202 | bitmap.GetHeight(), | |
1203 | 1, 1, 0 | |
1204 | ); | |
1205 | HDC srcDC = CreateCompatibleDC(0); | |
1206 | SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
1207 | HDC destDC = CreateCompatibleDC(0); | |
1208 | SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
1209 | BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY); | |
1210 | SelectObject(srcDC, 0); | |
1211 | DeleteDC(srcDC); | |
1212 | SelectObject(destDC, 0); | |
1213 | DeleteDC(destDC); | |
1214 | return TRUE; | |
04ef50df JS |
1215 | #else |
1216 | return FALSE; | |
1217 | #endif | |
2bda0e17 KB |
1218 | } |
1219 | ||
1220 | // Create a mask from a bitmap and a palette index indicating | |
1221 | // the transparent area | |
debe6624 | 1222 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
1223 | { |
1224 | if ( m_maskBitmap ) | |
1d792928 | 1225 | { |
6d167489 VZ |
1226 | ::DeleteObject((HBITMAP) m_maskBitmap); |
1227 | m_maskBitmap = 0; | |
1d792928 | 1228 | } |
d275c7eb VZ |
1229 | |
1230 | #if wxUSE_PALETTE | |
6d167489 VZ |
1231 | if (bitmap.Ok() && bitmap.GetPalette()->Ok()) |
1232 | { | |
1233 | unsigned char red, green, blue; | |
1234 | if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue)) | |
1235 | { | |
1236 | wxColour transparentColour(red, green, blue); | |
1237 | return Create(bitmap, transparentColour); | |
1238 | } | |
1239 | } | |
d275c7eb VZ |
1240 | #endif // wxUSE_PALETTE |
1241 | ||
6d167489 | 1242 | return FALSE; |
2bda0e17 KB |
1243 | } |
1244 | ||
1245 | // Create a mask from a bitmap and a colour indicating | |
1246 | // the transparent area | |
1247 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
1248 | { | |
04ef50df | 1249 | #ifndef __WXMICROWIN__ |
4b7f2165 VZ |
1250 | wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") ); |
1251 | ||
2bda0e17 | 1252 | if ( m_maskBitmap ) |
1d792928 | 1253 | { |
6d167489 VZ |
1254 | ::DeleteObject((HBITMAP) m_maskBitmap); |
1255 | m_maskBitmap = 0; | |
1256 | } | |
4b7f2165 VZ |
1257 | |
1258 | int width = bitmap.GetWidth(), | |
1259 | height = bitmap.GetHeight(); | |
1260 | ||
1261 | // scan the bitmap for the transparent colour and set the corresponding | |
1262 | // pixels in the mask to BLACK and the rest to WHITE | |
25acfd4c | 1263 | COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue()); |
4b7f2165 VZ |
1264 | m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0); |
1265 | ||
1266 | HDC srcDC = ::CreateCompatibleDC(NULL); | |
1267 | HDC destDC = ::CreateCompatibleDC(NULL); | |
1268 | if ( !srcDC || !destDC ) | |
1269 | { | |
f6bcfd97 | 1270 | wxLogLastError(wxT("CreateCompatibleDC")); |
4b7f2165 VZ |
1271 | } |
1272 | ||
0bafad0c VZ |
1273 | bool ok = TRUE; |
1274 | ||
1e6feb95 VZ |
1275 | // SelectObject() will fail |
1276 | wxASSERT_MSG( !bitmap.GetSelectedInto(), | |
1277 | _T("bitmap can't be selected in another DC") ); | |
1278 | ||
0bafad0c VZ |
1279 | HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap)); |
1280 | if ( !hbmpSrcOld ) | |
6d167489 | 1281 | { |
f6bcfd97 | 1282 | wxLogLastError(wxT("SelectObject")); |
0bafad0c VZ |
1283 | |
1284 | ok = FALSE; | |
4b7f2165 | 1285 | } |
0bafad0c VZ |
1286 | |
1287 | HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap); | |
1288 | if ( !hbmpDstOld ) | |
4b7f2165 | 1289 | { |
f6bcfd97 | 1290 | wxLogLastError(wxT("SelectObject")); |
0bafad0c VZ |
1291 | |
1292 | ok = FALSE; | |
1d792928 | 1293 | } |
2bda0e17 | 1294 | |
59ff46cb | 1295 | if ( ok ) |
2bda0e17 | 1296 | { |
59ff46cb VZ |
1297 | // this will create a monochrome bitmap with 0 points for the pixels |
1298 | // which have the same value as the background colour and 1 for the | |
1299 | // others | |
1300 | ::SetBkColor(srcDC, maskColour); | |
1301 | ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY); | |
2bda0e17 | 1302 | } |
4b7f2165 | 1303 | |
0bafad0c | 1304 | ::SelectObject(srcDC, hbmpSrcOld); |
6d167489 | 1305 | ::DeleteDC(srcDC); |
0bafad0c | 1306 | ::SelectObject(destDC, hbmpDstOld); |
6d167489 | 1307 | ::DeleteDC(destDC); |
4b7f2165 | 1308 | |
0bafad0c | 1309 | return ok; |
59ff46cb | 1310 | #else // __WXMICROWIN__ |
04ef50df | 1311 | return FALSE; |
59ff46cb | 1312 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
6d167489 VZ |
1313 | } |
1314 | ||
1315 | // ---------------------------------------------------------------------------- | |
1316 | // wxBitmapHandler | |
1317 | // ---------------------------------------------------------------------------- | |
1d792928 | 1318 | |
6d167489 VZ |
1319 | bool wxBitmapHandler::Create(wxGDIImage *image, |
1320 | void *data, | |
1321 | long flags, | |
1322 | int width, int height, int depth) | |
1323 | { | |
1324 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); | |
1d792928 | 1325 | |
8c1375b9 | 1326 | return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE; |
2bda0e17 KB |
1327 | } |
1328 | ||
6d167489 VZ |
1329 | bool wxBitmapHandler::Load(wxGDIImage *image, |
1330 | const wxString& name, | |
1331 | long flags, | |
1332 | int width, int height) | |
2bda0e17 | 1333 | { |
6d167489 | 1334 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
2bda0e17 | 1335 | |
6d167489 VZ |
1336 | return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE; |
1337 | } | |
2bda0e17 | 1338 | |
6d167489 VZ |
1339 | bool wxBitmapHandler::Save(wxGDIImage *image, |
1340 | const wxString& name, | |
1341 | int type) | |
2bda0e17 | 1342 | { |
6d167489 VZ |
1343 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
1344 | ||
1345 | return bitmap ? SaveFile(bitmap, name, type) : FALSE; | |
2bda0e17 KB |
1346 | } |
1347 | ||
6d167489 VZ |
1348 | bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), |
1349 | void *WXUNUSED(data), | |
1350 | long WXUNUSED(type), | |
1351 | int WXUNUSED(width), | |
1352 | int WXUNUSED(height), | |
1353 | int WXUNUSED(depth)) | |
2bda0e17 | 1354 | { |
6d167489 | 1355 | return FALSE; |
2bda0e17 KB |
1356 | } |
1357 | ||
6d167489 VZ |
1358 | bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), |
1359 | const wxString& WXUNUSED(name), | |
1360 | long WXUNUSED(type), | |
1361 | int WXUNUSED(desiredWidth), | |
1362 | int WXUNUSED(desiredHeight)) | |
2bda0e17 | 1363 | { |
6d167489 | 1364 | return FALSE; |
2bda0e17 KB |
1365 | } |
1366 | ||
6d167489 VZ |
1367 | bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), |
1368 | const wxString& WXUNUSED(name), | |
1369 | int WXUNUSED(type), | |
1370 | const wxPalette *WXUNUSED(palette)) | |
2bda0e17 | 1371 | { |
6d167489 | 1372 | return FALSE; |
7b46ecac JS |
1373 | } |
1374 | ||
6d167489 VZ |
1375 | // ---------------------------------------------------------------------------- |
1376 | // DIB functions | |
1377 | // ---------------------------------------------------------------------------- | |
1378 | ||
04ef50df | 1379 | #ifndef __WXMICROWIN__ |
6d167489 VZ |
1380 | bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, |
1381 | HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) | |
7b46ecac JS |
1382 | { |
1383 | unsigned long i, headerSize; | |
1384 | LPBITMAPINFO lpDIBheader = NULL; | |
1385 | LPPALETTEENTRY lpPe = NULL; | |
1386 | ||
1387 | ||
1388 | // Allocate space for a DIB header | |
1389 | headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); | |
1390 | lpDIBheader = (BITMAPINFO *) malloc(headerSize); | |
1391 | lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); | |
1392 | ||
1393 | GetPaletteEntries(hPal, 0, 256, lpPe); | |
1394 | ||
7b46ecac JS |
1395 | memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER)); |
1396 | ||
7b46ecac JS |
1397 | // Fill in the static parts of the DIB header |
1398 | lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
1399 | lpDIBheader->bmiHeader.biWidth = xSize; | |
1400 | lpDIBheader->bmiHeader.biHeight = ySize; | |
1401 | lpDIBheader->bmiHeader.biPlanes = 1; | |
1402 | ||
1403 | // this value must be 1, 4, 8 or 24 so PixelDepth can only be | |
1404 | lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); | |
1405 | lpDIBheader->bmiHeader.biCompression = BI_RGB; | |
6d167489 | 1406 | lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3; |
7b46ecac JS |
1407 | lpDIBheader->bmiHeader.biClrUsed = 256; |
1408 | ||
1409 | ||
1410 | // Initialize the DIB palette | |
1411 | for (i = 0; i < 256; i++) { | |
1412 | lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags; | |
1413 | lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed; | |
1414 | lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen; | |
1415 | lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue; | |
1416 | } | |
1417 | ||
1418 | *lpDIBHeader = lpDIBheader; | |
1419 | ||
6d167489 | 1420 | return TRUE; |
7b46ecac JS |
1421 | } |
1422 | ||
6d167489 | 1423 | void wxFreeDIB(LPBITMAPINFO lpDIBHeader) |
7b46ecac | 1424 | { |
6d167489 | 1425 | free(lpDIBHeader); |
7b46ecac | 1426 | } |
04ef50df | 1427 | #endif |
7b46ecac | 1428 | |
4b7f2165 VZ |
1429 | // ---------------------------------------------------------------------------- |
1430 | // other helper functions | |
1431 | // ---------------------------------------------------------------------------- | |
1432 | ||
1433 | extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) | |
1434 | { | |
04ef50df | 1435 | #ifndef __WXMICROWIN__ |
4b7f2165 VZ |
1436 | wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") ); |
1437 | ||
1438 | // get width/height from the bitmap if not given | |
1439 | if ( !w || !h ) | |
1440 | { | |
1441 | BITMAP bm; | |
1442 | ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm); | |
1443 | w = bm.bmWidth; | |
1444 | h = bm.bmHeight; | |
1445 | } | |
1446 | ||
1447 | HDC hdcSrc = ::CreateCompatibleDC(NULL); | |
1448 | HDC hdcDst = ::CreateCompatibleDC(NULL); | |
1449 | if ( !hdcSrc || !hdcDst ) | |
1450 | { | |
f6bcfd97 | 1451 | wxLogLastError(wxT("CreateCompatibleDC")); |
4b7f2165 VZ |
1452 | } |
1453 | ||
1454 | HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0); | |
1455 | if ( !hbmpInvMask ) | |
1456 | { | |
f6bcfd97 | 1457 | wxLogLastError(wxT("CreateBitmap")); |
4b7f2165 VZ |
1458 | } |
1459 | ||
1460 | ::SelectObject(hdcSrc, hbmpMask); | |
1461 | ::SelectObject(hdcDst, hbmpInvMask); | |
1462 | if ( !::BitBlt(hdcDst, 0, 0, w, h, | |
1463 | hdcSrc, 0, 0, | |
1464 | NOTSRCCOPY) ) | |
1465 | { | |
f6bcfd97 | 1466 | wxLogLastError(wxT("BitBlt")); |
4b7f2165 | 1467 | } |
7b46ecac | 1468 | |
4b7f2165 VZ |
1469 | ::DeleteDC(hdcSrc); |
1470 | ::DeleteDC(hdcDst); | |
1471 | ||
1472 | return hbmpInvMask; | |
04ef50df JS |
1473 | #else |
1474 | return 0; | |
1475 | #endif | |
4b7f2165 | 1476 | } |