Move more to rpng_process_t

This commit is contained in:
twinaphex 2015-02-25 10:05:21 +01:00
parent 93706c67e8
commit 1e2a91fca7
2 changed files with 54 additions and 52 deletions

View File

@ -92,67 +92,64 @@ static void deinterlace_pass(uint32_t *data, const struct png_ihdr *ihdr,
static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr, static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
struct rpng_process_t *pngp) struct rpng_process_t *pngp)
{ {
unsigned i, h; unsigned i;
unsigned bpp;
unsigned pitch;
size_t pass_size;
uint8_t *prev_scanline = NULL;
uint8_t *decoded_scanline = NULL;
bool ret = true; 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, &pngp->pass_size);
if (pngp->total_out < pass_size) if (pngp->total_out < pngp->pass_size)
return false; return false;
prev_scanline = (uint8_t*)calloc(1, pitch); pngp->prev_scanline = (uint8_t*)calloc(1, pngp->pitch);
decoded_scanline = (uint8_t*)calloc(1, pitch); pngp->decoded_scanline = (uint8_t*)calloc(1, pngp->pitch);
if (!prev_scanline || !decoded_scanline) if (!pngp->prev_scanline || !pngp->decoded_scanline)
GOTO_END_ERROR(); GOTO_END_ERROR();
for (h = 0; h < ihdr->height; for (pngp->h = 0; pngp->h < ihdr->height;
h++, pngp->inflate_buf += pitch, data += ihdr->width) pngp->h++, pngp->inflate_buf += pngp->pitch, data += ihdr->width)
{ {
unsigned filter = *pngp->inflate_buf++; unsigned filter = *pngp->inflate_buf++;
switch (filter) switch (filter)
{ {
case 0: /* None */ case 0: /* None */
memcpy(decoded_scanline, pngp->inflate_buf, pitch); memcpy(pngp->decoded_scanline, pngp->inflate_buf, pngp->pitch);
break; break;
case 1: /* Sub */ case 1: /* Sub */
for (i = 0; i < bpp; i++) for (i = 0; i < pngp->bpp; i++)
decoded_scanline[i] = pngp->inflate_buf[i]; pngp->decoded_scanline[i] = pngp->inflate_buf[i];
for (i = bpp; i < pitch; i++) for (i = pngp->bpp; i < pngp->pitch; i++)
decoded_scanline[i] = decoded_scanline[i - bpp] + pngp->inflate_buf[i]; pngp->decoded_scanline[i] = pngp->decoded_scanline[i - pngp->bpp] + pngp->inflate_buf[i];
break; break;
case 2: /* Up */ case 2: /* Up */
for (i = 0; i < pitch; i++) for (i = 0; i < pngp->pitch; i++)
decoded_scanline[i] = prev_scanline[i] + pngp->inflate_buf[i]; pngp->decoded_scanline[i] = pngp->prev_scanline[i] + pngp->inflate_buf[i];
break; break;
case 3: /* Average */ case 3: /* Average */
for (i = 0; i < bpp; i++) for (i = 0; i < pngp->bpp; i++)
{ {
uint8_t avg = prev_scanline[i] >> 1; uint8_t avg = pngp->prev_scanline[i] >> 1;
decoded_scanline[i] = avg + pngp->inflate_buf[i];
pngp->decoded_scanline[i] = avg + pngp->inflate_buf[i];
} }
for (i = bpp; i < pitch; i++) for (i = pngp->bpp; i < pngp->pitch; i++)
{ {
uint8_t avg = (decoded_scanline[i - bpp] + prev_scanline[i]) >> 1; uint8_t avg = (pngp->decoded_scanline[i - pngp->bpp] + pngp->prev_scanline[i]) >> 1;
decoded_scanline[i] = avg + pngp->inflate_buf[i];
pngp->decoded_scanline[i] = avg + pngp->inflate_buf[i];
} }
break; break;
case 4: /* Paeth */ case 4: /* Paeth */
for (i = 0; i < bpp; i++) for (i = 0; i < pngp->bpp; i++)
decoded_scanline[i] = paeth(0, prev_scanline[i], 0) + pngp->inflate_buf[i]; pngp->decoded_scanline[i] = paeth(0, pngp->prev_scanline[i], 0) + pngp->inflate_buf[i];
for (i = bpp; i < pitch; i++) for (i = pngp->bpp; i < pngp->pitch; i++)
decoded_scanline[i] = paeth(decoded_scanline[i - bpp], pngp->decoded_scanline[i] = paeth(pngp->decoded_scanline[i - pngp->bpp],
prev_scanline[i], prev_scanline[i - bpp]) + pngp->inflate_buf[i]; pngp->prev_scanline[i], pngp->prev_scanline[i - pngp->bpp]) + pngp->inflate_buf[i];
break; break;
default: default:
@ -160,24 +157,24 @@ static bool png_reverse_filter(uint32_t *data, const struct png_ihdr *ihdr,
} }
if (ihdr->color_type == 0) if (ihdr->color_type == 0)
copy_line_bw(data, decoded_scanline, ihdr->width, ihdr->depth); copy_line_bw(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
else if (ihdr->color_type == 2) else if (ihdr->color_type == 2)
copy_line_rgb(data, decoded_scanline, ihdr->width, ihdr->depth); copy_line_rgb(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
else if (ihdr->color_type == 3) else if (ihdr->color_type == 3)
copy_line_plt(data, decoded_scanline, ihdr->width, copy_line_plt(data, pngp->decoded_scanline, ihdr->width,
ihdr->depth, pngp->palette); ihdr->depth, pngp->palette);
else if (ihdr->color_type == 4) else if (ihdr->color_type == 4)
copy_line_gray_alpha(data, decoded_scanline, ihdr->width, copy_line_gray_alpha(data, pngp->decoded_scanline, ihdr->width,
ihdr->depth); ihdr->depth);
else if (ihdr->color_type == 6) else if (ihdr->color_type == 6)
copy_line_rgba(data, decoded_scanline, ihdr->width, ihdr->depth); copy_line_rgba(data, pngp->decoded_scanline, ihdr->width, ihdr->depth);
memcpy(prev_scanline, decoded_scanline, pitch); memcpy(pngp->prev_scanline, pngp->decoded_scanline, pngp->pitch);
} }
end: end:
free(decoded_scanline); free(pngp->decoded_scanline);
free(prev_scanline); free(pngp->prev_scanline);
return ret; return ret;
} }
@ -185,7 +182,6 @@ static bool png_reverse_filter_adam7(uint32_t *data,
const struct png_ihdr *ihdr, const struct png_ihdr *ihdr,
struct rpng_process_t *pngp) struct rpng_process_t *pngp)
{ {
unsigned pass;
static const struct adam7_pass passes[] = { static const struct adam7_pass passes[] = {
{ 0, 0, 8, 8 }, { 0, 0, 8, 8 },
{ 4, 0, 8, 8 }, { 4, 0, 8, 8 },
@ -196,21 +192,20 @@ static bool png_reverse_filter_adam7(uint32_t *data,
{ 0, 1, 1, 2 }, { 0, 1, 1, 2 },
}; };
for (pass = 0; pass < ARRAY_SIZE(passes); pass++) for (pngp->pass = 0; pngp->pass < ARRAY_SIZE(passes); pngp->pass++)
{ {
unsigned pass_width, pass_height; unsigned pass_width, pass_height;
size_t pass_size;
struct png_ihdr tmp_ihdr; struct png_ihdr tmp_ihdr;
uint32_t *tmp_data = NULL; uint32_t *tmp_data = NULL;
if (ihdr->width <= passes[pass].x || if (ihdr->width <= passes[pngp->pass].x ||
ihdr->height <= passes[pass].y) /* Empty pass */ ihdr->height <= passes[pngp->pass].y) /* Empty pass */
continue; continue;
pass_width = (ihdr->width - pass_width = (ihdr->width -
passes[pass].x + passes[pass].stride_x - 1) / passes[pass].stride_x; passes[pngp->pass].x + passes[pngp->pass].stride_x - 1) / passes[pngp->pass].stride_x;
pass_height = (ihdr->height - passes[pass].y + pass_height = (ihdr->height - passes[pngp->pass].y +
passes[pass].stride_y - 1) / passes[pass].stride_y; passes[pngp->pass].stride_y - 1) / passes[pngp->pass].stride_y;
tmp_data = (uint32_t*)malloc( tmp_data = (uint32_t*)malloc(
pass_width * pass_height * sizeof(uint32_t)); pass_width * pass_height * sizeof(uint32_t));
@ -223,9 +218,9 @@ static bool png_reverse_filter_adam7(uint32_t *data,
tmp_ihdr.height = pass_height; tmp_ihdr.height = pass_height;
png_pass_geom(&tmp_ihdr, pass_width, png_pass_geom(&tmp_ihdr, pass_width,
pass_height, NULL, NULL, &pass_size); pass_height, NULL, NULL, &pngp->pass_size);
if (pass_size > pngp->total_out) if (pngp->pass_size > pngp->total_out)
{ {
free(tmp_data); free(tmp_data);
return false; return false;
@ -238,11 +233,11 @@ static bool png_reverse_filter_adam7(uint32_t *data,
return false; return false;
} }
pngp->inflate_buf += pass_size; pngp->inflate_buf += pngp->pass_size;
pngp->total_out -= pass_size; pngp->total_out -= pngp->pass_size;
deinterlace_pass(data, deinterlace_pass(data,
ihdr, tmp_data, pass_width, pass_height, &passes[pass]); ihdr, tmp_data, pass_width, pass_height, &passes[pngp->pass]);
free(tmp_data); free(tmp_data);
} }

View File

@ -64,6 +64,13 @@ struct rpng_process_t
{ {
const uint8_t *inflate_buf; const uint8_t *inflate_buf;
const uint32_t *palette; const uint32_t *palette;
uint8_t *prev_scanline;
uint8_t *decoded_scanline;
unsigned bpp;
unsigned h;
unsigned pitch;
unsigned pass;
size_t pass_size;
size_t total_out; size_t total_out;
}; };