]>
Commit | Line | Data |
---|---|---|
32b8ec41 | 1 | ///////////////////////////////////////////////////////////////////////////// |
127eab18 | 2 | // Name: src/mgl/bitmap.cpp |
32b8ec41 VZ |
3 | // Author: Vaclav Slavik |
4 | // RCS-ID: $Id$ | |
c41c20a5 | 5 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
65571936 | 6 | // Licence: wxWindows licence |
32b8ec41 VZ |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
32b8ec41 VZ |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #include "wx/bitmap.h" | |
88a7a4e1 WS |
17 | |
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/intl.h" | |
e4db172a | 20 | #include "wx/log.h" |
de6185e2 | 21 | #include "wx/utils.h" |
f38924e8 | 22 | #include "wx/dcmemory.h" |
923d28da | 23 | #include "wx/icon.h" |
155ecd4c | 24 | #include "wx/image.h" |
88a7a4e1 WS |
25 | #endif |
26 | ||
32b8ec41 | 27 | #include "wx/filefn.h" |
32b8ec41 VZ |
28 | #include "wx/xpmdecod.h" |
29 | ||
30 | #include "wx/mgl/private.h" | |
31 | ||
32 | #include <mgraph.hpp> | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // MGL pixel formats: | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | static pixel_format_t gs_pixel_format_15 = | |
127eab18 WS |
39 | {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp |
40 | ||
32b8ec41 | 41 | static pixel_format_t gs_pixel_format_16 = |
127eab18 | 42 | {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp |
32b8ec41 VZ |
43 | |
44 | static pixel_format_t gs_pixel_format_24 = | |
127eab18 | 45 | {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp |
32b8ec41 VZ |
46 | |
47 | static pixel_format_t gs_pixel_format_32 = | |
127eab18 | 48 | {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp |
32b8ec41 | 49 | |
553dce51 | 50 | static pixel_format_t gs_pixel_format_wxImage = |
127eab18 | 51 | {0xFF,0x00,0, 0xFF,0x08,0, 0xFF,0x10,0, 0x00,0x00,0}; // RGB 24bpp for wxImage |
553dce51 | 52 | |
cdcb56fc VS |
53 | //----------------------------------------------------------------------------- |
54 | // helpers | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | // Convert wxColour into it's quantized value in lower-precision | |
58 | // pixel format (needed for masking by colour). | |
59 | static wxColour wxQuantizeColour(const wxColour& clr, const wxBitmap& bmp) | |
60 | { | |
61 | pixel_format_t *pf = bmp.GetMGLbitmap_t()->pf; | |
127eab18 | 62 | |
cdcb56fc VS |
63 | if ( pf->redAdjust == 0 && pf->greenAdjust == 0 && pf->blueAdjust == 0 ) |
64 | return clr; | |
65 | else | |
127eab18 WS |
66 | return wxColour((unsigned char)((clr.Red() >> pf->redAdjust) << pf->redAdjust), |
67 | (unsigned char)((clr.Green() >> pf->greenAdjust) << pf->greenAdjust), | |
68 | (unsigned char)((clr.Blue() >> pf->blueAdjust) << pf->blueAdjust)); | |
cdcb56fc VS |
69 | } |
70 | ||
71 | ||
72 | ||
32b8ec41 VZ |
73 | //----------------------------------------------------------------------------- |
74 | // wxMask | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
78 | ||
79 | wxMask::wxMask() | |
80 | { | |
81 | m_bitmap = NULL; | |
82 | } | |
83 | ||
84 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
85 | { | |
86 | m_bitmap = NULL; | |
87 | Create(bitmap, colour); | |
88 | } | |
89 | ||
90 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
91 | { | |
92 | m_bitmap = NULL; | |
93 | Create(bitmap, paletteIndex); | |
94 | } | |
95 | ||
96 | wxMask::wxMask(const wxBitmap& bitmap) | |
97 | { | |
98 | m_bitmap = NULL; | |
99 | Create(bitmap); | |
100 | } | |
101 | ||
102 | wxMask::~wxMask() | |
103 | { | |
104 | delete m_bitmap; | |
105 | } | |
106 | ||
107 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
108 | { | |
109 | delete m_bitmap; | |
110 | m_bitmap = NULL; | |
127eab18 | 111 | |
cdcb56fc | 112 | wxColour clr(wxQuantizeColour(colour, bitmap)); |
32b8ec41 | 113 | |
cdcb56fc | 114 | wxImage imgSrc(bitmap.ConvertToImage()); |
127eab18 | 115 | imgSrc.SetMask(false); |
cdcb56fc | 116 | wxImage image(imgSrc.ConvertToMono(clr.Red(), clr.Green(), clr.Blue())); |
127eab18 WS |
117 | if ( !image.Ok() ) |
118 | return false; | |
cdcb56fc | 119 | |
32b8ec41 VZ |
120 | m_bitmap = new wxBitmap(image, 1); |
121 | ||
122 | return m_bitmap->Ok(); | |
123 | } | |
124 | ||
125 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
126 | { | |
127 | unsigned char r,g,b; | |
128 | wxPalette *pal = bitmap.GetPalette(); | |
129 | ||
127eab18 WS |
130 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); |
131 | ||
32b8ec41 VZ |
132 | pal->GetRGB(paletteIndex, &r, &g, &b); |
133 | ||
134 | return Create(bitmap, wxColour(r, g, b)); | |
135 | } | |
136 | ||
137 | bool wxMask::Create(const wxBitmap& bitmap) | |
138 | { | |
139 | delete m_bitmap; | |
140 | m_bitmap = NULL; | |
141 | ||
127eab18 WS |
142 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap") ); |
143 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); | |
32b8ec41 VZ |
144 | |
145 | m_bitmap = new wxBitmap(bitmap); | |
127eab18 | 146 | return true; |
32b8ec41 VZ |
147 | } |
148 | ||
149 | ||
150 | //----------------------------------------------------------------------------- | |
151 | // wxBitmap | |
152 | //----------------------------------------------------------------------------- | |
153 | ||
154 | class wxBitmapRefData: public wxObjectRefData | |
155 | { | |
156 | public: | |
157 | wxBitmapRefData(); | |
158 | ~wxBitmapRefData(); | |
159 | ||
160 | int m_width; | |
161 | int m_height; | |
162 | int m_bpp; | |
163 | wxPalette *m_palette; | |
164 | wxMask *m_mask; | |
165 | bitmap_t *m_bitmap; | |
166 | }; | |
167 | ||
168 | wxBitmapRefData::wxBitmapRefData() | |
169 | { | |
170 | m_mask = NULL; | |
171 | m_width = 0; | |
172 | m_height = 0; | |
173 | m_bpp = 0; | |
174 | m_palette = NULL; | |
175 | m_bitmap = NULL; | |
176 | } | |
177 | ||
178 | wxBitmapRefData::~wxBitmapRefData() | |
179 | { | |
180 | if ( m_bitmap ) | |
181 | MGL_unloadBitmap(m_bitmap); | |
182 | delete m_mask; | |
183 | delete m_palette; | |
184 | } | |
185 | ||
186 | //----------------------------------------------------------------------------- | |
187 | ||
188 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
189 | ||
190 | ||
191 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler,wxObject) | |
192 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxBitmapBase) | |
193 | ||
32b8ec41 VZ |
194 | wxBitmap::wxBitmap(int width, int height, int depth) |
195 | { | |
196 | Create(width, height, depth); | |
32b8ec41 VZ |
197 | } |
198 | ||
199 | ||
127eab18 | 200 | static bitmap_t *MyMGL_createBitmap(int width, int height, |
32b8ec41 VZ |
201 | int bpp, pixel_format_t *pf) |
202 | { | |
203 | MGLMemoryDC mdc(width, height, bpp, pf); | |
204 | return MGL_getBitmapFromDC(mdc.getDC(), 0, 0, width, height, TRUE); | |
205 | } | |
206 | ||
207 | bool wxBitmap::Create(int width, int height, int depth) | |
208 | { | |
209 | UnRef(); | |
210 | ||
637b7e4f | 211 | wxCHECK_MSG( (width > 0) && (height > 0), false, wxT("invalid bitmap size") ); |
127eab18 WS |
212 | |
213 | pixel_format_t pf_dummy; | |
214 | pixel_format_t *pf; | |
32b8ec41 VZ |
215 | int mglDepth = depth; |
216 | ||
217 | switch ( depth ) | |
218 | { | |
219 | case -1: | |
220 | wxASSERT_MSG( g_displayDC, wxT("MGL display DC not created yet.") ); | |
221 | ||
222 | g_displayDC->getPixelFormat(pf_dummy); | |
127eab18 | 223 | mglDepth = g_displayDC->getBitsPerPixel(); |
32b8ec41 VZ |
224 | pf = &pf_dummy; |
225 | break; | |
226 | case 1: | |
227 | case 8: | |
228 | pf = NULL; | |
229 | break; | |
230 | case 15: | |
231 | pf = &gs_pixel_format_15; | |
232 | break; | |
233 | case 16: | |
234 | pf = &gs_pixel_format_16; | |
235 | break; | |
236 | case 24: | |
237 | pf = &gs_pixel_format_24; | |
238 | break; | |
239 | case 32: | |
240 | pf = &gs_pixel_format_32; | |
241 | break; | |
242 | default: | |
6a64f8d4 | 243 | wxFAIL_MSG(wxT("invalid bitmap depth")); |
127eab18 | 244 | return false; |
32b8ec41 VZ |
245 | } |
246 | ||
247 | m_refData = new wxBitmapRefData(); | |
248 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
249 | M_BMPDATA->m_palette = (wxPalette *) NULL; | |
250 | M_BMPDATA->m_width = width; | |
251 | M_BMPDATA->m_height = height; | |
252 | M_BMPDATA->m_bpp = mglDepth; | |
253 | ||
254 | if ( mglDepth != 1 ) | |
255 | { | |
256 | M_BMPDATA->m_bitmap = MyMGL_createBitmap(width, height, mglDepth, pf); | |
257 | } | |
258 | else | |
259 | { | |
260 | // MGL does not support mono DCs, so we have to emulate them with | |
261 | // 8bpp ones. We do that by using a special palette with color 0 | |
127eab18 | 262 | // set to black and all other colors set to white. |
32b8ec41 VZ |
263 | |
264 | M_BMPDATA->m_bitmap = MyMGL_createBitmap(width, height, 8, pf); | |
265 | SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0)); | |
266 | } | |
267 | ||
268 | return Ok(); | |
269 | } | |
270 | ||
271 | bool wxBitmap::CreateFromXpm(const char **bits) | |
272 | { | |
637b7e4f | 273 | wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); |
127eab18 | 274 | |
32b8ec41 VZ |
275 | wxXPMDecoder decoder; |
276 | wxImage img = decoder.ReadData(bits); | |
637b7e4f | 277 | wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") ); |
127eab18 | 278 | |
32b8ec41 | 279 | *this = wxBitmap(img); |
127eab18 WS |
280 | |
281 | return true; | |
32b8ec41 VZ |
282 | } |
283 | ||
f043bf8d | 284 | wxBitmap::wxBitmap(const wxImage& image, int depth) |
32b8ec41 VZ |
285 | { |
286 | long width, height; | |
287 | ||
637b7e4f | 288 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); |
127eab18 | 289 | |
32b8ec41 VZ |
290 | width = image.GetWidth(); |
291 | height = image.GetHeight(); | |
292 | ||
293 | if ( !Create(width, height, depth) ) return; | |
127eab18 | 294 | |
553dce51 | 295 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_wxImage, |
32b8ec41 VZ |
296 | width * 3, (void*)image.GetData(), NULL); |
297 | wxASSERT_MSG( idc.isValid(), wxT("cannot create custom MGLDC") ); | |
298 | ||
299 | MGLDevCtx *bdc = CreateTmpDC(); | |
300 | ||
f9619ece | 301 | if ( GetDepth() <= 8 && image.HasPalette() ) |
32b8ec41 VZ |
302 | SetPalette(image.GetPalette()); |
303 | ||
304 | bdc->bitBlt(idc, 0, 0, width, height, 0, 0, MGL_REPLACE_MODE); | |
305 | delete bdc; | |
127eab18 | 306 | |
32b8ec41 VZ |
307 | if ( image.HasMask() ) |
308 | { | |
309 | wxImage mask_image = image.ConvertToMono(image.GetMaskRed(), | |
310 | image.GetMaskGreen(), | |
311 | image.GetMaskBlue()); | |
127eab18 | 312 | mask_image.SetMask(false); |
32b8ec41 VZ |
313 | wxBitmap mask_bmp(mask_image, 1); |
314 | SetMask(new wxMask(mask_bmp)); | |
315 | } | |
316 | } | |
317 | ||
318 | wxImage wxBitmap::ConvertToImage() const | |
319 | { | |
db28c33c | 320 | wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") ); |
32b8ec41 | 321 | |
a4bbc9f7 | 322 | int width, height; |
32b8ec41 VZ |
323 | width = GetWidth(); |
324 | height = GetHeight(); | |
127eab18 | 325 | |
32b8ec41 VZ |
326 | wxImage image(width, height); |
327 | wxASSERT_MSG( image.Ok(), wxT("cannot create image") ); | |
127eab18 | 328 | |
553dce51 | 329 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_wxImage, |
32b8ec41 VZ |
330 | width * 3, (void*)image.GetData(), NULL); |
331 | wxASSERT_MSG( idc.isValid(), wxT("cannot create custom MGLDC") ); | |
332 | ||
333 | if ( M_BMPDATA->m_palette ) | |
334 | image.SetPalette(*(M_BMPDATA->m_palette)); | |
127eab18 | 335 | |
32b8ec41 VZ |
336 | if ( GetMask() ) |
337 | { | |
338 | // in consistency with other ports, we convert parts covered | |
339 | // by the mask to <16,16,16> colour and set that colour to image's | |
340 | // mask. We do that by OR-blitting the mask over image with | |
341 | // bg colour set to black and fg colour to <16,16,16> | |
342 | ||
343 | image.SetMaskColour(16, 16, 16); | |
127eab18 | 344 | image.SetMask(true); |
32b8ec41 VZ |
345 | |
346 | wxDC tmpDC; | |
127eab18 | 347 | tmpDC.SetMGLDC(&idc, false); |
32b8ec41 VZ |
348 | tmpDC.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID)); |
349 | tmpDC.Clear(); | |
127eab18 | 350 | tmpDC.DrawBitmap(*this, 0, 0, true); |
32b8ec41 VZ |
351 | } |
352 | else | |
353 | { | |
127eab18 | 354 | image.SetMask(false); |
32b8ec41 VZ |
355 | idc.putBitmap(0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE); |
356 | } | |
357 | ||
358 | return image; | |
359 | } | |
360 | ||
32b8ec41 VZ |
361 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) |
362 | { | |
363 | LoadFile(filename, type); | |
32b8ec41 VZ |
364 | } |
365 | ||
366 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
367 | { | |
368 | wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") ); | |
369 | ||
370 | if ( !Create(width, height, 1) ) return; | |
371 | MGLDevCtx *bdc = CreateTmpDC(); | |
372 | wxCurrentDCSwitcher curDC(bdc); | |
373 | bdc->setColor(1); | |
374 | bdc->setBackColor(0); | |
375 | bdc->clearDevice(); | |
376 | bdc->putMonoImage(0, 0, width, (width + 7) / 8, height, (void*)bits); | |
377 | delete bdc; | |
32b8ec41 VZ |
378 | } |
379 | ||
32b8ec41 VZ |
380 | bool wxBitmap::operator == (const wxBitmap& bmp) const |
381 | { | |
382 | return (m_refData == bmp.m_refData); | |
383 | } | |
384 | ||
385 | bool wxBitmap::operator != (const wxBitmap& bmp) const | |
386 | { | |
387 | return (m_refData != bmp.m_refData); | |
388 | } | |
389 | ||
390 | bool wxBitmap::Ok() const | |
391 | { | |
392 | return (m_refData != NULL && M_BMPDATA->m_bitmap != NULL); | |
393 | } | |
394 | ||
395 | int wxBitmap::GetHeight() const | |
396 | { | |
397 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
398 | ||
399 | return M_BMPDATA->m_height; | |
400 | } | |
401 | ||
402 | int wxBitmap::GetWidth() const | |
403 | { | |
404 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
405 | ||
406 | return M_BMPDATA->m_width; | |
407 | } | |
408 | ||
409 | int wxBitmap::GetDepth() const | |
410 | { | |
411 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
412 | ||
413 | return M_BMPDATA->m_bpp; | |
414 | } | |
415 | ||
416 | wxMask *wxBitmap::GetMask() const | |
417 | { | |
418 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
419 | ||
420 | return M_BMPDATA->m_mask; | |
421 | } | |
422 | ||
423 | void wxBitmap::SetMask(wxMask *mask) | |
424 | { | |
425 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
426 | ||
427 | delete M_BMPDATA->m_mask; | |
428 | M_BMPDATA->m_mask = mask; | |
429 | } | |
430 | ||
431 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
432 | { | |
433 | wxBitmap *bmp = (wxBitmap*)(&icon); | |
434 | *this = *bmp; | |
127eab18 | 435 | return true; |
32b8ec41 VZ |
436 | } |
437 | ||
438 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const | |
439 | { | |
440 | wxCHECK_MSG( Ok() && | |
441 | (rect.x >= 0) && (rect.y >= 0) && | |
442 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
443 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
444 | ||
445 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); | |
446 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
447 | ||
448 | if ( GetPalette() ) | |
449 | ret.SetPalette(*GetPalette()); | |
450 | ||
451 | MGLDevCtx *tdc = ret.CreateTmpDC(); | |
127eab18 | 452 | tdc->putBitmapSection(rect.x, rect.y, |
32b8ec41 VZ |
453 | rect.x + rect.width, rect.y + rect.height, |
454 | 0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE); | |
455 | delete tdc; | |
456 | ||
457 | if ( GetMask() ) | |
458 | { | |
459 | wxBitmap submask = GetMask()->GetBitmap()->GetSubBitmap(rect); | |
460 | ret.SetMask(new wxMask(submask)); | |
461 | } | |
462 | ||
463 | return ret; | |
464 | } | |
465 | ||
466 | void wxBitmap::SetMonoPalette(const wxColour& fg, const wxColour& bg) | |
467 | { | |
468 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
469 | ||
470 | palette_t *mono = M_BMPDATA->m_bitmap->pal; | |
471 | ||
472 | wxCHECK_RET( M_BMPDATA->m_bpp == 1, wxT("bitmap is not 1bpp") ); | |
473 | wxCHECK_RET( mono != NULL, wxT("bitmap w/o palette") ); | |
474 | ||
475 | mono[0].red = bg.Red(); | |
476 | mono[0].green = bg.Green(); | |
477 | mono[0].blue = bg.Blue(); | |
478 | mono[0].alpha = 0; | |
479 | for (size_t i = 1; i < 256; i++) | |
480 | { | |
481 | mono[i].red = fg.Red(); | |
482 | mono[i].green = fg.Green(); | |
483 | mono[i].blue = fg.Blue(); | |
484 | mono[i].alpha = 0; | |
485 | } | |
486 | } | |
487 | ||
488 | MGLDevCtx *wxBitmap::CreateTmpDC() const | |
489 | { | |
490 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
491 | ||
492 | MGLDevCtx *tdc = new MGLMemoryDC(GetWidth(), GetHeight(), | |
493 | M_BMPDATA->m_bitmap->bitsPerPixel, | |
494 | M_BMPDATA->m_bitmap->pf, | |
495 | M_BMPDATA->m_bitmap->bytesPerLine, | |
127eab18 | 496 | M_BMPDATA->m_bitmap->surface, |
32b8ec41 VZ |
497 | NULL); |
498 | wxCHECK_MSG( tdc->isValid(), NULL, wxT("cannot create temporary MGLDC") ); | |
499 | ||
500 | if ( M_BMPDATA->m_bitmap->pal != NULL ) | |
501 | { | |
502 | int cnt; | |
127eab18 | 503 | |
32b8ec41 VZ |
504 | switch (M_BMPDATA->m_bitmap->bitsPerPixel) |
505 | { | |
506 | case 2: cnt = 2; break; | |
507 | case 4: cnt = 16; break; | |
508 | case 8: cnt = 256; break; | |
509 | default: | |
a4bbc9f7 | 510 | cnt = 0; |
32b8ec41 VZ |
511 | wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") ); |
512 | break; | |
513 | } | |
127eab18 | 514 | |
32b8ec41 VZ |
515 | tdc->setPalette(M_BMPDATA->m_bitmap->pal, cnt, 0); |
516 | tdc->realizePalette(cnt, 0, FALSE); | |
517 | } | |
127eab18 | 518 | |
32b8ec41 VZ |
519 | return tdc; |
520 | } | |
521 | ||
522 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
523 | { | |
524 | UnRef(); | |
127eab18 WS |
525 | |
526 | if ( type == wxBITMAP_TYPE_BMP || type == wxBITMAP_TYPE_PNG || | |
32b8ec41 VZ |
527 | type == wxBITMAP_TYPE_PCX || type == wxBITMAP_TYPE_JPEG ) |
528 | { | |
529 | // prevent accidental loading of bitmap from $MGL_ROOT: | |
530 | if ( !wxFileExists(name) ) | |
531 | { | |
532 | wxLogError(_("File %s does not exist."), name.c_str()); | |
127eab18 | 533 | return false; |
32b8ec41 VZ |
534 | } |
535 | } | |
127eab18 | 536 | |
32b8ec41 VZ |
537 | wxBitmapHandler *handler = FindHandler(type); |
538 | ||
127eab18 | 539 | if ( handler == NULL ) |
32b8ec41 VZ |
540 | { |
541 | wxImage image; | |
542 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
543 | { | |
127eab18 WS |
544 | wxLogError("no bitmap handler for type %d defined.", type); |
545 | return false; | |
32b8ec41 VZ |
546 | } |
547 | else | |
548 | { | |
549 | *this = wxBitmap(image); | |
127eab18 | 550 | return true; |
32b8ec41 VZ |
551 | } |
552 | } | |
553 | ||
554 | m_refData = new wxBitmapRefData(); | |
555 | ||
556 | return handler->LoadFile(this, name, type, -1, -1); | |
557 | } | |
558 | ||
559 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const | |
560 | { | |
127eab18 | 561 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); |
32b8ec41 VZ |
562 | |
563 | wxBitmapHandler *handler = FindHandler(type); | |
564 | ||
127eab18 | 565 | if ( handler == NULL ) |
32b8ec41 VZ |
566 | { |
567 | wxImage image = ConvertToImage(); | |
568 | if ( palette ) | |
569 | image.SetPalette(*palette); | |
570 | ||
571 | if ( image.Ok() ) | |
572 | return image.SaveFile(filename, type); | |
573 | else | |
574 | { | |
575 | wxLogError("no bitmap handler for type %d defined.", type); | |
127eab18 | 576 | return false; |
32b8ec41 VZ |
577 | } |
578 | } | |
579 | ||
580 | return handler->SaveFile(this, filename, type, palette); | |
581 | } | |
582 | ||
583 | wxPalette *wxBitmap::GetPalette() const | |
584 | { | |
585 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
586 | ||
587 | return M_BMPDATA->m_palette; | |
588 | } | |
589 | ||
590 | void wxBitmap::SetPalette(const wxPalette& palette) | |
591 | { | |
592 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
593 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); | |
594 | ||
595 | delete M_BMPDATA->m_palette; | |
596 | M_BMPDATA->m_palette = NULL; | |
597 | ||
598 | if ( !palette.Ok() ) return; | |
127eab18 | 599 | |
32b8ec41 VZ |
600 | M_BMPDATA->m_palette = new wxPalette(palette); |
601 | ||
602 | int cnt = palette.GetColoursCount(); | |
603 | palette_t *pal = palette.GetMGLpalette_t(); | |
604 | memcpy(M_BMPDATA->m_bitmap->pal, pal, cnt * sizeof(palette_t)); | |
605 | } | |
606 | ||
607 | void wxBitmap::SetHeight(int height) | |
608 | { | |
609 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
610 | ||
611 | M_BMPDATA->m_height = height; | |
612 | } | |
613 | ||
614 | void wxBitmap::SetWidth(int width) | |
615 | { | |
616 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
617 | ||
618 | M_BMPDATA->m_width = width; | |
619 | } | |
620 | ||
621 | void wxBitmap::SetDepth(int depth) | |
622 | { | |
623 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
624 | ||
625 | M_BMPDATA->m_bpp = depth; | |
626 | } | |
627 | ||
628 | bitmap_t *wxBitmap::GetMGLbitmap_t() const | |
629 | { | |
630 | return M_BMPDATA->m_bitmap; | |
631 | } | |
632 | ||
633 | ||
634 | ||
635 | //----------------------------------------------------------------------------- | |
636 | // wxBitmap I/O handlers | |
637 | //----------------------------------------------------------------------------- | |
638 | ||
639 | class wxMGLBitmapHandler: public wxBitmapHandler | |
640 | { | |
641 | public: | |
642 | wxMGLBitmapHandler(wxBitmapType type, | |
643 | const wxString& extension, const wxString& name); | |
644 | ||
127eab18 WS |
645 | virtual bool Create(wxBitmap *WXUNUSED(bitmap), |
646 | void *WXUNUSED(data), | |
647 | long WXUNUSED(flags), | |
648 | int WXUNUSED(width), | |
649 | int WXUNUSED(height), | |
650 | int WXUNUSED(depth) = 1) | |
651 | { return false; } | |
32b8ec41 VZ |
652 | |
653 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
654 | int desiredWidth, int desiredHeight); | |
127eab18 | 655 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, |
32b8ec41 VZ |
656 | int type, const wxPalette *palette = NULL); |
657 | }; | |
658 | ||
127eab18 | 659 | wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type, |
32b8ec41 VZ |
660 | const wxString& extension, |
661 | const wxString& name) | |
662 | : wxBitmapHandler() | |
663 | { | |
664 | SetType(type); | |
665 | SetName(name); | |
666 | SetExtension(extension); | |
667 | } | |
668 | ||
127eab18 WS |
669 | bool wxMGLBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, |
670 | long flags, | |
671 | int WXUNUSED(desiredWidth), | |
32b8ec41 VZ |
672 | int WXUNUSED(desiredHeight)) |
673 | { | |
674 | int width, height, bpp; | |
675 | pixel_format_t pf; | |
676 | wxString fullname; | |
677 | wxMemoryDC dc; | |
127eab18 | 678 | |
32b8ec41 VZ |
679 | switch (flags) |
680 | { | |
681 | case wxBITMAP_TYPE_BMP_RESOURCE: | |
682 | case wxBITMAP_TYPE_JPEG_RESOURCE: | |
683 | case wxBITMAP_TYPE_PNG_RESOURCE: | |
684 | case wxBITMAP_TYPE_PCX_RESOURCE: | |
685 | fullname = name + wxT(".bmp"); | |
686 | break; | |
687 | default: | |
688 | fullname= name; | |
689 | break; | |
127eab18 | 690 | } |
32b8ec41 VZ |
691 | |
692 | switch (flags) | |
693 | { | |
694 | case wxBITMAP_TYPE_BMP: | |
695 | case wxBITMAP_TYPE_BMP_RESOURCE: | |
696 | if ( !MGL_getBitmapSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
127eab18 | 697 | return false; |
32b8ec41 | 698 | bitmap->Create(width, height, -1); |
127eab18 | 699 | if ( !bitmap->Ok() ) return false; |
32b8ec41 VZ |
700 | dc.SelectObject(*bitmap); |
701 | if ( !dc.GetMGLDC()->loadBitmapIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
127eab18 | 702 | return false; |
32b8ec41 VZ |
703 | break; |
704 | ||
705 | case wxBITMAP_TYPE_JPEG: | |
706 | case wxBITMAP_TYPE_JPEG_RESOURCE: | |
707 | if ( !MGL_getJPEGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
127eab18 | 708 | return false; |
32b8ec41 | 709 | bitmap->Create(width, height, -1); |
127eab18 | 710 | if ( !bitmap->Ok() ) return false; |
32b8ec41 VZ |
711 | dc.SelectObject(*bitmap); |
712 | if ( !dc.GetMGLDC()->loadJPEGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
127eab18 | 713 | return false; |
32b8ec41 VZ |
714 | break; |
715 | ||
716 | case wxBITMAP_TYPE_PNG: | |
717 | case wxBITMAP_TYPE_PNG_RESOURCE: | |
718 | if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
127eab18 | 719 | return false; |
32b8ec41 | 720 | bitmap->Create(width, height, -1); |
127eab18 | 721 | if ( !bitmap->Ok() ) return false; |
32b8ec41 VZ |
722 | dc.SelectObject(*bitmap); |
723 | if ( !dc.GetMGLDC()->loadPNGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
127eab18 | 724 | return false; |
32b8ec41 VZ |
725 | break; |
726 | ||
727 | case wxBITMAP_TYPE_PCX: | |
728 | case wxBITMAP_TYPE_PCX_RESOURCE: | |
729 | if ( !MGL_getPCXSize(fullname.mb_str(), &width, &height, &bpp) ) | |
127eab18 | 730 | return false; |
32b8ec41 | 731 | bitmap->Create(width, height, -1); |
127eab18 | 732 | if ( !bitmap->Ok() ) return false; |
32b8ec41 VZ |
733 | dc.SelectObject(*bitmap); |
734 | if ( !dc.GetMGLDC()->loadPCXIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
127eab18 | 735 | return false; |
32b8ec41 VZ |
736 | break; |
737 | ||
738 | default: | |
739 | wxFAIL_MSG(wxT("Unsupported image format.")); | |
740 | break; | |
741 | } | |
742 | ||
127eab18 | 743 | return true; |
32b8ec41 VZ |
744 | } |
745 | ||
127eab18 | 746 | bool wxMGLBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, |
32b8ec41 VZ |
747 | int type, const wxPalette * WXUNUSED(palette)) |
748 | { | |
749 | wxMemoryDC mem; | |
750 | MGLDevCtx *tdc; | |
751 | int w = bitmap->GetWidth(), | |
752 | h = bitmap->GetHeight(); | |
753 | ||
754 | mem.SelectObject(*bitmap); | |
755 | tdc = mem.GetMGLDC(); | |
756 | ||
757 | switch (type) | |
758 | { | |
759 | case wxBITMAP_TYPE_BMP: | |
127eab18 | 760 | return (bool)tdc->saveBitmapFromDC(name.mb_str(), 0, 0, w, h); |
32b8ec41 | 761 | case wxBITMAP_TYPE_JPEG: |
127eab18 | 762 | return (bool)tdc->saveJPEGFromDC(name.mb_str(), 0, 0, w, h, 75); |
32b8ec41 | 763 | case wxBITMAP_TYPE_PNG: |
127eab18 | 764 | return (bool)tdc->savePNGFromDC(name.mb_str(), 0, 0, w, h); |
32b8ec41 | 765 | case wxBITMAP_TYPE_PCX: |
127eab18 | 766 | return (bool)tdc->savePCXFromDC(name.mb_str(), 0, 0, w, h); |
32b8ec41 | 767 | } |
127eab18 WS |
768 | |
769 | return false; | |
32b8ec41 VZ |
770 | } |
771 | ||
772 | ||
773 | ||
127eab18 | 774 | // let's handle PNGs in special way because they have alpha channel |
32b8ec41 VZ |
775 | // which we can access via bitmap_t most easily |
776 | class wxPNGBitmapHandler: public wxMGLBitmapHandler | |
777 | { | |
778 | public: | |
779 | wxPNGBitmapHandler(wxBitmapType type, | |
780 | const wxString& extension, const wxString& name) | |
781 | : wxMGLBitmapHandler(type, extension, name) {} | |
782 | ||
783 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
784 | int desiredWidth, int desiredHeight); | |
785 | }; | |
786 | ||
127eab18 WS |
787 | bool wxPNGBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, |
788 | long flags, | |
32b8ec41 VZ |
789 | int desiredWidth, int desiredHeight) |
790 | { | |
791 | int width, height, bpp; | |
792 | pixel_format_t pf; | |
793 | wxString fullname; | |
794 | ||
795 | if ( flags == wxBITMAP_TYPE_PNG_RESOURCE ) | |
796 | fullname = name + wxT(".png"); | |
797 | else | |
798 | fullname = name; | |
799 | ||
800 | if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
127eab18 | 801 | return false; |
32b8ec41 VZ |
802 | |
803 | if ( bpp != 32 ) | |
804 | { | |
805 | // We can load ordinary PNGs faster with 'normal' MGL handler. | |
806 | // Only RGBA PNGs need to be processed in special way because | |
807 | // we have to convert alpha channel to mask | |
127eab18 | 808 | return wxMGLBitmapHandler::LoadFile(bitmap, name, flags, |
32b8ec41 VZ |
809 | desiredWidth, desiredHeight); |
810 | } | |
127eab18 | 811 | |
32b8ec41 | 812 | bitmap_t *bmp = MGL_loadPNG(fullname.mb_str(), TRUE); |
127eab18 WS |
813 | |
814 | if ( bmp == NULL ) return false; | |
32b8ec41 VZ |
815 | |
816 | bitmap->Create(bmp->width, bmp->height, -1); | |
127eab18 WS |
817 | if ( !bitmap->Ok() ) return false; |
818 | ||
32b8ec41 VZ |
819 | // convert bmp to display's depth and write it to *bitmap: |
820 | wxMemoryDC dc; | |
821 | dc.SelectObject(*bitmap); | |
822 | dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE); | |
823 | dc.SelectObject(wxNullBitmap); | |
127eab18 | 824 | |
32b8ec41 VZ |
825 | // create mask, if bmp contains alpha channel (ARGB format): |
826 | if ( bmp->bitsPerPixel == 32 ) | |
827 | { | |
828 | int x, y; | |
829 | wxUint32 *s = (wxUint32*)bmp->surface; | |
830 | for (y = 0; y < bmp->height; y++) | |
831 | { | |
832 | s = ((wxUint32*)bmp->surface) + y * bmp->bytesPerLine/4; | |
833 | for (x = 0; x < bmp->width; x++, s ++) | |
834 | { | |
127eab18 | 835 | if ( ((((*s) >> bmp->pf->alphaPos) & bmp->pf->alphaMask) |
c0f02dbc | 836 | << bmp->pf->alphaAdjust) < 128 ) |
32b8ec41 VZ |
837 | *s = 0; |
838 | else | |
839 | *s = 0x00FFFFFF; // white | |
840 | } | |
841 | } | |
842 | wxBitmap mask(bmp->width, bmp->height, 1); | |
843 | dc.SelectObject(mask); | |
844 | dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE); | |
845 | dc.SelectObject(wxNullBitmap); | |
846 | bitmap->SetMask(new wxMask(mask)); | |
847 | } | |
127eab18 | 848 | |
32b8ec41 | 849 | MGL_unloadBitmap(bmp); |
127eab18 WS |
850 | |
851 | return true; | |
32b8ec41 VZ |
852 | } |
853 | ||
854 | ||
855 | ||
856 | ||
857 | class wxICOBitmapHandler: public wxBitmapHandler | |
858 | { | |
859 | public: | |
860 | wxICOBitmapHandler(wxBitmapType type, | |
861 | const wxString& extension, const wxString& name); | |
127eab18 WS |
862 | |
863 | virtual bool Create(wxBitmap *WXUNUSED(bitmap), | |
864 | void *WXUNUSED(data), | |
865 | long WXUNUSED(flags), | |
866 | int WXUNUSED(width), | |
867 | int WXUNUSED(height), | |
868 | int WXUNUSED(depth) = 1) | |
869 | { return false; } | |
870 | ||
32b8ec41 VZ |
871 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
872 | int desiredWidth, int desiredHeight); | |
127eab18 | 873 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, |
32b8ec41 VZ |
874 | int type, const wxPalette *palette = NULL); |
875 | }; | |
876 | ||
127eab18 | 877 | wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type, |
32b8ec41 VZ |
878 | const wxString& extension, |
879 | const wxString& name) | |
880 | : wxBitmapHandler() | |
881 | { | |
882 | SetType(type); | |
883 | SetName(name); | |
884 | SetExtension(extension); | |
885 | } | |
886 | ||
127eab18 WS |
887 | bool wxICOBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, |
888 | long flags, | |
889 | int WXUNUSED(desiredWidth), | |
32b8ec41 VZ |
890 | int WXUNUSED(desiredHeight)) |
891 | { | |
892 | icon_t *icon = NULL; | |
893 | MGLDevCtx *dc; | |
894 | ||
895 | if ( flags == wxBITMAP_TYPE_ICO_RESOURCE ) | |
896 | icon = MGL_loadIcon(wxString(name + wxT(".ico")).mb_str(), TRUE); | |
127eab18 | 897 | else |
32b8ec41 VZ |
898 | icon = MGL_loadIcon(name.mb_str(), TRUE); |
899 | ||
127eab18 | 900 | if ( icon == NULL ) return false; |
32b8ec41 VZ |
901 | |
902 | bitmap->Create(icon->xorMask.width, icon->xorMask.height); | |
903 | ||
904 | wxMemoryDC mem; | |
905 | mem.SelectObject(*bitmap); | |
906 | dc = mem.GetMGLDC(); | |
907 | dc->putBitmap(0, 0, &(icon->xorMask), MGL_REPLACE_MODE); | |
908 | mem.SelectObject(wxNullBitmap); | |
909 | ||
910 | wxBitmap mask(icon->xorMask.width, icon->xorMask.height, 1); | |
911 | mem.SelectObject(mask); | |
912 | dc = mem.GetMGLDC(); | |
913 | ||
914 | wxCurrentDCSwitcher curDC(dc); | |
915 | dc->setColor(0); | |
916 | dc->setBackColor(1); | |
917 | dc->clearDevice(); | |
918 | dc->putMonoImage(0, 0, icon->xorMask.width, icon->byteWidth, | |
919 | icon->xorMask.height, (void*)icon->andMask); | |
127eab18 | 920 | |
32b8ec41 VZ |
921 | bitmap->SetMask(new wxMask(mask)); |
922 | ||
923 | MGL_unloadIcon(icon); | |
127eab18 WS |
924 | |
925 | return true; | |
32b8ec41 VZ |
926 | } |
927 | ||
127eab18 WS |
928 | bool wxICOBitmapHandler::SaveFile(const wxBitmap *WXUNUSED(bitmap), |
929 | const wxString& WXUNUSED(name), | |
930 | int WXUNUSED(type), | |
931 | const wxPalette * WXUNUSED(palette)) | |
32b8ec41 | 932 | { |
127eab18 | 933 | return false; |
32b8ec41 VZ |
934 | } |
935 | ||
936 | ||
937 | ||
938 | ||
939 | /*static*/ void wxBitmap::InitStandardHandlers() | |
940 | { | |
941 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP, wxT("bmp"), wxT("Windows bitmap"))); | |
942 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE, wxEmptyString, wxT("Windows bitmap resource"))); | |
943 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG, wxT("jpg"), wxT("JPEG image"))); | |
944 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE, wxEmptyString, wxT("JPEG resource"))); | |
945 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX, wxT("pcx"), wxT("PCX image"))); | |
946 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE, wxEmptyString, wxT("PCX resource"))); | |
947 | ||
948 | AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG, wxT("png"), wxT("PNG image"))); | |
949 | AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE, wxEmptyString, wxT("PNG resource"))); | |
950 | ||
951 | AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO, wxT("ico"), wxT("Icon resource"))); | |
952 | AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE, wxEmptyString, wxT("Icon resource"))); | |
953 | } |