X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e0a76d8d8a91d5a13c1fab7257aae4da8311f58d..008a56c968ed7e0694e32e93c2dbf95dde2ba5c8:/src/common/image.cpp diff --git a/src/common/image.cpp b/src/common/image.cpp index 48b39581f5..a9201c2aaf 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -131,6 +131,7 @@ wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index ) #endif // wxUSE_STREAMS wxImage::wxImage( const wxImage& image ) + : wxObject() { Ref(image); } @@ -198,8 +199,8 @@ wxImage wxImage::Copy() 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 ); + image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); + image.SetMask( M_IMGDATA->m_hasMask ); memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 ); @@ -275,6 +276,14 @@ wxImage wxImage::Scale( int width, int height ) const } #endif + // In case this is a cursor, make sure the hotspot is scalled accordingly: + if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, + (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); + if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, + (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); + return image; } @@ -930,12 +939,32 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int #endif // wxUSE_STREAMS } + + +bool wxImage::SaveFile( const wxString& filename ) const +{ + wxString ext = filename.AfterLast('.').Lower(); + + wxImageHandler * pHandler = FindHandler(ext, -1); + if (pHandler) + { + SaveFile(filename, pHandler->GetType()); + return TRUE; + } + + wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str()); + + return FALSE; +} + bool wxImage::SaveFile( const wxString& filename, int type ) const { #if wxUSE_STREAMS + ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); + wxFileOutputStream stream(filename); - if ( stream.LastError() == wxStream_NOERROR ) + if ( stream.IsOk() ) { wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, type); @@ -948,9 +977,11 @@ bool wxImage::SaveFile( const wxString& filename, int type ) const bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const { #if wxUSE_STREAMS + ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); + wxFileOutputStream stream(filename); - if ( stream.LastError() == wxStream_NOERROR ) + if ( stream.IsOk() ) { wxBufferedOutputStream bstream( stream ); return SaveFile(bstream, mimetype); @@ -974,7 +1005,10 @@ int wxImage::GetImageCount( const wxString &name, long type ) { #if wxUSE_STREAMS wxFileInputStream stream(name); - return GetImageCount(stream, type); + if (!stream.Ok()) + return 0; + else + return GetImageCount(stream, type); #else return 0; #endif @@ -984,7 +1018,7 @@ int wxImage::GetImageCount( const wxString &name, long type ) bool wxImage::CanRead( wxInputStream &stream ) { - wxList &list=GetHandlers(); + const wxList& list = GetHandlers(); for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) { @@ -1061,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 ); @@ -1079,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() ); @@ -1095,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 ); @@ -1111,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() ); @@ -1127,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 ) @@ -1135,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 ) @@ -1160,7 +1221,7 @@ wxImageHandler *wxImage::FindHandler( const wxString& name ) node = node->Next(); } - return (wxImageHandler *)NULL; + return 0; } wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) @@ -1174,7 +1235,7 @@ wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType return handler; node = node->Next(); } - return (wxImageHandler*)NULL; + return 0; } wxImageHandler *wxImage::FindHandler( long bitmapType ) @@ -1186,7 +1247,7 @@ wxImageHandler *wxImage::FindHandler( long bitmapType ) if (handler->GetType() == bitmapType) return handler; node = node->Next(); } - return NULL; + return 0; } wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) @@ -1198,7 +1259,7 @@ wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler; node = node->Next(); } - return NULL; + return 0; } void wxImage::InitStandardHandlers() @@ -1206,10 +1267,6 @@ 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() @@ -1255,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 @@ -1268,10 +1345,10 @@ bool wxImageHandler::CanRead( const wxString& name ) //----------------------------------------------------------------------------- -// wxBitmap convertion routines +// Deprecated wxBitmap conversion routines //----------------------------------------------------------------------------- -#if wxUSE_GUI +#if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI #ifdef __WXGTK__ wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue ) const @@ -1293,24 +1370,7 @@ wxImage::wxImage( const wxBitmap &bitmap ) *this = bitmap.ConvertToImage(); } -#endif - - - -// A module to allow wxImage initialization/cleanup -// without calling these functions from app.cpp or from -// the user's application. - -class wxImageModule: public wxModule -{ -DECLARE_DYNAMIC_CLASS(wxImageModule) -public: - wxImageModule() {} - bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; }; - void OnExit() { wxImage::CleanUpHandlers(); }; -}; - -IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) +#endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI //----------------------------------------------------------------------------- @@ -1637,4 +1697,24 @@ wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool i return rotated; } + + + + +// A module to allow wxImage initialization/cleanup +// without calling these functions from app.cpp or from +// the user's application. + +class wxImageModule: public wxModule +{ +DECLARE_DYNAMIC_CLASS(wxImageModule) +public: + wxImageModule() {} + bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; }; + void OnExit() { wxImage::CleanUpHandlers(); }; +}; + +IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) + + #endif // wxUSE_IMAGE