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