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 <zlib.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>
|
2014-01-11 12:21:43 +01:00
|
|
|
|
2014-02-20 17:58:06 -05:00
|
|
|
#ifdef GEKKO
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
#include "rpng_common.h"
|
|
|
|
|
2013-01-24 07:32:15 +01:00
|
|
|
#undef GOTO_END_ERROR
|
2013-01-12 14:05:05 +01:00
|
|
|
#define GOTO_END_ERROR() do { \
|
|
|
|
fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__); \
|
|
|
|
ret = false; \
|
|
|
|
goto end; \
|
|
|
|
} while(0)
|
|
|
|
|
2015-02-20 23:17:04 +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)
|
|
|
|
{
|
2014-09-15 08:17:16 -04:00
|
|
|
if (!chunk)
|
|
|
|
return;
|
|
|
|
|
2013-01-12 14:05:05 +01:00
|
|
|
free(chunk->data);
|
|
|
|
chunk->data = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
static bool png_parse_ihdr(FILE *file,
|
|
|
|
struct png_chunk *chunk, struct png_ihdr *ihdr)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
2013-10-19 19:39:38 +02: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)
|
2013-06-04 12:57:08 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
|
2013-06-04 12:57:08 +02:00
|
|
|
{
|
|
|
|
if (valid_bpp[i] == ihdr->depth)
|
|
|
|
{
|
|
|
|
correct_bpp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-12 14:05:05 +01:00
|
|
|
|
2013-06-04 12:57:08 +02: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
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(valid_bpp); i++)
|
2013-06-04 12:57:08 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2013-06-04 12:57:08 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:09:09 +02:00
|
|
|
static void png_pass_geom(const struct png_ihdr *ihdr,
|
|
|
|
unsigned width, unsigned height,
|
|
|
|
unsigned *bpp_out, unsigned *pitch_out, size_t *pass_size)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
2013-06-04 12:57:08 +02:00
|
|
|
unsigned bpp;
|
|
|
|
unsigned pitch;
|
2015-01-26 18:58:12 +01:00
|
|
|
|
2013-06-04 12:57:08 +02:00
|
|
|
switch (ihdr->color_type)
|
|
|
|
{
|
|
|
|
case 0:
|
2015-01-26 19:02:01 +01:00
|
|
|
bpp = (ihdr->depth + 7) / 8;
|
2013-06-04 12:57:08 +02:00
|
|
|
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2015-01-26 19:02:01 +01:00
|
|
|
bpp = (ihdr->depth * 3 + 7) / 8;
|
2013-06-04 12:57:08 +02:00
|
|
|
pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2015-01-26 19:02:01 +01:00
|
|
|
bpp = (ihdr->depth + 7) / 8;
|
2013-06-04 12:57:08 +02:00
|
|
|
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2015-01-26 19:02:01 +01:00
|
|
|
bpp = (ihdr->depth * 2 + 7) / 8;
|
2013-06-04 12:57:08 +02:00
|
|
|
pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
2015-01-26 19:02:01 +01:00
|
|
|
bpp = (ihdr->depth * 4 + 7) / 8;
|
2013-06-04 12:57:08 +02:00
|
|
|
pitch = (ihdr->width * ihdr->depth * 4 + 7) / 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bpp = 0;
|
|
|
|
pitch = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:09:09 +02:00
|
|
|
if (pass_size)
|
|
|
|
*pass_size = (pitch + 1) * ihdr->height;
|
|
|
|
if (bpp_out)
|
|
|
|
*bpp_out = bpp;
|
|
|
|
if (pitch_out)
|
|
|
|
*pitch_out = pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
|
2014-09-08 19:28:10 +02:00
|
|
|
const uint8_t *inflate_buf, size_t inflate_buf_size,
|
|
|
|
const uint32_t *palette)
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
2013-10-19 19:39:38 +02:00
|
|
|
unsigned i, h;
|
2013-06-04 14:09:09 +02:00
|
|
|
unsigned bpp;
|
|
|
|
unsigned pitch;
|
|
|
|
size_t pass_size;
|
2015-01-26 18:58:12 +01:00
|
|
|
uint8_t *prev_scanline = NULL;
|
|
|
|
uint8_t *decoded_scanline = NULL;
|
|
|
|
bool ret = true;
|
|
|
|
|
2013-06-04 14:09:09 +02:00
|
|
|
png_pass_geom(ihdr, ihdr->width, ihdr->height, &bpp, &pitch, &pass_size);
|
|
|
|
|
|
|
|
if (inflate_buf_size < pass_size)
|
2013-01-12 14:05:05 +01:00
|
|
|
return false;
|
|
|
|
|
2015-01-26 18:58:12 +01:00
|
|
|
prev_scanline = (uint8_t*)calloc(1, pitch);
|
|
|
|
decoded_scanline = (uint8_t*)calloc(1, pitch);
|
2013-01-12 14:05:05 +01:00
|
|
|
|
2013-10-13 10:19:02 +02:00
|
|
|
if (!prev_scanline || !decoded_scanline)
|
2013-01-12 14:05:05 +01:00
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (h = 0; h < ihdr->height;
|
2013-01-12 14:05:05 +01:00
|
|
|
h++, inflate_buf += pitch, data += ihdr->width)
|
|
|
|
{
|
|
|
|
unsigned filter = *inflate_buf++;
|
2015-01-26 18:58:12 +01:00
|
|
|
|
2013-01-12 14:05:05 +01:00
|
|
|
switch (filter)
|
|
|
|
{
|
2014-09-08 19:28:10 +02:00
|
|
|
case 0: /* None */
|
2013-01-12 14:05:05 +01:00
|
|
|
memcpy(decoded_scanline, inflate_buf, pitch);
|
|
|
|
break;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
case 1: /* Sub */
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < bpp; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
decoded_scanline[i] = inflate_buf[i];
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = bpp; i < pitch; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
decoded_scanline[i] = decoded_scanline[i - bpp] + inflate_buf[i];
|
|
|
|
break;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
case 2: /* Up */
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < pitch; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
decoded_scanline[i] = prev_scanline[i] + inflate_buf[i];
|
|
|
|
break;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
case 3: /* Average */
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < bpp; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
|
|
|
uint8_t avg = prev_scanline[i] >> 1;
|
|
|
|
decoded_scanline[i] = avg + inflate_buf[i];
|
|
|
|
}
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = bpp; i < pitch; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
|
|
|
uint8_t avg = (decoded_scanline[i - bpp] + prev_scanline[i]) >> 1;
|
|
|
|
decoded_scanline[i] = avg + inflate_buf[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
case 4: /* Paeth */
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < bpp; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
decoded_scanline[i] = paeth(0, prev_scanline[i], 0) + inflate_buf[i];
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = bpp; i < pitch; i++)
|
2014-09-08 19:28:10 +02:00
|
|
|
decoded_scanline[i] = paeth(decoded_scanline[i - bpp],
|
|
|
|
prev_scanline[i], prev_scanline[i - bpp]) + inflate_buf[i];
|
2013-01-12 14:05:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
GOTO_END_ERROR();
|
|
|
|
}
|
|
|
|
|
2013-06-04 12:57:08 +02:00
|
|
|
if (ihdr->color_type == 0)
|
|
|
|
copy_line_bw(data, decoded_scanline, ihdr->width, ihdr->depth);
|
|
|
|
else if (ihdr->color_type == 2)
|
|
|
|
copy_line_rgb(data, decoded_scanline, ihdr->width, ihdr->depth);
|
|
|
|
else if (ihdr->color_type == 3)
|
2014-09-08 19:28:10 +02:00
|
|
|
copy_line_plt(data, decoded_scanline, ihdr->width,
|
|
|
|
ihdr->depth, palette);
|
2013-06-04 12:57:08 +02:00
|
|
|
else if (ihdr->color_type == 4)
|
2014-09-08 19:28:10 +02:00
|
|
|
copy_line_gray_alpha(data, decoded_scanline, ihdr->width,
|
|
|
|
ihdr->depth);
|
2013-06-04 12:57:08 +02:00
|
|
|
else if (ihdr->color_type == 6)
|
|
|
|
copy_line_rgba(data, decoded_scanline, ihdr->width, ihdr->depth);
|
2013-01-12 14:05:05 +01:00
|
|
|
|
|
|
|
memcpy(prev_scanline, decoded_scanline, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
free(decoded_scanline);
|
|
|
|
free(prev_scanline);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:09:09 +02:00
|
|
|
struct adam7_pass
|
|
|
|
{
|
|
|
|
unsigned x;
|
|
|
|
unsigned y;
|
|
|
|
unsigned stride_x;
|
|
|
|
unsigned stride_y;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void deinterlace_pass(uint32_t *data, const struct png_ihdr *ihdr,
|
2014-09-08 19:28:10 +02:00
|
|
|
const uint32_t *input, unsigned pass_width, unsigned pass_height,
|
|
|
|
const struct adam7_pass *pass)
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
2013-10-19 19:39:38 +02:00
|
|
|
unsigned x, y;
|
2015-01-26 18:58:12 +01:00
|
|
|
|
2013-06-04 14:09:09 +02:00
|
|
|
data += pass->y * ihdr->width + pass->x;
|
2015-01-26 18:58:12 +01:00
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
for (y = 0; y < pass_height;
|
|
|
|
y++, data += ihdr->width * pass->stride_y, input += pass_width)
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
|
|
|
uint32_t *out = data;
|
2015-01-26 18:58:12 +01:00
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (x = 0; x < pass_width; x++, out += pass->stride_x)
|
2013-06-04 14:09:09 +02:00
|
|
|
*out = input[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
static bool png_reverse_filter_adam7(uint32_t *data,
|
|
|
|
const struct png_ihdr *ihdr,
|
|
|
|
const uint8_t *inflate_buf, size_t inflate_buf_size,
|
|
|
|
const uint32_t *palette)
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
2013-10-19 19:39:38 +02:00
|
|
|
unsigned pass;
|
2013-06-04 14:09:09 +02:00
|
|
|
static const struct adam7_pass passes[] = {
|
|
|
|
{ 0, 0, 8, 8 },
|
|
|
|
{ 4, 0, 8, 8 },
|
|
|
|
{ 0, 4, 4, 8 },
|
|
|
|
{ 2, 0, 4, 4 },
|
|
|
|
{ 0, 2, 2, 4 },
|
|
|
|
{ 1, 0, 2, 2 },
|
|
|
|
{ 0, 1, 1, 2 },
|
|
|
|
};
|
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (pass = 0; pass < ARRAY_SIZE(passes); pass++)
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
2015-01-26 18:58:12 +01:00
|
|
|
unsigned pass_width, pass_height;
|
|
|
|
size_t pass_size;
|
|
|
|
struct png_ihdr tmp_ihdr;
|
|
|
|
uint32_t *tmp_data = NULL;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
if (ihdr->width <= passes[pass].x ||
|
|
|
|
ihdr->height <= passes[pass].y) /* Empty pass */
|
2013-06-04 14:09:09 +02:00
|
|
|
continue;
|
|
|
|
|
2015-01-26 18:58:12 +01:00
|
|
|
pass_width = (ihdr->width -
|
2014-09-08 19:28:10 +02:00
|
|
|
passes[pass].x + passes[pass].stride_x - 1) / passes[pass].stride_x;
|
2015-01-26 18:58:12 +01:00
|
|
|
pass_height = (ihdr->height - passes[pass].y +
|
2014-09-08 19:28:10 +02:00
|
|
|
passes[pass].stride_y - 1) / passes[pass].stride_y;
|
|
|
|
|
2015-01-26 18:58:12 +01:00
|
|
|
tmp_data = (uint32_t*)malloc(
|
|
|
|
pass_width * pass_height * sizeof(uint32_t));
|
2013-06-04 14:09:09 +02:00
|
|
|
|
|
|
|
if (!tmp_data)
|
|
|
|
return false;
|
|
|
|
|
2015-01-26 18:58:12 +01:00
|
|
|
tmp_ihdr = *ihdr;
|
2013-06-04 14:09:09 +02:00
|
|
|
tmp_ihdr.width = pass_width;
|
|
|
|
tmp_ihdr.height = pass_height;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
png_pass_geom(&tmp_ihdr, pass_width,
|
|
|
|
pass_height, NULL, NULL, &pass_size);
|
2013-06-04 14:09:09 +02:00
|
|
|
|
|
|
|
if (pass_size > inflate_buf_size)
|
|
|
|
{
|
|
|
|
free(tmp_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
if (!png_reverse_filter(tmp_data,
|
|
|
|
&tmp_ihdr, inflate_buf, pass_size, palette))
|
2013-06-04 14:09:09 +02:00
|
|
|
{
|
|
|
|
free(tmp_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inflate_buf += pass_size;
|
|
|
|
inflate_buf_size -= pass_size;
|
|
|
|
|
2014-09-08 19:28:10 +02:00
|
|
|
deinterlace_pass(data,
|
|
|
|
ihdr, tmp_data, pass_width, pass_height, &passes[pass]);
|
2013-06-04 14:09:09 +02:00
|
|
|
free(tmp_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
static bool png_read_plte_fio(FILE *file, uint32_t *buffer, unsigned entries)
|
2013-06-04 12:57:08 +02:00
|
|
|
{
|
2013-10-19 19:39:38 +02:00
|
|
|
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;
|
|
|
|
|
2013-10-19 19:39:38 +02:00
|
|
|
for (i = 0; i < entries; i++)
|
2013-06-04 12:57:08 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-06-04 12:57:08 +02:00
|
|
|
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];
|
|
|
|
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};
|
|
|
|
bool has_ihdr = false;
|
|
|
|
bool has_idat = false;
|
|
|
|
bool has_iend = false;
|
|
|
|
bool has_plte = false;
|
|
|
|
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
|
|
|
|
2015-02-20 23:17:04 +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:
|
|
|
|
if (has_ihdr || has_idat || has_iend)
|
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:15:13 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
if (!png_parse_ihdr(file, &chunk, &ihdr))
|
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:15:13 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
has_ihdr = true;
|
|
|
|
break;
|
2015-02-19 15:15:13 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
case PNG_CHUNK_PLTE:
|
|
|
|
if (!has_ihdr || has_plte || has_iend || has_idat)
|
|
|
|
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
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
if (!png_read_plte_fio(file, 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-20 22:49:49 +01:00
|
|
|
has_plte = true;
|
|
|
|
break;
|
2015-02-19 15:15:13 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
case PNG_CHUNK_IDAT:
|
|
|
|
if (!has_ihdr || has_iend || (ihdr.color_type == 3 && !has_plte))
|
|
|
|
GOTO_END_ERROR();
|
2015-02-20 21:26:57 +01:00
|
|
|
|
2015-02-20 23:17:04 +01:00
|
|
|
if (!png_append_idat_fio(file, &chunk, &idat_buf))
|
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
|
|
|
has_idat = true;
|
|
|
|
break;
|
2015-02-19 15:15:13 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
case PNG_CHUNK_IEND:
|
|
|
|
if (!has_ihdr || !has_idat)
|
|
|
|
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-20 22:49:49 +01:00
|
|
|
has_iend = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-19 15:15:13 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
if (!has_ihdr || !has_idat || !has_iend)
|
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:25:12 +01:00
|
|
|
|
|
|
|
if (inflateInit(&stream) != Z_OK)
|
2015-02-20 22:49:49 +01:00
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:25:12 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
png_pass_geom(&ihdr, ihdr.width, ihdr.height, NULL, NULL, &inflate_buf_size);
|
|
|
|
if (ihdr.interlace == 1) /* To be sure. */
|
2015-02-19 15:25:12 +01:00
|
|
|
inflate_buf_size *= 2;
|
|
|
|
|
|
|
|
inflate_buf = (uint8_t*)malloc(inflate_buf_size);
|
|
|
|
if (!inflate_buf)
|
2015-02-20 22:49:49 +01:00
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:25:12 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
stream.next_in = idat_buf.data;
|
|
|
|
stream.avail_in = idat_buf.size;
|
2015-02-19 15:25:12 +01:00
|
|
|
stream.avail_out = inflate_buf_size;
|
|
|
|
stream.next_out = inflate_buf;
|
|
|
|
|
|
|
|
if (inflate(&stream, Z_FINISH) != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
inflateEnd(&stream);
|
2015-02-20 22:49:49 +01:00
|
|
|
GOTO_END_ERROR();
|
2015-02-19 15:25:12 +01:00
|
|
|
}
|
|
|
|
inflateEnd(&stream);
|
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
*width = ihdr.width;
|
|
|
|
*height = 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-20 22:49:49 +01:00
|
|
|
*data = (uint32_t*)memalign(32, ihdr.width * ihdr.height * sizeof(uint32_t));
|
2015-02-19 15:25:12 +01:00
|
|
|
#else
|
2015-02-20 22:49:49 +01:00
|
|
|
*data = (uint32_t*)malloc(ihdr.width * 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-20 03:12:09 +01:00
|
|
|
|
2015-02-20 22:49:49 +01:00
|
|
|
if (ihdr.interlace == 1)
|
2015-02-19 15:15:13 +01:00
|
|
|
{
|
2015-02-20 22:49:49 +01:00
|
|
|
if (!png_reverse_filter_adam7(*data,
|
|
|
|
&ihdr, inflate_buf, stream.total_out, palette))
|
2015-02-19 17:11:37 +01:00
|
|
|
GOTO_END_ERROR();
|
2013-01-12 14:05:05 +01:00
|
|
|
}
|
2015-02-20 22:49:49 +01:00
|
|
|
else if (!png_reverse_filter(*data,
|
|
|
|
&ihdr, inflate_buf, stream.total_out, palette))
|
2013-01-12 14:05:05 +01:00
|
|
|
GOTO_END_ERROR();
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
if (!ret)
|
|
|
|
free(*data);
|
2015-02-20 22:49:49 +01:00
|
|
|
free(idat_buf.data);
|
|
|
|
free(inflate_buf);
|
2013-01-12 14:05:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|