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