IMG_tga.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 May 2010 21:26:13 -0700
changeset 250 692379215a48
parent 249 8271062b627a
child 255 2d440550af78
permissions -rw-r--r--
Fixed linker error when building from the command line on Mac OS X
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@186
     3
    Copyright (C) 1997-2009 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@250
    23
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@250
    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
/* Load a TGA type image from an SDL datasource */
slouken@0
    87
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
slouken@0
    88
{
slouken@117
    89
    int start;
slouken@118
    90
    const char *error = NULL;
slouken@0
    91
    struct TGAheader hdr;
slouken@0
    92
    int rle = 0;
slouken@0
    93
    int alpha = 0;
slouken@0
    94
    int indexed = 0;
slouken@0
    95
    int grey = 0;
slouken@0
    96
    int ckey = -1;
slouken@0
    97
    int ncols, w, h;
slouken@118
    98
    SDL_Surface *img = NULL;
slouken@0
    99
    Uint32 rmask, gmask, bmask, amask;
slouken@0
   100
    Uint8 *dst;
slouken@0
   101
    int i;
slouken@0
   102
    int bpp;
slouken@0
   103
    int lstep;
slouken@0
   104
    Uint32 pixel;
slouken@0
   105
    int count, rep;
slouken@0
   106
slouken@98
   107
    if ( !src ) {
slouken@98
   108
        /* The error message has been set in SDL_RWFromFile */
slouken@98
   109
        return NULL;
slouken@98
   110
    }
slouken@118
   111
    start = SDL_RWtell(src);
slouken@98
   112
slouken@118
   113
    if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
slouken@118
   114
        error = "Error reading TGA data";
slouken@0
   115
	goto error;
slouken@118
   116
    }
slouken@0
   117
    ncols = LE16(hdr.cmap_len);
slouken@0
   118
    switch(hdr.type) {
slouken@0
   119
    case TGA_TYPE_RLE_INDEXED:
slouken@0
   120
	rle = 1;
slouken@0
   121
	/* fallthrough */
slouken@0
   122
    case TGA_TYPE_INDEXED:
slouken@0
   123
	if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
slouken@118
   124
	    goto unsupported;
slouken@0
   125
	indexed = 1;
slouken@0
   126
	break;
slouken@0
   127
slouken@0
   128
    case TGA_TYPE_RLE_RGB:
slouken@0
   129
	rle = 1;
slouken@0
   130
	/* fallthrough */
slouken@0
   131
    case TGA_TYPE_RGB:
slouken@0
   132
	indexed = 0;
slouken@0
   133
	break;
slouken@0
   134
slouken@0
   135
    case TGA_TYPE_RLE_BW:
slouken@0
   136
	rle = 1;
slouken@0
   137
	/* fallthrough */
slouken@0
   138
    case TGA_TYPE_BW:
slouken@0
   139
	if(hdr.pixel_bits != 8)
slouken@118
   140
	    goto unsupported;
slouken@0
   141
	/* Treat greyscale as 8bpp indexed images */
slouken@0
   142
	indexed = grey = 1;
slouken@0
   143
	break;
slouken@0
   144
slouken@0
   145
    default:
slouken@117
   146
        goto unsupported;
slouken@0
   147
    }
slouken@0
   148
slouken@0
   149
    bpp = (hdr.pixel_bits + 7) >> 3;
slouken@4
   150
    rmask = gmask = bmask = amask = 0;
slouken@0
   151
    switch(hdr.pixel_bits) {
slouken@0
   152
    case 8:
slouken@0
   153
	if(!indexed) {
slouken@117
   154
            goto unsupported;
slouken@0
   155
	}
slouken@0
   156
	break;
slouken@0
   157
slouken@0
   158
    case 15:
slouken@0
   159
    case 16:
slouken@0
   160
	/* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
slouken@0
   161
	   is ignored for now. */
slouken@0
   162
	rmask = 0x7c00;
slouken@0
   163
	gmask = 0x03e0;
slouken@0
   164
	bmask = 0x001f;
slouken@0
   165
	break;
slouken@0
   166
slouken@0
   167
    case 32:
slouken@0
   168
	alpha = 1;
slouken@0
   169
	/* fallthrough */
slouken@0
   170
    case 24:
slouken@0
   171
	if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
slouken@0
   172
	    int s = alpha ? 0 : 8;
slouken@0
   173
	    amask = 0x000000ff >> s;
slouken@0
   174
	    rmask = 0x0000ff00 >> s;
slouken@0
   175
	    gmask = 0x00ff0000 >> s;
slouken@0
   176
	    bmask = 0xff000000 >> s;
slouken@0
   177
	} else {
slouken@0
   178
	    amask = alpha ? 0xff000000 : 0;
slouken@0
   179
	    rmask = 0x00ff0000;
slouken@0
   180
	    gmask = 0x0000ff00;
slouken@0
   181
	    bmask = 0x000000ff;
slouken@0
   182
	}
slouken@0
   183
	break;
slouken@0
   184
slouken@0
   185
    default:
slouken@117
   186
        goto unsupported;
slouken@0
   187
    }
slouken@0
   188
slouken@0
   189
    if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
slouken@0
   190
       || hdr.flags & TGA_ORIGIN_RIGHT) {
slouken@117
   191
        goto unsupported;
slouken@0
   192
    }
slouken@0
   193
    
slouken@236
   194
    SDL_RWseek(src, hdr.infolen, RW_SEEK_CUR); /* skip info field */
slouken@0
   195
slouken@0
   196
    w = LE16(hdr.width);
slouken@0
   197
    h = LE16(hdr.height);
slouken@0
   198
    img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
slouken@0
   199
			       bpp * 8,
slouken@0
   200
			       rmask, gmask, bmask, amask);
slouken@118
   201
    if(img == NULL) {
slouken@118
   202
        error = "Out of memory";
slouken@118
   203
        goto error;
slouken@118
   204
    }
slouken@0
   205
slouken@0
   206
    if(hdr.has_cmap) {
slouken@0
   207
	int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
slouken@0
   208
	if(indexed && !grey) {
slouken@0
   209
	    Uint8 *pal = malloc(palsiz), *p = pal;
slouken@0
   210
	    SDL_Color *colors = img->format->palette->colors;
slouken@0
   211
	    img->format->palette->ncolors = ncols;
slouken@0
   212
	    SDL_RWread(src, pal, palsiz, 1);
slouken@0
   213
	    for(i = 0; i < ncols; i++) {
slouken@0
   214
		switch(hdr.cmap_bits) {
slouken@0
   215
		case 15:
slouken@0
   216
		case 16:
slouken@0
   217
		    {
slouken@0
   218
			Uint16 c = p[0] + (p[1] << 8);
slouken@0
   219
			p += 2;
slouken@0
   220
			colors[i].r = (c >> 7) & 0xf8;
slouken@0
   221
			colors[i].g = (c >> 2) & 0xf8;
slouken@0
   222
			colors[i].b = c << 3;
slouken@0
   223
		    }
slouken@0
   224
		    break;
slouken@0
   225
		case 24:
slouken@0
   226
		case 32:
slouken@0
   227
		    colors[i].b = *p++;
slouken@0
   228
		    colors[i].g = *p++;
slouken@0
   229
		    colors[i].r = *p++;
slouken@0
   230
		    if(hdr.cmap_bits == 32 && *p++ < 128)
slouken@0
   231
			ckey = i;
slouken@0
   232
		    break;
slouken@0
   233
		}
slouken@0
   234
	    }
slouken@0
   235
	    free(pal);
slouken@0
   236
	    if(ckey >= 0)
slouken@0
   237
		SDL_SetColorKey(img, SDL_SRCCOLORKEY, ckey);
slouken@0
   238
	} else {
slouken@0
   239
	    /* skip unneeded colormap */
slouken@236
   240
	    SDL_RWseek(src, palsiz, RW_SEEK_CUR);
slouken@0
   241
	}
slouken@0
   242
    }
slouken@0
   243
slouken@0
   244
    if(grey) {
slouken@0
   245
	SDL_Color *colors = img->format->palette->colors;
slouken@0
   246
	for(i = 0; i < 256; i++)
slouken@0
   247
	    colors[i].r = colors[i].g = colors[i].b = i;
slouken@0
   248
	img->format->palette->ncolors = 256;
slouken@0
   249
    }
slouken@0
   250
slouken@0
   251
    if(hdr.flags & TGA_ORIGIN_UPPER) {
slouken@0
   252
	lstep = img->pitch;
slouken@4
   253
	dst = img->pixels;
slouken@0
   254
    } else {
slouken@0
   255
	lstep = -img->pitch;
slouken@0
   256
	dst = (Uint8 *)img->pixels + (h - 1) * img->pitch;
slouken@0
   257
    }
slouken@0
   258
slouken@0
   259
    /* The RLE decoding code is slightly convoluted since we can't rely on
slouken@0
   260
       spans not to wrap across scan lines */
slouken@0
   261
    count = rep = 0;
slouken@0
   262
    for(i = 0; i < h; i++) {
slouken@0
   263
	if(rle) {
slouken@0
   264
	    int x = 0;
slouken@0
   265
	    for(;;) {
slouken@0
   266
		Uint8 c;
slouken@0
   267
slouken@0
   268
		if(count) {
slouken@0
   269
		    int n = count;
slouken@0
   270
		    if(n > w - x)
slouken@0
   271
			n = w - x;
slouken@0
   272
		    SDL_RWread(src, dst + x * bpp, n * bpp, 1);
slouken@0
   273
		    count -= n;
slouken@0
   274
		    x += n;
slouken@0
   275
		    if(x == w)
slouken@0
   276
			break;
slouken@0
   277
		} else if(rep) {
slouken@0
   278
		    int n = rep;
slouken@0
   279
		    if(n > w - x)
slouken@0
   280
			n = w - x;
slouken@0
   281
		    rep -= n;
slouken@0
   282
		    while(n--) {
slouken@0
   283
			memcpy(dst + x * bpp, &pixel, bpp);
slouken@0
   284
			x++;
slouken@0
   285
		    }
slouken@0
   286
		    if(x == w)
slouken@0
   287
			break;
slouken@0
   288
		}
slouken@0
   289
slouken@0
   290
		SDL_RWread(src, &c, 1, 1);
slouken@0
   291
		if(c & 0x80) {
slouken@0
   292
		    SDL_RWread(src, &pixel, bpp, 1);
slouken@0
   293
		    rep = (c & 0x7f) + 1;
slouken@0
   294
		} else {
slouken@0
   295
		    count = c + 1;
slouken@0
   296
		}
slouken@0
   297
	    }
slouken@0
   298
slouken@0
   299
	} else {
slouken@0
   300
	    SDL_RWread(src, dst, w * bpp, 1);
slouken@0
   301
	}
slouken@0
   302
	if(SDL_BYTEORDER == SDL_BIG_ENDIAN && bpp == 2) {
slouken@0
   303
	    /* swap byte order */
slouken@0
   304
	    int x;
slouken@0
   305
	    Uint16 *p = (Uint16 *)dst;
slouken@0
   306
	    for(x = 0; x < w; x++)
slouken@0
   307
		p[x] = SDL_Swap16(p[x]);
slouken@0
   308
	}
slouken@0
   309
	dst += lstep;
slouken@0
   310
    }
slouken@0
   311
    return img;
slouken@0
   312
slouken@118
   313
unsupported:
slouken@118
   314
    error = "Unsupported TGA format";
slouken@118
   315
slouken@0
   316
error:
slouken@236
   317
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@118
   318
    if ( img ) {
slouken@118
   319
        SDL_FreeSurface(img);
slouken@118
   320
    }
slouken@118
   321
    IMG_SetError(error);
slouken@117
   322
    return NULL;
slouken@0
   323
}
slouken@0
   324
slouken@0
   325
#else
slouken@0
   326
slouken@0
   327
/* dummy TGA load routine */
slouken@0
   328
SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
slouken@0
   329
{
slouken@0
   330
	return(NULL);
slouken@0
   331
}
slouken@0
   332
slouken@0
   333
#endif /* LOAD_TGA */
slouken@250
   334
slouken@250
   335
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */