]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpnm.cpp
Added wxVector::swap().
[wxWidgets.git] / src / common / imagpnm.cpp
CommitLineData
a8d9809f 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/common/imagpnm.cpp
a8d9809f
SB
3// Purpose: wxImage PNM handler
4// Author: Sylvain Bougnoux
5// RCS-ID: $Id$
6// Copyright: (c) Sylvain Bougnoux
65571936 7// Licence: wxWindows licence
a8d9809f
SB
8/////////////////////////////////////////////////////////////////////////////
9
a8d9809f
SB
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
88a7a4e1 14 #pragma hdrstop
b9b32d5c
GRG
15#endif
16
c96ea657 17#if wxUSE_IMAGE && wxUSE_PNM
b9b32d5c 18
8f493002 19#include "wx/imagpnm.h"
88a7a4e1
WS
20
21#ifndef WX_PRECOMP
22 #include "wx/intl.h"
e4db172a 23 #include "wx/log.h"
88a7a4e1
WS
24#endif
25
a8d9809f
SB
26#include "wx/txtstrm.h"
27
a8d9809f
SB
28//-----------------------------------------------------------------------------
29// wxBMPHandler
30//-----------------------------------------------------------------------------
31
a8d9809f 32IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
a8d9809f 33
9ab6ee85 34#if wxUSE_STREAMS
a8d9809f
SB
35
36void Skip_Comment(wxInputStream &stream)
37{
2b5f62a0 38 wxTextInputStream text_stream(stream);
a8d9809f 39
2b5f62a0 40 if (stream.Peek()==wxT('#'))
a8d9809f 41 {
2b5f62a0
VZ
42 text_stream.ReadLine();
43 Skip_Comment(stream);
a8d9809f
SB
44 }
45}
46
58c837a4 47bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
a8d9809f
SB
48{
49 wxUint32 width, height;
50 wxUint16 maxval;
a8d9809f 51 char c(0);
995612e2 52
a8d9809f
SB
53 image->Destroy();
54
55 /*
56 * Read the PNM header
57 */
58
6319afe3
GL
59 wxBufferedInputStream buf_stream(stream);
60 wxTextInputStream text_stream(buf_stream);
a8d9809f 61
6319afe3 62 Skip_Comment(buf_stream);
223d09f6 63 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
a8d9809f
SB
64
65 switch (c)
58c837a4 66 {
7448de8d 67 case wxT('2'): // ASCII Grey
3e8711ce
JS
68 case wxT('3'): // ASCII RGB
69 case wxT('5'): // RAW Grey
7beb59f3 70 case wxT('6'): break;
58c837a4 71 default:
af588446
VZ
72 if (verbose)
73 {
74 wxLogError(_("PNM: File format is not recognized."));
75 }
7beb59f3 76 return false;
58c837a4 77 }
a8d9809f 78
41d1f82d 79 text_stream.ReadLine(); // for the \n
6319afe3 80 Skip_Comment(buf_stream);
a8d9809f 81 text_stream >> width >> height ;
995612e2 82 Skip_Comment(buf_stream);
a8d9809f
SB
83 text_stream >> maxval;
84
a2b8bd55 85 //cout << line << " " << width << " " << height << " " << maxval << endl;
a8d9809f
SB
86 image->Create( width, height );
87 unsigned char *ptr = image->GetData();
88 if (!ptr)
89 {
58c837a4 90 if (verbose)
af588446 91 {
58c837a4 92 wxLogError( _("PNM: Couldn't allocate memory.") );
af588446 93 }
7beb59f3 94 return false;
a8d9809f
SB
95 }
96
3e8711ce 97
7448de8d
WS
98 if (c=='2') // Ascii GREY
99 {
3e8711ce
JS
100 wxUint32 value, size=width*height;
101 for (wxUint32 i=0; i<size; ++i)
102 {
103 value=text_stream.Read32();
57beadd2
VZ
104 if ( maxval != 255 )
105 value = (255 * value)/maxval;
3e8711ce 106 *ptr++=(unsigned char)value; // R
7448de8d 107 *ptr++=(unsigned char)value; // G
3e8711ce
JS
108 *ptr++=(unsigned char)value; // B
109 if ( !buf_stream )
110 {
af588446
VZ
111 if (verbose)
112 {
113 wxLogError(_("PNM: File seems truncated."));
114 }
3e8711ce
JS
115 return false;
116 }
117 }
118 }
7448de8d
WS
119 if (c=='3') // Ascii RBG
120 {
995612e2
VZ
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();
57beadd2
VZ
127 if ( maxval != 255 )
128 value = (255 * value)/maxval;
995612e2
VZ
129 *ptr++=(unsigned char)value;
130
2b5f62a0 131 if ( !buf_stream )
995612e2 132 {
af588446
VZ
133 if (verbose)
134 {
135 wxLogError(_("PNM: File seems truncated."));
136 }
7beb59f3 137 return false;
995612e2
VZ
138 }
139 }
7448de8d
WS
140 }
141 if (c=='5') // Raw GREY
142 {
3e8711ce
JS
143 wxUint32 size=width*height;
144 unsigned char value;
145 for (wxUint32 i=0; i<size; ++i)
146 {
147 buf_stream.Read(&value,1);
57beadd2
VZ
148 if ( maxval != 255 )
149 value = (255 * value)/maxval;
3e8711ce
JS
150 *ptr++=value; // R
151 *ptr++=value; // G
7448de8d 152 *ptr++=value; // B
3e8711ce
JS
153 if ( !buf_stream )
154 {
af588446
VZ
155 if (verbose)
156 {
157 wxLogError(_("PNM: File seems truncated."));
158 }
3e8711ce
JS
159 return false;
160 }
161 }
162 }
57beadd2
VZ
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 }
a8d9809f 173
7beb59f3 174 image->SetMask( false );
a8d9809f 175
2b5f62a0
VZ
176 const wxStreamError err = buf_stream.GetLastError();
177 return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF;
a8d9809f
SB
178}
179
180bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
181{
182 wxTextOutputStream text_stream(stream);
995612e2
VZ
183
184 //text_stream << "P6" << endl
185 //<< image->GetWidth() << " " << image->GetHeight() << endl
a8d9809f 186 //<< "255" << endl;
2b5f62a0 187 text_stream << wxT("P6\n") << image->GetWidth() << wxT(" ") << image->GetHeight() << wxT("\n255\n");
a8d9809f
SB
188 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
189
2b5f62a0 190 return stream.IsOk();
a8d9809f
SB
191}
192
995612e2 193bool wxPNMHandler::DoCanRead( wxInputStream& stream )
0828c087 194{
93dfff5a
SB
195 Skip_Comment(stream);
196
8faef7cc 197 // it's ok to modify the stream position here
995612e2
VZ
198 if ( stream.GetC() == 'P' )
199 {
cdf0ff0f 200 switch ( stream.GetC() )
995612e2 201 {
cdf0ff0f
VZ
202 case '2': // ASCII Grey
203 case '3': // ASCII RGB
204 case '5': // RAW Grey
205 case '6': // RAW RGB
7beb59f3 206 return true;
995612e2
VZ
207 }
208 }
93dfff5a 209
7beb59f3 210 return false;
0828c087
VS
211}
212
213
9ab6ee85 214#endif // wxUSE_STREAMS
a8d9809f 215
e4db172a 216#endif // wxUSE_IMAGE && wxUSE_PNM