removed USE_SHARED_LIBRARY(IES)
[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 #ifndef WX_PRECOMP
23 # include "wx/setup.h"
24 #endif
25
26 #if wxUSE_PNM
27
28 #include "wx/image.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #include "wx/txtstrm.h"
32
33 //-----------------------------------------------------------------------------
34 // wxBMPHandler
35 //-----------------------------------------------------------------------------
36
37 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
38
39 #if wxUSE_STREAMS
40
41 void Skip_Comment(wxInputStream &stream)
42 {
43 wxTextInputStream text_stream(stream);
44
45 if (stream.Peek()==wxT('#'))
46 {
47 text_stream.ReadLine();
48 Skip_Comment(stream);
49 }
50 }
51
52 bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
53 {
54 wxUint32 width, height;
55 wxUint16 maxval;
56 char c(0);
57
58 image->Destroy();
59
60 /*
61 * Read the PNM header
62 */
63
64 wxBufferedInputStream buf_stream(stream);
65 wxTextInputStream text_stream(buf_stream);
66
67 Skip_Comment(buf_stream);
68 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
69
70 switch (c)
71 {
72 case wxT('2'):
73 if (verbose) wxLogError(_("Loading Grey Ascii PNM image is not yet implemented."));
74 return FALSE;
75 case wxT('5'):
76 if (verbose) wxLogError(_("Loading Grey Raw PNM image is not yet implemented."));
77 return FALSE;
78 case wxT('3'):
79 case wxT('6'): break;
80 default:
81 if (verbose) wxLogError(_("PNM: File format is not recognized."));
82 return FALSE;
83 }
84
85 text_stream.ReadLine(); // for the \n
86 Skip_Comment(buf_stream);
87 text_stream >> width >> height ;
88 Skip_Comment(buf_stream);
89 text_stream >> maxval;
90
91 //cout << line << " " << width << " " << height << " " << maxval << endl;
92 image->Create( width, height );
93 unsigned char *ptr = image->GetData();
94 if (!ptr)
95 {
96 if (verbose)
97 wxLogError( _("PNM: Couldn't allocate memory.") );
98 return FALSE;
99 }
100
101 if (c=='3') // Ascii RBG
102 {
103 wxUint32 value, size=3*width*height;
104 for (wxUint32 i=0; i<size; ++i)
105 {
106 //this is very slow !!!
107 //I wonder how we can make any better ?
108 value=text_stream.Read32();
109 *ptr++=(unsigned char)value;
110
111 if (buf_stream.LastError()!=wxSTREAM_NOERROR)
112 {
113 if (verbose) wxLogError(_("PNM: File seems truncated."));
114 return FALSE;
115 }
116 }
117 }
118 if (c=='6') // Raw RGB
119 buf_stream.Read( ptr, 3*width*height );
120
121 image->SetMask( FALSE );
122
123 return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF);
124 }
125
126 bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
127 {
128 wxTextOutputStream text_stream(stream);
129
130 //text_stream << "P6" << endl
131 //<< image->GetWidth() << " " << image->GetHeight() << endl
132 //<< "255" << endl;
133 text_stream << "P6\n" << image->GetWidth() << " " << image->GetHeight() << "\n255\n";
134 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
135
136 return (stream.LastError()==wxStream_NOERROR);
137 }
138
139 bool wxPNMHandler::DoCanRead( wxInputStream& stream )
140 {
141 off_t pos = stream.TellI();
142
143 Skip_Comment(stream);
144
145 if ( stream.GetC() == 'P' )
146 {
147 switch (stream.GetC())
148 {
149 case '3':
150 case '6':
151 stream.SeekI(pos);
152 return TRUE;
153 }
154 }
155
156 stream.SeekI(pos);
157 return FALSE;
158 }
159
160
161 #endif // wxUSE_STREAMS
162
163 #endif // wxUSE_PNM