1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "bitmap.h"
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/bitmap.h"
22 #include "wx/filefn.h"
24 #include "wx/dcmemory.h"
29 #include "wx/xpmdecod.h"
31 #include "wx/mgl/private.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static pixel_format_t gs_pixel_format_15
=
40 {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp
42 static pixel_format_t gs_pixel_format_16
=
43 {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp
45 static pixel_format_t gs_pixel_format_24
=
46 {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp
48 static pixel_format_t gs_pixel_format_32
=
49 {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp
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
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 // Convert wxColour into it's quantized value in lower-precision
59 // pixel format (needed for masking by colour).
60 static wxColour
wxQuantizeColour(const wxColour
& clr
, const wxBitmap
& bmp
)
62 pixel_format_t
*pf
= bmp
.GetMGLbitmap_t()->pf
;
64 if ( pf
->redAdjust
== 0 && pf
->greenAdjust
== 0 && pf
->blueAdjust
== 0 )
67 return wxColour((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
,
68 (clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
,
69 (clr
.Blue() >> pf
->blueAdjust
) << pf
->blueAdjust
);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
78 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
85 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
88 Create(bitmap
, colour
);
91 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
94 Create(bitmap
, paletteIndex
);
97 wxMask::wxMask(const wxBitmap
& bitmap
)
108 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
113 wxColour
clr(wxQuantizeColour(colour
, bitmap
));
115 wxImage
imgSrc(bitmap
.ConvertToImage());
116 imgSrc
.SetMask(FALSE
);
117 wxImage
image(imgSrc
.ConvertToMono(clr
.Red(), clr
.Green(), clr
.Blue()));
118 if ( !image
.Ok() ) return FALSE
;
120 m_bitmap
= new wxBitmap(image
, 1);
122 return m_bitmap
->Ok();
125 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
128 wxPalette
*pal
= bitmap
.GetPalette();
130 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
132 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
134 return Create(bitmap
, wxColour(r
, g
, b
));
137 bool wxMask::Create(const wxBitmap
& bitmap
)
142 wxCHECK_MSG( bitmap
.Ok(), FALSE
, wxT("Invalid bitmap") );
143 wxCHECK_MSG( bitmap
.GetDepth() == 1, FALSE
, wxT("Cannot create mask from colour bitmap") );
145 m_bitmap
= new wxBitmap(bitmap
);
150 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
154 class wxBitmapRefData
: public wxObjectRefData
163 wxPalette
*m_palette
;
168 wxBitmapRefData::wxBitmapRefData()
178 wxBitmapRefData::~wxBitmapRefData()
181 MGL_unloadBitmap(m_bitmap
);
186 //-----------------------------------------------------------------------------
188 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
191 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
,wxObject
)
192 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxBitmapBase
)
196 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
199 wxBitmap::wxBitmap(int width
, int height
, int depth
)
201 Create(width
, height
, depth
);
203 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
207 static bitmap_t
*MyMGL_createBitmap(int width
, int height
,
208 int bpp
, pixel_format_t
*pf
)
210 MGLMemoryDC
mdc(width
, height
, bpp
, pf
);
211 return MGL_getBitmapFromDC(mdc
.getDC(), 0, 0, width
, height
, TRUE
);
214 bool wxBitmap::Create(int width
, int height
, int depth
)
218 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
220 pixel_format_t pf_dummy
, *pf
;
221 int mglDepth
= depth
;
226 wxASSERT_MSG( g_displayDC
, wxT("MGL display DC not created yet.") );
228 g_displayDC
->getPixelFormat(pf_dummy
);
229 mglDepth
= g_displayDC
->getBitsPerPixel();
237 pf
= &gs_pixel_format_15
;
240 pf
= &gs_pixel_format_16
;
243 pf
= &gs_pixel_format_24
;
246 pf
= &gs_pixel_format_32
;
249 wxASSERT_MSG( 0, wxT("invalid bitmap depth") );
254 m_refData
= new wxBitmapRefData();
255 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
256 M_BMPDATA
->m_palette
= (wxPalette
*) NULL
;
257 M_BMPDATA
->m_width
= width
;
258 M_BMPDATA
->m_height
= height
;
259 M_BMPDATA
->m_bpp
= mglDepth
;
263 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, mglDepth
, pf
);
267 // MGL does not support mono DCs, so we have to emulate them with
268 // 8bpp ones. We do that by using a special palette with color 0
269 // set to black and all other colors set to white.
271 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, 8, pf
);
272 SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0));
278 bool wxBitmap::CreateFromXpm(const char **bits
)
280 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
282 wxXPMDecoder decoder
;
283 wxImage img
= decoder
.ReadData(bits
);
284 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
286 *this = wxBitmap(img
);
288 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
293 wxBitmap::wxBitmap(const wxImage
& image
, int depth
= -1)
297 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
299 width
= image
.GetWidth();
300 height
= image
.GetHeight();
302 if ( !Create(width
, height
, depth
) ) return;
304 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
305 width
* 3, (void*)image
.GetData(), NULL
);
306 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
308 MGLDevCtx
*bdc
= CreateTmpDC();
310 if ( GetDepth() <= 8 && image
.HasPalette() )
311 SetPalette(image
.GetPalette());
313 bdc
->bitBlt(idc
, 0, 0, width
, height
, 0, 0, MGL_REPLACE_MODE
);
316 if ( image
.HasMask() )
318 wxImage mask_image
= image
.ConvertToMono(image
.GetMaskRed(),
319 image
.GetMaskGreen(),
320 image
.GetMaskBlue());
321 mask_image
.SetMask(FALSE
);
322 wxBitmap
mask_bmp(mask_image
, 1);
323 SetMask(new wxMask(mask_bmp
));
327 wxImage
wxBitmap::ConvertToImage() const
329 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
333 height
= GetHeight();
335 wxImage
image(width
, height
);
336 wxASSERT_MSG( image
.Ok(), wxT("cannot create image") );
338 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
339 width
* 3, (void*)image
.GetData(), NULL
);
340 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
342 if ( M_BMPDATA
->m_palette
)
343 image
.SetPalette(*(M_BMPDATA
->m_palette
));
347 // in consistency with other ports, we convert parts covered
348 // by the mask to <16,16,16> colour and set that colour to image's
349 // mask. We do that by OR-blitting the mask over image with
350 // bg colour set to black and fg colour to <16,16,16>
352 image
.SetMaskColour(16, 16, 16);
356 tmpDC
.SetMGLDC(&idc
, FALSE
);
357 tmpDC
.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID
));
359 tmpDC
.DrawBitmap(*this, 0, 0, TRUE
);
363 image
.SetMask(FALSE
);
364 idc
.putBitmap(0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
370 wxBitmap::wxBitmap(const wxBitmap
& bmp
)
374 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
377 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
379 LoadFile(filename
, type
);
381 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
384 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
386 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
388 if ( !Create(width
, height
, 1) ) return;
389 MGLDevCtx
*bdc
= CreateTmpDC();
390 wxCurrentDCSwitcher
curDC(bdc
);
392 bdc
->setBackColor(0);
394 bdc
->putMonoImage(0, 0, width
, (width
+ 7) / 8, height
, (void*)bits
);
397 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
400 wxBitmap::~wxBitmap()
402 if ( wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
405 wxBitmap
& wxBitmap::operator = (const wxBitmap
& bmp
)
407 if ( *this == bmp
) return (*this);
412 bool wxBitmap::operator == (const wxBitmap
& bmp
) const
414 return (m_refData
== bmp
.m_refData
);
417 bool wxBitmap::operator != (const wxBitmap
& bmp
) const
419 return (m_refData
!= bmp
.m_refData
);
422 bool wxBitmap::Ok() const
424 return (m_refData
!= NULL
&& M_BMPDATA
->m_bitmap
!= NULL
);
427 int wxBitmap::GetHeight() const
429 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
431 return M_BMPDATA
->m_height
;
434 int wxBitmap::GetWidth() const
436 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
438 return M_BMPDATA
->m_width
;
441 int wxBitmap::GetDepth() const
443 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
445 return M_BMPDATA
->m_bpp
;
448 wxMask
*wxBitmap::GetMask() const
450 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
452 return M_BMPDATA
->m_mask
;
455 void wxBitmap::SetMask(wxMask
*mask
)
457 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
459 delete M_BMPDATA
->m_mask
;
460 M_BMPDATA
->m_mask
= mask
;
463 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
465 wxBitmap
*bmp
= (wxBitmap
*)(&icon
);
470 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
473 (rect
.x
>= 0) && (rect
.y
>= 0) &&
474 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
475 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
477 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
478 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
481 ret
.SetPalette(*GetPalette());
483 MGLDevCtx
*tdc
= ret
.CreateTmpDC();
484 tdc
->putBitmapSection(rect
.x
, rect
.y
,
485 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
486 0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
491 wxBitmap submask
= GetMask()->GetBitmap()->GetSubBitmap(rect
);
492 ret
.SetMask(new wxMask(submask
));
498 void wxBitmap::SetMonoPalette(const wxColour
& fg
, const wxColour
& bg
)
500 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
502 palette_t
*mono
= M_BMPDATA
->m_bitmap
->pal
;
504 wxCHECK_RET( M_BMPDATA
->m_bpp
== 1, wxT("bitmap is not 1bpp") );
505 wxCHECK_RET( mono
!= NULL
, wxT("bitmap w/o palette") );
507 mono
[0].red
= bg
.Red();
508 mono
[0].green
= bg
.Green();
509 mono
[0].blue
= bg
.Blue();
511 for (size_t i
= 1; i
< 256; i
++)
513 mono
[i
].red
= fg
.Red();
514 mono
[i
].green
= fg
.Green();
515 mono
[i
].blue
= fg
.Blue();
520 MGLDevCtx
*wxBitmap::CreateTmpDC() const
522 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
524 MGLDevCtx
*tdc
= new MGLMemoryDC(GetWidth(), GetHeight(),
525 M_BMPDATA
->m_bitmap
->bitsPerPixel
,
526 M_BMPDATA
->m_bitmap
->pf
,
527 M_BMPDATA
->m_bitmap
->bytesPerLine
,
528 M_BMPDATA
->m_bitmap
->surface
,
530 wxCHECK_MSG( tdc
->isValid(), NULL
, wxT("cannot create temporary MGLDC") );
532 if ( M_BMPDATA
->m_bitmap
->pal
!= NULL
)
536 switch (M_BMPDATA
->m_bitmap
->bitsPerPixel
)
538 case 2: cnt
= 2; break;
539 case 4: cnt
= 16; break;
540 case 8: cnt
= 256; break;
543 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
547 tdc
->setPalette(M_BMPDATA
->m_bitmap
->pal
, cnt
, 0);
548 tdc
->realizePalette(cnt
, 0, FALSE
);
554 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
558 if ( type
== wxBITMAP_TYPE_BMP
|| type
== wxBITMAP_TYPE_PNG
||
559 type
== wxBITMAP_TYPE_PCX
|| type
== wxBITMAP_TYPE_JPEG
)
561 // prevent accidental loading of bitmap from $MGL_ROOT:
562 if ( !wxFileExists(name
) )
564 wxLogError(_("File %s does not exist."), name
.c_str());
569 wxBitmapHandler
*handler
= FindHandler(type
);
571 if ( handler
== NULL
)
574 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
576 wxLogError("no bitmap handler for type %d defined.", type
);
581 *this = wxBitmap(image
);
586 m_refData
= new wxBitmapRefData();
588 return handler
->LoadFile(this, name
, type
, -1, -1);
591 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
593 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
595 wxBitmapHandler
*handler
= FindHandler(type
);
597 if ( handler
== NULL
)
599 wxImage image
= ConvertToImage();
601 image
.SetPalette(*palette
);
604 return image
.SaveFile(filename
, type
);
607 wxLogError("no bitmap handler for type %d defined.", type
);
612 return handler
->SaveFile(this, filename
, type
, palette
);
615 wxPalette
*wxBitmap::GetPalette() const
617 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
619 return M_BMPDATA
->m_palette
;
622 void wxBitmap::SetPalette(const wxPalette
& palette
)
624 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
625 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
627 delete M_BMPDATA
->m_palette
;
628 M_BMPDATA
->m_palette
= NULL
;
630 if ( !palette
.Ok() ) return;
632 M_BMPDATA
->m_palette
= new wxPalette(palette
);
634 int cnt
= palette
.GetColoursCount();
635 palette_t
*pal
= palette
.GetMGLpalette_t();
636 memcpy(M_BMPDATA
->m_bitmap
->pal
, pal
, cnt
* sizeof(palette_t
));
639 void wxBitmap::SetHeight(int height
)
641 if (!m_refData
) m_refData
= new wxBitmapRefData();
643 M_BMPDATA
->m_height
= height
;
646 void wxBitmap::SetWidth(int width
)
648 if (!m_refData
) m_refData
= new wxBitmapRefData();
650 M_BMPDATA
->m_width
= width
;
653 void wxBitmap::SetDepth(int depth
)
655 if (!m_refData
) m_refData
= new wxBitmapRefData();
657 M_BMPDATA
->m_bpp
= depth
;
660 bitmap_t
*wxBitmap::GetMGLbitmap_t() const
662 return M_BMPDATA
->m_bitmap
;
667 //-----------------------------------------------------------------------------
668 // wxBitmap I/O handlers
669 //-----------------------------------------------------------------------------
671 class wxMGLBitmapHandler
: public wxBitmapHandler
674 wxMGLBitmapHandler(wxBitmapType type
,
675 const wxString
& extension
, const wxString
& name
);
677 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
678 int width
, int height
, int depth
= 1)
681 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
682 int desiredWidth
, int desiredHeight
);
683 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
684 int type
, const wxPalette
*palette
= NULL
);
687 wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type
,
688 const wxString
& extension
,
689 const wxString
& name
)
694 SetExtension(extension
);
697 bool wxMGLBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
699 int WXUNUSED(desiredWidth
),
700 int WXUNUSED(desiredHeight
))
702 int width
, height
, bpp
;
709 case wxBITMAP_TYPE_BMP_RESOURCE
:
710 case wxBITMAP_TYPE_JPEG_RESOURCE
:
711 case wxBITMAP_TYPE_PNG_RESOURCE
:
712 case wxBITMAP_TYPE_PCX_RESOURCE
:
713 fullname
= name
+ wxT(".bmp");
722 case wxBITMAP_TYPE_BMP
:
723 case wxBITMAP_TYPE_BMP_RESOURCE
:
724 if ( !MGL_getBitmapSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
726 bitmap
->Create(width
, height
, -1);
727 if ( !bitmap
->Ok() ) return FALSE
;
728 dc
.SelectObject(*bitmap
);
729 if ( !dc
.GetMGLDC()->loadBitmapIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
733 case wxBITMAP_TYPE_JPEG
:
734 case wxBITMAP_TYPE_JPEG_RESOURCE
:
735 if ( !MGL_getJPEGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
737 bitmap
->Create(width
, height
, -1);
738 if ( !bitmap
->Ok() ) return FALSE
;
739 dc
.SelectObject(*bitmap
);
740 if ( !dc
.GetMGLDC()->loadJPEGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
744 case wxBITMAP_TYPE_PNG
:
745 case wxBITMAP_TYPE_PNG_RESOURCE
:
746 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
748 bitmap
->Create(width
, height
, -1);
749 if ( !bitmap
->Ok() ) return FALSE
;
750 dc
.SelectObject(*bitmap
);
751 if ( !dc
.GetMGLDC()->loadPNGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
755 case wxBITMAP_TYPE_PCX
:
756 case wxBITMAP_TYPE_PCX_RESOURCE
:
757 if ( !MGL_getPCXSize(fullname
.mb_str(), &width
, &height
, &bpp
) )
759 bitmap
->Create(width
, height
, -1);
760 if ( !bitmap
->Ok() ) return FALSE
;
761 dc
.SelectObject(*bitmap
);
762 if ( !dc
.GetMGLDC()->loadPCXIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
767 wxFAIL_MSG(wxT("Unsupported image format."));
774 bool wxMGLBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
775 int type
, const wxPalette
* WXUNUSED(palette
))
779 int w
= bitmap
->GetWidth(),
780 h
= bitmap
->GetHeight();
782 mem
.SelectObject(*bitmap
);
783 tdc
= mem
.GetMGLDC();
787 case wxBITMAP_TYPE_BMP
:
788 return tdc
->saveBitmapFromDC(name
.mb_str(), 0, 0, w
, h
);
790 case wxBITMAP_TYPE_JPEG
:
791 return tdc
->saveJPEGFromDC(name
.mb_str(), 0, 0, w
, h
, 75);
793 case wxBITMAP_TYPE_PNG
:
794 return tdc
->savePNGFromDC(name
.mb_str(), 0, 0, w
, h
);
796 case wxBITMAP_TYPE_PCX
:
797 return tdc
->savePCXFromDC(name
.mb_str(), 0, 0, w
, h
);
807 // let's handle PNGs in special way because they have alpha channel
808 // which we can access via bitmap_t most easily
809 class wxPNGBitmapHandler
: public wxMGLBitmapHandler
812 wxPNGBitmapHandler(wxBitmapType type
,
813 const wxString
& extension
, const wxString
& name
)
814 : wxMGLBitmapHandler(type
, extension
, name
) {}
816 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
817 int desiredWidth
, int desiredHeight
);
820 bool wxPNGBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
822 int desiredWidth
, int desiredHeight
)
824 int width
, height
, bpp
;
828 if ( flags
== wxBITMAP_TYPE_PNG_RESOURCE
)
829 fullname
= name
+ wxT(".png");
833 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
838 // We can load ordinary PNGs faster with 'normal' MGL handler.
839 // Only RGBA PNGs need to be processed in special way because
840 // we have to convert alpha channel to mask
841 return wxMGLBitmapHandler::LoadFile(bitmap
, name
, flags
,
842 desiredWidth
, desiredHeight
);
845 bitmap_t
*bmp
= MGL_loadPNG(fullname
.mb_str(), TRUE
);
847 if ( bmp
== NULL
) return FALSE
;
849 bitmap
->Create(bmp
->width
, bmp
->height
, -1);
850 if ( !bitmap
->Ok() ) return FALSE
;
852 // convert bmp to display's depth and write it to *bitmap:
854 dc
.SelectObject(*bitmap
);
855 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
856 dc
.SelectObject(wxNullBitmap
);
858 // create mask, if bmp contains alpha channel (ARGB format):
859 if ( bmp
->bitsPerPixel
== 32 )
862 wxUint32
*s
= (wxUint32
*)bmp
->surface
;
863 for (y
= 0; y
< bmp
->height
; y
++)
865 s
= ((wxUint32
*)bmp
->surface
) + y
* bmp
->bytesPerLine
/4;
866 for (x
= 0; x
< bmp
->width
; x
++, s
++)
868 if ( ((((*s
) >> bmp
->pf
->rsvdPos
) & bmp
->pf
->rsvdMask
)
869 << bmp
->pf
->rsvdAdjust
) < 128 )
872 *s
= 0x00FFFFFF; // white
875 wxBitmap
mask(bmp
->width
, bmp
->height
, 1);
876 dc
.SelectObject(mask
);
877 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
878 dc
.SelectObject(wxNullBitmap
);
879 bitmap
->SetMask(new wxMask(mask
));
882 MGL_unloadBitmap(bmp
);
890 class wxICOBitmapHandler
: public wxBitmapHandler
893 wxICOBitmapHandler(wxBitmapType type
,
894 const wxString
& extension
, const wxString
& name
);
896 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
897 int width
, int height
, int depth
= 1)
900 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
901 int desiredWidth
, int desiredHeight
);
902 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
903 int type
, const wxPalette
*palette
= NULL
);
906 wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type
,
907 const wxString
& extension
,
908 const wxString
& name
)
913 SetExtension(extension
);
916 bool wxICOBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
918 int WXUNUSED(desiredWidth
),
919 int WXUNUSED(desiredHeight
))
924 if ( flags
== wxBITMAP_TYPE_ICO_RESOURCE
)
925 icon
= MGL_loadIcon(wxString(name
+ wxT(".ico")).mb_str(), TRUE
);
927 icon
= MGL_loadIcon(name
.mb_str(), TRUE
);
929 if ( icon
== NULL
) return FALSE
;
931 bitmap
->Create(icon
->xorMask
.width
, icon
->xorMask
.height
);
934 mem
.SelectObject(*bitmap
);
936 dc
->putBitmap(0, 0, &(icon
->xorMask
), MGL_REPLACE_MODE
);
937 mem
.SelectObject(wxNullBitmap
);
939 wxBitmap
mask(icon
->xorMask
.width
, icon
->xorMask
.height
, 1);
940 mem
.SelectObject(mask
);
943 wxCurrentDCSwitcher
curDC(dc
);
947 dc
->putMonoImage(0, 0, icon
->xorMask
.width
, icon
->byteWidth
,
948 icon
->xorMask
.height
, (void*)icon
->andMask
);
950 bitmap
->SetMask(new wxMask(mask
));
952 MGL_unloadIcon(icon
);
957 bool wxICOBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
958 int type
, const wxPalette
* WXUNUSED(palette
))
966 /*static*/ void wxBitmap::InitStandardHandlers()
968 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP
, wxT("bmp"), wxT("Windows bitmap")));
969 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE
, wxEmptyString
, wxT("Windows bitmap resource")));
970 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG
, wxT("jpg"), wxT("JPEG image")));
971 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE
, wxEmptyString
, wxT("JPEG resource")));
972 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX
, wxT("pcx"), wxT("PCX image")));
973 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE
, wxEmptyString
, wxT("PCX resource")));
975 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG
, wxT("png"), wxT("PNG image")));
976 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE
, wxEmptyString
, wxT("PNG resource")));
978 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO
, wxT("ico"), wxT("Icon resource")));
979 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE
, wxEmptyString
, wxT("Icon resource")));