(PNG) More refactors

This commit is contained in:
twinaphex 2015-02-26 07:14:43 +01:00
parent c874ee3aa1
commit 9eaf62f319
2 changed files with 19 additions and 19 deletions

View File

@ -94,64 +94,62 @@ static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
const uint32_t *palette)
{
unsigned i;
unsigned bpp;
unsigned pitch;
size_t pass_size;
bool ret = true;
png_pass_geom(ihdr, ihdr->width, ihdr->height, &bpp, &pitch, &pass_size);
png_pass_geom(ihdr, ihdr->width, ihdr->height, &pngp->bpp, &pngp->pitch, &pass_size);
if (pngp->stream.total_out < pass_size)
return false;
pngp->prev_scanline = (uint8_t*)calloc(1, pitch);
pngp->decoded_scanline = (uint8_t*)calloc(1, pitch);
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_END_ERROR();
for (; pngp->h < ihdr->height;
pngp->h++, inflate_buf += pitch, data += ihdr->width)
pngp->h++, inflate_buf += pngp->pitch, data += ihdr->width)
{
unsigned filter = *inflate_buf++;
switch (filter)
{
case 0: /* None */
memcpy(pngp->decoded_scanline, inflate_buf, pitch);
memcpy(pngp->decoded_scanline, inflate_buf, pngp->pitch);
break;
case 1: /* Sub */
for (i = 0; i < bpp; i++)
for (i = 0; i < pngp->bpp; i++)
pngp->decoded_scanline[i] = inflate_buf[i];
for (i = bpp; i < pitch; i++)
pngp->decoded_scanline[i] = pngp->decoded_scanline[i - bpp] + inflate_buf[i];
for (i = pngp->bpp; i < pngp->pitch; i++)
pngp->decoded_scanline[i] = pngp->decoded_scanline[i - pngp->bpp] + inflate_buf[i];
break;
case 2: /* Up */
for (i = 0; i < pitch; i++)
for (i = 0; i < pngp->pitch; i++)
pngp->decoded_scanline[i] = pngp->prev_scanline[i] + inflate_buf[i];
break;
case 3: /* Average */
for (i = 0; i < bpp; i++)
for (i = 0; i < pngp->bpp; i++)
{
uint8_t avg = pngp->prev_scanline[i] >> 1;
pngp->decoded_scanline[i] = avg + inflate_buf[i];
}
for (i = bpp; i < pitch; i++)
for (i = pngp->bpp; i < pngp->pitch; i++)
{
uint8_t avg = (pngp->decoded_scanline[i - bpp] + pngp->prev_scanline[i]) >> 1;
uint8_t avg = (pngp->decoded_scanline[i - pngp->bpp] + pngp->prev_scanline[i]) >> 1;
pngp->decoded_scanline[i] = avg + inflate_buf[i];
}
break;
case 4: /* Paeth */
for (i = 0; i < bpp; i++)
for (i = 0; i < pngp->bpp; i++)
pngp->decoded_scanline[i] = paeth(0, pngp->prev_scanline[i], 0) + inflate_buf[i];
for (i = bpp; i < pitch; i++)
pngp->decoded_scanline[i] = paeth(pngp->decoded_scanline[i - bpp],
pngp->prev_scanline[i], pngp->prev_scanline[i - bpp]) + inflate_buf[i];
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]) + inflate_buf[i];
break;
default:
@ -171,7 +169,7 @@ static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
else if (ihdr->color_type == 6)
copy_line_rgba(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
memcpy(pngp->prev_scanline, pngp->decoded_scanline, pitch);
memcpy(pngp->prev_scanline, pngp->decoded_scanline, pngp->pitch);
}
end:

View File

@ -67,6 +67,8 @@ struct rpng_process_t
uint8_t *decoded_scanline;
uint8_t *inflate_buf;
size_t inflate_buf_size;
unsigned bpp;
unsigned pitch;
unsigned pass;
unsigned h;
z_stream stream;