Create common png_read_plte

This commit is contained in:
twinaphex 2015-09-01 13:22:20 +02:00
parent c259b983de
commit c80e59b888
3 changed files with 43 additions and 54 deletions

View File

@ -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

View File

@ -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:

View File

@ -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;