X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1229a1e7bf7a28e44d764f1c23d97d317df07bfd..29fbbf8967e4c4b3174a4bc45a9007b92ecb0de7:/src/common/imagbmp.cpp diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index 908813c133..66718fc518 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: imagbmp.cpp +// Name: src/common/imagbmp.cpp // Purpose: wxImage BMP,ICO and CUR handlers // Author: Robert Roebling, Chris Elliott // RCS-ID: $Id$ @@ -7,45 +7,37 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "imagbmp.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#include "wx/defs.h" - #if wxUSE_IMAGE #include "wx/imagbmp.h" -#include "wx/bitmap.h" -#include "wx/debug.h" -#include "wx/log.h" -#include "wx/app.h" + +#ifndef WX_PRECOMP + #ifdef __WXMSW__ + #include "wx/msw/wrapwin.h" + #endif + #include "wx/log.h" + #include "wx/app.h" + #include "wx/bitmap.h" + #include "wx/palette.h" + #include "wx/intl.h" + #include "wx/math.h" +#endif + #include "wx/filefn.h" #include "wx/wfstream.h" -#include "wx/intl.h" -#include "wx/module.h" #include "wx/quantize.h" +#include "wx/anidecod.h" // For memcpy #include -#ifdef __SALFORDC__ -#ifdef FAR -#undef FAR -#endif -#endif - -#ifdef __WXMSW__ -#include "wx/msw/wrapwin.h" -#endif - //----------------------------------------------------------------------------- // wxBMPHandler //----------------------------------------------------------------------------- @@ -157,7 +149,7 @@ bool wxBMPHandler::SaveDib(wxImage *image, wxUint16 bpp; // bits per pixel wxUint32 compression; // compression method wxUint32 size_of_bmp; // size of the bitmap - wxUint32 h_res, v_res; // image resolution in dpi + wxUint32 h_res, v_res; // image resolution in pixels-per-meter wxUint32 num_clrs; // number of colors used wxUint32 num_signif_clrs;// number of significant colors } hdr; @@ -184,7 +176,37 @@ bool wxBMPHandler::SaveDib(wxImage *image, hdr.bpp = wxUINT16_SWAP_ON_BE(bpp); hdr.compression = 0; // RGB uncompressed hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight()); - hdr.h_res = hdr.v_res = wxUINT32_SWAP_ON_BE(72); // 72dpi is standard + + // get the resolution from the image options or fall back to 72dpi standard + // for the BMP format if not specified + wxUint32 hres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX), + vres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); + switch ( image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION) ) + { + default: + wxFAIL_MSG( _T("unexpected image resolution units") ); + // fall through + + case wxIMAGE_RESOLUTION_NONE: + hres = + vres = 72; + // fall through to convert it to correct units + + case wxIMAGE_RESOLUTION_INCHES: + // convert resolution in inches to resolution in centimeters + hres = (wxUint32)(100*mm2inches*hres); + vres = (wxUint32)(100*mm2inches*vres); + // fall through to convert it to resolution in meters + + case wxIMAGE_RESOLUTION_CM: + // convert resolution in centimeters to resolution in meters + hres *= 100; + vres *= 100; + break; + } + + hdr.h_res = wxUINT32_SWAP_ON_BE(hres); + hdr.v_res = wxUINT32_SWAP_ON_BE(vres); hdr.num_clrs = wxUINT32_SWAP_ON_BE(palette_size); // # colors in colormap hdr.num_signif_clrs = 0; // all colors are significant @@ -909,6 +931,7 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream, } stream.Read(dbuf, 4 * 2); + int ncolors = wxINT32_SWAP_ON_BE( (int)dbuf[0] ); if (ncolors == 0) ncolors = 1 << bpp; @@ -927,7 +950,7 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream, verbose, IsBmp, true) ) { if (verbose) - wxLogError( _("Error in reading image DIB .") ); + wxLogError( _("Error in reading image DIB.") ); return false; } @@ -947,6 +970,11 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream, } + // the resolution in the bitmap header is in meters, convert to centimeters + image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, wxIMAGE_RESOLUTION_CM); + image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, dbuf[2]/100); + image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, dbuf[3]/100); + return true; } @@ -1118,7 +1146,7 @@ bool wxICOHandler::SaveFile(wxImage *image, } wxUint32 Size = cStream.GetSize(); - // wxCountingOutputStream::Ok() always returns true for now and this + // wxCountingOutputStream::IsOk() always returns true for now and this // "if" provokes VC++ warnings in optimized build #if 0 if ( !cStream.Ok() ) @@ -1328,160 +1356,28 @@ IMPLEMENT_DYNAMIC_CLASS(wxANIHandler, wxCURHandler) #if wxUSE_STREAMS bool wxANIHandler::LoadFile(wxImage *image, wxInputStream& stream, - bool verbose, int index) + bool WXUNUSED(verbose), int index) { - wxInt32 FCC1, FCC2; - wxUint32 datalen; - - wxInt32 riff32; - memcpy( &riff32, "RIFF", 4 ); - wxInt32 list32; - memcpy( &list32, "LIST", 4 ); - wxInt32 ico32; - memcpy( &ico32, "icon", 4 ); - int iIcon = 0; - - stream.SeekI(0); - stream.Read(&FCC1, 4); - if ( FCC1 != riff32 ) + wxANIDecoder decoder; + if (!decoder.Load(stream)) return false; - // we have a riff file: - while (stream.IsOk()) - { - // we always have a data size - stream.Read(&datalen, 4); - datalen = wxINT32_SWAP_ON_BE(datalen) ; - //data should be padded to make even number of bytes - if (datalen % 2 == 1) datalen ++ ; - //now either data or a FCC - if ( (FCC1 == riff32) || (FCC1 == list32) ) - { - stream.Read(&FCC2, 4); - } - else - { - if (FCC1 == ico32 && iIcon >= index) - { - return DoLoadFile(image, stream, verbose, -1); - } - else - { - stream.SeekI(stream.TellI() + datalen); - if ( FCC1 == ico32 ) - iIcon ++; - } - } - - // try to read next data chunk: - stream.Read(&FCC1, 4); - } - return false; + return decoder.ConvertToImage(index != -1 ? (size_t)index : 0, image); } bool wxANIHandler::DoCanRead(wxInputStream& stream) { - wxInt32 FCC1, FCC2; - wxUint32 datalen ; - - wxInt32 riff32; - memcpy( &riff32, "RIFF", 4 ); - wxInt32 list32; - memcpy( &list32, "LIST", 4 ); - wxInt32 ico32; - memcpy( &ico32, "icon", 4 ); - wxInt32 anih32; - memcpy( &anih32, "anih", 4 ); - - stream.SeekI(0); - if ( !stream.Read(&FCC1, 4) ) - return false; - - if ( FCC1 != riff32 ) - return false; - - // we have a riff file: - while ( stream.IsOk() ) - { - if ( FCC1 == anih32 ) - return true; - // we always have a data size: - stream.Read(&datalen, 4); - datalen = wxINT32_SWAP_ON_BE(datalen) ; - //data should be padded to make even number of bytes - if (datalen % 2 == 1) datalen ++ ; - // now either data or a FCC: - if ( (FCC1 == riff32) || (FCC1 == list32) ) - { - stream.Read(&FCC2, 4); - } - else - { - stream.SeekI(stream.TellI() + datalen); - } - - // try to read next data chunk: - if ( !stream.Read(&FCC1, 4) ) - { - // reading failed -- either EOF or IO error, bail out anyhow - return false; - } - } - - return false; + wxANIDecoder decod; + return decod.CanRead(stream); } int wxANIHandler::GetImageCount(wxInputStream& stream) { - wxInt32 FCC1, FCC2; - wxUint32 datalen ; - - wxInt32 riff32; - memcpy( &riff32, "RIFF", 4 ); - wxInt32 list32; - memcpy( &list32, "LIST", 4 ); - wxInt32 ico32; - memcpy( &ico32, "icon", 4 ); - wxInt32 anih32; - memcpy( &anih32, "anih", 4 ); - - stream.SeekI(0); - stream.Read(&FCC1, 4); - if ( FCC1 != riff32 ) + wxANIDecoder decoder; + if (!decoder.Load(stream)) return wxNOT_FOUND; - // we have a riff file: - while ( stream.IsOk() ) - { - // we always have a data size: - stream.Read(&datalen, 4); - datalen = wxINT32_SWAP_ON_BE(datalen) ; - //data should be padded to make even number of bytes - if (datalen % 2 == 1) datalen ++ ; - // now either data or a FCC: - if ( (FCC1 == riff32) || (FCC1 == list32) ) - { - stream.Read(&FCC2, 4); - } - else - { - if ( FCC1 == anih32 ) - { - wxUint32 *pData = new wxUint32[datalen/4]; - stream.Read(pData, datalen); - int nIcons = wxINT32_SWAP_ON_BE(*(pData + 1)); - delete[] pData; - return nIcons; - } - else - stream.SeekI(stream.TellI() + datalen); - } - - // try to read next data chunk: - stream.Read(&FCC1, 4); - } - - return wxNOT_FOUND; + return decoder.GetFrameCount(); } #endif // wxUSE_STREAMS