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