X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/716cd4107bb8a5f37dea34507453be6870aaef3a..84c5b38d579e140df28cfbb649587e8862148c89:/src/common/image.cpp?ds=sidebyside diff --git a/src/common/image.cpp b/src/common/image.cpp index 5045805f8b..5086141878 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -28,7 +28,6 @@ #include "wx/log.h" #include "wx/app.h" #include "wx/filefn.h" -#include "wx/filesys.h" #include "wx/wfstream.h" #include "wx/intl.h" #include "wx/module.h" @@ -211,7 +210,14 @@ wxImage wxImage::Scale( int width, int height ) const wxCHECK_MSG( Ok(), image, wxT("invalid image") ); - wxCHECK_MSG( (width > 0) && (height > 0), image, wxT("invalid image size") ); + // can't scale to/from 0 size + wxCHECK_MSG( (width > 0) && (height > 0), image, + wxT("invalid new image size") ); + + long old_height = M_IMGDATA->m_height, + old_width = M_IMGDATA->m_width; + wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, + wxT("invalid old image size") ); image.Create( width, height ); @@ -220,23 +226,32 @@ wxImage wxImage::Scale( int width, int height ) const wxCHECK_MSG( data, image, wxT("unable to create image") ); if (M_IMGDATA->m_hasMask) - image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); - - long old_height = M_IMGDATA->m_height; - long old_width = M_IMGDATA->m_width; + { + image.SetMaskColour( M_IMGDATA->m_maskRed, + M_IMGDATA->m_maskGreen, + M_IMGDATA->m_maskBlue ); + } char unsigned *source_data = M_IMGDATA->m_data; char unsigned *target_data = data; - for (long j = 0; j < height; j++) + // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y + // are from 0 to size-1, hence all decrement the sizes + long old_old_width = old_width; + old_height--; + old_width--; + height--; + width--; + for ( long j = 0; j <= height; j++ ) { - long y_offset = (j * (old_height-1) / (height-1)) * old_width; + // don't crash for images with height == 1 + long y_offset = height ? (j * old_height / height)* old_old_width : 0; - for (long i = 0; i < width; i++) + for ( long i = 0; i <= width; i++ ) { - memcpy( target_data, - source_data + 3*(y_offset + ((i * (old_width-1) )/ (width-1))), - 3 ); + long x_offset = width ? (i * old_width) / width : 0; + + memcpy( target_data, source_data + 3*(y_offset + x_offset), 3 ); target_data += 3; } } @@ -763,19 +778,18 @@ bool wxImage::HasOption(const wxString& name) const bool wxImage::LoadFile( const wxString& filename, long type ) { #if wxUSE_STREAMS - // We want to use wxFileSystem for virtual FS compatibility - wxFileSystem fsys; - wxFSFile *file = fsys.OpenFile(filename); - if (!file) { - wxLogError(_("Can't open file '%s'"), filename); - return FALSE; - } - wxInputStream *stream = file->GetStream(); - if (!stream) { - wxLogError(_("Can't open stream for file '%s'"), filename); + if (wxFileExists(filename)) + { + wxFileInputStream stream(filename); + wxBufferedInputStream bstream( stream ); + return LoadFile(bstream, type); + } + else + { + wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); + return FALSE; - } - return LoadFile(*stream, type); + } #else // !wxUSE_STREAMS return FALSE; #endif // wxUSE_STREAMS @@ -784,19 +798,18 @@ bool wxImage::LoadFile( const wxString& filename, long type ) bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype ) { #if wxUSE_STREAMS - // We want to use wxFileSystem for virtual FS compatibility - wxFileSystem fsys; - wxFSFile *file = fsys.OpenFile(filename); - if (!file) { - wxLogError(_("Can't open file '%s'"), filename); - return FALSE; - } - wxInputStream *stream = file->GetStream(); - if (!stream) { - wxLogError(_("Can't open stream for file '%s'"), filename); + if (wxFileExists(filename)) + { + wxFileInputStream stream(filename); + wxBufferedInputStream bstream( stream ); + return LoadFile(bstream, mimetype); + } + else + { + wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); + return FALSE; - } - return LoadFile(*stream, mimetype); + } #else // !wxUSE_STREAMS return FALSE; #endif // wxUSE_STREAMS @@ -1027,7 +1040,7 @@ wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) void wxImage::InitStandardHandlers() { AddHandler(new wxBMPHandler); -#if !defined(__WXGTK__) && !defined(__WXMOTIF__) +#if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__) AddHandler(new wxXPMHandler); #endif } @@ -1147,7 +1160,7 @@ unsigned long wxImage::CountColours( unsigned long stopafter ) wxHashTable h; wxObject dummy; unsigned char r, g, b; - unsigned char *p; + unsigned char *p; unsigned long size, nentries, key; p = GetData(); @@ -1182,7 +1195,7 @@ unsigned long wxImage::CountColours( unsigned long stopafter ) unsigned long wxImage::ComputeHistogram( wxHashTable &h ) { unsigned char r, g, b; - unsigned char *p; + unsigned char *p; unsigned long size, nentries, key; wxHNode *hnode;