Simplify rpng_load_image_iterate

This commit is contained in:
twinaphex 2015-02-19 17:28:17 +01:00
parent f382aceb28
commit af91764fc8

View File

@ -622,21 +622,17 @@ static bool png_read_plte_into_buf(uint32_t *buffer, unsigned entries)
return true; return true;
} }
bool rpng_load_image_argb_iterate(FILE *file, uint32_t *palette, bool rpng_load_image_argb_iterate(FILE *file, struct png_chunk *chunk,
uint32_t *palette,
struct png_ihdr *ihdr, struct idat_buffer *idat_buf, struct png_ihdr *ihdr, struct idat_buffer *idat_buf,
bool *has_ihdr, bool *has_idat, bool *has_ihdr, bool *has_idat,
bool *has_iend, bool *has_plte, size_t *increment_size) bool *has_iend, bool *has_plte, size_t *increment_size)
{ {
struct png_chunk chunk = {0}; switch (png_chunk_type(chunk))
if (!read_chunk_header(file, &chunk))
return false;
switch (png_chunk_type(&chunk))
{ {
case PNG_CHUNK_NOOP: case PNG_CHUNK_NOOP:
default: default:
*increment_size = chunk.size + sizeof(uint32_t); *increment_size = chunk->size + sizeof(uint32_t);
break; break;
case PNG_CHUNK_ERROR: case PNG_CHUNK_ERROR:
@ -646,17 +642,17 @@ bool rpng_load_image_argb_iterate(FILE *file, uint32_t *palette,
if (*has_ihdr || *has_idat || *has_iend) if (*has_ihdr || *has_idat || *has_iend)
return false; return false;
if (!png_alloc_chunk(&chunk)) if (!png_alloc_chunk(chunk))
return false; return false;
if (fread(chunk.data, 1, chunk.size + if (fread(chunk->data, 1, chunk->size +
sizeof(uint32_t), file) != (chunk.size + sizeof(uint32_t))) sizeof(uint32_t), file) != (chunk->size + sizeof(uint32_t)))
{ {
free(chunk.data); free(chunk->data);
return false; return false;
} }
if (!png_parse_ihdr(&chunk, ihdr)) if (!png_parse_ihdr(chunk, ihdr))
return false; return false;
*has_ihdr = true; *has_ihdr = true;
@ -664,12 +660,12 @@ bool rpng_load_image_argb_iterate(FILE *file, uint32_t *palette,
case PNG_CHUNK_PLTE: case PNG_CHUNK_PLTE:
{ {
unsigned entries = chunk.size / 3; unsigned entries = chunk->size / 3;
if (!*has_ihdr || *has_plte || *has_iend || *has_idat) if (!*has_ihdr || *has_plte || *has_iend || *has_idat)
return false; return false;
if (chunk.size % 3) if (chunk->size % 3)
return false; return false;
if (entries > 256) if (entries > 256)
@ -678,7 +674,7 @@ bool rpng_load_image_argb_iterate(FILE *file, uint32_t *palette,
if (fread(&palette, 3, entries, file) != entries) if (fread(&palette, 3, entries, file) != entries)
return false; return false;
if (!png_read_plte_into_buf(palette, chunk.size / 3)) if (!png_read_plte_into_buf(palette, chunk->size / 3))
return false; return false;
*increment_size = sizeof(uint32_t); *increment_size = sizeof(uint32_t);
@ -690,14 +686,14 @@ bool rpng_load_image_argb_iterate(FILE *file, uint32_t *palette,
if (!(*has_ihdr) || *has_iend || (ihdr->color_type == 3 && !(*has_plte))) if (!(*has_ihdr) || *has_iend || (ihdr->color_type == 3 && !(*has_plte)))
return false; return false;
if (!png_realloc_idat(&chunk, idat_buf)) if (!png_realloc_idat(chunk, idat_buf))
return false; return false;
if (fread(idat_buf->data + idat_buf->size, 1, chunk.size, file) != chunk.size) if (fread(idat_buf->data + idat_buf->size, 1, chunk->size, file) != chunk->size)
return false; return false;
*increment_size = sizeof(uint32_t); *increment_size = sizeof(uint32_t);
idat_buf->size += chunk.size; idat_buf->size += chunk->size;
*has_idat = true; *has_idat = true;
break; break;
@ -826,13 +822,15 @@ bool rpng_load_image_argb(const char *path, uint32_t **data,
for (pos = 0; pos < file_len && pos >= 0; pos = ftell(file)) for (pos = 0; pos < file_len && pos >= 0; pos = ftell(file))
{ {
size_t increment = 0; size_t increment = 0;
struct png_chunk chunk = {0};
ret = rpng_load_image_argb_iterate( if (!read_chunk_header(file, &chunk))
file, palette, &ihdr, &idat_buf, GOTO_END_ERROR();
if (!rpng_load_image_argb_iterate(
file, &chunk, palette, &ihdr, &idat_buf,
&has_ihdr, &has_idat, &has_iend, &has_plte, &has_ihdr, &has_idat, &has_iend, &has_plte,
&increment); &increment))
if (!ret)
GOTO_END_ERROR(); GOTO_END_ERROR();
if (increment != 0) if (increment != 0)