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"
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((unsigned char)((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
),
64 (unsigned char)((clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
),
65 (unsigned char)((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()));
117 m_bitmap
= new wxBitmap(image
, 1);
119 return m_bitmap
->Ok();
122 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
125 wxPalette
*pal
= bitmap
.GetPalette();
127 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
129 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
131 return Create(bitmap
, wxColour(r
, g
, b
));
134 bool wxMask::Create(const wxBitmap
& bitmap
)
139 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap") );
140 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
142 m_bitmap
= new wxBitmap(bitmap
);
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 class wxBitmapRefData
: public wxObjectRefData
160 wxPalette
*m_palette
;
165 wxBitmapRefData::wxBitmapRefData()
175 wxBitmapRefData::~wxBitmapRefData()
178 MGL_unloadBitmap(m_bitmap
);
183 //-----------------------------------------------------------------------------
185 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
188 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
,wxObject
)
189 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxBitmapBase
)
191 wxBitmap::wxBitmap(int width
, int height
, int depth
)
193 Create(width
, height
, depth
);
197 static bitmap_t
*MyMGL_createBitmap(int width
, int height
,
198 int bpp
, pixel_format_t
*pf
)
200 MGLMemoryDC
mdc(width
, height
, bpp
, pf
);
201 return MGL_getBitmapFromDC(mdc
.getDC(), 0, 0, width
, height
, TRUE
);
204 bool wxBitmap::Create(int width
, int height
, int depth
)
208 wxCHECK_MSG( (width
> 0) && (height
> 0), false, wxT("invalid bitmap size") );
210 pixel_format_t pf_dummy
;
212 int mglDepth
= depth
;
217 wxASSERT_MSG( g_displayDC
, wxT("MGL display DC not created yet.") );
219 g_displayDC
->getPixelFormat(pf_dummy
);
220 mglDepth
= g_displayDC
->getBitsPerPixel();
228 pf
= &gs_pixel_format_15
;
231 pf
= &gs_pixel_format_16
;
234 pf
= &gs_pixel_format_24
;
237 pf
= &gs_pixel_format_32
;
240 wxASSERT_MSG( 0, wxT("invalid bitmap depth") );
244 m_refData
= new wxBitmapRefData();
245 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
246 M_BMPDATA
->m_palette
= (wxPalette
*) NULL
;
247 M_BMPDATA
->m_width
= width
;
248 M_BMPDATA
->m_height
= height
;
249 M_BMPDATA
->m_bpp
= mglDepth
;
253 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, mglDepth
, pf
);
257 // MGL does not support mono DCs, so we have to emulate them with
258 // 8bpp ones. We do that by using a special palette with color 0
259 // set to black and all other colors set to white.
261 M_BMPDATA
->m_bitmap
= MyMGL_createBitmap(width
, height
, 8, pf
);
262 SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0));
268 bool wxBitmap::CreateFromXpm(const char **bits
)
270 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
272 wxXPMDecoder decoder
;
273 wxImage img
= decoder
.ReadData(bits
);
274 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
276 *this = wxBitmap(img
);
281 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
285 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
287 width
= image
.GetWidth();
288 height
= image
.GetHeight();
290 if ( !Create(width
, height
, depth
) ) return;
292 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
293 width
* 3, (void*)image
.GetData(), NULL
);
294 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
296 MGLDevCtx
*bdc
= CreateTmpDC();
298 if ( GetDepth() <= 8 && image
.HasPalette() )
299 SetPalette(image
.GetPalette());
301 bdc
->bitBlt(idc
, 0, 0, width
, height
, 0, 0, MGL_REPLACE_MODE
);
304 if ( image
.HasMask() )
306 wxImage mask_image
= image
.ConvertToMono(image
.GetMaskRed(),
307 image
.GetMaskGreen(),
308 image
.GetMaskBlue());
309 mask_image
.SetMask(false);
310 wxBitmap
mask_bmp(mask_image
, 1);
311 SetMask(new wxMask(mask_bmp
));
315 wxImage
wxBitmap::ConvertToImage() const
317 wxCHECK_MSG( Ok(), wxImage(), wxT("invalid bitmap") );
321 height
= GetHeight();
323 wxImage
image(width
, height
);
324 wxASSERT_MSG( image
.Ok(), wxT("cannot create image") );
326 MGLMemoryDC
idc(width
, height
, 24, &gs_pixel_format_wxImage
,
327 width
* 3, (void*)image
.GetData(), NULL
);
328 wxASSERT_MSG( idc
.isValid(), wxT("cannot create custom MGLDC") );
330 if ( M_BMPDATA
->m_palette
)
331 image
.SetPalette(*(M_BMPDATA
->m_palette
));
335 // in consistency with other ports, we convert parts covered
336 // by the mask to <16,16,16> colour and set that colour to image's
337 // mask. We do that by OR-blitting the mask over image with
338 // bg colour set to black and fg colour to <16,16,16>
340 image
.SetMaskColour(16, 16, 16);
344 tmpDC
.SetMGLDC(&idc
, false);
345 tmpDC
.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID
));
347 tmpDC
.DrawBitmap(*this, 0, 0, true);
351 image
.SetMask(false);
352 idc
.putBitmap(0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
358 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
360 LoadFile(filename
, type
);
363 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
365 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
367 if ( !Create(width
, height
, 1) ) return;
368 MGLDevCtx
*bdc
= CreateTmpDC();
369 wxCurrentDCSwitcher
curDC(bdc
);
371 bdc
->setBackColor(0);
373 bdc
->putMonoImage(0, 0, width
, (width
+ 7) / 8, height
, (void*)bits
);
377 bool wxBitmap::operator == (const wxBitmap
& bmp
) const
379 return (m_refData
== bmp
.m_refData
);
382 bool wxBitmap::operator != (const wxBitmap
& bmp
) const
384 return (m_refData
!= bmp
.m_refData
);
387 bool wxBitmap::Ok() const
389 return (m_refData
!= NULL
&& M_BMPDATA
->m_bitmap
!= NULL
);
392 int wxBitmap::GetHeight() const
394 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
396 return M_BMPDATA
->m_height
;
399 int wxBitmap::GetWidth() const
401 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
403 return M_BMPDATA
->m_width
;
406 int wxBitmap::GetDepth() const
408 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
410 return M_BMPDATA
->m_bpp
;
413 wxMask
*wxBitmap::GetMask() const
415 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
417 return M_BMPDATA
->m_mask
;
420 void wxBitmap::SetMask(wxMask
*mask
)
422 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
424 delete M_BMPDATA
->m_mask
;
425 M_BMPDATA
->m_mask
= mask
;
428 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
430 wxBitmap
*bmp
= (wxBitmap
*)(&icon
);
435 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
438 (rect
.x
>= 0) && (rect
.y
>= 0) &&
439 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
440 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
442 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
443 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
446 ret
.SetPalette(*GetPalette());
448 MGLDevCtx
*tdc
= ret
.CreateTmpDC();
449 tdc
->putBitmapSection(rect
.x
, rect
.y
,
450 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
451 0, 0, M_BMPDATA
->m_bitmap
, MGL_REPLACE_MODE
);
456 wxBitmap submask
= GetMask()->GetBitmap()->GetSubBitmap(rect
);
457 ret
.SetMask(new wxMask(submask
));
463 void wxBitmap::SetMonoPalette(const wxColour
& fg
, const wxColour
& bg
)
465 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
467 palette_t
*mono
= M_BMPDATA
->m_bitmap
->pal
;
469 wxCHECK_RET( M_BMPDATA
->m_bpp
== 1, wxT("bitmap is not 1bpp") );
470 wxCHECK_RET( mono
!= NULL
, wxT("bitmap w/o palette") );
472 mono
[0].red
= bg
.Red();
473 mono
[0].green
= bg
.Green();
474 mono
[0].blue
= bg
.Blue();
476 for (size_t i
= 1; i
< 256; i
++)
478 mono
[i
].red
= fg
.Red();
479 mono
[i
].green
= fg
.Green();
480 mono
[i
].blue
= fg
.Blue();
485 MGLDevCtx
*wxBitmap::CreateTmpDC() const
487 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
489 MGLDevCtx
*tdc
= new MGLMemoryDC(GetWidth(), GetHeight(),
490 M_BMPDATA
->m_bitmap
->bitsPerPixel
,
491 M_BMPDATA
->m_bitmap
->pf
,
492 M_BMPDATA
->m_bitmap
->bytesPerLine
,
493 M_BMPDATA
->m_bitmap
->surface
,
495 wxCHECK_MSG( tdc
->isValid(), NULL
, wxT("cannot create temporary MGLDC") );
497 if ( M_BMPDATA
->m_bitmap
->pal
!= NULL
)
501 switch (M_BMPDATA
->m_bitmap
->bitsPerPixel
)
503 case 2: cnt
= 2; break;
504 case 4: cnt
= 16; break;
505 case 8: cnt
= 256; break;
508 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
512 tdc
->setPalette(M_BMPDATA
->m_bitmap
->pal
, cnt
, 0);
513 tdc
->realizePalette(cnt
, 0, FALSE
);
519 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
523 if ( type
== wxBITMAP_TYPE_BMP
|| type
== wxBITMAP_TYPE_PNG
||
524 type
== wxBITMAP_TYPE_PCX
|| type
== wxBITMAP_TYPE_JPEG
)
526 // prevent accidental loading of bitmap from $MGL_ROOT:
527 if ( !wxFileExists(name
) )
529 wxLogError(_("File %s does not exist."), name
.c_str());
534 wxBitmapHandler
*handler
= FindHandler(type
);
536 if ( handler
== NULL
)
539 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
541 wxLogError("no bitmap handler for type %d defined.", type
);
546 *this = wxBitmap(image
);
551 m_refData
= new wxBitmapRefData();
553 return handler
->LoadFile(this, name
, type
, -1, -1);
556 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
558 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
560 wxBitmapHandler
*handler
= FindHandler(type
);
562 if ( handler
== NULL
)
564 wxImage image
= ConvertToImage();
566 image
.SetPalette(*palette
);
569 return image
.SaveFile(filename
, type
);
572 wxLogError("no bitmap handler for type %d defined.", type
);
577 return handler
->SaveFile(this, filename
, type
, palette
);
580 wxPalette
*wxBitmap::GetPalette() const
582 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
584 return M_BMPDATA
->m_palette
;
587 void wxBitmap::SetPalette(const wxPalette
& palette
)
589 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
590 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
592 delete M_BMPDATA
->m_palette
;
593 M_BMPDATA
->m_palette
= NULL
;
595 if ( !palette
.Ok() ) return;
597 M_BMPDATA
->m_palette
= new wxPalette(palette
);
599 int cnt
= palette
.GetColoursCount();
600 palette_t
*pal
= palette
.GetMGLpalette_t();
601 memcpy(M_BMPDATA
->m_bitmap
->pal
, pal
, cnt
* sizeof(palette_t
));
604 void wxBitmap::SetHeight(int height
)
606 if (!m_refData
) m_refData
= new wxBitmapRefData();
608 M_BMPDATA
->m_height
= height
;
611 void wxBitmap::SetWidth(int width
)
613 if (!m_refData
) m_refData
= new wxBitmapRefData();
615 M_BMPDATA
->m_width
= width
;
618 void wxBitmap::SetDepth(int depth
)
620 if (!m_refData
) m_refData
= new wxBitmapRefData();
622 M_BMPDATA
->m_bpp
= depth
;
625 bitmap_t
*wxBitmap::GetMGLbitmap_t() const
627 return M_BMPDATA
->m_bitmap
;
632 //-----------------------------------------------------------------------------
633 // wxBitmap I/O handlers
634 //-----------------------------------------------------------------------------
636 class wxMGLBitmapHandler
: public wxBitmapHandler
639 wxMGLBitmapHandler(wxBitmapType type
,
640 const wxString
& extension
, const wxString
& name
);
642 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
643 void *WXUNUSED(data
),
644 long WXUNUSED(flags
),
646 int WXUNUSED(height
),
647 int WXUNUSED(depth
) = 1)
650 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
651 int desiredWidth
, int desiredHeight
);
652 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
653 int type
, const wxPalette
*palette
= NULL
);
656 wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type
,
657 const wxString
& extension
,
658 const wxString
& name
)
663 SetExtension(extension
);
666 bool wxMGLBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
668 int WXUNUSED(desiredWidth
),
669 int WXUNUSED(desiredHeight
))
671 int width
, height
, bpp
;
678 case wxBITMAP_TYPE_BMP_RESOURCE
:
679 case wxBITMAP_TYPE_JPEG_RESOURCE
:
680 case wxBITMAP_TYPE_PNG_RESOURCE
:
681 case wxBITMAP_TYPE_PCX_RESOURCE
:
682 fullname
= name
+ wxT(".bmp");
691 case wxBITMAP_TYPE_BMP
:
692 case wxBITMAP_TYPE_BMP_RESOURCE
:
693 if ( !MGL_getBitmapSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
695 bitmap
->Create(width
, height
, -1);
696 if ( !bitmap
->Ok() ) return false;
697 dc
.SelectObject(*bitmap
);
698 if ( !dc
.GetMGLDC()->loadBitmapIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
702 case wxBITMAP_TYPE_JPEG
:
703 case wxBITMAP_TYPE_JPEG_RESOURCE
:
704 if ( !MGL_getJPEGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
706 bitmap
->Create(width
, height
, -1);
707 if ( !bitmap
->Ok() ) return false;
708 dc
.SelectObject(*bitmap
);
709 if ( !dc
.GetMGLDC()->loadJPEGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
713 case wxBITMAP_TYPE_PNG
:
714 case wxBITMAP_TYPE_PNG_RESOURCE
:
715 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
717 bitmap
->Create(width
, height
, -1);
718 if ( !bitmap
->Ok() ) return false;
719 dc
.SelectObject(*bitmap
);
720 if ( !dc
.GetMGLDC()->loadPNGIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
724 case wxBITMAP_TYPE_PCX
:
725 case wxBITMAP_TYPE_PCX_RESOURCE
:
726 if ( !MGL_getPCXSize(fullname
.mb_str(), &width
, &height
, &bpp
) )
728 bitmap
->Create(width
, height
, -1);
729 if ( !bitmap
->Ok() ) return false;
730 dc
.SelectObject(*bitmap
);
731 if ( !dc
.GetMGLDC()->loadPCXIntoDC(fullname
.mb_str(), 0, 0, TRUE
) )
736 wxFAIL_MSG(wxT("Unsupported image format."));
743 bool wxMGLBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
744 int type
, const wxPalette
* WXUNUSED(palette
))
748 int w
= bitmap
->GetWidth(),
749 h
= bitmap
->GetHeight();
751 mem
.SelectObject(*bitmap
);
752 tdc
= mem
.GetMGLDC();
756 case wxBITMAP_TYPE_BMP
:
757 return (bool)tdc
->saveBitmapFromDC(name
.mb_str(), 0, 0, w
, h
);
758 case wxBITMAP_TYPE_JPEG
:
759 return (bool)tdc
->saveJPEGFromDC(name
.mb_str(), 0, 0, w
, h
, 75);
760 case wxBITMAP_TYPE_PNG
:
761 return (bool)tdc
->savePNGFromDC(name
.mb_str(), 0, 0, w
, h
);
762 case wxBITMAP_TYPE_PCX
:
763 return (bool)tdc
->savePCXFromDC(name
.mb_str(), 0, 0, w
, h
);
771 // let's handle PNGs in special way because they have alpha channel
772 // which we can access via bitmap_t most easily
773 class wxPNGBitmapHandler
: public wxMGLBitmapHandler
776 wxPNGBitmapHandler(wxBitmapType type
,
777 const wxString
& extension
, const wxString
& name
)
778 : wxMGLBitmapHandler(type
, extension
, name
) {}
780 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
781 int desiredWidth
, int desiredHeight
);
784 bool wxPNGBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
786 int desiredWidth
, int desiredHeight
)
788 int width
, height
, bpp
;
792 if ( flags
== wxBITMAP_TYPE_PNG_RESOURCE
)
793 fullname
= name
+ wxT(".png");
797 if ( !MGL_getPNGSize(fullname
.mb_str(), &width
, &height
, &bpp
, &pf
) )
802 // We can load ordinary PNGs faster with 'normal' MGL handler.
803 // Only RGBA PNGs need to be processed in special way because
804 // we have to convert alpha channel to mask
805 return wxMGLBitmapHandler::LoadFile(bitmap
, name
, flags
,
806 desiredWidth
, desiredHeight
);
809 bitmap_t
*bmp
= MGL_loadPNG(fullname
.mb_str(), TRUE
);
811 if ( bmp
== NULL
) return false;
813 bitmap
->Create(bmp
->width
, bmp
->height
, -1);
814 if ( !bitmap
->Ok() ) return false;
816 // convert bmp to display's depth and write it to *bitmap:
818 dc
.SelectObject(*bitmap
);
819 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
820 dc
.SelectObject(wxNullBitmap
);
822 // create mask, if bmp contains alpha channel (ARGB format):
823 if ( bmp
->bitsPerPixel
== 32 )
826 wxUint32
*s
= (wxUint32
*)bmp
->surface
;
827 for (y
= 0; y
< bmp
->height
; y
++)
829 s
= ((wxUint32
*)bmp
->surface
) + y
* bmp
->bytesPerLine
/4;
830 for (x
= 0; x
< bmp
->width
; x
++, s
++)
832 if ( ((((*s
) >> bmp
->pf
->alphaPos
) & bmp
->pf
->alphaMask
)
833 << bmp
->pf
->alphaAdjust
) < 128 )
836 *s
= 0x00FFFFFF; // white
839 wxBitmap
mask(bmp
->width
, bmp
->height
, 1);
840 dc
.SelectObject(mask
);
841 dc
.GetMGLDC()->putBitmap(0, 0, bmp
, MGL_REPLACE_MODE
);
842 dc
.SelectObject(wxNullBitmap
);
843 bitmap
->SetMask(new wxMask(mask
));
846 MGL_unloadBitmap(bmp
);
854 class wxICOBitmapHandler
: public wxBitmapHandler
857 wxICOBitmapHandler(wxBitmapType type
,
858 const wxString
& extension
, const wxString
& name
);
860 virtual bool Create(wxBitmap
*WXUNUSED(bitmap
),
861 void *WXUNUSED(data
),
862 long WXUNUSED(flags
),
864 int WXUNUSED(height
),
865 int WXUNUSED(depth
) = 1)
868 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
869 int desiredWidth
, int desiredHeight
);
870 virtual bool SaveFile(const wxBitmap
*bitmap
, const wxString
& name
,
871 int type
, const wxPalette
*palette
= NULL
);
874 wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type
,
875 const wxString
& extension
,
876 const wxString
& name
)
881 SetExtension(extension
);
884 bool wxICOBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
,
886 int WXUNUSED(desiredWidth
),
887 int WXUNUSED(desiredHeight
))
892 if ( flags
== wxBITMAP_TYPE_ICO_RESOURCE
)
893 icon
= MGL_loadIcon(wxString(name
+ wxT(".ico")).mb_str(), TRUE
);
895 icon
= MGL_loadIcon(name
.mb_str(), TRUE
);
897 if ( icon
== NULL
) return false;
899 bitmap
->Create(icon
->xorMask
.width
, icon
->xorMask
.height
);
902 mem
.SelectObject(*bitmap
);
904 dc
->putBitmap(0, 0, &(icon
->xorMask
), MGL_REPLACE_MODE
);
905 mem
.SelectObject(wxNullBitmap
);
907 wxBitmap
mask(icon
->xorMask
.width
, icon
->xorMask
.height
, 1);
908 mem
.SelectObject(mask
);
911 wxCurrentDCSwitcher
curDC(dc
);
915 dc
->putMonoImage(0, 0, icon
->xorMask
.width
, icon
->byteWidth
,
916 icon
->xorMask
.height
, (void*)icon
->andMask
);
918 bitmap
->SetMask(new wxMask(mask
));
920 MGL_unloadIcon(icon
);
925 bool wxICOBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
926 const wxString
& WXUNUSED(name
),
928 const wxPalette
* WXUNUSED(palette
))
936 /*static*/ void wxBitmap::InitStandardHandlers()
938 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP
, wxT("bmp"), wxT("Windows bitmap")));
939 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE
, wxEmptyString
, wxT("Windows bitmap resource")));
940 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG
, wxT("jpg"), wxT("JPEG image")));
941 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE
, wxEmptyString
, wxT("JPEG resource")));
942 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX
, wxT("pcx"), wxT("PCX image")));
943 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE
, wxEmptyString
, wxT("PCX resource")));
945 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG
, wxT("png"), wxT("PNG image")));
946 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE
, wxEmptyString
, wxT("PNG resource")));
948 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO
, wxT("ico"), wxT("Icon resource")));
949 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE
, wxEmptyString
, wxT("Icon resource")));