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