From: Václav Slavík Date: Wed, 18 Aug 1999 21:22:54 +0000 (+0000) Subject: added wxImage::CanRead X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0828c0875e1516e4bfc0aa5b0a84195a2ac92cca added wxImage::CanRead git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3421 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/image.h b/include/wx/image.h index 4a42b42635..3b351600e9 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -52,6 +52,9 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + + virtual bool CanRead( wxInputStream& stream ); + virtual bool CanRead( const wxString& name ); #endif inline void SetName(const wxString& name) { m_name = name; } @@ -93,6 +96,7 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + virtual bool CanRead( wxInputStream& stream ); #endif }; #endif @@ -119,6 +123,7 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + virtual bool CanRead( wxInputStream& stream ); #endif }; #endif @@ -143,6 +148,7 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); + virtual bool CanRead( wxInputStream& stream ); #endif }; @@ -167,6 +173,7 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + virtual bool CanRead( wxInputStream& stream ); #endif }; @@ -191,6 +198,7 @@ public: #if wxUSE_STREAMS virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + virtual bool CanRead( wxInputStream& stream ); #endif }; diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 30bb7eb986..24306d42a6 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -126,22 +126,22 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, else my_horse_png = new wxBitmap( image.ConvertToBitmap() ); - if ( !image.LoadFile( dir + wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ) ) + if ( !image.LoadFile( dir + wxString("horse.jpg") ) ) wxLogError("Can't load JPG image"); else my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() ); - if ( !image.LoadFile( dir + wxString("horse.gif"), wxBITMAP_TYPE_GIF ) ) + if ( !image.LoadFile( dir + wxString("horse.gif") ) ) wxLogError("Can't load GIF image"); else my_horse_gif = new wxBitmap( image.ConvertToBitmap() ); - if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) ) + if ( !image.LoadFile( dir + wxString("horse.bmp") ) ) wxLogError("Can't load BMP image"); else my_horse_bmp = new wxBitmap( image.ConvertToBitmap() ); - image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ); + image.LoadFile( dir + wxString("test.png") ); my_square = new wxBitmap( image.ConvertToBitmap() ); CreateAntiAliasedBitmap(); diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index db3f1e2b40..005e007dfa 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -401,6 +401,15 @@ bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSE return TRUE; } +bool wxBMPHandler::CanRead( wxInputStream& stream ) +{ + unsigned char hdr[2]; + + stream.Read(&hdr, 2); + stream.SeekI(-2, wxFromCurrent); + return (hdr[0] == 'B' && hdr[1] == 'M'); +} + #endif // wxUSE_STREAMS diff --git a/src/common/image.cpp b/src/common/image.cpp index 2ee0a4c1bb..7478392fe2 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -436,23 +436,14 @@ bool wxImage::LoadFile( wxInputStream& stream, long type ) if (type==wxBITMAP_TYPE_ANY) { - // here we can try to guess the handler according the extension, - // but we lose the stream name !? - // Probably we should write methods such as - // bool wxImageHandler::CanRead(wxString&) - // bool wxImageHandler::CanRead(sxInputStream&&) - // for png : see example.c wxList &list=GetHandlers(); - off_t pos=stream.TellI(); - - wxLogNull prevent_log; for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() ) { handler=(wxImageHandler*)node->GetData(); - if (handler->LoadFile( this, stream, FALSE )) return TRUE; + if (handler->CanRead( stream )) + return handler->LoadFile( this, stream ); - stream.SeekI(pos); } wxLogWarning( _T("No handler found for this image.") ); @@ -637,6 +628,33 @@ bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSE { return FALSE; } + +bool wxImageHandler::CanRead( wxInputStream& stream ) +{ + return FALSE; +} + +bool wxImageHandler::CanRead( const wxString& name ) +{ +#if wxUSE_STREAMS + if (wxFileExists(name)) + { + wxFileInputStream stream(name); + return CanRead(stream); + } + + else { + wxLogError( _T("Can't check image format of file '%s': file does not exist."), name.c_str() ); + + return FALSE; + } +#else // !wxUSE_STREAMS + return FALSE; +#endif // wxUSE_STREAMS +} + + + #endif // wxUSE_STREAMS //----------------------------------------------------------------------------- diff --git a/src/common/imaggif.cpp b/src/common/imaggif.cpp index 785f91da54..c6a775e15c 100644 --- a/src/common/imaggif.cpp +++ b/src/common/imaggif.cpp @@ -66,4 +66,13 @@ bool wxGIFHandler::SaveFile( wxImage * WXUNUSED(image), return FALSE; } +bool wxGIFHandler::CanRead( wxInputStream& stream ) +{ + unsigned char hdr[5]; + + stream.Read(&hdr, 5); + stream.SeekI(-5, wxFromCurrent); + return (hdr[0] == 'G' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == '8' && hdr[4] == '9'); +} + #endif diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp index 0b89c5c3ec..94bded7c01 100644 --- a/src/common/imagjpeg.cpp +++ b/src/common/imagjpeg.cpp @@ -310,6 +310,16 @@ bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbo return TRUE; } + +bool wxJPEGHandler::CanRead( wxInputStream& stream ) +{ + unsigned char hdr[2]; + + stream.Read(&hdr, 2); + stream.SeekI(-2, wxFromCurrent); + return (hdr[0] == 0xFF && hdr[1] == 0xD8); +} + #endif // wxUSE_STREAMS diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index 1a27982b12..7609aae068 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -346,6 +346,15 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbos return TRUE; } +bool wxPNGHandler::CanRead( wxInputStream& stream ) +{ + unsigned char hdr[4]; + + stream.Read(&hdr, 4); + stream.SeekI(-4, wxFromCurrent); + return (hdr[0] == 0x89 && hdr[1] == 'P' && hdr[2] == 'N' && hdr[3] == 'G'); +} + #endif // wxUSE_STREAMS diff --git a/src/common/imagpnm.cpp b/src/common/imagpnm.cpp index ffbd4c1bd4..d2b3244965 100644 --- a/src/common/imagpnm.cpp +++ b/src/common/imagpnm.cpp @@ -136,6 +136,12 @@ bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUS return (stream.LastError()==wxStream_NOERROR); } +bool wxPNMHandler::CanRead( wxInputStream& stream ) +{ + return FALSE; // VS - temporary - FIXME +} + + #endif // wxUSE_STREAMS