]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpnm.cpp
Fixed wxBufferedInputStream support ...
[wxWidgets.git] / src / common / imagpnm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagpnm.cpp
3 // Purpose: wxImage PNM handler
4 // Author: Sylvain Bougnoux
5 // RCS-ID: $Id$
6 // Copyright: (c) Sylvain Bougnoux
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11 We don't put pragma implement in this file because it is already present in
12 src/common/image.cpp
13 */
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/image.h"
23 #include "wx/log.h"
24 #include "wx/txtstrm.h"
25
26 #ifdef __WXMSW__
27 #include <windows.h>
28 #endif
29
30 //-----------------------------------------------------------------------------
31 // wxBMPHandler
32 //-----------------------------------------------------------------------------
33
34 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
36 #endif
37
38 #if wxUSE_STREAMS
39
40 //#include <stream.h> // for cout
41
42 void Skip_Comment(wxInputStream &stream)
43 {
44 wxString line;
45 wxTextInputStream text_stream(stream);
46
47 if (stream.Peek()==_T('#'))
48 {
49 text_stream >> line;
50 Skip_Comment(stream);
51 }
52 }
53
54 bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose) )
55 {
56 wxUint32 width, height;
57 wxUint16 maxval;
58 wxString line;
59 char c(0);
60
61 image->Destroy();
62
63 /*
64 * Read the PNM header
65 */
66
67 wxTextInputStream text_stream(stream);
68
69 Skip_Comment(stream);
70 if (stream.GetC()==_T('P')) c=stream.GetC();
71
72 switch (c)
73 {
74 case _T('2'):
75 wxLogError(_T("Loading Grey Ascii PNM image is not yet implemented."));
76 return FALSE;
77 case _T('5'):
78 wxLogError(_T("Loading Grey Raw PNM image is not yet implemented."));
79 return FALSE;
80 case _T('3'): case _T('6'): break;
81 default :
82 wxLogError(_T("Loading PNM image : file not recognized."));
83 return FALSE;
84 }
85
86 text_stream >> line; // for the \n
87 Skip_Comment(stream);
88 text_stream >> width >> height ;
89 Skip_Comment(stream);
90 text_stream >> maxval;
91
92 //cout << line << " " << width << " " << height << " " << maxval << endl;
93 image->Create( width, height );
94 unsigned char *ptr = image->GetData();
95 if (!ptr)
96 {
97 wxLogError( _T("Cannot allocate RAM for RGB data in PNM file.") );
98 return FALSE;
99 }
100
101 wxBufferedInputStream buf_stream(stream);
102
103 if (c=='3') // Ascii RBG
104 {
105 wxUint32 value, size=3*width*height;
106 for (wxUint32 i=0; i<size; ++i)
107 {
108 //this is very slow !!!
109 //I wonder how we can make any better ?
110 value=text_stream.Read32();
111 *ptr++=(unsigned char)value;
112
113 if (buf_stream.LastError()!=wxSTREAM_NOERROR)
114 {
115 wxLogError(_T("Loading PNM image : file seems truncated."));
116 return FALSE;
117 }
118 }
119 }
120 if (c=='6') // Raw RGB
121 buf_stream.Read( ptr, 3*width*height );
122
123 image->SetMask( FALSE );
124
125 return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF);
126 }
127
128 bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
129 {
130 wxTextOutputStream text_stream(stream);
131
132 //text_stream << "P6" << endl
133 //<< image->GetWidth() << " " << image->GetHeight() << endl
134 //<< "255" << endl;
135 text_stream << "P6\n" << image->GetWidth() << " " << image->GetHeight() << "\n255\n";
136 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
137
138 return (stream.LastError()==wxStream_NOERROR);
139 }
140
141 bool wxPNMHandler::CanRead( wxInputStream& stream )
142 {
143 off_t pos=stream.TellI();
144
145 Skip_Comment(stream);
146
147 if (stream.GetC()==_T('P'))
148 switch (stream.GetC())
149 {
150 case _T('3'): case _T('6'):
151 stream.SeekI(pos);
152 return TRUE;
153 }
154
155 stream.SeekI(pos);
156 return FALSE;
157 }
158
159
160 #endif // wxUSE_STREAMS
161
162