]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpcx.cpp
fix printf() argument type in GetOsInfo()
[wxWidgets.git] / src / common / imagpcx.cpp
CommitLineData
56c66755 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/imagpcx.cpp
56c66755
GRG
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
65571936 8// Licence: wxWindows licence
56c66755
GRG
9/////////////////////////////////////////////////////////////////////////////
10
56c66755
GRG
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
8898456d 15 #pragma hdrstop
56c66755
GRG
16#endif
17
8898456d
WS
18#if wxUSE_IMAGE && wxUSE_PCX
19
b9b32d5c 20#ifndef WX_PRECOMP
8e3f3880 21 #include "wx/object.h"
8ecff181 22 #include "wx/list.h"
8898456d
WS
23 #include "wx/log.h"
24 #include "wx/intl.h"
25 #include "wx/palette.h"
32d4c30a 26 #include "wx/hash.h"
b9b32d5c
GRG
27#endif
28
8f493002 29#include "wx/imagpcx.h"
56c66755
GRG
30#include "wx/wfstream.h"
31#include "wx/module.h"
1044a386 32
e30285ab
VZ
33//-----------------------------------------------------------------------------
34// wxPCXHandler
35//-----------------------------------------------------------------------------
36
37IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler)
38
39#if wxUSE_STREAMS
40
4df78dc3 41//-----------------------------------------------------------------------------
d32548aa 42// RLE encoding and decoding
4df78dc3
GRG
43//-----------------------------------------------------------------------------
44
528dad23 45void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s)
4df78dc3 46{
528dad23
GRG
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
0bddb3cc
GRG
62 // Up to 63 bytes with the same value can be stored using
63 // a single { cont, value } pair.
528dad23
GRG
64 //
65 if ((data == last) && (cont < 63))
66 {
67 cont++;
68 }
69 else
70 {
0bddb3cc 71 // need to write a 'counter' byte?
528dad23
GRG
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
0bddb3cc 81 // write the last one and return;
528dad23
GRG
82 if ((cont > 1) || ((last & 0xC0) == 0xC0))
83 s.PutC((char) (cont | 0xC0));
84
85 s.PutC((char) last);
4df78dc3
GRG
86}
87
88void RLEdecode(unsigned char *p, unsigned int size, wxInputStream& s)
89{
90 unsigned int i, data, cont;
91
92 // Read 'size' bytes. The PCX official specs say there will be
93 // a decoding break at the end of each scanline (but not at the
94 // end of each plane inside a scanline). Only use this function
95 // to read one or more _complete_ scanlines. Else, more than
96 // 'size' bytes might be read and the buffer might overflow.
0bddb3cc 97
4df78dc3
GRG
98 while (size > 0)
99 {
100 data = (unsigned char)s.GetC();
101
102 // If ((data & 0xC0) != 0xC0), then the value read is a data
103 // byte. Else, it is a counter (cont = val & 0x3F) and the
104 // next byte is the data byte.
7beb59f3 105
4df78dc3
GRG
106 if ((data & 0xC0) != 0xC0)
107 {
33ac7e6f 108 *(p++) = (unsigned char)data;
4df78dc3
GRG
109 size--;
110 }
111 else
112 {
113 cont = data & 0x3F;
114 data = (unsigned char)s.GetC();
115 for (i = 1; i <= cont; i++)
33ac7e6f 116 *(p++) = (unsigned char)data;
4df78dc3
GRG
117 size -= cont;
118 }
119 }
120}
121
122
d32548aa
GRG
123//-----------------------------------------------------------------------------
124// PCX reading and saving
125//-----------------------------------------------------------------------------
126
127// PCX header
528dad23 128#define HDR_MANUFACTURER 0
995612e2 129#define HDR_VERSION 1
4df78dc3
GRG
130#define HDR_ENCODING 2
131#define HDR_BITSPERPIXEL 3
132#define HDR_XMIN 4
133#define HDR_YMIN 6
995612e2 134#define HDR_XMAX 8
4df78dc3
GRG
135#define HDR_YMAX 10
136#define HDR_NPLANES 65
137#define HDR_BYTESPERLINE 66
528dad23 138#define HDR_PALETTEINFO 68
4df78dc3 139
e4b8154a
GRG
140// image formats
141enum {
142 wxPCX_8BIT, // 8 bpp, 1 plane (8 bit)
143 wxPCX_24BIT // 8 bpp, 3 planes (24 bit)
144};
145
146// error codes
147enum {
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};
4df78dc3 153
faa7a70e 154
4df78dc3
GRG
155// ReadPCX:
156// Loads a PCX file into the wxImage object pointed by image.
e4b8154a
GRG
157// Returns wxPCX_OK on success, or an error code otherwise
158// (see above for error codes)
4df78dc3
GRG
159//
160int 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)
51dba3f8 172 unsigned int i, j;
4df78dc3
GRG
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).
0bddb3cc 176
4df78dc3
GRG
177 stream.Read(hdr, 128);
178
e4b8154a 179 if (hdr[HDR_VERSION] < 5) return wxPCX_VERERR;
4df78dc3
GRG
180
181 // Extract all image info from the PCX header.
0bddb3cc 182
4df78dc3
GRG
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).
0bddb3cc 194
4df78dc3 195 if ((nplanes == 3) && (bitsperpixel == 8))
e4b8154a 196 format = wxPCX_24BIT;
4df78dc3 197 else if ((nplanes == 1) && (bitsperpixel == 8))
e4b8154a 198 format = wxPCX_8BIT;
4df78dc3 199 else
e4b8154a 200 return wxPCX_INVFORMAT;
4df78dc3 201
51dba3f8
GRG
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.
4df78dc3 212
51dba3f8 213 // Resize the image and allocate memory for a scanline.
0bddb3cc 214
4df78dc3
GRG
215 image->Create(width, height);
216
217 if (!image->Ok())
e4b8154a 218 return wxPCX_MEMERR;
4df78dc3
GRG
219
220 if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL)
e4b8154a 221 return wxPCX_MEMERR;
4df78dc3
GRG
222
223 // Now start reading the file, line by line, and store
224 // the data in the format required by wxImage.
0bddb3cc 225
4df78dc3
GRG
226 dst = image->GetData();
227
51dba3f8 228 for (j = height; j; j--)
4df78dc3
GRG
229 {
230 if (encoding)
231 RLEdecode(p, bytesperline * nplanes, stream);
232 else
233 stream.Read(p, bytesperline * nplanes);
234
235 switch (format)
236 {
e4b8154a 237 case wxPCX_8BIT:
4df78dc3
GRG
238 {
239 for (i = 0; i < width; i++)
240 {
51dba3f8
GRG
241 // first pass, just store the colour index
242 *dst = p[i];
243 dst += 3;
4df78dc3
GRG
244 }
245 break;
246 }
e4b8154a 247 case wxPCX_24BIT:
4df78dc3
GRG
248 {
249 for (i = 0; i < width; i++)
250 {
251 *(dst++) = p[i];
252 *(dst++) = p[i + bytesperline];
995612e2 253 *(dst++) = p[i + 2 * bytesperline];
4df78dc3
GRG
254 }
255 break;
256 }
257 }
258 }
259
260 free(p);
261
51dba3f8
GRG
262 // For 8 bit images, we read the palette, and then do a second
263 // pass replacing indexes with their RGB values;
0bddb3cc 264
51dba3f8
GRG
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 }
3f4fc796 282
b11e8fb6
VZ
283#if wxUSE_PALETTE
284 unsigned char r[256];
285 unsigned char g[256];
286 unsigned char b[256];
3f4fc796
JS
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));
b11e8fb6 294#endif // wxUSE_PALETTE
51dba3f8
GRG
295 }
296
e4b8154a 297 return wxPCX_OK;
4df78dc3
GRG
298}
299
528dad23
GRG
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
0bddb3cc
GRG
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.
528dad23
GRG
306//
307int SavePCX(wxImage *image, wxOutputStream& stream)
308{
309 unsigned char hdr[128]; // PCX header
0848b0dd 310 unsigned char pal[768]; // palette for 8 bit images
528dad23
GRG
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)
7ac31c42 315 unsigned char nplanes = 3; // number of planes
d32548aa 316 int format = wxPCX_24BIT; // image format (8 bit, 24 bit)
952ae1e8 317 wxImageHistogram histogram; // image histogram
0848b0dd 318 unsigned long key; // key in the hashtable
528dad23 319 unsigned int i;
479cd5de 320
d32548aa 321 // See if we can save as 8 bit.
0bddb3cc 322
d32548aa 323 if (image->CountColours(256) <= 256)
528dad23 324 {
952ae1e8 325 image->ComputeHistogram(histogram);
528dad23
GRG
326 format = wxPCX_8BIT;
327 nplanes = 1;
328 }
528dad23
GRG
329
330 // Get image dimensions, calculate bytesperline (must be even,
331 // according to PCX specs) and allocate space for one complete
332 // scanline.
0bddb3cc 333
528dad23
GRG
334 if (!image->Ok())
335 return wxPCX_INVFORMAT;
336
337 width = image->GetWidth();
338 height = image->GetHeight();
528dad23
GRG
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).
0bddb3cc 348
528dad23
GRG
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;
33ac7e6f
KB
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);
528dad23
GRG
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
0bddb3cc 367
528dad23
GRG
368 src = image->GetData();
369
370 for (; height; height--)
371 {
372 switch (format)
373 {
374 case wxPCX_8BIT:
528dad23 375 {
0848b0dd 376 unsigned char r, g, b;
0848b0dd 377
528dad23
GRG
378 for (i = 0; i < width; i++)
379 {
0848b0dd
GRG
380 r = *(src++);
381 g = *(src++);
382 b = *(src++);
383 key = (r << 16) | (g << 8) | b;
384
952ae1e8 385 p[i] = (unsigned char)histogram[key].index;
528dad23
GRG
386 }
387 break;
388 }
528dad23
GRG
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 }
d32548aa 400
528dad23
GRG
401 RLEencode(p, bytesperline * nplanes, stream);
402 }
479cd5de 403
528dad23
GRG
404 free(p);
405
952ae1e8 406 // For 8 bit images, build the palette and write it to the stream:
528dad23
GRG
407 if (format == wxPCX_8BIT)
408 {
0848b0dd
GRG
409 // zero unused colours
410 memset(pal, 0, sizeof(pal));
411
952ae1e8 412 unsigned long index;
7beb59f3 413
952ae1e8 414 for (wxImageHistogram::iterator entry = histogram.begin();
60d8e886 415 entry != histogram.end(); ++entry )
0848b0dd 416 {
952ae1e8
VS
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);
0848b0dd
GRG
422 }
423
528dad23 424 stream.PutC(12);
0848b0dd 425 stream.Write(pal, 768);
528dad23 426 }
528dad23
GRG
427
428 return wxPCX_OK;
429}
430
56c66755
GRG
431//-----------------------------------------------------------------------------
432// wxPCXHandler
433//-----------------------------------------------------------------------------
434
700ec454 435bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
56c66755 436{
4df78dc3
GRG
437 int error;
438
439 if (!CanRead(stream))
440 {
441 if (verbose)
58c837a4 442 wxLogError(_("PCX: this is not a PCX file."));
4df78dc3 443
7beb59f3 444 return false;
4df78dc3
GRG
445 }
446
56c66755
GRG
447 image->Destroy();
448
e4b8154a 449 if ((error = ReadPCX(image, stream)) != wxPCX_OK)
4df78dc3
GRG
450 {
451 if (verbose)
452 {
453 switch (error)
454 {
add95ac3
VS
455 case wxPCX_INVFORMAT: wxLogError(_("PCX: image format unsupported")); break;
456 case wxPCX_MEMERR: wxLogError(_("PCX: couldn't allocate memory")); break;
457 case wxPCX_VERERR: wxLogError(_("PCX: version number too low")); break;
458 default: wxLogError(_("PCX: unknown error !!!"));
4df78dc3
GRG
459 }
460 }
461 image->Destroy();
7beb59f3 462 return false;
4df78dc3 463 }
56c66755 464
7beb59f3 465 return true;
56c66755
GRG
466}
467
528dad23 468bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
56c66755 469{
528dad23
GRG
470 int error;
471
472 if ((error = SavePCX(image, stream)) != wxPCX_OK)
473 {
474 if (verbose)
475 {
476 switch (error)
477 {
add95ac3
VS
478 case wxPCX_INVFORMAT: wxLogError(_("PCX: invalid image")); break;
479 case wxPCX_MEMERR: wxLogError(_("PCX: couldn't allocate memory")); break;
480 default: wxLogError(_("PCX: unknown error !!!"));
528dad23
GRG
481 }
482 }
483 }
56c66755 484
528dad23 485 return (error == wxPCX_OK);
56c66755
GRG
486}
487
995612e2 488bool wxPCXHandler::DoCanRead( wxInputStream& stream )
56c66755 489{
79fa2374
VZ
490 unsigned char c = stream.GetC();
491 if ( !stream )
7beb59f3 492 return false;
4df78dc3 493
4df78dc3 494 // not very safe, but this is all we can get from PCX header :-(
79fa2374 495 return c == 10;
56c66755
GRG
496}
497
e30285ab
VZ
498#endif // wxUSE_STREAMS
499
500#endif // wxUSE_IMAGE && wxUSE_PCX