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