Skip to content

Latest commit

 

History

History
298 lines (272 loc) · 9 KB

IMG_pcx.c

File metadata and controls

298 lines (272 loc) · 9 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Aug 10, 2000
Aug 10, 2000
20
21
*/
Feb 27, 2001
Feb 27, 2001
22
23
24
25
26
27
28
/*
* 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
29
30
* (The <8bpp formats are expanded to 8bpp surfaces)
*
Feb 27, 2001
Feb 27, 2001
31
32
33
34
* 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
35
36
37
38
39
40
41
42
#include "SDL_endian.h"
#include "SDL_image.h"
#ifdef LOAD_PCX
struct PCXheader {
May 22, 2013
May 22, 2013
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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];
Aug 10, 2000
Aug 10, 2000
57
58
59
60
61
};
/* See if an image is contained in a data source */
int IMG_isPCX(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
62
63
64
65
66
67
68
Sint64 start;
int is_PCX;
const int ZSoft_Manufacturer = 10;
const int PC_Paintbrush_Version = 5;
const int PCX_Uncompressed_Encoding = 0;
const int PCX_RunLength_Encoding = 1;
struct PCXheader pcxh;
Aug 10, 2000
Aug 10, 2000
69
May 22, 2013
May 22, 2013
70
71
72
73
74
75
76
77
78
79
80
81
82
83
if ( !src )
return 0;
start = SDL_RWtell(src);
is_PCX = 0;
if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
(pcxh.Version == PC_Paintbrush_Version) &&
(pcxh.Encoding == PCX_RunLength_Encoding ||
pcxh.Encoding == PCX_Uncompressed_Encoding) ) {
is_PCX = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_PCX);
Aug 10, 2000
Aug 10, 2000
84
85
86
87
88
}
/* Load a PCX type image from an SDL datasource */
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
89
90
91
92
93
94
95
96
97
98
99
100
Sint64 start;
struct PCXheader pcxh;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
SDL_Surface *surface = NULL;
int width, height;
int y, bpl;
Uint8 *row, *buf = NULL;
char *error = NULL;
int bits, src_bits;
Jun 10, 2019
Jun 10, 2019
101
102
int count = 0;
Uint8 ch;
Aug 10, 2000
Aug 10, 2000
103
May 22, 2013
May 22, 2013
104
105
106
107
108
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
Feb 4, 2006
Feb 4, 2006
109
Jun 10, 2019
Jun 10, 2019
110
if ( !SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
May 22, 2013
May 22, 2013
111
112
113
114
115
116
117
118
error = "file truncated";
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);
Aug 10, 2000
Aug 10, 2000
119
Jun 10, 2019
Jun 10, 2019
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#if 0
printf("Manufacturer = %d\n", pcxh.Manufacturer);
printf("Version = %d\n", pcxh.Version);
printf("Encoding = %d\n", pcxh.Encoding);
printf("BitsPerPixel = %d\n", pcxh.BitsPerPixel);
printf("Xmin = %d, Ymin = %d, Xmax = %d, Ymax = %d\n", pcxh.Xmin, pcxh.Ymin, pcxh.Xmax, pcxh.Ymax);
printf("HDpi = %d, VDpi = %d\n", pcxh.HDpi, pcxh.VDpi);
printf("NPlanes = %d\n", pcxh.NPlanes);
printf("BytesPerLine = %d\n", pcxh.BytesPerLine);
printf("PaletteInfo = %d\n", pcxh.PaletteInfo);
printf("HscreenSize = %d\n", pcxh.HscreenSize);
printf("VscreenSize = %d\n", pcxh.VscreenSize);
#endif
May 22, 2013
May 22, 2013
134
135
136
137
138
139
140
141
142
143
/* Create the surface of the appropriate type */
width = (pcxh.Xmax - pcxh.Xmin) + 1;
height = (pcxh.Ymax - pcxh.Ymin) + 1;
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;
Feb 3, 2013
Feb 3, 2013
144
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 22, 2013
May 22, 2013
145
146
147
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Feb 3, 2013
Feb 3, 2013
148
#else
May 22, 2013
May 22, 2013
149
150
151
Rmask = 0xFF0000;
Gmask = 0x00FF00;
Bmask = 0x0000FF;
Feb 3, 2013
Feb 3, 2013
152
#endif
May 22, 2013
May 22, 2013
153
154
155
156
157
158
159
160
} else {
error = "unsupported PCX format";
goto done;
}
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
bits, Rmask, Gmask, Bmask, Amask);
if ( surface == NULL )
goto done;
Aug 10, 2000
Aug 10, 2000
161
May 22, 2013
May 22, 2013
162
bpl = pcxh.NPlanes * pcxh.BytesPerLine;
Jun 10, 2019
Jun 10, 2019
163
if ( bpl < 0 || bpl > surface->pitch ) {
May 22, 2013
May 22, 2013
164
error = "bytes per line is too large (corrupt?)";
Jun 10, 2019
Jun 10, 2019
165
goto done;
May 22, 2013
May 22, 2013
166
}
Jun 10, 2019
Jun 10, 2019
167
buf = (Uint8 *)SDL_calloc(surface->pitch, 1);
May 22, 2013
May 22, 2013
168
169
170
row = (Uint8 *)surface->pixels;
for ( y=0; y<surface->h; ++y ) {
/* decode a scan line to a temporary buffer first */
Jun 10, 2019
Jun 10, 2019
171
172
int i;
Uint8 *dst = buf;
May 22, 2013
May 22, 2013
173
if ( pcxh.Encoding == 0 ) {
Jun 10, 2019
Jun 10, 2019
174
if ( !SDL_RWread(src, dst, bpl, 1) ) {
May 22, 2013
May 22, 2013
175
176
177
178
error = "file truncated";
goto done;
}
} else {
Jun 10, 2019
Jun 10, 2019
179
180
181
for ( i = 0; i < bpl; i++ ) {
if ( !count ) {
if ( !SDL_RWread(src, &ch, 1, 1) ) {
May 22, 2013
May 22, 2013
182
183
184
error = "file truncated";
goto done;
}
Jun 10, 2019
Jun 10, 2019
185
186
187
188
189
if ( ch < 0xc0 ) {
count = 1;
} else {
count = ch - 0xc0;
if( !SDL_RWread(src, &ch, 1, 1) ) {
May 22, 2013
May 22, 2013
190
191
192
error = "file truncated";
goto done;
}
Jun 10, 2019
Jun 10, 2019
193
}
May 22, 2013
May 22, 2013
194
195
196
197
198
}
dst[i] = ch;
count--;
}
}
Feb 27, 2001
Feb 27, 2001
199
Jun 10, 2019
Jun 10, 2019
200
if ( src_bits <= 4 ) {
May 22, 2013
May 22, 2013
201
202
203
/* expand planes to 1 byte/pixel */
Uint8 *innerSrc = buf;
int plane;
Jun 10, 2019
Jun 10, 2019
204
for ( plane = 0; plane < pcxh.NPlanes; plane++ ) {
May 22, 2013
May 22, 2013
205
int j, k, x = 0;
Jun 10, 2019
Jun 10, 2019
206
for( j = 0; j < pcxh.BytesPerLine; j++ ) {
May 22, 2013
May 22, 2013
207
Uint8 byte = *innerSrc++;
Jun 10, 2019
Jun 10, 2019
208
for( k = 7; k >= 0; k-- ) {
May 22, 2013
May 22, 2013
209
210
211
212
213
214
215
216
unsigned bit = (byte >> k) & 1;
/* skip padding bits */
if (j * 8 + k >= width)
continue;
row[x++] |= bit << plane;
}
}
}
Jun 10, 2019
Jun 10, 2019
217
} else if ( src_bits == 24 ) {
May 22, 2013
May 22, 2013
218
219
220
/* de-interlace planes */
Uint8 *innerSrc = buf;
int plane;
Jun 10, 2019
Jun 10, 2019
221
for ( plane = 0; plane < pcxh.NPlanes; plane++ ) {
May 22, 2013
May 22, 2013
222
223
int x;
dst = row + plane;
Jun 10, 2019
Jun 10, 2019
224
225
226
227
228
for ( x = 0; x < width; x++ ) {
if ( dst >= row+surface->pitch ) {
error = "decoding out of bounds (corrupt?)";
goto done;
}
May 22, 2013
May 22, 2013
229
230
231
232
*dst = *innerSrc++;
dst += pcxh.NPlanes;
}
}
Jun 10, 2019
Jun 10, 2019
233
234
} else {
SDL_memcpy(row, buf, bpl);
May 22, 2013
May 22, 2013
235
}
Aug 10, 2000
Aug 10, 2000
236
May 22, 2013
May 22, 2013
237
238
row += surface->pitch;
}
Aug 10, 2000
Aug 10, 2000
239
Jun 10, 2019
Jun 10, 2019
240
if ( bits == 8 ) {
May 22, 2013
May 22, 2013
241
242
243
SDL_Color *colors = surface->format->palette->colors;
int nc = 1 << src_bits;
int i;
Feb 27, 2001
Feb 27, 2001
244
May 22, 2013
May 22, 2013
245
surface->format->palette->ncolors = nc;
Jun 10, 2019
Jun 10, 2019
246
if ( src_bits == 8 ) {
May 22, 2013
May 22, 2013
247
248
249
Uint8 ch;
/* look for a 256-colour palette */
do {
Jun 10, 2019
Jun 10, 2019
250
251
252
253
if ( !SDL_RWread(src, &ch, 1, 1) ) {
/* Couldn't find the palette, try the end of the file */
SDL_RWseek(src, -768, RW_SEEK_END);
break;
May 22, 2013
May 22, 2013
254
255
}
} while ( ch != 12 );
Aug 10, 2000
Aug 10, 2000
256
Jun 10, 2019
Jun 10, 2019
257
for ( i = 0; i < 256; i++ ) {
May 22, 2013
May 22, 2013
258
259
260
261
262
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 {
Jun 10, 2019
Jun 10, 2019
263
for ( i = 0; i < nc; i++ ) {
May 22, 2013
May 22, 2013
264
265
266
267
268
269
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
270
271
done:
May 22, 2013
May 22, 2013
272
273
274
275
276
277
278
SDL_free(buf);
if ( error ) {
SDL_RWseek(src, start, RW_SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
Jun 15, 2014
Jun 15, 2014
279
IMG_SetError("%s", error);
May 22, 2013
May 22, 2013
280
281
}
return(surface);
Aug 10, 2000
Aug 10, 2000
282
283
284
285
286
287
288
}
#else
/* See if an image is contained in a data source */
int IMG_isPCX(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
289
return(0);
Aug 10, 2000
Aug 10, 2000
290
291
292
293
294
}
/* Load a PCX type image from an SDL datasource */
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
295
return(NULL);
Aug 10, 2000
Aug 10, 2000
296
297
298
}
#endif /* LOAD_PCX */