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