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