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