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