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