X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fdbb06ba2cf7e82e5a9c325c60da1772050cbf33..525582584c8fe3a8ef11d2c981af415f7bab2073:/src/common/image.cpp?ds=sidebyside diff --git a/src/common/image.cpp b/src/common/image.cpp index 03f2b4b36e..e43c7da643 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -63,6 +63,8 @@ public: #endif // wxUSE_PALETTE wxArrayString m_optionNames; wxArrayString m_optionValues; + + DECLARE_NO_COPY_CLASS(wxImageRefData) }; wxImageRefData::wxImageRefData() @@ -964,7 +966,7 @@ bool wxImage::SaveFile( const wxString& filename, int type ) const wxFileOutputStream stream(filename); - if ( stream.LastError() == wxStream_NOERROR ) + if ( stream.IsOk() ) { wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, type); @@ -981,7 +983,7 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) con wxFileOutputStream stream(filename); - if ( stream.LastError() == wxStream_NOERROR ) + if ( stream.IsOk() ) { wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, mimetype); @@ -1005,10 +1007,11 @@ int wxImage::GetImageCount( const wxString &name, long type ) { #if wxUSE_STREAMS wxFileInputStream stream(name); - return GetImageCount(stream, type); -#else - return 0; + if (stream.Ok()) + return GetImageCount(stream, type); #endif + + return 0; } #if wxUSE_STREAMS @@ -1092,7 +1095,7 @@ bool wxImage::LoadFile( wxInputStream& stream, long type, int index ) handler = FindHandler(type); - if (handler == NULL) + if (handler == 0) { wxLogWarning( _("No image handler for type %d defined."), type ); @@ -1110,7 +1113,7 @@ bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int ind wxImageHandler *handler = FindHandlerMime(mimetype); - if (handler == NULL) + if (handler == 0) { wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); @@ -1126,7 +1129,7 @@ bool wxImage::SaveFile( wxOutputStream& stream, int type ) const wxImageHandler *handler = FindHandler(type); - if (handler == NULL) + if (handler == 0) { wxLogWarning( _("No image handler for type %d defined."), type ); @@ -1142,7 +1145,7 @@ bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const wxImageHandler *handler = FindHandlerMime(mimetype); - if (handler == NULL) + if (handler == 0) { wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); @@ -1158,7 +1161,23 @@ void wxImage::AddHandler( wxImageHandler *handler ) // make sure that the memory will be freed at the program end sm_handlers.DeleteContents(TRUE); - sm_handlers.Append( handler ); + // Check for an existing handler of the type being added. + if (FindHandler( handler->GetType() ) == 0) + { + sm_handlers.Append( handler ); + } + else + { + // This is not documented behaviour, merely the simplest 'fix' + // for preventing duplicate additions. If someone ever has + // a good reason to add and remove duplicate handlers (and they + // may) we should probably refcount the duplicates. + // also an issue in InsertHandler below. + + wxLogDebug( _T("Adding duplicate image handler for '%s'"), + handler->GetName().c_str() ); + delete handler; + } } void wxImage::InsertHandler( wxImageHandler *handler ) @@ -1166,7 +1185,18 @@ void wxImage::InsertHandler( wxImageHandler *handler ) // make sure that the memory will be freed at the program end sm_handlers.DeleteContents(TRUE); - sm_handlers.Insert( handler ); + // Check for an existing handler of the type being added. + if (FindHandler( handler->GetType() ) == 0) + { + sm_handlers.Insert( handler ); + } + else + { + // see AddHandler for additional comments. + wxLogDebug( _T("Inserting duplicate image handler for '%s'"), + handler->GetName().c_str() ); + delete handler; + } } bool wxImage::RemoveHandler( const wxString& name ) @@ -1183,53 +1213,53 @@ bool wxImage::RemoveHandler( const wxString& name ) wxImageHandler *wxImage::FindHandler( const wxString& name ) { - wxNode *node = sm_handlers.First(); + wxNode *node = sm_handlers.GetFirst(); while (node) { - wxImageHandler *handler = (wxImageHandler*)node->Data(); + wxImageHandler *handler = (wxImageHandler*)node->GetData(); if (handler->GetName().Cmp(name) == 0) return handler; - node = node->Next(); + node = node->GetNext(); } - return (wxImageHandler *)NULL; + return 0; } wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) { - wxNode *node = sm_handlers.First(); + wxNode *node = sm_handlers.GetFirst(); while (node) { - wxImageHandler *handler = (wxImageHandler*)node->Data(); + wxImageHandler *handler = (wxImageHandler*)node->GetData(); if ( (handler->GetExtension().Cmp(extension) == 0) && (bitmapType == -1 || handler->GetType() == bitmapType) ) return handler; - node = node->Next(); + node = node->GetNext(); } - return (wxImageHandler*)NULL; + return 0; } wxImageHandler *wxImage::FindHandler( long bitmapType ) { - wxNode *node = sm_handlers.First(); + wxNode *node = sm_handlers.GetFirst(); while (node) { - wxImageHandler *handler = (wxImageHandler *)node->Data(); + wxImageHandler *handler = (wxImageHandler *)node->GetData(); if (handler->GetType() == bitmapType) return handler; - node = node->Next(); + node = node->GetNext(); } - return NULL; + return 0; } wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) { - wxNode *node = sm_handlers.First(); + wxNode *node = sm_handlers.GetFirst(); while (node) { - wxImageHandler *handler = (wxImageHandler *)node->Data(); + wxImageHandler *handler = (wxImageHandler *)node->GetData(); if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler; - node = node->Next(); + node = node->GetNext(); } - return NULL; + return 0; } void wxImage::InitStandardHandlers() @@ -1237,19 +1267,15 @@ void wxImage::InitStandardHandlers() #if wxUSE_STREAMS AddHandler(new wxBMPHandler); #endif // wxUSE_STREAMS - -#if wxUSE_XPM && !defined(__WXGTK__) && !defined(__WXMOTIF__) - AddHandler(new wxXPMHandler); -#endif } void wxImage::CleanUpHandlers() { - wxNode *node = sm_handlers.First(); + wxNode *node = sm_handlers.GetFirst(); while (node) { - wxImageHandler *handler = (wxImageHandler *)node->Data(); - wxNode *next = node->Next(); + wxImageHandler *handler = (wxImageHandler *)node->GetData(); + wxNode *next = node->GetNext(); delete handler; delete node; node = next; @@ -1286,12 +1312,32 @@ bool wxImageHandler::CanRead( const wxString& name ) return CanRead(stream); } - else { - wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); + wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); + + return FALSE; +} +bool wxImageHandler::CallDoCanRead(wxInputStream& stream) +{ + off_t posOld = stream.TellI(); + if ( posOld == wxInvalidOffset ) + { + // can't test unseekable stream return FALSE; } -// return FALSE; + + bool ok = DoCanRead(stream); + + // restore the old position to be able to test other formats and so on + if ( stream.SeekI(posOld) == wxInvalidOffset ) + { + wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!")); + + // reading would fail anyhow as we're not at the right position + return FALSE; + } + + return ok; } #endif // wxUSE_STREAMS @@ -1299,7 +1345,7 @@ bool wxImageHandler::CanRead( const wxString& name ) //----------------------------------------------------------------------------- -// Deprecated wxBitmap convertion routines +// Deprecated wxBitmap conversion routines //----------------------------------------------------------------------------- #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI