IMG_tga.c
changeset 471 c5078fe7c32c
parent 451 48116d511e5d
child 486 7bb8af91e887
     1.1 --- a/IMG_tga.c	Sun Apr 26 22:21:02 2015 -0700
     1.2 +++ b/IMG_tga.c	Sat May 02 19:35:56 2015 -0700
     1.3 @@ -111,37 +111,37 @@
     1.4      }
     1.5      start = SDL_RWtell(src);
     1.6  
     1.7 -    if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
     1.8 +    if (!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
     1.9          error = "Error reading TGA data";
    1.10 -    goto error;
    1.11 +        goto error;
    1.12      }
    1.13      ncols = LE16(hdr.cmap_len);
    1.14      switch(hdr.type) {
    1.15      case TGA_TYPE_RLE_INDEXED:
    1.16 -    rle = 1;
    1.17 -    /* fallthrough */
    1.18 +        rle = 1;
    1.19 +        /* fallthrough */
    1.20      case TGA_TYPE_INDEXED:
    1.21 -    if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
    1.22 -        goto unsupported;
    1.23 -    indexed = 1;
    1.24 -    break;
    1.25 +        if (!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
    1.26 +            goto unsupported;
    1.27 +        indexed = 1;
    1.28 +        break;
    1.29  
    1.30      case TGA_TYPE_RLE_RGB:
    1.31 -    rle = 1;
    1.32 -    /* fallthrough */
    1.33 +        rle = 1;
    1.34 +        /* fallthrough */
    1.35      case TGA_TYPE_RGB:
    1.36 -    indexed = 0;
    1.37 -    break;
    1.38 +        indexed = 0;
    1.39 +        break;
    1.40  
    1.41      case TGA_TYPE_RLE_BW:
    1.42 -    rle = 1;
    1.43 -    /* fallthrough */
    1.44 +        rle = 1;
    1.45 +        /* fallthrough */
    1.46      case TGA_TYPE_BW:
    1.47 -    if(hdr.pixel_bits != 8)
    1.48 -        goto unsupported;
    1.49 -    /* Treat greyscale as 8bpp indexed images */
    1.50 -    indexed = grey = 1;
    1.51 -    break;
    1.52 +        if (hdr.pixel_bits != 8)
    1.53 +            goto unsupported;
    1.54 +        /* Treat greyscale as 8bpp indexed images */
    1.55 +        indexed = grey = 1;
    1.56 +        break;
    1.57  
    1.58      default:
    1.59          goto unsupported;
    1.60 @@ -151,23 +151,23 @@
    1.61      rmask = gmask = bmask = amask = 0;
    1.62      switch(hdr.pixel_bits) {
    1.63      case 8:
    1.64 -    if(!indexed) {
    1.65 -            goto unsupported;
    1.66 -    }
    1.67 -    break;
    1.68 +        if (!indexed) {
    1.69 +                goto unsupported;
    1.70 +        }
    1.71 +        break;
    1.72  
    1.73      case 15:
    1.74      case 16:
    1.75 -    /* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
    1.76 -       is ignored for now. */
    1.77 -    rmask = 0x7c00;
    1.78 -    gmask = 0x03e0;
    1.79 -    bmask = 0x001f;
    1.80 -    break;
    1.81 +        /* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
    1.82 +           is ignored for now. */
    1.83 +        rmask = 0x7c00;
    1.84 +        gmask = 0x03e0;
    1.85 +        bmask = 0x001f;
    1.86 +        break;
    1.87  
    1.88      case 32:
    1.89 -    alpha = 1;
    1.90 -    /* fallthrough */
    1.91 +        alpha = 1;
    1.92 +        /* fallthrough */
    1.93      case 24:
    1.94  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    1.95          {
    1.96 @@ -183,13 +183,13 @@
    1.97          gmask = 0x0000ff00;
    1.98          bmask = 0x000000ff;
    1.99  #endif
   1.100 -    break;
   1.101 +        break;
   1.102  
   1.103      default:
   1.104          goto unsupported;
   1.105      }
   1.106  
   1.107 -    if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
   1.108 +    if ((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
   1.109         || hdr.flags & TGA_ORIGIN_RIGHT) {
   1.110          goto unsupported;
   1.111      }
   1.112 @@ -201,57 +201,57 @@
   1.113      img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
   1.114                     bpp * 8,
   1.115                     rmask, gmask, bmask, amask);
   1.116 -    if(img == NULL) {
   1.117 +    if (img == NULL) {
   1.118          error = "Out of memory";
   1.119          goto error;
   1.120      }
   1.121  
   1.122 -    if(hdr.has_cmap) {
   1.123 -    int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
   1.124 -    if(indexed && !grey) {
   1.125 -        Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
   1.126 -        SDL_Color *colors = img->format->palette->colors;
   1.127 -        img->format->palette->ncolors = ncols;
   1.128 -        SDL_RWread(src, pal, palsiz, 1);
   1.129 -        for(i = 0; i < ncols; i++) {
   1.130 -        switch(hdr.cmap_bits) {
   1.131 -        case 15:
   1.132 -        case 16:
   1.133 -            {
   1.134 -            Uint16 c = p[0] + (p[1] << 8);
   1.135 -            p += 2;
   1.136 -            colors[i].r = (c >> 7) & 0xf8;
   1.137 -            colors[i].g = (c >> 2) & 0xf8;
   1.138 -            colors[i].b = c << 3;
   1.139 +    if (hdr.has_cmap) {
   1.140 +        int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
   1.141 +        if (indexed && !grey) {
   1.142 +            Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
   1.143 +            SDL_Color *colors = img->format->palette->colors;
   1.144 +            img->format->palette->ncolors = ncols;
   1.145 +            SDL_RWread(src, pal, palsiz, 1);
   1.146 +            for(i = 0; i < ncols; i++) {
   1.147 +                switch(hdr.cmap_bits) {
   1.148 +                case 15:
   1.149 +                case 16:
   1.150 +                    {
   1.151 +                    Uint16 c = p[0] + (p[1] << 8);
   1.152 +                    p += 2;
   1.153 +                    colors[i].r = (c >> 7) & 0xf8;
   1.154 +                    colors[i].g = (c >> 2) & 0xf8;
   1.155 +                    colors[i].b = c << 3;
   1.156 +                    }
   1.157 +                    break;
   1.158 +                case 24:
   1.159 +                case 32:
   1.160 +                    colors[i].b = *p++;
   1.161 +                    colors[i].g = *p++;
   1.162 +                    colors[i].r = *p++;
   1.163 +                    if (hdr.cmap_bits == 32 && *p++ < 128)
   1.164 +                    ckey = i;
   1.165 +                    break;
   1.166 +                }
   1.167              }
   1.168 -            break;
   1.169 -        case 24:
   1.170 -        case 32:
   1.171 -            colors[i].b = *p++;
   1.172 -            colors[i].g = *p++;
   1.173 -            colors[i].r = *p++;
   1.174 -            if(hdr.cmap_bits == 32 && *p++ < 128)
   1.175 -            ckey = i;
   1.176 -            break;
   1.177 +            SDL_free(pal);
   1.178 +            if (ckey >= 0)
   1.179 +                SDL_SetColorKey(img, SDL_TRUE, ckey);
   1.180 +        } else {
   1.181 +            /* skip unneeded colormap */
   1.182 +            SDL_RWseek(src, palsiz, RW_SEEK_CUR);
   1.183          }
   1.184 -        }
   1.185 -        SDL_free(pal);
   1.186 -        if(ckey >= 0)
   1.187 -        SDL_SetColorKey(img, SDL_TRUE, ckey);
   1.188 -    } else {
   1.189 -        /* skip unneeded colormap */
   1.190 -        SDL_RWseek(src, palsiz, RW_SEEK_CUR);
   1.191 -    }
   1.192      }
   1.193  
   1.194 -    if(grey) {
   1.195 -    SDL_Color *colors = img->format->palette->colors;
   1.196 -    for(i = 0; i < 256; i++)
   1.197 -        colors[i].r = colors[i].g = colors[i].b = i;
   1.198 -    img->format->palette->ncolors = 256;
   1.199 +    if (grey) {
   1.200 +        SDL_Color *colors = img->format->palette->colors;
   1.201 +        for(i = 0; i < 256; i++)
   1.202 +            colors[i].r = colors[i].g = colors[i].b = i;
   1.203 +        img->format->palette->ncolors = 256;
   1.204      }
   1.205  
   1.206 -    if(hdr.flags & TGA_ORIGIN_UPPER) {
   1.207 +    if (hdr.flags & TGA_ORIGIN_UPPER) {
   1.208          lstep = img->pitch;
   1.209          dst = (Uint8 *)img->pixels;
   1.210      } else {
   1.211 @@ -263,55 +263,54 @@
   1.212         spans not to wrap across scan lines */
   1.213      count = rep = 0;
   1.214      for(i = 0; i < h; i++) {
   1.215 -    if(rle) {
   1.216 -        int x = 0;
   1.217 -        for(;;) {
   1.218 -        Uint8 c;
   1.219 +        if (rle) {
   1.220 +            int x = 0;
   1.221 +            for(;;) {
   1.222 +                Uint8 c;
   1.223  
   1.224 -        if(count) {
   1.225 -            int n = count;
   1.226 -            if(n > w - x)
   1.227 -            n = w - x;
   1.228 -            SDL_RWread(src, dst + x * bpp, n * bpp, 1);
   1.229 -            count -= n;
   1.230 -            x += n;
   1.231 -            if(x == w)
   1.232 -            break;
   1.233 -        } else if(rep) {
   1.234 -            int n = rep;
   1.235 -            if(n > w - x)
   1.236 -            n = w - x;
   1.237 -            rep -= n;
   1.238 -            while(n--) {
   1.239 -            SDL_memcpy(dst + x * bpp, &pixel, bpp);
   1.240 -            x++;
   1.241 +                if (count) {
   1.242 +                    int n = count;
   1.243 +                    if (n > w - x)
   1.244 +                        n = w - x;
   1.245 +                    SDL_RWread(src, dst + x * bpp, n * bpp, 1);
   1.246 +                    count -= n;
   1.247 +                    x += n;
   1.248 +                    if (x == w)
   1.249 +                        break;
   1.250 +                } else if (rep) {
   1.251 +                    int n = rep;
   1.252 +                    if (n > w - x)
   1.253 +                        n = w - x;
   1.254 +                    rep -= n;
   1.255 +                    while (n--) {
   1.256 +                        SDL_memcpy(dst + x * bpp, &pixel, bpp);
   1.257 +                        x++;
   1.258 +                    }
   1.259 +                    if (x == w)
   1.260 +                        break;
   1.261 +                }
   1.262 +
   1.263 +                SDL_RWread(src, &c, 1, 1);
   1.264 +                if (c & 0x80) {
   1.265 +                    SDL_RWread(src, &pixel, bpp, 1);
   1.266 +                    rep = (c & 0x7f) + 1;
   1.267 +                } else {
   1.268 +                    count = c + 1;
   1.269 +                }
   1.270              }
   1.271 -            if(x == w)
   1.272 -            break;
   1.273 +        } else {
   1.274 +            SDL_RWread(src, dst, w * bpp, 1);
   1.275          }
   1.276 -
   1.277 -        SDL_RWread(src, &c, 1, 1);
   1.278 -        if(c & 0x80) {
   1.279 -            SDL_RWread(src, &pixel, bpp, 1);
   1.280 -            rep = (c & 0x7f) + 1;
   1.281 -        } else {
   1.282 -            count = c + 1;
   1.283 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.284 +        if (bpp == 2) {
   1.285 +            /* swap byte order */
   1.286 +            int x;
   1.287 +            Uint16 *p = (Uint16 *)dst;
   1.288 +            for(x = 0; x < w; x++)
   1.289 +            p[x] = SDL_Swap16(p[x]);
   1.290          }
   1.291 -        }
   1.292 -
   1.293 -    } else {
   1.294 -        SDL_RWread(src, dst, w * bpp, 1);
   1.295 -    }
   1.296 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.297 -    if (bpp == 2) {
   1.298 -        /* swap byte order */
   1.299 -        int x;
   1.300 -        Uint16 *p = (Uint16 *)dst;
   1.301 -        for(x = 0; x < w; x++)
   1.302 -        p[x] = SDL_Swap16(p[x]);
   1.303 -    }
   1.304  #endif
   1.305 -    dst += lstep;
   1.306 +        dst += lstep;
   1.307      }
   1.308      return img;
   1.309