]>
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"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 void RLEencode(unsigned char *p
, unsigned int size
, wxOutputStream
& s
)
41 unsigned int data
, last
, cont
;
43 // Write 'size' bytes. The PCX official specs say there will be
44 // a decoding break at the end of each scanline, so in order to
45 // force this decoding break use this function to write, at most,
46 // _one_ complete scanline at a time.
48 last
= (unsigned char) *(p
++);
54 data
= (unsigned char) *(p
++);
56 // Up to 63 bytes with the same value can be stored using a
57 // single { cont, value } pair.
59 if ((data
== last
) && (cont
< 63))
65 // Need to write a 'counter' byte?
67 if ((cont
> 1) || ((last
& 0xC0) == 0xC0))
68 s
.PutC((char) (cont
| 0xC0));
77 // Write the last one and return;
79 if ((cont
> 1) || ((last
& 0xC0) == 0xC0))
80 s
.PutC((char) (cont
| 0xC0));
85 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
87 unsigned int i
, data
, cont
;
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.
97 data
= (unsigned char)s
.GetC();
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.
103 if ((data
& 0xC0) != 0xC0)
111 data
= (unsigned char)s
.GetC();
112 for (i
= 1; i
<= cont
; i
++)
121 #define HDR_MANUFACTURER 0
122 #define HDR_VERSION 1
123 #define HDR_ENCODING 2
124 #define HDR_BITSPERPIXEL 3
129 #define HDR_NPLANES 65
130 #define HDR_BYTESPERLINE 66
131 #define HDR_PALETTEINFO 68
135 wxPCX_8BIT
, // 8 bpp, 1 plane (8 bit)
136 wxPCX_24BIT
// 8 bpp, 3 planes (24 bit)
141 wxPCX_OK
= 0, // everything was OK
142 wxPCX_INVFORMAT
= 1, // error in pcx file format
143 wxPCX_MEMERR
= 2, // error allocating memory
144 wxPCX_VERERR
= 3 // error in pcx version number
148 // Loads a PCX file into the wxImage object pointed by image.
149 // Returns wxPCX_OK on success, or an error code otherwise
150 // (see above for error codes)
152 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
154 unsigned char hdr
[128]; // PCX header
155 unsigned char pal
[768]; // palette for 8 bit images
156 unsigned char *p
; // space to store one scanline
157 unsigned char *dst
; // pointer into wxImage data
158 unsigned int width
, height
; // size of the image
159 unsigned int bytesperline
; // bytes per line (each plane)
160 int bitsperpixel
; // bits per pixel (each plane)
161 int nplanes
; // number of planes
162 int encoding
; // is the image RLE encoded?
163 int format
; // image format (8 bit, 24 bit)
167 // Read PCX header and check the version number (it must
168 // be at least 5 or higher for 8 bit and 24 bit images).
170 stream
.Read(hdr
, 128);
172 if (hdr
[HDR_VERSION
] < 5) return wxPCX_VERERR
;
174 // Extract all image info from the PCX header.
176 encoding
= hdr
[HDR_ENCODING
];
177 nplanes
= hdr
[HDR_NPLANES
];
178 bitsperpixel
= hdr
[HDR_BITSPERPIXEL
];
179 bytesperline
= hdr
[HDR_BYTESPERLINE
] + 256 * hdr
[HDR_BYTESPERLINE
+ 1];
180 width
= (hdr
[HDR_XMAX
] + 256 * hdr
[HDR_XMAX
+ 1]) -
181 (hdr
[HDR_XMIN
] + 256 * hdr
[HDR_XMIN
+ 1]) + 1;
182 height
= (hdr
[HDR_YMAX
] + 256 * hdr
[HDR_YMAX
+ 1]) -
183 (hdr
[HDR_YMIN
] + 256 * hdr
[HDR_YMIN
+ 1]) + 1;
185 // Check image format. Currently supported formats are
186 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
188 if ((nplanes
== 3) && (bitsperpixel
== 8))
189 format
= wxPCX_24BIT
;
190 else if ((nplanes
== 1) && (bitsperpixel
== 8))
193 return wxPCX_INVFORMAT
;
195 // If the image is of type wxPCX_8BIT, then there is a
196 // palette at the end of the file. Read it now before
199 if (format
== wxPCX_8BIT
)
201 pos
= stream
.TellI();
202 stream
.SeekI(-769, wxFromEnd
);
204 if (stream
.GetC() != 12)
205 return wxPCX_INVFORMAT
;
207 stream
.Read(pal
, 768);
208 stream
.SeekI(pos
, wxFromStart
);
211 // Allocate memory for a scanline and resize the image.
213 image
->Create(width
, height
);
218 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
221 // Now start reading the file, line by line, and store
222 // the data in the format required by wxImage.
224 dst
= image
->GetData();
226 for (; height
; height
--)
229 RLEdecode(p
, bytesperline
* nplanes
, stream
);
231 stream
.Read(p
, bytesperline
* nplanes
);
237 for (i
= 0; i
< width
; i
++)
239 *(dst
++) = pal
[ 3 * (p
[i
]) ];
240 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
241 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
247 for (i
= 0; i
< width
; i
++)
250 *(dst
++) = p
[i
+ bytesperline
];
251 *(dst
++) = p
[i
+ 2 * bytesperline
];
264 // Saves a PCX file into the wxImage object pointed by image.
265 // Returns wxPCX_OK on success, or an error code otherwise
266 // (see above for error codes). Currently, always saves images
267 // in 24 bit format. XXX
269 int SavePCX(wxImage
*image
, wxOutputStream
& stream
)
271 unsigned char hdr
[128]; // PCX header
272 unsigned char *p
; // space to store one scanline
273 unsigned char *src
; // pointer into wxImage data
274 unsigned int width
, height
; // size of the image
275 unsigned int bytesperline
; // bytes per line (each plane)
276 int nplanes
= 3; // number of planes
277 int format
= wxPCX_24BIT
; // image format (8 bit, 24 bit)
282 if (num_of_colors <= 256)
291 // Get image dimensions, calculate bytesperline (must be even,
292 // according to PCX specs) and allocate space for one complete
296 return wxPCX_INVFORMAT
;
298 width
= image
->GetWidth();
299 height
= image
->GetHeight();
300 nplanes
= 3; /* 1 for wxPCX_8BIT */
301 bytesperline
= width
;
302 if (bytesperline
% 2)
305 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
308 // Build header data and write it to the stream. Initially,
309 // set all bytes to zero (most values default to zero).
311 memset(hdr
, 0, sizeof(hdr
));
313 hdr
[HDR_MANUFACTURER
] = 10;
314 hdr
[HDR_VERSION
] = 5;
315 hdr
[HDR_ENCODING
] = 1;
316 hdr
[HDR_NPLANES
] = nplanes
;
317 hdr
[HDR_BITSPERPIXEL
] = 8;
318 hdr
[HDR_BYTESPERLINE
] = bytesperline
% 256;
319 hdr
[HDR_BYTESPERLINE
+ 1] = bytesperline
/ 256;
320 hdr
[HDR_XMAX
] = (width
- 1) % 256;
321 hdr
[HDR_XMAX
+ 1] = (width
- 1) / 256;
322 hdr
[HDR_YMAX
] = (height
- 1) % 256;
323 hdr
[HDR_YMAX
+ 1] = (height
- 1) / 256;
324 hdr
[HDR_PALETTEINFO
] = 1;
326 stream
.Write(hdr
, 128);
328 // Encode image data line by line and write it to the stream
330 src
= image
->GetData();
332 for (; height
; height
--)
339 for (i = 0; i < width; i++)
341 hash = *(src++) << 24 +
345 p[i] = palette_lookup(hash);
352 for (i
= 0; i
< width
; i
++)
355 p
[i
+ bytesperline
] = *(src
++);
356 p
[i
+ 2 * bytesperline
] = *(src
++);
362 RLEencode(p
, bytesperline
* nplanes
, stream
);
368 if (format == wxPCX_8BIT)
379 //-----------------------------------------------------------------------------
381 //-----------------------------------------------------------------------------
383 #if !USE_SHARED_LIBRARIES
384 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
387 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
391 if (!CanRead(stream
))
394 wxLogError(_("PCX: this is not a PCX file."));
401 if ((error
= ReadPCX(image
, stream
)) != wxPCX_OK
)
407 case wxPCX_INVFORMAT
: wxLogError(_("wxPCXHandler: image format unsupported")); break;
408 case wxPCX_MEMERR
: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
409 case wxPCX_VERERR
: wxLogError(_("wxPCXHandler: version number too low")); break;
410 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
420 bool wxPCXHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
424 if ((error
= SavePCX(image
, stream
)) != wxPCX_OK
)
430 case wxPCX_INVFORMAT
: wxLogError(_("wxPCXHandler: invalid image")); break;
431 case wxPCX_MEMERR
: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
432 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
437 return (error
== wxPCX_OK
);
440 bool wxPCXHandler::DoCanRead( wxInputStream
& stream
)
445 stream
.SeekI(-1, wxFromCurrent
);
447 // not very safe, but this is all we can get from PCX header :-(
451 #endif // wxUSE_STREAMS && wxUSE_PCX