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