]>
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "bitmap.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include <stdio.h> | |
25 | #include "wx/setup.h" | |
26 | #include "wx/list.h" | |
27 | #include "wx/utils.h" | |
28 | #include "wx/app.h" | |
29 | #include "wx/palette.h" | |
2432b92d | 30 | #include "wx/dcmemory.h" |
2bda0e17 KB |
31 | #include "wx/bitmap.h" |
32 | #include "wx/icon.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/msw/private.h" | |
1d792928 VZ |
36 | #include "wx/log.h" |
37 | ||
2bda0e17 KB |
38 | #include "assert.h" |
39 | ||
2bda0e17 KB |
40 | #include "wx/msw/dib.h" |
41 | ||
42 | #if !USE_SHARED_LIBRARIES | |
43 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) | |
44 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
45 | #endif | |
46 | ||
47 | wxBitmapRefData::wxBitmapRefData(void) | |
48 | { | |
49 | m_ok = FALSE; | |
50 | m_width = 0; | |
51 | m_height = 0; | |
52 | m_depth = 0; | |
53 | m_quality = 0; | |
54 | m_hBitmap = 0 ; | |
55 | m_selectedInto = NULL; | |
56 | m_numColors = 0; | |
57 | m_bitmapMask = NULL; | |
58 | } | |
59 | ||
60 | wxBitmapRefData::~wxBitmapRefData(void) | |
61 | { | |
62 | if (m_selectedInto) | |
63 | { | |
64 | char buf[200]; | |
65 | sprintf(buf, "Bitmap was deleted without selecting out of wxMemoryDC %X.", (unsigned int) m_selectedInto); | |
66 | wxFatalError(buf); | |
67 | } | |
68 | if (m_hBitmap) | |
69 | { | |
70 | DeleteObject((HBITMAP) m_hBitmap); | |
71 | } | |
72 | m_hBitmap = 0 ; | |
73 | ||
74 | if (m_bitmapMask) | |
c9f8e0e2 | 75 | delete m_bitmapMask; |
2bda0e17 | 76 | m_bitmapMask = NULL; |
e7003166 | 77 | |
2bda0e17 KB |
78 | } |
79 | ||
80 | wxList wxBitmap::sm_handlers; | |
81 | ||
82 | wxBitmap::wxBitmap(void) | |
83 | { | |
2bda0e17 KB |
84 | if ( wxTheBitmapList ) |
85 | wxTheBitmapList->AddBitmap(this); | |
86 | } | |
87 | ||
88 | wxBitmap::~wxBitmap(void) | |
89 | { | |
90 | if (wxTheBitmapList) | |
91 | wxTheBitmapList->DeleteObject(this); | |
92 | } | |
93 | ||
94 | bool wxBitmap::FreeResource(bool force) | |
95 | { | |
96 | if ( !M_BITMAPDATA ) | |
1d792928 | 97 | return FALSE; |
2bda0e17 KB |
98 | |
99 | if (M_BITMAPDATA->m_selectedInto) | |
100 | { | |
101 | char buf[200]; | |
102 | sprintf(buf, "Bitmap %X was deleted without selecting out of wxMemoryDC %X.", (unsigned int) this, (unsigned int) M_BITMAPDATA->m_selectedInto); | |
103 | wxFatalError(buf); | |
104 | } | |
105 | if (M_BITMAPDATA->m_hBitmap) | |
106 | { | |
107 | DeleteObject((HBITMAP) M_BITMAPDATA->m_hBitmap); | |
108 | } | |
109 | M_BITMAPDATA->m_hBitmap = 0 ; | |
110 | ||
111 | /* | |
112 | if (M_BITMAPDATA->m_bitmapPalette) | |
113 | delete M_BITMAPDATA->m_bitmapPalette; | |
114 | ||
115 | M_BITMAPDATA->m_bitmapPalette = NULL ; | |
116 | */ | |
117 | ||
118 | return TRUE; | |
119 | } | |
120 | ||
121 | ||
debe6624 | 122 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) |
2bda0e17 KB |
123 | { |
124 | m_refData = new wxBitmapRefData; | |
125 | ||
126 | M_BITMAPDATA->m_width = the_width ; | |
127 | M_BITMAPDATA->m_height = the_height ; | |
128 | M_BITMAPDATA->m_depth = no_bits ; | |
129 | M_BITMAPDATA->m_numColors = 0; | |
130 | ||
4c444f19 | 131 | M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(the_width, the_height, 1, no_bits, bits); |
2bda0e17 KB |
132 | |
133 | if (M_BITMAPDATA->m_hBitmap) | |
134 | M_BITMAPDATA->m_ok = TRUE; | |
135 | else | |
136 | M_BITMAPDATA->m_ok = FALSE; | |
137 | ||
138 | M_BITMAPDATA->m_selectedInto = NULL; | |
139 | ||
140 | if ( wxTheBitmapList ) | |
141 | wxTheBitmapList->AddBitmap(this); | |
142 | } | |
143 | ||
2fd284a4 JS |
144 | // Create from XPM data |
145 | wxBitmap::wxBitmap(char **data, wxItem *WXUNUSED(anItem)) | |
146 | { | |
147 | (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); | |
148 | } | |
149 | ||
debe6624 | 150 | wxBitmap::wxBitmap(int w, int h, int d) |
2bda0e17 KB |
151 | { |
152 | (void)Create(w, h, d); | |
153 | ||
154 | if ( wxTheBitmapList ) | |
155 | wxTheBitmapList->AddBitmap(this); | |
156 | } | |
157 | ||
debe6624 | 158 | wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) |
2bda0e17 KB |
159 | { |
160 | (void) Create(data, type, width, height, depth); | |
161 | ||
162 | if ( wxTheBitmapList ) | |
163 | wxTheBitmapList->AddBitmap(this); | |
164 | } | |
165 | ||
debe6624 | 166 | wxBitmap::wxBitmap(const wxString& filename, long type) |
2bda0e17 KB |
167 | { |
168 | LoadFile(filename, (int)type); | |
169 | ||
170 | if ( wxTheBitmapList ) | |
171 | wxTheBitmapList->AddBitmap(this); | |
172 | } | |
173 | ||
debe6624 | 174 | bool wxBitmap::Create(int w, int h, int d) |
2bda0e17 KB |
175 | { |
176 | UnRef(); | |
177 | ||
178 | m_refData = new wxBitmapRefData; | |
179 | ||
180 | M_BITMAPDATA->m_width = w; | |
181 | M_BITMAPDATA->m_height = h; | |
182 | M_BITMAPDATA->m_depth = d; | |
183 | ||
184 | if (d > 0) | |
185 | { | |
4c444f19 | 186 | M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(w, h, 1, d, NULL); |
2bda0e17 KB |
187 | } |
188 | else | |
189 | { | |
190 | HDC dc = GetDC(NULL); | |
191 | M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateCompatibleBitmap(dc, w, h); | |
192 | ReleaseDC(NULL, dc); | |
193 | M_BITMAPDATA->m_depth = wxDisplayDepth(); | |
194 | } | |
195 | if (M_BITMAPDATA->m_hBitmap) | |
196 | M_BITMAPDATA->m_ok = TRUE; | |
197 | else | |
198 | M_BITMAPDATA->m_ok = FALSE; | |
199 | return M_BITMAPDATA->m_ok; | |
200 | } | |
201 | ||
debe6624 | 202 | bool wxBitmap::LoadFile(const wxString& filename, long type) |
2bda0e17 KB |
203 | { |
204 | UnRef(); | |
205 | ||
206 | m_refData = new wxBitmapRefData; | |
207 | ||
208 | wxBitmapHandler *handler = FindHandler(type); | |
209 | ||
1d792928 VZ |
210 | if ( handler == NULL ) { |
211 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
212 | ||
213 | return FALSE; | |
214 | } | |
215 | ||
216 | return handler->LoadFile(this, filename, type, -1, -1); | |
2bda0e17 KB |
217 | } |
218 | ||
debe6624 | 219 | bool wxBitmap::Create(void *data, long type, int width, int height, int depth) |
2bda0e17 KB |
220 | { |
221 | UnRef(); | |
222 | ||
223 | m_refData = new wxBitmapRefData; | |
224 | ||
225 | wxBitmapHandler *handler = FindHandler(type); | |
226 | ||
1d792928 VZ |
227 | if ( handler == NULL ) { |
228 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
229 | ||
230 | return FALSE; | |
231 | } | |
232 | ||
233 | return handler->Create(this, data, type, width, height, depth); | |
2bda0e17 KB |
234 | } |
235 | ||
debe6624 | 236 | bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) |
2bda0e17 KB |
237 | { |
238 | wxBitmapHandler *handler = FindHandler(type); | |
239 | ||
1d792928 VZ |
240 | if ( handler == NULL ) { |
241 | wxLogWarning("no bitmap handler for type %d defined.", type); | |
242 | ||
243 | return FALSE; | |
244 | } | |
245 | ||
246 | return handler->SaveFile(this, filename, type, palette); | |
2bda0e17 KB |
247 | } |
248 | ||
249 | void wxBitmap::SetWidth(int w) | |
250 | { | |
251 | if (!M_BITMAPDATA) | |
b823f5a1 | 252 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
253 | |
254 | M_BITMAPDATA->m_width = w; | |
255 | } | |
256 | ||
257 | void wxBitmap::SetHeight(int h) | |
258 | { | |
259 | if (!M_BITMAPDATA) | |
1d792928 | 260 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
261 | |
262 | M_BITMAPDATA->m_height = h; | |
263 | } | |
264 | ||
265 | void wxBitmap::SetDepth(int d) | |
266 | { | |
267 | if (!M_BITMAPDATA) | |
1d792928 | 268 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
269 | |
270 | M_BITMAPDATA->m_depth = d; | |
271 | } | |
272 | ||
273 | void wxBitmap::SetQuality(int q) | |
274 | { | |
275 | if (!M_BITMAPDATA) | |
1d792928 | 276 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
277 | |
278 | M_BITMAPDATA->m_quality = q; | |
279 | } | |
280 | ||
281 | void wxBitmap::SetOk(bool isOk) | |
282 | { | |
283 | if (!M_BITMAPDATA) | |
1d792928 | 284 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
285 | |
286 | M_BITMAPDATA->m_ok = isOk; | |
287 | } | |
288 | ||
289 | void wxBitmap::SetPalette(const wxPalette& palette) | |
290 | { | |
291 | if (!M_BITMAPDATA) | |
1d792928 | 292 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
293 | |
294 | M_BITMAPDATA->m_bitmapPalette = palette ; | |
295 | } | |
296 | ||
297 | void wxBitmap::SetMask(wxMask *mask) | |
298 | { | |
299 | if (!M_BITMAPDATA) | |
1d792928 | 300 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
301 | |
302 | M_BITMAPDATA->m_bitmapMask = mask ; | |
303 | } | |
304 | ||
305 | void wxBitmap::SetHBITMAP(WXHBITMAP bmp) | |
306 | { | |
307 | if (!M_BITMAPDATA) | |
1d792928 | 308 | m_refData = new wxBitmapRefData; |
2bda0e17 KB |
309 | |
310 | M_BITMAPDATA->m_hBitmap = bmp; | |
311 | } | |
312 | ||
313 | void wxBitmap::AddHandler(wxBitmapHandler *handler) | |
314 | { | |
1d792928 | 315 | sm_handlers.Append(handler); |
2bda0e17 KB |
316 | } |
317 | ||
318 | void wxBitmap::InsertHandler(wxBitmapHandler *handler) | |
319 | { | |
1d792928 | 320 | sm_handlers.Insert(handler); |
2bda0e17 KB |
321 | } |
322 | ||
323 | bool wxBitmap::RemoveHandler(const wxString& name) | |
324 | { | |
1d792928 VZ |
325 | wxBitmapHandler *handler = FindHandler(name); |
326 | if ( handler ) | |
327 | { | |
328 | sm_handlers.DeleteObject(handler); | |
329 | return TRUE; | |
330 | } | |
331 | else | |
332 | return FALSE; | |
2bda0e17 KB |
333 | } |
334 | ||
335 | wxBitmapHandler *wxBitmap::FindHandler(const wxString& name) | |
336 | { | |
1d792928 VZ |
337 | wxNode *node = sm_handlers.First(); |
338 | while ( node ) | |
339 | { | |
340 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); | |
341 | if ( handler->GetName() == name ) | |
342 | return handler; | |
343 | node = node->Next(); | |
344 | } | |
345 | return NULL; | |
2bda0e17 KB |
346 | } |
347 | ||
348 | wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType) | |
349 | { | |
1d792928 VZ |
350 | wxNode *node = sm_handlers.First(); |
351 | while ( node ) | |
352 | { | |
353 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); | |
354 | if ( handler->GetExtension() == extension && | |
355 | (bitmapType == -1 || handler->GetType() == bitmapType) ) | |
356 | return handler; | |
357 | node = node->Next(); | |
358 | } | |
359 | return NULL; | |
2bda0e17 KB |
360 | } |
361 | ||
362 | wxBitmapHandler *wxBitmap::FindHandler(long bitmapType) | |
363 | { | |
1d792928 VZ |
364 | wxNode *node = sm_handlers.First(); |
365 | while ( node ) | |
366 | { | |
367 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); | |
368 | if (handler->GetType() == bitmapType) | |
369 | return handler; | |
370 | node = node->Next(); | |
371 | } | |
372 | return NULL; | |
2bda0e17 KB |
373 | } |
374 | ||
7b46ecac JS |
375 | // New Create/FreeDIB functions since ones in dibutils.cpp are confusing |
376 | static long createDIB(long xSize, long ySize, long bitsPerPixel, | |
377 | HPALETTE hPal, LPBITMAPINFO* lpDIBHeader); | |
378 | static long freeDIB(LPBITMAPINFO lpDIBHeader); | |
379 | ||
380 | // Creates a bitmap that matches the device context, from | |
381 | // an arbitray bitmap. At present, the original bitmap must have an | |
382 | // associated palette. TODO: use a default palette if no palette exists. | |
383 | // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com> | |
384 | wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const | |
385 | { | |
386 | wxMemoryDC memDC; | |
387 | wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth()); | |
388 | HPALETTE hPal = NULL; | |
389 | LPBITMAPINFO lpDib; | |
390 | void *lpBits = NULL; | |
391 | ||
a367b9b3 | 392 | /* |
27529614 | 393 | wxASSERT( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) ); |
7b46ecac JS |
394 | |
395 | tmpBitmap.SetPalette(this->GetPalette()); | |
396 | memDC.SelectObject(tmpBitmap); | |
397 | memDC.SetPalette(this->GetPalette()); | |
398 | ||
399 | hPal = (HPALETTE) this->GetPalette()->GetHPALETTE(); | |
a367b9b3 JS |
400 | */ |
401 | if( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) ) | |
402 | { | |
c0ed460c | 403 | tmpBitmap.SetPalette(* this->GetPalette()); |
a367b9b3 | 404 | memDC.SelectObject(tmpBitmap); |
c0ed460c | 405 | memDC.SetPalette(* this->GetPalette()); |
a367b9b3 JS |
406 | hPal = (HPALETTE) this->GetPalette()->GetHPALETTE(); |
407 | } | |
408 | else | |
409 | { | |
410 | hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); | |
411 | wxPalette palette; | |
412 | palette.SetHPALETTE( (WXHPALETTE)hPal ); | |
413 | tmpBitmap.SetPalette( palette ); | |
414 | memDC.SelectObject(tmpBitmap); | |
415 | memDC.SetPalette( palette ); | |
416 | } | |
7b46ecac JS |
417 | |
418 | // set the height negative because in a DIB the order of the lines is reversed | |
419 | createDIB(this->GetWidth(), -this->GetHeight(), this->GetDepth(), hPal, &lpDib); | |
420 | ||
421 | lpBits = malloc(lpDib->bmiHeader.biSizeImage); | |
422 | ||
423 | ::GetBitmapBits((HBITMAP)GetHBITMAP(), lpDib->bmiHeader.biSizeImage, lpBits); | |
424 | ||
425 | ::SetDIBitsToDevice((HDC) memDC.GetHDC(), 0, 0, this->GetWidth(), this->GetHeight(), | |
426 | 0, 0, 0, this->GetHeight(), lpBits, lpDib, DIB_RGB_COLORS); | |
427 | ||
428 | free(lpBits); | |
429 | ||
430 | freeDIB(lpDib); | |
431 | return (tmpBitmap); | |
432 | } | |
433 | ||
2bda0e17 KB |
434 | /* |
435 | * wxMask | |
436 | */ | |
437 | ||
438 | wxMask::wxMask(void) | |
439 | { | |
440 | m_maskBitmap = 0; | |
441 | } | |
442 | ||
443 | // Construct a mask from a bitmap and a colour indicating | |
444 | // the transparent area | |
445 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
446 | { | |
447 | m_maskBitmap = 0; | |
1d792928 | 448 | Create(bitmap, colour); |
2bda0e17 KB |
449 | } |
450 | ||
451 | // Construct a mask from a bitmap and a palette index indicating | |
452 | // the transparent area | |
debe6624 | 453 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
454 | { |
455 | m_maskBitmap = 0; | |
1d792928 | 456 | Create(bitmap, paletteIndex); |
2bda0e17 KB |
457 | } |
458 | ||
459 | // Construct a mask from a mono bitmap (copies the bitmap). | |
460 | wxMask::wxMask(const wxBitmap& bitmap) | |
461 | { | |
462 | m_maskBitmap = 0; | |
1d792928 | 463 | Create(bitmap); |
2bda0e17 KB |
464 | } |
465 | ||
466 | wxMask::~wxMask(void) | |
467 | { | |
468 | if ( m_maskBitmap ) | |
469 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
470 | } | |
471 | ||
472 | // Create a mask from a mono bitmap (copies the bitmap). | |
473 | bool wxMask::Create(const wxBitmap& bitmap) | |
474 | { | |
475 | if ( m_maskBitmap ) | |
1d792928 VZ |
476 | { |
477 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
478 | m_maskBitmap = 0; | |
479 | } | |
480 | if (!bitmap.Ok() || bitmap.GetDepth() != 1) | |
481 | { | |
482 | return FALSE; | |
483 | } | |
484 | m_maskBitmap = (WXHBITMAP) CreateBitmap( | |
485 | bitmap.GetWidth(), | |
486 | bitmap.GetHeight(), | |
487 | 1, 1, 0 | |
488 | ); | |
489 | HDC srcDC = CreateCompatibleDC(0); | |
490 | SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
491 | HDC destDC = CreateCompatibleDC(0); | |
492 | SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
493 | BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY); | |
494 | SelectObject(srcDC, 0); | |
495 | DeleteDC(srcDC); | |
496 | SelectObject(destDC, 0); | |
497 | DeleteDC(destDC); | |
498 | return TRUE; | |
2bda0e17 KB |
499 | } |
500 | ||
501 | // Create a mask from a bitmap and a palette index indicating | |
502 | // the transparent area | |
debe6624 | 503 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) |
2bda0e17 KB |
504 | { |
505 | if ( m_maskBitmap ) | |
1d792928 VZ |
506 | { |
507 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
508 | m_maskBitmap = 0; | |
509 | } | |
510 | if (bitmap.Ok() && bitmap.GetPalette()->Ok()) | |
511 | { | |
512 | unsigned char red, green, blue; | |
513 | if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue)) | |
514 | { | |
515 | wxColour transparentColour(red, green, blue); | |
516 | return Create(bitmap, transparentColour); | |
517 | } | |
518 | } | |
519 | return FALSE; | |
2bda0e17 KB |
520 | } |
521 | ||
522 | // Create a mask from a bitmap and a colour indicating | |
523 | // the transparent area | |
524 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
525 | { | |
526 | if ( m_maskBitmap ) | |
1d792928 VZ |
527 | { |
528 | ::DeleteObject((HBITMAP) m_maskBitmap); | |
529 | m_maskBitmap = 0; | |
530 | } | |
531 | if (!bitmap.Ok()) | |
532 | { | |
533 | return FALSE; | |
534 | } | |
535 | ||
536 | // scan the bitmap for the transparent colour and set | |
537 | // the corresponding pixels in the mask to BLACK and | |
538 | // the rest to WHITE | |
539 | COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue()); | |
540 | m_maskBitmap = (WXHBITMAP) ::CreateBitmap( | |
541 | bitmap.GetWidth(), | |
542 | bitmap.GetHeight(), | |
543 | 1, 1, 0 | |
544 | ); | |
545 | HDC srcDC = ::CreateCompatibleDC(0); | |
546 | ::SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP()); | |
547 | HDC destDC = ::CreateCompatibleDC(0); | |
548 | ::SelectObject(destDC, (HBITMAP) m_maskBitmap); | |
549 | ||
550 | // this is not very efficient, but I can't think | |
551 | // of a better way of doing it | |
552 | for (int w = 0; w < bitmap.GetWidth(); w++) | |
553 | { | |
554 | for (int h = 0; h < bitmap.GetHeight(); h++) | |
555 | { | |
556 | COLORREF col = GetPixel(srcDC, w, h); | |
557 | if (col == maskColour) | |
558 | { | |
559 | ::SetPixel(destDC, w, h, RGB(0, 0, 0)); | |
560 | } | |
561 | else | |
562 | { | |
563 | ::SetPixel(destDC, w, h, RGB(255, 255, 255)); | |
564 | } | |
565 | } | |
566 | } | |
567 | ::SelectObject(srcDC, 0); | |
568 | ::DeleteDC(srcDC); | |
569 | ::SelectObject(destDC, 0); | |
570 | ::DeleteDC(destDC); | |
571 | return TRUE; | |
2bda0e17 KB |
572 | } |
573 | ||
574 | /* | |
575 | * wxBitmapHandler | |
576 | */ | |
577 | ||
578 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) | |
579 | ||
debe6624 | 580 | bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) |
2bda0e17 | 581 | { |
1d792928 | 582 | return FALSE; |
2bda0e17 KB |
583 | } |
584 | ||
debe6624 | 585 | bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type, |
2bda0e17 KB |
586 | int desiredWidth, int desiredHeight) |
587 | { | |
1d792928 | 588 | return FALSE; |
2bda0e17 KB |
589 | } |
590 | ||
debe6624 | 591 | bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) |
2bda0e17 | 592 | { |
1d792928 | 593 | return FALSE; |
2bda0e17 KB |
594 | } |
595 | ||
596 | /* | |
597 | * Standard handlers | |
598 | */ | |
599 | ||
600 | class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler | |
601 | { | |
602 | DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler) | |
603 | public: | |
604 | inline wxBMPResourceHandler(void) | |
605 | { | |
1d792928 VZ |
606 | m_name = "Windows bitmap resource"; |
607 | m_extension = ""; | |
608 | m_type = wxBITMAP_TYPE_BMP_RESOURCE; | |
2bda0e17 KB |
609 | }; |
610 | ||
debe6624 | 611 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
2bda0e17 KB |
612 | int desiredWidth, int desiredHeight); |
613 | }; | |
614 | IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) | |
615 | ||
debe6624 | 616 | bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
2bda0e17 KB |
617 | int desiredWidth, int desiredHeight) |
618 | { | |
619 | // TODO: load colourmap. | |
620 | M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ::LoadBitmap(wxGetInstance(), name); | |
621 | if (M_BITMAPHANDLERDATA->m_hBitmap) | |
622 | { | |
623 | M_BITMAPHANDLERDATA->m_ok = TRUE; | |
624 | BITMAP bm; | |
625 | GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(BITMAP), (LPSTR) &bm); | |
626 | M_BITMAPHANDLERDATA->m_width = bm.bmWidth; | |
627 | M_BITMAPHANDLERDATA->m_height = bm.bmHeight; | |
7b46ecac | 628 | M_BITMAPHANDLERDATA->m_depth = bm.bmBitsPixel; |
2bda0e17 KB |
629 | return TRUE; |
630 | } | |
1d792928 VZ |
631 | |
632 | // it's probably not found | |
dabeb021 | 633 | wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name.c_str()); |
1d792928 VZ |
634 | |
635 | return FALSE; | |
2bda0e17 KB |
636 | } |
637 | ||
638 | class WXDLLEXPORT wxBMPFileHandler: public wxBitmapHandler | |
639 | { | |
640 | DECLARE_DYNAMIC_CLASS(wxBMPFileHandler) | |
641 | public: | |
642 | inline wxBMPFileHandler(void) | |
643 | { | |
1d792928 VZ |
644 | m_name = "Windows bitmap file"; |
645 | m_extension = "bmp"; | |
646 | m_type = wxBITMAP_TYPE_BMP; | |
2bda0e17 KB |
647 | }; |
648 | ||
debe6624 | 649 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
2bda0e17 | 650 | int desiredWidth, int desiredHeight); |
debe6624 | 651 | virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL); |
2bda0e17 KB |
652 | }; |
653 | IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) | |
654 | ||
debe6624 | 655 | bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
2bda0e17 KB |
656 | int desiredWidth, int desiredHeight) |
657 | { | |
47d67540 | 658 | #if wxUSE_IMAGE_LOADING_IN_MSW |
2bda0e17 KB |
659 | wxPalette *palette = NULL; |
660 | bool success = FALSE; | |
661 | /* | |
662 | if (type & wxBITMAP_DISCARD_COLOURMAP) | |
663 | success = wxLoadIntoBitmap(WXSTRINGCAST name, bitmap); | |
664 | else | |
665 | */ | |
666 | success = (wxLoadIntoBitmap(WXSTRINGCAST name, bitmap, &palette) != 0); | |
667 | if (!success && palette) | |
668 | { | |
669 | delete palette; | |
670 | palette = NULL; | |
671 | } | |
672 | if (palette) | |
673 | M_BITMAPHANDLERDATA->m_bitmapPalette = *palette; | |
674 | return success; | |
675 | #else | |
1d792928 | 676 | return FALSE; |
2bda0e17 KB |
677 | #endif |
678 | } | |
679 | ||
debe6624 | 680 | bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal) |
2bda0e17 | 681 | { |
47d67540 | 682 | #if wxUSE_IMAGE_LOADING_IN_MSW |
2bda0e17 KB |
683 | wxPalette *actualPalette = (wxPalette *)pal; |
684 | if (!actualPalette && (!M_BITMAPHANDLERDATA->m_bitmapPalette.IsNull())) | |
685 | actualPalette = & (M_BITMAPHANDLERDATA->m_bitmapPalette); | |
686 | return (wxSaveBitmap(WXSTRINGCAST name, bitmap, actualPalette) != 0); | |
687 | #else | |
1d792928 | 688 | return FALSE; |
2bda0e17 KB |
689 | #endif |
690 | } | |
691 | ||
2bda0e17 KB |
692 | void wxBitmap::CleanUpHandlers(void) |
693 | { | |
1d792928 VZ |
694 | wxNode *node = sm_handlers.First(); |
695 | while ( node ) | |
696 | { | |
697 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); | |
698 | wxNode *next = node->Next(); | |
699 | delete handler; | |
700 | delete node; | |
701 | node = next; | |
702 | } | |
2bda0e17 KB |
703 | } |
704 | ||
705 | void wxBitmap::InitStandardHandlers(void) | |
706 | { | |
1d792928 VZ |
707 | AddHandler(new wxBMPResourceHandler); |
708 | AddHandler(new wxBMPFileHandler); | |
2fd284a4 JS |
709 | |
710 | // Not added by default: include xpmhand.h in your app | |
711 | // and call these in your wxApp::OnInit. | |
712 | // AddHandler(new wxXPMFileHandler); | |
713 | // AddHandler(new wxXPMDataHandler); | |
714 | ||
1d792928 VZ |
715 | AddHandler(new wxICOResourceHandler); |
716 | AddHandler(new wxICOFileHandler); | |
7b46ecac JS |
717 | } |
718 | ||
719 | static long createDIB(long xSize, long ySize, long bitsPerPixel, | |
720 | HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) | |
721 | { | |
722 | unsigned long i, headerSize; | |
723 | LPBITMAPINFO lpDIBheader = NULL; | |
724 | LPPALETTEENTRY lpPe = NULL; | |
725 | ||
726 | ||
727 | // Allocate space for a DIB header | |
728 | headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); | |
729 | lpDIBheader = (BITMAPINFO *) malloc(headerSize); | |
730 | lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); | |
731 | ||
732 | GetPaletteEntries(hPal, 0, 256, lpPe); | |
733 | ||
734 | ||
735 | memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER)); | |
736 | ||
737 | ||
738 | // Fill in the static parts of the DIB header | |
739 | lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
740 | lpDIBheader->bmiHeader.biWidth = xSize; | |
741 | lpDIBheader->bmiHeader.biHeight = ySize; | |
742 | lpDIBheader->bmiHeader.biPlanes = 1; | |
743 | ||
744 | // this value must be 1, 4, 8 or 24 so PixelDepth can only be | |
745 | lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); | |
746 | lpDIBheader->bmiHeader.biCompression = BI_RGB; | |
747 | lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> | |
748 | 3; | |
749 | lpDIBheader->bmiHeader.biClrUsed = 256; | |
750 | ||
751 | ||
752 | // Initialize the DIB palette | |
753 | for (i = 0; i < 256; i++) { | |
754 | lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags; | |
755 | lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed; | |
756 | lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen; | |
757 | lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue; | |
758 | } | |
759 | ||
760 | *lpDIBHeader = lpDIBheader; | |
761 | ||
762 | ||
763 | return (0); | |
764 | ||
765 | } | |
766 | ||
767 | ||
768 | ||
769 | static long freeDIB(LPBITMAPINFO lpDIBHeader) | |
770 | { | |
771 | ||
772 | if (lpDIBHeader != NULL) { | |
773 | free(lpDIBHeader); | |
774 | } | |
775 | ||
776 | return (0); | |
777 | } | |
778 | ||
779 |