]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagpcx.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage PCX handler
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
7 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 We don't put pragma implement in this file because it is already present in
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_STREAMS && wxUSE_PCX
30 #include "wx/wfstream.h"
31 #include "wx/module.h"
37 #include "wx/object.h"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 void RLEencode(unsigned char *p
, unsigned int size
, wxOutputStream
& s
)
45 unsigned int data
, last
, cont
;
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.
52 last
= (unsigned char) *(p
++);
58 data
= (unsigned char) *(p
++);
60 // Up to 63 bytes with the same value can be stored using a
61 // single { cont, value } pair.
63 if ((data
== last
) && (cont
< 63))
69 // Need to write a 'counter' byte?
71 if ((cont
> 1) || ((last
& 0xC0) == 0xC0))
72 s
.PutC((char) (cont
| 0xC0));
81 // Write the last one and return;
83 if ((cont
> 1) || ((last
& 0xC0) == 0xC0))
84 s
.PutC((char) (cont
| 0xC0));
89 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
91 unsigned int i
, data
, cont
;
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.
101 data
= (unsigned char)s
.GetC();
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.
107 if ((data
& 0xC0) != 0xC0)
115 data
= (unsigned char)s
.GetC();
116 for (i
= 1; i
<= cont
; i
++)
125 #define HDR_MANUFACTURER 0
126 #define HDR_VERSION 1
127 #define HDR_ENCODING 2
128 #define HDR_BITSPERPIXEL 3
133 #define HDR_NPLANES 65
134 #define HDR_BYTESPERLINE 66
135 #define HDR_PALETTEINFO 68
139 wxPCX_8BIT
, // 8 bpp, 1 plane (8 bit)
140 wxPCX_24BIT
// 8 bpp, 3 planes (24 bit)
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
152 // Loads a PCX file into the wxImage object pointed by image.
153 // Returns wxPCX_OK on success, or an error code otherwise
154 // (see above for error codes)
156 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
158 unsigned char hdr
[128]; // PCX header
159 unsigned char pal
[768]; // palette for 8 bit images
160 unsigned char *p
; // space to store one scanline
161 unsigned char *dst
; // pointer into wxImage data
162 unsigned int width
, height
; // size of the image
163 unsigned int bytesperline
; // bytes per line (each plane)
164 int bitsperpixel
; // bits per pixel (each plane)
165 int nplanes
; // number of planes
166 int encoding
; // is the image RLE encoded?
167 int format
; // image format (8 bit, 24 bit)
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).
174 stream
.Read(hdr
, 128);
176 if (hdr
[HDR_VERSION
] < 5) return wxPCX_VERERR
;
178 // Extract all image info from the PCX header.
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;
189 // Check image format. Currently supported formats are
190 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
192 if ((nplanes
== 3) && (bitsperpixel
== 8))
193 format
= wxPCX_24BIT
;
194 else if ((nplanes
== 1) && (bitsperpixel
== 8))
197 return wxPCX_INVFORMAT
;
199 // If the image is of type wxPCX_8BIT, then there is a
200 // palette at the end of the file. Read it now before
203 if (format
== wxPCX_8BIT
)
205 pos
= stream
.TellI();
206 stream
.SeekI(-769, wxFromEnd
);
208 if (stream
.GetC() != 12)
209 return wxPCX_INVFORMAT
;
211 stream
.Read(pal
, 768);
212 stream
.SeekI(pos
, wxFromStart
);
215 // Allocate memory for a scanline and resize the image.
217 image
->Create(width
, height
);
222 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
225 // Now start reading the file, line by line, and store
226 // the data in the format required by wxImage.
228 dst
= image
->GetData();
230 for (; height
; height
--)
233 RLEdecode(p
, bytesperline
* nplanes
, stream
);
235 stream
.Read(p
, bytesperline
* nplanes
);
241 for (i
= 0; i
< width
; i
++)
243 *(dst
++) = pal
[ 3 * (p
[i
]) ];
244 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
245 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
251 for (i
= 0; i
< width
; i
++)
254 *(dst
++) = p
[i
+ bytesperline
];
255 *(dst
++) = p
[i
+ 2 * bytesperline
];
268 // Saves a PCX file into the wxImage object pointed by image.
269 // Returns wxPCX_OK on success, or an error code otherwise
270 // (see above for error codes). Currently, always saves images
271 // in 24 bit format. XXX
273 int SavePCX(wxImage
*image
, wxOutputStream
& stream
)
275 unsigned char hdr
[128]; // PCX header
276 unsigned char pal
[768]; // palette for 8 bit images
277 unsigned char *p
; // space to store one scanline
278 unsigned char *src
; // pointer into wxImage data
279 unsigned int width
, height
; // size of the image
280 unsigned int bytesperline
; // bytes per line (each plane)
281 int nplanes
; // number of planes
282 int format
; // image format (8 bit, 24 bit)
283 wxHashTable
h(wxKEY_INTEGER
); // image histogram
284 unsigned long ncolours
; // num. of different colours
285 unsigned long key
; // key in the hashtable
288 // Get the histogram of the image, and decide whether to save
289 // as 8 bit or 24 bit, according to the number of colours.
291 ncolours
= image
->ComputeHistogram(h
);
300 format
= wxPCX_24BIT
;
304 // Get image dimensions, calculate bytesperline (must be even,
305 // according to PCX specs) and allocate space for one complete
309 return wxPCX_INVFORMAT
;
311 width
= image
->GetWidth();
312 height
= image
->GetHeight();
313 bytesperline
= width
;
314 if (bytesperline
% 2)
317 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
320 // Build header data and write it to the stream. Initially,
321 // set all bytes to zero (most values default to zero).
323 memset(hdr
, 0, sizeof(hdr
));
325 hdr
[HDR_MANUFACTURER
] = 10;
326 hdr
[HDR_VERSION
] = 5;
327 hdr
[HDR_ENCODING
] = 1;
328 hdr
[HDR_NPLANES
] = nplanes
;
329 hdr
[HDR_BITSPERPIXEL
] = 8;
330 hdr
[HDR_BYTESPERLINE
] = bytesperline
% 256;
331 hdr
[HDR_BYTESPERLINE
+ 1] = bytesperline
/ 256;
332 hdr
[HDR_XMAX
] = (width
- 1) % 256;
333 hdr
[HDR_XMAX
+ 1] = (width
- 1) / 256;
334 hdr
[HDR_YMAX
] = (height
- 1) % 256;
335 hdr
[HDR_YMAX
+ 1] = (height
- 1) / 256;
336 hdr
[HDR_PALETTEINFO
] = 1;
338 stream
.Write(hdr
, 128);
340 // Encode image data line by line and write it to the stream
342 src
= image
->GetData();
344 for (; height
; height
--)
350 unsigned char r
, g
, b
;
353 for (i
= 0; i
< width
; i
++)
358 key
= (r
<< 16) | (g
<< 8) | b
;
360 hnode
= (wxHNode
*) h
.Get(key
);
362 wxLogError("!hnode");
370 for (i
= 0; i
< width
; i
++)
373 p
[i
+ bytesperline
] = *(src
++);
374 p
[i
+ 2 * bytesperline
] = *(src
++);
380 RLEencode(p
, bytesperline
* nplanes
, stream
);
385 // For 8 bit images, build the palette and write it to the stream
387 if (format
== wxPCX_8BIT
)
392 // zero unused colours
393 memset(pal
, 0, sizeof(pal
));
396 while ((node
= h
.Next()) != NULL
)
398 key
= node
->GetKeyInteger();
399 hnode
= (wxHNode
*) node
->GetData();
402 wxLogError("!hnode");
405 pal
[3 * hnode
->index
] = (unsigned char)(key
>> 16);
406 pal
[3 * hnode
->index
+ 1] = (unsigned char)(key
>> 8);
407 pal
[3 * hnode
->index
+ 2] = (unsigned char)(key
);
413 stream
.Write(pal
, 768);
419 //-----------------------------------------------------------------------------
421 //-----------------------------------------------------------------------------
423 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
425 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
429 if (!CanRead(stream
))
432 wxLogError(_("PCX: this is not a PCX file."));
439 if ((error
= ReadPCX(image
, stream
)) != wxPCX_OK
)
445 case wxPCX_INVFORMAT
: wxLogError(_("wxPCXHandler: image format unsupported")); break;
446 case wxPCX_MEMERR
: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
447 case wxPCX_VERERR
: wxLogError(_("wxPCXHandler: version number too low")); break;
448 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
458 bool wxPCXHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
462 if ((error
= SavePCX(image
, stream
)) != wxPCX_OK
)
468 case wxPCX_INVFORMAT
: wxLogError(_("wxPCXHandler: invalid image")); break;
469 case wxPCX_MEMERR
: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
470 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
475 return (error
== wxPCX_OK
);
478 bool wxPCXHandler::DoCanRead( wxInputStream
& stream
)
483 stream
.SeekI(-1, wxFromCurrent
);
485 // not very safe, but this is all we can get from PCX header :-(
489 #endif // wxUSE_STREAMS && wxUSE_PCX