1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/bitmap.cpp
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"
22 #include "wx/dcmemory.h"
27 #include "wx/filefn.h"
28 #include "wx/xpmdecod.h"
30 #include "wx/mgl/private.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static pixel_format_t gs_pixel_format_15
=
39 {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp
41 static pixel_format_t gs_pixel_format_16
=
42 {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp
44 static pixel_format_t gs_pixel_format_24
=
45 {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp
47 static pixel_format_t gs_pixel_format_32
=
48 {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp
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
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 // Convert wxColour into it's quantized value in lower-precision
58 // pixel format (needed for masking by colour).
59 static wxColour
wxQuantizeColour(const wxColour
& clr
, const wxBitmap
& bmp
)
61 pixel_format_t
*pf
= bmp
.GetMGLbitmap_t()->pf
;
63 if ( pf
->redAdjust
== 0 && pf
->greenAdjust
== 0 && pf
->blueAdjust
== 0 )
66 return wxColour((unsigned char)((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
),
67 (unsigned char)((clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
),
68 (unsigned char)((clr
.Blue() >> pf
->blueAdjust
) << pf
->blueAdjust
));
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
84 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
87 Create(bitmap
, colour
);
90 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
93 Create(bitmap
, paletteIndex
);
96 wxMask::wxMask(const wxBitmap
& bitmap
)
107 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
112 wxColour
clr(wxQuantizeColour(colour
, bitmap
));
114 wxImage
imgSrc(bitmap
.ConvertToImage());
115 imgSrc
.SetMask(false);
116 wxImage
image(imgSrc
.ConvertToMono(clr
.Red(), clr
.Green(), clr
.Blue()));
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
)
194 wxBitmap::wxBitmap(int width
, int height
, int depth
)
196 Create(width
, height
, depth
);
200 static bitmap_t
*MyMGL_createBitmap(int width
, int height
,
201 int bpp
, pixel_format_t
*pf
)
203 MGLMemoryDC
mdc(width
, height
, bpp
, pf
);
204 return MGL_getBitmapFromDC(mdc
.getDC(), 0, 0, width
, height
, TRUE
);
207 bool wxBitmap::Create(int width
, int height
, int depth
)
211 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
213 pixel_format_t pf_dummy
;
215 int mglDepth
= depth
;
220 wxASSERT_MSG( g_displayDC
, wxT("MGL display DC not created yet.") );
222 g_displayDC
->getPixelFormat(pf_dummy
);
223 mglDepth
= g_displayDC
->getBitsPerPixel();
231 pf
= &gs_pixel_format_15
;
234 pf
= &gs_pixel_format_16
;
237 pf
= &gs_pixel_format_24
;
240 pf
= &gs_pixel_format_32
;
243 wxFAIL_MSG(wxT("invalid bitmap depth"));
247 m_refData
= new wxBitmapRefData();
248 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
249 M_BMPDATA
->m_palette
= (wxPalette
*) NULL
;
250 M_BMPDATA
->m_width
= width
;
251 M_BMPDATA
->m_height
= height
;
252 M_BMPDATA
->m_bpp
= mglDepth
;
256 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, mglDepth
, pf
);
260 // MGL does not support mono DCs, so we have to emulate them with
261 // 8bpp ones. We do that by using a special palette with color 0
262 // set to black and all other colors set to white.
264 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, 8, pf
);
265 SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0));
271 bool wxBitmap::CreateFromXpm(const char **bits
)
273 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
275 wxXPMDecoder decoder
;
276 wxImage img
= decoder
.ReadData(bits
);
277 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
279 *this = wxBitmap(img
);
284 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
288 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
290 width
= image
.GetWidth();
291 height
= image
.GetHeight();
293 if ( !Create(width
, height
, depth
) ) return;
295 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
296 width
* 3, (void*)image
.GetData(), NULL
);
297 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
299 MGLDevCtx
*bdc
= CreateTmpDC();
301 if ( GetDepth() <= 8 && image
.HasPalette() )
302 SetPalette(image
.GetPalette());
304 bdc
->bitBlt(idc
, 0, 0, width
, height
, 0, 0, MGL_REPLACE_MODE
);
307 if ( image
.HasMask() )
309 wxImage mask_image
= image
.ConvertToMono(image
.GetMaskRed(),
310 image
.GetMaskGreen(),
311 image
.GetMaskBlue());
312 mask_image
.SetMask(false);
313 wxBitmap
mask_bmp(mask_image
, 1);
314 SetMask(new wxMask(mask_bmp
));
318 wxImage
wxBitmap::ConvertToImage() const
320 wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") );
324 height
= GetHeight();
326 wxImage
image(width
, height
);
327 wxASSERT_MSG( image
.Ok(), wxT("cannot create image") );
329 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
330 width
* 3, (void*)image
.GetData(), NULL
);
331 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
333 if ( M_BMPDATA
->m_palette
)
334 image
.SetPalette(*(M_BMPDATA
->m_palette
));
338 // in consistency with other ports, we convert parts covered
339 // by the mask to <16,16,16> colour and set that colour to image's
340 // mask. We do that by OR-blitting the mask over image with
341 // bg colour set to black and fg colour to <16,16,16>
343 image
.SetMaskColour(16, 16, 16);
347 tmpDC
.SetMGLDC(&idc
, false);
348 tmpDC
.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID
));
350 tmpDC
.DrawBitmap(*this, 0, 0, true);
354 image
.SetMask(false);
355 idc
.putBitmap(0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
361 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
363 LoadFile(filename
, type
);
366 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
368 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
370 if ( !Create(width
, height
, 1) ) return;
371 MGLDevCtx
*bdc
= CreateTmpDC();
372 wxCurrentDCSwitcher
curDC(bdc
);
374 bdc
->setBackColor(0);
376 bdc
->putMonoImage(0, 0, width
, (width
+ 7) / 8, height
, (void*)bits
);
380 bool wxBitmap::operator == (const wxBitmap
& bmp
) const
382 return (m_refData
== bmp
.m_refData
);
385 bool wxBitmap::operator != (const wxBitmap
& bmp
) const
387 return (m_refData
!= bmp
.m_refData
);
390 bool wxBitmap::Ok() const
392 return (m_refData
!= NULL
&& M_BMPDATA
->m_bitmap
!= NULL
);
395 int wxBitmap::GetHeight() const
397 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
399 return M_BMPDATA
->m_height
;
402 int wxBitmap::GetWidth() const
404 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
406 return M_BMPDATA
->m_width
;
409 int wxBitmap::GetDepth() const
411 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
413 return M_BMPDATA
->m_bpp
;
416 wxMask
*wxBitmap::GetMask() const
418 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
420 return M_BMPDATA
->m_mask
;
423 void wxBitmap::SetMask(wxMask
*mask
)
425 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
427 delete M_BMPDATA
->m_mask
;
428 M_BMPDATA
->m_mask
= mask
;
431 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
433 wxBitmap
*bmp
= (wxBitmap
*)(&icon
);
438 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
441 (rect
.x
>= 0) && (rect
.y
>= 0) &&
442 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
443 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
445 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
446 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
449 ret
.SetPalette(*GetPalette());
451 MGLDevCtx
*tdc
= ret
.CreateTmpDC();
452 tdc
->putBitmapSection(rect
.x
, rect
.y
,
453 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
454 0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
459 wxBitmap submask
= GetMask()->GetBitmap()->GetSubBitmap(rect
);
460 ret
.SetMask(new wxMask(submask
));
466 void wxBitmap::SetMonoPalette(const wxColour
& fg
, const wxColour
& bg
)
468 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
470 palette_t
*mono
= M_BMPDATA
->m_bitmap
->pal
;
472 wxCHECK_RET( M_BMPDATA
->m_bpp
== 1, wxT("bitmap is not 1bpp") );
473 wxCHECK_RET( mono
!= NULL
, wxT("bitmap w/o palette") );
475 mono
[0].red
= bg
.Red();
476 mono
[0].green
= bg
.Green();
477 mono
[0].blue
= bg
.Blue();
479 for (size_t i
= 1; i
< 256; i
++)
481 mono
[i
].red
= fg
.Red();
482 mono
[i
].green
= fg
.Green();
483 mono
[i
].blue
= fg
.Blue();
488 MGLDevCtx
*wxBitmap::CreateTmpDC() const
490 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
492 MGLDevCtx
*tdc
= new MGLMemoryDC(GetWidth(), GetHeight(),
493 M_BMPDATA
->m_bitmap
->bitsPerPixel
,
494 M_BMPDATA
->m_bitmap
->pf
,
495 M_BMPDATA
->m_bitmap
->bytesPerLine
,
496 M_BMPDATA
->m_bitmap
->surface
,
498 wxCHECK_MSG( tdc
->isValid(), NULL
, wxT("cannot create temporary MGLDC") );
500 if ( M_BMPDATA
->m_bitmap
->pal
!= NULL
)
504 switch (M_BMPDATA
->m_bitmap
->bitsPerPixel
)
506 case 2: cnt
= 2; break;
507 case 4: cnt
= 16; break;
508 case 8: cnt
= 256; break;
511 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
515 tdc
->setPalette(M_BMPDATA
->m_bitmap
->pal
, cnt
, 0);
516 tdc
->realizePalette(cnt
, 0, FALSE
);
522 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
526 if ( type
== wxBITMAP_TYPE_BMP
|| type
== wxBITMAP_TYPE_PNG
||
527 type
== wxBITMAP_TYPE_PCX
|| type
== wxBITMAP_TYPE_JPEG
)
529 // prevent accidental loading of bitmap from $MGL_ROOT:
530 if ( !wxFileExists(name
) )
532 wxLogError(_("File %s does not exist."), name
.c_str());
537 wxBitmapHandler
*handler
= FindHandler(type
);
539 if ( handler
== NULL
)
542 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
544 wxLogError("no bitmap handler for type %d defined.", type
);
549 *this = wxBitmap(image
);
554 m_refData
= new wxBitmapRefData();
556 return handler
->LoadFile(this, name
, type
, -1, -1);
559 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
561 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
563 wxBitmapHandler
*handler
= FindHandler(type
);
565 if ( handler
== NULL
)
567 wxImage image
= ConvertToImage();
569 image
.SetPalette(*palette
);
572 return image
.SaveFile(filename
, type
);
575 wxLogError("no bitmap handler for type %d defined.", type
);
580 return handler
->SaveFile(this, filename
, type
, palette
);
583 wxPalette
*wxBitmap::GetPalette() const
585 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
587 return M_BMPDATA
->m_palette
;
590 void wxBitmap::SetPalette(const wxPalette
& palette
)
592 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
593 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
595 delete M_BMPDATA
->m_palette
;
596 M_BMPDATA
->m_palette
= NULL
;
598 if ( !palette
.Ok() ) return;
600 M_BMPDATA
->m_palette
= new wxPalette(palette
);
602 int cnt
= palette
.GetColoursCount();
603 palette_t
*pal
= palette
.GetMGLpalette_t();
604 memcpy(M_BMPDATA
->m_bitmap
->pal
, pal
, cnt
* sizeof(palette_t
));
607 void wxBitmap::SetHeight(int height
)
609 if (!m_refData
) m_refData
= new wxBitmapRefData();
611 M_BMPDATA
->m_height
= height
;
614 void wxBitmap::SetWidth(int width
)
616 if (!m_refData
) m_refData
= new wxBitmapRefData();
618 M_BMPDATA
->m_width
= width
;
621 void wxBitmap::SetDepth(int depth
)
623 if (!m_refData
) m_refData
= new wxBitmapRefData();
625 M_BMPDATA
->m_bpp
= depth
;
628 bitmap_t
*wxBitmap::GetMGLbitmap_t() const
630 return M_BMPDATA
->m_bitmap
;
635 //-----------------------------------------------------------------------------
636 // wxBitmap I/O handlers
637 //-----------------------------------------------------------------------------
639 class wxMGLBitmapHandler
: public wxBitmapHandler
642 wxMGLBitmapHandler(wxBitmapType type
,
643 const wxString
& extension
, const wxString
& name
);
645 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
646 void *WXUNUSED(data
),
647 long WXUNUSED(flags
),
649 int WXUNUSED(height
),
650 int WXUNUSED(depth
) = 1)
653 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
654 int desiredWidth
, int desiredHeight
);
655 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
656 int type
, const wxPalette
*palette
= NULL
);
659 wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type
,
660 const wxString
& extension
,
661 const wxString
& name
)
666 SetExtension(extension
);
669 bool wxMGLBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
671 int WXUNUSED(desiredWidth
),
672 int WXUNUSED(desiredHeight
))
674 int width
, height
, bpp
;
681 case wxBITMAP_TYPE_BMP_RESOURCE
:
682 case wxBITMAP_TYPE_JPEG_RESOURCE
:
683 case wxBITMAP_TYPE_PNG_RESOURCE
:
684 case wxBITMAP_TYPE_PCX_RESOURCE
:
685 fullname
= name
+ wxT(".bmp");
694 case wxBITMAP_TYPE_BMP
:
695 case wxBITMAP_TYPE_BMP_RESOURCE
:
696 if ( !MGL_getBitmapSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
698 bitmap
->Create(width
, height
, -1);
699 if ( !bitmap
->Ok() ) return false;
700 dc
.SelectObject(*bitmap
);
701 if ( !dc
.GetMGLDC()->loadBitmapIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
705 case wxBITMAP_TYPE_JPEG
:
706 case wxBITMAP_TYPE_JPEG_RESOURCE
:
707 if ( !MGL_getJPEGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
709 bitmap
->Create(width
, height
, -1);
710 if ( !bitmap
->Ok() ) return false;
711 dc
.SelectObject(*bitmap
);
712 if ( !dc
.GetMGLDC()->loadJPEGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
716 case wxBITMAP_TYPE_PNG
:
717 case wxBITMAP_TYPE_PNG_RESOURCE
:
718 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
720 bitmap
->Create(width
, height
, -1);
721 if ( !bitmap
->Ok() ) return false;
722 dc
.SelectObject(*bitmap
);
723 if ( !dc
.GetMGLDC()->loadPNGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
727 case wxBITMAP_TYPE_PCX
:
728 case wxBITMAP_TYPE_PCX_RESOURCE
:
729 if ( !MGL_getPCXSize(fullname
.mb_str(), &width
, &height
, &bpp
) )
731 bitmap
->Create(width
, height
, -1);
732 if ( !bitmap
->Ok() ) return false;
733 dc
.SelectObject(*bitmap
);
734 if ( !dc
.GetMGLDC()->loadPCXIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
739 wxFAIL_MSG(wxT("Unsupported image format."));
746 bool wxMGLBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
747 int type
, const wxPalette
* WXUNUSED(palette
))
751 int w
= bitmap
->GetWidth(),
752 h
= bitmap
->GetHeight();
754 mem
.SelectObject(*bitmap
);
755 tdc
= mem
.GetMGLDC();
759 case wxBITMAP_TYPE_BMP
:
760 return (bool)tdc
->saveBitmapFromDC(name
.mb_str(), 0, 0, w
, h
);
761 case wxBITMAP_TYPE_JPEG
:
762 return (bool)tdc
->saveJPEGFromDC(name
.mb_str(), 0, 0, w
, h
, 75);
763 case wxBITMAP_TYPE_PNG
:
764 return (bool)tdc
->savePNGFromDC(name
.mb_str(), 0, 0, w
, h
);
765 case wxBITMAP_TYPE_PCX
:
766 return (bool)tdc
->savePCXFromDC(name
.mb_str(), 0, 0, w
, h
);
774 // let's handle PNGs in special way because they have alpha channel
775 // which we can access via bitmap_t most easily
776 class wxPNGBitmapHandler
: public wxMGLBitmapHandler
779 wxPNGBitmapHandler(wxBitmapType type
,
780 const wxString
& extension
, const wxString
& name
)
781 : wxMGLBitmapHandler(type
, extension
, name
) {}
783 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
784 int desiredWidth
, int desiredHeight
);
787 bool wxPNGBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
789 int desiredWidth
, int desiredHeight
)
791 int width
, height
, bpp
;
795 if ( flags
== wxBITMAP_TYPE_PNG_RESOURCE
)
796 fullname
= name
+ wxT(".png");
800 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
805 // We can load ordinary PNGs faster with 'normal' MGL handler.
806 // Only RGBA PNGs need to be processed in special way because
807 // we have to convert alpha channel to mask
808 return wxMGLBitmapHandler::LoadFile(bitmap
, name
, flags
,
809 desiredWidth
, desiredHeight
);
812 bitmap_t
*bmp
= MGL_loadPNG(fullname
.mb_str(), TRUE
);
814 if ( bmp
== NULL
) return false;
816 bitmap
->Create(bmp
->width
, bmp
->height
, -1);
817 if ( !bitmap
->Ok() ) return false;
819 // convert bmp to display's depth and write it to *bitmap:
821 dc
.SelectObject(*bitmap
);
822 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
823 dc
.SelectObject(wxNullBitmap
);
825 // create mask, if bmp contains alpha channel (ARGB format):
826 if ( bmp
->bitsPerPixel
== 32 )
829 wxUint32
*s
= (wxUint32
*)bmp
->surface
;
830 for (y
= 0; y
< bmp
->height
; y
++)
832 s
= ((wxUint32
*)bmp
->surface
) + y
* bmp
->bytesPerLine
/4;
833 for (x
= 0; x
< bmp
->width
; x
++, s
++)
835 if ( ((((*s
) >> bmp
->pf
->alphaPos
) & bmp
->pf
->alphaMask
)
836 << bmp
->pf
->alphaAdjust
) < 128 )
839 *s
= 0x00FFFFFF; // white
842 wxBitmap
mask(bmp
->width
, bmp
->height
, 1);
843 dc
.SelectObject(mask
);
844 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
845 dc
.SelectObject(wxNullBitmap
);
846 bitmap
->SetMask(new wxMask(mask
));
849 MGL_unloadBitmap(bmp
);
857 class wxICOBitmapHandler
: public wxBitmapHandler
860 wxICOBitmapHandler(wxBitmapType type
,
861 const wxString
& extension
, const wxString
& name
);
863 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
864 void *WXUNUSED(data
),
865 long WXUNUSED(flags
),
867 int WXUNUSED(height
),
868 int WXUNUSED(depth
) = 1)
871 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
872 int desiredWidth
, int desiredHeight
);
873 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
874 int type
, const wxPalette
*palette
= NULL
);
877 wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type
,
878 const wxString
& extension
,
879 const wxString
& name
)
884 SetExtension(extension
);
887 bool wxICOBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
889 int WXUNUSED(desiredWidth
),
890 int WXUNUSED(desiredHeight
))
895 if ( flags
== wxBITMAP_TYPE_ICO_RESOURCE
)
896 icon
= MGL_loadIcon(wxString(name
+ wxT(".ico")).mb_str(), TRUE
);
898 icon
= MGL_loadIcon(name
.mb_str(), TRUE
);
900 if ( icon
== NULL
) return false;
902 bitmap
->Create(icon
->xorMask
.width
, icon
->xorMask
.height
);
905 mem
.SelectObject(*bitmap
);
907 dc
->putBitmap(0, 0, &(icon
->xorMask
), MGL_REPLACE_MODE
);
908 mem
.SelectObject(wxNullBitmap
);
910 wxBitmap
mask(icon
->xorMask
.width
, icon
->xorMask
.height
, 1);
911 mem
.SelectObject(mask
);
914 wxCurrentDCSwitcher
curDC(dc
);
918 dc
->putMonoImage(0, 0, icon
->xorMask
.width
, icon
->byteWidth
,
919 icon
->xorMask
.height
, (void*)icon
->andMask
);
921 bitmap
->SetMask(new wxMask(mask
));
923 MGL_unloadIcon(icon
);
928 bool wxICOBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
929 const wxString
& WXUNUSED(name
),
931 const wxPalette
* WXUNUSED(palette
))
939 /*static*/ void wxBitmap::InitStandardHandlers()
941 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP
, wxT("bmp"), wxT("Windows bitmap")));
942 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE
, wxEmptyString
, wxT("Windows bitmap resource")));
943 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG
, wxT("jpg"), wxT("JPEG image")));
944 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE
, wxEmptyString
, wxT("JPEG resource")));
945 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX
, wxT("pcx"), wxT("PCX image")));
946 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE
, wxEmptyString
, wxT("PCX resource")));
948 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG
, wxT("png"), wxT("PNG image")));
949 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE
, wxEmptyString
, wxT("PNG resource")));
951 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO
, wxT("ico"), wxT("Icon resource")));
952 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE
, wxEmptyString
, wxT("Icon resource")));