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