From 3119f69dd9536cdaab0719fba2898be894e8fc01 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Tue, 7 Nov 2017 17:32:15 +0000 Subject: Pixmap writers for formats with alpha should handle premultiplied data. Any pixmap writers that can handle data with an alpha plane should accept that data in premultiplied form, and write it out appropriately for the file format. This avoids the need to unpremultiply data in mudraw, and solves the issue we were seeing where we want the png writer to be able to cope with premultiplied data (such as for the debug blending routines) and unpremultiplied data (such as that given after mudraw has unpremultiplied the data). --- source/fitz/output-tga.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'source/fitz/output-tga.c') diff --git a/source/fitz/output-tga.c b/source/fitz/output-tga.c index 3bee3640..0e7a213c 100644 --- a/source/fitz/output-tga.c +++ b/source/fitz/output-tga.c @@ -13,19 +13,22 @@ typedef struct { static inline void tga_put_pixel(fz_context *ctx, fz_output *out, const unsigned char *data, int n, int is_bgr) { + int a, inva; switch(n) { case 4: /* RGBA or BGRA */ + a = data[3]; + inva = a ? 256 * 255 / a : 0; if (!is_bgr) { - fz_write_byte(ctx, out, data[2]); - fz_write_byte(ctx, out, data[1]); - fz_write_byte(ctx, out, data[0]); + fz_write_byte(ctx, out, (data[2] * inva + 128)>>8); + fz_write_byte(ctx, out, (data[1] * inva + 128)>>8); + fz_write_byte(ctx, out, (data[0] * inva + 128)>>8); } else { - fz_write_byte(ctx, out, data[0]); - fz_write_byte(ctx, out, data[1]); - fz_write_byte(ctx, out, data[2]); + fz_write_byte(ctx, out, (data[0] * inva + 128)>>8); + fz_write_byte(ctx, out, (data[1] * inva + 128)>>8); + fz_write_byte(ctx, out, (data[2] * inva + 128)>>8); } - fz_write_byte(ctx, out, data[3]); + fz_write_byte(ctx, out, a); break; case 3: /* RGB or BGR */ if (!is_bgr) { @@ -39,10 +42,13 @@ static inline void tga_put_pixel(fz_context *ctx, fz_output *out, const unsigned } break; case 2: /* GA */ - fz_write_byte(ctx, out, data[0]); - fz_write_byte(ctx, out, data[0]); - fz_write_byte(ctx, out, data[0]); - fz_write_byte(ctx, out, data[1]); + a = data[1]; + inva = a ? 256 * 255 / a : 0; + inva = (data[0] * inva + 128)>>8; + fz_write_byte(ctx, out, inva); + fz_write_byte(ctx, out, inva); + fz_write_byte(ctx, out, inva); + fz_write_byte(ctx, out, a); break; case 1: /* G */ fz_write_byte(ctx, out, data[0]); -- cgit v1.2.3