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