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