From 6319afe332dd1b386cf05902af86d3b5df6692f7 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sun, 29 Aug 1999 19:48:03 +0000 Subject: [PATCH] Removed LastError() from wxFilterStream Added a better Peek() to wxBufferedInputStream Fixed wxPNMHandler (it is a lot faster on text PNM now) Image sample loads a PNM git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@3525 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/stream.h | 4 ++-- samples/image/image.cpp | 13 ++++++++++++- src/common/imagpnm.cpp | 13 ++++++------- src/common/stream.cpp | 24 +++++++++++++++++++++++- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/include/wx/stream.h b/include/wx/stream.h index 44660c9e90..efef1ec113 100644 --- a/include/wx/stream.h +++ b/include/wx/stream.h @@ -170,7 +170,6 @@ class WXDLLEXPORT wxFilterInputStream: public wxInputStream { char Peek() { return m_parent_i_stream->Peek(); } - wxStreamError LastError() const { return m_parent_i_stream->LastError(); } size_t GetSize() const { return m_parent_i_stream->GetSize(); } protected: @@ -183,7 +182,6 @@ class WXDLLEXPORT wxFilterOutputStream: public wxOutputStream { wxFilterOutputStream(wxOutputStream& stream); ~wxFilterOutputStream(); - wxStreamError LastError() const { return m_parent_o_stream->LastError(); } size_t GetSize() const { return m_parent_o_stream->GetSize(); } protected: @@ -216,6 +214,7 @@ class WXDLLEXPORT wxStreamBuffer { size_t Write(const void *buffer, size_t size); size_t Write(wxStreamBuffer *buf); + char Peek(); char GetChar(); void PutChar(char c); off_t Tell() const; @@ -273,6 +272,7 @@ class wxBufferedInputStream: public wxFilterInputStream { wxBufferedInputStream(wxInputStream& stream); ~wxBufferedInputStream(); + char Peek(); wxInputStream& Read(void *buffer, size_t size); // Position functions diff --git a/samples/image/image.cpp b/samples/image/image.cpp index d0d48d451d..fe40a70b0b 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -43,6 +43,7 @@ public: wxBitmap *my_horse_gif; wxBitmap *my_horse_bmp; wxBitmap *my_horse_pcx; + wxBitmap *my_horse_pnm; wxBitmap *my_square; wxBitmap *my_anti; @@ -147,6 +148,11 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, wxLogError("Can't load BMP image"); else my_horse_bmp = new wxBitmap( image.ConvertToBitmap() ); + + if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) ) + wxLogError("Can't load PNM image"); + else + my_horse_pnm = new wxBitmap( image.ConvertToBitmap() ); image.LoadFile( dir + wxString("test.png") ); my_square = new wxBitmap( image.ConvertToBitmap() ); @@ -156,6 +162,7 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, MyCanvas::~MyCanvas() { + delete my_horse_pnm; delete my_horse_png; delete my_horse_jpeg; delete my_horse_gif; @@ -194,6 +201,9 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) dc.DrawText( "BMP handler", 30, 1055 ); if (my_horse_bmp && my_horse_bmp->Ok()) dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); + + dc.DrawText( "PNM handler", 30, 1285 ); + if (my_horse_pnm && my_horse_pnm->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); } void MyCanvas::CreateAntiAliasedBitmap() @@ -277,7 +287,7 @@ MyFrame::MyFrame() m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); // 500 width * 1300 height - m_canvas->SetScrollbars( 10, 10, 50, 130 ); + m_canvas->SetScrollbars( 10, 10, 50, 152 ); } void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) @@ -309,6 +319,7 @@ bool MyApp::OnInit() wxImage::AddHandler( new wxGIFHandler ); wxImage::AddHandler( new wxPCXHandler ); + wxImage::AddHandler( new wxPNMHandler ); wxFrame *frame = new MyFrame(); frame->Show( TRUE ); diff --git a/src/common/imagpnm.cpp b/src/common/imagpnm.cpp index e7de922c9c..5352da9f4f 100644 --- a/src/common/imagpnm.cpp +++ b/src/common/imagpnm.cpp @@ -64,10 +64,11 @@ bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSE * Read the PNM header */ - wxTextInputStream text_stream(stream); + wxBufferedInputStream buf_stream(stream); + wxTextInputStream text_stream(buf_stream); - Skip_Comment(stream); - if (stream.GetC()==_T('P')) c=stream.GetC(); + Skip_Comment(buf_stream); + if (buf_stream.GetC()==_T('P')) c=buf_stream.GetC(); switch (c) { @@ -84,9 +85,9 @@ bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSE } text_stream >> line; // for the \n - Skip_Comment(stream); + Skip_Comment(buf_stream); text_stream >> width >> height ; - Skip_Comment(stream); + Skip_Comment(buf_stream); text_stream >> maxval; //cout << line << " " << width << " " << height << " " << maxval << endl; @@ -98,8 +99,6 @@ bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSE return FALSE; } - wxBufferedInputStream buf_stream(stream); - if (c=='3') // Ascii RBG { wxUint32 value, size=3*width*height; diff --git a/src/common/stream.cpp b/src/common/stream.cpp index fdfc912333..5c7ba37d3f 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -195,6 +195,23 @@ void wxStreamBuffer::PutChar(char c) m_stream->m_lastcount = 1; } +char wxStreamBuffer::Peek() +{ + char c; + + wxASSERT(m_stream != NULL && m_buffer_size != 0); + + if (!GetDataLeft()) { + CHECK_ERROR(wxStream_READ_ERR); + return 0; + } + + GetFromBuffer(&c, 1); + m_buffer_pos--; + + return c; +} + char wxStreamBuffer::GetChar() { char c; @@ -248,7 +265,7 @@ size_t wxStreamBuffer::Read(void *buffer, size_t size) buffer = (char *)buffer + buf_left; // ANSI C++ violation. if (!FillBuffer()) { - CHECK_ERROR(wxStream_READ_ERR); + CHECK_ERROR(wxStream_EOF); return (m_stream->m_lastcount = orig_size-size); } } else { @@ -743,6 +760,11 @@ wxBufferedInputStream::~wxBufferedInputStream() delete m_i_streambuf; } +char wxBufferedInputStream::Peek() +{ + return m_i_streambuf->Peek(); +} + wxInputStream& wxBufferedInputStream::Read(void *buffer, size_t size) { size_t retsize; -- 2.45.2