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