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