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