]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mgl/bitmap.cpp | |
3 | // Author: Vaclav Slavik | |
4 | // RCS-ID: $Id$ | |
5 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
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 = | |
36 | {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp | |
37 | ||
38 | static pixel_format_t gs_pixel_format_16 = | |
39 | {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp | |
40 | ||
41 | static pixel_format_t gs_pixel_format_24 = | |
42 | {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp | |
43 | ||
44 | static pixel_format_t gs_pixel_format_32 = | |
45 | {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp | |
46 | ||
47 | static pixel_format_t gs_pixel_format_wxImage = | |
48 | {0xFF,0x00,0, 0xFF,0x08,0, 0xFF,0x10,0, 0x00,0x00,0}; // RGB 24bpp for wxImage | |
49 | ||
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; | |
59 | ||
60 | if ( pf->redAdjust == 0 && pf->greenAdjust == 0 && pf->blueAdjust == 0 ) | |
61 | return clr; | |
62 | else | |
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)); | |
66 | } | |
67 | ||
68 | ||
69 | ||
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; | |
108 | ||
109 | wxColour clr(wxQuantizeColour(colour, bitmap)); | |
110 | ||
111 | wxImage imgSrc(bitmap.ConvertToImage()); | |
112 | imgSrc.SetMask(false); | |
113 | wxImage image(imgSrc.ConvertToMono(clr.Red(), clr.Green(), clr.Blue())); | |
114 | if ( !image.Ok() ) | |
115 | return false; | |
116 | ||
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 | ||
127 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); | |
128 | ||
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 | ||
139 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap") ); | |
140 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); | |
141 | ||
142 | m_bitmap = new wxBitmap(bitmap); | |
143 | return true; | |
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 | ||
191 | wxBitmap::wxBitmap(int width, int height, int depth) | |
192 | { | |
193 | Create(width, height, depth); | |
194 | } | |
195 | ||
196 | ||
197 | static bitmap_t *MyMGL_createBitmap(int width, int height, | |
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 | ||
208 | wxCHECK_MSG( (width > 0) && (height > 0), false, wxT("invalid bitmap size") ); | |
209 | ||
210 | pixel_format_t pf_dummy; | |
211 | pixel_format_t *pf; | |
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); | |
220 | mglDepth = g_displayDC->getBitsPerPixel(); | |
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 | wxFAIL_MSG(wxT("invalid bitmap depth")); | |
241 | return false; | |
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 | |
259 | // set to black and all other colors set to white. | |
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 | { | |
270 | wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); | |
271 | ||
272 | wxXPMDecoder decoder; | |
273 | wxImage img = decoder.ReadData(bits); | |
274 | wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") ); | |
275 | ||
276 | *this = wxBitmap(img); | |
277 | ||
278 | return true; | |
279 | } | |
280 | ||
281 | wxBitmap::wxBitmap(const wxImage& image, int depth) | |
282 | { | |
283 | long width, height; | |
284 | ||
285 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
286 | ||
287 | width = image.GetWidth(); | |
288 | height = image.GetHeight(); | |
289 | ||
290 | if ( !Create(width, height, depth) ) return; | |
291 | ||
292 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_wxImage, | |
293 | width * 3, (void*)image.GetData(), NULL); | |
294 | wxASSERT_MSG( idc.isValid(), wxT("cannot create custom MGLDC") ); | |
295 | ||
296 | MGLDevCtx *bdc = CreateTmpDC(); | |
297 | ||
298 | if ( GetDepth() <= 8 && image.HasPalette() ) | |
299 | SetPalette(image.GetPalette()); | |
300 | ||
301 | bdc->bitBlt(idc, 0, 0, width, height, 0, 0, MGL_REPLACE_MODE); | |
302 | delete bdc; | |
303 | ||
304 | if ( image.HasMask() ) | |
305 | { | |
306 | wxImage mask_image = image.ConvertToMono(image.GetMaskRed(), | |
307 | image.GetMaskGreen(), | |
308 | image.GetMaskBlue()); | |
309 | mask_image.SetMask(false); | |
310 | wxBitmap mask_bmp(mask_image, 1); | |
311 | SetMask(new wxMask(mask_bmp)); | |
312 | } | |
313 | } | |
314 | ||
315 | wxImage wxBitmap::ConvertToImage() const | |
316 | { | |
317 | wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") ); | |
318 | ||
319 | int width, height; | |
320 | width = GetWidth(); | |
321 | height = GetHeight(); | |
322 | ||
323 | wxImage image(width, height); | |
324 | wxASSERT_MSG( image.Ok(), wxT("cannot create image") ); | |
325 | ||
326 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_wxImage, | |
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)); | |
332 | ||
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); | |
341 | image.SetMask(true); | |
342 | ||
343 | wxDC tmpDC; | |
344 | tmpDC.SetMGLDC(&idc, false); | |
345 | tmpDC.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID)); | |
346 | tmpDC.Clear(); | |
347 | tmpDC.DrawBitmap(*this, 0, 0, true); | |
348 | } | |
349 | else | |
350 | { | |
351 | image.SetMask(false); | |
352 | idc.putBitmap(0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE); | |
353 | } | |
354 | ||
355 | return image; | |
356 | } | |
357 | ||
358 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
359 | { | |
360 | LoadFile(filename, type); | |
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; | |
375 | } | |
376 | ||
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; | |
432 | return true; | |
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(); | |
449 | tdc->putBitmapSection(rect.x, rect.y, | |
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, | |
493 | M_BMPDATA->m_bitmap->surface, | |
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; | |
500 | ||
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: | |
507 | cnt = 0; | |
508 | wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") ); | |
509 | break; | |
510 | } | |
511 | ||
512 | tdc->setPalette(M_BMPDATA->m_bitmap->pal, cnt, 0); | |
513 | tdc->realizePalette(cnt, 0, FALSE); | |
514 | } | |
515 | ||
516 | return tdc; | |
517 | } | |
518 | ||
519 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
520 | { | |
521 | UnRef(); | |
522 | ||
523 | if ( type == wxBITMAP_TYPE_BMP || type == wxBITMAP_TYPE_PNG || | |
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()); | |
530 | return false; | |
531 | } | |
532 | } | |
533 | ||
534 | wxBitmapHandler *handler = FindHandler(type); | |
535 | ||
536 | if ( handler == NULL ) | |
537 | { | |
538 | wxImage image; | |
539 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
540 | { | |
541 | wxLogError("no bitmap handler for type %d defined.", type); | |
542 | return false; | |
543 | } | |
544 | else | |
545 | { | |
546 | *this = wxBitmap(image); | |
547 | return true; | |
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 | { | |
558 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
559 | ||
560 | wxBitmapHandler *handler = FindHandler(type); | |
561 | ||
562 | if ( handler == NULL ) | |
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); | |
573 | return false; | |
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; | |
596 | ||
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 | ||
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; } | |
649 | ||
650 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
651 | int desiredWidth, int desiredHeight); | |
652 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, | |
653 | int type, const wxPalette *palette = NULL); | |
654 | }; | |
655 | ||
656 | wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type, | |
657 | const wxString& extension, | |
658 | const wxString& name) | |
659 | : wxBitmapHandler() | |
660 | { | |
661 | SetType(type); | |
662 | SetName(name); | |
663 | SetExtension(extension); | |
664 | } | |
665 | ||
666 | bool wxMGLBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
667 | long flags, | |
668 | int WXUNUSED(desiredWidth), | |
669 | int WXUNUSED(desiredHeight)) | |
670 | { | |
671 | int width, height, bpp; | |
672 | pixel_format_t pf; | |
673 | wxString fullname; | |
674 | wxMemoryDC dc; | |
675 | ||
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; | |
687 | } | |
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) ) | |
694 | return false; | |
695 | bitmap->Create(width, height, -1); | |
696 | if ( !bitmap->Ok() ) return false; | |
697 | dc.SelectObject(*bitmap); | |
698 | if ( !dc.GetMGLDC()->loadBitmapIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
699 | return false; | |
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) ) | |
705 | return false; | |
706 | bitmap->Create(width, height, -1); | |
707 | if ( !bitmap->Ok() ) return false; | |
708 | dc.SelectObject(*bitmap); | |
709 | if ( !dc.GetMGLDC()->loadJPEGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
710 | return false; | |
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) ) | |
716 | return false; | |
717 | bitmap->Create(width, height, -1); | |
718 | if ( !bitmap->Ok() ) return false; | |
719 | dc.SelectObject(*bitmap); | |
720 | if ( !dc.GetMGLDC()->loadPNGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
721 | return false; | |
722 | break; | |
723 | ||
724 | case wxBITMAP_TYPE_PCX: | |
725 | case wxBITMAP_TYPE_PCX_RESOURCE: | |
726 | if ( !MGL_getPCXSize(fullname.mb_str(), &width, &height, &bpp) ) | |
727 | return false; | |
728 | bitmap->Create(width, height, -1); | |
729 | if ( !bitmap->Ok() ) return false; | |
730 | dc.SelectObject(*bitmap); | |
731 | if ( !dc.GetMGLDC()->loadPCXIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
732 | return false; | |
733 | break; | |
734 | ||
735 | default: | |
736 | wxFAIL_MSG(wxT("Unsupported image format.")); | |
737 | break; | |
738 | } | |
739 | ||
740 | return true; | |
741 | } | |
742 | ||
743 | bool wxMGLBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, | |
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: | |
757 | return (bool)tdc->saveBitmapFromDC(name.mb_str(), 0, 0, w, h); | |
758 | case wxBITMAP_TYPE_JPEG: | |
759 | return (bool)tdc->saveJPEGFromDC(name.mb_str(), 0, 0, w, h, 75); | |
760 | case wxBITMAP_TYPE_PNG: | |
761 | return (bool)tdc->savePNGFromDC(name.mb_str(), 0, 0, w, h); | |
762 | case wxBITMAP_TYPE_PCX: | |
763 | return (bool)tdc->savePCXFromDC(name.mb_str(), 0, 0, w, h); | |
764 | } | |
765 | ||
766 | return false; | |
767 | } | |
768 | ||
769 | ||
770 | ||
771 | // let's handle PNGs in special way because they have alpha channel | |
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 | ||
784 | bool wxPNGBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
785 | long flags, | |
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) ) | |
798 | return false; | |
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 | |
805 | return wxMGLBitmapHandler::LoadFile(bitmap, name, flags, | |
806 | desiredWidth, desiredHeight); | |
807 | } | |
808 | ||
809 | bitmap_t *bmp = MGL_loadPNG(fullname.mb_str(), TRUE); | |
810 | ||
811 | if ( bmp == NULL ) return false; | |
812 | ||
813 | bitmap->Create(bmp->width, bmp->height, -1); | |
814 | if ( !bitmap->Ok() ) return false; | |
815 | ||
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); | |
821 | ||
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 | { | |
832 | if ( ((((*s) >> bmp->pf->alphaPos) & bmp->pf->alphaMask) | |
833 | << bmp->pf->alphaAdjust) < 128 ) | |
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 | } | |
845 | ||
846 | MGL_unloadBitmap(bmp); | |
847 | ||
848 | return true; | |
849 | } | |
850 | ||
851 | ||
852 | ||
853 | ||
854 | class wxICOBitmapHandler: public wxBitmapHandler | |
855 | { | |
856 | public: | |
857 | wxICOBitmapHandler(wxBitmapType type, | |
858 | const wxString& extension, const wxString& name); | |
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 | ||
868 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
869 | int desiredWidth, int desiredHeight); | |
870 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, | |
871 | int type, const wxPalette *palette = NULL); | |
872 | }; | |
873 | ||
874 | wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type, | |
875 | const wxString& extension, | |
876 | const wxString& name) | |
877 | : wxBitmapHandler() | |
878 | { | |
879 | SetType(type); | |
880 | SetName(name); | |
881 | SetExtension(extension); | |
882 | } | |
883 | ||
884 | bool wxICOBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
885 | long flags, | |
886 | int WXUNUSED(desiredWidth), | |
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); | |
894 | else | |
895 | icon = MGL_loadIcon(name.mb_str(), TRUE); | |
896 | ||
897 | if ( icon == NULL ) return false; | |
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); | |
917 | ||
918 | bitmap->SetMask(new wxMask(mask)); | |
919 | ||
920 | MGL_unloadIcon(icon); | |
921 | ||
922 | return true; | |
923 | } | |
924 | ||
925 | bool wxICOBitmapHandler::SaveFile(const wxBitmap *WXUNUSED(bitmap), | |
926 | const wxString& WXUNUSED(name), | |
927 | int WXUNUSED(type), | |
928 | const wxPalette * WXUNUSED(palette)) | |
929 | { | |
930 | return false; | |
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 | } |