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