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"
25 #include "wx/filefn.h"
27 #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((unsigned char)((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
),
68 (unsigned char)((clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
),
69 (unsigned char)((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()));
121 m_bitmap
= new wxBitmap(image
, 1);
123 return m_bitmap
->Ok();
126 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
129 wxPalette
*pal
= bitmap
.GetPalette();
131 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
133 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
135 return Create(bitmap
, wxColour(r
, g
, b
));
138 bool wxMask::Create(const wxBitmap
& bitmap
)
143 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap") );
144 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
146 m_bitmap
= new wxBitmap(bitmap
);
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 class wxBitmapRefData
: public wxObjectRefData
164 wxPalette
*m_palette
;
169 wxBitmapRefData::wxBitmapRefData()
179 wxBitmapRefData::~wxBitmapRefData()
182 MGL_unloadBitmap(m_bitmap
);
187 //-----------------------------------------------------------------------------
189 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
192 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
,wxObject
)
193 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxBitmapBase
)
195 wxBitmap::wxBitmap(int width
, int height
, int depth
)
197 Create(width
, height
, depth
);
201 static bitmap_t
*MyMGL_createBitmap(int width
, int height
,
202 int bpp
, pixel_format_t
*pf
)
204 MGLMemoryDC
mdc(width
, height
, bpp
, pf
);
205 return MGL_getBitmapFromDC(mdc
.getDC(), 0, 0, width
, height
, TRUE
);
208 bool wxBitmap::Create(int width
, int height
, int depth
)
212 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
214 pixel_format_t pf_dummy
;
216 int mglDepth
= depth
;
221 wxASSERT_MSG( g_displayDC
, wxT("MGL display DC not created yet.") );
223 g_displayDC
->getPixelFormat(pf_dummy
);
224 mglDepth
= g_displayDC
->getBitsPerPixel();
232 pf
= &gs_pixel_format_15
;
235 pf
= &gs_pixel_format_16
;
238 pf
= &gs_pixel_format_24
;
241 pf
= &gs_pixel_format_32
;
244 wxFAIL_MSG(wxT("invalid bitmap depth"));
248 m_refData
= new wxBitmapRefData();
249 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
250 M_BMPDATA
->m_palette
= (wxPalette
*) NULL
;
251 M_BMPDATA
->m_width
= width
;
252 M_BMPDATA
->m_height
= height
;
253 M_BMPDATA
->m_bpp
= mglDepth
;
257 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, mglDepth
, pf
);
261 // MGL does not support mono DCs, so we have to emulate them with
262 // 8bpp ones. We do that by using a special palette with color 0
263 // set to black and all other colors set to white.
265 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, 8, pf
);
266 SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0));
272 bool wxBitmap::CreateFromXpm(const char **bits
)
274 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
276 wxXPMDecoder decoder
;
277 wxImage img
= decoder
.ReadData(bits
);
278 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
280 *this = wxBitmap(img
);
285 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
289 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
291 width
= image
.GetWidth();
292 height
= image
.GetHeight();
294 if ( !Create(width
, height
, depth
) ) return;
296 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
297 width
* 3, (void*)image
.GetData(), NULL
);
298 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
300 MGLDevCtx
*bdc
= CreateTmpDC();
302 if ( GetDepth() <= 8 && image
.HasPalette() )
303 SetPalette(image
.GetPalette());
305 bdc
->bitBlt(idc
, 0, 0, width
, height
, 0, 0, MGL_REPLACE_MODE
);
308 if ( image
.HasMask() )
310 wxImage mask_image
= image
.ConvertToMono(image
.GetMaskRed(),
311 image
.GetMaskGreen(),
312 image
.GetMaskBlue());
313 mask_image
.SetMask(false);
314 wxBitmap
mask_bmp(mask_image
, 1);
315 SetMask(new wxMask(mask_bmp
));
319 wxImage
wxBitmap::ConvertToImage() const
321 wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") );
325 height
= GetHeight();
327 wxImage
image(width
, height
);
328 wxASSERT_MSG( image
.Ok(), wxT("cannot create image") );
330 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
331 width
* 3, (void*)image
.GetData(), NULL
);
332 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
334 if ( M_BMPDATA
->m_palette
)
335 image
.SetPalette(*(M_BMPDATA
->m_palette
));
339 // in consistency with other ports, we convert parts covered
340 // by the mask to <16,16,16> colour and set that colour to image's
341 // mask. We do that by OR-blitting the mask over image with
342 // bg colour set to black and fg colour to <16,16,16>
344 image
.SetMaskColour(16, 16, 16);
348 tmpDC
.SetMGLDC(&idc
, false);
349 tmpDC
.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID
));
351 tmpDC
.DrawBitmap(*this, 0, 0, true);
355 image
.SetMask(false);
356 idc
.putBitmap(0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
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 bool wxBitmap::operator == (const wxBitmap
& bmp
) const
383 return (m_refData
== bmp
.m_refData
);
386 bool wxBitmap::operator != (const wxBitmap
& bmp
) const
388 return (m_refData
!= bmp
.m_refData
);
391 bool wxBitmap::Ok() const
393 return (m_refData
!= NULL
&& M_BMPDATA
->m_bitmap
!= NULL
);
396 int wxBitmap::GetHeight() const
398 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
400 return M_BMPDATA
->m_height
;
403 int wxBitmap::GetWidth() const
405 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
407 return M_BMPDATA
->m_width
;
410 int wxBitmap::GetDepth() const
412 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
414 return M_BMPDATA
->m_bpp
;
417 wxMask
*wxBitmap::GetMask() const
419 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
421 return M_BMPDATA
->m_mask
;
424 void wxBitmap::SetMask(wxMask
*mask
)
426 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
428 delete M_BMPDATA
->m_mask
;
429 M_BMPDATA
->m_mask
= mask
;
432 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
434 wxBitmap
*bmp
= (wxBitmap
*)(&icon
);
439 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
442 (rect
.x
>= 0) && (rect
.y
>= 0) &&
443 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
444 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
446 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
447 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
450 ret
.SetPalette(*GetPalette());
452 MGLDevCtx
*tdc
= ret
.CreateTmpDC();
453 tdc
->putBitmapSection(rect
.x
, rect
.y
,
454 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
455 0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
460 wxBitmap submask
= GetMask()->GetBitmap()->GetSubBitmap(rect
);
461 ret
.SetMask(new wxMask(submask
));
467 void wxBitmap::SetMonoPalette(const wxColour
& fg
, const wxColour
& bg
)
469 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
471 palette_t
*mono
= M_BMPDATA
->m_bitmap
->pal
;
473 wxCHECK_RET( M_BMPDATA
->m_bpp
== 1, wxT("bitmap is not 1bpp") );
474 wxCHECK_RET( mono
!= NULL
, wxT("bitmap w/o palette") );
476 mono
[0].red
= bg
.Red();
477 mono
[0].green
= bg
.Green();
478 mono
[0].blue
= bg
.Blue();
480 for (size_t i
= 1; i
< 256; i
++)
482 mono
[i
].red
= fg
.Red();
483 mono
[i
].green
= fg
.Green();
484 mono
[i
].blue
= fg
.Blue();
489 MGLDevCtx
*wxBitmap::CreateTmpDC() const
491 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
493 MGLDevCtx
*tdc
= new MGLMemoryDC(GetWidth(), GetHeight(),
494 M_BMPDATA
->m_bitmap
->bitsPerPixel
,
495 M_BMPDATA
->m_bitmap
->pf
,
496 M_BMPDATA
->m_bitmap
->bytesPerLine
,
497 M_BMPDATA
->m_bitmap
->surface
,
499 wxCHECK_MSG( tdc
->isValid(), NULL
, wxT("cannot create temporary MGLDC") );
501 if ( M_BMPDATA
->m_bitmap
->pal
!= NULL
)
505 switch (M_BMPDATA
->m_bitmap
->bitsPerPixel
)
507 case 2: cnt
= 2; break;
508 case 4: cnt
= 16; break;
509 case 8: cnt
= 256; break;
512 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
516 tdc
->setPalette(M_BMPDATA
->m_bitmap
->pal
, cnt
, 0);
517 tdc
->realizePalette(cnt
, 0, FALSE
);
523 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
527 if ( type
== wxBITMAP_TYPE_BMP
|| type
== wxBITMAP_TYPE_PNG
||
528 type
== wxBITMAP_TYPE_PCX
|| type
== wxBITMAP_TYPE_JPEG
)
530 // prevent accidental loading of bitmap from $MGL_ROOT:
531 if ( !wxFileExists(name
) )
533 wxLogError(_("File %s does not exist."), name
.c_str());
538 wxBitmapHandler
*handler
= FindHandler(type
);
540 if ( handler
== NULL
)
543 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
545 wxLogError("no bitmap handler for type %d defined.", type
);
550 *this = wxBitmap(image
);
555 m_refData
= new wxBitmapRefData();
557 return handler
->LoadFile(this, name
, type
, -1, -1);
560 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
562 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
564 wxBitmapHandler
*handler
= FindHandler(type
);
566 if ( handler
== NULL
)
568 wxImage image
= ConvertToImage();
570 image
.SetPalette(*palette
);
573 return image
.SaveFile(filename
, type
);
576 wxLogError("no bitmap handler for type %d defined.", type
);
581 return handler
->SaveFile(this, filename
, type
, palette
);
584 wxPalette
*wxBitmap::GetPalette() const
586 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
588 return M_BMPDATA
->m_palette
;
591 void wxBitmap::SetPalette(const wxPalette
& palette
)
593 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
594 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
596 delete M_BMPDATA
->m_palette
;
597 M_BMPDATA
->m_palette
= NULL
;
599 if ( !palette
.Ok() ) return;
601 M_BMPDATA
->m_palette
= new wxPalette(palette
);
603 int cnt
= palette
.GetColoursCount();
604 palette_t
*pal
= palette
.GetMGLpalette_t();
605 memcpy(M_BMPDATA
->m_bitmap
->pal
, pal
, cnt
* sizeof(palette_t
));
608 void wxBitmap::SetHeight(int height
)
610 if (!m_refData
) m_refData
= new wxBitmapRefData();
612 M_BMPDATA
->m_height
= height
;
615 void wxBitmap::SetWidth(int width
)
617 if (!m_refData
) m_refData
= new wxBitmapRefData();
619 M_BMPDATA
->m_width
= width
;
622 void wxBitmap::SetDepth(int depth
)
624 if (!m_refData
) m_refData
= new wxBitmapRefData();
626 M_BMPDATA
->m_bpp
= depth
;
629 bitmap_t
*wxBitmap::GetMGLbitmap_t() const
631 return M_BMPDATA
->m_bitmap
;
636 //-----------------------------------------------------------------------------
637 // wxBitmap I/O handlers
638 //-----------------------------------------------------------------------------
640 class wxMGLBitmapHandler
: public wxBitmapHandler
643 wxMGLBitmapHandler(wxBitmapType type
,
644 const wxString
& extension
, const wxString
& name
);
646 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
647 void *WXUNUSED(data
),
648 long WXUNUSED(flags
),
650 int WXUNUSED(height
),
651 int WXUNUSED(depth
) = 1)
654 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
655 int desiredWidth
, int desiredHeight
);
656 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
657 int type
, const wxPalette
*palette
= NULL
);
660 wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type
,
661 const wxString
& extension
,
662 const wxString
& name
)
667 SetExtension(extension
);
670 bool wxMGLBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
672 int WXUNUSED(desiredWidth
),
673 int WXUNUSED(desiredHeight
))
675 int width
, height
, bpp
;
682 case wxBITMAP_TYPE_BMP_RESOURCE
:
683 case wxBITMAP_TYPE_JPEG_RESOURCE
:
684 case wxBITMAP_TYPE_PNG_RESOURCE
:
685 case wxBITMAP_TYPE_PCX_RESOURCE
:
686 fullname
= name
+ wxT(".bmp");
695 case wxBITMAP_TYPE_BMP
:
696 case wxBITMAP_TYPE_BMP_RESOURCE
:
697 if ( !MGL_getBitmapSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
699 bitmap
->Create(width
, height
, -1);
700 if ( !bitmap
->Ok() ) return false;
701 dc
.SelectObject(*bitmap
);
702 if ( !dc
.GetMGLDC()->loadBitmapIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
706 case wxBITMAP_TYPE_JPEG
:
707 case wxBITMAP_TYPE_JPEG_RESOURCE
:
708 if ( !MGL_getJPEGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
710 bitmap
->Create(width
, height
, -1);
711 if ( !bitmap
->Ok() ) return false;
712 dc
.SelectObject(*bitmap
);
713 if ( !dc
.GetMGLDC()->loadJPEGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
717 case wxBITMAP_TYPE_PNG
:
718 case wxBITMAP_TYPE_PNG_RESOURCE
:
719 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
721 bitmap
->Create(width
, height
, -1);
722 if ( !bitmap
->Ok() ) return false;
723 dc
.SelectObject(*bitmap
);
724 if ( !dc
.GetMGLDC()->loadPNGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
728 case wxBITMAP_TYPE_PCX
:
729 case wxBITMAP_TYPE_PCX_RESOURCE
:
730 if ( !MGL_getPCXSize(fullname
.mb_str(), &width
, &height
, &bpp
) )
732 bitmap
->Create(width
, height
, -1);
733 if ( !bitmap
->Ok() ) return false;
734 dc
.SelectObject(*bitmap
);
735 if ( !dc
.GetMGLDC()->loadPCXIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
740 wxFAIL_MSG(wxT("Unsupported image format."));
747 bool wxMGLBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
748 int type
, const wxPalette
* WXUNUSED(palette
))
752 int w
= bitmap
->GetWidth(),
753 h
= bitmap
->GetHeight();
755 mem
.SelectObject(*bitmap
);
756 tdc
= mem
.GetMGLDC();
760 case wxBITMAP_TYPE_BMP
:
761 return (bool)tdc
->saveBitmapFromDC(name
.mb_str(), 0, 0, w
, h
);
762 case wxBITMAP_TYPE_JPEG
:
763 return (bool)tdc
->saveJPEGFromDC(name
.mb_str(), 0, 0, w
, h
, 75);
764 case wxBITMAP_TYPE_PNG
:
765 return (bool)tdc
->savePNGFromDC(name
.mb_str(), 0, 0, w
, h
);
766 case wxBITMAP_TYPE_PCX
:
767 return (bool)tdc
->savePCXFromDC(name
.mb_str(), 0, 0, w
, h
);
775 // let's handle PNGs in special way because they have alpha channel
776 // which we can access via bitmap_t most easily
777 class wxPNGBitmapHandler
: public wxMGLBitmapHandler
780 wxPNGBitmapHandler(wxBitmapType type
,
781 const wxString
& extension
, const wxString
& name
)
782 : wxMGLBitmapHandler(type
, extension
, name
) {}
784 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
785 int desiredWidth
, int desiredHeight
);
788 bool wxPNGBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
790 int desiredWidth
, int desiredHeight
)
792 int width
, height
, bpp
;
796 if ( flags
== wxBITMAP_TYPE_PNG_RESOURCE
)
797 fullname
= name
+ wxT(".png");
801 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
806 // We can load ordinary PNGs faster with 'normal' MGL handler.
807 // Only RGBA PNGs need to be processed in special way because
808 // we have to convert alpha channel to mask
809 return wxMGLBitmapHandler::LoadFile(bitmap
, name
, flags
,
810 desiredWidth
, desiredHeight
);
813 bitmap_t
*bmp
= MGL_loadPNG(fullname
.mb_str(), TRUE
);
815 if ( bmp
== NULL
) return false;
817 bitmap
->Create(bmp
->width
, bmp
->height
, -1);
818 if ( !bitmap
->Ok() ) return false;
820 // convert bmp to display's depth and write it to *bitmap:
822 dc
.SelectObject(*bitmap
);
823 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
824 dc
.SelectObject(wxNullBitmap
);
826 // create mask, if bmp contains alpha channel (ARGB format):
827 if ( bmp
->bitsPerPixel
== 32 )
830 wxUint32
*s
= (wxUint32
*)bmp
->surface
;
831 for (y
= 0; y
< bmp
->height
; y
++)
833 s
= ((wxUint32
*)bmp
->surface
) + y
* bmp
->bytesPerLine
/4;
834 for (x
= 0; x
< bmp
->width
; x
++, s
++)
836 if ( ((((*s
) >> bmp
->pf
->alphaPos
) & bmp
->pf
->alphaMask
)
837 << bmp
->pf
->alphaAdjust
) < 128 )
840 *s
= 0x00FFFFFF; // white
843 wxBitmap
mask(bmp
->width
, bmp
->height
, 1);
844 dc
.SelectObject(mask
);
845 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
846 dc
.SelectObject(wxNullBitmap
);
847 bitmap
->SetMask(new wxMask(mask
));
850 MGL_unloadBitmap(bmp
);
858 class wxICOBitmapHandler
: public wxBitmapHandler
861 wxICOBitmapHandler(wxBitmapType type
,
862 const wxString
& extension
, const wxString
& name
);
864 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
865 void *WXUNUSED(data
),
866 long WXUNUSED(flags
),
868 int WXUNUSED(height
),
869 int WXUNUSED(depth
) = 1)
872 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
873 int desiredWidth
, int desiredHeight
);
874 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
875 int type
, const wxPalette
*palette
= NULL
);
878 wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type
,
879 const wxString
& extension
,
880 const wxString
& name
)
885 SetExtension(extension
);
888 bool wxICOBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
890 int WXUNUSED(desiredWidth
),
891 int WXUNUSED(desiredHeight
))
896 if ( flags
== wxBITMAP_TYPE_ICO_RESOURCE
)
897 icon
= MGL_loadIcon(wxString(name
+ wxT(".ico")).mb_str(), TRUE
);
899 icon
= MGL_loadIcon(name
.mb_str(), TRUE
);
901 if ( icon
== NULL
) return false;
903 bitmap
->Create(icon
->xorMask
.width
, icon
->xorMask
.height
);
906 mem
.SelectObject(*bitmap
);
908 dc
->putBitmap(0, 0, &(icon
->xorMask
), MGL_REPLACE_MODE
);
909 mem
.SelectObject(wxNullBitmap
);
911 wxBitmap
mask(icon
->xorMask
.width
, icon
->xorMask
.height
, 1);
912 mem
.SelectObject(mask
);
915 wxCurrentDCSwitcher
curDC(dc
);
919 dc
->putMonoImage(0, 0, icon
->xorMask
.width
, icon
->byteWidth
,
920 icon
->xorMask
.height
, (void*)icon
->andMask
);
922 bitmap
->SetMask(new wxMask(mask
));
924 MGL_unloadIcon(icon
);
929 bool wxICOBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
930 const wxString
& WXUNUSED(name
),
932 const wxPalette
* WXUNUSED(palette
))
940 /*static*/ void wxBitmap::InitStandardHandlers()
942 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP
, wxT("bmp"), wxT("Windows bitmap")));
943 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE
, wxEmptyString
, wxT("Windows bitmap resource")));
944 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG
, wxT("jpg"), wxT("JPEG image")));
945 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE
, wxEmptyString
, wxT("JPEG resource")));
946 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX
, wxT("pcx"), wxT("PCX image")));
947 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE
, wxEmptyString
, wxT("PCX resource")));
949 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG
, wxT("png"), wxT("PNG image")));
950 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE
, wxEmptyString
, wxT("PNG resource")));
952 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO
, wxT("ico"), wxT("Icon resource")));
953 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE
, wxEmptyString
, wxT("Icon resource")));