diff --git a/libretro-common/formats/png/rpng_common.h b/libretro-common/formats/png/rpng_common.h index 902c49057c..22fb4e563f 100644 --- a/libretro-common/formats/png/rpng_common.h +++ b/libretro-common/formats/png/rpng_common.h @@ -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; diff --git a/libretro-common/formats/png/rpng_decode_common.h b/libretro-common/formats/png/rpng_decode_common.h index 1de5e20de3..7d5afa5a92 100644 --- a/libretro-common/formats/png/rpng_decode_common.h +++ b/libretro-common/formats/png/rpng_decode_common.h @@ -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; diff --git a/libretro-common/formats/png/rpng_decode_fbio.c b/libretro-common/formats/png/rpng_decode_fbio.c index ed1daa17ac..992b9b8d7a 100644 --- a/libretro-common/formats/png/rpng_decode_fbio.c +++ b/libretro-common/formats/png/rpng_decode_fbio.c @@ -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)) diff --git a/libretro-common/formats/png/rpng_decode_fnbio.c b/libretro-common/formats/png/rpng_decode_fnbio.c index 7b0e546518..0dad7bd1f1 100644 --- a/libretro-common/formats/png/rpng_decode_fnbio.c +++ b/libretro-common/formats/png/rpng_decode_fnbio.c @@ -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))