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