]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpcx.cpp
test for timegm() added
[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
b9b32d5c
GRG
23#ifndef WX_PRECOMP
24# include "wx/defs.h"
25#endif
26
995612e2 27#if wxUSE_STREAMS && wxUSE_PCX
b9b32d5c 28
56c66755
GRG
29#include "wx/image.h"
30#include "wx/wfstream.h"
31#include "wx/module.h"
32#include "wx/log.h"
33
1044a386 34
4df78dc3
GRG
35//-----------------------------------------------------------------------------
36// PCX decoding
37//-----------------------------------------------------------------------------
38
5e0201ea 39void RLEencode(unsigned char *WXUNUSED(p), unsigned int WXUNUSED(size), wxOutputStream& WXUNUSED(s))
4df78dc3
GRG
40{
41}
42
43void 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 */
995612e2 79#define HDR_VERSION 1
4df78dc3
GRG
80#define HDR_ENCODING 2
81#define HDR_BITSPERPIXEL 3
82#define HDR_XMIN 4
83#define HDR_YMIN 6
995612e2 84#define HDR_XMAX 8
4df78dc3
GRG
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//
105int 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 //
f0d922cf
GRG
152 if (format == IMAGE_8BIT)
153 {
154 pos = stream.TellI();
155 stream.SeekI(-769, wxFromEnd);
4df78dc3 156
f0d922cf
GRG
157 if (stream.GetC() != 12)
158 return E_FORMATO;
4df78dc3 159
f0d922cf
GRG
160 stream.Read(pal, 768);
161 stream.SeekI(pos, wxFromStart);
162 }
4df78dc3
GRG
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];
995612e2 204 *(dst++) = p[i + 2 * bytesperline];
4df78dc3
GRG
205 }
206 break;
207 }
208 }
209 }
210
211 free(p);
212
213 return E_OK;
214}
215
216
56c66755
GRG
217//-----------------------------------------------------------------------------
218// wxPCXHandler
219//-----------------------------------------------------------------------------
220
221#if !USE_SHARED_LIBRARIES
222IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler,wxImageHandler)
223#endif
224
700ec454 225bool wxPCXHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
56c66755 226{
4df78dc3
GRG
227 int error;
228
229 if (!CanRead(stream))
230 {
231 if (verbose)
223d09f6 232 wxLogError(wxT("wxPCXHandler: this is not a PCX file"));
4df78dc3
GRG
233
234 return FALSE;
235 }
236
56c66755
GRG
237 image->Destroy();
238
4df78dc3
GRG
239 if ((error = ReadPCX(image, stream)) != E_OK)
240 {
241 if (verbose)
242 {
243 switch (error)
244 {
223d09f6
KB
245 case E_FORMATO: wxLogError(wxT("wxPCXHandler: image format unsupported")); break;
246 case E_MEMORIA: wxLogError(wxT("wxPCXHandler: couldn't allocate memory")); break;
247 case E_VERSION: wxLogError(wxT("wxPCXHandler: version number too low")); break;
248 default: wxLogError(wxT("wxPCXHandler: unknown error !!!"));
4df78dc3
GRG
249 }
250 }
251 image->Destroy();
252 return FALSE;
253 }
56c66755 254
4df78dc3 255 return TRUE;
56c66755
GRG
256}
257
5e0201ea 258bool wxPCXHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool verbose )
56c66755
GRG
259{
260 if (verbose)
223d09f6 261 wxLogError(wxT("wxPCXHandler::SaveFile still not implemented"));
56c66755
GRG
262
263 return FALSE;
264}
265
995612e2 266bool wxPCXHandler::DoCanRead( wxInputStream& stream )
56c66755 267{
4df78dc3 268 unsigned char c;
4df78dc3 269
4df78dc3 270 c = stream.GetC();
a05da1b6 271 stream.SeekI(-1, wxFromCurrent);
4df78dc3
GRG
272
273 // not very safe, but this is all we can get from PCX header :-(
274 return (c == 10);
56c66755
GRG
275}
276
b9b32d5c 277#endif // wxUSE_STREAMS && wxUSE_PCX
56c66755 278