diff options
Diffstat (limited to 'source/fitz')
-rw-r--r-- | source/fitz/output-png.c | 46 | ||||
-rw-r--r-- | source/fitz/output-pnm.c | 54 | ||||
-rw-r--r-- | source/fitz/output-tga.c | 12 | ||||
-rw-r--r-- | source/fitz/pixmap.c | 6 |
4 files changed, 46 insertions, 72 deletions
diff --git a/source/fitz/output-png.c b/source/fitz/output-png.c index ab8826ba..9fe58ef0 100644 --- a/source/fitz/output-png.c +++ b/source/fitz/output-png.c @@ -1,9 +1,5 @@ #include "mupdf/fitz.h" -/* - * Write pixmap to PNG file (with or without alpha channel) - */ - #include <zlib.h> static inline void big32(unsigned char *buf, unsigned int v) @@ -27,7 +23,7 @@ static void putchunk(fz_context *ctx, fz_output *out, char *tag, unsigned char * } void -fz_save_pixmap_as_png(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int savealpha) +fz_save_pixmap_as_png(fz_context *ctx, fz_pixmap *pixmap, const char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_png_output_context *poc = NULL; @@ -36,7 +32,7 @@ fz_save_pixmap_as_png(fz_context *ctx, fz_pixmap *pixmap, const char *filename, fz_try(ctx) { - poc = fz_write_png_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, savealpha); + poc = fz_write_png_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha); fz_write_png_band(ctx, out, poc, pixmap->stride, 0, pixmap->h, pixmap->samples); } fz_always(ctx) @@ -51,14 +47,14 @@ fz_save_pixmap_as_png(fz_context *ctx, fz_pixmap *pixmap, const char *filename, } void -fz_write_pixmap_as_png(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, int savealpha) +fz_write_pixmap_as_png(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap) { fz_png_output_context *poc; if (!out) return; - poc = fz_write_png_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, savealpha); + poc = fz_write_png_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha); fz_try(ctx) { @@ -84,11 +80,10 @@ struct fz_png_output_context_s int h; int n; int alpha; - int savealpha; }; fz_png_output_context * -fz_write_png_header(fz_context *ctx, fz_output *out, int w, int h, int n, int alpha, int savealpha) +fz_write_png_header(fz_context *ctx, fz_output *out, int w, int h, int n, int alpha) { static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; unsigned char head[13]; @@ -110,16 +105,11 @@ fz_write_png_header(fz_context *ctx, fz_output *out, int w, int h, int n, int al fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as png"); } - /* If we have no alpha, save no alpha */ - if (!alpha) - savealpha = 0; - poc = fz_malloc_struct(ctx, fz_png_output_context); poc->w = w; poc->h = h; poc->n = n; poc->alpha = alpha; - poc->savealpha = savealpha; big32(head+0, w); big32(head+4, h); @@ -139,8 +129,8 @@ void fz_write_png_band(fz_context *ctx, fz_output *out, fz_png_output_context *poc, int stride, int band, int bandheight, unsigned char *sp) { unsigned char *dp; - int y, x, k, sn, dn, err, finalband; - int w, h, n, alpha, savealpha; + int y, x, k, err, finalband; + int w, h, n, alpha; if (!out || !sp || !poc) return; @@ -149,19 +139,15 @@ fz_write_png_band(fz_context *ctx, fz_output *out, fz_png_output_context *poc, i h = poc->h; n = poc->n; alpha = poc->alpha; - savealpha = poc->savealpha; band *= bandheight; finalband = (band+bandheight >= h); if (finalband) bandheight = h - band; - sn = n; - dn = n - alpha + savealpha; - if (poc->udata == NULL) { - poc->usize = (w * dn + 1) * bandheight; + poc->usize = (w * n + 1) * bandheight; /* Sadly the bound returned by compressBound is just for a * single usize chunk; if you compress a sequence of them * the buffering can result in you suddenly getting a block @@ -186,21 +172,21 @@ fz_write_png_band(fz_context *ctx, fz_output *out, fz_png_output_context *poc, i } dp = poc->udata; - stride -= w*sn; + stride -= w*n; for (y = 0; y < bandheight; y++) { *dp++ = 1; /* sub prediction filter */ for (x = 0; x < w; x++) { - for (k = 0; k < dn; k++) + for (k = 0; k < n; k++) { if (x == 0) dp[k] = sp[k]; else - dp[k] = sp[k] - sp[k-sn]; + dp[k] = sp[k] - sp[k-n]; } - sp += sn; - dp += dn; + sp += n; + dp += n; } sp += stride; } @@ -272,7 +258,7 @@ png_from_pixmap(fz_context *ctx, fz_pixmap *pix, int drop) { if (pix->colorspace && pix->colorspace != fz_device_gray(ctx) && pix->colorspace != fz_device_rgb(ctx)) { - pix2 = fz_new_pixmap(ctx, fz_device_rgb(ctx), pix->w, pix->h, 1); + pix2 = fz_new_pixmap(ctx, fz_device_rgb(ctx), pix->w, pix->h, pix->alpha); fz_convert_pixmap(ctx, pix2, pix); if (drop) fz_drop_pixmap(ctx, pix); @@ -280,7 +266,7 @@ png_from_pixmap(fz_context *ctx, fz_pixmap *pix, int drop) } buf = fz_new_buffer(ctx, 1024); out = fz_new_output_with_buffer(ctx, buf); - fz_write_pixmap_as_png(ctx, out, pix, 1); + fz_write_pixmap_as_png(ctx, out, pix); } fz_always(ctx) { @@ -298,7 +284,7 @@ png_from_pixmap(fz_context *ctx, fz_pixmap *pix, int drop) fz_buffer * fz_new_buffer_from_image_as_png(fz_context *ctx, fz_image *image) { - return png_from_pixmap(ctx, fz_get_pixmap_from_image(ctx, image, NULL, NULL, NULL, NULL), 1); + return png_from_pixmap(ctx, fz_get_pixmap_from_image(ctx, image, NULL, NULL, NULL, NULL), 0); } fz_buffer * diff --git a/source/fitz/output-pnm.c b/source/fitz/output-pnm.c index 197c92e9..e253d96a 100644 --- a/source/fitz/output-pnm.c +++ b/source/fitz/output-pnm.c @@ -118,41 +118,28 @@ fz_save_pixmap_as_pnm(fz_context *ctx, fz_pixmap *pixmap, char *filename) */ void -fz_write_pam_header(fz_context *ctx, fz_output *out, int w, int h, int n, int alpha, int savealpha) +fz_write_pam_header(fz_context *ctx, fz_output *out, int w, int h, int n, int alpha) { - int sn = n; - int dn = n - alpha; - if (!alpha) - savealpha = 0; - - dn += savealpha; - fz_printf(ctx, out, "P7\n"); fz_printf(ctx, out, "WIDTH %d\n", w); fz_printf(ctx, out, "HEIGHT %d\n", h); - fz_printf(ctx, out, "DEPTH %d\n", dn); + fz_printf(ctx, out, "DEPTH %d\n", n); fz_printf(ctx, out, "MAXVAL 255\n"); - if (dn == 1) fz_printf(ctx, out, "TUPLTYPE GRAYSCALE\n"); - else if (dn == 2 && sn == 2) fz_printf(ctx, out, "TUPLTYPE GRAYSCALE_ALPHA\n"); - else if (dn == 3 && sn == 3) fz_printf(ctx, out, "TUPLTYPE RGB\n"); - else if (dn == 4 && sn == 3) fz_printf(ctx, out, "TUPLTYPE RGB_ALPHA\n"); - else if (dn == 4 && sn == 4) fz_printf(ctx, out, "TUPLTYPE CMYK\n"); - else if (dn == 5 && sn == 4) fz_printf(ctx, out, "TUPLTYPE CMYK_ALPHA\n"); + if (n == 1) fz_printf(ctx, out, "TUPLTYPE GRAYSCALE\n"); + else if (n == 2) fz_printf(ctx, out, "TUPLTYPE GRAYSCALE_ALPHA\n"); + else if (n == 3) fz_printf(ctx, out, "TUPLTYPE RGB\n"); + else if (n == 4 && alpha) fz_printf(ctx, out, "TUPLTYPE RGB_ALPHA\n"); + else if (n == 4 && !alpha) fz_printf(ctx, out, "TUPLTYPE CMYK\n"); + else if (n == 5) fz_printf(ctx, out, "TUPLTYPE CMYK_ALPHA\n"); fz_printf(ctx, out, "ENDHDR\n"); } void -fz_write_pam_band(fz_context *ctx, fz_output *out, int w, int h, int n, int alpha, int stride, int band, int bandheight, unsigned char *sp, int savealpha) +fz_write_pam_band(fz_context *ctx, fz_output *out, int w, int h, int n, int stride, int band, int bandheight, unsigned char *sp) { - int y, x; + int y; int start = band * bandheight; int end = start + bandheight; - int sn = n; - int dn = n - alpha; - if (!alpha) - savealpha = 0; - - dn += savealpha; if (!out) return; @@ -163,31 +150,26 @@ fz_write_pam_band(fz_context *ctx, fz_output *out, int w, int h, int n, int alph for (y = 0; y < end; y++) { - x = w; - while (x--) - { - fz_write(ctx, out, sp, dn); - sp += sn; - } - sp += stride - w*n; + fz_write(ctx, out, sp, w * n); + sp += stride; } } void -fz_write_pixmap_as_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap, int savealpha) +fz_write_pixmap_as_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap) { - fz_write_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, savealpha); - fz_write_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->stride, 0, pixmap->h, pixmap->samples, savealpha); + fz_write_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha); + fz_write_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->stride, 0, pixmap->h, pixmap->samples); } void -fz_save_pixmap_as_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) +fz_save_pixmap_as_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_try(ctx) { - fz_write_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, savealpha); - fz_write_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->stride, 0, pixmap->h, pixmap->samples, savealpha); + fz_write_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha); + fz_write_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, pixmap->stride, 0, pixmap->h, pixmap->samples); } fz_always(ctx) fz_drop_output(ctx, out); diff --git a/source/fitz/output-tga.c b/source/fitz/output-tga.c index 3e1112b4..b5e594b6 100644 --- a/source/fitz/output-tga.c +++ b/source/fitz/output-tga.c @@ -24,11 +24,11 @@ static inline void tga_put_pixel(fz_context *ctx, fz_output *out, unsigned char } void -fz_save_pixmap_as_tga(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int savealpha) +fz_save_pixmap_as_tga(fz_context *ctx, fz_pixmap *pixmap, const char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_try(ctx) - fz_write_pixmap_as_tga(ctx, out, pixmap, savealpha); + fz_write_pixmap_as_tga(ctx, out, pixmap); fz_always(ctx) fz_drop_output(ctx, out); fz_catch(ctx) @@ -36,11 +36,11 @@ fz_save_pixmap_as_tga(fz_context *ctx, fz_pixmap *pixmap, const char *filename, } void -fz_write_pixmap_as_tga(fz_context *ctx, fz_output *out, fz_pixmap *pixmap, int savealpha) +fz_write_pixmap_as_tga(fz_context *ctx, fz_output *out, fz_pixmap *pixmap) { unsigned char head[18]; int n = pixmap->n; - int d = savealpha || n == 1 ? n : n - 1; + int d = pixmap->alpha || n == 1 ? n : n - 1; int is_bgr = pixmap->colorspace == fz_device_bgr(ctx); int k; @@ -55,8 +55,8 @@ fz_write_pixmap_as_tga(fz_context *ctx, fz_output *out, fz_pixmap *pixmap, int s head[12] = pixmap->w & 0xFF; head[13] = (pixmap->w >> 8) & 0xFF; head[14] = pixmap->h & 0xFF; head[15] = (pixmap->h >> 8) & 0xFF; head[16] = d * 8; - head[17] = savealpha && n > 1 ? 8 : 0; - if (savealpha && d == 2) + head[17] = pixmap->alpha && n > 1 ? 8 : 0; + if (pixmap->alpha && d == 2) head[16] = 32; fz_write(ctx, out, head, sizeof(head)); diff --git a/source/fitz/pixmap.c b/source/fitz/pixmap.c index 186cd6d0..0631e909 100644 --- a/source/fitz/pixmap.c +++ b/source/fitz/pixmap.c @@ -665,6 +665,9 @@ fz_premultiply_pixmap(fz_context *ctx, fz_pixmap *pix) int k, x, y; int stride = pix->stride - pix->w * pix->n; + if (!pix->alpha) + return; + for (y = 0; y < pix->h; y++) { for (x = 0; x < pix->w; x++) @@ -686,6 +689,9 @@ fz_unmultiply_pixmap(fz_context *ctx, fz_pixmap *pix) int k, x, y; int stride = pix->stride - pix->w * pix->n; + if (!pix->alpha) + return; + for (y = 0; y < pix->h; y++) { for (x = 0; x < pix->w; x++) |