From c80e59b8880b886cfc563029f5a1d8160719d3ff Mon Sep 17 00:00:00 2001 From: twinaphex Date: Tue, 1 Sep 2015 13:22:20 +0200 Subject: [PATCH] Create common png_read_plte --- libretro-common/formats/png/rpng_common.h | 16 +++++++ libretro-common/formats/png/rpng_fbio.c | 54 +++++++++-------------- libretro-common/formats/png/rpng_nbio.c | 27 +++--------- 3 files changed, 43 insertions(+), 54 deletions(-) diff --git a/libretro-common/formats/png/rpng_common.h b/libretro-common/formats/png/rpng_common.h index e6e8667d91..2e678e0382 100644 --- a/libretro-common/formats/png/rpng_common.h +++ b/libretro-common/formats/png/rpng_common.h @@ -165,5 +165,21 @@ static INLINE uint32_t dword_be(const uint8_t *buf) return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3] << 0); } +static INLINE bool png_read_plte(uint8_t *buf, + uint32_t *buffer, unsigned entries) +{ + unsigned i; + + for (i = 0; i < entries; i++) + { + uint32_t r = buf[3 * i + 0]; + uint32_t g = buf[3 * i + 1]; + uint32_t b = buf[3 * i + 2]; + buffer[i] = (r << 16) | (g << 8) | (b << 0) | (0xffu << 24); + } + + return true; +} + #endif diff --git a/libretro-common/formats/png/rpng_fbio.c b/libretro-common/formats/png/rpng_fbio.c index ccaecb85f6..66f05e46ef 100644 --- a/libretro-common/formats/png/rpng_fbio.c +++ b/libretro-common/formats/png/rpng_fbio.c @@ -114,32 +114,6 @@ static bool png_append_idat_fio(FILE **fd, return true; } -static bool png_read_plte_fio(FILE **fd, uint32_t *buffer, unsigned entries) -{ - unsigned i; - uint8_t buf[256 * 3]; - FILE *file = *fd; - - if (entries > 256) - return false; - - if (fread(buf, 3, entries, file) != entries) - return false; - - for (i = 0; i < entries; i++) - { - uint32_t r = buf[3 * i + 0]; - uint32_t g = buf[3 * i + 1]; - uint32_t b = buf[3 * i + 2]; - buffer[i] = (r << 16) | (g << 8) | (b << 0) | (0xffu << 24); - } - - if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0) - return false; - - return true; -} - bool rpng_load_image_argb_iterate(FILE **fd, struct rpng_t *rpng) { struct png_chunk chunk = {0}; @@ -180,16 +154,30 @@ bool rpng_load_image_argb_iterate(FILE **fd, struct rpng_t *rpng) break; case PNG_CHUNK_PLTE: - if (!rpng->has_ihdr || rpng->has_plte || rpng->has_iend || rpng->has_idat) - return false; + { + uint8_t buf[256 * 3]; + unsigned entries = chunk.size / 3; - if (chunk.size % 3) - return false; + if (!rpng->has_ihdr || rpng->has_plte || rpng->has_iend || rpng->has_idat) + return false; - if (!png_read_plte_fio(fd, rpng->palette, chunk.size / 3)) - return false; + if (chunk.size % 3) + return false; - rpng->has_plte = true; + if (entries > 256) + return false; + + if (fread(rpng->palette, 3, entries, *fd) != entries) + return false; + + if (!png_read_plte(&buf[0], rpng->palette, entries)) + return false; + + if (fseek(*fd, sizeof(uint32_t), SEEK_CUR) < 0) + return false; + + rpng->has_plte = true; + } break; case PNG_CHUNK_IDAT: diff --git a/libretro-common/formats/png/rpng_nbio.c b/libretro-common/formats/png/rpng_nbio.c index 42a222d307..f62e3aa9a2 100644 --- a/libretro-common/formats/png/rpng_nbio.c +++ b/libretro-common/formats/png/rpng_nbio.c @@ -77,26 +77,6 @@ static bool png_realloc_idat(const struct png_chunk *chunk, struct idat_buffer * return true; } -static bool png_read_plte_into_buf(uint8_t *buf, - uint32_t *buffer, unsigned entries) -{ - unsigned i; - - if (entries > 256) - return false; - - buf += 8; - - for (i = 0; i < entries; i++) - { - uint32_t r = buf[3 * i + 0]; - uint32_t g = buf[3 * i + 1]; - uint32_t b = buf[3 * i + 2]; - buffer[i] = (r << 16) | (g << 8) | (b << 0) | (0xffu << 24); - } - - return true; -} bool rpng_nbio_load_image_argb_iterate(struct rpng_t *rpng) { @@ -151,7 +131,12 @@ bool rpng_nbio_load_image_argb_iterate(struct rpng_t *rpng) if (chunk.size % 3) goto error; - if (!png_read_plte_into_buf(buf, rpng->palette, entries)) + if (entries > 256) + goto error; + + buf += 8; + + if (!png_read_plte(buf, rpng->palette, entries)) goto error; rpng->has_plte = true;