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