1193 lines
29 KiB
C
Raw Normal View History

2016-03-20 16:36:39 +01:00
/* Copyright (C) 2010-2016 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.
*/
2015-03-14 00:52:55 +01:00
#include <stdio.h>
2015-09-19 01:40:29 +02:00
#include <stdint.h>
2015-03-14 00:52:55 +01:00
#include <stdlib.h>
#include <string.h>
2015-04-03 03:23:58 +02:00
#ifdef GEKKO
#include <malloc.h>
#endif
2015-09-19 01:40:29 +02:00
#include <boolean.h>
2016-05-18 17:18:36 +02:00
#include <formats/image.h>
2015-09-19 01:40:29 +02:00
#include <formats/rpng.h>
#include <file/archive_file.h>
2015-09-05 20:34:22 +02:00
#include "rpng_internal.h"
2015-09-19 01:59:46 +02:00
enum png_ihdr_color_type
{
PNG_IHDR_COLOR_GRAY = 0,
PNG_IHDR_COLOR_RGB = 2,
PNG_IHDR_COLOR_PLT = 3,
PNG_IHDR_COLOR_GRAY_ALPHA = 4,
PNG_IHDR_COLOR_RGBA = 6
};
enum png_line_filter
{
PNG_FILTER_NONE = 0,
PNG_FILTER_SUB,
PNG_FILTER_UP,
PNG_FILTER_AVERAGE,
PNG_FILTER_PAETH
};
enum png_chunk_type
{
PNG_CHUNK_NOOP = 0,
PNG_CHUNK_ERROR,
PNG_CHUNK_IHDR,
PNG_CHUNK_IDAT,
PNG_CHUNK_PLTE,
PNG_CHUNK_tRNS,
2015-09-19 01:59:46 +02:00
PNG_CHUNK_IEND
};
struct adam7_pass
{
unsigned x;
unsigned y;
unsigned stride_x;
unsigned stride_y;
};
struct idat_buffer
{
uint8_t *data;
size_t size;
};
struct png_chunk
{
uint32_t size;
char type[4];
uint8_t *data;
};
2016-05-19 01:00:03 +02:00
struct rpng_process
2015-09-19 01:59:46 +02:00
{
bool inflate_initialized;
bool adam7_pass_initialized;
bool pass_initialized;
uint32_t *data;
uint32_t *palette;
struct png_ihdr ihdr;
uint8_t *prev_scanline;
uint8_t *decoded_scanline;
uint8_t *inflate_buf;
size_t restore_buf_size;
size_t adam7_restore_buf_size;
size_t data_restore_buf_size;
size_t inflate_buf_size;
unsigned bpp;
unsigned pitch;
unsigned h;
struct
{
unsigned width;
unsigned height;
size_t size;
unsigned pos;
} pass;
void *stream;
2016-01-24 22:17:11 +01:00
const struct file_archive_file_backend *stream_backend;
2015-09-19 01:59:46 +02:00
};
struct rpng
{
2016-05-19 01:00:03 +02:00
struct rpng_process *process;
2015-09-19 01:59:46 +02:00
bool has_ihdr;
bool has_idat;
bool has_iend;
bool has_plte;
bool has_trns;
2015-09-19 01:59:46 +02:00
struct idat_buffer idat_buf;
struct png_ihdr ihdr;
uint8_t *buff_data;
uint32_t palette[256];
};
2015-09-19 01:49:29 +02:00
static INLINE uint32_t dword_be(const uint8_t *buf)
{
return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3] << 0);
}
2015-09-19 01:42:58 +02:00
static enum png_chunk_type png_chunk_type(const struct png_chunk *chunk)
{
unsigned i;
struct
{
const char *id;
enum png_chunk_type type;
} static const chunk_map[] = {
{ "IHDR", PNG_CHUNK_IHDR },
{ "IDAT", PNG_CHUNK_IDAT },
{ "IEND", PNG_CHUNK_IEND },
{ "PLTE", PNG_CHUNK_PLTE },
{ "tRNS", PNG_CHUNK_tRNS },
};
for (i = 0; i < ARRAY_SIZE(chunk_map); i++)
{
if (memcmp(chunk->type, chunk_map[i].id, 4) == 0)
return chunk_map[i].type;
}
return PNG_CHUNK_NOOP;
}
2015-09-19 01:42:58 +02:00
static bool png_process_ihdr(struct png_ihdr *ihdr)
{
unsigned i;
bool ret = true;
switch (ihdr->color_type)
{
case PNG_IHDR_COLOR_RGB:
case PNG_IHDR_COLOR_GRAY_ALPHA:
case PNG_IHDR_COLOR_RGBA:
if (ihdr->depth != 8 && ihdr->depth != 16)
GOTO_END_ERROR();
break;
case PNG_IHDR_COLOR_GRAY:
{
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();
}
break;
case PNG_IHDR_COLOR_PLT:
{
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();
}
break;
default:
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,
2015-09-28 18:44:28 +02:00
ihdr->depth, (ihdr->color_type == PNG_IHDR_COLOR_PLT) ? "yes" : "no",
(ihdr->color_type & PNG_IHDR_COLOR_RGB) ? "yes" : "no",
(ihdr->color_type & PNG_IHDR_COLOR_GRAY_ALPHA) ? "yes" : "no",
ihdr->interlace == 1 ? "yes" : "no");
#endif
if (ihdr->compression != 0)
GOTO_END_ERROR();
end:
return ret;
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_copy_line_rgb(uint32_t *data,
const uint8_t *decoded, unsigned width, unsigned bpp)
{
unsigned i;
bpp /= 8;
for (i = 0; i < width; i++)
{
uint32_t r, g, b;
r = *decoded;
decoded += bpp;
g = *decoded;
decoded += bpp;
b = *decoded;
decoded += bpp;
data[i] = (0xffu << 24) | (r << 16) | (g << 8) | (b << 0);
}
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_copy_line_rgba(uint32_t *data,
const uint8_t *decoded, unsigned width, unsigned bpp)
{
unsigned i;
bpp /= 8;
for (i = 0; i < width; i++)
{
uint32_t r, g, b, a;
r = *decoded;
decoded += bpp;
g = *decoded;
decoded += bpp;
b = *decoded;
decoded += bpp;
a = *decoded;
decoded += bpp;
data[i] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
}
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_copy_line_bw(uint32_t *data,
const uint8_t *decoded, unsigned width, unsigned depth)
{
unsigned i, bit;
static const unsigned mul_table[] = { 0, 0xff, 0x55, 0, 0x11, 0, 0, 0, 0x01 };
unsigned mul, mask;
if (depth == 16)
{
for (i = 0; i < width; i++)
{
uint32_t val = decoded[i << 1];
data[i] = (val * 0x010101) | (0xffu << 24);
}
return;
}
mul = mul_table[depth];
mask = (1 << depth) - 1;
bit = 0;
for (i = 0; i < width; i++, bit += depth)
{
unsigned byte = bit >> 3;
unsigned val = decoded[byte] >> (8 - depth - (bit & 7));
val &= mask;
val *= mul;
data[i] = (val * 0x010101) | (0xffu << 24);
}
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_copy_line_gray_alpha(uint32_t *data,
const uint8_t *decoded, unsigned width,
unsigned bpp)
{
unsigned i;
bpp /= 8;
for (i = 0; i < width; i++)
{
uint32_t gray, alpha;
gray = *decoded;
decoded += bpp;
alpha = *decoded;
decoded += bpp;
data[i] = (gray * 0x010101) | (alpha << 24);
}
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_copy_line_plt(uint32_t *data,
const uint8_t *decoded, unsigned width,
unsigned depth, const uint32_t *palette)
{
2016-05-29 10:57:57 -03:00
switch (depth)
{
2016-05-29 10:57:57 -03:00
case 1:
{
unsigned w = width / 8;
unsigned i;
for (i = 0; i < w; i++, decoded++)
{
*data++ = palette[(*decoded >> 7) & 1];
*data++ = palette[(*decoded >> 6) & 1];
*data++ = palette[(*decoded >> 5) & 1];
*data++ = palette[(*decoded >> 4) & 1];
*data++ = palette[(*decoded >> 3) & 1];
*data++ = palette[(*decoded >> 2) & 1];
*data++ = palette[(*decoded >> 1) & 1];
*data++ = palette[*decoded & 1];
}
switch (width & 7)
{
case 7:
data[6] = palette[(*decoded >> 1) & 1];
case 6:
data[5] = palette[(*decoded >> 2) & 1];
case 5:
data[4] = palette[(*decoded >> 3) & 1];
case 4:
data[3] = palette[(*decoded >> 4) & 1];
case 3:
data[2] = palette[(*decoded >> 5) & 1];
case 2:
data[1] = palette[(*decoded >> 6) & 1];
case 1:
data[0] = palette[(*decoded >> 7) & 1];
break;
}
}
break;
case 2:
{
unsigned w = width / 4;
unsigned i;
for (i = 0; i < w; i++, decoded++)
{
*data++ = palette[(*decoded >> 6) & 3];
*data++ = palette[(*decoded >> 4) & 3];
*data++ = palette[(*decoded >> 2) & 3];
*data++ = palette[*decoded & 3];
}
switch (width & 3)
{
case 3:
data[2] = palette[(*decoded >> 2) & 3];
case 2:
data[1] = palette[(*decoded >> 4) & 3];
case 1:
data[0] = palette[(*decoded >> 6) & 3];
break;
}
}
break;
case 4:
{
unsigned w = width / 2;
unsigned i;
for (i = 0; i < w; i++, decoded++)
{
*data++ = palette[*decoded >> 4];
*data++ = palette[*decoded & 0x0f];
}
if (width & 1)
{
*data = palette[*decoded >> 4];
}
}
break;
case 8:
{
unsigned i;
for (i = 0; i < width; i++, decoded++, data++)
{
*data = palette[*decoded];
}
}
break;
}
}
2015-03-14 02:10:44 +01: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)
{
unsigned bpp;
unsigned pitch;
switch (ihdr->color_type)
{
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_GRAY:
bpp = (ihdr->depth + 7) / 8;
pitch = (ihdr->width * ihdr->depth + 7) / 8;
break;
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_RGB:
bpp = (ihdr->depth * 3 + 7) / 8;
pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
break;
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_PLT:
bpp = (ihdr->depth + 7) / 8;
pitch = (ihdr->width * ihdr->depth + 7) / 8;
break;
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_GRAY_ALPHA:
bpp = (ihdr->depth * 2 + 7) / 8;
pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
break;
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_RGBA:
bpp = (ihdr->depth * 4 + 7) / 8;
pitch = (ihdr->width * ihdr->depth * 4 + 7) / 8;
break;
default:
bpp = 0;
pitch = 0;
break;
}
if (pass_size)
*pass_size = (pitch + 1) * ihdr->height;
if (bpp_out)
*bpp_out = bpp;
if (pitch_out)
*pitch_out = pitch;
}
2015-03-13 19:21:04 +01:00
static void png_reverse_filter_adam7_deinterlace_pass(uint32_t *data,
const struct png_ihdr *ihdr,
const uint32_t *input, unsigned pass_width, unsigned pass_height,
const struct adam7_pass *pass)
{
unsigned x, y;
data += pass->y * ihdr->width + pass->x;
for (y = 0; y < pass_height;
y++, data += ihdr->width * pass->stride_y, input += pass_width)
{
uint32_t *out = data;
for (x = 0; x < pass_width; x++, out += pass->stride_x)
*out = input[x];
}
}
2016-05-19 01:00:03 +02:00
static void png_reverse_filter_deinit(struct rpng_process *pngp)
{
if (pngp->decoded_scanline)
free(pngp->decoded_scanline);
pngp->decoded_scanline = NULL;
if (pngp->prev_scanline)
free(pngp->prev_scanline);
pngp->prev_scanline = NULL;
pngp->pass_initialized = false;
pngp->h = 0;
}
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 },
};
2015-03-14 02:10:44 +01:00
static int png_reverse_filter_init(const struct png_ihdr *ihdr,
2016-05-19 01:00:03 +02:00
struct rpng_process *pngp)
{
size_t pass_size;
if (!pngp->adam7_pass_initialized && ihdr->interlace)
{
if (ihdr->width <= passes[pngp->pass.pos].x ||
ihdr->height <= passes[pngp->pass.pos].y) /* Empty pass */
return 1;
pngp->pass.width = (ihdr->width -
passes[pngp->pass.pos].x + passes[pngp->pass.pos].stride_x - 1) / passes[pngp->pass.pos].stride_x;
pngp->pass.height = (ihdr->height - passes[pngp->pass.pos].y +
passes[pngp->pass.pos].stride_y - 1) / passes[pngp->pass.pos].stride_y;
pngp->data = (uint32_t*)malloc(
pngp->pass.width * pngp->pass.height * sizeof(uint32_t));
if (!pngp->data)
return -1;
pngp->ihdr = *ihdr;
pngp->ihdr.width = pngp->pass.width;
pngp->ihdr.height = pngp->pass.height;
png_pass_geom(&pngp->ihdr, pngp->pass.width,
pngp->pass.height, NULL, NULL, &pngp->pass.size);
if (pngp->pass.size > pngp->stream_backend->stream_get_total_out(pngp->stream))
{
free(pngp->data);
return -1;
}
pngp->adam7_pass_initialized = true;
return 0;
}
2015-02-26 20:25:51 +01:00
if (pngp->pass_initialized)
return 0;
2015-02-26 07:14:43 +01:00
png_pass_geom(ihdr, ihdr->width, ihdr->height, &pngp->bpp, &pngp->pitch, &pass_size);
if (pngp->stream_backend->stream_get_total_out(pngp->stream) < pass_size)
return -1;
2015-03-13 23:54:17 +01:00
pngp->restore_buf_size = 0;
pngp->data_restore_buf_size = 0;
2015-02-26 07:14:43 +01:00
pngp->prev_scanline = (uint8_t*)calloc(1, pngp->pitch);
pngp->decoded_scanline = (uint8_t*)calloc(1, pngp->pitch);
if (!pngp->prev_scanline || !pngp->decoded_scanline)
goto error;
2015-02-26 07:26:02 +01:00
pngp->h = 0;
pngp->pass_initialized = true;
return 0;
error:
png_reverse_filter_deinit(pngp);
return -1;
}
static int png_reverse_filter_copy_line(uint32_t *data, const struct png_ihdr *ihdr,
2016-05-19 01:00:03 +02:00
struct rpng_process *pngp, unsigned filter)
{
unsigned i;
2015-02-26 19:45:25 +01:00
switch (filter)
{
2015-03-14 00:13:55 +01:00
case PNG_FILTER_NONE:
memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
break;
2015-03-14 00:13:55 +01:00
case PNG_FILTER_SUB:
2015-02-26 19:45:25 +01:00
for (i = 0; i < pngp->bpp; i++)
pngp->decoded_scanline[i] = pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
for (i = pngp->bpp; i < pngp->pitch; i++)
pngp->decoded_scanline[i] = pngp->decoded_scanline[i - pngp->bpp] + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
break;
2015-03-14 00:13:55 +01:00
case PNG_FILTER_UP:
2015-02-26 19:45:25 +01:00
for (i = 0; i < pngp->pitch; i++)
pngp->decoded_scanline[i] = pngp->prev_scanline[i] + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
break;
2015-03-14 00:13:55 +01:00
case PNG_FILTER_AVERAGE:
2015-02-26 19:45:25 +01:00
for (i = 0; i < pngp->bpp; i++)
{
uint8_t avg = pngp->prev_scanline[i] >> 1;
pngp->decoded_scanline[i] = avg + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
}
for (i = pngp->bpp; i < pngp->pitch; i++)
{
uint8_t avg = (pngp->decoded_scanline[i - pngp->bpp] + pngp->prev_scanline[i]) >> 1;
pngp->decoded_scanline[i] = avg + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
}
break;
2015-03-14 00:13:55 +01:00
case PNG_FILTER_PAETH:
2015-02-26 19:45:25 +01:00
for (i = 0; i < pngp->bpp; i++)
pngp->decoded_scanline[i] = paeth(0, pngp->prev_scanline[i], 0) + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
for (i = pngp->bpp; i < pngp->pitch; i++)
pngp->decoded_scanline[i] = paeth(pngp->decoded_scanline[i - pngp->bpp],
pngp->prev_scanline[i], pngp->prev_scanline[i - pngp->bpp]) + pngp->inflate_buf[i];
2015-02-26 19:45:25 +01:00
break;
default:
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_ERROR_END;
2015-02-26 19:45:25 +01:00
}
switch (ihdr->color_type)
{
2015-03-14 00:16:59 +01:00
case PNG_IHDR_COLOR_GRAY:
2015-03-13 19:21:04 +01:00
png_reverse_filter_copy_line_bw(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
2015-02-26 19:45:25 +01:00
break;
2015-03-14 00:13:55 +01:00
case PNG_IHDR_COLOR_RGB:
2015-03-13 19:21:04 +01:00
png_reverse_filter_copy_line_rgb(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
2015-02-26 19:45:25 +01:00
break;
2015-03-14 00:13:55 +01:00
case PNG_IHDR_COLOR_PLT:
2015-03-13 19:21:04 +01:00
png_reverse_filter_copy_line_plt(data, pngp->decoded_scanline, ihdr->width,
ihdr->depth, pngp->palette);
2015-02-26 19:45:25 +01:00
break;
2015-03-14 00:13:55 +01:00
case PNG_IHDR_COLOR_GRAY_ALPHA:
2015-03-13 19:21:04 +01:00
png_reverse_filter_copy_line_gray_alpha(data, pngp->decoded_scanline, ihdr->width,
2015-02-26 19:45:25 +01:00
ihdr->depth);
break;
2015-03-14 00:13:55 +01:00
case PNG_IHDR_COLOR_RGBA:
2015-03-13 19:21:04 +01:00
png_reverse_filter_copy_line_rgba(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
2015-02-26 19:45:25 +01:00
break;
}
memcpy(pngp->prev_scanline, pngp->decoded_scanline, pngp->pitch);
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_NEXT;
}
static int png_reverse_filter_regular_iterate(uint32_t **data, const struct png_ihdr *ihdr,
2016-05-19 01:00:03 +02:00
struct rpng_process *pngp)
{
2016-05-18 17:18:36 +02:00
int ret = IMAGE_PROCESS_END;
if (pngp->h < ihdr->height)
{
unsigned filter = *pngp->inflate_buf++;
pngp->restore_buf_size += 1;
ret = png_reverse_filter_copy_line(*data,
ihdr, pngp, filter);
}
2016-05-18 17:18:36 +02:00
if (ret == IMAGE_PROCESS_END || ret == IMAGE_PROCESS_ERROR_END)
goto end;
pngp->h++;
pngp->inflate_buf += pngp->pitch;
pngp->restore_buf_size += pngp->pitch;
*data += ihdr->width;
pngp->data_restore_buf_size += ihdr->width;
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_NEXT;
end:
png_reverse_filter_deinit(pngp);
2015-03-13 19:50:00 +01:00
pngp->inflate_buf -= pngp->restore_buf_size;
2015-03-14 00:52:55 +01:00
*data -= pngp->data_restore_buf_size;
2015-03-13 23:54:17 +01:00
pngp->data_restore_buf_size = 0;
return ret;
}
static int png_reverse_filter_adam7_iterate(uint32_t **data_,
const struct png_ihdr *ihdr,
2016-05-19 01:00:03 +02:00
struct rpng_process *pngp)
{
int ret = 0;
bool to_next = pngp->pass.pos < ARRAY_SIZE(passes);
uint32_t *data = *data_;
if (!to_next)
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_END;
ret = png_reverse_filter_init(ihdr, pngp);
2015-03-13 18:54:19 +01:00
if (ret == 1)
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_NEXT;
2015-03-13 18:54:19 +01:00
if (ret == -1)
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_ERROR_END;
if (png_reverse_filter_init(&pngp->ihdr, pngp) == -1)
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_ERROR;
do{
ret = png_reverse_filter_regular_iterate(&pngp->data,
&pngp->ihdr, pngp);
2016-05-18 17:18:36 +02:00
}while(ret == IMAGE_PROCESS_NEXT);
2016-05-18 17:18:36 +02:00
if (ret == IMAGE_PROCESS_ERROR || ret == IMAGE_PROCESS_ERROR_END)
return IMAGE_PROCESS_ERROR;
2015-03-13 19:39:36 +01:00
pngp->inflate_buf += pngp->pass.size;
pngp->adam7_restore_buf_size += pngp->pass.size;
2015-03-29 15:52:17 +02:00
pngp->stream_backend->stream_decrement_total_out(pngp->stream, pngp->pass.size);
2015-03-13 19:21:04 +01:00
png_reverse_filter_adam7_deinterlace_pass(data,
2015-03-13 18:54:19 +01:00
ihdr, pngp->data, pngp->pass.width, pngp->pass.height, &passes[pngp->pass.pos]);
2015-03-13 18:54:19 +01:00
free(pngp->data);
pngp->pass.width = 0;
pngp->pass.height = 0;
pngp->pass.size = 0;
pngp->adam7_pass_initialized = false;
2016-05-18 17:18:36 +02:00
return IMAGE_PROCESS_NEXT;
}
static int png_reverse_filter_adam7(uint32_t **data_,
const struct png_ihdr *ihdr,
2016-05-19 01:00:03 +02:00
struct rpng_process *pngp)
{
int ret = png_reverse_filter_adam7_iterate(data_,
ihdr, pngp);
switch (ret)
{
2016-05-18 17:18:36 +02:00
case IMAGE_PROCESS_ERROR_END:
case IMAGE_PROCESS_END:
break;
2016-05-18 17:18:36 +02:00
case IMAGE_PROCESS_NEXT:
pngp->pass.pos++;
return 0;
2016-05-18 17:18:36 +02:00
case IMAGE_PROCESS_ERROR:
if (pngp->data)
free(pngp->data);
pngp->inflate_buf -= pngp->adam7_restore_buf_size;
pngp->adam7_restore_buf_size = 0;
return -1;
}
pngp->inflate_buf -= pngp->adam7_restore_buf_size;
pngp->adam7_restore_buf_size = 0;
return ret;
}
2015-09-19 01:42:58 +02:00
static int png_reverse_filter_iterate(rpng_t *rpng, uint32_t **data)
2015-02-26 21:36:23 +01:00
{
2015-03-14 01:56:32 +01:00
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);
2015-02-26 21:36:23 +01:00
}
2015-09-19 03:25:56 +02:00
static int rpng_load_image_argb_process_inflate_init(rpng_t *rpng,
uint32_t **data, unsigned *width, unsigned *height)
{
int zstatus;
2016-05-19 01:00:03 +02:00
struct rpng_process *process = (struct rpng_process*)rpng->process;
bool to_continue = (process->stream_backend->stream_get_avail_in(process->stream) > 0
&& process->stream_backend->stream_get_avail_out(process->stream) > 0);
if (!to_continue)
goto end;
zstatus = process->stream_backend->stream_decompress_data_to_file_iterate(process->stream);
switch (zstatus)
{
case 1:
goto end;
case -1:
goto error;
default:
break;
}
return 0;
end:
process->stream_backend->stream_free(process->stream);
*width = rpng->ihdr.width;
*height = rpng->ihdr.height;
#ifdef GEKKO
/* we often use these in textures, make sure they're 32-byte aligned */
*data = (uint32_t*)memalign(32, rpng->ihdr.width *
rpng->ihdr.height * sizeof(uint32_t));
#else
*data = (uint32_t*)malloc(rpng->ihdr.width *
rpng->ihdr.height * sizeof(uint32_t));
#endif
if (!*data)
goto false_end;
process->adam7_restore_buf_size = 0;
process->restore_buf_size = 0;
process->palette = rpng->palette;
2015-03-14 01:56:32 +01:00
if (rpng->ihdr.interlace != 1)
if (png_reverse_filter_init(&rpng->ihdr, process) == -1)
goto false_end;
process->inflate_initialized = true;
return 1;
error:
false_end:
process->inflate_initialized = false;
return -1;
}
2015-09-19 01:42:58 +02:00
static bool png_read_plte(uint8_t *buf,
uint32_t *buffer, unsigned entries)
{
unsigned i;
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;
}
static bool png_read_trns(uint8_t *buf, uint32_t *palette, unsigned entries)
{
unsigned i;
for (i = 0; i < entries; i++, buf++, palette++)
{
*palette = (*palette & 0x00ffffff) | *buf << 24;
}
return true;
}
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;
}
2016-05-24 20:14:59 +02:00
static struct rpng_process *rpng_process_init(rpng_t *rpng, unsigned *width, unsigned *height)
{
2016-05-20 15:37:36 +02:00
uint8_t *inflate_buf = NULL;
2016-05-19 01:00:03 +02:00
struct rpng_process *process = (struct rpng_process*)calloc(1, sizeof(*process));
if (!process)
return NULL;
process->stream_backend = file_archive_get_zlib_file_backend();
png_pass_geom(&rpng->ihdr, rpng->ihdr.width,
rpng->ihdr.height, NULL, NULL, &process->inflate_buf_size);
if (rpng->ihdr.interlace == 1) /* To be sure. */
process->inflate_buf_size *= 2;
process->stream = process->stream_backend->stream_new();
2015-03-29 16:19:09 +02:00
if (!process->stream)
{
free(process);
return NULL;
}
2015-03-29 16:19:09 +02:00
if (!process->stream_backend->stream_decompress_init(process->stream))
{
free(process);
return NULL;
}
2015-03-29 14:30:53 +02:00
2016-05-20 15:37:36 +02:00
inflate_buf = (uint8_t*)malloc(process->inflate_buf_size);
if (!inflate_buf)
2016-05-23 21:28:43 +02:00
goto error;
2016-05-20 15:37:36 +02:00
process->inflate_buf = inflate_buf;
process->stream_backend->stream_set(
process->stream,
2015-03-29 15:35:55 +02:00
rpng->idat_buf.size,
process->inflate_buf_size,
2015-03-29 15:35:55 +02:00
rpng->idat_buf.data,
process->inflate_buf);
2015-03-14 01:56:32 +01:00
return process;
2016-05-23 21:28:43 +02:00
error:
if (process)
2016-05-23 21:33:26 +02:00
{
if (process->stream)
process->stream_backend->stream_free(process->stream);
2016-05-23 21:28:43 +02:00
free(process);
2016-05-23 21:33:26 +02:00
}
2016-05-23 21:28:43 +02:00
return NULL;
}
2015-09-19 01:40:29 +02:00
static bool read_chunk_header(uint8_t *buf, struct png_chunk *chunk)
{
unsigned i;
2016-10-16 01:34:16 +02:00
uint8_t dword[4];
dword[0] = '\0';
2015-09-19 01:40:29 +02:00
for (i = 0; i < 4; i++)
dword[i] = buf[i];
chunk->size = dword_be(dword);
for (i = 0; i < 4; i++)
2016-05-04 03:58:29 +02:00
chunk->type[i] = buf[i + 4];
2015-09-19 01:40:29 +02:00
return true;
}
static bool png_parse_ihdr(uint8_t *buf,
struct png_ihdr *ihdr)
{
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];
if (ihdr->width == 0 || ihdr->height == 0)
return false;
return true;
}
2016-05-13 09:49:38 +02:00
bool rpng_iterate_image(rpng_t *rpng)
2015-09-19 01:40:29 +02:00
{
unsigned i;
2016-10-16 01:34:16 +02:00
struct png_chunk chunk;
2016-05-04 03:58:29 +02:00
uint8_t *buf = (uint8_t*)rpng->buff_data;
2015-09-19 01:40:29 +02:00
2016-10-16 01:34:16 +02:00
chunk.size = 0;
chunk.type[0] = 0;
chunk.data = NULL;
2015-09-19 01:40:29 +02:00
if (!read_chunk_header(buf, &chunk))
return false;
2016-05-04 03:58:29 +02:00
*buf += 8;
2015-09-19 01:40:29 +02:00
#if 0
for (i = 0; i < 4; i++)
{
fprintf(stderr, "chunktype: %c\n", chunk.type[i]);
}
#endif
switch (png_chunk_type(&chunk))
{
case PNG_CHUNK_NOOP:
default:
break;
case PNG_CHUNK_ERROR:
goto error;
case PNG_CHUNK_IHDR:
if (rpng->has_ihdr || rpng->has_idat || rpng->has_iend)
goto error;
if (chunk.size != 13)
goto error;
if (!png_parse_ihdr(buf, &rpng->ihdr))
goto error;
if (!png_process_ihdr(&rpng->ihdr))
goto error;
rpng->has_ihdr = true;
break;
case PNG_CHUNK_PLTE:
{
unsigned entries = chunk.size / 3;
if (!rpng->has_ihdr || rpng->has_plte || rpng->has_iend || rpng->has_idat || rpng->has_trns)
2015-09-19 01:40:29 +02:00
goto error;
if (chunk.size % 3)
goto error;
if (entries > 256)
goto error;
buf += 8;
if (!png_read_plte(buf, rpng->palette, entries))
goto error;
rpng->has_plte = true;
}
break;
case PNG_CHUNK_tRNS:
2016-06-01 04:09:59 +02:00
if (rpng->has_idat)
goto error;
2016-06-01 04:09:59 +02:00
if (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT)
{
/* we should compare with the number of palette entries */
if (chunk.size > 256)
goto error;
2016-06-01 04:09:59 +02:00
buf += 8;
if (!png_read_trns(buf, rpng->palette, chunk.size))
goto error;
}
2016-06-01 04:09:59 +02:00
/* TODO: support colorkey in grayscale and truecolor images */
rpng->has_trns = true;
break;
2015-09-19 01:40:29 +02:00
case PNG_CHUNK_IDAT:
if (!(rpng->has_ihdr) || rpng->has_iend || (rpng->ihdr.color_type == PNG_IHDR_COLOR_PLT && !(rpng->has_plte)))
goto error;
if (!png_realloc_idat(&chunk, &rpng->idat_buf))
goto error;
buf += 8;
for (i = 0; i < chunk.size; i++)
rpng->idat_buf.data[i + rpng->idat_buf.size] = buf[i];
rpng->idat_buf.size += chunk.size;
rpng->has_idat = true;
break;
case PNG_CHUNK_IEND:
if (!(rpng->has_ihdr) || !(rpng->has_idat))
goto error;
rpng->has_iend = true;
goto error;
}
2016-05-04 03:58:29 +02:00
rpng->buff_data += chunk.size + 12;
2015-09-19 01:40:29 +02:00
return true;
error:
return false;
}
2016-05-13 09:49:38 +02:00
int rpng_process_image(rpng_t *rpng,
void **_data, size_t size, unsigned *width, unsigned *height)
2015-09-19 01:40:29 +02:00
{
uint32_t **data = (uint32_t**)_data;
2016-05-13 09:49:38 +02:00
(void)size;
if (!rpng->process)
2015-09-19 01:40:29 +02:00
{
2016-05-19 01:00:03 +02:00
struct rpng_process *process = rpng_process_init(
2016-05-24 20:14:59 +02:00
rpng, width, height);
if (!process)
goto error;
rpng->process = process;
2016-05-25 15:24:18 +02:00
return IMAGE_PROCESS_NEXT;
2015-09-19 01:40:29 +02:00
}
if (!rpng->process->inflate_initialized)
2015-09-19 01:40:29 +02:00
{
2016-05-13 09:51:08 +02:00
if (rpng_load_image_argb_process_inflate_init(rpng, data,
width, height) == -1)
goto error;
2016-05-25 15:24:18 +02:00
return IMAGE_PROCESS_NEXT;
2015-09-19 01:40:29 +02:00
}
return png_reverse_filter_iterate(rpng, data);
error:
if (rpng->process)
{
if (rpng->process->inflate_buf)
free(rpng->process->inflate_buf);
if (rpng->process->stream)
rpng->process->stream_backend->stream_free(rpng->process->stream);
free(rpng->process);
}
return IMAGE_PROCESS_ERROR;
2015-09-19 01:40:29 +02:00
}
2016-05-13 09:49:38 +02:00
void rpng_free(rpng_t *rpng)
2015-09-19 01:40:29 +02:00
{
if (!rpng)
return;
if (rpng->idat_buf.data)
free(rpng->idat_buf.data);
if (rpng->process)
2015-09-19 01:40:29 +02:00
{
2016-05-19 00:51:34 +02:00
if (rpng->process->inflate_buf)
free(rpng->process->inflate_buf);
if (rpng->process->stream)
{
2016-05-19 00:51:34 +02:00
if (rpng->process->stream_backend)
rpng->process->stream_backend->stream_free(rpng->process->stream);
free(rpng->process->stream);
}
free(rpng->process);
2015-09-19 01:40:29 +02:00
}
free(rpng);
}
2016-05-13 09:49:38 +02:00
bool rpng_start(rpng_t *rpng)
2015-09-19 01:40:29 +02:00
{
unsigned i;
2016-10-16 01:34:16 +02:00
char header[8];
2015-09-19 01:40:29 +02:00
if (!rpng)
return false;
2016-10-16 01:34:16 +02:00
header[0] = '\0';
2015-09-19 01:40:29 +02:00
for (i = 0; i < 8; i++)
header[i] = rpng->buff_data[i];
if (memcmp(header, png_magic, sizeof(png_magic)) != 0)
return false;
rpng->buff_data += 8;
return true;
}
bool rpng_is_valid(rpng_t *rpng)
{
if (!rpng)
return false;
if (rpng->has_ihdr)
return true;
if (rpng->has_idat)
return true;
if (rpng->has_iend)
return true;
return false;
}
bool rpng_set_buf_ptr(rpng_t *rpng, void *data)
2015-09-19 01:40:29 +02:00
{
if (!rpng)
return false;
rpng->buff_data = (uint8_t*)data;
2015-09-19 01:40:29 +02:00
return true;
}
rpng_t *rpng_alloc(void)
{
rpng_t *rpng = (rpng_t*)calloc(1, sizeof(rpng_t));
if (!rpng)
return NULL;
return rpng;
}