292 lines
7.4 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
*/
#include <stdio.h>
2015-03-31 02:47:51 +11:00
#include <stdint.h>
2013-01-12 14:05:05 +01:00
#include <stdlib.h>
#include <string.h>
2015-03-31 02:47:51 +11:00
#include <boolean.h>
#include "rpng_common.h"
2015-03-14 00:52:55 +01:00
#include "rpng_decode.h"
2013-01-12 14:05:05 +01:00
static bool read_chunk_header_fio(FILE **fd, struct png_chunk *chunk)
2013-01-12 14:05:05 +01:00
{
uint8_t dword[4] = {0};
FILE *file = *fd;
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;
}
static bool png_read_chunk(FILE **fd, struct png_chunk *chunk)
2013-01-12 14:05:05 +01:00
{
FILE *file = *fd;
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 **fd,
2015-02-20 22:49:49 +01:00
struct png_chunk *chunk, struct png_ihdr *ihdr)
2013-01-12 14:05:05 +01:00
{
if (!png_read_chunk(fd, chunk))
2015-02-20 22:49:49 +01:00
return false;
2013-01-12 14:05:05 +01:00
if (chunk->size != 13)
return false;
2013-01-12 14:05:05 +01:00
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)
return false;
2013-01-12 14:05:05 +01:00
return true;
2013-01-12 14:05:05 +01:00
}
static bool png_append_idat_fio(FILE **fd,
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
{
FILE *file = *fd;
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 **fd, uint32_t *buffer, unsigned entries)
{
unsigned i;
2015-01-26 18:58:12 +01:00
uint8_t buf[256 * 3];
FILE *file = *fd;
2015-01-26 18:58:12 +01:00
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;
}
bool rpng_load_image_argb_iterate(FILE **fd, struct rpng_t *rpng)
{
struct png_chunk chunk = {0};
FILE *file = *fd;
if (!read_chunk_header_fio(fd, &chunk))
return false;
switch (png_chunk_type(&chunk))
{
case PNG_CHUNK_NOOP:
default:
if (fseek(file, chunk.size + sizeof(uint32_t), SEEK_CUR) < 0)
return false;
break;
case PNG_CHUNK_ERROR:
return false;
case PNG_CHUNK_IHDR:
if (rpng->has_ihdr || rpng->has_idat || rpng->has_iend)
return false;
if (!png_parse_ihdr_fio(fd, &chunk, &rpng->ihdr))
{
png_free_chunk(&chunk);
return false;
}
if (!png_process_ihdr(&rpng->ihdr))
{
png_free_chunk(&chunk);
return false;
}
png_free_chunk(&chunk);
rpng->has_ihdr = true;
break;
case PNG_CHUNK_PLTE:
if (!rpng->has_ihdr || rpng->has_plte || rpng->has_iend || rpng->has_idat)
return false;
if (chunk.size % 3)
return false;
if (!png_read_plte_fio(fd, rpng->palette, chunk.size / 3))
return false;
rpng->has_plte = true;
break;
case PNG_CHUNK_IDAT:
if (!rpng->has_ihdr || rpng->has_iend || (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT && !rpng->has_plte))
return false;
if (!png_append_idat_fio(fd, &chunk, &rpng->idat_buf))
return false;
rpng->has_idat = true;
break;
case PNG_CHUNK_IEND:
if (!rpng->has_ihdr || !rpng->has_idat)
return false;
if (fseek(file, sizeof(uint32_t), SEEK_CUR) < 0)
return false;
rpng->has_iend = true;
break;
}
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;
2015-06-13 00:33:31 +02:00
char header[8] = {0};
2015-02-26 03:49:35 +01:00
struct rpng_t rpng = {{0}};
2015-06-13 00:33:31 +02:00
bool ret = true;
int retval = 0;
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))
{
if (!rpng_load_image_argb_iterate(&file, &rpng))
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
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
if (!rpng_load_image_argb_process_init(&rpng, data, width,
height))
2015-02-19 15:36:10 +01:00
GOTO_END_ERROR();
do{
retval = rpng_load_image_argb_process_inflate_init(&rpng, data,
width, height);
}while(retval == 0);
if (retval == -1)
GOTO_END_ERROR();
do{
retval = png_reverse_filter_iterate(&rpng, data);
}while(retval == PNG_PROCESS_NEXT);
if (retval == PNG_PROCESS_ERROR || retval == PNG_PROCESS_ERROR_END)
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);
2015-04-25 21:04:52 -03:00
if (rpng.process.stream)
2015-04-25 21:28:22 -03:00
{
2015-04-25 21:04:52 -03:00
zlib_stream_free(rpng.process.stream);
2015-04-25 21:28:22 -03:00
free(rpng.process.stream);
}
2013-01-12 14:05:05 +01:00
return ret;
}