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