Skip to content

Latest commit

 

History

History
277 lines (251 loc) · 6.52 KB

IMG_pcx.c

File metadata and controls

277 lines (251 loc) · 6.52 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
SDL_image: An example image loading library for use with SDL
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
This library is free software; you can redistribute it and/or
Feb 4, 2006
Feb 4, 2006
6
modify it under the terms of the GNU Lesser General Public
Aug 10, 2000
Aug 10, 2000
7
License as published by the Free Software Foundation; either
Feb 4, 2006
Feb 4, 2006
8
version 2.1 of the License, or (at your option) any later version.
Aug 10, 2000
Aug 10, 2000
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 4, 2006
Feb 4, 2006
13
Lesser General Public License for more details.
Aug 10, 2000
Aug 10, 2000
14
Feb 4, 2006
Feb 4, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Aug 10, 2000
Aug 10, 2000
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
*/
Feb 27, 2001
Feb 27, 2001
23
24
25
26
27
28
29
/*
* PCX file reader:
* Supports:
* 1..4 bits/pixel in multiplanar format (1 bit/plane/pixel)
* 8 bits/pixel in single-planar format (8 bits/plane/pixel)
* 24 bits/pixel in 3-plane format (8 bits/plane/pixel)
*
Mar 7, 2001
Mar 7, 2001
30
31
* (The <8bpp formats are expanded to 8bpp surfaces)
*
Feb 27, 2001
Feb 27, 2001
32
33
34
35
* Doesn't support:
* single-planar packed-pixel formats other than 8bpp
* 4-plane 32bpp format with a fourth "intensity" plane
*/
Aug 10, 2000
Aug 10, 2000
36
#include <stdio.h>
Feb 27, 2001
Feb 27, 2001
37
#include <stdlib.h>
Aug 10, 2000
Aug 10, 2000
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "SDL_endian.h"
#include "SDL_image.h"
#ifdef LOAD_PCX
struct PCXheader {
Uint8 Manufacturer;
Uint8 Version;
Uint8 Encoding;
Uint8 BitsPerPixel;
Sint16 Xmin, Ymin, Xmax, Ymax;
Sint16 HDpi, VDpi;
Uint8 Colormap[48];
Uint8 Reserved;
Uint8 NPlanes;
Sint16 BytesPerLine;
Sint16 PaletteInfo;
Sint16 HscreenSize;
Sint16 VscreenSize;
Uint8 Filler[54];
};
/* See if an image is contained in a data source */
int IMG_isPCX(SDL_RWops *src)
{
Feb 4, 2006
Feb 4, 2006
65
int start;
Aug 10, 2000
Aug 10, 2000
66
67
68
int is_PCX;
const int ZSoft_Manufacturer = 10;
const int PC_Paintbrush_Version = 5;
Oct 4, 2009
Oct 4, 2009
69
const int PCX_Uncompressed_Encoding = 0;
Aug 10, 2000
Aug 10, 2000
70
71
72
const int PCX_RunLength_Encoding = 1;
struct PCXheader pcxh;
Feb 13, 2007
Feb 13, 2007
73
74
if ( !src )
return 0;
Feb 4, 2006
Feb 4, 2006
75
start = SDL_RWtell(src);
Aug 10, 2000
Aug 10, 2000
76
77
78
79
is_PCX = 0;
if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
(pcxh.Version == PC_Paintbrush_Version) &&
Oct 4, 2009
Oct 4, 2009
80
81
(pcxh.Encoding == PCX_RunLength_Encoding ||
pcxh.Encoding == PCX_Uncompressed_Encoding) ) {
Aug 10, 2000
Aug 10, 2000
82
83
84
is_PCX = 1;
}
}
Nov 8, 2009
Nov 8, 2009
85
SDL_RWseek(src, start, RW_SEEK_SET);
Aug 10, 2000
Aug 10, 2000
86
87
88
89
90
91
return(is_PCX);
}
/* Load a PCX type image from an SDL datasource */
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
{
Feb 4, 2006
Feb 4, 2006
92
int start;
Aug 10, 2000
Aug 10, 2000
93
94
95
96
97
struct PCXheader pcxh;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Feb 27, 2001
Feb 27, 2001
98
SDL_Surface *surface = NULL;
Aug 10, 2000
Aug 10, 2000
99
int width, height;
Feb 27, 2001
Feb 27, 2001
100
101
102
103
int y, bpl;
Uint8 *row, *buf = NULL;
char *error = NULL;
int bits, src_bits;
Aug 10, 2000
Aug 10, 2000
104
Jan 4, 2004
Jan 4, 2004
105
106
107
108
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
109
110
start = SDL_RWtell(src);
Aug 10, 2000
Aug 10, 2000
111
if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
Feb 27, 2001
Feb 27, 2001
112
error = "file truncated";
Aug 10, 2000
Aug 10, 2000
113
114
115
116
117
118
119
120
121
122
123
goto done;
}
pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
/* Create the surface of the appropriate type */
width = (pcxh.Xmax - pcxh.Xmin) + 1;
height = (pcxh.Ymax - pcxh.Ymin) + 1;
Feb 27, 2001
Feb 27, 2001
124
125
126
127
128
129
130
Rmask = Gmask = Bmask = Amask = 0;
src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
|| (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
bits = 8;
} else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
bits = 24;
Aug 10, 2000
Aug 10, 2000
131
132
133
134
135
if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
} else {
Sep 1, 2000
Sep 1, 2000
136
137
138
Rmask = 0xFF0000;
Gmask = 0x00FF00;
Bmask = 0x0000FF;
Aug 10, 2000
Aug 10, 2000
139
}
Feb 27, 2001
Feb 27, 2001
140
141
142
} else {
error = "unsupported PCX format";
goto done;
Aug 10, 2000
Aug 10, 2000
143
144
}
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
Feb 27, 2001
Feb 27, 2001
145
146
bits, Rmask, Gmask, Bmask, Amask);
if ( surface == NULL )
Aug 10, 2000
Aug 10, 2000
147
148
goto done;
Feb 27, 2001
Feb 27, 2001
149
bpl = pcxh.NPlanes * pcxh.BytesPerLine;
Oct 4, 2009
Oct 4, 2009
150
151
152
if (bpl > surface->pitch) {
error = "bytes per line is too large (corrupt?)";
}
Feb 27, 2001
Feb 27, 2001
153
154
buf = malloc(bpl);
row = surface->pixels;
Aug 10, 2000
Aug 10, 2000
155
for ( y=0; y<surface->h; ++y ) {
Feb 27, 2001
Feb 27, 2001
156
157
158
159
/* decode a scan line to a temporary buffer first */
int i, count = 0;
Uint8 ch;
Uint8 *dst = (src_bits == 8) ? row : buf;
Oct 4, 2009
Oct 4, 2009
160
161
162
163
164
165
166
167
if ( pcxh.Encoding == 0 ) {
if(!SDL_RWread(src, dst, bpl, 1)) {
error = "file truncated";
goto done;
}
} else {
for(i = 0; i < bpl; i++) {
if(!count) {
Feb 27, 2001
Feb 27, 2001
168
169
170
171
if(!SDL_RWread(src, &ch, 1, 1)) {
error = "file truncated";
goto done;
}
Oct 4, 2009
Oct 4, 2009
172
173
174
175
176
177
178
179
180
181
182
if( (ch & 0xc0) == 0xc0) {
count = ch & 0x3f;
if(!SDL_RWread(src, &ch, 1, 1)) {
error = "file truncated";
goto done;
}
} else
count = 1;
}
dst[i] = ch;
count--;
Feb 27, 2001
Feb 27, 2001
183
184
185
186
187
188
189
190
191
192
193
194
195
}
}
if(src_bits <= 4) {
/* expand planes to 1 byte/pixel */
Uint8 *src = buf;
int plane;
for(plane = 0; plane < pcxh.NPlanes; plane++) {
int i, j, x = 0;
for(i = 0; i < pcxh.BytesPerLine; i++) {
Uint8 byte = *src++;
for(j = 7; j >= 0; j--) {
unsigned bit = (byte >> j) & 1;
Aug 6, 2003
Aug 6, 2003
196
197
198
/* skip padding bits */
if (i * 8 + j >= width)
continue;
Feb 27, 2001
Feb 27, 2001
199
200
row[x++] |= bit << plane;
}
Aug 10, 2000
Aug 10, 2000
201
}
Feb 27, 2001
Feb 27, 2001
202
203
204
205
206
207
208
209
210
211
212
}
} else if(src_bits == 24) {
/* de-interlace planes */
Uint8 *src = buf;
int plane;
for(plane = 0; plane < pcxh.NPlanes; plane++) {
int x;
dst = row + plane;
for(x = 0; x < width; x++) {
*dst = *src++;
dst += pcxh.NPlanes;
Aug 10, 2000
Aug 10, 2000
213
214
215
216
}
}
}
Feb 27, 2001
Feb 27, 2001
217
218
row += surface->pitch;
}
Aug 10, 2000
Aug 10, 2000
219
Feb 27, 2001
Feb 27, 2001
220
if(bits == 8) {
Aug 10, 2000
Aug 10, 2000
221
SDL_Color *colors = surface->format->palette->colors;
Feb 27, 2001
Feb 27, 2001
222
223
224
225
226
227
228
229
230
231
232
233
234
int nc = 1 << src_bits;
int i;
surface->format->palette->ncolors = nc;
if(src_bits == 8) {
Uint8 ch;
/* look for a 256-colour palette */
do {
if ( !SDL_RWread(src, &ch, 1, 1)) {
error = "file truncated";
goto done;
}
} while ( ch != 12 );
Aug 10, 2000
Aug 10, 2000
235
Feb 27, 2001
Feb 27, 2001
236
237
238
239
240
241
242
243
244
245
for(i = 0; i < 256; i++) {
SDL_RWread(src, &colors[i].r, 1, 1);
SDL_RWread(src, &colors[i].g, 1, 1);
SDL_RWread(src, &colors[i].b, 1, 1);
}
} else {
for(i = 0; i < nc; i++) {
colors[i].r = pcxh.Colormap[i * 3];
colors[i].g = pcxh.Colormap[i * 3 + 1];
colors[i].b = pcxh.Colormap[i * 3 + 2];
Aug 10, 2000
Aug 10, 2000
246
247
248
249
250
}
}
}
done:
Feb 27, 2001
Feb 27, 2001
251
252
free(buf);
if ( error ) {
Nov 8, 2009
Nov 8, 2009
253
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
254
255
256
257
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
Feb 27, 2001
Feb 27, 2001
258
IMG_SetError(error);
Aug 10, 2000
Aug 10, 2000
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
}
return(surface);
}
#else
/* See if an image is contained in a data source */
int IMG_isPCX(SDL_RWops *src)
{
return(0);
}
/* Load a PCX type image from an SDL datasource */
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_PCX */