1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
5 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
16 #include "wx/bitmap.h"
18 #include "wx/filefn.h"
20 #include "wx/dcmemory.h"
25 #include "wx/xpmdecod.h"
27 #include "wx/mgl/private.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 static pixel_format_t gs_pixel_format_15
=
36 {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp
38 static pixel_format_t gs_pixel_format_16
=
39 {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp
41 static pixel_format_t gs_pixel_format_24
=
42 {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp
44 static pixel_format_t gs_pixel_format_32
=
45 {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp
47 static pixel_format_t gs_pixel_format_wxImage
=
48 {0xFF,0x00,0, 0xFF,0x08,0, 0xFF,0x10,0, 0x00,0x00,0}; // RGB 24bpp for wxImage
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 // Convert wxColour into it's quantized value in lower-precision
55 // pixel format (needed for masking by colour).
56 static wxColour
wxQuantizeColour(const wxColour
& clr
, const wxBitmap
& bmp
)
58 pixel_format_t
*pf
= bmp
.GetMGLbitmap_t()->pf
;
60 if ( pf
->redAdjust
== 0 && pf
->greenAdjust
== 0 && pf
->blueAdjust
== 0 )
63 return wxColour((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
,
64 (clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
,
65 (clr
.Blue() >> pf
->blueAdjust
) << pf
->blueAdjust
);
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
81 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
84 Create(bitmap
, colour
);
87 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
90 Create(bitmap
, paletteIndex
);
93 wxMask::wxMask(const wxBitmap
& bitmap
)
104 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
109 wxColour
clr(wxQuantizeColour(colour
, bitmap
));
111 wxImage
imgSrc(bitmap
.ConvertToImage());
112 imgSrc
.SetMask(FALSE
);
113 wxImage
image(imgSrc
.ConvertToMono(clr
.Red(), clr
.Green(), clr
.Blue()));
114 if ( !image
.Ok() ) return FALSE
;
116 m_bitmap
= new wxBitmap(image
, 1);
118 return m_bitmap
->Ok();
121 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
124 wxPalette
*pal
= bitmap
.GetPalette();
126 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
128 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
130 return Create(bitmap
, wxColour(r
, g
, b
));
133 bool wxMask::Create(const wxBitmap
& bitmap
)
138 wxCHECK_MSG( bitmap
.Ok(), FALSE
, wxT("Invalid bitmap") );
139 wxCHECK_MSG( bitmap
.GetDepth() == 1, FALSE
, wxT("Cannot create mask from colour bitmap") );
141 m_bitmap
= new wxBitmap(bitmap
);
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 class wxBitmapRefData
: public wxObjectRefData
159 wxPalette
*m_palette
;
164 wxBitmapRefData::wxBitmapRefData()
174 wxBitmapRefData::~wxBitmapRefData()
177 MGL_unloadBitmap(m_bitmap
);
182 //-----------------------------------------------------------------------------
184 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
187 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
,wxObject
)
188 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxBitmapBase
)
190 wxBitmap::wxBitmap(int width
, int height
, int depth
)
192 Create(width
, height
, depth
);
196 static bitmap_t
*MyMGL_createBitmap(int width
, int height
,
197 int bpp
, pixel_format_t
*pf
)
199 MGLMemoryDC
mdc(width
, height
, bpp
, pf
);
200 return MGL_getBitmapFromDC(mdc
.getDC(), 0, 0, width
, height
, TRUE
);
203 bool wxBitmap::Create(int width
, int height
, int depth
)
207 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
209 pixel_format_t pf_dummy
, *pf
;
210 int mglDepth
= depth
;
215 wxASSERT_MSG( g_displayDC
, wxT("MGL display DC not created yet.") );
217 g_displayDC
->getPixelFormat(pf_dummy
);
218 mglDepth
= g_displayDC
->getBitsPerPixel();
226 pf
= &gs_pixel_format_15
;
229 pf
= &gs_pixel_format_16
;
232 pf
= &gs_pixel_format_24
;
235 pf
= &gs_pixel_format_32
;
238 wxASSERT_MSG( 0, wxT("invalid bitmap depth") );
243 m_refData
= new wxBitmapRefData();
244 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
245 M_BMPDATA
->m_palette
= (wxPalette
*) NULL
;
246 M_BMPDATA
->m_width
= width
;
247 M_BMPDATA
->m_height
= height
;
248 M_BMPDATA
->m_bpp
= mglDepth
;
252 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, mglDepth
, pf
);
256 // MGL does not support mono DCs, so we have to emulate them with
257 // 8bpp ones. We do that by using a special palette with color 0
258 // set to black and all other colors set to white.
260 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, 8, pf
);
261 SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0));
267 bool wxBitmap::CreateFromXpm(const char **bits
)
269 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
271 wxXPMDecoder decoder
;
272 wxImage img
= decoder
.ReadData(bits
);
273 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
275 *this = wxBitmap(img
);
280 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
284 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
286 width
= image
.GetWidth();
287 height
= image
.GetHeight();
289 if ( !Create(width
, height
, depth
) ) return;
291 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
292 width
* 3, (void*)image
.GetData(), NULL
);
293 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
295 MGLDevCtx
*bdc
= CreateTmpDC();
297 if ( GetDepth() <= 8 && image
.HasPalette() )
298 SetPalette(image
.GetPalette());
300 bdc
->bitBlt(idc
, 0, 0, width
, height
, 0, 0, MGL_REPLACE_MODE
);
303 if ( image
.HasMask() )
305 wxImage mask_image
= image
.ConvertToMono(image
.GetMaskRed(),
306 image
.GetMaskGreen(),
307 image
.GetMaskBlue());
308 mask_image
.SetMask(FALSE
);
309 wxBitmap
mask_bmp(mask_image
, 1);
310 SetMask(new wxMask(mask_bmp
));
314 wxImage
wxBitmap::ConvertToImage() const
316 wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") );
320 height
= GetHeight();
322 wxImage
image(width
, height
);
323 wxASSERT_MSG( image
.Ok(), wxT("cannot create image") );
325 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
326 width
* 3, (void*)image
.GetData(), NULL
);
327 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
329 if ( M_BMPDATA
->m_palette
)
330 image
.SetPalette(*(M_BMPDATA
->m_palette
));
334 // in consistency with other ports, we convert parts covered
335 // by the mask to <16,16,16> colour and set that colour to image's
336 // mask. We do that by OR-blitting the mask over image with
337 // bg colour set to black and fg colour to <16,16,16>
339 image
.SetMaskColour(16, 16, 16);
343 tmpDC
.SetMGLDC(&idc
, FALSE
);
344 tmpDC
.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID
));
346 tmpDC
.DrawBitmap(*this, 0, 0, TRUE
);
350 image
.SetMask(FALSE
);
351 idc
.putBitmap(0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
357 wxBitmap::wxBitmap(const wxBitmap
& bmp
)
362 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
364 LoadFile(filename
, type
);
367 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
369 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
371 if ( !Create(width
, height
, 1) ) return;
372 MGLDevCtx
*bdc
= CreateTmpDC();
373 wxCurrentDCSwitcher
curDC(bdc
);
375 bdc
->setBackColor(0);
377 bdc
->putMonoImage(0, 0, width
, (width
+ 7) / 8, height
, (void*)bits
);
381 wxBitmap
& wxBitmap::operator = (const wxBitmap
& bmp
)
383 if ( *this == bmp
) return (*this);
388 bool wxBitmap::operator == (const wxBitmap
& bmp
) const
390 return (m_refData
== bmp
.m_refData
);
393 bool wxBitmap::operator != (const wxBitmap
& bmp
) const
395 return (m_refData
!= bmp
.m_refData
);
398 bool wxBitmap::Ok() const
400 return (m_refData
!= NULL
&& M_BMPDATA
->m_bitmap
!= NULL
);
403 int wxBitmap::GetHeight() const
405 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
407 return M_BMPDATA
->m_height
;
410 int wxBitmap::GetWidth() const
412 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
414 return M_BMPDATA
->m_width
;
417 int wxBitmap::GetDepth() const
419 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
421 return M_BMPDATA
->m_bpp
;
424 wxMask
*wxBitmap::GetMask() const
426 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
428 return M_BMPDATA
->m_mask
;
431 void wxBitmap::SetMask(wxMask
*mask
)
433 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
435 delete M_BMPDATA
->m_mask
;
436 M_BMPDATA
->m_mask
= mask
;
439 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
441 wxBitmap
*bmp
= (wxBitmap
*)(&icon
);
446 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
449 (rect
.x
>= 0) && (rect
.y
>= 0) &&
450 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
451 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
453 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
454 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
457 ret
.SetPalette(*GetPalette());
459 MGLDevCtx
*tdc
= ret
.CreateTmpDC();
460 tdc
->putBitmapSection(rect
.x
, rect
.y
,
461 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
462 0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
467 wxBitmap submask
= GetMask()->GetBitmap()->GetSubBitmap(rect
);
468 ret
.SetMask(new wxMask(submask
));
474 void wxBitmap::SetMonoPalette(const wxColour
& fg
, const wxColour
& bg
)
476 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
478 palette_t
*mono
= M_BMPDATA
->m_bitmap
->pal
;
480 wxCHECK_RET( M_BMPDATA
->m_bpp
== 1, wxT("bitmap is not 1bpp") );
481 wxCHECK_RET( mono
!= NULL
, wxT("bitmap w/o palette") );
483 mono
[0].red
= bg
.Red();
484 mono
[0].green
= bg
.Green();
485 mono
[0].blue
= bg
.Blue();
487 for (size_t i
= 1; i
< 256; i
++)
489 mono
[i
].red
= fg
.Red();
490 mono
[i
].green
= fg
.Green();
491 mono
[i
].blue
= fg
.Blue();
496 MGLDevCtx
*wxBitmap::CreateTmpDC() const
498 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
500 MGLDevCtx
*tdc
= new MGLMemoryDC(GetWidth(), GetHeight(),
501 M_BMPDATA
->m_bitmap
->bitsPerPixel
,
502 M_BMPDATA
->m_bitmap
->pf
,
503 M_BMPDATA
->m_bitmap
->bytesPerLine
,
504 M_BMPDATA
->m_bitmap
->surface
,
506 wxCHECK_MSG( tdc
->isValid(), NULL
, wxT("cannot create temporary MGLDC") );
508 if ( M_BMPDATA
->m_bitmap
->pal
!= NULL
)
512 switch (M_BMPDATA
->m_bitmap
->bitsPerPixel
)
514 case 2: cnt
= 2; break;
515 case 4: cnt
= 16; break;
516 case 8: cnt
= 256; break;
519 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
523 tdc
->setPalette(M_BMPDATA
->m_bitmap
->pal
, cnt
, 0);
524 tdc
->realizePalette(cnt
, 0, FALSE
);
530 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
534 if ( type
== wxBITMAP_TYPE_BMP
|| type
== wxBITMAP_TYPE_PNG
||
535 type
== wxBITMAP_TYPE_PCX
|| type
== wxBITMAP_TYPE_JPEG
)
537 // prevent accidental loading of bitmap from $MGL_ROOT:
538 if ( !wxFileExists(name
) )
540 wxLogError(_("File %s does not exist."), name
.c_str());
545 wxBitmapHandler
*handler
= FindHandler(type
);
547 if ( handler
== NULL
)
550 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
552 wxLogError("no bitmap handler for type %d defined.", type
);
557 *this = wxBitmap(image
);
562 m_refData
= new wxBitmapRefData();
564 return handler
->LoadFile(this, name
, type
, -1, -1);
567 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
569 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
571 wxBitmapHandler
*handler
= FindHandler(type
);
573 if ( handler
== NULL
)
575 wxImage image
= ConvertToImage();
577 image
.SetPalette(*palette
);
580 return image
.SaveFile(filename
, type
);
583 wxLogError("no bitmap handler for type %d defined.", type
);
588 return handler
->SaveFile(this, filename
, type
, palette
);
591 wxPalette
*wxBitmap::GetPalette() const
593 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
595 return M_BMPDATA
->m_palette
;
598 void wxBitmap::SetPalette(const wxPalette
& palette
)
600 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
601 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
603 delete M_BMPDATA
->m_palette
;
604 M_BMPDATA
->m_palette
= NULL
;
606 if ( !palette
.Ok() ) return;
608 M_BMPDATA
->m_palette
= new wxPalette(palette
);
610 int cnt
= palette
.GetColoursCount();
611 palette_t
*pal
= palette
.GetMGLpalette_t();
612 memcpy(M_BMPDATA
->m_bitmap
->pal
, pal
, cnt
* sizeof(palette_t
));
615 void wxBitmap::SetHeight(int height
)
617 if (!m_refData
) m_refData
= new wxBitmapRefData();
619 M_BMPDATA
->m_height
= height
;
622 void wxBitmap::SetWidth(int width
)
624 if (!m_refData
) m_refData
= new wxBitmapRefData();
626 M_BMPDATA
->m_width
= width
;
629 void wxBitmap::SetDepth(int depth
)
631 if (!m_refData
) m_refData
= new wxBitmapRefData();
633 M_BMPDATA
->m_bpp
= depth
;
636 bitmap_t
*wxBitmap::GetMGLbitmap_t() const
638 return M_BMPDATA
->m_bitmap
;
643 //-----------------------------------------------------------------------------
644 // wxBitmap I/O handlers
645 //-----------------------------------------------------------------------------
647 class wxMGLBitmapHandler
: public wxBitmapHandler
650 wxMGLBitmapHandler(wxBitmapType type
,
651 const wxString
& extension
, const wxString
& name
);
653 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
654 int width
, int height
, int depth
= 1)
657 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
658 int desiredWidth
, int desiredHeight
);
659 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
660 int type
, const wxPalette
*palette
= NULL
);
663 wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type
,
664 const wxString
& extension
,
665 const wxString
& name
)
670 SetExtension(extension
);
673 bool wxMGLBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
675 int WXUNUSED(desiredWidth
),
676 int WXUNUSED(desiredHeight
))
678 int width
, height
, bpp
;
685 case wxBITMAP_TYPE_BMP_RESOURCE
:
686 case wxBITMAP_TYPE_JPEG_RESOURCE
:
687 case wxBITMAP_TYPE_PNG_RESOURCE
:
688 case wxBITMAP_TYPE_PCX_RESOURCE
:
689 fullname
= name
+ wxT(".bmp");
698 case wxBITMAP_TYPE_BMP
:
699 case wxBITMAP_TYPE_BMP_RESOURCE
:
700 if ( !MGL_getBitmapSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
702 bitmap
->Create(width
, height
, -1);
703 if ( !bitmap
->Ok() ) return FALSE
;
704 dc
.SelectObject(*bitmap
);
705 if ( !dc
.GetMGLDC()->loadBitmapIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
709 case wxBITMAP_TYPE_JPEG
:
710 case wxBITMAP_TYPE_JPEG_RESOURCE
:
711 if ( !MGL_getJPEGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
713 bitmap
->Create(width
, height
, -1);
714 if ( !bitmap
->Ok() ) return FALSE
;
715 dc
.SelectObject(*bitmap
);
716 if ( !dc
.GetMGLDC()->loadJPEGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
720 case wxBITMAP_TYPE_PNG
:
721 case wxBITMAP_TYPE_PNG_RESOURCE
:
722 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
724 bitmap
->Create(width
, height
, -1);
725 if ( !bitmap
->Ok() ) return FALSE
;
726 dc
.SelectObject(*bitmap
);
727 if ( !dc
.GetMGLDC()->loadPNGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
731 case wxBITMAP_TYPE_PCX
:
732 case wxBITMAP_TYPE_PCX_RESOURCE
:
733 if ( !MGL_getPCXSize(fullname
.mb_str(), &width
, &height
, &bpp
) )
735 bitmap
->Create(width
, height
, -1);
736 if ( !bitmap
->Ok() ) return FALSE
;
737 dc
.SelectObject(*bitmap
);
738 if ( !dc
.GetMGLDC()->loadPCXIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
743 wxFAIL_MSG(wxT("Unsupported image format."));
750 bool wxMGLBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
751 int type
, const wxPalette
* WXUNUSED(palette
))
755 int w
= bitmap
->GetWidth(),
756 h
= bitmap
->GetHeight();
758 mem
.SelectObject(*bitmap
);
759 tdc
= mem
.GetMGLDC();
763 case wxBITMAP_TYPE_BMP
:
764 return tdc
->saveBitmapFromDC(name
.mb_str(), 0, 0, w
, h
);
766 case wxBITMAP_TYPE_JPEG
:
767 return tdc
->saveJPEGFromDC(name
.mb_str(), 0, 0, w
, h
, 75);
769 case wxBITMAP_TYPE_PNG
:
770 return tdc
->savePNGFromDC(name
.mb_str(), 0, 0, w
, h
);
772 case wxBITMAP_TYPE_PCX
:
773 return tdc
->savePCXFromDC(name
.mb_str(), 0, 0, w
, h
);
783 // let's handle PNGs in special way because they have alpha channel
784 // which we can access via bitmap_t most easily
785 class wxPNGBitmapHandler
: public wxMGLBitmapHandler
788 wxPNGBitmapHandler(wxBitmapType type
,
789 const wxString
& extension
, const wxString
& name
)
790 : wxMGLBitmapHandler(type
, extension
, name
) {}
792 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
793 int desiredWidth
, int desiredHeight
);
796 bool wxPNGBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
798 int desiredWidth
, int desiredHeight
)
800 int width
, height
, bpp
;
804 if ( flags
== wxBITMAP_TYPE_PNG_RESOURCE
)
805 fullname
= name
+ wxT(".png");
809 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
814 // We can load ordinary PNGs faster with 'normal' MGL handler.
815 // Only RGBA PNGs need to be processed in special way because
816 // we have to convert alpha channel to mask
817 return wxMGLBitmapHandler::LoadFile(bitmap
, name
, flags
,
818 desiredWidth
, desiredHeight
);
821 bitmap_t
*bmp
= MGL_loadPNG(fullname
.mb_str(), TRUE
);
823 if ( bmp
== NULL
) return FALSE
;
825 bitmap
->Create(bmp
->width
, bmp
->height
, -1);
826 if ( !bitmap
->Ok() ) return FALSE
;
828 // convert bmp to display's depth and write it to *bitmap:
830 dc
.SelectObject(*bitmap
);
831 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
832 dc
.SelectObject(wxNullBitmap
);
834 // create mask, if bmp contains alpha channel (ARGB format):
835 if ( bmp
->bitsPerPixel
== 32 )
838 wxUint32
*s
= (wxUint32
*)bmp
->surface
;
839 for (y
= 0; y
< bmp
->height
; y
++)
841 s
= ((wxUint32
*)bmp
->surface
) + y
* bmp
->bytesPerLine
/4;
842 for (x
= 0; x
< bmp
->width
; x
++, s
++)
844 if ( ((((*s
) >> bmp
->pf
->alphaPos
) & bmp
->pf
->alphaMask
)
845 << bmp
->pf
->alphaAdjust
) < 128 )
848 *s
= 0x00FFFFFF; // white
851 wxBitmap
mask(bmp
->width
, bmp
->height
, 1);
852 dc
.SelectObject(mask
);
853 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
854 dc
.SelectObject(wxNullBitmap
);
855 bitmap
->SetMask(new wxMask(mask
));
858 MGL_unloadBitmap(bmp
);
866 class wxICOBitmapHandler
: public wxBitmapHandler
869 wxICOBitmapHandler(wxBitmapType type
,
870 const wxString
& extension
, const wxString
& name
);
872 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
,
873 int width
, int height
, int depth
= 1)
876 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
877 int desiredWidth
, int desiredHeight
);
878 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
879 int type
, const wxPalette
*palette
= NULL
);
882 wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type
,
883 const wxString
& extension
,
884 const wxString
& name
)
889 SetExtension(extension
);
892 bool wxICOBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
894 int WXUNUSED(desiredWidth
),
895 int WXUNUSED(desiredHeight
))
900 if ( flags
== wxBITMAP_TYPE_ICO_RESOURCE
)
901 icon
= MGL_loadIcon(wxString(name
+ wxT(".ico")).mb_str(), TRUE
);
903 icon
= MGL_loadIcon(name
.mb_str(), TRUE
);
905 if ( icon
== NULL
) return FALSE
;
907 bitmap
->Create(icon
->xorMask
.width
, icon
->xorMask
.height
);
910 mem
.SelectObject(*bitmap
);
912 dc
->putBitmap(0, 0, &(icon
->xorMask
), MGL_REPLACE_MODE
);
913 mem
.SelectObject(wxNullBitmap
);
915 wxBitmap
mask(icon
->xorMask
.width
, icon
->xorMask
.height
, 1);
916 mem
.SelectObject(mask
);
919 wxCurrentDCSwitcher
curDC(dc
);
923 dc
->putMonoImage(0, 0, icon
->xorMask
.width
, icon
->byteWidth
,
924 icon
->xorMask
.height
, (void*)icon
->andMask
);
926 bitmap
->SetMask(new wxMask(mask
));
928 MGL_unloadIcon(icon
);
933 bool wxICOBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
934 int type
, const wxPalette
* WXUNUSED(palette
))
942 /*static*/ void wxBitmap::InitStandardHandlers()
944 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP
, wxT("bmp"), wxT("Windows bitmap")));
945 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE
, wxEmptyString
, wxT("Windows bitmap resource")));
946 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG
, wxT("jpg"), wxT("JPEG image")));
947 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE
, wxEmptyString
, wxT("JPEG resource")));
948 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX
, wxT("pcx"), wxT("PCX image")));
949 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE
, wxEmptyString
, wxT("PCX resource")));
951 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG
, wxT("png"), wxT("PNG image")));
952 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE
, wxEmptyString
, wxT("PNG resource")));
954 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO
, wxT("ico"), wxT("Icon resource")));
955 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE
, wxEmptyString
, wxT("Icon resource")));