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