]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpnm.cpp
corrected error code for http responses with no content-length indicated (from READ_E...
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imagpnm.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 # include "wx/setup.h"
23 #endif
24
25 #if wxUSE_IMAGE && wxUSE_PNM
26
27 #include "wx/imagpnm.h"
28 #include "wx/log.h"
29 #include "wx/intl.h"
30 #include "wx/txtstrm.h"
31
32 //-----------------------------------------------------------------------------
33 // wxBMPHandler
34 //-----------------------------------------------------------------------------
35
36 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
37
38 #if wxUSE_STREAMS
39
40 void Skip_Comment(wxInputStream &stream)
41 {
42 wxTextInputStream text_stream(stream);
43
44 if (stream.Peek()==wxT('#'))
45 {
46 text_stream.ReadLine();
47 Skip_Comment(stream);
48 }
49 }
50
51 bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
52 {
53 wxUint32 width, height;
54 wxUint16 maxval;
55 char c(0);
56
57 image->Destroy();
58
59 /*
60 * Read the PNM header
61 */
62
63 wxBufferedInputStream buf_stream(stream);
64 wxTextInputStream text_stream(buf_stream);
65
66 Skip_Comment(buf_stream);
67 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
68
69 switch (c)
70 {
71 case wxT('2'): // ASCII Grey
72 case wxT('3'): // ASCII RGB
73 case wxT('5'): // RAW Grey
74 case wxT('6'): break;
75 default:
76 if (verbose) wxLogError(_("PNM: File format is not recognized."));
77 return false;
78 }
79
80 text_stream.ReadLine(); // for the \n
81 Skip_Comment(buf_stream);
82 text_stream >> width >> height ;
83 Skip_Comment(buf_stream);
84 text_stream >> maxval;
85
86 //cout << line << " " << width << " " << height << " " << maxval << endl;
87 image->Create( width, height );
88 unsigned char *ptr = image->GetData();
89 if (!ptr)
90 {
91 if (verbose)
92 wxLogError( _("PNM: Couldn't allocate memory.") );
93 return false;
94 }
95
96
97 if (c=='2') // Ascii GREY
98 {
99 wxUint32 value, size=width*height;
100 for (wxUint32 i=0; i<size; ++i)
101 {
102 value=text_stream.Read32();
103 *ptr++=(unsigned char)value; // R
104 *ptr++=(unsigned char)value; // G
105 *ptr++=(unsigned char)value; // B
106 if ( !buf_stream )
107 {
108 if (verbose) wxLogError(_("PNM: File seems truncated."));
109 return false;
110 }
111 }
112 }
113 if (c=='3') // Ascii RBG
114 {
115 wxUint32 value, size=3*width*height;
116 for (wxUint32 i=0; i<size; ++i)
117 {
118 //this is very slow !!!
119 //I wonder how we can make any better ?
120 value=text_stream.Read32();
121 *ptr++=(unsigned char)value;
122
123 if ( !buf_stream )
124 {
125 if (verbose) wxLogError(_("PNM: File seems truncated."));
126 return false;
127 }
128 }
129 }
130 if (c=='5') // Raw GREY
131 {
132 wxUint32 size=width*height;
133 unsigned char value;
134 for (wxUint32 i=0; i<size; ++i)
135 {
136 buf_stream.Read(&value,1);
137 *ptr++=value; // R
138 *ptr++=value; // G
139 *ptr++=value; // B
140 if ( !buf_stream )
141 {
142 if (verbose) wxLogError(_("PNM: File seems truncated."));
143 return false;
144 }
145 }
146 }
147 if (c=='6') // Raw RGB
148 buf_stream.Read( ptr, 3*width*height );
149
150 image->SetMask( false );
151
152 const wxStreamError err = buf_stream.GetLastError();
153 return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF;
154 }
155
156 bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
157 {
158 wxTextOutputStream text_stream(stream);
159
160 //text_stream << "P6" << endl
161 //<< image->GetWidth() << " " << image->GetHeight() << endl
162 //<< "255" << endl;
163 text_stream << wxT("P6\n") << image->GetWidth() << wxT(" ") << image->GetHeight() << wxT("\n255\n");
164 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
165
166 return stream.IsOk();
167 }
168
169 bool wxPNMHandler::DoCanRead( wxInputStream& stream )
170 {
171 Skip_Comment(stream);
172
173 if ( stream.GetC() == 'P' )
174 {
175 switch (stream.GetC())
176 {
177 case '3':
178 case '6':
179 return true;
180 }
181 }
182
183 return false;
184 }
185
186
187 #endif // wxUSE_STREAMS
188
189 #endif // wxUSE_PNM