RetroArch/libretro-common/formats/png/rpng_decode_fbio.c

349 lines
9.2 KiB
C
Raw Normal View History

2015-02-11 02:30:56 +01:00
/* Copyright (C) 2010-2015 The RetroArch team
2013-01-12 14:05:05 +01:00
*
2015-02-11 02:30:56 +01:00
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (rpng.c).
* ---------------------------------------------------------------------------------------
2013-01-12 14:05:05 +01:00
*
2015-02-11 02:30:56 +01:00
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2013-01-12 14:05:05 +01:00
*/
2015-02-11 02:30:56 +01:00
#include <formats/rpng.h>
2013-01-19 01:31:24 +01:00
2013-01-12 14:05:05 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef GEKKO
#include <malloc.h>
#endif
#include "rpng_common.h"
#include "rpng_decode_common.h"
2013-01-12 14:05:05 +01:00
static bool read_chunk_header_fio(FILE *file, struct png_chunk *chunk)
2013-01-12 14:05:05 +01:00
{
uint8_t dword[4] = {0};
2015-01-26 18:58:12 +01:00
2013-01-12 14:05:05 +01:00
if (fread(dword, 1, 4, file) != 4)
return false;
chunk->size = dword_be(dword);
if (fread(chunk->type, 1, 4, file) != 4)
return false;
return true;
}
2015-02-20 22:49:49 +01:00
static bool png_read_chunk(FILE *file, struct png_chunk *chunk)
2013-01-12 14:05:05 +01:00
{
free(chunk->data);
2014-09-08 19:28:10 +02:00
chunk->data = (uint8_t*)calloc(1, chunk->size + sizeof(uint32_t)); /* CRC32 */
2013-01-12 14:05:05 +01:00
if (!chunk->data)
return false;
2015-02-20 22:49:49 +01:00
if (fread(chunk->data, 1, chunk->size +
sizeof(uint32_t), file) != (chunk->size + sizeof(uint32_t)))
{
free(chunk->data);
return false;
}
/* Ignore CRC. */
2013-01-12 14:05:05 +01:00
return true;
}
static void png_free_chunk(struct png_chunk *chunk)
{
if (!chunk)
return;
2013-01-12 14:05:05 +01:00
free(chunk->data);
chunk->data = NULL;
}
static bool png_parse_ihdr_fio(FILE *file,
2015-02-20 22:49:49 +01:00
struct png_chunk *chunk, struct png_ihdr *ihdr)
2013-01-12 14:05:05 +01:00
{
unsigned i;
2013-01-12 14:05:05 +01:00
bool ret = true;
2015-01-26 18:58:12 +01:00
2015-02-20 22:49:49 +01:00
if (!png_read_chunk(file, chunk))
return false;
2013-01-12 14:05:05 +01:00
if (chunk->size != 13)
GOTO_END_ERROR();
ihdr->width = dword_be(chunk->data + 0);
ihdr->height = dword_be(chunk->data + 4);
ihdr->depth = chunk->data[8];
ihdr->color_type = chunk->data[9];
ihdr->compression = chunk->data[10];
ihdr->filter = chunk->data[11];
ihdr->interlace = chunk->data[12];
if (ihdr->width == 0 || ihdr->height == 0)
GOTO_END_ERROR();
2014-09-08 19:28:10 +02:00
if (ihdr->color_type == 2 ||
ihdr->color_type == 4 || ihdr->color_type == 6)
{
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;
2015-01-26 18:58:12 +01:00
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
{
if (valid_bpp[i] == ihdr->depth)
{
correct_bpp = true;
break;
}
}
2013-01-12 14:05:05 +01:00
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;
2015-01-26 18:58:12 +01:00
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();
}
else
2013-01-12 14:05:05 +01:00
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->interlace == 1 ? "yes" : "no");
#endif
2013-01-12 14:05:05 +01:00
if (ihdr->compression != 0)
GOTO_END_ERROR();
2015-01-26 18:58:12 +01:00
#if 0
if (ihdr->interlace != 0) /* No Adam7 supported. */
GOTO_END_ERROR();
#endif
2013-01-12 14:05:05 +01:00
end:
png_free_chunk(chunk);
return ret;
}
static bool png_append_idat_fio(FILE *file,
2015-02-20 22:49:49 +01:00
const struct png_chunk *chunk, struct idat_buffer *buf)
2013-01-12 14:05:05 +01:00
{
uint8_t *new_buffer = (uint8_t*)realloc(buf->data, buf->size + chunk->size);
2015-01-26 18:58:12 +01:00
2013-01-12 14:05:05 +01:00
if (!new_buffer)
return false;
buf->data = new_buffer;
2015-02-20 22:49:49 +01:00
if (fread(buf->data + buf->size, 1, chunk->size, file) != chunk->size)
return false;
if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0)
return false;
buf->size += chunk->size;
2015-02-19 14:47:26 +01:00
return true;
}
static bool png_read_plte_fio(FILE *file, uint32_t *buffer, unsigned entries)
{
unsigned i;
2015-01-26 18:58:12 +01:00
uint8_t buf[256 * 3];
2015-02-20 22:49:49 +01:00
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);
}
2015-02-20 22:49:49 +01:00
if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0)
return false;
return true;
}
2015-02-20 22:49:49 +01:00
bool rpng_load_image_argb(const char *path, uint32_t **data,
unsigned *width, unsigned *height)
2015-02-19 15:15:13 +01:00
{
2015-02-20 22:49:49 +01:00
long pos, file_len;
FILE *file;
char header[8];
2015-02-26 03:49:35 +01:00
struct rpng_t rpng = {{0}};
2015-02-20 22:49:49 +01:00
bool ret = true;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
*data = NULL;
*width = 0;
*height = 0;
file = fopen(path, "rb");
if (!file)
return false;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
fseek(file, 0, SEEK_END);
file_len = ftell(file);
rewind(file);
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
if (fread(header, 1, sizeof(header), file) != sizeof(header))
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
if (memcmp(header, png_magic, sizeof(png_magic)) != 0)
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
/* 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};
2015-02-19 15:15:13 +01:00
if (!read_chunk_header_fio(file, &chunk))
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
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;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
case PNG_CHUNK_ERROR:
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
case PNG_CHUNK_IHDR:
2015-02-21 01:40:21 +01:00
if (rpng.has_ihdr || rpng.has_idat || rpng.has_iend)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-21 01:59:59 +01:00
if (!png_parse_ihdr_fio(file, &chunk, &rpng.ihdr))
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-21 01:40:21 +01:00
rpng.has_ihdr = true;
2015-02-20 22:49:49 +01:00
break;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
case PNG_CHUNK_PLTE:
2015-02-21 01:40:21 +01:00
if (!rpng.has_ihdr || rpng.has_plte || rpng.has_iend || rpng.has_idat)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
if (chunk.size % 3)
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
if (!png_read_plte_fio(file, rpng.palette, chunk.size / 3))
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-21 01:40:21 +01:00
rpng.has_plte = true;
2015-02-20 22:49:49 +01:00
break;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
case PNG_CHUNK_IDAT:
2015-02-21 01:59:59 +01:00
if (!rpng.has_ihdr || rpng.has_iend || (rpng.ihdr.color_type == 3 && !rpng.has_plte))
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-20 21:26:57 +01:00
2015-02-21 01:56:56 +01:00
if (!png_append_idat_fio(file, &chunk, &rpng.idat_buf))
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-21 01:40:21 +01:00
rpng.has_idat = true;
2015-02-20 22:49:49 +01:00
break;
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
case PNG_CHUNK_IEND:
2015-02-21 01:40:21 +01:00
if (!rpng.has_ihdr || !rpng.has_idat)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-20 22:49:49 +01:00
if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0)
GOTO_END_ERROR();
2015-02-19 15:15:13 +01:00
2015-02-21 01:40:21 +01:00
rpng.has_iend = true;
2015-02-20 22:49:49 +01:00
break;
}
2015-02-19 15:15:13 +01:00
}
2015-02-21 01:40:21 +01:00
if (!rpng.has_ihdr || !rpng.has_idat || !rpng.has_iend)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:25:12 +01:00
2015-02-26 03:49:35 +01:00
if (inflateInit(&rpng.process.stream) != Z_OK)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:25:12 +01:00
2015-02-26 03:49:35 +01:00
png_pass_geom(&rpng.ihdr, rpng.ihdr.width, rpng.ihdr.height, NULL, NULL, &rpng.process.inflate_buf_size);
2015-02-21 01:59:59 +01:00
if (rpng.ihdr.interlace == 1) /* To be sure. */
2015-02-26 03:49:35 +01:00
rpng.process.inflate_buf_size *= 2;
2015-02-19 15:25:12 +01:00
2015-02-26 03:49:35 +01:00
rpng.process.inflate_buf = (uint8_t*)malloc(rpng.process.inflate_buf_size);
if (!rpng.process.inflate_buf)
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:25:12 +01:00
2015-02-26 03:49:35 +01:00
rpng.process.stream.next_in = rpng.idat_buf.data;
rpng.process.stream.avail_in = rpng.idat_buf.size;
rpng.process.stream.avail_out = rpng.process.inflate_buf_size;
rpng.process.stream.next_out = rpng.process.inflate_buf;
2015-02-19 15:25:12 +01:00
2015-02-26 03:49:35 +01:00
if (inflate(&rpng.process.stream, Z_FINISH) != Z_STREAM_END)
2015-02-19 15:25:12 +01:00
{
2015-02-26 03:49:35 +01:00
inflateEnd(&rpng.process.stream);
2015-02-20 22:49:49 +01:00
GOTO_END_ERROR();
2015-02-19 15:25:12 +01:00
}
2015-02-26 03:49:35 +01:00
inflateEnd(&rpng.process.stream);
2015-02-19 15:25:12 +01:00
2015-02-21 01:59:59 +01:00
*width = rpng.ihdr.width;
*height = rpng.ihdr.height;
2015-02-19 15:25:12 +01:00
#ifdef GEKKO
/* we often use these in textures, make sure they're 32-byte aligned */
2015-02-21 01:59:59 +01:00
*data = (uint32_t*)memalign(32, rpng.ihdr.width * rpng.ihdr.height * sizeof(uint32_t));
2015-02-19 15:25:12 +01:00
#else
2015-02-21 01:59:59 +01:00
*data = (uint32_t*)malloc(rpng.ihdr.width * rpng.ihdr.height * sizeof(uint32_t));
2015-02-19 15:25:12 +01:00
#endif
if (!*data)
2015-02-19 15:36:10 +01:00
GOTO_END_ERROR();
2015-02-26 21:36:23 +01:00
if (!png_reverse_filter_loop(&rpng, data))
2013-01-12 14:05:05 +01:00
GOTO_END_ERROR();
end:
if (file)
fclose(file);
if (!ret)
free(*data);
2015-02-21 01:56:56 +01:00
free(rpng.idat_buf.data);
2015-02-26 03:49:35 +01:00
free(rpng.process.inflate_buf);
2013-01-12 14:05:05 +01:00
return ret;
}