mirror of
https://github.com/CTCaer/RetroArch.git
synced 2025-01-10 13:02:27 +00:00
(RPNG) Move around code to make it suitable for Griffin
This commit is contained in:
parent
ff939e9b03
commit
cd7d967701
@ -33,13 +33,7 @@
|
||||
#endif
|
||||
|
||||
#include "rpng_common.h"
|
||||
|
||||
#undef GOTO_END_ERROR
|
||||
#define GOTO_END_ERROR() do { \
|
||||
fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__); \
|
||||
ret = false; \
|
||||
goto end; \
|
||||
} while(0)
|
||||
#include "rpng_decode_common.h"
|
||||
|
||||
static bool read_chunk_header_fio(FILE *file, struct png_chunk *chunk)
|
||||
{
|
||||
@ -171,243 +165,6 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
case 0:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bpp = (ihdr->depth * 3 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
bpp = (ihdr->depth * 2 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
|
||||
const uint8_t *inflate_buf, size_t inflate_buf_size,
|
||||
const uint32_t *palette)
|
||||
{
|
||||
unsigned i, h;
|
||||
unsigned bpp;
|
||||
unsigned pitch;
|
||||
size_t pass_size;
|
||||
uint8_t *prev_scanline = NULL;
|
||||
uint8_t *decoded_scanline = NULL;
|
||||
bool ret = true;
|
||||
|
||||
png_pass_geom(ihdr, ihdr->width, ihdr->height, &bpp, &pitch, &pass_size);
|
||||
|
||||
if (inflate_buf_size < pass_size)
|
||||
return false;
|
||||
|
||||
prev_scanline = (uint8_t*)calloc(1, pitch);
|
||||
decoded_scanline = (uint8_t*)calloc(1, pitch);
|
||||
|
||||
if (!prev_scanline || !decoded_scanline)
|
||||
GOTO_END_ERROR();
|
||||
|
||||
for (h = 0; h < ihdr->height;
|
||||
h++, inflate_buf += pitch, data += ihdr->width)
|
||||
{
|
||||
unsigned filter = *inflate_buf++;
|
||||
|
||||
switch (filter)
|
||||
{
|
||||
case 0: /* None */
|
||||
memcpy(decoded_scanline, inflate_buf, pitch);
|
||||
break;
|
||||
|
||||
case 1: /* Sub */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = decoded_scanline[i - bpp] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 2: /* Up */
|
||||
for (i = 0; i < pitch; i++)
|
||||
decoded_scanline[i] = prev_scanline[i] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 3: /* Average */
|
||||
for (i = 0; i < bpp; i++)
|
||||
{
|
||||
uint8_t avg = prev_scanline[i] >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
for (i = bpp; i < pitch; i++)
|
||||
{
|
||||
uint8_t avg = (decoded_scanline[i - bpp] + prev_scanline[i]) >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
break;
|
||||
|
||||
case 4: /* Paeth */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = paeth(0, prev_scanline[i], 0) + inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = paeth(decoded_scanline[i - bpp],
|
||||
prev_scanline[i], prev_scanline[i - bpp]) + inflate_buf[i];
|
||||
break;
|
||||
|
||||
default:
|
||||
GOTO_END_ERROR();
|
||||
}
|
||||
|
||||
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)
|
||||
copy_line_plt(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth, palette);
|
||||
else if (ihdr->color_type == 4)
|
||||
copy_line_gray_alpha(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth);
|
||||
else if (ihdr->color_type == 6)
|
||||
copy_line_rgba(data, decoded_scanline, ihdr->width, ihdr->depth);
|
||||
|
||||
memcpy(prev_scanline, decoded_scanline, pitch);
|
||||
}
|
||||
|
||||
end:
|
||||
free(decoded_scanline);
|
||||
free(prev_scanline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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,
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
unsigned pass;
|
||||
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 },
|
||||
};
|
||||
|
||||
for (pass = 0; pass < ARRAY_SIZE(passes); pass++)
|
||||
{
|
||||
unsigned pass_width, pass_height;
|
||||
size_t pass_size;
|
||||
struct png_ihdr tmp_ihdr;
|
||||
uint32_t *tmp_data = NULL;
|
||||
|
||||
if (ihdr->width <= passes[pass].x ||
|
||||
ihdr->height <= passes[pass].y) /* Empty pass */
|
||||
continue;
|
||||
|
||||
pass_width = (ihdr->width -
|
||||
passes[pass].x + passes[pass].stride_x - 1) / passes[pass].stride_x;
|
||||
pass_height = (ihdr->height - passes[pass].y +
|
||||
passes[pass].stride_y - 1) / passes[pass].stride_y;
|
||||
|
||||
tmp_data = (uint32_t*)malloc(
|
||||
pass_width * pass_height * sizeof(uint32_t));
|
||||
|
||||
if (!tmp_data)
|
||||
return false;
|
||||
|
||||
tmp_ihdr = *ihdr;
|
||||
tmp_ihdr.width = pass_width;
|
||||
tmp_ihdr.height = pass_height;
|
||||
|
||||
png_pass_geom(&tmp_ihdr, pass_width,
|
||||
pass_height, NULL, NULL, &pass_size);
|
||||
|
||||
if (pass_size > inflate_buf_size)
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!png_reverse_filter(tmp_data,
|
||||
&tmp_ihdr, inflate_buf, pass_size, palette))
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
inflate_buf += pass_size;
|
||||
inflate_buf_size -= pass_size;
|
||||
|
||||
deinterlace_pass(data,
|
||||
ihdr, tmp_data, pass_width, pass_height, &passes[pass]);
|
||||
free(tmp_data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool png_append_idat_fio(FILE *file,
|
||||
const struct png_chunk *chunk, struct idat_buffer *buf)
|
||||
{
|
||||
|
@ -25,6 +25,13 @@
|
||||
|
||||
#include <retro_inline.h>
|
||||
|
||||
#undef GOTO_END_ERROR
|
||||
#define GOTO_END_ERROR() do { \
|
||||
fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__); \
|
||||
ret = false; \
|
||||
goto end; \
|
||||
} while(0)
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#endif
|
||||
@ -67,6 +74,14 @@ enum png_chunk_type
|
||||
PNG_CHUNK_IEND
|
||||
};
|
||||
|
||||
struct adam7_pass
|
||||
{
|
||||
unsigned x;
|
||||
unsigned y;
|
||||
unsigned stride_x;
|
||||
unsigned stride_y;
|
||||
};
|
||||
|
||||
/* Paeth prediction filter. */
|
||||
static INLINE int paeth(int a, int b, int c)
|
||||
{
|
||||
|
254
libretro-common/formats/png/rpng_decode_common.h
Normal file
254
libretro-common/formats/png/rpng_decode_common.h
Normal file
@ -0,0 +1,254 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#ifndef _RPNG_DECODE_COMMON_H
|
||||
#define _RPNG_DECODE_COMMON_H
|
||||
|
||||
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)
|
||||
{
|
||||
case 0:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bpp = (ihdr->depth * 3 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
bpp = (ihdr->depth * 2 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
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;
|
||||
}
|
||||
|
||||
static void 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];
|
||||
}
|
||||
}
|
||||
|
||||
static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
|
||||
const uint8_t *inflate_buf, size_t inflate_buf_size,
|
||||
const uint32_t *palette)
|
||||
{
|
||||
unsigned i, h;
|
||||
unsigned bpp;
|
||||
unsigned pitch;
|
||||
size_t pass_size;
|
||||
uint8_t *prev_scanline = NULL;
|
||||
uint8_t *decoded_scanline = NULL;
|
||||
bool ret = true;
|
||||
|
||||
png_pass_geom(ihdr, ihdr->width, ihdr->height, &bpp, &pitch, &pass_size);
|
||||
|
||||
if (inflate_buf_size < pass_size)
|
||||
return false;
|
||||
|
||||
prev_scanline = (uint8_t*)calloc(1, pitch);
|
||||
decoded_scanline = (uint8_t*)calloc(1, pitch);
|
||||
|
||||
if (!prev_scanline || !decoded_scanline)
|
||||
GOTO_END_ERROR();
|
||||
|
||||
for (h = 0; h < ihdr->height;
|
||||
h++, inflate_buf += pitch, data += ihdr->width)
|
||||
{
|
||||
unsigned filter = *inflate_buf++;
|
||||
|
||||
switch (filter)
|
||||
{
|
||||
case 0: /* None */
|
||||
memcpy(decoded_scanline, inflate_buf, pitch);
|
||||
break;
|
||||
|
||||
case 1: /* Sub */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = decoded_scanline[i - bpp] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 2: /* Up */
|
||||
for (i = 0; i < pitch; i++)
|
||||
decoded_scanline[i] = prev_scanline[i] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 3: /* Average */
|
||||
for (i = 0; i < bpp; i++)
|
||||
{
|
||||
uint8_t avg = prev_scanline[i] >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
for (i = bpp; i < pitch; i++)
|
||||
{
|
||||
uint8_t avg = (decoded_scanline[i - bpp] + prev_scanline[i]) >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
break;
|
||||
|
||||
case 4: /* Paeth */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = paeth(0, prev_scanline[i], 0) + inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = paeth(decoded_scanline[i - bpp],
|
||||
prev_scanline[i], prev_scanline[i - bpp]) + inflate_buf[i];
|
||||
break;
|
||||
|
||||
default:
|
||||
GOTO_END_ERROR();
|
||||
}
|
||||
|
||||
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)
|
||||
copy_line_plt(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth, palette);
|
||||
else if (ihdr->color_type == 4)
|
||||
copy_line_gray_alpha(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth);
|
||||
else if (ihdr->color_type == 6)
|
||||
copy_line_rgba(data, decoded_scanline, ihdr->width, ihdr->depth);
|
||||
|
||||
memcpy(prev_scanline, decoded_scanline, pitch);
|
||||
}
|
||||
|
||||
end:
|
||||
free(decoded_scanline);
|
||||
free(prev_scanline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
unsigned pass;
|
||||
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 },
|
||||
};
|
||||
|
||||
for (pass = 0; pass < ARRAY_SIZE(passes); pass++)
|
||||
{
|
||||
unsigned pass_width, pass_height;
|
||||
size_t pass_size;
|
||||
struct png_ihdr tmp_ihdr;
|
||||
uint32_t *tmp_data = NULL;
|
||||
|
||||
if (ihdr->width <= passes[pass].x ||
|
||||
ihdr->height <= passes[pass].y) /* Empty pass */
|
||||
continue;
|
||||
|
||||
pass_width = (ihdr->width -
|
||||
passes[pass].x + passes[pass].stride_x - 1) / passes[pass].stride_x;
|
||||
pass_height = (ihdr->height - passes[pass].y +
|
||||
passes[pass].stride_y - 1) / passes[pass].stride_y;
|
||||
|
||||
tmp_data = (uint32_t*)malloc(
|
||||
pass_width * pass_height * sizeof(uint32_t));
|
||||
|
||||
if (!tmp_data)
|
||||
return false;
|
||||
|
||||
tmp_ihdr = *ihdr;
|
||||
tmp_ihdr.width = pass_width;
|
||||
tmp_ihdr.height = pass_height;
|
||||
|
||||
png_pass_geom(&tmp_ihdr, pass_width,
|
||||
pass_height, NULL, NULL, &pass_size);
|
||||
|
||||
if (pass_size > inflate_buf_size)
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!png_reverse_filter(tmp_data,
|
||||
&tmp_ihdr, inflate_buf, pass_size, palette))
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
inflate_buf += pass_size;
|
||||
inflate_buf_size -= pass_size;
|
||||
|
||||
deinterlace_pass(data,
|
||||
ihdr, tmp_data, pass_width, pass_height, &passes[pass]);
|
||||
free(tmp_data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
@ -35,17 +35,7 @@
|
||||
#endif
|
||||
|
||||
#include "rpng_common.h"
|
||||
|
||||
#undef GOTO_END_ERROR
|
||||
#define GOTO_END_ERROR() do { \
|
||||
fprintf(stderr, "[RPNG]: Error in line %d.\n", __LINE__); \
|
||||
ret = false; \
|
||||
goto end; \
|
||||
} while(0)
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#endif
|
||||
#include "rpng_decode_common.h"
|
||||
|
||||
static bool read_chunk_header(uint8_t *buf, struct png_chunk *chunk)
|
||||
{
|
||||
@ -150,243 +140,6 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
case 0:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bpp = (ihdr->depth * 3 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 3 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bpp = (ihdr->depth + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth + 7) / 8;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
bpp = (ihdr->depth * 2 + 7) / 8;
|
||||
pitch = (ihdr->width * ihdr->depth * 2 + 7) / 8;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
|
||||
const uint8_t *inflate_buf, size_t inflate_buf_size,
|
||||
const uint32_t *palette)
|
||||
{
|
||||
unsigned i, h;
|
||||
unsigned bpp;
|
||||
unsigned pitch;
|
||||
size_t pass_size;
|
||||
uint8_t *prev_scanline = NULL;
|
||||
uint8_t *decoded_scanline = NULL;
|
||||
bool ret = true;
|
||||
|
||||
png_pass_geom(ihdr, ihdr->width, ihdr->height, &bpp, &pitch, &pass_size);
|
||||
|
||||
if (inflate_buf_size < pass_size)
|
||||
return false;
|
||||
|
||||
prev_scanline = (uint8_t*)calloc(1, pitch);
|
||||
decoded_scanline = (uint8_t*)calloc(1, pitch);
|
||||
|
||||
if (!prev_scanline || !decoded_scanline)
|
||||
GOTO_END_ERROR();
|
||||
|
||||
for (h = 0; h < ihdr->height;
|
||||
h++, inflate_buf += pitch, data += ihdr->width)
|
||||
{
|
||||
unsigned filter = *inflate_buf++;
|
||||
|
||||
switch (filter)
|
||||
{
|
||||
case 0: /* None */
|
||||
memcpy(decoded_scanline, inflate_buf, pitch);
|
||||
break;
|
||||
|
||||
case 1: /* Sub */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = decoded_scanline[i - bpp] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 2: /* Up */
|
||||
for (i = 0; i < pitch; i++)
|
||||
decoded_scanline[i] = prev_scanline[i] + inflate_buf[i];
|
||||
break;
|
||||
|
||||
case 3: /* Average */
|
||||
for (i = 0; i < bpp; i++)
|
||||
{
|
||||
uint8_t avg = prev_scanline[i] >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
for (i = bpp; i < pitch; i++)
|
||||
{
|
||||
uint8_t avg = (decoded_scanline[i - bpp] + prev_scanline[i]) >> 1;
|
||||
decoded_scanline[i] = avg + inflate_buf[i];
|
||||
}
|
||||
break;
|
||||
|
||||
case 4: /* Paeth */
|
||||
for (i = 0; i < bpp; i++)
|
||||
decoded_scanline[i] = paeth(0, prev_scanline[i], 0) + inflate_buf[i];
|
||||
for (i = bpp; i < pitch; i++)
|
||||
decoded_scanline[i] = paeth(decoded_scanline[i - bpp],
|
||||
prev_scanline[i], prev_scanline[i - bpp]) + inflate_buf[i];
|
||||
break;
|
||||
|
||||
default:
|
||||
GOTO_END_ERROR();
|
||||
}
|
||||
|
||||
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)
|
||||
copy_line_plt(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth, palette);
|
||||
else if (ihdr->color_type == 4)
|
||||
copy_line_gray_alpha(data, decoded_scanline, ihdr->width,
|
||||
ihdr->depth);
|
||||
else if (ihdr->color_type == 6)
|
||||
copy_line_rgba(data, decoded_scanline, ihdr->width, ihdr->depth);
|
||||
|
||||
memcpy(prev_scanline, decoded_scanline, pitch);
|
||||
}
|
||||
|
||||
end:
|
||||
free(decoded_scanline);
|
||||
free(prev_scanline);
|
||||
return ret;
|
||||
}
|
||||
|
||||
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,
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
unsigned pass;
|
||||
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 },
|
||||
};
|
||||
|
||||
for (pass = 0; pass < ARRAY_SIZE(passes); pass++)
|
||||
{
|
||||
unsigned pass_width, pass_height;
|
||||
size_t pass_size;
|
||||
struct png_ihdr tmp_ihdr;
|
||||
uint32_t *tmp_data = NULL;
|
||||
|
||||
if (ihdr->width <= passes[pass].x ||
|
||||
ihdr->height <= passes[pass].y) /* Empty pass */
|
||||
continue;
|
||||
|
||||
pass_width = (ihdr->width -
|
||||
passes[pass].x + passes[pass].stride_x - 1) / passes[pass].stride_x;
|
||||
pass_height = (ihdr->height - passes[pass].y +
|
||||
passes[pass].stride_y - 1) / passes[pass].stride_y;
|
||||
|
||||
tmp_data = (uint32_t*)malloc(
|
||||
pass_width * pass_height * sizeof(uint32_t));
|
||||
|
||||
if (!tmp_data)
|
||||
return false;
|
||||
|
||||
tmp_ihdr = *ihdr;
|
||||
tmp_ihdr.width = pass_width;
|
||||
tmp_ihdr.height = pass_height;
|
||||
|
||||
png_pass_geom(&tmp_ihdr, pass_width,
|
||||
pass_height, NULL, NULL, &pass_size);
|
||||
|
||||
if (pass_size > inflate_buf_size)
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!png_reverse_filter(tmp_data,
|
||||
&tmp_ihdr, inflate_buf, pass_size, palette))
|
||||
{
|
||||
free(tmp_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
inflate_buf += pass_size;
|
||||
inflate_buf_size -= pass_size;
|
||||
|
||||
deinterlace_pass(data,
|
||||
ihdr, tmp_data, pass_width, pass_height, &passes[pass]);
|
||||
free(tmp_data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user