X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5d4510dc48b2dffad281ab0b2ca873b50e2a0d19..64ea838d8f4d1853b7d850db93ee565e901d099a:/src/common/image.cpp diff --git a/src/common/image.cpp b/src/common/image.cpp index 28e43c26d3..2c61998e6d 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -29,7 +29,6 @@ #include "wx/colour.h" #endif -#include "wx/filefn.h" #include "wx/wfstream.h" #include "wx/xpmdecod.h" @@ -141,8 +140,9 @@ bool wxImage::Create(const char* const* xpmData) wxXPMDecoder decoder; (*this) = decoder.ReadData(xpmData); - return Ok(); + return IsOk(); #else + wxUnusedVar(xpmData); return false; #endif } @@ -259,7 +259,7 @@ wxImage wxImage::MakeEmptyClone(int flags) const { wxImage image; - wxCHECK_MSG( Ok(), image, wxS("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxS("invalid image") ); long height = M_IMGDATA->m_height; long width = M_IMGDATA->m_width; @@ -294,7 +294,7 @@ wxImage wxImage::Copy() const { wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); image.m_refData = CloneRefData(m_refData); @@ -308,7 +308,7 @@ wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); // can't scale to/from 0 size wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image, @@ -427,7 +427,7 @@ wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const { wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); // can't scale to/from 0 size wxCHECK_MSG( (width > 0) && (height > 0), image, @@ -443,23 +443,16 @@ wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const if ( old_width == width && old_height == height ) return *this; - // resample the image using either the nearest neighbourhood, bilinear or - // bicubic method as specified - switch ( quality ) + if (quality == wxIMAGE_QUALITY_HIGH) { - case wxIMAGE_QUALITY_BICUBIC: - case wxIMAGE_QUALITY_BILINEAR: - // both of these algorithms should be used for up-sampling the - // image only, when down-sampling always use box averaging for best - // results - if ( width < old_width && height < old_height ) - image = ResampleBox(width, height); - else if ( quality == wxIMAGE_QUALITY_BILINEAR ) - image = ResampleBilinear(width, height); - else if ( quality == wxIMAGE_QUALITY_BICUBIC ) - image = ResampleBicubic(width, height); - break; + quality = (width < old_width && height < old_height) + ? wxIMAGE_QUALITY_BOX_AVERAGE + : wxIMAGE_QUALITY_BICUBIC; + } + // Resample the image using the method as specified. + switch ( quality ) + { case wxIMAGE_QUALITY_NEAREST: if ( old_width % width == 0 && old_width >= width && old_height % height == 0 && old_height >= height ) @@ -469,6 +462,18 @@ wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const image = ResampleNearest(width, height); break; + + case wxIMAGE_QUALITY_BILINEAR: + image = ResampleBilinear(width, height); + break; + + case wxIMAGE_QUALITY_BICUBIC: + image = ResampleBicubic(width, height); + break; + + case wxIMAGE_QUALITY_BOX_AVERAGE: + image = ResampleBox(width, height); + break; } // If the original image has a mask, apply the mask to the new image @@ -590,16 +595,16 @@ wxImage wxImage::ResampleBox(int width, int height) const averaged_pixels = 0; sum_r = sum_g = sum_b = sum_a = 0.0; - for ( int j = int(src_y - scale_factor_y/2.0 + 1); - j <= int(src_y + scale_factor_y_2); + for ( int j = int(src_y - scale_factor_y/2.0 + 1), k = j; + j <= int(src_y + scale_factor_y_2) || j < k + 2; j++ ) { // We don't care to average pixels that don't exist (edges) if ( j < 0 || j > M_IMGDATA->m_height - 1 ) continue; - for ( int i = int(src_x - scale_factor_x/2.0 + 1); - i <= src_x + scale_factor_x_2; + for ( int i = int(src_x - scale_factor_x/2.0 + 1), e = i; + i <= src_x + scale_factor_x_2 || i < e + 2; i++ ) { // Don't average edge pixels @@ -852,7 +857,7 @@ wxImage wxImage::BlurHorizontal(int blurRadius) const { wxImage ret_image(MakeEmptyClone()); - wxCHECK( ret_image.Ok(), ret_image ); + wxCHECK( ret_image.IsOk(), ret_image ); const unsigned char* src_data = M_IMGDATA->m_data; unsigned char* dst_data = ret_image.GetData(); @@ -955,7 +960,7 @@ wxImage wxImage::BlurVertical(int blurRadius) const { wxImage ret_image(MakeEmptyClone()); - wxCHECK( ret_image.Ok(), ret_image ); + wxCHECK( ret_image.IsOk(), ret_image ); const unsigned char* src_data = M_IMGDATA->m_data; unsigned char* dst_data = ret_image.GetData(); @@ -1070,7 +1075,7 @@ wxImage wxImage::Rotate90( bool clockwise ) const { wxImage image(MakeEmptyClone(Clone_SwapOrientation)); - wxCHECK( image.Ok(), image ); + wxCHECK( image.IsOk(), image ); long height = M_IMGDATA->m_height; long width = M_IMGDATA->m_width; @@ -1090,36 +1095,70 @@ wxImage wxImage::Rotate90( bool clockwise ) const } unsigned char *data = image.GetData(); - const unsigned char *source_data = M_IMGDATA->m_data; unsigned char *target_data; - unsigned char *alpha_data = image.GetAlpha(); - const unsigned char *source_alpha = M_IMGDATA->m_alpha; - unsigned char *target_alpha = 0 ; - for (long j = 0; j < height; j++) + // we rotate the image in 21-pixel (63-byte) wide strips + // to make better use of cpu cache - memory transfers + // (note: while much better than single-pixel "strips", + // our vertical strips will still generally straddle 64-byte cachelines) + for (long ii = 0; ii < width; ) { - for (long i = 0; i < width; i++) + long next_ii = wxMin(ii + 21, width); + + for (long j = 0; j < height; j++) { - if (clockwise) - { - target_data = data + (((i+1)*height) - j - 1)*3; - if (source_alpha) - target_alpha = alpha_data + (((i+1)*height) - j - 1); - } - else + const unsigned char *source_data + = M_IMGDATA->m_data + (j*width + ii)*3; + + for (long i = ii; i < next_ii; i++) { - target_data = data + ((height*(width-1)) + j - (i*height))*3; - if (source_alpha) - target_alpha = alpha_data + ((height*(width-1)) + j - (i*height)); + if ( clockwise ) + { + target_data = data + ((i + 1)*height - j - 1)*3; + } + else + { + target_data = data + (height*(width - 1 - i) + j)*3; + } + memcpy( target_data, source_data, 3 ); + source_data += 3; } - memcpy( target_data, source_data, 3 ); - source_data += 3; + } + + ii = next_ii; + } + + const unsigned char *source_alpha = M_IMGDATA->m_alpha; + + if ( source_alpha ) + { + unsigned char *alpha_data = image.GetAlpha(); + unsigned char *target_alpha = 0 ; + + for (long ii = 0; ii < width; ) + { + long next_ii = wxMin(ii + 64, width); - if (source_alpha) + for (long j = 0; j < height; j++) { - memcpy( target_alpha, source_alpha, 1 ); - source_alpha += 1; + source_alpha = M_IMGDATA->m_alpha + j*width + ii; + + for (long i = ii; i < next_ii; i++) + { + if ( clockwise ) + { + target_alpha = alpha_data + (i+1)*height - j - 1; + } + else + { + target_alpha = alpha_data + height*(width - i - 1) + j; + } + + *target_alpha = *source_alpha++; + } } + + ii = next_ii; } } @@ -1130,7 +1169,7 @@ wxImage wxImage::Rotate180() const { wxImage image(MakeEmptyClone()); - wxCHECK( image.Ok(), image ); + wxCHECK( image.IsOk(), image ); long height = M_IMGDATA->m_height; long width = M_IMGDATA->m_width; @@ -1183,7 +1222,7 @@ wxImage wxImage::Mirror( bool horizontally ) const { wxImage image(MakeEmptyClone()); - wxCHECK( image.Ok(), image ); + wxCHECK( image.IsOk(), image ); long height = M_IMGDATA->m_height; long width = M_IMGDATA->m_width; @@ -1260,7 +1299,7 @@ wxImage wxImage::GetSubImage( const wxRect &rect ) const { wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()), @@ -1313,7 +1352,7 @@ wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, { wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") ); int width = GetWidth(), height = GetHeight(); @@ -1361,8 +1400,8 @@ wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, void wxImage::Paste( const wxImage &image, int x, int y ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); - wxCHECK_RET( image.Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); + wxCHECK_RET( image.IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1390,12 +1429,15 @@ void wxImage::Paste( const wxImage &image, int x, int y ) if (width < 1) return; if (height < 1) return; - if ((!HasMask() && !image.HasMask()) || - (HasMask() && !image.HasMask()) || - ((HasMask() && image.HasMask() && + // If we can, copy the data using memcpy() as this is the fastest way. But + // for this the image being pasted must have "compatible" mask with this + // one meaning that either it must not have one at all or it must use the + // same masked colour. + if ( !image.HasMask() || + ((HasMask() && (GetMaskRed()==image.GetMaskRed()) && (GetMaskGreen()==image.GetMaskGreen()) && - (GetMaskBlue()==image.GetMaskBlue())))) + (GetMaskBlue()==image.GetMaskBlue()))) ) { const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth()); int source_step = image.GetWidth()*3; @@ -1462,7 +1504,7 @@ void wxImage::Paste( const wxImage &image, int x, int y ) void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, unsigned char r2, unsigned char g2, unsigned char b2 ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1493,7 +1535,7 @@ wxImage wxImage::ConvertToGreyscale(double weight_r, double weight_g, double wei { wxImage image(MakeEmptyClone()); - wxCHECK( image.Ok(), image ); + wxCHECK( image.IsOk(), image ); const unsigned char *src = M_IMGDATA->m_data; unsigned char *dest = image.GetData(); @@ -1527,7 +1569,7 @@ wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char { wxImage image; - wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), image, wxT("invalid image") ); image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); @@ -1590,14 +1632,14 @@ wxImage wxImage::ConvertToDisabled(unsigned char brightness) const int wxImage::GetWidth() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), 0, wxT("invalid image") ); return M_IMGDATA->m_width; } int wxImage::GetHeight() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), 0, wxT("invalid image") ); return M_IMGDATA->m_height; } @@ -1621,7 +1663,7 @@ void wxImage::SetType(wxBitmapType type) long wxImage::XYToIndex(int x, int y) const { - if ( Ok() && + if ( IsOk() && x >= 0 && y >= 0 && x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) { @@ -1647,7 +1689,7 @@ void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned c void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1723,14 +1765,14 @@ bool wxImage::IsOk() const unsigned char *wxImage::GetData() const { - wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), (unsigned char *)NULL, wxT("invalid image") ); return M_IMGDATA->m_data; } void wxImage::SetData( unsigned char *data, bool static_data ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); wxImageRefData *newRefData = new wxImageRefData(); @@ -1831,7 +1873,7 @@ wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1849,7 +1891,7 @@ void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) unsigned char *wxImage::GetAlpha() const { - wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), (unsigned char *)NULL, wxT("invalid image") ); return M_IMGDATA->m_alpha; } @@ -1907,7 +1949,7 @@ void wxImage::ClearAlpha() void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1919,7 +1961,7 @@ void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const { - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); if (M_IMGDATA->m_hasMask) { @@ -1937,28 +1979,28 @@ bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned unsigned char wxImage::GetMaskRed() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), 0, wxT("invalid image") ); return M_IMGDATA->m_maskRed; } unsigned char wxImage::GetMaskGreen() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), 0, wxT("invalid image") ); return M_IMGDATA->m_maskGreen; } unsigned char wxImage::GetMaskBlue() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), 0, wxT("invalid image") ); return M_IMGDATA->m_maskBlue; } void wxImage::SetMask( bool mask ) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -1967,7 +2009,7 @@ void wxImage::SetMask( bool mask ) bool wxImage::HasMask() const { - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); return M_IMGDATA->m_hasMask; } @@ -2114,22 +2156,22 @@ bool wxImage::ConvertAlphaToMask(unsigned char mr, bool wxImage::HasPalette() const { - if (!Ok()) + if (!IsOk()) return false; - return M_IMGDATA->m_palette.Ok(); + return M_IMGDATA->m_palette.IsOk(); } const wxPalette& wxImage::GetPalette() const { - wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), wxNullPalette, wxT("invalid image") ); return M_IMGDATA->m_palette; } void wxImage::SetPalette(const wxPalette& palette) { - wxCHECK_RET( Ok(), wxT("invalid image") ); + wxCHECK_RET( IsOk(), wxT("invalid image") ); AllocExclusive(); @@ -2193,26 +2235,118 @@ bool wxImage::HasOption(const wxString& name) const // image I/O // ---------------------------------------------------------------------------- -bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), - wxBitmapType WXUNUSED_UNLESS_STREAMS(type), - int WXUNUSED_UNLESS_STREAMS(index) ) +// Under Windows we can load wxImage not only from files but also from +// resources. +#if defined(__WINDOWS__) && wxUSE_WXDIB && wxUSE_IMAGE + #define HAS_LOAD_FROM_RESOURCE +#endif + +#ifdef HAS_LOAD_FROM_RESOURCE + +#include "wx/msw/dib.h" +#include "wx/msw/private.h" + +static wxImage LoadImageFromResource(const wxString &name, wxBitmapType type) { -#if HAS_FILE_STREAMS - if (wxFileExists(filename)) + AutoHBITMAP + hBitmap, + hMask; + + if ( type == wxBITMAP_TYPE_BMP_RESOURCE ) { - wxImageFileInputStream stream(filename); - wxBufferedInputStream bstream( stream ); - return LoadFile(bstream, type, index); + hBitmap.Init( ::LoadBitmap(wxGetInstance(), name.t_str()) ); + + if ( !hBitmap ) + { + wxLogError(_("Failed to load bitmap \"%s\" from resources."), name); + } + } + else if ( type == wxBITMAP_TYPE_ICO_RESOURCE ) + { + const HICON hIcon = ::LoadIcon(wxGetInstance(), name.t_str()); + + if ( !hIcon ) + { + wxLogError(_("Failed to load icon \"%s\" from resources."), name); + } + else + { + ICONINFO info; + if ( !::GetIconInfo(hIcon, &info) ) + { + wxLogLastError(wxT("GetIconInfo")); + return wxImage(); + } + + hBitmap.Init(info.hbmColor); + hMask.Init(info.hbmMask); + } + } + else if ( type == wxBITMAP_TYPE_CUR_RESOURCE ) + { + wxLogDebug(wxS("Loading cursors from resources is not implemented.")); } else { - wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); + wxFAIL_MSG(wxS("Invalid bitmap resource type.")); + } - return false; + if ( !hBitmap ) + return wxImage(); + + wxImage image = wxDIB(hBitmap).ConvertToImage(); + if ( hMask ) + { + const wxImage mask = wxDIB(hMask).ConvertToImage(); + image.SetMaskFromImage(mask, 255, 255, 255); } -#else // !HAS_FILE_STREAMS - return false; + else + { + // Light gray colour is a default mask + image.SetMaskColour(0xc0, 0xc0, 0xc0); + } + + // We could have already loaded alpha from the resources, but if not, + // initialize it now using the mask. + if ( !image.HasAlpha() ) + image.InitAlpha(); + + return image; +} + +#endif // HAS_LOAD_FROM_RESOURCE + +bool wxImage::LoadFile( const wxString& filename, + wxBitmapType type, + int WXUNUSED_UNLESS_STREAMS(index) ) +{ +#ifdef HAS_LOAD_FROM_RESOURCE + if ( type == wxBITMAP_TYPE_BMP_RESOURCE + || type == wxBITMAP_TYPE_ICO_RESOURCE + || type == wxBITMAP_TYPE_CUR_RESOURCE) + { + const wxImage image = ::LoadImageFromResource(filename, type); + if ( image.IsOk() ) + { + *this = image; + return true; + } + } +#endif // HAS_LOAD_FROM_RESOURCE + +#if HAS_FILE_STREAMS + wxImageFileInputStream stream(filename); + if ( stream.IsOk() ) + { + wxBufferedInputStream bstream( stream ); + if ( LoadFile(bstream, type, index) ) + return true; + } + + wxLogError(_("Failed to load image from file \"%s\"."), filename); #endif // HAS_FILE_STREAMS + + return false; } bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), @@ -2220,21 +2354,18 @@ bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), int WXUNUSED_UNLESS_STREAMS(index) ) { #if HAS_FILE_STREAMS - if (wxFileExists(filename)) + wxImageFileInputStream stream(filename); + if ( stream.IsOk() ) { - wxImageFileInputStream stream(filename); wxBufferedInputStream bstream( stream ); - return LoadFile(bstream, mimetype, index); + if ( LoadFile(bstream, mimetype, index) ) + return true; } - else - { - wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); - return false; - } -#else // !HAS_FILE_STREAMS - return false; + wxLogError(_("Failed to load image from file \"%s\"."), filename); #endif // HAS_FILE_STREAMS + + return false; } @@ -2257,7 +2388,7 @@ bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) const { #if HAS_FILE_STREAMS - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); @@ -2277,7 +2408,7 @@ bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const { #if HAS_FILE_STREAMS - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); @@ -2308,7 +2439,7 @@ int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name), { #if HAS_FILE_STREAMS wxImageFileInputStream stream(name); - if (stream.Ok()) + if (stream.IsOk()) return GetImageCount(stream, type); #endif @@ -2383,8 +2514,20 @@ bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index) const unsigned maxWidth = GetOptionInt(wxIMAGE_OPTION_MAX_WIDTH), maxHeight = GetOptionInt(wxIMAGE_OPTION_MAX_HEIGHT); + // Preserve the original stream position if possible to rewind back to it + // if we failed to load the file -- maybe the next handler that we try can + // succeed after us then. + wxFileOffset posOld = wxInvalidOffset; + if ( stream.IsSeekable() ) + posOld = stream.TellI(); + if ( !handler.LoadFile(this, stream, true/*verbose*/, index) ) + { + if ( posOld != wxInvalidOffset ) + stream.SeekI(posOld); + return false; + } // rescale the image to the specified size if needed if ( maxWidth || maxHeight ) @@ -2403,7 +2546,17 @@ bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index) } if ( width != widthOrig || height != heightOrig ) + { + // get the original size if it was set by the image handler + // but also in order to restore it after Rescale + int widthOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH), + heightOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT); + Rescale(width, height, wxIMAGE_QUALITY_HIGH); + + SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH, widthOrigOption ? widthOrigOption : widthOrig); + SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT, heightOrigOption ? heightOrigOption : heightOrig); + } } // Set this after Rescale, which currently does not preserve it @@ -2420,6 +2573,17 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) if ( type == wxBITMAP_TYPE_ANY ) { + if ( !stream.IsSeekable() ) + { + // The error message about image data format being unknown below + // would be misleading in this case as we are not even going to try + // any handlers because CanRead() never does anything for not + // seekable stream, so try to be more precise here. + wxLogError(_("Can't automatically determine the image format " + "for non-seekable input.")); + return false; + } + const wxList& list = GetHandlers(); for ( wxList::compatibility_iterator node = list.GetFirst(); node; @@ -2430,7 +2594,7 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) return true; } - wxLogWarning( _("No handler found for image type.") ); + wxLogWarning( _("Unknown image data format.") ); return false; } @@ -2445,7 +2609,7 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) if ( stream.IsSeekable() && !handler->CanRead(stream) ) { - wxLogError(_("Image file is not of type %d."), type); + wxLogError(_("This is not a %s."), handler->GetName()); return false; } @@ -2468,7 +2632,7 @@ bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int ind if ( stream.IsSeekable() && !handler->CanRead(stream) ) { - wxLogError(_("Image file is not of type %s."), mimetype); + wxLogError(_("Image is not of type %s."), mimetype); return false; } @@ -2487,7 +2651,7 @@ bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const { - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); wxImageHandler *handler = FindHandler(type); if ( !handler ) @@ -2501,12 +2665,13 @@ bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const { - wxCHECK_MSG( Ok(), false, wxT("invalid image") ); + wxCHECK_MSG( IsOk(), false, wxT("invalid image") ); wxImageHandler *handler = FindHandlerMime(mimetype); if ( !handler ) { wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); + return false; } return DoSave(*handler, stream); @@ -2868,15 +3033,15 @@ int wxImageHandler::GetImageCount( wxInputStream& stream ) bool wxImageHandler::CanRead( const wxString& name ) { - if (wxFileExists(name)) + wxImageFileInputStream stream(name); + if ( !stream.IsOk() ) { - wxImageFileInputStream stream(name); - return CanRead(stream); - } + wxLogError(_("Failed to check format of image file \"%s\"."), name); - wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); + return false; + } - return false; + return CanRead(stream); } bool wxImageHandler::CallDoCanRead(wxInputStream& stream)