static int png_reverse_filter_iterate(rpng_t *rpng, uint32_t **data) { if (!rpng) return false; if (rpng->ihdr.interlace) return png_reverse_filter_adam7(data, &rpng->ihdr, rpng->process); return png_reverse_filter_regular_iterate(data, &rpng->ihdr, rpng->process); }
bool rpng_load_image_argb(const char *path, uint32_t **data, unsigned *width, unsigned *height) { long pos; *data = NULL; *width = 0; *height = 0; bool ret = true; FILE *file = fopen(path, "rb"); if (!file) return false; fseek(file, 0, SEEK_END); long file_len = ftell(file); rewind(file); bool has_ihdr = false; bool has_idat = false; bool has_iend = false; bool has_plte = false; uint8_t *inflate_buf = NULL; size_t inflate_buf_size = 0; z_stream stream = {0}; struct idat_buffer idat_buf = {0}; struct png_ihdr ihdr = {0}; uint32_t palette[256] = {0}; char header[8]; if (fread(header, 1, sizeof(header), file) != sizeof(header)) GOTO_END_ERROR(); if (memcmp(header, png_magic, sizeof(png_magic)) != 0) GOTO_END_ERROR(); // feof() apparently isn't triggered after a seek (IEND). for (pos = ftell(file); pos < file_len && pos >= 0; pos = ftell(file)) { struct png_chunk chunk = {0}; if (!read_chunk_header(file, &chunk)) GOTO_END_ERROR(); switch (png_chunk_type(&chunk)) { case PNG_CHUNK_NOOP: default: if (fseek(file, chunk.size + sizeof(uint32_t), SEEK_CUR) < 0) GOTO_END_ERROR(); break; case PNG_CHUNK_ERROR: GOTO_END_ERROR(); case PNG_CHUNK_IHDR: if (has_ihdr || has_idat || has_iend) GOTO_END_ERROR(); if (!png_parse_ihdr(file, &chunk, &ihdr)) GOTO_END_ERROR(); has_ihdr = true; break; case PNG_CHUNK_PLTE: if (!has_ihdr || has_plte || has_iend || has_idat) GOTO_END_ERROR(); if (chunk.size % 3) GOTO_END_ERROR(); if (!png_read_plte(file, palette, chunk.size / 3)) GOTO_END_ERROR(); has_plte = true; break; case PNG_CHUNK_IDAT: if (!has_ihdr || has_iend || (ihdr.color_type == 3 && !has_plte)) GOTO_END_ERROR(); if (!png_append_idat(file, &chunk, &idat_buf)) GOTO_END_ERROR(); has_idat = true; break; case PNG_CHUNK_IEND: if (!has_ihdr || !has_idat) GOTO_END_ERROR(); if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0) GOTO_END_ERROR(); has_iend = true; break; } } if (!has_ihdr || !has_idat || !has_iend) GOTO_END_ERROR(); if (inflateInit(&stream) != Z_OK) GOTO_END_ERROR(); png_pass_geom(&ihdr, ihdr.width, ihdr.height, NULL, NULL, &inflate_buf_size); if (ihdr.interlace == 1) // To be sure. inflate_buf_size *= 2; inflate_buf = (uint8_t*)malloc(inflate_buf_size); if (!inflate_buf) GOTO_END_ERROR(); stream.next_in = idat_buf.data; stream.avail_in = idat_buf.size; stream.avail_out = inflate_buf_size; stream.next_out = inflate_buf; if (inflate(&stream, Z_FINISH) != Z_STREAM_END) { inflateEnd(&stream); GOTO_END_ERROR(); } inflateEnd(&stream); *width = ihdr.width; *height = ihdr.height; *data = (uint32_t*)malloc(ihdr.width * ihdr.height * sizeof(uint32_t)); if (!*data) GOTO_END_ERROR(); if (ihdr.interlace == 1) { if (!png_reverse_filter_adam7(*data, &ihdr, inflate_buf, stream.total_out, palette)) GOTO_END_ERROR(); } else if (!png_reverse_filter(*data, &ihdr, inflate_buf, stream.total_out, palette)) GOTO_END_ERROR(); end: if (file) fclose(file); if (!ret) free(*data); free(idat_buf.data); free(inflate_buf); return ret; }