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