]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpnm.cpp
Tidied some docs, made VC++ 6 DLL compilation work with wxvc_dll.dsp,
[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/txtstrm.h"
31
32 //-----------------------------------------------------------------------------
33 // wxBMPHandler
34 //-----------------------------------------------------------------------------
35
36 #if !USE_SHARED_LIBRARIES
37 IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler)
38 #endif
39
40 #if wxUSE_STREAMS
41
42 void Skip_Comment(wxInputStream &stream)
43 {
44 wxTextInputStream text_stream(stream);
45
46 if (stream.Peek()==wxT('#'))
47 {
48 text_stream.ReadLine();
49 Skip_Comment(stream);
50 }
51 }
52
53 bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool WXUNUSED(verbose) )
54 {
55 wxUint32 width, height;
56 wxUint16 maxval;
57 char c(0);
58
59 image->Destroy();
60
61 /*
62 * Read the PNM header
63 */
64
65 wxBufferedInputStream buf_stream(stream);
66 wxTextInputStream text_stream(buf_stream);
67
68 Skip_Comment(buf_stream);
69 if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC();
70
71 switch (c)
72 {
73 case wxT('2'):
74 wxLogError(wxT("Loading Grey Ascii PNM image is not yet implemented."));
75 return FALSE;
76 case wxT('5'):
77 wxLogError(wxT("Loading Grey Raw PNM image is not yet implemented."));
78 return FALSE;
79 case wxT('3'): case wxT('6'): break;
80 default :
81 wxLogError(wxT("Loading PNM image : file 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 wxLogError( wxT("Cannot allocate RAM for RGB data in PNM file.") );
97 return FALSE;
98 }
99
100 if (c=='3') // Ascii RBG
101 {
102 wxUint32 value, size=3*width*height;
103 for (wxUint32 i=0; i<size; ++i)
104 {
105 //this is very slow !!!
106 //I wonder how we can make any better ?
107 value=text_stream.Read32();
108 *ptr++=(unsigned char)value;
109
110 if (buf_stream.LastError()!=wxSTREAM_NOERROR)
111 {
112 wxLogError(wxT("Loading PNM image : file seems truncated."));
113 return FALSE;
114 }
115 }
116 }
117 if (c=='6') // Raw RGB
118 buf_stream.Read( ptr, 3*width*height );
119
120 image->SetMask( FALSE );
121
122 return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF);
123 }
124
125 bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) )
126 {
127 wxTextOutputStream text_stream(stream);
128
129 //text_stream << "P6" << endl
130 //<< image->GetWidth() << " " << image->GetHeight() << endl
131 //<< "255" << endl;
132 text_stream << "P6\n" << image->GetWidth() << " " << image->GetHeight() << "\n255\n";
133 stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight());
134
135 return (stream.LastError()==wxStream_NOERROR);
136 }
137
138 bool wxPNMHandler::DoCanRead( wxInputStream& stream )
139 {
140 off_t pos = stream.TellI();
141
142 Skip_Comment(stream);
143
144 if ( stream.GetC() == 'P' )
145 {
146 switch (stream.GetC())
147 {
148 case '3':
149 case '6':
150 stream.SeekI(pos);
151 return TRUE;
152 }
153 }
154
155 stream.SeekI(pos);
156 return FALSE;
157 }
158
159
160 #endif // wxUSE_STREAMS
161
162 #endif // wxUSE_PNM