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