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