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