(RPNG) More cleanups

This commit is contained in:
twinaphex 2015-03-14 00:24:21 +01:00
parent 3a56a6c274
commit 1902c92ad5
4 changed files with 106 additions and 96 deletions

View File

@ -50,6 +50,24 @@ enum png_chunk_type
PNG_CHUNK_IEND
};
enum png_line_filter
{
PNG_FILTER_NONE = 0,
PNG_FILTER_SUB,
PNG_FILTER_UP,
PNG_FILTER_AVERAGE,
PNG_FILTER_PAETH,
};
enum png_ihdr_color_type
{
PNG_IHDR_COLOR_GRAY = 0,
PNG_IHDR_COLOR_RGB = 2,
PNG_IHDR_COLOR_PLT = 3,
PNG_IHDR_COLOR_GRAY_ALPHA = 4,
PNG_IHDR_COLOR_RGBA = 6,
};
struct adam7_pass
{
unsigned x;

View File

@ -23,24 +23,6 @@
#ifndef _RPNG_DECODE_COMMON_H
#define _RPNG_DECODE_COMMON_H
enum png_line_filter
{
PNG_FILTER_NONE = 0,
PNG_FILTER_SUB,
PNG_FILTER_UP,
PNG_FILTER_AVERAGE,
PNG_FILTER_PAETH,
};
enum png_ihdr_color_type
{
PNG_IHDR_COLOR_GRAY = 0,
PNG_IHDR_COLOR_RGB = 2,
PNG_IHDR_COLOR_PLT = 3,
PNG_IHDR_COLOR_GRAY_ALPHA = 4,
PNG_IHDR_COLOR_RGBA = 6,
};
static enum png_chunk_type png_chunk_type(const struct png_chunk *chunk)
{
unsigned i;

View File

@ -98,55 +98,60 @@ static bool png_parse_ihdr_fio(FILE *file,
if (ihdr->width == 0 || ihdr->height == 0)
GOTO_END_ERROR();
if (ihdr->color_type == 2 ||
ihdr->color_type == 4 || ihdr->color_type == 6)
switch (ihdr->color_type)
{
if (ihdr->depth != 8 && ihdr->depth != 16)
GOTO_END_ERROR();
}
else if (ihdr->color_type == 0)
{
static const unsigned valid_bpp[] = { 1, 2, 4, 8, 16 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
case PNG_IHDR_COLOR_RGB:
case PNG_IHDR_COLOR_GRAY_ALPHA:
case PNG_IHDR_COLOR_RGBA:
if (ihdr->depth != 8 && ihdr->depth != 16)
GOTO_END_ERROR();
break;
case PNG_IHDR_COLOR_GRAY:
{
correct_bpp = true;
break;
static const unsigned valid_bpp[] = { 1, 2, 4, 8, 16 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
{
correct_bpp = true;
break;
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
else if (ihdr->color_type == 3)
{
static const unsigned valid_bpp[] = { 1, 2, 4, 8 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
break;
case PNG_IHDR_COLOR_PLT:
{
correct_bpp = true;
break;
}
}
static const unsigned valid_bpp[] = { 1, 2, 4, 8 };
bool correct_bpp = false;
if (!correct_bpp)
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
{
correct_bpp = true;
break;
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
break;
default:
GOTO_END_ERROR();
}
else
GOTO_END_ERROR();
#ifdef RPNG_TEST
fprintf(stderr, "IHDR: (%u x %u), bpc = %u, palette = %s, color = %s, alpha = %s, adam7 = %s.\n",
ihdr->width, ihdr->height,
ihdr->depth, ihdr->color_type == 3 ? "yes" : "no",
ihdr->color_type & 2 ? "yes" : "no",
ihdr->color_type & 4 ? "yes" : "no",
ihdr->depth, ihdr->color_type == PNG_IHDR_COLOR_PLT ? "yes" : "no",
ihdr->color_type & PNG_IHDR_COLOR_RGB ? "yes" : "no",
ihdr->color_type & PNG_IHDR_COLOR_GRAY_ALPHA ? "yes" : "no",
ihdr->interlace == 1 ? "yes" : "no");
#endif
@ -276,7 +281,7 @@ bool rpng_load_image_argb(const char *path, uint32_t **data,
break;
case PNG_CHUNK_IDAT:
if (!rpng.has_ihdr || rpng.has_iend || (rpng.ihdr.color_type == 3 && !rpng.has_plte))
if (!rpng.has_ihdr || rpng.has_iend || (rpng.ihdr.color_type == PNG_IHDR_COLOR_PLT && !rpng.has_plte))
GOTO_END_ERROR();
if (!png_append_idat_fio(file, &chunk, &rpng.idat_buf))

View File

@ -72,55 +72,60 @@ static bool png_parse_ihdr(uint8_t *buf,
if (ihdr->width == 0 || ihdr->height == 0)
GOTO_END_ERROR();
if (ihdr->color_type == 2 ||
ihdr->color_type == 4 || ihdr->color_type == 6)
switch (ihdr->color_type)
{
if (ihdr->depth != 8 && ihdr->depth != 16)
GOTO_END_ERROR();
}
else if (ihdr->color_type == 0)
{
static const unsigned valid_bpp[] = { 1, 2, 4, 8, 16 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
case PNG_IHDR_COLOR_RGB:
case PNG_IHDR_COLOR_GRAY_ALPHA:
case PNG_IHDR_COLOR_RGBA:
if (ihdr->depth != 8 && ihdr->depth != 16)
GOTO_END_ERROR();
break;
case PNG_IHDR_COLOR_GRAY:
{
correct_bpp = true;
break;
static const unsigned valid_bpp[] = { 1, 2, 4, 8, 16 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
{
correct_bpp = true;
break;
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
else if (ihdr->color_type == 3)
{
static const unsigned valid_bpp[] = { 1, 2, 4, 8 };
bool correct_bpp = false;
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
break;
case PNG_IHDR_COLOR_PLT:
{
correct_bpp = true;
break;
}
}
static const unsigned valid_bpp[] = { 1, 2, 4, 8 };
bool correct_bpp = false;
if (!correct_bpp)
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
{
correct_bpp = true;
break;
}
}
if (!correct_bpp)
GOTO_END_ERROR();
}
break;
default:
GOTO_END_ERROR();
}
else
GOTO_END_ERROR();
#ifdef RPNG_TEST
fprintf(stderr, "IHDR: (%u x %u), bpc = %u, palette = %s, color = %s, alpha = %s, adam7 = %s.\n",
ihdr->width, ihdr->height,
ihdr->depth, ihdr->color_type == 3 ? "yes" : "no",
ihdr->color_type & 2 ? "yes" : "no",
ihdr->color_type & 4 ? "yes" : "no",
ihdr->depth, ihdr->color_type == PNG_IHDR_COLOR_PLT ? "yes" : "no",
ihdr->color_type & PNG_IHDR_COLOR_RGB ? "yes" : "no",
ihdr->color_type & PNG_IHDR_COLOR_GRAY_ALPHA ? "yes" : "no",
ihdr->interlace == 1 ? "yes" : "no");
#endif
@ -225,7 +230,7 @@ bool rpng_nbio_load_image_argb_iterate(uint8_t *buf, struct rpng_t *rpng)
break;
case PNG_CHUNK_IDAT:
if (!(rpng->has_ihdr) || rpng->has_iend || (rpng->ihdr.color_type == 3 && !(rpng->has_plte)))
if (!(rpng->has_ihdr) || rpng->has_iend || (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT && !(rpng->has_plte)))
return false;
if (!png_realloc_idat(&rpng->chunk, &rpng->idat_buf))