Skip to content

Commit

Permalink
Fixed whitespace so it's easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed May 3, 2015
1 parent 544623c commit a2b9ccd
Showing 1 changed file with 117 additions and 118 deletions.
235 changes: 117 additions & 118 deletions IMG_tga.c
Expand Up @@ -111,37 +111,37 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
}
start = SDL_RWtell(src);

if(!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
if (!SDL_RWread(src, &hdr, sizeof(hdr), 1)) {
error = "Error reading TGA data";
goto error;
goto error;
}
ncols = LE16(hdr.cmap_len);
switch(hdr.type) {
case TGA_TYPE_RLE_INDEXED:
rle = 1;
/* fallthrough */
rle = 1;
/* fallthrough */
case TGA_TYPE_INDEXED:
if(!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
goto unsupported;
indexed = 1;
break;
if (!hdr.has_cmap || hdr.pixel_bits != 8 || ncols > 256)
goto unsupported;
indexed = 1;
break;

case TGA_TYPE_RLE_RGB:
rle = 1;
/* fallthrough */
rle = 1;
/* fallthrough */
case TGA_TYPE_RGB:
indexed = 0;
break;
indexed = 0;
break;

case TGA_TYPE_RLE_BW:
rle = 1;
/* fallthrough */
rle = 1;
/* fallthrough */
case TGA_TYPE_BW:
if(hdr.pixel_bits != 8)
goto unsupported;
/* Treat greyscale as 8bpp indexed images */
indexed = grey = 1;
break;
if (hdr.pixel_bits != 8)
goto unsupported;
/* Treat greyscale as 8bpp indexed images */
indexed = grey = 1;
break;

default:
goto unsupported;
Expand All @@ -151,23 +151,23 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
rmask = gmask = bmask = amask = 0;
switch(hdr.pixel_bits) {
case 8:
if(!indexed) {
goto unsupported;
}
break;
if (!indexed) {
goto unsupported;
}
break;

case 15:
case 16:
/* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
is ignored for now. */
rmask = 0x7c00;
gmask = 0x03e0;
bmask = 0x001f;
break;
/* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
is ignored for now. */
rmask = 0x7c00;
gmask = 0x03e0;
bmask = 0x001f;
break;

case 32:
alpha = 1;
/* fallthrough */
alpha = 1;
/* fallthrough */
case 24:
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
{
Expand All @@ -183,13 +183,13 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
gmask = 0x0000ff00;
bmask = 0x000000ff;
#endif
break;
break;

default:
goto unsupported;
}

if((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
if ((hdr.flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE
|| hdr.flags & TGA_ORIGIN_RIGHT) {
goto unsupported;
}
Expand All @@ -201,57 +201,57 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
bpp * 8,
rmask, gmask, bmask, amask);
if(img == NULL) {
if (img == NULL) {
error = "Out of memory";
goto error;
}

if(hdr.has_cmap) {
int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
if(indexed && !grey) {
Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
SDL_Color *colors = img->format->palette->colors;
img->format->palette->ncolors = ncols;
SDL_RWread(src, pal, palsiz, 1);
for(i = 0; i < ncols; i++) {
switch(hdr.cmap_bits) {
case 15:
case 16:
{
Uint16 c = p[0] + (p[1] << 8);
p += 2;
colors[i].r = (c >> 7) & 0xf8;
colors[i].g = (c >> 2) & 0xf8;
colors[i].b = c << 3;
if (hdr.has_cmap) {
int palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
if (indexed && !grey) {
Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
SDL_Color *colors = img->format->palette->colors;
img->format->palette->ncolors = ncols;
SDL_RWread(src, pal, palsiz, 1);
for(i = 0; i < ncols; i++) {
switch(hdr.cmap_bits) {
case 15:
case 16:
{
Uint16 c = p[0] + (p[1] << 8);
p += 2;
colors[i].r = (c >> 7) & 0xf8;
colors[i].g = (c >> 2) & 0xf8;
colors[i].b = c << 3;
}
break;
case 24:
case 32:
colors[i].b = *p++;
colors[i].g = *p++;
colors[i].r = *p++;
if (hdr.cmap_bits == 32 && *p++ < 128)
ckey = i;
break;
}
}
break;
case 24:
case 32:
colors[i].b = *p++;
colors[i].g = *p++;
colors[i].r = *p++;
if(hdr.cmap_bits == 32 && *p++ < 128)
ckey = i;
break;
}
SDL_free(pal);
if (ckey >= 0)
SDL_SetColorKey(img, SDL_TRUE, ckey);
} else {
/* skip unneeded colormap */
SDL_RWseek(src, palsiz, RW_SEEK_CUR);
}
SDL_free(pal);
if(ckey >= 0)
SDL_SetColorKey(img, SDL_TRUE, ckey);
} else {
/* skip unneeded colormap */
SDL_RWseek(src, palsiz, RW_SEEK_CUR);
}
}

if(grey) {
SDL_Color *colors = img->format->palette->colors;
for(i = 0; i < 256; i++)
colors[i].r = colors[i].g = colors[i].b = i;
img->format->palette->ncolors = 256;
if (grey) {
SDL_Color *colors = img->format->palette->colors;
for(i = 0; i < 256; i++)
colors[i].r = colors[i].g = colors[i].b = i;
img->format->palette->ncolors = 256;
}

if(hdr.flags & TGA_ORIGIN_UPPER) {
if (hdr.flags & TGA_ORIGIN_UPPER) {
lstep = img->pitch;
dst = (Uint8 *)img->pixels;
} else {
Expand All @@ -263,55 +263,54 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
spans not to wrap across scan lines */
count = rep = 0;
for(i = 0; i < h; i++) {
if(rle) {
int x = 0;
for(;;) {
Uint8 c;

if(count) {
int n = count;
if(n > w - x)
n = w - x;
SDL_RWread(src, dst + x * bpp, n * bpp, 1);
count -= n;
x += n;
if(x == w)
break;
} else if(rep) {
int n = rep;
if(n > w - x)
n = w - x;
rep -= n;
while(n--) {
SDL_memcpy(dst + x * bpp, &pixel, bpp);
x++;
if (rle) {
int x = 0;
for(;;) {
Uint8 c;

if (count) {
int n = count;
if (n > w - x)
n = w - x;
SDL_RWread(src, dst + x * bpp, n * bpp, 1);
count -= n;
x += n;
if (x == w)
break;
} else if (rep) {
int n = rep;
if (n > w - x)
n = w - x;
rep -= n;
while (n--) {
SDL_memcpy(dst + x * bpp, &pixel, bpp);
x++;
}
if (x == w)
break;
}

SDL_RWread(src, &c, 1, 1);
if (c & 0x80) {
SDL_RWread(src, &pixel, bpp, 1);
rep = (c & 0x7f) + 1;
} else {
count = c + 1;
}
}
if(x == w)
break;
}

SDL_RWread(src, &c, 1, 1);
if(c & 0x80) {
SDL_RWread(src, &pixel, bpp, 1);
rep = (c & 0x7f) + 1;
} else {
count = c + 1;
}
SDL_RWread(src, dst, w * bpp, 1);
}

} else {
SDL_RWread(src, dst, w * bpp, 1);
}
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
if (bpp == 2) {
/* swap byte order */
int x;
Uint16 *p = (Uint16 *)dst;
for(x = 0; x < w; x++)
p[x] = SDL_Swap16(p[x]);
}
if (bpp == 2) {
/* swap byte order */
int x;
Uint16 *p = (Uint16 *)dst;
for(x = 0; x < w; x++)
p[x] = SDL_Swap16(p[x]);
}
#endif
dst += lstep;
dst += lstep;
}
return img;

Expand Down

0 comments on commit a2b9ccd

Please sign in to comment.