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