IMG_tga.c
changeset 368 8a61842d00ce
parent 347 ad5034cad524
child 389 1ff9cd8cf18b
     1.1 --- a/IMG_tga.c	Thu Apr 25 00:22:51 2013 -0700
     1.2 +++ b/IMG_tga.c	Tue May 21 21:24:32 2013 -0700
     1.3 @@ -44,19 +44,19 @@
     1.4   */
     1.5  
     1.6  struct TGAheader {
     1.7 -    Uint8 infolen;		/* length of info field */
     1.8 -    Uint8 has_cmap;		/* 1 if image has colormap, 0 otherwise */
     1.9 +    Uint8 infolen;      /* length of info field */
    1.10 +    Uint8 has_cmap;     /* 1 if image has colormap, 0 otherwise */
    1.11      Uint8 type;
    1.12  
    1.13 -    Uint8 cmap_start[2];	/* index of first colormap entry */
    1.14 -    Uint8 cmap_len[2];		/* number of entries in colormap */
    1.15 -    Uint8 cmap_bits;		/* bits per colormap entry */
    1.16 +    Uint8 cmap_start[2];    /* index of first colormap entry */
    1.17 +    Uint8 cmap_len[2];      /* number of entries in colormap */
    1.18 +    Uint8 cmap_bits;        /* bits per colormap entry */
    1.19  
    1.20 -    Uint8 yorigin[2];		/* image origin (ignored here) */
    1.21 +    Uint8 yorigin[2];       /* image origin (ignored here) */
    1.22      Uint8 xorigin[2];
    1.23 -    Uint8 width[2];		/* image size */
    1.24 +    Uint8 width[2];     /* image size */
    1.25      Uint8 height[2];
    1.26 -    Uint8 pixel_bits;		/* bits/pixel */
    1.27 +    Uint8 pixel_bits;       /* bits/pixel */
    1.28      Uint8 flags;
    1.29  };
    1.30  
    1.31 @@ -69,16 +69,16 @@
    1.32      TGA_TYPE_RLE_BW = 11
    1.33  };
    1.34  
    1.35 -#define TGA_INTERLEAVE_MASK	0xc0
    1.36 -#define TGA_INTERLEAVE_NONE	0x00
    1.37 -#define TGA_INTERLEAVE_2WAY	0x40
    1.38 -#define TGA_INTERLEAVE_4WAY	0x80
    1.39 +#define TGA_INTERLEAVE_MASK 0xc0
    1.40 +#define TGA_INTERLEAVE_NONE 0x00
    1.41 +#define TGA_INTERLEAVE_2WAY 0x40
    1.42 +#define TGA_INTERLEAVE_4WAY 0x80
    1.43  
    1.44 -#define TGA_ORIGIN_MASK		0x30
    1.45 -#define TGA_ORIGIN_LEFT		0x00
    1.46 -#define TGA_ORIGIN_RIGHT	0x10
    1.47 -#define TGA_ORIGIN_LOWER	0x00
    1.48 -#define TGA_ORIGIN_UPPER	0x20
    1.49 +#define TGA_ORIGIN_MASK     0x30
    1.50 +#define TGA_ORIGIN_LEFT     0x00
    1.51 +#define TGA_ORIGIN_RIGHT    0x10
    1.52 +#define TGA_ORIGIN_LOWER    0x00
    1.53 +#define TGA_ORIGIN_UPPER    0x20
    1.54  
    1.55  /* read/write unaligned little-endian 16-bit ints */
    1.56  #define LE16(p) ((p)[0] + ((p)[1] << 8))
    1.57 @@ -113,35 +113,35 @@
    1.58  
    1.59      if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
    1.60          error = "Error reading TGA data";
    1.61 -	goto error;
    1.62 +    goto error;
    1.63      }
    1.64      ncols = LE16(hdr.cmap_len);
    1.65      switch(hdr.type) {
    1.66      case TGA_TYPE_RLE_INDEXED:
    1.67 -	rle = 1;
    1.68 -	/* fallthrough */
    1.69 +    rle = 1;
    1.70 +    /* fallthrough */
    1.71      case TGA_TYPE_INDEXED:
    1.72 -	if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
    1.73 -	    goto unsupported;
    1.74 -	indexed = 1;
    1.75 -	break;
    1.76 +    if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
    1.77 +        goto unsupported;
    1.78 +    indexed = 1;
    1.79 +    break;
    1.80  
    1.81      case TGA_TYPE_RLE_RGB:
    1.82 -	rle = 1;
    1.83 -	/* fallthrough */
    1.84 +    rle = 1;
    1.85 +    /* fallthrough */
    1.86      case TGA_TYPE_RGB:
    1.87 -	indexed = 0;
    1.88 -	break;
    1.89 +    indexed = 0;
    1.90 +    break;
    1.91  
    1.92      case TGA_TYPE_RLE_BW:
    1.93 -	rle = 1;
    1.94 -	/* fallthrough */
    1.95 +    rle = 1;
    1.96 +    /* fallthrough */
    1.97      case TGA_TYPE_BW:
    1.98 -	if(hdr.pixel_bits != 8)
    1.99 -	    goto unsupported;
   1.100 -	/* Treat greyscale as 8bpp indexed images */
   1.101 -	indexed = grey = 1;
   1.102 -	break;
   1.103 +    if(hdr.pixel_bits != 8)
   1.104 +        goto unsupported;
   1.105 +    /* Treat greyscale as 8bpp indexed images */
   1.106 +    indexed = grey = 1;
   1.107 +    break;
   1.108  
   1.109      default:
   1.110          goto unsupported;
   1.111 @@ -151,39 +151,39 @@
   1.112      rmask = gmask = bmask = amask = 0;
   1.113      switch(hdr.pixel_bits) {
   1.114      case 8:
   1.115 -	if(!indexed) {
   1.116 +    if(!indexed) {
   1.117              goto unsupported;
   1.118 -	}
   1.119 -	break;
   1.120 +    }
   1.121 +    break;
   1.122  
   1.123      case 15:
   1.124      case 16:
   1.125 -	/* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
   1.126 -	   is ignored for now. */
   1.127 -	rmask = 0x7c00;
   1.128 -	gmask = 0x03e0;
   1.129 -	bmask = 0x001f;
   1.130 -	break;
   1.131 +    /* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
   1.132 +       is ignored for now. */
   1.133 +    rmask = 0x7c00;
   1.134 +    gmask = 0x03e0;
   1.135 +    bmask = 0x001f;
   1.136 +    break;
   1.137  
   1.138      case 32:
   1.139 -	alpha = 1;
   1.140 -	/* fallthrough */
   1.141 +    alpha = 1;
   1.142 +    /* fallthrough */
   1.143      case 24:
   1.144  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.145 -		{
   1.146 -	    int s = alpha ? 0 : 8;
   1.147 -	    amask = 0x000000ff >> s;
   1.148 -	    rmask = 0x0000ff00 >> s;
   1.149 -	    gmask = 0x00ff0000 >> s;
   1.150 -	    bmask = 0xff000000 >> s;
   1.151 -		}
   1.152 +        {
   1.153 +        int s = alpha ? 0 : 8;
   1.154 +        amask = 0x000000ff >> s;
   1.155 +        rmask = 0x0000ff00 >> s;
   1.156 +        gmask = 0x00ff0000 >> s;
   1.157 +        bmask = 0xff000000 >> s;
   1.158 +        }
   1.159  #else
   1.160 -	    amask = alpha ? 0xff000000 : 0;
   1.161 -	    rmask = 0x00ff0000;
   1.162 -	    gmask = 0x0000ff00;
   1.163 -	    bmask = 0x000000ff;
   1.164 +        amask = alpha ? 0xff000000 : 0;
   1.165 +        rmask = 0x00ff0000;
   1.166 +        gmask = 0x0000ff00;
   1.167 +        bmask = 0x000000ff;
   1.168  #endif
   1.169 -	break;
   1.170 +    break;
   1.171  
   1.172      default:
   1.173          goto unsupported;
   1.174 @@ -193,125 +193,125 @@
   1.175         || hdr.flags & TGA_ORIGIN_RIGHT) {
   1.176          goto unsupported;
   1.177      }
   1.178 -    
   1.179 +
   1.180      SDL_RWseek(src, hdr.infolen, RW_SEEK_CUR); /* skip info field */
   1.181  
   1.182      w = LE16(hdr.width);
   1.183      h = LE16(hdr.height);
   1.184      img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
   1.185 -			       bpp * 8,
   1.186 -			       rmask, gmask, bmask, amask);
   1.187 +                   bpp * 8,
   1.188 +                   rmask, gmask, bmask, amask);
   1.189      if(img == NULL) {
   1.190          error = "Out of memory";
   1.191          goto error;
   1.192      }
   1.193  
   1.194      if(hdr.has_cmap) {
   1.195 -	int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
   1.196 -	if(indexed && !grey) {
   1.197 -	    Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
   1.198 -	    SDL_Color *colors = img->format->palette->colors;
   1.199 -	    img->format->palette->ncolors = ncols;
   1.200 -	    SDL_RWread(src, pal, palsiz, 1);
   1.201 -	    for(i = 0; i < ncols; i++) {
   1.202 -		switch(hdr.cmap_bits) {
   1.203 -		case 15:
   1.204 -		case 16:
   1.205 -		    {
   1.206 -			Uint16 c = p[0] + (p[1] << 8);
   1.207 -			p += 2;
   1.208 -			colors[i].r = (c >> 7) & 0xf8;
   1.209 -			colors[i].g = (c >> 2) & 0xf8;
   1.210 -			colors[i].b = c << 3;
   1.211 -		    }
   1.212 -		    break;
   1.213 -		case 24:
   1.214 -		case 32:
   1.215 -		    colors[i].b = *p++;
   1.216 -		    colors[i].g = *p++;
   1.217 -		    colors[i].r = *p++;
   1.218 -		    if(hdr.cmap_bits == 32 && *p++ < 128)
   1.219 -			ckey = i;
   1.220 -		    break;
   1.221 -		}
   1.222 -	    }
   1.223 -	    SDL_free(pal);
   1.224 -	    if(ckey >= 0)
   1.225 -		SDL_SetColorKey(img, SDL_TRUE, ckey);
   1.226 -	} else {
   1.227 -	    /* skip unneeded colormap */
   1.228 -	    SDL_RWseek(src, palsiz, RW_SEEK_CUR);
   1.229 -	}
   1.230 +    int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
   1.231 +    if(indexed && !grey) {
   1.232 +        Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
   1.233 +        SDL_Color *colors = img->format->palette->colors;
   1.234 +        img->format->palette->ncolors = ncols;
   1.235 +        SDL_RWread(src, pal, palsiz, 1);
   1.236 +        for(i = 0; i < ncols; i++) {
   1.237 +        switch(hdr.cmap_bits) {
   1.238 +        case 15:
   1.239 +        case 16:
   1.240 +            {
   1.241 +            Uint16 c = p[0] + (p[1] << 8);
   1.242 +            p += 2;
   1.243 +            colors[i].r = (c >> 7) & 0xf8;
   1.244 +            colors[i].g = (c >> 2) & 0xf8;
   1.245 +            colors[i].b = c << 3;
   1.246 +            }
   1.247 +            break;
   1.248 +        case 24:
   1.249 +        case 32:
   1.250 +            colors[i].b = *p++;
   1.251 +            colors[i].g = *p++;
   1.252 +            colors[i].r = *p++;
   1.253 +            if(hdr.cmap_bits == 32 && *p++ < 128)
   1.254 +            ckey = i;
   1.255 +            break;
   1.256 +        }
   1.257 +        }
   1.258 +        SDL_free(pal);
   1.259 +        if(ckey >= 0)
   1.260 +        SDL_SetColorKey(img, SDL_TRUE, ckey);
   1.261 +    } else {
   1.262 +        /* skip unneeded colormap */
   1.263 +        SDL_RWseek(src, palsiz, RW_SEEK_CUR);
   1.264 +    }
   1.265      }
   1.266  
   1.267      if(grey) {
   1.268 -	SDL_Color *colors = img->format->palette->colors;
   1.269 -	for(i = 0; i < 256; i++)
   1.270 -	    colors[i].r = colors[i].g = colors[i].b = i;
   1.271 -	img->format->palette->ncolors = 256;
   1.272 +    SDL_Color *colors = img->format->palette->colors;
   1.273 +    for(i = 0; i < 256; i++)
   1.274 +        colors[i].r = colors[i].g = colors[i].b = i;
   1.275 +    img->format->palette->ncolors = 256;
   1.276      }
   1.277  
   1.278      if(hdr.flags & TGA_ORIGIN_UPPER) {
   1.279 -		lstep = img->pitch;
   1.280 -		dst = (Uint8 *)img->pixels;
   1.281 +        lstep = img->pitch;
   1.282 +        dst = (Uint8 *)img->pixels;
   1.283      } else {
   1.284 -		lstep = -img->pitch;
   1.285 -		dst = (Uint8 *)img->pixels + (h - 1) * img->pitch;
   1.286 +        lstep = -img->pitch;
   1.287 +        dst = (Uint8 *)img->pixels + (h - 1) * img->pitch;
   1.288      }
   1.289  
   1.290      /* The RLE decoding code is slightly convoluted since we can't rely on
   1.291         spans not to wrap across scan lines */
   1.292      count = rep = 0;
   1.293      for(i = 0; i < h; i++) {
   1.294 -	if(rle) {
   1.295 -	    int x = 0;
   1.296 -	    for(;;) {
   1.297 -		Uint8 c;
   1.298 +    if(rle) {
   1.299 +        int x = 0;
   1.300 +        for(;;) {
   1.301 +        Uint8 c;
   1.302  
   1.303 -		if(count) {
   1.304 -		    int n = count;
   1.305 -		    if(n > w - x)
   1.306 -			n = w - x;
   1.307 -		    SDL_RWread(src, dst + x * bpp, n * bpp, 1);
   1.308 -		    count -= n;
   1.309 -		    x += n;
   1.310 -		    if(x == w)
   1.311 -			break;
   1.312 -		} else if(rep) {
   1.313 -		    int n = rep;
   1.314 -		    if(n > w - x)
   1.315 -			n = w - x;
   1.316 -		    rep -= n;
   1.317 -		    while(n--) {
   1.318 -			SDL_memcpy(dst + x * bpp, &pixel, bpp);
   1.319 -			x++;
   1.320 -		    }
   1.321 -		    if(x == w)
   1.322 -			break;
   1.323 -		}
   1.324 +        if(count) {
   1.325 +            int n = count;
   1.326 +            if(n > w - x)
   1.327 +            n = w - x;
   1.328 +            SDL_RWread(src, dst + x * bpp, n * bpp, 1);
   1.329 +            count -= n;
   1.330 +            x += n;
   1.331 +            if(x == w)
   1.332 +            break;
   1.333 +        } else if(rep) {
   1.334 +            int n = rep;
   1.335 +            if(n > w - x)
   1.336 +            n = w - x;
   1.337 +            rep -= n;
   1.338 +            while(n--) {
   1.339 +            SDL_memcpy(dst + x * bpp, &pixel, bpp);
   1.340 +            x++;
   1.341 +            }
   1.342 +            if(x == w)
   1.343 +            break;
   1.344 +        }
   1.345  
   1.346 -		SDL_RWread(src, &c, 1, 1);
   1.347 -		if(c & 0x80) {
   1.348 -		    SDL_RWread(src, &pixel, bpp, 1);
   1.349 -		    rep = (c & 0x7f) + 1;
   1.350 -		} else {
   1.351 -		    count = c + 1;
   1.352 -		}
   1.353 -	    }
   1.354 +        SDL_RWread(src, &c, 1, 1);
   1.355 +        if(c & 0x80) {
   1.356 +            SDL_RWread(src, &pixel, bpp, 1);
   1.357 +            rep = (c & 0x7f) + 1;
   1.358 +        } else {
   1.359 +            count = c + 1;
   1.360 +        }
   1.361 +        }
   1.362  
   1.363 -	} else {
   1.364 -	    SDL_RWread(src, dst, w * bpp, 1);
   1.365 -	}
   1.366 +    } else {
   1.367 +        SDL_RWread(src, dst, w * bpp, 1);
   1.368 +    }
   1.369  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.370 -	if (bpp == 2) {
   1.371 -	    /* swap byte order */
   1.372 -	    int x;
   1.373 -	    Uint16 *p = (Uint16 *)dst;
   1.374 -	    for(x = 0; x < w; x++)
   1.375 -		p[x] = SDL_Swap16(p[x]);
   1.376 -	}
   1.377 +    if (bpp == 2) {
   1.378 +        /* swap byte order */
   1.379 +        int x;
   1.380 +        Uint16 *p = (Uint16 *)dst;
   1.381 +        for(x = 0; x < w; x++)
   1.382 +        p[x] = SDL_Swap16(p[x]);
   1.383 +    }
   1.384  #endif
   1.385 -	dst += lstep;
   1.386 +    dst += lstep;
   1.387      }
   1.388      return img;
   1.389  
   1.390 @@ -332,7 +332,7 @@
   1.391  /* dummy TGA load routine */
   1.392  SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
   1.393  {
   1.394 -	return(NULL);
   1.395 +    return(NULL);
   1.396  }
   1.397  
   1.398  #endif /* LOAD_TGA */