IMG_tga.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 97 e1161bd417c4
parent 53 96b084473b47
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@9
    25
#include <stdlib.h>
slouken@0
    26
#include <stdio.h>
slouken@9
    27
#include <string.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_TGA
slouken@0
    34
slouken@0
    35
/*
slouken@0
    36
 * A TGA loader for the SDL library
slouken@0
    37
 * Supports: Reading 8, 15, 16, 24 and 32bpp images, with alpha or colourkey,
slouken@0
    38
 *           uncompressed or RLE encoded.
slouken@0
    39
 *
slouken@0
    40
 * 2000-06-10 Mattias Engdegård <f91-men@nada.kth.se>: initial version
slouken@0
    41
 * 2000-06-26 Mattias Engdegård <f91-men@nada.kth.se>: read greyscale TGAs
slouken@4
    42
 * 2000-08-09 Mattias Engdegård <f91-men@nada.kth.se>: alpha inversion removed
slouken@0
    43
 */
slouken@0
    44
slouken@0
    45
struct TGAheader {
slouken@0
    46
    Uint8 infolen;		/* length of info field */
slouken@0
    47
    Uint8 has_cmap;		/* 1 if image has colormap, 0 otherwise */
slouken@0
    48
    Uint8 type;
slouken@0
    49
slouken@0
    50
    Uint8 cmap_start[2];	/* index of first colormap entry */
slouken@0
    51
    Uint8 cmap_len[2];		/* number of entries in colormap */
slouken@0
    52
    Uint8 cmap_bits;		/* bits per colormap entry */
slouken@0
    53
slouken@0
    54
    Uint8 yorigin[2];		/* image origin (ignored here) */
slouken@0
    55
    Uint8 xorigin[2];
slouken@0
    56
    Uint8 width[2];		/* image size */
slouken@0
    57
    Uint8 height[2];
slouken@0
    58
    Uint8 pixel_bits;		/* bits/pixel */
slouken@0
    59
    Uint8 flags;
slouken@0
    60
};
slouken@0
    61
slouken@0
    62
enum tga_type {
slouken@0
    63
    TGA_TYPE_INDEXED = 1,
slouken@0
    64
    TGA_TYPE_RGB = 2,
slouken@0
    65
    TGA_TYPE_BW = 3,
slouken@0
    66
    TGA_TYPE_RLE_INDEXED = 9,
slouken@0
    67
    TGA_TYPE_RLE_RGB = 10,
slouken@0
    68
    TGA_TYPE_RLE_BW = 11
slouken@0
    69
};
slouken@0
    70
slouken@0
    71
#define TGA_INTERLEAVE_MASK	0xc0
slouken@0
    72
#define TGA_INTERLEAVE_NONE	0x00
slouken@0
    73
#define TGA_INTERLEAVE_2WAY	0x40
slouken@0
    74
#define TGA_INTERLEAVE_4WAY	0x80
slouken@0
    75
slouken@0
    76
#define TGA_ORIGIN_MASK		0x30
slouken@0
    77
#define TGA_ORIGIN_LEFT		0x00
slouken@0
    78
#define TGA_ORIGIN_RIGHT	0x10
slouken@0
    79
#define TGA_ORIGIN_LOWER	0x00
slouken@0
    80
#define TGA_ORIGIN_UPPER	0x20
slouken@0
    81
slouken@0
    82
/* read/write unaligned little-endian 16-bit ints */
slouken@0
    83
#define LE16(p) ((p)[0] + ((p)[1] << 8))
slouken@0
    84
#define SETLE16(p, v) ((p)[0] = (v), (p)[1] = (v) >> 8)
slouken@0
    85
slouken@0
    86
static void unsupported(void)
slouken@0
    87
{
slouken@0
    88
    IMG_SetError("unsupported TGA format");
slouken@0
    89
}
slouken@0
    90
slouken@0
    91
/* Load a TGA type image from an SDL datasource */
slouken@0
    92
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
slouken@0
    93
{
slouken@0
    94
    struct TGAheader hdr;
slouken@0
    95
    int rle = 0;
slouken@0
    96
    int alpha = 0;
slouken@0
    97
    int indexed = 0;
slouken@0
    98
    int grey = 0;
slouken@0
    99
    int ckey = -1;
slouken@0
   100
    int ncols, w, h;
slouken@0
   101
    SDL_Surface *img;
slouken@0
   102
    Uint32 rmask, gmask, bmask, amask;
slouken@0
   103
    Uint8 *dst;
slouken@0
   104
    int i;
slouken@0
   105
    int bpp;
slouken@0
   106
    int lstep;
slouken@0
   107
    Uint32 pixel;
slouken@0
   108
    int count, rep;
slouken@0
   109
slouken@0
   110
    if(!SDL_RWread(src, &hdr, sizeof(hdr), 1))
slouken@0
   111
	goto error;
slouken@0
   112
    ncols = LE16(hdr.cmap_len);
slouken@0
   113
    switch(hdr.type) {
slouken@0
   114
    case TGA_TYPE_RLE_INDEXED:
slouken@0
   115
	rle = 1;
slouken@0
   116
	/* fallthrough */
slouken@0
   117
    case TGA_TYPE_INDEXED:
slouken@0
   118
	if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
slouken@0
   119
	    goto error;
slouken@0
   120
	indexed = 1;
slouken@0
   121
	break;
slouken@0
   122
slouken@0
   123
    case TGA_TYPE_RLE_RGB:
slouken@0
   124
	rle = 1;
slouken@0
   125
	/* fallthrough */
slouken@0
   126
    case TGA_TYPE_RGB:
slouken@0
   127
	indexed = 0;
slouken@0
   128
	break;
slouken@0
   129
slouken@0
   130
    case TGA_TYPE_RLE_BW:
slouken@0
   131
	rle = 1;
slouken@0
   132
	/* fallthrough */
slouken@0
   133
    case TGA_TYPE_BW:
slouken@0
   134
	if(hdr.pixel_bits != 8)
slouken@0
   135
	    goto error;
slouken@0
   136
	/* Treat greyscale as 8bpp indexed images */
slouken@0
   137
	indexed = grey = 1;
slouken@0
   138
	break;
slouken@0
   139
slouken@0
   140
    default:
slouken@0
   141
        unsupported();
slouken@0
   142
	return NULL;
slouken@0
   143
    }
slouken@0
   144
slouken@0
   145
    bpp = (hdr.pixel_bits + 7) >> 3;
slouken@4
   146
    rmask = gmask = bmask = amask = 0;
slouken@0
   147
    switch(hdr.pixel_bits) {
slouken@0
   148
    case 8:
slouken@0
   149
	if(!indexed) {
slouken@0
   150
	    unsupported();
slouken@0
   151
	    return NULL;
slouken@0
   152
	}
slouken@0
   153
	break;
slouken@0
   154
slouken@0
   155
    case 15:
slouken@0
   156
    case 16:
slouken@0
   157
	/* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
slouken@0
   158
	   is ignored for now. */
slouken@0
   159
	rmask = 0x7c00;
slouken@0
   160
	gmask = 0x03e0;
slouken@0
   161
	bmask = 0x001f;
slouken@0
   162
	break;
slouken@0
   163
slouken@0
   164
    case 32:
slouken@0
   165
	alpha = 1;
slouken@0
   166
	/* fallthrough */
slouken@0
   167
    case 24:
slouken@0
   168
	if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
slouken@0
   169
	    int s = alpha ? 0 : 8;
slouken@0
   170
	    amask = 0x000000ff >> s;
slouken@0
   171
	    rmask = 0x0000ff00 >> s;
slouken@0
   172
	    gmask = 0x00ff0000 >> s;
slouken@0
   173
	    bmask = 0xff000000 >> s;
slouken@0
   174
	} else {
slouken@0
   175
	    amask = alpha ? 0xff000000 : 0;
slouken@0
   176
	    rmask = 0x00ff0000;
slouken@0
   177
	    gmask = 0x0000ff00;
slouken@0
   178
	    bmask = 0x000000ff;
slouken@0
   179
	}
slouken@0
   180
	break;
slouken@0
   181
slouken@0
   182
    default:
slouken@0
   183
	unsupported();
slouken@0
   184
	return NULL;
slouken@0
   185
    }
slouken@0
   186
slouken@0
   187
    if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
slouken@0
   188
       || hdr.flags & TGA_ORIGIN_RIGHT) {
slouken@0
   189
	unsupported();
slouken@0
   190
	return NULL;
slouken@0
   191
    }
slouken@0
   192
    
slouken@0
   193
    SDL_RWseek(src, hdr.infolen, SEEK_CUR); /* skip info field */
slouken@0
   194
slouken@0
   195
    w = LE16(hdr.width);
slouken@0
   196
    h = LE16(hdr.height);
slouken@0
   197
    img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
slouken@0
   198
			       bpp * 8,
slouken@0
   199
			       rmask, gmask, bmask, amask);
slouken@0
   200
slouken@0
   201
    if(hdr.has_cmap) {
slouken@0
   202
	int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
slouken@0
   203
	if(indexed && !grey) {
slouken@0
   204
	    Uint8 *pal = malloc(palsiz), *p = pal;
slouken@0
   205
	    SDL_Color *colors = img->format->palette->colors;
slouken@0
   206
	    img->format->palette->ncolors = ncols;
slouken@0
   207
	    SDL_RWread(src, pal, palsiz, 1);
slouken@0
   208
	    for(i = 0; i < ncols; i++) {
slouken@0
   209
		switch(hdr.cmap_bits) {
slouken@0
   210
		case 15:
slouken@0
   211
		case 16:
slouken@0
   212
		    {
slouken@0
   213
			Uint16 c = p[0] + (p[1] << 8);
slouken@0
   214
			p += 2;
slouken@0
   215
			colors[i].r = (c >> 7) & 0xf8;
slouken@0
   216
			colors[i].g = (c >> 2) & 0xf8;
slouken@0
   217
			colors[i].b = c << 3;
slouken@0
   218
		    }
slouken@0
   219
		    break;
slouken@0
   220
		case 24:
slouken@0
   221
		case 32:
slouken@0
   222
		    colors[i].b = *p++;
slouken@0
   223
		    colors[i].g = *p++;
slouken@0
   224
		    colors[i].r = *p++;
slouken@0
   225
		    if(hdr.cmap_bits == 32 && *p++ < 128)
slouken@0
   226
			ckey = i;
slouken@0
   227
		    break;
slouken@0
   228
		}
slouken@0
   229
	    }
slouken@0
   230
	    free(pal);
slouken@0
   231
	    if(ckey >= 0)
slouken@0
   232
		SDL_SetColorKey(img, SDL_SRCCOLORKEY, ckey);
slouken@0
   233
	} else {
slouken@0
   234
	    /* skip unneeded colormap */
slouken@0
   235
	    SDL_RWseek(src, palsiz, SEEK_CUR);
slouken@0
   236
	}
slouken@0
   237
    }
slouken@0
   238
slouken@0
   239
    if(grey) {
slouken@0
   240
	SDL_Color *colors = img->format->palette->colors;
slouken@0
   241
	for(i = 0; i < 256; i++)
slouken@0
   242
	    colors[i].r = colors[i].g = colors[i].b = i;
slouken@0
   243
	img->format->palette->ncolors = 256;
slouken@0
   244
    }
slouken@0
   245
slouken@0
   246
    if(hdr.flags & TGA_ORIGIN_UPPER) {
slouken@0
   247
	lstep = img->pitch;
slouken@4
   248
	dst = img->pixels;
slouken@0
   249
    } else {
slouken@0
   250
	lstep = -img->pitch;
slouken@0
   251
	dst = (Uint8 *)img->pixels + (h - 1) * img->pitch;
slouken@0
   252
    }
slouken@0
   253
slouken@0
   254
    /* The RLE decoding code is slightly convoluted since we can't rely on
slouken@0
   255
       spans not to wrap across scan lines */
slouken@0
   256
    count = rep = 0;
slouken@0
   257
    for(i = 0; i < h; i++) {
slouken@0
   258
	if(rle) {
slouken@0
   259
	    int x = 0;
slouken@0
   260
	    for(;;) {
slouken@0
   261
		Uint8 c;
slouken@0
   262
slouken@0
   263
		if(count) {
slouken@0
   264
		    int n = count;
slouken@0
   265
		    if(n > w - x)
slouken@0
   266
			n = w - x;
slouken@0
   267
		    SDL_RWread(src, dst + x * bpp, n * bpp, 1);
slouken@0
   268
		    count -= n;
slouken@0
   269
		    x += n;
slouken@0
   270
		    if(x == w)
slouken@0
   271
			break;
slouken@0
   272
		} else if(rep) {
slouken@0
   273
		    int n = rep;
slouken@0
   274
		    if(n > w - x)
slouken@0
   275
			n = w - x;
slouken@0
   276
		    rep -= n;
slouken@0
   277
		    while(n--) {
slouken@0
   278
			memcpy(dst + x * bpp, &pixel, bpp);
slouken@0
   279
			x++;
slouken@0
   280
		    }
slouken@0
   281
		    if(x == w)
slouken@0
   282
			break;
slouken@0
   283
		}
slouken@0
   284
slouken@0
   285
		SDL_RWread(src, &c, 1, 1);
slouken@0
   286
		if(c & 0x80) {
slouken@0
   287
		    SDL_RWread(src, &pixel, bpp, 1);
slouken@0
   288
		    rep = (c & 0x7f) + 1;
slouken@0
   289
		} else {
slouken@0
   290
		    count = c + 1;
slouken@0
   291
		}
slouken@0
   292
	    }
slouken@0
   293
slouken@0
   294
	} else {
slouken@0
   295
	    SDL_RWread(src, dst, w * bpp, 1);
slouken@0
   296
	}
slouken@0
   297
	if(SDL_BYTEORDER == SDL_BIG_ENDIAN && bpp == 2) {
slouken@0
   298
	    /* swap byte order */
slouken@0
   299
	    int x;
slouken@0
   300
	    Uint16 *p = (Uint16 *)dst;
slouken@0
   301
	    for(x = 0; x < w; x++)
slouken@0
   302
		p[x] = SDL_Swap16(p[x]);
slouken@0
   303
	}
slouken@0
   304
	dst += lstep;
slouken@0
   305
    }
slouken@0
   306
    return img;
slouken@0
   307
slouken@0
   308
error:
slouken@0
   309
    IMG_SetError("Error reading TGA data");
slouken@0
   310
    return NULL;
slouken@0
   311
}
slouken@0
   312
slouken@0
   313
#else
slouken@0
   314
slouken@0
   315
/* dummy TGA load routine */
slouken@0
   316
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
slouken@0
   317
{
slouken@0
   318
	return(NULL);
slouken@0
   319
}
slouken@0
   320
slouken@0
   321
#endif /* LOAD_TGA */