]>
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 *WXUNUSED(p
), unsigned int WXUNUSED(size
), wxOutputStream
& WXUNUSED(s
))
43 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
45 unsigned int i
, data
, cont
;
47 // Read 'size' bytes. The PCX official specs say there will be
48 // a decoding break at the end of each scanline (but not at the
49 // end of each plane inside a scanline). Only use this function
50 // to read one or more _complete_ scanlines. Else, more than
51 // 'size' bytes might be read and the buffer might overflow.
55 data
= (unsigned char)s
.GetC();
57 // If ((data & 0xC0) != 0xC0), then the value read is a data
58 // byte. Else, it is a counter (cont = val & 0x3F) and the
59 // next byte is the data byte.
61 if ((data
& 0xC0) != 0xC0)
69 data
= (unsigned char)s
.GetC();
70 for (i
= 1; i
<= cont
; i
++)
80 #define HDR_ENCODING 2
81 #define HDR_BITSPERPIXEL 3
86 #define HDR_NPLANES 65
87 #define HDR_BYTESPERLINE 66
91 wxPCX_8BIT
, // 8 bpp, 1 plane (8 bit)
92 wxPCX_24BIT
// 8 bpp, 3 planes (24 bit)
97 wxPCX_OK
= 0, // everything was OK
98 wxPCX_INVFORMAT
= 1, // error in pcx file format
99 wxPCX_MEMERR
= 2, // error allocating memory
100 wxPCX_VERERR
= 3 // error in pcx version number
104 // Loads a PCX file into the wxImage object pointed by image.
105 // Returns wxPCX_OK on success, or an error code otherwise
106 // (see above for error codes)
108 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
110 unsigned char hdr
[128]; // PCX header
111 unsigned char pal
[768]; // palette for 8 bit images
112 unsigned char *p
; // space to store one scanline
113 unsigned char *dst
; // pointer into wxImage data
114 unsigned int width
, height
; // size of the image
115 unsigned int bytesperline
; // bytes per line (each plane)
116 int bitsperpixel
; // bits per pixel (each plane)
117 int nplanes
; // number of planes
118 int encoding
; // is the image RLE encoded?
119 int format
; // image format (8 bit, 24 bit)
123 // Read PCX header and check the version number (it must
124 // be at least 5 or higher for 8 bit and 24 bit images).
126 stream
.Read(hdr
, 128);
128 if (hdr
[HDR_VERSION
] < 5) return wxPCX_VERERR
;
130 // Extract all image info from the PCX header.
132 encoding
= hdr
[HDR_ENCODING
];
133 nplanes
= hdr
[HDR_NPLANES
];
134 bitsperpixel
= hdr
[HDR_BITSPERPIXEL
];
135 bytesperline
= hdr
[HDR_BYTESPERLINE
] + 256 * hdr
[HDR_BYTESPERLINE
+ 1];
136 width
= (hdr
[HDR_XMAX
] + 256 * hdr
[HDR_XMAX
+ 1]) -
137 (hdr
[HDR_XMIN
] + 256 * hdr
[HDR_XMIN
+ 1]) + 1;
138 height
= (hdr
[HDR_YMAX
] + 256 * hdr
[HDR_YMAX
+ 1]) -
139 (hdr
[HDR_YMIN
] + 256 * hdr
[HDR_YMIN
+ 1]) + 1;
141 // Check image format. Currently supported formats are
142 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
144 if ((nplanes
== 3) && (bitsperpixel
== 8))
145 format
= wxPCX_24BIT
;
146 else if ((nplanes
== 1) && (bitsperpixel
== 8))
149 return wxPCX_INVFORMAT
;
151 // If the image is of type wxPCX_8BIT, then there is a
152 // palette at the end of the file. Read it now before
155 if (format
== wxPCX_8BIT
)
157 pos
= stream
.TellI();
158 stream
.SeekI(-769, wxFromEnd
);
160 if (stream
.GetC() != 12)
161 return wxPCX_INVFORMAT
;
163 stream
.Read(pal
, 768);
164 stream
.SeekI(pos
, wxFromStart
);
167 // Allocate memory for a scanline and resize the image.
169 image
->Create(width
, height
);
174 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
177 // Now start reading the file, line by line, and store
178 // the data in the format required by wxImage.
180 dst
= image
->GetData();
182 for (; height
; height
--)
185 RLEdecode(p
, bytesperline
* nplanes
, stream
);
187 stream
.Read(p
, bytesperline
* nplanes
);
193 for (i
= 0; i
< width
; i
++)
195 *(dst
++) = pal
[ 3 * (p
[i
]) ];
196 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
197 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
203 for (i
= 0; i
< width
; i
++)
206 *(dst
++) = p
[i
+ bytesperline
];
207 *(dst
++) = p
[i
+ 2 * bytesperline
];
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
224 #if !USE_SHARED_LIBRARIES
225 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
228 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
232 if (!CanRead(stream
))
235 wxLogError(_("PCX: this is not a PCX file."));
242 if ((error
= ReadPCX(image
, stream
)) != wxPCX_OK
)
248 case wxPCX_INVFORMAT
: wxLogError(_("wxPCXHandler: image format unsupported")); break;
249 case wxPCX_MEMERR
: wxLogError(_("wxPCXHandler: couldn't allocate memory")); break;
250 case wxPCX_VERERR
: wxLogError(_("wxPCXHandler: version number too low")); break;
251 default: wxLogError(_("wxPCXHandler: unknown error !!!"));
261 bool wxPCXHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool verbose
)
263 wxFAIL_MSG(wxT("wxPCXHandler::SaveFile still not implemented"));
268 bool wxPCXHandler::DoCanRead( wxInputStream
& stream
)
273 stream
.SeekI(-1, wxFromCurrent
);
275 // not very safe, but this is all we can get from PCX header :-(
279 #endif // wxUSE_STREAMS && wxUSE_PCX