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