]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
3 | // Purpose: wxBitmap | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
1d792928 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
10fcf31a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
10fcf31a | 21 | #pragma implementation "bitmap.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
10fcf31a | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
10fcf31a VZ |
32 | #include <stdio.h> |
33 | ||
34 | #include "wx/list.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/app.h" | |
37 | #include "wx/palette.h" | |
38 | #include "wx/dcmemory.h" | |
39 | #include "wx/bitmap.h" | |
40 | #include "wx/icon.h" | |
2bda0e17 KB |
41 | #endif |
42 | ||
43 | #include "wx/msw/private.h" | |
1d792928 VZ |
44 | #include "wx/log.h" |
45 | ||
2bda0e17 | 46 | #include "wx/msw/dib.h" |
b75dd496 | 47 | #include "wx/image.h" |
2bda0e17 | 48 | |
10fcf31a VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
10fcf31a VZ |
53 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) |
54 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
6d167489 VZ |
55 | |
56 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) | |
2bda0e17 | 57 | |
10fcf31a VZ |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // wxBitmapRefData | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | wxBitmapRefData::wxBitmapRefData() | |
2bda0e17 | 67 | { |
6d167489 VZ |
68 | m_quality = 0; |
69 | m_selectedInto = NULL; | |
70 | m_numColors = 0; | |
71 | m_bitmapMask = NULL; | |
340196c0 | 72 | m_hBitmap = (WXHBITMAP) NULL; |
2bda0e17 KB |
73 | } |
74 | ||
6d167489 | 75 | void wxBitmapRefData::Free() |
2bda0e17 | 76 | { |
d59ceba5 VZ |
77 | wxASSERT_MSG( !m_selectedInto, |
78 | wxT("deleting bitmap still selected into wxMemoryDC") ); | |
2bda0e17 | 79 | |
d59ceba5 | 80 | if ( m_hBitmap) |
6d167489 VZ |
81 | { |
82 | if ( !::DeleteObject((HBITMAP)m_hBitmap) ) | |
83 | { | |
84 | wxLogLastError("DeleteObject(hbitmap)"); | |
85 | } | |
86 | } | |
e7003166 | 87 | |
6d167489 VZ |
88 | delete m_bitmapMask; |
89 | m_bitmapMask = NULL; | |
2bda0e17 KB |
90 | } |
91 | ||
10fcf31a | 92 | // ---------------------------------------------------------------------------- |
6d167489 | 93 | // wxBitmap creation |
10fcf31a VZ |
94 | // ---------------------------------------------------------------------------- |
95 | ||
4fe5383d VZ |
96 | // this function should be called from all wxBitmap ctors |
97 | void wxBitmap::Init() | |
2bda0e17 | 98 | { |
4fe5383d | 99 | // m_refData = NULL; done in the base class ctor |
2bda0e17 | 100 | |
07cf98cb VZ |
101 | if ( wxTheBitmapList ) |
102 | wxTheBitmapList->AddBitmap(this); | |
4fe5383d VZ |
103 | } |
104 | ||
6d167489 VZ |
105 | #ifdef __WIN32__ |
106 | ||
107 | bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) | |
108 | { | |
109 | // it may be either HICON or HCURSOR | |
110 | HICON hicon = (HICON)icon.GetHandle(); | |
111 | ||
112 | ICONINFO iconInfo; | |
113 | if ( !::GetIconInfo(hicon, &iconInfo) ) | |
114 | { | |
115 | wxLogLastError("GetIconInfo"); | |
116 | ||
117 | return FALSE; | |
118 | } | |
119 | ||
120 | wxBitmapRefData *refData = new wxBitmapRefData; | |
121 | m_refData = refData; | |
122 | ||
d9c8e68e VZ |
123 | int w = icon.GetWidth(), |
124 | h = icon.GetHeight(); | |
125 | ||
126 | refData->m_width = w; | |
127 | refData->m_height = h; | |
6d167489 VZ |
128 | refData->m_depth = wxDisplayDepth(); |
129 | ||
130 | refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor; | |
d9c8e68e VZ |
131 | |
132 | // the mask returned by GetIconInfo() is inversed compared to the usual | |
133 | // wxWin convention | |
134 | HBITMAP hbmpMask = ::CreateBitmap(w, h, 1, 1, 0); | |
135 | ||
136 | // the icons mask is opposite to the usual wxWin convention | |
137 | HDC dcSrc = ::CreateCompatibleDC(NULL); | |
138 | HDC dcDst = ::CreateCompatibleDC(NULL); | |
139 | (void)SelectObject(dcSrc, iconInfo.hbmMask); | |
140 | (void)SelectObject(dcDst, hbmpMask); | |
141 | ||
142 | HBRUSH brush = ::CreateSolidBrush(RGB(255, 255, 255)); | |
143 | RECT rect = { 0, 0, w, h }; | |
144 | FillRect(dcDst, &rect, brush); | |
145 | ||
146 | BitBlt(dcDst, 0, 0, w, h, dcSrc, 0, 0, SRCINVERT); | |
147 | ||
148 | SelectObject(dcDst, NULL); | |
149 | SelectObject(dcSrc, NULL); | |
150 | DeleteDC(dcDst); | |
151 | DeleteDC(dcSrc); | |
152 | ||
153 | refData->m_bitmapMask = new wxMask((WXHBITMAP)hbmpMask); | |
6d167489 VZ |
154 | |
155 | #if WXWIN_COMPATIBILITY_2 | |
156 | refData->m_ok = TRUE; | |
157 | #endif // WXWIN_COMPATIBILITY_2 | |
158 | ||
159 | return TRUE; | |
160 | } | |
161 | ||
162 | #endif // Win32 | |
163 | ||
164 | bool wxBitmap::CopyFromCursor(const wxCursor& cursor) | |
4fe5383d VZ |
165 | { |
166 | UnRef(); | |
07cf98cb | 167 | |
6d167489 | 168 | if ( !cursor.Ok() ) |
4fe5383d | 169 | return FALSE; |
07cf98cb | 170 | |
6d167489 VZ |
171 | #ifdef __WIN16__ |
172 | wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") ); | |
173 | ||
174 | return FALSE; | |
8f177c8e | 175 | #else |
6d167489 | 176 | return CopyFromIconOrCursor(cursor); |
8f177c8e | 177 | #endif // Win16 |
6d167489 VZ |
178 | } |
179 | ||
180 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
181 | { | |
182 | UnRef(); | |
07cf98cb | 183 | |
6d167489 VZ |
184 | if ( !icon.Ok() ) |
185 | return FALSE; | |
4fe5383d VZ |
186 | |
187 | // GetIconInfo() doesn't exist under Win16 and I don't know any other way | |
188 | // to create a bitmap from icon there - but using this way we won't have | |
189 | // the mask (FIXME) | |
190 | #ifdef __WIN16__ | |
6d167489 VZ |
191 | int width = icon.GetWidth(), |
192 | height = icon.GetHeight(); | |
193 | ||
4fe5383d | 194 | // copy the icon to the bitmap |
6d167489 | 195 | ScreenHDC hdcScreen; |
4fe5383d VZ |
196 | HDC hdc = ::CreateCompatibleDC(hdcScreen); |
197 | HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height); | |
07cf98cb VZ |
198 | HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap); |
199 | ||
6d167489 | 200 | ::DrawIcon(hdc, 0, 0, GetHiconOf(icon)); |
07cf98cb VZ |
201 | |
202 | ::SelectObject(hdc, hbmpOld); | |
203 | ::DeleteDC(hdc); | |
4fe5383d | 204 | |
6d167489 VZ |
205 | wxBitmapRefData *refData = new wxBitmapRefData; |
206 | m_refData = refData; | |
4fe5383d | 207 | |
6d167489 VZ |
208 | refData->m_width = width; |
209 | refData->m_height = height; | |
210 | refData->m_depth = wxDisplayDepth(); | |
07cf98cb | 211 | |
6d167489 | 212 | refData->m_hBitmap = (WXHBITMAP)hbitmap; |
07cf98cb | 213 | |
6d167489 VZ |
214 | #if WXWIN_COMPATIBILITY_2 |
215 | refData->m_ok = TRUE; | |
216 | #endif // WXWIN_COMPATIBILITY_2 | |
222594ea | 217 | |
4fe5383d | 218 | return TRUE; |
6d167489 VZ |
219 | #else // Win32 |
220 | return CopyFromIconOrCursor(icon); | |
221 | #endif // Win16/Win32 | |
10fcf31a VZ |
222 | } |
223 | ||
224 | wxBitmap::~wxBitmap() | |
2bda0e17 KB |
225 | { |
226 | if (wxTheBitmapList) | |
227 | wxTheBitmapList->DeleteObject(this); | |
228 | } | |
229 | ||
debe6624 | 230 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) |
2bda0e17 | 231 | { |
4fe5383d | 232 | Init(); |
2bda0e17 | 233 | |
6d167489 VZ |
234 | wxBitmapRefData *refData = new wxBitmapRefData; |
235 | m_refData = refData; | |
2bda0e17 | 236 | |
6d167489 VZ |
237 | refData->m_width = the_width; |
238 | refData->m_height = the_height; | |
239 | refData->m_depth = no_bits; | |
240 | refData->m_numColors = 0; | |
241 | refData->m_selectedInto = NULL; | |
2bda0e17 | 242 | |
6d167489 VZ |
243 | HBITMAP hbmp = ::CreateBitmap(the_width, the_height, 1, no_bits, bits); |
244 | if ( !hbmp ) | |
245 | { | |
246 | wxLogLastError("CreateBitmap"); | |
247 | } | |
2bda0e17 | 248 | |
6d167489 | 249 | SetHBITMAP((WXHBITMAP)hbmp); |
2bda0e17 KB |
250 | } |
251 | ||
2fd284a4 | 252 | // Create from XPM data |
03ab016d | 253 | wxBitmap::wxBitmap(char **data, wxControl *WXUNUSED(anItem)) |
2fd284a4 | 254 | { |
4fe5383d VZ |
255 | Init(); |
256 | ||
257 | (void)Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); | |
2fd284a4 JS |
258 | } |
259 | ||
debe6624 | 260 | wxBitmap::wxBitmap(int w, int h, int d) |
2bda0e17 | 261 | { |
4fe5383d | 262 | Init(); |
2bda0e17 | 263 | |
4fe5383d | 264 | (void)Create(w, h, d); |
2bda0e17 KB |
265 | } |
266 | ||
debe6624 | 267 | wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) |
2bda0e17 | 268 | { |
4fe5383d | 269 | Init(); |
2bda0e17 | 270 | |
6d167489 | 271 | (void)Create(data, type, width, height, depth); |
2bda0e17 KB |
272 | } |
273 | ||
debe6624 | 274 | wxBitmap::wxBitmap(const wxString& filename, long type) |
2bda0e17 | 275 | { |
4fe5383d | 276 | Init(); |
2bda0e17 | 277 | |
4fe5383d | 278 | LoadFile(filename, (int)type); |
2bda0e17 KB |
279 | } |
280 | ||
debe6624 | 281 | bool wxBitmap::Create(int w, int h, int d) |
2bda0e17 | 282 | { |
6d167489 VZ |
283 | UnRef(); |
284 | ||
285 | m_refData = new wxBitmapRefData; | |
286 | ||
287 | GetBitmapData()->m_width = w; | |
288 | GetBitmapData()->m_height = h; | |
289 | GetBitmapData()->m_depth = d; | |
290 | ||
291 | HBITMAP hbmp; | |
292 | ||
293 | if ( d > 0 ) | |
294 | { | |
295 | hbmp = ::CreateBitmap(w, h, 1, d, NULL); | |
296 | if ( !hbmp ) | |
297 | { | |
298 | wxLogLastError("CreateBitmap"); | |
299 | } | |
300 | } | |
301 | else | |
302 | { | |
303 | ScreenHDC dc; | |
304 | hbmp = ::CreateCompatibleBitmap(dc, w, h); | |
305 | if ( !hbmp ) | |
306 | { | |
307 | wxLogLastError("CreateCompatibleBitmap"); | |
308 | } | |
309 | ||
310 | GetBitmapData()->m_depth = wxDisplayDepth(); | |
311 | } | |
2bda0e17 | 312 | |
6d167489 | 313 | SetHBITMAP((WXHBITMAP)hbmp); |
2bda0e17 | 314 | |
6d167489 VZ |
315 | #if WXWIN_COMPATIBILITY_2 |
316 | GetBitmapData()->m_ok = hbmp != 0; | |
317 | #endif // WXWIN_COMPATIBILITY_2 | |
2bda0e17 | 318 | |
6d167489 | 319 | return Ok(); |
2bda0e17 KB |
320 | } |
321 | ||
debe6624 | 322 | bool wxBitmap::LoadFile(const wxString& filename, long type) |
2bda0e17 | 323 | { |
6d167489 | 324 | UnRef(); |
2bda0e17 | 325 | |
6d167489 | 326 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 327 | |
6d167489 VZ |
328 | if ( handler ) |
329 | { | |
330 | m_refData = new wxBitmapRefData; | |
2bda0e17 | 331 | |
6d167489 VZ |
332 | return handler->LoadFile(this, filename, type, -1, -1); |
333 | } | |
334 | else | |
b75dd496 | 335 | { |
6d167489 VZ |
336 | wxImage image; |
337 | if ( !image.LoadFile( filename, type ) || !image.Ok() ) | |
338 | return FALSE; | |
339 | ||
b75dd496 | 340 | *this = image.ConvertToBitmap(); |
6d167489 | 341 | |
b75dd496 VS |
342 | return TRUE; |
343 | } | |
2bda0e17 KB |
344 | } |
345 | ||
debe6624 | 346 | bool wxBitmap::Create(void *data, long type, int width, int height, int depth) |
2bda0e17 | 347 | { |
6d167489 | 348 | UnRef(); |
2bda0e17 | 349 | |
6d167489 | 350 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 351 | |
6d167489 VZ |
352 | if ( !handler ) |
353 | { | |
354 | wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for " | |
355 | "type %d defined."), type); | |
2bda0e17 | 356 | |
6d167489 VZ |
357 | return FALSE; |
358 | } | |
1d792928 | 359 | |
6d167489 | 360 | m_refData = new wxBitmapRefData; |
1d792928 | 361 | |
6d167489 | 362 | return handler->Create(this, data, type, width, height, depth); |
2bda0e17 KB |
363 | } |
364 | ||
debe6624 | 365 | bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) |
2bda0e17 | 366 | { |
6d167489 | 367 | wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); |
2bda0e17 | 368 | |
6d167489 VZ |
369 | if ( handler ) |
370 | { | |
371 | return handler->SaveFile(this, filename, type, palette); | |
372 | } | |
373 | else | |
374 | { | |
375 | // FIXME what about palette? shouldn't we use it? | |
376 | wxImage image( *this ); | |
377 | if (!image.Ok()) | |
378 | return FALSE; | |
2bda0e17 | 379 | |
6d167489 VZ |
380 | return image.SaveFile( filename, type ); |
381 | } | |
2bda0e17 KB |
382 | } |
383 | ||
6d167489 VZ |
384 | // ---------------------------------------------------------------------------- |
385 | // wxBitmap accessors | |
386 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
387 | |
388 | void wxBitmap::SetQuality(int q) | |
389 | { | |
6d167489 | 390 | EnsureHasData(); |
2bda0e17 | 391 | |
6d167489 | 392 | GetBitmapData()->m_quality = q; |
2bda0e17 KB |
393 | } |
394 | ||
6d167489 | 395 | #if WXWIN_COMPATIBILITY_2 |
2bda0e17 KB |
396 | void wxBitmap::SetOk(bool isOk) |
397 | { | |
6d167489 | 398 | EnsureHasData(); |
2bda0e17 | 399 | |
6d167489 | 400 | GetBitmapData()->m_ok = isOk; |
2bda0e17 | 401 | } |
6d167489 | 402 | #endif // WXWIN_COMPATIBILITY_2 |
2bda0e17 KB |
403 | |
404 | void wxBitmap::SetPalette(const wxPalette& palette) | |
405 | { | |
6d167489 | 406 | EnsureHasData(); |
2bda0e17 | 407 | |
6d167489 | 408 | GetBitmapData()->m_bitmapPalette = palette; |
2bda0e17 KB |
409 | } |
410 | ||
411 | void wxBitmap::SetMask(wxMask *mask) | |
412 | { | |
6d167489 | 413 | EnsureHasData(); |
2bda0e17 | 414 | |
6d167489 | 415 | GetBitmapData()->m_bitmapMask = mask; |
2bda0e17 KB |
416 | } |
417 | ||
7b46ecac JS |
418 | // Creates a bitmap that matches the device context, from |
419 | // an arbitray bitmap. At present, the original bitmap must have an | |
420 | // associated palette. TODO: use a default palette if no palette exists. | |
421 | // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com> | |
422 | wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const | |
423 | { | |
424 | wxMemoryDC memDC; | |
425 | wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth()); | |
57c208c5 | 426 | HPALETTE hPal = (HPALETTE) NULL; |
7b46ecac | 427 | LPBITMAPINFO lpDib; |
57c208c5 | 428 | void *lpBits = (void*) NULL; |
7b46ecac | 429 | |
6d167489 | 430 | if( GetPalette() && GetPalette()->Ok() ) |
a367b9b3 | 431 | { |
6d167489 | 432 | tmpBitmap.SetPalette(*GetPalette()); |
a367b9b3 | 433 | memDC.SelectObject(tmpBitmap); |
6d167489 VZ |
434 | memDC.SetPalette(*GetPalette()); |
435 | hPal = (HPALETTE)GetPalette()->GetHPALETTE(); | |
a367b9b3 JS |
436 | } |
437 | else | |
438 | { | |
439 | hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); | |
440 | wxPalette palette; | |
441 | palette.SetHPALETTE( (WXHPALETTE)hPal ); | |
442 | tmpBitmap.SetPalette( palette ); | |
443 | memDC.SelectObject(tmpBitmap); | |
444 | memDC.SetPalette( palette ); | |
445 | } | |
7b46ecac | 446 | |
6d167489 VZ |
447 | // set the height negative because in a DIB the order of the lines is |
448 | // reversed | |
449 | if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) ) | |
450 | { | |
451 | return wxNullBitmap; | |
452 | } | |
7b46ecac JS |
453 | |
454 | lpBits = malloc(lpDib->bmiHeader.biSizeImage); | |
455 | ||
6d167489 | 456 | ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits); |
7b46ecac | 457 | |
6d167489 VZ |
458 | ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0, |
459 | GetWidth(), GetHeight(), | |
460 | 0, 0, 0, GetHeight(), | |
461 | lpBits, lpDib, DIB_RGB_COLORS); | |
7b46ecac JS |
462 | |
463 | free(lpBits); | |
464 | ||
6d167489 VZ |
465 | wxFreeDIB(lpDib); |
466 | ||
467 | return tmpBitmap; | |
7b46ecac JS |
468 | } |
469 | ||
6d167489 VZ |
470 | // ---------------------------------------------------------------------------- |
471 | // wxMask | |
472 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 473 | |
10fcf31a | 474 | wxMask::wxMask() |
2bda0e17 KB |
475 | { |
476 | m_maskBitmap = 0; | |
477 | } | |
478 | ||
479 | // Construct a mask from a bitmap and a colour indicating | |
480 | // the transparent area | |
481 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
482 | { | |
483 | m_maskBitmap = 0; | |
6d167489 | 484 | Create(bitmap, colour); |
2bda0e17 KB |
485 | } |
486 | ||
487 | // Construct a mask from a bitmap and a palette index indicating | |
488 | // the transparent area | |
debe6624 | 489 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
490 | { |
491 | m_maskBitmap = 0; | |
6d167489 | 492 | Create(bitmap, paletteIndex); |
2bda0e17 KB |
493 | } |
494 | ||
495 | // Construct a mask from a mono bitmap (copies the bitmap). | |
496 | wxMask::wxMask(const wxBitmap& bitmap) | |
497 | { | |
498 | m_maskBitmap = 0; | |
6d167489 | 499 | Create(bitmap); |
2bda0e17 KB |
500 | } |
501 | ||
10fcf31a | 502 | wxMask::~wxMask() |
2bda0e17 KB |
503 | { |
504 | if ( m_maskBitmap ) | |
505 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
506 | } | |
507 | ||
508 | // Create a mask from a mono bitmap (copies the bitmap). | |
509 | bool wxMask::Create(const wxBitmap& bitmap) | |
510 | { | |
511 | if ( m_maskBitmap ) | |
6d167489 VZ |
512 | { |
513 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
514 | m_maskBitmap = 0; | |
515 | } | |
516 | if (!bitmap.Ok() || bitmap.GetDepth() != 1) | |
517 | { | |
518 | return FALSE; | |
519 | } | |
520 | m_maskBitmap = (WXHBITMAP) CreateBitmap( | |
521 | bitmap.GetWidth(), | |
522 | bitmap.GetHeight(), | |
523 | 1, 1, 0 | |
524 | ); | |
525 | HDC srcDC = CreateCompatibleDC(0); | |
526 | SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
527 | HDC destDC = CreateCompatibleDC(0); | |
528 | SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
529 | BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY); | |
530 | SelectObject(srcDC, 0); | |
531 | DeleteDC(srcDC); | |
532 | SelectObject(destDC, 0); | |
533 | DeleteDC(destDC); | |
534 | return TRUE; | |
2bda0e17 KB |
535 | } |
536 | ||
537 | // Create a mask from a bitmap and a palette index indicating | |
538 | // the transparent area | |
debe6624 | 539 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
540 | { |
541 | if ( m_maskBitmap ) | |
1d792928 | 542 | { |
6d167489 VZ |
543 | ::DeleteObject((HBITMAP) m_maskBitmap); |
544 | m_maskBitmap = 0; | |
1d792928 | 545 | } |
6d167489 VZ |
546 | if (bitmap.Ok() && bitmap.GetPalette()->Ok()) |
547 | { | |
548 | unsigned char red, green, blue; | |
549 | if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue)) | |
550 | { | |
551 | wxColour transparentColour(red, green, blue); | |
552 | return Create(bitmap, transparentColour); | |
553 | } | |
554 | } | |
555 | return FALSE; | |
2bda0e17 KB |
556 | } |
557 | ||
558 | // Create a mask from a bitmap and a colour indicating | |
559 | // the transparent area | |
560 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
561 | { | |
562 | if ( m_maskBitmap ) | |
1d792928 | 563 | { |
6d167489 VZ |
564 | ::DeleteObject((HBITMAP) m_maskBitmap); |
565 | m_maskBitmap = 0; | |
566 | } | |
567 | if (!bitmap.Ok()) | |
568 | { | |
569 | return FALSE; | |
1d792928 | 570 | } |
2bda0e17 | 571 | |
6d167489 VZ |
572 | // scan the bitmap for the transparent colour and set |
573 | // the corresponding pixels in the mask to BLACK and | |
574 | // the rest to WHITE | |
575 | COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue()); | |
576 | m_maskBitmap = (WXHBITMAP) ::CreateBitmap( | |
577 | bitmap.GetWidth(), | |
578 | bitmap.GetHeight(), | |
579 | 1, 1, 0 | |
580 | ); | |
581 | HDC srcDC = ::CreateCompatibleDC(0); | |
582 | ::SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
583 | HDC destDC = ::CreateCompatibleDC(0); | |
584 | ::SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
585 | ||
586 | // this is not very efficient, but I can't think | |
587 | // of a better way of doing it | |
588 | for (int w = 0; w < bitmap.GetWidth(); w++) | |
2bda0e17 | 589 | { |
6d167489 | 590 | for (int h = 0; h < bitmap.GetHeight(); h++) |
c793fa87 | 591 | { |
6d167489 VZ |
592 | COLORREF col = GetPixel(srcDC, w, h); |
593 | if (col == maskColour) | |
594 | { | |
595 | ::SetPixel(destDC, w, h, RGB(0, 0, 0)); | |
596 | } | |
597 | else | |
598 | { | |
599 | ::SetPixel(destDC, w, h, RGB(255, 255, 255)); | |
600 | } | |
c793fa87 | 601 | } |
2bda0e17 | 602 | } |
6d167489 VZ |
603 | ::SelectObject(srcDC, 0); |
604 | ::DeleteDC(srcDC); | |
605 | ::SelectObject(destDC, 0); | |
606 | ::DeleteDC(destDC); | |
607 | return TRUE; | |
608 | } | |
609 | ||
610 | // ---------------------------------------------------------------------------- | |
611 | // wxBitmapHandler | |
612 | // ---------------------------------------------------------------------------- | |
1d792928 | 613 | |
6d167489 VZ |
614 | bool wxBitmapHandler::Create(wxGDIImage *image, |
615 | void *data, | |
616 | long flags, | |
617 | int width, int height, int depth) | |
618 | { | |
619 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); | |
1d792928 | 620 | |
6d167489 | 621 | return bitmap ? Create(bitmap, data, width, height, depth) : FALSE; |
2bda0e17 KB |
622 | } |
623 | ||
6d167489 VZ |
624 | bool wxBitmapHandler::Load(wxGDIImage *image, |
625 | const wxString& name, | |
626 | long flags, | |
627 | int width, int height) | |
2bda0e17 | 628 | { |
6d167489 | 629 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
2bda0e17 | 630 | |
6d167489 VZ |
631 | return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE; |
632 | } | |
2bda0e17 | 633 | |
6d167489 VZ |
634 | bool wxBitmapHandler::Save(wxGDIImage *image, |
635 | const wxString& name, | |
636 | int type) | |
2bda0e17 | 637 | { |
6d167489 VZ |
638 | wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); |
639 | ||
640 | return bitmap ? SaveFile(bitmap, name, type) : FALSE; | |
2bda0e17 KB |
641 | } |
642 | ||
6d167489 VZ |
643 | bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), |
644 | void *WXUNUSED(data), | |
645 | long WXUNUSED(type), | |
646 | int WXUNUSED(width), | |
647 | int WXUNUSED(height), | |
648 | int WXUNUSED(depth)) | |
2bda0e17 | 649 | { |
6d167489 | 650 | return FALSE; |
2bda0e17 KB |
651 | } |
652 | ||
6d167489 VZ |
653 | bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), |
654 | const wxString& WXUNUSED(name), | |
655 | long WXUNUSED(type), | |
656 | int WXUNUSED(desiredWidth), | |
657 | int WXUNUSED(desiredHeight)) | |
2bda0e17 | 658 | { |
6d167489 | 659 | return FALSE; |
2bda0e17 KB |
660 | } |
661 | ||
6d167489 VZ |
662 | bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), |
663 | const wxString& WXUNUSED(name), | |
664 | int WXUNUSED(type), | |
665 | const wxPalette *WXUNUSED(palette)) | |
2bda0e17 | 666 | { |
6d167489 | 667 | return FALSE; |
7b46ecac JS |
668 | } |
669 | ||
6d167489 VZ |
670 | // ---------------------------------------------------------------------------- |
671 | // DIB functions | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
674 | bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, | |
675 | HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) | |
7b46ecac JS |
676 | { |
677 | unsigned long i, headerSize; | |
678 | LPBITMAPINFO lpDIBheader = NULL; | |
679 | LPPALETTEENTRY lpPe = NULL; | |
680 | ||
681 | ||
682 | // Allocate space for a DIB header | |
683 | headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); | |
684 | lpDIBheader = (BITMAPINFO *) malloc(headerSize); | |
685 | lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); | |
686 | ||
687 | GetPaletteEntries(hPal, 0, 256, lpPe); | |
688 | ||
7b46ecac JS |
689 | memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER)); |
690 | ||
7b46ecac JS |
691 | // Fill in the static parts of the DIB header |
692 | lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
693 | lpDIBheader->bmiHeader.biWidth = xSize; | |
694 | lpDIBheader->bmiHeader.biHeight = ySize; | |
695 | lpDIBheader->bmiHeader.biPlanes = 1; | |
696 | ||
697 | // this value must be 1, 4, 8 or 24 so PixelDepth can only be | |
698 | lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); | |
699 | lpDIBheader->bmiHeader.biCompression = BI_RGB; | |
6d167489 | 700 | lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3; |
7b46ecac JS |
701 | lpDIBheader->bmiHeader.biClrUsed = 256; |
702 | ||
703 | ||
704 | // Initialize the DIB palette | |
705 | for (i = 0; i < 256; i++) { | |
706 | lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags; | |
707 | lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed; | |
708 | lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen; | |
709 | lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue; | |
710 | } | |
711 | ||
712 | *lpDIBHeader = lpDIBheader; | |
713 | ||
6d167489 | 714 | return TRUE; |
7b46ecac JS |
715 | } |
716 | ||
6d167489 | 717 | void wxFreeDIB(LPBITMAPINFO lpDIBHeader) |
7b46ecac | 718 | { |
6d167489 | 719 | free(lpDIBHeader); |
7b46ecac JS |
720 | } |
721 | ||
722 |