From ee972fbfa1093fe2e8641dc450e1c20347df2b04 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Tue, 24 May 2016 13:46:28 +0100 Subject: Avoid unnecessary alphas when decompressing images from streams. --- source/fitz/colorspace.c | 14 ++++++++------ source/fitz/draw-unpack.c | 5 +++-- source/fitz/image.c | 7 +++++-- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'source') diff --git a/source/fitz/colorspace.c b/source/fitz/colorspace.c index b4d511e8..7923446a 100644 --- a/source/fitz/colorspace.c +++ b/source/fitz/colorspace.c @@ -1930,31 +1930,32 @@ fz_new_indexed_colorspace(fz_context *ctx, fz_colorspace *base, int high, unsign } fz_pixmap * -fz_expand_indexed_pixmap(fz_context *ctx, fz_pixmap *src) +fz_expand_indexed_pixmap(fz_context *ctx, const fz_pixmap *src, int alpha) { struct indexed *idx; fz_pixmap *dst; - unsigned char *s, *d; + const unsigned char *s; + unsigned char *d; int y, x, k, n, high; unsigned char *lookup; fz_irect bbox; int s_line_inc, d_line_inc; assert(src->colorspace->to_rgb == indexed_to_rgb); - assert(src->n == 2); + assert(src->n == 1 + alpha); idx = src->colorspace->data; high = idx->high; lookup = idx->lookup; n = idx->base->n; - dst = fz_new_pixmap_with_bbox(ctx, idx->base, fz_pixmap_bbox(ctx, src, &bbox), src->alpha); + dst = fz_new_pixmap_with_bbox(ctx, idx->base, fz_pixmap_bbox(ctx, src, &bbox), alpha); s = src->samples; d = dst->samples; s_line_inc = src->stride - src->w * src->n; d_line_inc = dst->stride - dst->w * dst->n; - if (src->alpha) + if (alpha) { for (y = 0; y < src->h; y++) { @@ -1962,9 +1963,10 @@ fz_expand_indexed_pixmap(fz_context *ctx, fz_pixmap *src) { int v = *s++; int a = *s++; + int aa = a + (a>>7); v = fz_mini(v, high); for (k = 0; k < n; k++) - *d++ = fz_mul255(lookup[v * n + k], a); + *d++ = (aa * lookup[v * n + k] + 128)>>8; *d++ = a; } s += s_line_inc; diff --git a/source/fitz/draw-unpack.c b/source/fitz/draw-unpack.c index ccb3c62f..0211b7e4 100644 --- a/source/fitz/draw-unpack.c +++ b/source/fitz/draw-unpack.c @@ -179,7 +179,8 @@ fz_decode_indexed_tile(fz_context *ctx, fz_pixmap *pix, const float *decode, int unsigned char *p = pix->samples; int stride = pix->stride - pix->w * pix->n; int len; - int n = pix->n - 1; + int pn = pix->n; + int n = pn - pix->alpha; int needed; int k; int h; @@ -208,7 +209,7 @@ fz_decode_indexed_tile(fz_context *ctx, fz_pixmap *pix, const float *decode, int int value = (add[k] + (((p[k] << 8) * mul[k]) >> 8)) >> 8; p[k] = fz_clampi(value, 0, 255); } - p += n + 1; + p += pn; } p += stride; } diff --git a/source/fitz/image.c b/source/fitz/image.c index bd6e0b62..c49580bf 100644 --- a/source/fitz/image.c +++ b/source/fitz/image.c @@ -198,7 +198,10 @@ fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image fz_try(ctx) { - tile = fz_new_pixmap(ctx, image->colorspace, w, h, 1); + int alpha = (image->colorspace == NULL); + if (image->use_colorkey) + alpha = 1; + tile = fz_new_pixmap(ctx, image->colorspace, w, h, alpha); tile->interpolate = image->interpolate; stride = (w * image->n * image->bpc + 7) / 8; @@ -276,7 +279,7 @@ fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image { fz_pixmap *conv; fz_decode_indexed_tile(ctx, tile, image->decode, (1 << image->bpc) - 1); - conv = fz_expand_indexed_pixmap(ctx, tile); + conv = fz_expand_indexed_pixmap(ctx, tile, alpha); fz_drop_pixmap(ctx, tile); tile = conv; } -- cgit v1.2.3