From deb2fec01e097102e2799d7d0797f66dbae4f0c4 Mon Sep 17 00:00:00 2001 From: Sylvain Bougnoux Date: Tue, 17 Aug 1999 09:27:44 +0000 Subject: [PATCH] Improve wxImage::LoadImage so that image format in not anymore required git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3397 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/image.h | 26 +++++++++++++------------- src/common/imagbmp.cpp | 2 +- src/common/image.cpp | 36 ++++++++++++++++++++++++++++++++---- src/common/imaggif.cpp | 6 +++--- src/common/imagjpeg.cpp | 14 +++++++++----- src/common/imagpng.cpp | 30 ++++++++++++++++++++++++++++-- 6 files changed, 86 insertions(+), 28 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index 7db3f34479..53618a1e19 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -50,8 +50,8 @@ public: wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; } #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream ); - virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); + virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); #endif inline void SetName(const wxString& name) { m_name = name; } @@ -91,8 +91,8 @@ public: }; #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream ); - virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); + virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); #endif }; #endif @@ -117,8 +117,8 @@ public: }; #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream ); - virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); + virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); #endif }; #endif @@ -142,7 +142,7 @@ public: }; #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); #endif }; @@ -165,8 +165,8 @@ public: }; #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream ); - virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE ); + virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); #endif }; @@ -184,8 +184,8 @@ public: wxImage(); wxImage( int width, int height ); - wxImage( const wxString& name, long type = wxBITMAP_TYPE_PNG ); - wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG ); + wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY ); + wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); wxImage( const wxString& name, const wxString& mimetype ); wxImage( wxInputStream& stream, const wxString& mimetype ); @@ -212,11 +212,11 @@ public: unsigned char GetGreen( int x, int y ); unsigned char GetBlue( int x, int y ); - virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_PNG ); + virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY ); virtual bool LoadFile( const wxString& name, const wxString& mimetype ); #if wxUSE_STREAMS - virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG ); + virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype ); #endif diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index 40c0f14512..db3f1e2b40 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -64,7 +64,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler) #define poffset (line * width * 3 + column * 3) -bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream ) +bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose) ) { int rshift = 0, gshift = 0, bshift = 0; wxUint8 aByte; diff --git a/src/common/image.cpp b/src/common/image.cpp index 18d73f4668..a67a7445ac 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -388,13 +388,41 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) } #if wxUSE_STREAMS +//#include + bool wxImage::LoadFile( wxInputStream& stream, long type ) { UnRef(); m_refData = new wxImageRefData; - wxImageHandler *handler = FindHandler(type); + wxImageHandler *handler; + + 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::IsAppropriate(wxString&) + // bool wxImageHandler::IsAppropriate(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(); + //cout << handler->GetExtension() << endl; + if (handler->LoadFile( this, stream, FALSE )) return TRUE; + stream.SeekI(pos); + } + + return FALSE; + } + + handler = FindHandler(type); if (handler == NULL) { @@ -538,7 +566,7 @@ wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) void wxImage::InitStandardHandlers() { - AddHandler( new wxBMPHandler ); + AddHandler( new wxBMPHandler ); } void wxImage::CleanUpHandlers() @@ -563,12 +591,12 @@ IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject) #endif #if wxUSE_STREAMS -bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream) ) +bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) { return FALSE; } -bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream) ) +bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) { return FALSE; } diff --git a/src/common/imaggif.cpp b/src/common/imaggif.cpp index 4977856f2a..37ef878478 100644 --- a/src/common/imaggif.cpp +++ b/src/common/imaggif.cpp @@ -367,7 +367,7 @@ FOLLOWING CODE IS BY V.S. : // wxGIFHandler //----------------------------------------------------------------------------- -bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream ) +bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose) ) { unsigned char *ptr, *src, *pal; IMAGEN igif; @@ -418,9 +418,9 @@ bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream ) } bool wxGIFHandler::SaveFile( wxImage * WXUNUSED(image), - wxOutputStream& WXUNUSED(stream) ) + wxOutputStream& WXUNUSED(stream), bool verbose ) { - wxLogDebug(_T("wxGIFHandler is read-only!!")); + if (verbose) wxLogDebug(_T("wxGIFHandler is read-only!!")); return FALSE; } diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp index 04de921c97..0b89c5c3ec 100644 --- a/src/common/imagjpeg.cpp +++ b/src/common/imagjpeg.cpp @@ -141,7 +141,7 @@ my_error_exit (j_common_ptr cinfo) /* Always display the message. */ /* We could postpone this until after returning, if we chose. */ - (*cinfo->err->output_message) (cinfo); + if (cinfo->err->output_message) (*cinfo->err->output_message) (cinfo); /* Return control to the setjmp point */ longjmp(myerr->setjmp_buffer, 1); @@ -149,7 +149,7 @@ my_error_exit (j_common_ptr cinfo) -bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream ) +bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose ) { struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; @@ -161,12 +161,14 @@ bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream ) cinfo.err = jpeg_std_error( &jerr.pub ); jerr.pub.error_exit = my_error_exit; + if (!verbose) cinfo.err->output_message=NULL; + /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ - wxLogError(_("Couldn't load a JPEG image - probably file is corrupted.")); + if (verbose) wxLogError(_("Couldn't load a JPEG image - probably file is corrupted.")); jpeg_destroy_decompress(&cinfo); if (image->Ok()) image->Destroy(); return FALSE; @@ -263,7 +265,7 @@ GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile) dest->stream = &outfile; } -bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream ) +bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { struct jpeg_compress_struct cinfo; struct my_error_mgr jerr; @@ -274,12 +276,14 @@ bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream ) cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; + if (!verbose) cinfo.err->output_message=NULL; + /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ - wxLogError(_("Couldn't save a JPEG image - probably file is corrupted.")); + if (verbose) wxLogError(_("Couldn't save a JPEG image - probably file is corrupted.")); jpeg_destroy_compress(&cinfo); return FALSE; } diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index 1b3a066639..1a27982b12 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -75,7 +75,28 @@ static void LINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length); } -bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream ) +// from pngerror.c +// so that the libpng doesn't send anything on stderr +void +png_silent_error(png_structp png_ptr, png_const_charp WXUNUSED(message)) +{ +#ifdef USE_FAR_KEYWORD + { + jmp_buf jmpbuf; + png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf)); + longjmp(jmpbuf, 1); + } +#else + longjmp(png_ptr->jmpbuf, 1); +#endif +} + +void +png_silent_warning(png_structp WXUNUSED(png_ptr), png_const_charp WXUNUSED(message)) +{ +} + +bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose) { // VZ: as this function uses setjmp() the only fool proof error handling // method is to use goto (setjmp is not really C++ dtors friendly...) @@ -93,6 +114,9 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream ) if (!png_ptr) goto error_nolines; + // the file example.c explain how to guess if the stream is a png image + if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning); + info_ptr = png_create_info_struct( png_ptr ); if (!info_ptr) goto error_nolines; @@ -245,7 +269,7 @@ bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream ) } -bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream ) +bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) { { png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); @@ -254,6 +278,8 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream ) return FALSE; } + if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning); + png_infop info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { -- 2.45.2