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