#endif // wxUSE_STREAMS
wxImage::wxImage( const wxImage& image )
+ : wxObject()
{
Ref(image);
}
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 );
}
}
#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,
#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
- if ( !HasOption(wxIMAGE_OPTION_FILENAME) )
- ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
+ ((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);
bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const
{
#if wxUSE_STREAMS
- if ( !HasOption(wxIMAGE_OPTION_FILENAME) )
- ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
+ ((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);
{
#if wxUSE_STREAMS
wxFileInputStream stream(name);
- return GetImageCount(stream, type);
+ if (!stream.Ok())
+ return 0;
+ else
+ return GetImageCount(stream, type);
#else
return 0;
#endif
bool wxImage::CanRead( wxInputStream &stream )
{
- wxList &list=GetHandlers();
+ const wxList& list = GetHandlers();
for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
{
handler = FindHandler(type);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %d defined."), type );
wxImageHandler *handler = FindHandlerMime(mimetype);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
wxImageHandler *handler = FindHandler(type);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %d defined."), type );
wxImageHandler *handler = FindHandlerMime(mimetype);
- if (handler == NULL)
+ if (handler == 0)
{
wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
// 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 )
// 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 )
node = node->Next();
}
- return (wxImageHandler *)NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
return handler;
node = node->Next();
}
- return (wxImageHandler*)NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandler( long bitmapType )
if (handler->GetType() == bitmapType) return handler;
node = node->Next();
}
- return NULL;
+ return 0;
}
wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
node = node->Next();
}
- return NULL;
+ return 0;
}
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()
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;
+ }
+
+ 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 FALSE;
+
+ return ok;
}
#endif // wxUSE_STREAMS
//-----------------------------------------------------------------------------
-// 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
*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
//-----------------------------------------------------------------------------
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