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