]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpcx.cpp
PCX handler now working for reading (8bit and 24bit images).
[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 pos = stream.TellI();
146 stream.SeekI(-769, wxFromEnd);
147
148 if (stream.GetC() != 12)
149 return E_FORMATO;
150
151 stream.Read(pal, 768);
152 stream.SeekI(pos, wxFromStart);
153
154 // Allocate memory for a scanline and resize the image.
155 //
156 image->Create(width, height);
157
158 if (!image->Ok())
159 return E_MEMORIA;
160
161 if ((p = (unsigned char *) malloc(bytesperline * nplanes)) == NULL)
162 return E_MEMORIA;
163
164 // Now start reading the file, line by line, and store
165 // the data in the format required by wxImage.
166 //
167 dst = image->GetData();
168
169 for (; height; height--)
170 {
171 if (encoding)
172 RLEdecode(p, bytesperline * nplanes, stream);
173 else
174 stream.Read(p, bytesperline * nplanes);
175
176 switch (format)
177 {
178 case IMAGE_8BIT:
179 {
180 for (i = 0; i < width; i++)
181 {
182 *(dst++) = pal[ 3 * (p[i]) ];
183 *(dst++) = pal[ 3 * (p[i]) + 1];
184 *(dst++) = pal[ 3 * (p[i]) + 2];
185 }
186 break;
187 }
188 case IMAGE_24BIT:
189 {
190 for (i = 0; i < width; i++)
191 {
192 *(dst++) = p[i];
193 *(dst++) = p[i + bytesperline];
194 *(dst++) = p[i + 2 * bytesperline];
195 }
196 break;
197 }
198 }
199 }
200
201 free(p);
202
203 return E_OK;
204 }
205
206
207 //-----------------------------------------------------------------------------
208 // wxPCXHandler
209 //-----------------------------------------------------------------------------
210
211 #if !USE_SHARED_LIBRARIES
212 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler)
213 #endif
214
215 #if wxUSE_STREAMS
216
217 bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose )
218 {
219 int error;
220
221 if (!CanRead(stream))
222 {
223 if (verbose)
224 wxLogError(_T("wxPCXHandler: this is not a PCX file"));
225
226 return FALSE;
227 }
228
229 image->Destroy();
230
231 if ((error = ReadPCX(image, stream)) != E_OK)
232 {
233 if (verbose)
234 {
235 switch (error)
236 {
237 case E_FORMATO: wxLogError(_T("wxPCXHandler: image format unsupported")); break;
238 case E_MEMORIA: wxLogError(_T("wxPCXHandler: couldn't allocate memory")); break;
239 case E_VERSION: wxLogError(_T("wxPCXHandler: version number too low")); break;
240 default: wxLogError(_T("wxPCXHandler: unknown error !!!"));
241 }
242 }
243 image->Destroy();
244 return FALSE;
245 }
246
247 return TRUE;
248 }
249
250 bool wxPCXHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
251 {
252 if (verbose)
253 wxLogError(_T("wxPCXHandler::SaveFile still not implemented"));
254
255 return FALSE;
256 }
257
258 bool wxPCXHandler::CanRead( wxInputStream& stream )
259 {
260 unsigned char c;
261 off_t pos;
262
263 pos = stream.TellI();
264 stream.SeekI(0, wxFromStart);
265 c = stream.GetC();
266 stream.SeekI(pos, wxFromStart);
267
268 // not very safe, but this is all we can get from PCX header :-(
269 return (c == 10);
270 }
271
272
273 #endif // wxUSE_STREAMS
274
275