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