]>
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 | ||
9ab6ee85 | 26 | #if wxUSE_PNM |
b9b32d5c | 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 | ||
9ab6ee85 | 40 | #if wxUSE_STREAMS |
a8d9809f SB |
41 | |
42 | void Skip_Comment(wxInputStream &stream) | |
43 | { | |
a8d9809f SB |
44 | wxTextInputStream text_stream(stream); |
45 | ||
995612e2 | 46 | if (stream.Peek()==wxT('#')) |
a8d9809f | 47 | { |
41d1f82d | 48 | text_stream.ReadLine(); |
a8d9809f SB |
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; | |
a8d9809f | 57 | char c(0); |
995612e2 | 58 | |
a8d9809f SB |
59 | image->Destroy(); |
60 | ||
61 | /* | |
62 | * Read the PNM header | |
63 | */ | |
64 | ||
6319afe3 GL |
65 | wxBufferedInputStream buf_stream(stream); |
66 | wxTextInputStream text_stream(buf_stream); | |
a8d9809f | 67 | |
6319afe3 | 68 | Skip_Comment(buf_stream); |
223d09f6 | 69 | if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC(); |
a8d9809f SB |
70 | |
71 | switch (c) | |
72 | { | |
223d09f6 | 73 | case wxT('2'): |
995612e2 VZ |
74 | wxLogError(wxT("Loading Grey Ascii PNM image is not yet implemented.")); |
75 | return FALSE; | |
223d09f6 | 76 | case wxT('5'): |
995612e2 VZ |
77 | wxLogError(wxT("Loading Grey Raw PNM image is not yet implemented.")); |
78 | return FALSE; | |
223d09f6 | 79 | case wxT('3'): case wxT('6'): break; |
995612e2 VZ |
80 | default : |
81 | wxLogError(wxT("Loading PNM image : file not recognized.")); | |
82 | return FALSE; | |
a8d9809f SB |
83 | } |
84 | ||
41d1f82d | 85 | text_stream.ReadLine(); // for the \n |
6319afe3 | 86 | Skip_Comment(buf_stream); |
a8d9809f | 87 | text_stream >> width >> height ; |
995612e2 | 88 | Skip_Comment(buf_stream); |
a8d9809f SB |
89 | text_stream >> maxval; |
90 | ||
a2b8bd55 | 91 | //cout << line << " " << width << " " << height << " " << maxval << endl; |
a8d9809f SB |
92 | image->Create( width, height ); |
93 | unsigned char *ptr = image->GetData(); | |
94 | if (!ptr) | |
95 | { | |
223d09f6 | 96 | wxLogError( wxT("Cannot allocate RAM for RGB data in PNM file.") ); |
995612e2 | 97 | return FALSE; |
a8d9809f SB |
98 | } |
99 | ||
a2b8bd55 | 100 | if (c=='3') // Ascii RBG |
995612e2 VZ |
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 | } | |
a8d9809f SB |
116 | } |
117 | if (c=='6') // Raw RGB | |
503aa33d | 118 | buf_stream.Read( ptr, 3*width*height ); |
a8d9809f SB |
119 | |
120 | image->SetMask( FALSE ); | |
121 | ||
503aa33d | 122 | return (buf_stream.LastError()==wxStream_NOERROR || buf_stream.LastError()==wxStream_EOF); |
a8d9809f SB |
123 | } |
124 | ||
125 | bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) ) | |
126 | { | |
127 | wxTextOutputStream text_stream(stream); | |
995612e2 VZ |
128 | |
129 | //text_stream << "P6" << endl | |
130 | //<< image->GetWidth() << " " << image->GetHeight() << endl | |
a8d9809f SB |
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 | ||
995612e2 | 138 | bool wxPNMHandler::DoCanRead( wxInputStream& stream ) |
0828c087 | 139 | { |
995612e2 | 140 | off_t pos = stream.TellI(); |
93dfff5a SB |
141 | |
142 | Skip_Comment(stream); | |
143 | ||
995612e2 VZ |
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 | } | |
93dfff5a SB |
154 | |
155 | stream.SeekI(pos); | |
156 | return FALSE; | |
0828c087 VS |
157 | } |
158 | ||
159 | ||
9ab6ee85 | 160 | #endif // wxUSE_STREAMS |
a8d9809f | 161 | |
9ab6ee85 | 162 | #endif // wxUSE_PNM |