]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpcx.cpp
Added mono bitmap to image conversion.
[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>
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
8f493002
VS
11#ifdef __GNUG__
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"
24#endif
25
995612e2 26#if wxUSE_STREAMS && wxUSE_PCX
b9b32d5c 27
8f493002 28#include "wx/imagpcx.h"
56c66755
GRG
29#include "wx/wfstream.h"
30#include "wx/module.h"
31#include "wx/log.h"
f6b77239 32#include "wx/intl.h"
1044a386 33
0848b0dd
GRG
34#include "wx/hash.h"
35#include "wx/list.h"
36#include "wx/object.h"
37
4df78dc3 38//-----------------------------------------------------------------------------
d32548aa 39// RLE encoding and decoding
4df78dc3
GRG
40//-----------------------------------------------------------------------------
41
528dad23 42void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s)
4df78dc3 43{
528dad23
GRG
44 unsigned int data, last, cont;
45
46 // Write 'size' bytes. The PCX official specs say there will be
47 // a decoding break at the end of each scanline, so in order to
48 // force this decoding break use this function to write, at most,
49 // _one_ complete scanline at a time.
50
51 last = (unsigned char) *(p++);
52 cont = 1;
53 size--;
54
55 while (size-- > 0)
56 {
57 data = (unsigned char) *(p++);
58
59 // Up to 63 bytes with the same value can be stored using a
60 // single { cont, value } pair.
61 //
62 if ((data == last) && (cont < 63))
63 {
64 cont++;
65 }
66 else
67 {
68 // Need to write a 'counter' byte?
69 //
70 if ((cont > 1) || ((last & 0xC0) == 0xC0))
71 s.PutC((char) (cont | 0xC0));
72
73 s.PutC((char) last);
74 last = data;
75 cont = 1;
76 }
77 }
78
528dad23
GRG
79 // Write the last one and return;
80 //
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{
89 unsigned int i, data, cont;
90
91 // Read 'size' bytes. The PCX official specs say there will be
92 // a decoding break at the end of each scanline (but not at the
93 // end of each plane inside a scanline). Only use this function
94 // to read one or more _complete_ scanlines. Else, more than
95 // 'size' bytes might be read and the buffer might overflow.
96 //
97 while (size > 0)
98 {
99 data = (unsigned char)s.GetC();
100
101 // If ((data & 0xC0) != 0xC0), then the value read is a data
102 // byte. Else, it is a counter (cont = val & 0x3F) and the
103 // next byte is the data byte.
104 //
105 if ((data & 0xC0) != 0xC0)
106 {
107 *(p++) = data;
108 size--;
109 }
110 else
111 {
112 cont = data & 0x3F;
113 data = (unsigned char)s.GetC();
114 for (i = 1; i <= cont; i++)
115 *(p++) = data;
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).
175 //
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.
181 //
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).
193 //
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
4df78dc3 212
51dba3f8 213 // Resize the image and allocate memory for a scanline.
4df78dc3
GRG
214 //
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.
225 //
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;
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
e4b8154a 284 return wxPCX_OK;
4df78dc3
GRG
285}
286
528dad23
GRG
287// SavePCX:
288// Saves a PCX file into the wxImage object pointed by image.
289// Returns wxPCX_OK on success, or an error code otherwise
290// (see above for error codes). Currently, always saves images
291// in 24 bit format. XXX
292//
293int SavePCX(wxImage *image, wxOutputStream& stream)
294{
295 unsigned char hdr[128]; // PCX header
0848b0dd 296 unsigned char pal[768]; // palette for 8 bit images
528dad23
GRG
297 unsigned char *p; // space to store one scanline
298 unsigned char *src; // pointer into wxImage data
299 unsigned int width, height; // size of the image
300 unsigned int bytesperline; // bytes per line (each plane)
d32548aa
GRG
301 int nplanes = 3; // number of planes
302 int format = wxPCX_24BIT; // image format (8 bit, 24 bit)
0848b0dd 303 wxHashTable h(wxKEY_INTEGER); // image histogram
0848b0dd 304 unsigned long key; // key in the hashtable
528dad23
GRG
305 unsigned int i;
306
d32548aa 307 // See if we can save as 8 bit.
0848b0dd 308 //
d32548aa 309 if (image->CountColours(256) <= 256)
528dad23 310 {
faa7a70e 311 image->ComputeHistogram(h);
528dad23
GRG
312 format = wxPCX_8BIT;
313 nplanes = 1;
314 }
528dad23
GRG
315
316 // Get image dimensions, calculate bytesperline (must be even,
317 // according to PCX specs) and allocate space for one complete
318 // scanline.
319 //
320 if (!image->Ok())
321 return wxPCX_INVFORMAT;
322
323 width = image->GetWidth();
324 height = image->GetHeight();
528dad23
GRG
325 bytesperline = width;
326 if (bytesperline % 2)
327 bytesperline++;
328
329 if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL)
330 return wxPCX_MEMERR;
331
332 // Build header data and write it to the stream. Initially,
333 // set all bytes to zero (most values default to zero).
334 //
335 memset(hdr, 0, sizeof(hdr));
336
337 hdr[HDR_MANUFACTURER] = 10;
338 hdr[HDR_VERSION] = 5;
339 hdr[HDR_ENCODING] = 1;
340 hdr[HDR_NPLANES] = nplanes;
341 hdr[HDR_BITSPERPIXEL] = 8;
342 hdr[HDR_BYTESPERLINE] = bytesperline % 256;
343 hdr[HDR_BYTESPERLINE + 1] = bytesperline / 256;
344 hdr[HDR_XMAX] = (width - 1) % 256;
345 hdr[HDR_XMAX + 1] = (width - 1) / 256;
346 hdr[HDR_YMAX] = (height - 1) % 256;
347 hdr[HDR_YMAX + 1] = (height - 1) / 256;
348 hdr[HDR_PALETTEINFO] = 1;
349
350 stream.Write(hdr, 128);
351
352 // Encode image data line by line and write it to the stream
353 //
354 src = image->GetData();
355
356 for (; height; height--)
357 {
358 switch (format)
359 {
360 case wxPCX_8BIT:
528dad23 361 {
0848b0dd
GRG
362 unsigned char r, g, b;
363 wxHNode *hnode;
364
528dad23
GRG
365 for (i = 0; i < width; i++)
366 {
0848b0dd
GRG
367 r = *(src++);
368 g = *(src++);
369 b = *(src++);
370 key = (r << 16) | (g << 8) | b;
371
372 hnode = (wxHNode *) h.Get(key);
faa7a70e 373 p[i] = hnode->index;
528dad23
GRG
374 }
375 break;
376 }
528dad23
GRG
377 case wxPCX_24BIT:
378 {
379 for (i = 0; i < width; i++)
380 {
381 p[i] = *(src++);
382 p[i + bytesperline] = *(src++);
383 p[i + 2 * bytesperline] = *(src++);
384 }
385 break;
386 }
387 }
d32548aa 388
528dad23
GRG
389 RLEencode(p, bytesperline * nplanes, stream);
390 }
391
392 free(p);
393
0848b0dd
GRG
394 // For 8 bit images, build the palette and write it to the stream
395 //
528dad23
GRG
396 if (format == wxPCX_8BIT)
397 {
0848b0dd
GRG
398 wxNode *node;
399 wxHNode *hnode;
400
401 // zero unused colours
402 memset(pal, 0, sizeof(pal));
403
404 h.BeginFind();
405 while ((node = h.Next()) != NULL)
406 {
407 key = node->GetKeyInteger();
408 hnode = (wxHNode *) node->GetData();
409
faa7a70e
GRG
410 pal[3 * hnode->index] = (unsigned char)(key >> 16);
411 pal[3 * hnode->index + 1] = (unsigned char)(key >> 8);
412 pal[3 * hnode->index + 2] = (unsigned char)(key);
413 delete hnode;
0848b0dd
GRG
414 }
415
528dad23 416 stream.PutC(12);
0848b0dd 417 stream.Write(pal, 768);
528dad23 418 }
528dad23
GRG
419
420 return wxPCX_OK;
421}
422
56c66755
GRG
423//-----------------------------------------------------------------------------
424// wxPCXHandler
425//-----------------------------------------------------------------------------
426
56c66755 427IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler)
56c66755 428
700ec454 429bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
56c66755 430{
4df78dc3
GRG
431 int error;
432
433 if (!CanRead(stream))
434 {
435 if (verbose)
58c837a4 436 wxLogError(_("PCX: this is not a PCX file."));
4df78dc3
GRG
437
438 return FALSE;
439 }
440
56c66755
GRG
441 image->Destroy();
442
e4b8154a 443 if ((error = ReadPCX(image, stream)) != wxPCX_OK)
4df78dc3
GRG
444 {
445 if (verbose)
446 {
447 switch (error)
448 {
e4b8154a
GRG
449 case wxPCX_INVFORMAT: wxLogError(_("wxPCXHandler: image format unsupported")); break;
450 case wxPCX_MEMERR: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
451 case wxPCX_VERERR: wxLogError(_("wxPCXHandler: version number too low")); break;
452 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
4df78dc3
GRG
453 }
454 }
455 image->Destroy();
456 return FALSE;
457 }
56c66755 458
4df78dc3 459 return TRUE;
56c66755
GRG
460}
461
528dad23 462bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
56c66755 463{
528dad23
GRG
464 int error;
465
466 if ((error = SavePCX(image, stream)) != wxPCX_OK)
467 {
468 if (verbose)
469 {
470 switch (error)
471 {
472 case wxPCX_INVFORMAT: wxLogError(_("wxPCXHandler: invalid image")); break;
473 case wxPCX_MEMERR: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
474 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
475 }
476 }
477 }
56c66755 478
528dad23 479 return (error == wxPCX_OK);
56c66755
GRG
480}
481
995612e2 482bool wxPCXHandler::DoCanRead( wxInputStream& stream )
56c66755 483{
4df78dc3 484 unsigned char c;
4df78dc3 485
4df78dc3 486 c = stream.GetC();
a05da1b6 487 stream.SeekI(-1, wxFromCurrent);
4df78dc3
GRG
488
489 // not very safe, but this is all we can get from PCX header :-(
490 return (c == 10);
56c66755
GRG
491}
492
b9b32d5c 493#endif // wxUSE_STREAMS && wxUSE_PCX
56c66755 494