]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpcx.cpp
Added #include "wx/setup.h" (or "wx/defs.h") before checking for
[wxWidgets.git] / src / common / imagpcx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagpcx.cpp
3 // Purpose: wxImage PCX handler
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 // Version: 1.00
6 // CVS-ID: $Id$
7 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 /*
12 We don't put pragma implement in this file because it is already present in
13 src/common/image.cpp
14 */
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 # include "wx/defs.h"
25 #endif
26
27 #if wxUSE_STREAMS && wxUSE_PCX
28
29 #include "wx/image.h"
30 #include "wx/wfstream.h"
31 #include "wx/module.h"
32 #include "wx/log.h"
33
34
35 //-----------------------------------------------------------------------------
36 // PCX decoding
37 //-----------------------------------------------------------------------------
38
39 void RLEencode(unsigned char *p, unsigned int size, wxOutputStream& s)
40 {
41 }
42
43 void RLEdecode(unsigned char *p, unsigned int size, wxInputStream& s)
44 {
45 unsigned int i, data, cont;
46
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.
52 //
53 while (size > 0)
54 {
55 data = (unsigned char)s.GetC();
56
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.
60 //
61 if ((data & 0xC0) != 0xC0)
62 {
63 *(p++) = data;
64 size--;
65 }
66 else
67 {
68 cont = data & 0x3F;
69 data = (unsigned char)s.GetC();
70 for (i = 1; i <= cont; i++)
71 *(p++) = data;
72 size -= cont;
73 }
74 }
75 }
76
77
78 /* PCX header */
79 #define HDR_VERSION 1
80 #define HDR_ENCODING 2
81 #define HDR_BITSPERPIXEL 3
82 #define HDR_XMIN 4
83 #define HDR_YMIN 6
84 #define HDR_XMAX 8
85 #define HDR_YMAX 10
86 #define HDR_NPLANES 65
87 #define HDR_BYTESPERLINE 66
88
89 /* image formats */
90 #define IMAGE_8BIT 0 // 8 bpp, 1 plane (8 bit)
91 #define IMAGE_24BIT 1 // 8 bpp, 3 planes (24 bit)
92
93 /* error codes */
94 #define E_OK 0 // everything was OK
95 #define E_FORMATO 1 // error in pcx file format
96 #define E_MEMORIA 2 // error allocating memory
97 #define E_VERSION 3 // error in pcx version number
98
99
100 // ReadPCX:
101 // Loads a PCX file into the wxImage object pointed by image.
102 // Returns E_OK on success, or an error code otherwise (see
103 // above for error codes)
104 //
105 int ReadPCX(wxImage *image, wxInputStream& stream)
106 {
107 unsigned char hdr[128]; // PCX header
108 unsigned char pal[768]; // palette for 8 bit images
109 unsigned char *p; // space to store one scanline
110 unsigned char *dst; // pointer into wxImage data
111 unsigned int width, height; // size of the image
112 unsigned int bytesperline; // bytes per line (each plane)
113 int bitsperpixel; // bits per pixel (each plane)
114 int nplanes; // number of planes
115 int encoding; // is the image RLE encoded?
116 int format; // image format (8 bit, 24 bit)
117 unsigned int i;
118 off_t pos;
119
120 // Read PCX header and check the version number (it must
121 // be at least 5 or higher for 8 bit and 24 bit images).
122 //
123 stream.Read(hdr, 128);
124
125 if (hdr[HDR_VERSION] < 5) return E_VERSION;
126
127 // Extract all image info from the PCX header.
128 //
129 encoding = hdr[HDR_ENCODING];
130 nplanes = hdr[HDR_NPLANES];
131 bitsperpixel = hdr[HDR_BITSPERPIXEL];
132 bytesperline = hdr[HDR_BYTESPERLINE] + 256 * hdr[HDR_BYTESPERLINE + 1];
133 width = (hdr[HDR_XMAX] + 256 * hdr[HDR_XMAX + 1]) -
134 (hdr[HDR_XMIN] + 256 * hdr[HDR_XMIN + 1]) + 1;
135 height = (hdr[HDR_YMAX] + 256 * hdr[HDR_YMAX + 1]) -
136 (hdr[HDR_YMIN] + 256 * hdr[HDR_YMIN + 1]) + 1;
137
138 // Check image format. Currently supported formats are
139 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
140 //
141 if ((nplanes == 3) && (bitsperpixel == 8))
142 format = IMAGE_24BIT;
143 else if ((nplanes == 1) && (bitsperpixel == 8))
144 format = IMAGE_8BIT;
145 else
146 return E_FORMATO;
147
148 // If the image is of type IMAGE_8BIT, then there is a
149 // palette at the end of the file. Read it now before
150 // proceeding.
151 //
152 if (format == IMAGE_8BIT)
153 {
154 pos = stream.TellI();
155 stream.SeekI(-769, wxFromEnd);
156
157 if (stream.GetC() != 12)
158 return E_FORMATO;
159
160 stream.Read(pal, 768);
161 stream.SeekI(pos, wxFromStart);
162 }
163
164 // Allocate memory for a scanline and resize the image.
165 //
166 image->Create(width, height);
167
168 if (!image->Ok())
169 return E_MEMORIA;
170
171 if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL)
172 return E_MEMORIA;
173
174 // Now start reading the file, line by line, and store
175 // the data in the format required by wxImage.
176 //
177 dst = image->GetData();
178
179 for (; height; height--)
180 {
181 if (encoding)
182 RLEdecode(p, bytesperline * nplanes, stream);
183 else
184 stream.Read(p, bytesperline * nplanes);
185
186 switch (format)
187 {
188 case IMAGE_8BIT:
189 {
190 for (i = 0; i < width; i++)
191 {
192 *(dst++) = pal[ 3 * (p[i]) ];
193 *(dst++) = pal[ 3 * (p[i]) + 1];
194 *(dst++) = pal[ 3 * (p[i]) + 2];
195 }
196 break;
197 }
198 case IMAGE_24BIT:
199 {
200 for (i = 0; i < width; i++)
201 {
202 *(dst++) = p[i];
203 *(dst++) = p[i + bytesperline];
204 *(dst++) = p[i + 2 * bytesperline];
205 }
206 break;
207 }
208 }
209 }
210
211 free(p);
212
213 return E_OK;
214 }
215
216
217 //-----------------------------------------------------------------------------
218 // wxPCXHandler
219 //-----------------------------------------------------------------------------
220
221 #if !USE_SHARED_LIBRARIES
222 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler)
223 #endif
224
225 bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose )
226 {
227 int error;
228
229 if (!CanRead(stream))
230 {
231 if (verbose)
232 wxLogError(_T("wxPCXHandler: this is not a PCX file"));
233
234 return FALSE;
235 }
236
237 image->Destroy();
238
239 if ((error = ReadPCX(image, stream)) != E_OK)
240 {
241 if (verbose)
242 {
243 switch (error)
244 {
245 case E_FORMATO: wxLogError(_T("wxPCXHandler: image format unsupported")); break;
246 case E_MEMORIA: wxLogError(_T("wxPCXHandler: couldn't allocate memory")); break;
247 case E_VERSION: wxLogError(_T("wxPCXHandler: version number too low")); break;
248 default: wxLogError(_T("wxPCXHandler: unknown error !!!"));
249 }
250 }
251 image->Destroy();
252 return FALSE;
253 }
254
255 return TRUE;
256 }
257
258 bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
259 {
260 if (verbose)
261 wxLogError(_T("wxPCXHandler::SaveFile still not implemented"));
262
263 return FALSE;
264 }
265
266 bool wxPCXHandler::CanRead( wxInputStream& stream )
267 {
268 unsigned char c;
269 off_t pos;
270
271 pos = stream.TellI();
272 stream.SeekI(0, wxFromStart);
273 c = stream.GetC();
274 stream.SeekI(pos, wxFromStart);
275
276 // not very safe, but this is all we can get from PCX header :-(
277 return (c == 10);
278 }
279
280 #endif // wxUSE_STREAMS && wxUSE_PCX
281