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