summaryrefslogtreecommitdiff
path: root/source/tools
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2016-10-11 21:10:21 +0100
committerRobin Watts <robin.watts@artifex.com>2016-10-12 12:37:48 +0100
commit80308eae9964e71b66a18f3de6ebcd2ebf0d306b (patch)
tree6aa51f6f53fca59d3c7afcb727f36cf627249e7a /source/tools
parent7de80f5eea6c9475b1db2b4fd905fd69ed3795e7 (diff)
downloadmupdf-80308eae9964e71b66a18f3de6ebcd2ebf0d306b.tar.xz
Regularize band writer interface.
We have various functions that, for different image formats, write a header, then a band, then (sometimes) a trailer. Push them all through a single interface. This change also fixes potential problems caused by the trailer writing being an implicit destructor, which can cause problems in cleanup code if the trailer writing throws an error.
Diffstat (limited to 'source/tools')
-rw-r--r--source/tools/mudraw.c79
-rw-r--r--source/tools/muraster.c57
2 files changed, 52 insertions, 84 deletions
diff --git a/source/tools/mudraw.c b/source/tools/mudraw.c
index 1b5300d9..4708516a 100644
--- a/source/tools/mudraw.c
+++ b/source/tools/mudraw.c
@@ -760,17 +760,11 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in
fz_irect ibounds;
fz_pixmap *pix = NULL;
int w, h;
- fz_png_output_context *poc = NULL;
- fz_ps_output_context *psoc = NULL;
- fz_mono_pcl_output_context *pmcoc = NULL;
- fz_color_pcl_output_context *pccoc = NULL;
+ fz_band_writer *bander = NULL;
fz_bitmap *bit = NULL;
fz_var(pix);
- fz_var(poc);
- fz_var(psoc);
- fz_var(pmcoc);
- fz_var(pccoc);
+ fz_var(bander);
fz_var(bit);
fz_bound_page(ctx, page, &bounds);
@@ -878,24 +872,26 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in
if (output)
{
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);
+ bander = fz_new_pnm_band_writer(ctx, out);
else if (output_format == OUT_PAM)
- fz_write_pam_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha);
+ bander = fz_new_pam_band_writer(ctx, out);
else if (output_format == OUT_PNG)
- poc = fz_write_png_header(ctx, out, pix->w, totalheight, pix->n, pix->alpha);
+ bander = fz_new_png_band_writer(ctx, out);
else if (output_format == OUT_PBM)
- fz_write_pbm_header(ctx, out, pix->w, totalheight);
+ bander = fz_new_pbm_band_writer(ctx, out);
else if (output_format == OUT_PKM)
- fz_write_pkm_header(ctx, out, pix->w, totalheight);
+ bander = fz_new_pkm_band_writer(ctx, out);
else if (output_format == OUT_PS)
- psoc = fz_write_ps_header(ctx, out, pix->w, totalheight, pix->n, pix->xres, pix->yres, ++output_pagenum);
+ bander = fz_new_ps_band_writer(ctx, out);
else if (output_format == OUT_PCL)
{
if (out_cs == CS_MONO)
- pmcoc = fz_write_mono_pcl_header(ctx, out, pix->w, totalheight, pix->xres, pix->yres, ++output_pagenum, NULL);
+ bander = fz_new_mono_pcl_band_writer(ctx, out, NULL);
else
- pccoc = fz_write_color_pcl_header(ctx, out, pix->w, totalheight, pix->n, pix->xres, pix->yres, ++output_pagenum, NULL);
+ bander = fz_new_color_pcl_band_writer(ctx, out, NULL);
}
+ if (bander)
+ fz_write_header(ctx, bander, pix->w, totalheight, pix->n, pix->alpha, pix->xres, pix->yres, ++output_pagenum);
}
for (band = 0; band < bands; band++)
@@ -915,43 +911,14 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in
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 * bandheight, 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 * bandheight, drawheight, pix->samples);
- else if (output_format == OUT_PNG)
- fz_write_png_band(ctx, out, poc, pix->stride, band * bandheight, drawheight, pix->samples);
+ if (bander)
+ fz_write_band(ctx, bander, bit ? bit->stride : pix->stride, band * bandheight, drawheight, bit ? bit->samples : pix->samples);
else if (output_format == OUT_PWG)
fz_write_pixmap_as_pwg(ctx, out, pix, NULL);
- else if (output_format == OUT_PCL)
- {
- if (out_cs == CS_MONO)
- {
- fz_write_mono_pcl_band(ctx, out, pmcoc, bit);
- fz_drop_bitmap(ctx, bit);
- bit = NULL;
- }
- else
- fz_write_color_pcl_band(ctx, out, pccoc, pix->w, totalheight, pix->n, pix->stride, band * bandheight, drawheight, pix->samples);
- }
- else if (output_format == OUT_PS)
- fz_write_ps_band(ctx, out, psoc, pix->w, totalheight, pix->n, pix->stride, band * bandheight, drawheight, pix->samples);
- else if (output_format == OUT_PBM)
- {
- fz_write_pbm_band(ctx, out, bit);
- fz_drop_bitmap(ctx, bit);
- bit = NULL;
- }
- else if (output_format == OUT_PKM)
- {
- fz_write_pkm_band(ctx, out, bit);
- fz_drop_bitmap(ctx, bit);
- bit = NULL;
- }
else if (output_format == OUT_TGA)
- {
fz_write_pixmap_as_tga(ctx, out, pix);
- }
+ fz_drop_bitmap(ctx, bit);
+ bit = NULL;
}
if (num_workers > 0 && band + num_workers < bands)
@@ -982,21 +949,13 @@ static void dodrawpage(fz_context *ctx, fz_page *page, fz_display_list *list, in
/* Any page level trailers go here */
if (output)
{
- if (output_format == OUT_PNG)
- fz_write_png_trailer(ctx, out, poc);
- if (output_format == OUT_PS)
- fz_write_ps_trailer(ctx, out, psoc);
- if (output_format == OUT_PCL)
- {
- if (out_cs == CS_MONO)
- fz_write_mono_pcl_trailer(ctx, out, pmcoc);
- else
- fz_write_color_pcl_trailer(ctx, out, pccoc);
- }
+ if (bander)
+ fz_write_trailer(ctx, bander);
}
}
fz_always(ctx)
{
+ fz_drop_band_writer(ctx, bander);
fz_drop_bitmap(ctx, bit);
bit = NULL;
if (num_workers > 0)
diff --git a/source/tools/muraster.c b/source/tools/muraster.c
index 07c64b72..a2fc7204 100644
--- a/source/tools/muraster.c
+++ b/source/tools/muraster.c
@@ -604,6 +604,12 @@ typedef struct render_details
* will start at the maximum value, and may drop to 0
* if we have problems with memory. */
int num_workers;
+
+ /* The band writer to output the page */
+ fz_band_writer *bander;
+
+ /* Number of components in image */
+ int n;
} render_details;
enum
@@ -784,6 +790,7 @@ static int dodrawpage(fz_context *ctx, int pagenum, fz_cookie *cookie, render_de
pix = fz_new_pixmap_with_bbox(ctx, colorspace, &ibounds, 0);
fz_set_pixmap_resolution(ctx, pix, x_resolution, y_resolution);
}
+ fz_write_header(ctx, render->bander, pix->w, total_height, pix->n, pix->alpha, pix->xres, pix->yres, pagenum);
for (band = 0; band < bands; band++)
{
@@ -818,22 +825,9 @@ static int dodrawpage(fz_context *ctx, int pagenum, fz_cookie *cookie, render_de
{
/* If we get any errors while outputting the bands, retrying won't help. */
errors_are_fatal = 1;
- if (output_format == OUT_PGM || output_format == OUT_PPM)
- fz_write_pnm_band(ctx, out, pix->w, total_height, pix->n, pix->alpha, pix->stride, band_start, draw_height, pix->samples);
- else if (output_format == OUT_PAM)
- fz_write_pam_band(ctx, out, pix->w, total_height, pix->n, pix->alpha, pix->stride, band_start, draw_height, pix->samples);
- else if (output_format == OUT_PBM)
- {
- fz_write_pbm_band(ctx, out, bit);
- fz_drop_bitmap(ctx, bit);
- bit = NULL;
- }
- else if (output_format == OUT_PKM)
- {
- fz_write_pkm_band(ctx, out, bit);
- fz_drop_bitmap(ctx, bit);
- bit = NULL;
- }
+ fz_write_band(ctx, render->bander, bit ? bit->stride : pix->stride, band_start, draw_height, bit ? bit->samples : pix->samples);
+ fz_drop_bitmap(ctx, bit);
+ bit = NULL;
errors_are_fatal = 0;
}
@@ -899,14 +893,7 @@ static int try_render_page(fz_context *ctx, int pagenum, fz_cookie *cookie, int
{
int w = render->ibounds.x1 - render->ibounds.x0;
int h = render->ibounds.y1 - render->ibounds.y0;
- if (output_format == OUT_PGM || output_format == OUT_PPM)
- fz_write_pnm_header(ctx, out, w, h, output_format == OUT_PGM ? 1 : 3, 0);
- else if (output_format == OUT_PAM)
- fz_write_pam_header(ctx, out, w, h, 4, 0);
- else if (output_format == OUT_PBM)
- fz_write_pbm_header(ctx, out, w, h);
- else if (output_format == OUT_PKM)
- fz_write_pkm_header(ctx, out, w, h);
+ fz_write_header(ctx, render->bander, w, h, render->n, 0, 0, 0, 0);
}
fz_catch(ctx)
{
@@ -960,6 +947,7 @@ static int try_render_page(fz_context *ctx, int pagenum, fz_cookie *cookie, int
fz_drop_page(ctx, render->page);
fz_drop_display_list(ctx, render->list);
+ fz_drop_band_writer(ctx, render->bander);
if (showtime)
{
@@ -1175,6 +1163,27 @@ initialise_banding(fz_context *ctx, render_details *render, int color)
render->band_height_multiple = reps;
render->bands_rendered = 0;
+
+ if (output_format == OUT_PGM || output_format == OUT_PPM)
+ {
+ render->bander = fz_new_pnm_band_writer(ctx, out);
+ render->n = OUT_PGM ? 1 : 3;
+ }
+ else if (output_format == OUT_PAM)
+ {
+ render->bander = fz_new_pam_band_writer(ctx, out);
+ render->n = 4;
+ }
+ else if (output_format == OUT_PBM)
+ {
+ render->bander = fz_new_pbm_band_writer(ctx, out);
+ render->n = 1;
+ }
+ else if (output_format == OUT_PKM)
+ {
+ render->bander = fz_new_pkm_band_writer(ctx, out);
+ render->n = 4;
+ }
}
static void drawpage(fz_context *ctx, fz_document *doc, int pagenum)