Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/pcx.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,21 @@ BITMAP *load_pcx_pf(PACKFILE *f, RGB *pal)
#endif

while (x < bytes_per_line*bpp/8) {
ch = pack_getc(f);
c = pack_getc(f);
if (c == EOF) {
destroy_bitmap(b);
return NULL;
}
ch = c;
if ((ch & 0xC0) == 0xC0) {
c = (ch & 0x3F);
ch = pack_getc(f);
int run_count = (ch & 0x3F);
c = pack_getc(f);
if (c == EOF) {
destroy_bitmap(b);
return NULL;
}
ch = c;
c = run_count;
}
else
c = 1;
Expand Down
93 changes: 81 additions & 12 deletions src/tga.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,41 @@ static INLINE unsigned char *raw_tga_read8(unsigned char *b, int w, PACKFILE *f)

/* rle_tga_read8:
* Helper for reading 256-color RLE data from TGA files.
* Returns pointer past last written byte, or NULL on error.
*/
static void rle_tga_read8(unsigned char *b, int w, PACKFILE *f)
static unsigned char *rle_tga_read8(unsigned char *b, int w, PACKFILE *f)
{
int value, count, c = 0;

do {
count = pack_getc(f);
if (count == EOF)
return NULL;
if (count & 0x80) {
/* run-length packet */
count = (count & 0x7F) + 1;
if (c + count > w)
count = w - c;
c += count;
value = pack_getc(f);
if (value == EOF)
return NULL;
while (count--)
*b++ = value;
}
else {
/* raw packet */
count++;
if (c + count > w)
count = w - c;
c += count;
b = raw_tga_read8(b, count, f);
if (pack_feof(f))
return NULL;
}
} while (c < w);

return b;
}


Expand Down Expand Up @@ -98,28 +111,41 @@ static unsigned int *raw_tga_read32(unsigned int *b, int w, PACKFILE *f)

/* rle_tga_read32:
* Helper for reading 32-bit RLE data from TGA files.
* Returns pointer past last written element, or NULL on error.
*/
static void rle_tga_read32(unsigned int *b, int w, PACKFILE *f)
static unsigned int *rle_tga_read32(unsigned int *b, int w, PACKFILE *f)
{
int color, count, c = 0;

do {
count = pack_getc(f);
if (count == EOF)
return NULL;
if (count & 0x80) {
/* run-length packet */
count = (count & 0x7F) + 1;
if (c + count > w)
count = w - c;
c += count;
color = single_tga_read32(f);
if (pack_feof(f))
return NULL;
while (count--)
*b++ = color;
}
else {
/* raw packet */
count++;
if (c + count > w)
count = w - c;
c += count;
b = raw_tga_read32(b, count, f);
if (pack_feof(f))
return NULL;
}
} while (c < w);

return b;
}


Expand Down Expand Up @@ -159,18 +185,25 @@ static unsigned char *raw_tga_read24(unsigned char *b, int w, PACKFILE *f)

/* rle_tga_read24:
* Helper for reading 24-bit RLE data from TGA files.
* Returns pointer past last written byte, or NULL on error.
*/
static void rle_tga_read24(unsigned char *b, int w, PACKFILE *f)
static unsigned char *rle_tga_read24(unsigned char *b, int w, PACKFILE *f)
{
int color, count, c = 0;

do {
count = pack_getc(f);
if (count == EOF)
return NULL;
if (count & 0x80) {
/* run-length packet */
count = (count & 0x7F) + 1;
if (c + count > w)
count = w - c;
c += count;
color = single_tga_read24(f);
if (pack_feof(f))
return NULL;
while (count--) {
WRITE3BYTES(b, color);
b += 3;
Expand All @@ -179,10 +212,16 @@ static void rle_tga_read24(unsigned char *b, int w, PACKFILE *f)
else {
/* raw packet */
count++;
if (c + count > w)
count = w - c;
c += count;
b = raw_tga_read24(b, count, f);
if (pack_feof(f))
return NULL;
}
} while (c < w);

return b;
}


Expand All @@ -195,6 +234,7 @@ static INLINE int single_tga_read16(PACKFILE *f)
int value;

value = pack_igetw(f);
value &= 0x7FFF; /* mask out 1-bit alpha value */

return (((value >> 10) & 0x1F) << _rgb_r_shift_15) |
(((value >> 5) & 0x1F) << _rgb_g_shift_15) |
Expand All @@ -218,28 +258,41 @@ static unsigned short *raw_tga_read16(unsigned short *b, int w, PACKFILE *f)

/* rle_tga_read16:
* Helper for reading 16-bit RLE data from TGA files.
* Returns pointer past last written element, or NULL on error.
*/
static void rle_tga_read16(unsigned short *b, int w, PACKFILE *f)
static unsigned short *rle_tga_read16(unsigned short *b, int w, PACKFILE *f)
{
int color, count, c = 0;

do {
count = pack_getc(f);
if (count == EOF)
return NULL;
if (count & 0x80) {
/* run-length packet */
count = (count & 0x7F) + 1;
if (c + count > w)
count = w - c;
c += count;
color = single_tga_read16(f);
if (pack_feof(f))
return NULL;
while (count--)
*b++ = color;
}
else {
/* raw packet */
count++;
if (c + count > w)
count = w - c;
c += count;
b = raw_tga_read16(b, count, f);
if (pack_feof(f))
return NULL;
}
} while (c < w);

return b;
}


Expand Down Expand Up @@ -419,28 +472,44 @@ BITMAP *load_tga_pf(PACKFILE *f, RGB *pal)

case 1:
case 3:
if (compressed)
rle_tga_read8(bmp->line[yc], image_width, f);
if (compressed) {
if (!rle_tga_read8(bmp->line[yc], image_width, f)) {
destroy_bitmap(bmp);
return NULL;
}
}
else
raw_tga_read8(bmp->line[yc], image_width, f);
break;

case 2:
if (bpp == 32) {
if (compressed)
rle_tga_read32((unsigned int *)bmp->line[yc], image_width, f);
if (compressed) {
if (!rle_tga_read32((unsigned int *)bmp->line[yc], image_width, f)) {
destroy_bitmap(bmp);
return NULL;
}
}
else
raw_tga_read32((unsigned int *)bmp->line[yc], image_width, f);
}
else if (bpp == 24) {
if (compressed)
rle_tga_read24(bmp->line[yc], image_width, f);
if (compressed) {
if (!rle_tga_read24(bmp->line[yc], image_width, f)) {
destroy_bitmap(bmp);
return NULL;
}
}
else
raw_tga_read24(bmp->line[yc], image_width, f);
}
else {
if (compressed)
rle_tga_read16((unsigned short *)bmp->line[yc], image_width, f);
if (compressed) {
if (!rle_tga_read16((unsigned short *)bmp->line[yc], image_width, f)) {
destroy_bitmap(bmp);
return NULL;
}
}
else
raw_tga_read16((unsigned short *)bmp->line[yc], image_width, f);
}
Expand Down