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
This commit is contained in:
Sylvain Bougnoux
1999-08-17 09:27:44 +00:00
parent 40d4abbc70
commit deb2fec01e
6 changed files with 86 additions and 28 deletions

View File

@@ -50,8 +50,8 @@ public:
wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; } wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; }
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
#endif #endif
inline void SetName(const wxString& name) { m_name = name; } inline void SetName(const wxString& name) { m_name = name; }
@@ -91,8 +91,8 @@ public:
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
#endif #endif
}; };
#endif #endif
@@ -117,8 +117,8 @@ public:
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
#endif #endif
}; };
#endif #endif
@@ -142,7 +142,7 @@ public:
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
#endif #endif
}; };
@@ -165,8 +165,8 @@ public:
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE );
virtual bool SaveFile( wxImage *image, wxOutputStream& stream ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
#endif #endif
}; };
@@ -184,8 +184,8 @@ public:
wxImage(); wxImage();
wxImage( int width, int height ); wxImage( int width, int height );
wxImage( const wxString& name, long type = wxBITMAP_TYPE_PNG ); wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY );
wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG ); wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
wxImage( const wxString& name, const wxString& mimetype ); wxImage( const wxString& name, const wxString& mimetype );
wxImage( wxInputStream& stream, const wxString& mimetype ); wxImage( wxInputStream& stream, const wxString& mimetype );
@@ -212,11 +212,11 @@ public:
unsigned char GetGreen( int x, int y ); unsigned char GetGreen( int x, int y );
unsigned char GetBlue( 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 ); virtual bool LoadFile( const wxString& name, const wxString& mimetype );
#if wxUSE_STREAMS #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 ); virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype );
#endif #endif

View File

@@ -64,7 +64,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
#define poffset (line * width * 3 + column * 3) #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; int rshift = 0, gshift = 0, bshift = 0;
wxUint8 aByte; wxUint8 aByte;

View File

@@ -388,13 +388,41 @@ bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
} }
#if wxUSE_STREAMS #if wxUSE_STREAMS
//#include <stream.h>
bool wxImage::LoadFile( wxInputStream& stream, long type ) bool wxImage::LoadFile( wxInputStream& stream, long type )
{ {
UnRef(); UnRef();
m_refData = new wxImageRefData; 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) if (handler == NULL)
{ {
@@ -538,7 +566,7 @@ wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
void wxImage::InitStandardHandlers() void wxImage::InitStandardHandlers()
{ {
AddHandler( new wxBMPHandler ); AddHandler( new wxBMPHandler );
} }
void wxImage::CleanUpHandlers() void wxImage::CleanUpHandlers()
@@ -563,12 +591,12 @@ IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject)
#endif #endif
#if wxUSE_STREAMS #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; 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; return FALSE;
} }

View File

@@ -367,7 +367,7 @@ FOLLOWING CODE IS BY V.S. :
// wxGIFHandler // wxGIFHandler
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream ) bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose) )
{ {
unsigned char *ptr, *src, *pal; unsigned char *ptr, *src, *pal;
IMAGEN igif; IMAGEN igif;
@@ -418,9 +418,9 @@ bool wxGIFHandler::LoadFile( wxImage *image, wxInputStream& stream )
} }
bool wxGIFHandler::SaveFile( wxImage * WXUNUSED(image), 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; return FALSE;
} }

View File

@@ -141,7 +141,7 @@ my_error_exit (j_common_ptr cinfo)
/* Always display the message. */ /* Always display the message. */
/* We could postpone this until after returning, if we chose. */ /* 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 */ /* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1); 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 jpeg_decompress_struct cinfo;
struct my_error_mgr jerr; struct my_error_mgr jerr;
@@ -161,12 +161,14 @@ bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream )
cinfo.err = jpeg_std_error( &jerr.pub ); cinfo.err = jpeg_std_error( &jerr.pub );
jerr.pub.error_exit = my_error_exit; 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. */ /* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer)) { if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. /* 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. * 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); jpeg_destroy_decompress(&cinfo);
if (image->Ok()) image->Destroy(); if (image->Ok()) image->Destroy();
return FALSE; return FALSE;
@@ -263,7 +265,7 @@ GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo, wxOutputStream& outfile)
dest->stream = &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 jpeg_compress_struct cinfo;
struct my_error_mgr jerr; struct my_error_mgr jerr;
@@ -274,12 +276,14 @@ bool wxJPEGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
cinfo.err = jpeg_std_error(&jerr.pub); cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit; 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. */ /* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer)) { if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. /* 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. * 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); jpeg_destroy_compress(&cinfo);
return FALSE; return FALSE;
} }

View File

@@ -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); ((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 // VZ: as this function uses setjmp() the only fool proof error handling
// method is to use goto (setjmp is not really C++ dtors friendly...) // 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) if (!png_ptr)
goto error_nolines; 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 ); info_ptr = png_create_info_struct( png_ptr );
if (!info_ptr) if (!info_ptr)
goto error_nolines; 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); 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; 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); png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) if (info_ptr == NULL)
{ {