IMG_pcx.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 01 Sep 2000 00:45:57 +0000
changeset 4 b1bb33e907f8
parent 0 76be7dab668c
child 7 e1b6443ffb6b
permissions -rw-r--r--
Mattias Engdeg�rd - Wed Aug 9 20:32:22 MET DST 2000
* Removed the alpha flipping, made IMG_InvertAlpha() a noop
* Fixed nonexisting PCX alpha support
* Some TIFF bugfixes
* PNG greyscale images are loaded as 8bpp with a greyscale palette
slouken@0
     1
/*
slouken@0
     2
    IMGLIB:  An example image loading library for use with SDL
slouken@0
     3
    Copyright (C) 1999  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@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
/* This is a PCX image file loading framework */
slouken@0
    26
slouken@0
    27
#include <stdio.h>
slouken@0
    28
slouken@0
    29
#include "SDL_endian.h"
slouken@0
    30
slouken@0
    31
#include "SDL_image.h"
slouken@0
    32
slouken@0
    33
#ifdef LOAD_PCX
slouken@0
    34
slouken@0
    35
struct PCXheader {
slouken@0
    36
	Uint8 Manufacturer;
slouken@0
    37
	Uint8 Version;
slouken@0
    38
	Uint8 Encoding;
slouken@0
    39
	Uint8 BitsPerPixel;
slouken@0
    40
	Sint16 Xmin, Ymin, Xmax, Ymax;
slouken@0
    41
	Sint16 HDpi, VDpi;
slouken@0
    42
	Uint8 Colormap[48];
slouken@0
    43
	Uint8 Reserved;
slouken@0
    44
	Uint8 NPlanes;
slouken@0
    45
	Sint16 BytesPerLine;
slouken@0
    46
	Sint16 PaletteInfo;
slouken@0
    47
	Sint16 HscreenSize;
slouken@0
    48
	Sint16 VscreenSize;
slouken@0
    49
	Uint8 Filler[54];
slouken@0
    50
};
slouken@0
    51
slouken@0
    52
/* See if an image is contained in a data source */
slouken@0
    53
int IMG_isPCX(SDL_RWops *src)
slouken@0
    54
{
slouken@0
    55
	int is_PCX;
slouken@0
    56
	const int ZSoft_Manufacturer = 10;
slouken@0
    57
	const int PC_Paintbrush_Version = 5;
slouken@0
    58
	const int PCX_RunLength_Encoding = 1;
slouken@0
    59
	struct PCXheader pcxh;
slouken@0
    60
slouken@0
    61
	is_PCX = 0;
slouken@0
    62
	if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
slouken@0
    63
		if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
slouken@0
    64
		     (pcxh.Version == PC_Paintbrush_Version) &&
slouken@0
    65
		     (pcxh.Encoding == PCX_RunLength_Encoding) ) {
slouken@0
    66
			is_PCX = 1;
slouken@0
    67
		}
slouken@0
    68
	}
slouken@0
    69
	return(is_PCX);
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
/* Load a PCX type image from an SDL datasource */
slouken@0
    73
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
slouken@0
    74
{
slouken@0
    75
	struct PCXheader pcxh;
slouken@0
    76
	Uint32 Rmask;
slouken@0
    77
	Uint32 Gmask;
slouken@0
    78
	Uint32 Bmask;
slouken@0
    79
	Uint32 Amask;
slouken@0
    80
	SDL_Surface *surface;
slouken@0
    81
	int width, height;
slouken@0
    82
	int i, index, x, y;
slouken@0
    83
	int count;
slouken@0
    84
	Uint8 *row, ch;
slouken@0
    85
	int read_error;
slouken@0
    86
slouken@0
    87
	/* Initialize the data we will clean up when we're done */
slouken@0
    88
	surface = NULL;
slouken@0
    89
	read_error = 0;
slouken@0
    90
slouken@0
    91
	/* Check to make sure we have something to do */
slouken@0
    92
	if ( ! src ) {
slouken@0
    93
		goto done;
slouken@0
    94
	}
slouken@0
    95
slouken@0
    96
	/* Read and convert the header */
slouken@0
    97
	if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
slouken@0
    98
		goto done;
slouken@0
    99
	}
slouken@0
   100
	pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
slouken@0
   101
	pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
slouken@0
   102
	pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
slouken@0
   103
	pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
slouken@0
   104
	pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
slouken@0
   105
slouken@0
   106
	/* Create the surface of the appropriate type */
slouken@0
   107
	width = (pcxh.Xmax - pcxh.Xmin) + 1;
slouken@0
   108
	height = (pcxh.Ymax - pcxh.Ymin) + 1;
slouken@0
   109
	Rmask = Gmask = Bmask = Amask = 0 ; 
slouken@0
   110
	if ( pcxh.BitsPerPixel * pcxh.NPlanes > 16 ) {
slouken@0
   111
		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
slouken@0
   112
			Rmask = 0x000000FF;
slouken@0
   113
			Gmask = 0x0000FF00;
slouken@0
   114
			Bmask = 0x00FF0000;
slouken@0
   115
		} else {
slouken@0
   116
		        int s = (pcxh.NPlanes == 4) ? 0 : 8;
slouken@4
   117
			Rmask = 0xFF0000;
slouken@4
   118
			Gmask = 0x00FF00;
slouken@4
   119
			Bmask = 0x0000FF;
slouken@0
   120
		}
slouken@0
   121
	}
slouken@0
   122
	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
slouken@0
   123
			pcxh.BitsPerPixel*pcxh.NPlanes,Rmask,Gmask,Bmask,Amask);
slouken@0
   124
	if ( surface == NULL ) {
slouken@0
   125
		IMG_SetError("Out of memory");
slouken@0
   126
		goto done;
slouken@0
   127
	}
slouken@0
   128
slouken@0
   129
	/* Decode the image to the surface */
slouken@0
   130
	for ( y=0; y<surface->h; ++y ) {
slouken@0
   131
		for ( i=0; i<pcxh.NPlanes; ++i ) {
slouken@0
   132
			row = (Uint8 *)surface->pixels + y*surface->pitch;
slouken@0
   133
			index = i;
slouken@0
   134
			for ( x=0; x<pcxh.BytesPerLine; ) {
slouken@0
   135
				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
slouken@0
   136
					read_error = 1;
slouken@0
   137
					goto done;
slouken@0
   138
				}
slouken@0
   139
				if ( (ch & 0xC0) == 0xC0 ) {
slouken@0
   140
					count = ch & 0x3F;
slouken@0
   141
					SDL_RWread(src, &ch, 1, 1);
slouken@0
   142
				} else {
slouken@0
   143
					count = 1;
slouken@0
   144
				}
slouken@0
   145
				while ( count-- ) {
slouken@0
   146
					row[index] = ch;
slouken@0
   147
					++x;
slouken@0
   148
					index += pcxh.NPlanes;
slouken@0
   149
				}
slouken@0
   150
			}
slouken@0
   151
		}
slouken@0
   152
	}
slouken@0
   153
slouken@0
   154
	/* Look for the palette, if necessary */
slouken@0
   155
	switch (surface->format->BitsPerPixel) {
slouken@0
   156
	    case 1: {
slouken@0
   157
		SDL_Color *colors = surface->format->palette->colors;
slouken@0
   158
slouken@0
   159
		colors[0].r = 0x00;
slouken@0
   160
		colors[0].g = 0x00;
slouken@0
   161
		colors[0].b = 0x00;
slouken@0
   162
		colors[1].r = 0xFF;
slouken@0
   163
		colors[1].g = 0xFF;
slouken@0
   164
		colors[1].b = 0xFF;
slouken@0
   165
	    }
slouken@0
   166
	    break;
slouken@0
   167
slouken@0
   168
	    case 8: {
slouken@0
   169
		SDL_Color *colors = surface->format->palette->colors;
slouken@0
   170
slouken@0
   171
		/* Look for the palette */
slouken@0
   172
		do {
slouken@0
   173
			if ( ! SDL_RWread(src, &ch, 1, 1) ) {
slouken@0
   174
				read_error = 1;
slouken@0
   175
				goto done;
slouken@0
   176
			}
slouken@0
   177
		} while ( ch != 12 );
slouken@0
   178
slouken@0
   179
		/* Set the image surface palette */
slouken@0
   180
		for ( i=0; i<256; ++i ) {
slouken@0
   181
			SDL_RWread(src, &colors[i].r, 1, 1);
slouken@0
   182
			SDL_RWread(src, &colors[i].g, 1, 1);
slouken@0
   183
			SDL_RWread(src, &colors[i].b, 1, 1);
slouken@0
   184
		}
slouken@0
   185
	    }
slouken@0
   186
	    break;
slouken@0
   187
slouken@0
   188
	    default: {
slouken@0
   189
		/* Don't do anything... */;
slouken@0
   190
	    }
slouken@0
   191
	    break;
slouken@0
   192
	}
slouken@0
   193
slouken@0
   194
done:
slouken@0
   195
	if ( read_error ) {
slouken@0
   196
		SDL_FreeSurface(surface);
slouken@0
   197
		IMG_SetError("Error reading PCX data");
slouken@0
   198
		surface = NULL;
slouken@0
   199
	}
slouken@0
   200
	return(surface);
slouken@0
   201
}
slouken@0
   202
slouken@0
   203
#else
slouken@0
   204
slouken@0
   205
/* See if an image is contained in a data source */
slouken@0
   206
int IMG_isPCX(SDL_RWops *src)
slouken@0
   207
{
slouken@0
   208
	return(0);
slouken@0
   209
}
slouken@0
   210
slouken@0
   211
/* Load a PCX type image from an SDL datasource */
slouken@0
   212
SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
slouken@0
   213
{
slouken@0
   214
	return(NULL);
slouken@0
   215
}
slouken@0
   216
slouken@0
   217
#endif /* LOAD_PCX */