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