]>
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 | |
65571936 | 7 | // Licence: wxWindows licence |
a8d9809f SB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
a8d9809f SB |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
b9b32d5c GRG |
17 | #ifndef WX_PRECOMP |
18 | # include "wx/setup.h" | |
19 | #endif | |
20 | ||
c96ea657 | 21 | #if wxUSE_IMAGE && wxUSE_PNM |
b9b32d5c | 22 | |
8f493002 | 23 | #include "wx/imagpnm.h" |
a8d9809f | 24 | #include "wx/log.h" |
58c837a4 | 25 | #include "wx/intl.h" |
a8d9809f SB |
26 | #include "wx/txtstrm.h" |
27 | ||
a8d9809f SB |
28 | //----------------------------------------------------------------------------- |
29 | // wxBMPHandler | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
a8d9809f | 32 | IMPLEMENT_DYNAMIC_CLASS(wxPNMHandler,wxImageHandler) |
a8d9809f | 33 | |
9ab6ee85 | 34 | #if wxUSE_STREAMS |
a8d9809f SB |
35 | |
36 | void Skip_Comment(wxInputStream &stream) | |
37 | { | |
2b5f62a0 | 38 | wxTextInputStream text_stream(stream); |
a8d9809f | 39 | |
2b5f62a0 | 40 | if (stream.Peek()==wxT('#')) |
a8d9809f | 41 | { |
2b5f62a0 VZ |
42 | text_stream.ReadLine(); |
43 | Skip_Comment(stream); | |
a8d9809f SB |
44 | } |
45 | } | |
46 | ||
58c837a4 | 47 | bool wxPNMHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) |
a8d9809f SB |
48 | { |
49 | wxUint32 width, height; | |
50 | wxUint16 maxval; | |
a8d9809f | 51 | char c(0); |
995612e2 | 52 | |
a8d9809f SB |
53 | image->Destroy(); |
54 | ||
55 | /* | |
56 | * Read the PNM header | |
57 | */ | |
58 | ||
6319afe3 GL |
59 | wxBufferedInputStream buf_stream(stream); |
60 | wxTextInputStream text_stream(buf_stream); | |
a8d9809f | 61 | |
6319afe3 | 62 | Skip_Comment(buf_stream); |
223d09f6 | 63 | if (buf_stream.GetC()==wxT('P')) c=buf_stream.GetC(); |
a8d9809f SB |
64 | |
65 | switch (c) | |
58c837a4 | 66 | { |
7448de8d | 67 | case wxT('2'): // ASCII Grey |
3e8711ce JS |
68 | case wxT('3'): // ASCII RGB |
69 | case wxT('5'): // RAW Grey | |
7beb59f3 | 70 | case wxT('6'): break; |
58c837a4 RR |
71 | default: |
72 | if (verbose) wxLogError(_("PNM: File format is not recognized.")); | |
7beb59f3 | 73 | return false; |
58c837a4 | 74 | } |
a8d9809f | 75 | |
41d1f82d | 76 | text_stream.ReadLine(); // for the \n |
6319afe3 | 77 | Skip_Comment(buf_stream); |
a8d9809f | 78 | text_stream >> width >> height ; |
995612e2 | 79 | Skip_Comment(buf_stream); |
a8d9809f SB |
80 | text_stream >> maxval; |
81 | ||
a2b8bd55 | 82 | //cout << line << " " << width << " " << height << " " << maxval << endl; |
a8d9809f SB |
83 | image->Create( width, height ); |
84 | unsigned char *ptr = image->GetData(); | |
85 | if (!ptr) | |
86 | { | |
58c837a4 RR |
87 | if (verbose) |
88 | wxLogError( _("PNM: Couldn't allocate memory.") ); | |
7beb59f3 | 89 | return false; |
a8d9809f SB |
90 | } |
91 | ||
3e8711ce | 92 | |
7448de8d WS |
93 | if (c=='2') // Ascii GREY |
94 | { | |
3e8711ce JS |
95 | wxUint32 value, size=width*height; |
96 | for (wxUint32 i=0; i<size; ++i) | |
97 | { | |
98 | value=text_stream.Read32(); | |
99 | *ptr++=(unsigned char)value; // R | |
7448de8d | 100 | *ptr++=(unsigned char)value; // G |
3e8711ce JS |
101 | *ptr++=(unsigned char)value; // B |
102 | if ( !buf_stream ) | |
103 | { | |
104 | if (verbose) wxLogError(_("PNM: File seems truncated.")); | |
105 | return false; | |
106 | } | |
107 | } | |
108 | } | |
7448de8d WS |
109 | if (c=='3') // Ascii RBG |
110 | { | |
995612e2 VZ |
111 | wxUint32 value, size=3*width*height; |
112 | for (wxUint32 i=0; i<size; ++i) | |
113 | { | |
114 | //this is very slow !!! | |
115 | //I wonder how we can make any better ? | |
116 | value=text_stream.Read32(); | |
117 | *ptr++=(unsigned char)value; | |
118 | ||
2b5f62a0 | 119 | if ( !buf_stream ) |
995612e2 | 120 | { |
58c837a4 | 121 | if (verbose) wxLogError(_("PNM: File seems truncated.")); |
7beb59f3 | 122 | return false; |
995612e2 VZ |
123 | } |
124 | } | |
7448de8d WS |
125 | } |
126 | if (c=='5') // Raw GREY | |
127 | { | |
3e8711ce JS |
128 | wxUint32 size=width*height; |
129 | unsigned char value; | |
130 | for (wxUint32 i=0; i<size; ++i) | |
131 | { | |
132 | buf_stream.Read(&value,1); | |
133 | *ptr++=value; // R | |
134 | *ptr++=value; // G | |
7448de8d | 135 | *ptr++=value; // B |
3e8711ce JS |
136 | if ( !buf_stream ) |
137 | { | |
138 | if (verbose) wxLogError(_("PNM: File seems truncated.")); | |
139 | return false; | |
140 | } | |
141 | } | |
142 | } | |
a8d9809f | 143 | if (c=='6') // Raw RGB |
503aa33d | 144 | buf_stream.Read( ptr, 3*width*height ); |
a8d9809f | 145 | |
7beb59f3 | 146 | image->SetMask( false ); |
a8d9809f | 147 | |
2b5f62a0 VZ |
148 | const wxStreamError err = buf_stream.GetLastError(); |
149 | return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF; | |
a8d9809f SB |
150 | } |
151 | ||
152 | bool wxPNMHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool WXUNUSED(verbose) ) | |
153 | { | |
154 | wxTextOutputStream text_stream(stream); | |
995612e2 VZ |
155 | |
156 | //text_stream << "P6" << endl | |
157 | //<< image->GetWidth() << " " << image->GetHeight() << endl | |
a8d9809f | 158 | //<< "255" << endl; |
2b5f62a0 | 159 | text_stream << wxT("P6\n") << image->GetWidth() << wxT(" ") << image->GetHeight() << wxT("\n255\n"); |
a8d9809f SB |
160 | stream.Write(image->GetData(),3*image->GetWidth()*image->GetHeight()); |
161 | ||
2b5f62a0 | 162 | return stream.IsOk(); |
a8d9809f SB |
163 | } |
164 | ||
995612e2 | 165 | bool wxPNMHandler::DoCanRead( wxInputStream& stream ) |
0828c087 | 166 | { |
93dfff5a SB |
167 | Skip_Comment(stream); |
168 | ||
995612e2 VZ |
169 | if ( stream.GetC() == 'P' ) |
170 | { | |
cdf0ff0f | 171 | switch ( stream.GetC() ) |
995612e2 | 172 | { |
cdf0ff0f VZ |
173 | case '2': // ASCII Grey |
174 | case '3': // ASCII RGB | |
175 | case '5': // RAW Grey | |
176 | case '6': // RAW RGB | |
7beb59f3 | 177 | return true; |
995612e2 VZ |
178 | } |
179 | } | |
93dfff5a | 180 | |
7beb59f3 | 181 | return false; |
0828c087 VS |
182 | } |
183 | ||
184 | ||
9ab6ee85 | 185 | #endif // wxUSE_STREAMS |
a8d9809f | 186 | |
9ab6ee85 | 187 | #endif // wxUSE_PNM |