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