From 5825ef8805ca8439ac576fb0071f9ddbdf8f6281 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 16 Jun 2016 11:21:36 +0200 Subject: Drop save_alpha argument from image writing functions. --- source/fitz/output-png.c | 46 ++++++++++++++-------------------------- source/fitz/output-pnm.c | 54 ++++++++++++++++------------------------------- source/fitz/output-tga.c | 12 +++++------ source/fitz/pixmap.c | 6 ++++++ source/tools/mudraw.c | 21 ++++++++---------- source/tools/murun.c | 5 ++--- source/tools/pdfextract.c | 6 +++--- 7 files changed, 60 insertions(+), 90 deletions(-) (limited to 'source') 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 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++) diff --git a/source/tools/mudraw.c b/source/tools/mudraw.c index e1259db2..2efabcd7 100644 --- a/source/tools/mudraw.c +++ b/source/tools/mudraw.c @@ -314,7 +314,6 @@ typedef struct worker_t { fz_context *ctx; int num; int band; /* -1 to shutdown, or band to render */ - int savealpha; fz_display_list *list; fz_matrix ctm; fz_rect tbounds; @@ -533,7 +532,7 @@ file_level_trailers(fz_context *ctx) fz_drop_stext_sheet(ctx, sheet); } -static void drawband(fz_context *ctx, int savealpha, fz_page *page, fz_display_list *list, const fz_matrix *ctm, const fz_rect *tbounds, fz_cookie *cookie, int band, fz_pixmap *pix, fz_bitmap **bit) +static void drawband(fz_context *ctx, fz_page *page, fz_display_list *list, const fz_matrix *ctm, const fz_rect *tbounds, fz_cookie *cookie, int band, fz_pixmap *pix, fz_bitmap **bit) { fz_device *dev = NULL; @@ -541,7 +540,7 @@ static void drawband(fz_context *ctx, int savealpha, fz_page *page, fz_display_l fz_try(ctx) { - if (savealpha) + if (pix->alpha) fz_clear_pixmap(ctx, pix); else fz_clear_pixmap_with_value(ctx, pix, 255); @@ -563,7 +562,7 @@ static void drawband(fz_context *ctx, int savealpha, fz_page *page, fz_display_l if (gamma_value != 1) fz_gamma_pixmap(ctx, pix, gamma_value); - if (savealpha) + if (pix->alpha) fz_unmultiply_pixmap(ctx, pix); if ((output_format == OUT_PCL && out_cs == CS_MONO) || (output_format == OUT_PBM) || (output_format == OUT_PKM)) @@ -827,7 +826,6 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in fz_try(ctx) { - int savealpha = (out_cs == CS_GRAY_ALPHA || out_cs == CS_RGB_ALPHA || out_cs == CS_CMYK_ALPHA); fz_irect band_ibounds = ibounds; int band, bands = 1; int totalheight = ibounds.y1 - ibounds.y0; @@ -850,7 +848,6 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in for (band = 0; band < fz_mini(num_workers, bands); band++) { workers[band].band = band; - workers[band].savealpha = savealpha; /* Constant on a page */ workers[band].ctm = ctm; workers[band].tbounds = tbounds; memset(&workers[band].cookie, 0, sizeof(fz_cookie)); @@ -875,9 +872,9 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in if (output_format == OUT_PGM || output_format == OUT_PPM || output_format == OUT_PNM) fz_write_pnm_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha); else if (output_format == OUT_PAM) - fz_write_pam_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha, savealpha); + fz_write_pam_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha); else if (output_format == OUT_PNG) - poc = fz_write_png_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha, savealpha); + poc = fz_write_png_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha); else if (output_format == OUT_PBM) fz_write_pbm_header(ctx, out, pix->w, totalheight); else if (output_format == OUT_PKM) @@ -906,14 +903,14 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in cookie->errors += w->cookie.errors; } else - drawband(ctx, savealpha, page, list, &ctm, &tbounds, cookie, band, pix, &bit); + drawband(ctx, page, list, &ctm, &tbounds, cookie, band, pix, &bit); if (output) { if (output_format == OUT_PGM || output_format == OUT_PPM || output_format == OUT_PNM) fz_write_pnm_band(ctx, out, pix->w, totalheight, pix->n, pix->alpha, pix->stride, band, drawheight, pix->samples); else if (output_format == OUT_PAM) - fz_write_pam_band(ctx, out, pix->w, totalheight, pix->n, pix->alpha, pix->stride, band, drawheight, pix->samples, savealpha); + fz_write_pam_band(ctx, out, pix->w, totalheight, pix->n, pix->stride, band, drawheight, pix->samples); else if (output_format == OUT_PNG) fz_write_png_band(ctx, out, poc, pix->stride, band, drawheight, pix->samples); else if (output_format == OUT_PWG) @@ -945,7 +942,7 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in } else if (output_format == OUT_TGA) { - fz_write_pixmap_as_tga(ctx, out, pix, savealpha); + fz_write_pixmap_as_tga(ctx, out, pix); } } @@ -1306,7 +1303,7 @@ static THREAD_RETURN_TYPE worker_thread(void *arg) SEMAPHORE_WAIT(me->start); DEBUG_THREADS(("Worker %d woken for band %d\n", me->num, me->band)); if (me->band >= 0) - drawband(me->ctx, me->savealpha, NULL, me->list, &me->ctm, &me->tbounds, &me->cookie, me->band, me->pix, &me->bit); + drawband(me->ctx, NULL, me->list, &me->ctm, &me->tbounds, &me->cookie, me->band, me->pix, &me->bit); DEBUG_THREADS(("Worker %d completed band %d\n", me->num, me->band)); SEMAPHORE_TRIGGER(me->stop); } diff --git a/source/tools/murun.c b/source/tools/murun.c index 61e43e17..0b36e727 100644 --- a/source/tools/murun.c +++ b/source/tools/murun.c @@ -1585,10 +1585,9 @@ static void ffi_Pixmap_saveAsPNG(js_State *J) fz_context *ctx = js_getcontext(J); fz_pixmap *pixmap = js_touserdata(J, 0, "fz_pixmap"); const char *filename = js_tostring(J, 1); - int savealpha = js_toboolean(J, 2); fz_try(ctx) - fz_save_pixmap_as_png(ctx, pixmap, filename, savealpha); + fz_save_pixmap_as_png(ctx, pixmap, filename); fz_catch(ctx) rethrow(J); } @@ -3243,7 +3242,7 @@ int murun_main(int argc, char **argv) // Pixmap.gamma // Pixmap.scale() - jsB_propfun(J, "Pixmap.saveAsPNG", ffi_Pixmap_saveAsPNG, 2); + jsB_propfun(J, "Pixmap.saveAsPNG", ffi_Pixmap_saveAsPNG, 1); // Pixmap.saveAsPNM, PAM, TGA, PWG, PCL // Pixmap.halftone() -> Bitmap diff --git a/source/tools/pdfextract.c b/source/tools/pdfextract.c index b784fda1..80c25c2b 100644 --- a/source/tools/pdfextract.c +++ b/source/tools/pdfextract.c @@ -39,7 +39,7 @@ static void writepixmap(fz_context *ctx, fz_pixmap *pix, char *file, int rgb) if (rgb && pix->colorspace && pix->colorspace != fz_device_rgb(ctx)) { fz_irect bbox; - converted = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_pixmap_bbox(ctx, pix, &bbox), 1); + converted = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_pixmap_bbox(ctx, pix, &bbox), pix->alpha); fz_convert_pixmap(ctx, converted, pix); pix = converted; } @@ -48,13 +48,13 @@ static void writepixmap(fz_context *ctx, fz_pixmap *pix, char *file, int rgb) { snprintf(buf, sizeof(buf), "%s.png", file); printf("extracting image %s\n", buf); - fz_save_pixmap_as_png(ctx, pix, buf, 0); + fz_save_pixmap_as_png(ctx, pix, buf); } else { snprintf(buf, sizeof(buf), "%s.pam", file); printf("extracting image %s\n", buf); - fz_save_pixmap_as_pam(ctx, pix, buf, 0); + fz_save_pixmap_as_pam(ctx, pix, buf); } fz_drop_pixmap(ctx, converted); -- cgit v1.2.3