]>
Commit | Line | Data |
---|---|---|
56c66755 GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: imagpcx.cpp | |
3 | // Purpose: wxImage PCX handler | |
4 | // Author: Guillermo Rodriguez Garcia <guille@iies.es> | |
5 | // Version: 1.00 | |
4df78dc3 GRG |
6 | // CVS-ID: $Id$ |
7 | // Copyright: (c) 1999 Guillermo Rodriguez Garcia | |
56c66755 GRG |
8 | // Licence: wxWindows licence |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | /* | |
12 | We don't put pragma implement in this file because it is already present in | |
13 | src/common/image.cpp | |
14 | */ | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
b9b32d5c GRG |
23 | #ifndef WX_PRECOMP |
24 | # include "wx/defs.h" | |
25 | #endif | |
26 | ||
995612e2 | 27 | #if wxUSE_STREAMS && wxUSE_PCX |
b9b32d5c | 28 | |
56c66755 GRG |
29 | #include "wx/image.h" |
30 | #include "wx/wfstream.h" | |
31 | #include "wx/module.h" | |
32 | #include "wx/log.h" | |
f6b77239 | 33 | #include "wx/intl.h" |
1044a386 | 34 | |
4df78dc3 GRG |
35 | //----------------------------------------------------------------------------- |
36 | // PCX decoding | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
528dad23 | 39 | void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s) |
4df78dc3 | 40 | { |
528dad23 GRG |
41 | unsigned int data, last, cont; |
42 | ||
43 | // Write 'size' bytes. The PCX official specs say there will be | |
44 | // a decoding break at the end of each scanline, so in order to | |
45 | // force this decoding break use this function to write, at most, | |
46 | // _one_ complete scanline at a time. | |
47 | ||
48 | last = (unsigned char) *(p++); | |
49 | cont = 1; | |
50 | size--; | |
51 | ||
52 | while (size-- > 0) | |
53 | { | |
54 | data = (unsigned char) *(p++); | |
55 | ||
56 | // Up to 63 bytes with the same value can be stored using a | |
57 | // single { cont, value } pair. | |
58 | // | |
59 | if ((data == last) && (cont < 63)) | |
60 | { | |
61 | cont++; | |
62 | } | |
63 | else | |
64 | { | |
65 | // Need to write a 'counter' byte? | |
66 | // | |
67 | if ((cont > 1) || ((last & 0xC0) == 0xC0)) | |
68 | s.PutC((char) (cont | 0xC0)); | |
69 | ||
70 | s.PutC((char) last); | |
71 | last = data; | |
72 | cont = 1; | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | // Write the last one and return; | |
78 | // | |
79 | if ((cont > 1) || ((last & 0xC0) == 0xC0)) | |
80 | s.PutC((char) (cont | 0xC0)); | |
81 | ||
82 | s.PutC((char) last); | |
4df78dc3 GRG |
83 | } |
84 | ||
85 | void RLEdecode(unsigned char *p, unsigned int size, wxInputStream& s) | |
86 | { | |
87 | unsigned int i, data, cont; | |
88 | ||
89 | // Read 'size' bytes. The PCX official specs say there will be | |
90 | // a decoding break at the end of each scanline (but not at the | |
91 | // end of each plane inside a scanline). Only use this function | |
92 | // to read one or more _complete_ scanlines. Else, more than | |
93 | // 'size' bytes might be read and the buffer might overflow. | |
94 | // | |
95 | while (size > 0) | |
96 | { | |
97 | data = (unsigned char)s.GetC(); | |
98 | ||
99 | // If ((data & 0xC0) != 0xC0), then the value read is a data | |
100 | // byte. Else, it is a counter (cont = val & 0x3F) and the | |
101 | // next byte is the data byte. | |
102 | // | |
103 | if ((data & 0xC0) != 0xC0) | |
104 | { | |
105 | *(p++) = data; | |
106 | size--; | |
107 | } | |
108 | else | |
109 | { | |
110 | cont = data & 0x3F; | |
111 | data = (unsigned char)s.GetC(); | |
112 | for (i = 1; i <= cont; i++) | |
113 | *(p++) = data; | |
114 | size -= cont; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | /* PCX header */ | |
528dad23 | 121 | #define HDR_MANUFACTURER 0 |
995612e2 | 122 | #define HDR_VERSION 1 |
4df78dc3 GRG |
123 | #define HDR_ENCODING 2 |
124 | #define HDR_BITSPERPIXEL 3 | |
125 | #define HDR_XMIN 4 | |
126 | #define HDR_YMIN 6 | |
995612e2 | 127 | #define HDR_XMAX 8 |
4df78dc3 GRG |
128 | #define HDR_YMAX 10 |
129 | #define HDR_NPLANES 65 | |
130 | #define HDR_BYTESPERLINE 66 | |
528dad23 | 131 | #define HDR_PALETTEINFO 68 |
4df78dc3 | 132 | |
e4b8154a GRG |
133 | // image formats |
134 | enum { | |
135 | wxPCX_8BIT, // 8 bpp, 1 plane (8 bit) | |
136 | wxPCX_24BIT // 8 bpp, 3 planes (24 bit) | |
137 | }; | |
138 | ||
139 | // error codes | |
140 | enum { | |
141 | wxPCX_OK = 0, // everything was OK | |
142 | wxPCX_INVFORMAT = 1, // error in pcx file format | |
143 | wxPCX_MEMERR = 2, // error allocating memory | |
144 | wxPCX_VERERR = 3 // error in pcx version number | |
145 | }; | |
4df78dc3 GRG |
146 | |
147 | // ReadPCX: | |
148 | // Loads a PCX file into the wxImage object pointed by image. | |
e4b8154a GRG |
149 | // Returns wxPCX_OK on success, or an error code otherwise |
150 | // (see above for error codes) | |
4df78dc3 GRG |
151 | // |
152 | int ReadPCX(wxImage *image, wxInputStream& stream) | |
153 | { | |
154 | unsigned char hdr[128]; // PCX header | |
155 | unsigned char pal[768]; // palette for 8 bit images | |
156 | unsigned char *p; // space to store one scanline | |
157 | unsigned char *dst; // pointer into wxImage data | |
158 | unsigned int width, height; // size of the image | |
159 | unsigned int bytesperline; // bytes per line (each plane) | |
160 | int bitsperpixel; // bits per pixel (each plane) | |
161 | int nplanes; // number of planes | |
162 | int encoding; // is the image RLE encoded? | |
163 | int format; // image format (8 bit, 24 bit) | |
164 | unsigned int i; | |
165 | off_t pos; | |
166 | ||
167 | // Read PCX header and check the version number (it must | |
168 | // be at least 5 or higher for 8 bit and 24 bit images). | |
169 | // | |
170 | stream.Read(hdr, 128); | |
171 | ||
e4b8154a | 172 | if (hdr[HDR_VERSION] < 5) return wxPCX_VERERR; |
4df78dc3 GRG |
173 | |
174 | // Extract all image info from the PCX header. | |
175 | // | |
176 | encoding = hdr[HDR_ENCODING]; | |
177 | nplanes = hdr[HDR_NPLANES]; | |
178 | bitsperpixel = hdr[HDR_BITSPERPIXEL]; | |
179 | bytesperline = hdr[HDR_BYTESPERLINE] + 256 * hdr[HDR_BYTESPERLINE + 1]; | |
180 | width = (hdr[HDR_XMAX] + 256 * hdr[HDR_XMAX + 1]) - | |
181 | (hdr[HDR_XMIN] + 256 * hdr[HDR_XMIN + 1]) + 1; | |
182 | height = (hdr[HDR_YMAX] + 256 * hdr[HDR_YMAX + 1]) - | |
183 | (hdr[HDR_YMIN] + 256 * hdr[HDR_YMIN + 1]) + 1; | |
184 | ||
185 | // Check image format. Currently supported formats are | |
186 | // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes). | |
187 | // | |
188 | if ((nplanes == 3) && (bitsperpixel == 8)) | |
e4b8154a | 189 | format = wxPCX_24BIT; |
4df78dc3 | 190 | else if ((nplanes == 1) && (bitsperpixel == 8)) |
e4b8154a | 191 | format = wxPCX_8BIT; |
4df78dc3 | 192 | else |
e4b8154a | 193 | return wxPCX_INVFORMAT; |
4df78dc3 | 194 | |
e4b8154a | 195 | // If the image is of type wxPCX_8BIT, then there is a |
4df78dc3 GRG |
196 | // palette at the end of the file. Read it now before |
197 | // proceeding. | |
198 | // | |
e4b8154a | 199 | if (format == wxPCX_8BIT) |
f0d922cf GRG |
200 | { |
201 | pos = stream.TellI(); | |
202 | stream.SeekI(-769, wxFromEnd); | |
4df78dc3 | 203 | |
f0d922cf | 204 | if (stream.GetC() != 12) |
e4b8154a | 205 | return wxPCX_INVFORMAT; |
4df78dc3 | 206 | |
f0d922cf GRG |
207 | stream.Read(pal, 768); |
208 | stream.SeekI(pos, wxFromStart); | |
209 | } | |
4df78dc3 GRG |
210 | |
211 | // Allocate memory for a scanline and resize the image. | |
212 | // | |
213 | image->Create(width, height); | |
214 | ||
215 | if (!image->Ok()) | |
e4b8154a | 216 | return wxPCX_MEMERR; |
4df78dc3 GRG |
217 | |
218 | if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL) | |
e4b8154a | 219 | return wxPCX_MEMERR; |
4df78dc3 GRG |
220 | |
221 | // Now start reading the file, line by line, and store | |
222 | // the data in the format required by wxImage. | |
223 | // | |
224 | dst = image->GetData(); | |
225 | ||
226 | for (; height; height--) | |
227 | { | |
228 | if (encoding) | |
229 | RLEdecode(p, bytesperline * nplanes, stream); | |
230 | else | |
231 | stream.Read(p, bytesperline * nplanes); | |
232 | ||
233 | switch (format) | |
234 | { | |
e4b8154a | 235 | case wxPCX_8BIT: |
4df78dc3 GRG |
236 | { |
237 | for (i = 0; i < width; i++) | |
238 | { | |
239 | *(dst++) = pal[ 3 * (p[i]) ]; | |
240 | *(dst++) = pal[ 3 * (p[i]) + 1]; | |
241 | *(dst++) = pal[ 3 * (p[i]) + 2]; | |
242 | } | |
243 | break; | |
244 | } | |
e4b8154a | 245 | case wxPCX_24BIT: |
4df78dc3 GRG |
246 | { |
247 | for (i = 0; i < width; i++) | |
248 | { | |
249 | *(dst++) = p[i]; | |
250 | *(dst++) = p[i + bytesperline]; | |
995612e2 | 251 | *(dst++) = p[i + 2 * bytesperline]; |
4df78dc3 GRG |
252 | } |
253 | break; | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | free(p); | |
259 | ||
e4b8154a | 260 | return wxPCX_OK; |
4df78dc3 GRG |
261 | } |
262 | ||
528dad23 GRG |
263 | // SavePCX: |
264 | // Saves a PCX file into the wxImage object pointed by image. | |
265 | // Returns wxPCX_OK on success, or an error code otherwise | |
266 | // (see above for error codes). Currently, always saves images | |
267 | // in 24 bit format. XXX | |
268 | // | |
269 | int SavePCX(wxImage *image, wxOutputStream& stream) | |
270 | { | |
271 | unsigned char hdr[128]; // PCX header | |
272 | unsigned char *p; // space to store one scanline | |
273 | unsigned char *src; // pointer into wxImage data | |
274 | unsigned int width, height; // size of the image | |
275 | unsigned int bytesperline; // bytes per line (each plane) | |
276 | int nplanes = 3; // number of planes | |
277 | int format = wxPCX_24BIT; // image format (8 bit, 24 bit) | |
278 | unsigned int i; | |
279 | ||
280 | /* XXX | |
281 | build_palette(); | |
282 | if (num_of_colors <= 256) | |
283 | { | |
284 | format = wxPCX_8BIT; | |
285 | nplanes = 1; | |
286 | } | |
287 | else | |
288 | free_palette(); | |
289 | */ | |
290 | ||
291 | // Get image dimensions, calculate bytesperline (must be even, | |
292 | // according to PCX specs) and allocate space for one complete | |
293 | // scanline. | |
294 | // | |
295 | if (!image->Ok()) | |
296 | return wxPCX_INVFORMAT; | |
297 | ||
298 | width = image->GetWidth(); | |
299 | height = image->GetHeight(); | |
300 | nplanes = 3; /* 1 for wxPCX_8BIT */ | |
301 | bytesperline = width; | |
302 | if (bytesperline % 2) | |
303 | bytesperline++; | |
304 | ||
305 | if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL) | |
306 | return wxPCX_MEMERR; | |
307 | ||
308 | // Build header data and write it to the stream. Initially, | |
309 | // set all bytes to zero (most values default to zero). | |
310 | // | |
311 | memset(hdr, 0, sizeof(hdr)); | |
312 | ||
313 | hdr[HDR_MANUFACTURER] = 10; | |
314 | hdr[HDR_VERSION] = 5; | |
315 | hdr[HDR_ENCODING] = 1; | |
316 | hdr[HDR_NPLANES] = nplanes; | |
317 | hdr[HDR_BITSPERPIXEL] = 8; | |
318 | hdr[HDR_BYTESPERLINE] = bytesperline % 256; | |
319 | hdr[HDR_BYTESPERLINE + 1] = bytesperline / 256; | |
320 | hdr[HDR_XMAX] = (width - 1) % 256; | |
321 | hdr[HDR_XMAX + 1] = (width - 1) / 256; | |
322 | hdr[HDR_YMAX] = (height - 1) % 256; | |
323 | hdr[HDR_YMAX + 1] = (height - 1) / 256; | |
324 | hdr[HDR_PALETTEINFO] = 1; | |
325 | ||
326 | stream.Write(hdr, 128); | |
327 | ||
328 | // Encode image data line by line and write it to the stream | |
329 | // | |
330 | src = image->GetData(); | |
331 | ||
332 | for (; height; height--) | |
333 | { | |
334 | switch (format) | |
335 | { | |
336 | case wxPCX_8BIT: | |
337 | /* XXX | |
338 | { | |
339 | for (i = 0; i < width; i++) | |
340 | { | |
341 | hash = *(src++) << 24 + | |
342 | *(src++) << 16 + | |
343 | *(src++) << 8; | |
344 | ||
345 | p[i] = palette_lookup(hash); | |
346 | } | |
347 | break; | |
348 | } | |
349 | */ | |
350 | case wxPCX_24BIT: | |
351 | { | |
352 | for (i = 0; i < width; i++) | |
353 | { | |
354 | p[i] = *(src++); | |
355 | p[i + bytesperline] = *(src++); | |
356 | p[i + 2 * bytesperline] = *(src++); | |
357 | } | |
358 | break; | |
359 | } | |
360 | } | |
361 | ||
362 | RLEencode(p, bytesperline * nplanes, stream); | |
363 | } | |
364 | ||
365 | free(p); | |
366 | ||
367 | /* XXX | |
368 | if (format == wxPCX_8BIT) | |
369 | { | |
370 | stream.PutC(12); | |
371 | dump_palette(); | |
372 | } | |
373 | */ | |
374 | ||
375 | return wxPCX_OK; | |
376 | } | |
377 | ||
4df78dc3 | 378 | |
56c66755 GRG |
379 | //----------------------------------------------------------------------------- |
380 | // wxPCXHandler | |
381 | //----------------------------------------------------------------------------- | |
382 | ||
56c66755 | 383 | IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler) |
56c66755 | 384 | |
700ec454 | 385 | bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) |
56c66755 | 386 | { |
4df78dc3 GRG |
387 | int error; |
388 | ||
389 | if (!CanRead(stream)) | |
390 | { | |
391 | if (verbose) | |
58c837a4 | 392 | wxLogError(_("PCX: this is not a PCX file.")); |
4df78dc3 GRG |
393 | |
394 | return FALSE; | |
395 | } | |
396 | ||
56c66755 GRG |
397 | image->Destroy(); |
398 | ||
e4b8154a | 399 | if ((error = ReadPCX(image, stream)) != wxPCX_OK) |
4df78dc3 GRG |
400 | { |
401 | if (verbose) | |
402 | { | |
403 | switch (error) | |
404 | { | |
e4b8154a GRG |
405 | case wxPCX_INVFORMAT: wxLogError(_("wxPCXHandler: image format unsupported")); break; |
406 | case wxPCX_MEMERR: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break; | |
407 | case wxPCX_VERERR: wxLogError(_("wxPCXHandler: version number too low")); break; | |
408 | default: wxLogError(_("wxPCXHandler: unknown error !!!")); | |
4df78dc3 GRG |
409 | } |
410 | } | |
411 | image->Destroy(); | |
412 | return FALSE; | |
413 | } | |
56c66755 | 414 | |
4df78dc3 | 415 | return TRUE; |
56c66755 GRG |
416 | } |
417 | ||
528dad23 | 418 | bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
56c66755 | 419 | { |
528dad23 GRG |
420 | int error; |
421 | ||
422 | if ((error = SavePCX(image, stream)) != wxPCX_OK) | |
423 | { | |
424 | if (verbose) | |
425 | { | |
426 | switch (error) | |
427 | { | |
428 | case wxPCX_INVFORMAT: wxLogError(_("wxPCXHandler: invalid image")); break; | |
429 | case wxPCX_MEMERR: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break; | |
430 | default: wxLogError(_("wxPCXHandler: unknown error !!!")); | |
431 | } | |
432 | } | |
433 | } | |
56c66755 | 434 | |
528dad23 | 435 | return (error == wxPCX_OK); |
56c66755 GRG |
436 | } |
437 | ||
995612e2 | 438 | bool wxPCXHandler::DoCanRead( wxInputStream& stream ) |
56c66755 | 439 | { |
4df78dc3 | 440 | unsigned char c; |
4df78dc3 | 441 | |
4df78dc3 | 442 | c = stream.GetC(); |
a05da1b6 | 443 | stream.SeekI(-1, wxFromCurrent); |
4df78dc3 GRG |
444 | |
445 | // not very safe, but this is all we can get from PCX header :-( | |
446 | return (c == 10); | |
56c66755 GRG |
447 | } |
448 | ||
b9b32d5c | 449 | #endif // wxUSE_STREAMS && wxUSE_PCX |
56c66755 | 450 |