2015-02-20 16:36:00 +01:00
|
|
|
/* Copyright (C) 2010-2015 The RetroArch team
|
|
|
|
*
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
* The following license statement only applies to this file (rpng.c).
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <formats/rpng.h>
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <file/nbio.h>
|
|
|
|
|
|
|
|
#ifdef GEKKO
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2015-02-20 22:59:24 +01:00
|
|
|
#include "rpng_common.h"
|
2015-02-20 23:46:13 +01:00
|
|
|
#include "rpng_decode_common.h"
|
2015-02-20 16:36:00 +01:00
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
static bool read_chunk_header(uint8_t *buf, struct png_chunk *chunk)
|
2015-02-20 16:36:00 +01:00
|
|
|
{
|
2015-02-20 18:49:51 +01:00
|
|
|
unsigned i;
|
2015-02-20 16:36:00 +01:00
|
|
|
uint8_t dword[4] = {0};
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
dword[i] = buf[i];
|
|
|
|
|
|
|
|
buf += 4;
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
chunk->size = dword_be(dword);
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
chunk->type[i] = buf[i];
|
|
|
|
|
|
|
|
buf += 4;
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
static bool png_parse_ihdr(uint8_t *buf,
|
2015-02-20 16:36:00 +01:00
|
|
|
struct png_ihdr *ihdr)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
bool ret = true;
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
buf += 4 + 4;
|
|
|
|
|
|
|
|
ihdr->width = dword_be(buf + 0);
|
|
|
|
ihdr->height = dword_be(buf + 4);
|
|
|
|
ihdr->depth = buf[8];
|
|
|
|
ihdr->color_type = buf[9];
|
|
|
|
ihdr->compression = buf[10];
|
|
|
|
ihdr->filter = buf[11];
|
|
|
|
ihdr->interlace = buf[12];
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
if (ihdr->width == 0 || ihdr->height == 0)
|
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 if (ihdr->color_type == 3)
|
|
|
|
{
|
|
|
|
static const unsigned valid_bpp[] = { 1, 2, 4, 8 };
|
|
|
|
bool correct_bpp = false;
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
if (ihdr->compression != 0)
|
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (ihdr->interlace != 0) /* No Adam7 supported. */
|
|
|
|
GOTO_END_ERROR();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
end:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool png_realloc_idat(const struct png_chunk *chunk, struct idat_buffer *buf)
|
|
|
|
{
|
|
|
|
uint8_t *new_buffer = (uint8_t*)realloc(buf->data, buf->size + chunk->size);
|
|
|
|
|
|
|
|
if (!new_buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buf->data = new_buffer;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool png_read_plte_into_buf(uint32_t *buffer, unsigned entries)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
uint8_t buf[256 * 3];
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
bool rpng_nbio_load_image_argb_iterate(uint8_t *buf,
|
2015-02-20 18:49:51 +01:00
|
|
|
struct png_chunk *chunk,
|
2015-02-20 16:36:00 +01:00
|
|
|
uint32_t *palette,
|
|
|
|
struct png_ihdr *ihdr, struct idat_buffer *idat_buf,
|
2015-02-21 01:41:55 +01:00
|
|
|
struct rpng_t *rpng)
|
2015-02-20 16:36:00 +01:00
|
|
|
{
|
2015-02-20 18:49:51 +01:00
|
|
|
unsigned i;
|
|
|
|
|
2015-02-20 21:26:57 +01:00
|
|
|
#if 0
|
2015-02-20 18:49:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "chunktype: %c\n", chunk->type[i]);
|
|
|
|
}
|
2015-02-20 21:26:57 +01:00
|
|
|
#endif
|
2015-02-20 18:49:51 +01:00
|
|
|
|
2015-02-20 16:36:00 +01:00
|
|
|
switch (png_chunk_type(chunk))
|
|
|
|
{
|
|
|
|
case PNG_CHUNK_NOOP:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNG_CHUNK_ERROR:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
if (chunk->size != 13)
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
if (!png_parse_ihdr(buf, ihdr))
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
rpng->has_ihdr = true;
|
2015-02-20 16:36:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNG_CHUNK_PLTE:
|
|
|
|
{
|
|
|
|
unsigned entries = chunk->size / 3;
|
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
if (!rpng->has_ihdr || rpng->has_plte || rpng->has_iend || rpng->has_idat)
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (chunk->size % 3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (entries > 256)
|
|
|
|
return false;
|
|
|
|
|
2015-02-20 21:35:06 +01:00
|
|
|
buf += 8;
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
for (i = 0; i < entries; i++)
|
|
|
|
palette[i] = buf[i];
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
if (!png_read_plte_into_buf(palette, entries))
|
|
|
|
return false;
|
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
rpng->has_plte = true;
|
2015-02-20 16:36:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNG_CHUNK_IDAT:
|
2015-02-21 01:40:21 +01:00
|
|
|
if (!(rpng->has_ihdr) || rpng->has_iend || (ihdr->color_type == 3 && !(rpng->has_plte)))
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!png_realloc_idat(chunk, idat_buf))
|
|
|
|
return false;
|
|
|
|
|
2015-02-20 21:26:57 +01:00
|
|
|
buf += 8;
|
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
for (i = 0; i < chunk->size; i++)
|
|
|
|
idat_buf->data[i + idat_buf->size] = buf[i];
|
2015-02-20 16:36:00 +01:00
|
|
|
|
2015-02-20 21:26:57 +01:00
|
|
|
idat_buf->size += chunk->size;
|
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
rpng->has_idat = true;
|
2015-02-20 16:36:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNG_CHUNK_IEND:
|
2015-02-21 01:40:21 +01:00
|
|
|
if (!(rpng->has_ihdr) || !(rpng->has_idat))
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
rpng->has_iend = true;
|
2015-02-20 18:49:51 +01:00
|
|
|
return false;
|
2015-02-20 16:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
bool rpng_nbio_load_image_argb_process(struct rpng_t *rpng,
|
2015-02-20 16:36:00 +01:00
|
|
|
struct png_ihdr *ihdr,
|
|
|
|
struct idat_buffer *idat_buf, uint32_t **data,
|
|
|
|
uint32_t *palette,
|
|
|
|
unsigned *width, unsigned *height)
|
|
|
|
{
|
|
|
|
z_stream stream = {0};
|
|
|
|
size_t inflate_buf_size = 0;
|
|
|
|
|
|
|
|
if (inflateInit(&stream) != Z_OK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
png_pass_geom(ihdr, ihdr->width,
|
|
|
|
ihdr->height, NULL, NULL, &inflate_buf_size);
|
|
|
|
if (ihdr->interlace == 1) /* To be sure. */
|
|
|
|
inflate_buf_size *= 2;
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng->inflate_buf = (uint8_t*)malloc(inflate_buf_size);
|
|
|
|
if (!rpng->inflate_buf)
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
stream.next_in = idat_buf->data;
|
|
|
|
stream.avail_in = idat_buf->size;
|
|
|
|
stream.avail_out = inflate_buf_size;
|
2015-02-21 01:49:47 +01:00
|
|
|
stream.next_out = rpng->inflate_buf;
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
if (inflate(&stream, Z_FINISH) != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
inflateEnd(&stream);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inflateEnd(&stream);
|
|
|
|
|
|
|
|
*width = ihdr->width;
|
|
|
|
*height = ihdr->height;
|
|
|
|
#ifdef GEKKO
|
|
|
|
/* we often use these in textures, make sure they're 32-byte aligned */
|
|
|
|
*data = (uint32_t*)memalign(32, ihdr->width * ihdr->height * sizeof(uint32_t));
|
|
|
|
#else
|
|
|
|
*data = (uint32_t*)malloc(ihdr->width * ihdr->height * sizeof(uint32_t));
|
|
|
|
#endif
|
|
|
|
if (!*data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ihdr->interlace == 1)
|
|
|
|
{
|
|
|
|
if (!png_reverse_filter_adam7(*data,
|
2015-02-21 01:49:47 +01:00
|
|
|
ihdr, rpng->inflate_buf, stream.total_out, palette))
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (!png_reverse_filter(*data,
|
2015-02-21 01:49:47 +01:00
|
|
|
ihdr, rpng->inflate_buf, stream.total_out, palette))
|
2015-02-20 16:36:00 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
bool rpng_nbio_load_image_argb(const char *path, uint32_t **data,
|
2015-02-20 16:36:00 +01:00
|
|
|
unsigned *width, unsigned *height)
|
|
|
|
{
|
2015-02-20 18:49:51 +01:00
|
|
|
size_t file_len;
|
2015-02-20 16:36:00 +01:00
|
|
|
struct nbio_t* nbread = NULL;
|
|
|
|
struct idat_buffer idat_buf = {0};
|
|
|
|
struct png_ihdr ihdr = {0};
|
|
|
|
uint32_t palette[256] = {0};
|
2015-02-21 01:40:21 +01:00
|
|
|
struct rpng_t rpng = {0};
|
2015-02-20 16:36:00 +01:00
|
|
|
bool ret = true;
|
|
|
|
void* ptr = NULL;
|
2015-02-20 18:49:51 +01:00
|
|
|
|
2015-02-20 16:36:00 +01:00
|
|
|
{
|
|
|
|
bool looped = false;
|
|
|
|
nbread = nbio_open(path, NBIO_READ);
|
2015-02-20 18:49:51 +01:00
|
|
|
ptr = nbio_get_ptr(nbread, &file_len);
|
2015-02-20 16:36:00 +01:00
|
|
|
nbio_begin_read(nbread);
|
|
|
|
|
|
|
|
while (!nbio_iterate(nbread)) looped=true;
|
2015-02-20 18:49:51 +01:00
|
|
|
ptr = nbio_get_ptr(nbread, &file_len);
|
2015-02-20 16:36:00 +01:00
|
|
|
(void)ptr;
|
|
|
|
(void)looped;
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng.buff_data = (uint8_t*)ptr;
|
2015-02-20 16:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
char header[8];
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
2015-02-21 01:49:47 +01:00
|
|
|
header[i] = rpng.buff_data[i];
|
2015-02-20 16:36:00 +01:00
|
|
|
|
|
|
|
if (memcmp(header, png_magic, sizeof(png_magic)) != 0)
|
|
|
|
return false;
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng.buff_data += 8;
|
2015-02-20 18:49:51 +01:00
|
|
|
}
|
2015-02-20 16:36:00 +01:00
|
|
|
|
2015-02-20 18:49:51 +01:00
|
|
|
while (1)
|
2015-02-20 16:36:00 +01:00
|
|
|
{
|
|
|
|
struct png_chunk chunk = {0};
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
if (!read_chunk_header(rpng.buff_data, &chunk))
|
2015-02-20 16:36:00 +01:00
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
if (!rpng_nbio_load_image_argb_iterate(
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng.buff_data, &chunk, palette, &ihdr, &idat_buf,
|
2015-02-21 01:41:55 +01:00
|
|
|
&rpng))
|
2015-02-20 18:49:51 +01:00
|
|
|
break;
|
2015-02-20 16:36:00 +01:00
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng.buff_data += 4 + 4 + chunk.size + 4;
|
2015-02-20 16:36:00 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 21:26:57 +01:00
|
|
|
#if 0
|
2015-02-21 01:40:21 +01:00
|
|
|
fprintf(stderr, "has_ihdr: %d\n", rpng.has_ihdr);
|
|
|
|
fprintf(stderr, "has_idat: %d\n", rpng.has_idat);
|
|
|
|
fprintf(stderr, "has_iend: %d\n", rpng.has_iend);
|
2015-02-20 21:26:57 +01:00
|
|
|
#endif
|
2015-02-20 18:49:51 +01:00
|
|
|
|
2015-02-21 01:40:21 +01:00
|
|
|
if (!rpng.has_ihdr || !rpng.has_idat || !rpng.has_iend)
|
2015-02-20 16:36:00 +01:00
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
2015-02-21 01:49:47 +01:00
|
|
|
rpng_nbio_load_image_argb_process(&rpng,
|
2015-02-20 16:36:00 +01:00
|
|
|
&ihdr, &idat_buf, data, palette,
|
|
|
|
width, height);
|
|
|
|
|
|
|
|
end:
|
|
|
|
nbio_free(nbread);
|
|
|
|
if (!ret)
|
|
|
|
free(*data);
|
|
|
|
if (idat_buf.data)
|
|
|
|
free(idat_buf.data);
|
2015-02-21 01:49:47 +01:00
|
|
|
if (rpng.inflate_buf)
|
|
|
|
free(rpng.inflate_buf);
|
2015-02-20 16:36:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|