]>
Commit | Line | Data |
---|---|---|
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 | ||
b9b32d5c GRG |
22 | #ifndef WX_PRECOMP |
23 | # include "wx/setup.h" | |
24 | #endif | |
25 | ||
26 | #if wxUSE_STREAMS && wxUSE_PNM | |
27 | ||
a8d9809f SB |
28 | #include "wx/image.h" |
29 | #include "wx/log.h" | |
30 | #include "wx/txtstrm.h" | |
31 | ||
a8d9809f SB |
32 | //----------------------------------------------------------------------------- |
33 | // wxBMPHandler | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | #if !USE_SHARED_LIBRARIES | |
37 | IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler) | |
38 | #endif | |
39 | ||
a8d9809f SB |
40 | |
41 | void Skip_Comment(wxInputStream &stream) | |
42 | { | |
43 | wxString line; | |
44 | wxTextInputStream text_stream(stream); | |
45 | ||
46 | if (stream.Peek()==_T('#')) | |
47 | { | |
48 | text_stream >> line; | |
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 | wxString line; | |
58 | char c(0); | |
59 | ||
60 | image->Destroy(); | |
61 | ||
62 | /* | |
63 | * Read the PNM header | |
64 | */ | |
65 | ||
6319afe3 GL |
66 | wxBufferedInputStream buf_stream(stream); |
67 | wxTextInputStream text_stream(buf_stream); | |
a8d9809f | 68 | |
6319afe3 GL |
69 | Skip_Comment(buf_stream); |
70 | if (buf_stream.GetC()==_T('P')) c=buf_stream.GetC(); | |
a8d9809f SB |
71 | |
72 | switch (c) | |
73 | { | |
74 | case _T('2'): | |
75 | wxLogError(_T("Loading Grey Ascii PNM image is not yet implemented.")); | |
76 | return FALSE; | |
77 | case _T('5'): | |
78 | wxLogError(_T("Loading Grey Raw PNM image is not yet implemented.")); | |
79 | return FALSE; | |
80 | case _T('3'): case _T('6'): break; | |
81 | default : | |
82 | wxLogError(_T("Loading PNM image : file not recognized.")); | |
83 | return FALSE; | |
84 | } | |
85 | ||
86 | text_stream >> line; // for the \n | |
6319afe3 | 87 | Skip_Comment(buf_stream); |
a8d9809f | 88 | text_stream >> width >> height ; |
6319afe3 | 89 | Skip_Comment(buf_stream); |
a8d9809f SB |
90 | text_stream >> maxval; |
91 | ||
a2b8bd55 | 92 | //cout << line << " " << width << " " << height << " " << maxval << endl; |
a8d9809f SB |
93 | image->Create( width, height ); |
94 | unsigned char *ptr = image->GetData(); | |
95 | if (!ptr) | |
96 | { | |
97 | wxLogError( _T("Cannot allocate RAM for RGB data in PNM file.") ); | |
98 | return FALSE; | |
99 | } | |
100 | ||
a2b8bd55 | 101 | if (c=='3') // Ascii RBG |
a8d9809f SB |
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 | ||
503aa33d | 111 | if (buf_stream.LastError()!=wxSTREAM_NOERROR) |
a8d9809f SB |
112 | { |
113 | wxLogError(_T("Loading PNM image : file seems truncated.")); | |
114 | return FALSE; | |
115 | } | |
116 | } | |
117 | } | |
118 | if (c=='6') // Raw RGB | |
503aa33d | 119 | buf_stream.Read( ptr, 3*width*height ); |
a8d9809f SB |
120 | |
121 | image->SetMask( FALSE ); | |
122 | ||
503aa33d | 123 | return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF); |
a8d9809f SB |
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 | ||
0828c087 VS |
139 | bool wxPNMHandler::CanRead( wxInputStream& stream ) |
140 | { | |
93dfff5a SB |
141 | off_t pos=stream.TellI(); |
142 | ||
143 | Skip_Comment(stream); | |
144 | ||
145 | if (stream.GetC()==_T('P')) | |
146 | switch (stream.GetC()) | |
147 | { | |
148 | case _T('3'): case _T('6'): | |
149 | stream.SeekI(pos); | |
150 | return TRUE; | |
151 | } | |
152 | ||
153 | stream.SeekI(pos); | |
154 | return FALSE; | |
0828c087 VS |
155 | } |
156 | ||
157 | ||
b9b32d5c | 158 | #endif // wxUSE_STREAMS && wxUSE_PNM |
a8d9809f | 159 |