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