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