diff options
Diffstat (limited to 'source/fitz/output-pwg.c')
-rw-r--r-- | source/fitz/output-pwg.c | 175 |
1 files changed, 85 insertions, 90 deletions
diff --git a/source/fitz/output-pwg.c b/source/fitz/output-pwg.c index c6d1a0b5..4af67c25 100644 --- a/source/fitz/output-pwg.c +++ b/source/fitz/output-pwg.c @@ -1,103 +1,100 @@ #include "mupdf/fitz.h" void -fz_output_pwg_file_header(fz_output *out) +fz_output_pwg_file_header(fz_context *ctx, fz_output *out) { static const unsigned char pwgsig[4] = { 'R', 'a', 'S', '2' }; /* Sync word */ - fz_write(out, pwgsig, 4); + fz_write(ctx, out, pwgsig, 4); } static void -output_header(fz_output *out, const fz_pwg_options *pwg, int xres, int yres, int w, int h, int bpp) +output_header(fz_context *ctx, fz_output *out, const fz_pwg_options *pwg, int xres, int yres, int w, int h, int bpp) { static const char zero[64] = { 0 }; int i; /* Page Header: */ - fz_write(out, pwg ? pwg->media_class : zero, 64); - fz_write(out, pwg ? pwg->media_color : zero, 64); - fz_write(out, pwg ? pwg->media_type : zero, 64); - fz_write(out, pwg ? pwg->output_type : zero, 64); - fz_write_int32be(out, pwg ? pwg->advance_distance : 0); - fz_write_int32be(out, pwg ? pwg->advance_media : 0); - fz_write_int32be(out, pwg ? pwg->collate : 0); - fz_write_int32be(out, pwg ? pwg->cut_media : 0); - fz_write_int32be(out, pwg ? pwg->duplex : 0); - fz_write_int32be(out, xres); - fz_write_int32be(out, yres); + fz_write(ctx, out, pwg ? pwg->media_class : zero, 64); + fz_write(ctx, out, pwg ? pwg->media_color : zero, 64); + fz_write(ctx, out, pwg ? pwg->media_type : zero, 64); + fz_write(ctx, out, pwg ? pwg->output_type : zero, 64); + fz_write_int32be(ctx, out, pwg ? pwg->advance_distance : 0); + fz_write_int32be(ctx, out, pwg ? pwg->advance_media : 0); + fz_write_int32be(ctx, out, pwg ? pwg->collate : 0); + fz_write_int32be(ctx, out, pwg ? pwg->cut_media : 0); + fz_write_int32be(ctx, out, pwg ? pwg->duplex : 0); + fz_write_int32be(ctx, out, xres); + fz_write_int32be(ctx, out, yres); /* CUPS format says that 284->300 are supposed to be the bbox of the * page in points. PWG says 'Reserved'. */ for (i=284; i < 300; i += 4) - fz_write(out, zero, 4); - fz_write_int32be(out, pwg ? pwg->insert_sheet : 0); - fz_write_int32be(out, pwg ? pwg->jog : 0); - fz_write_int32be(out, pwg ? pwg->leading_edge : 0); + fz_write(ctx, out, zero, 4); + fz_write_int32be(ctx, out, pwg ? pwg->insert_sheet : 0); + fz_write_int32be(ctx, out, pwg ? pwg->jog : 0); + fz_write_int32be(ctx, out, pwg ? pwg->leading_edge : 0); /* CUPS format says that 312->320 are supposed to be the margins of * the lower left hand edge of page in points. PWG says 'Reserved'. */ for (i=312; i < 320; i += 4) - fz_write(out, zero, 4); - fz_write_int32be(out, pwg ? pwg->manual_feed : 0); - fz_write_int32be(out, pwg ? pwg->media_position : 0); - fz_write_int32be(out, pwg ? pwg->media_weight : 0); - fz_write_int32be(out, pwg ? pwg->mirror_print : 0); - fz_write_int32be(out, pwg ? pwg->negative_print : 0); - fz_write_int32be(out, pwg ? pwg->num_copies : 0); - fz_write_int32be(out, pwg ? pwg->orientation : 0); - fz_write_int32be(out, pwg ? pwg->output_face_up : 0); - fz_write_int32be(out, w * 72/ xres); /* Page size in points */ - fz_write_int32be(out, h * 72/ yres); - fz_write_int32be(out, pwg ? pwg->separations : 0); - fz_write_int32be(out, pwg ? pwg->tray_switch : 0); - fz_write_int32be(out, pwg ? pwg->tumble : 0); - fz_write_int32be(out, w); /* Page image in pixels */ - fz_write_int32be(out, h); - fz_write_int32be(out, pwg ? pwg->media_type_num : 0); - fz_write_int32be(out, bpp < 8 ? 1 : 8); /* Bits per color */ - fz_write_int32be(out, bpp); /* Bits per pixel */ - fz_write_int32be(out, (w * bpp + 7)/8); /* Bytes per line */ - fz_write_int32be(out, 0); /* Chunky pixels */ + fz_write(ctx, out, zero, 4); + fz_write_int32be(ctx, out, pwg ? pwg->manual_feed : 0); + fz_write_int32be(ctx, out, pwg ? pwg->media_position : 0); + fz_write_int32be(ctx, out, pwg ? pwg->media_weight : 0); + fz_write_int32be(ctx, out, pwg ? pwg->mirror_print : 0); + fz_write_int32be(ctx, out, pwg ? pwg->negative_print : 0); + fz_write_int32be(ctx, out, pwg ? pwg->num_copies : 0); + fz_write_int32be(ctx, out, pwg ? pwg->orientation : 0); + fz_write_int32be(ctx, out, pwg ? pwg->output_face_up : 0); + fz_write_int32be(ctx, out, w * 72/ xres); /* Page size in points */ + fz_write_int32be(ctx, out, h * 72/ yres); + fz_write_int32be(ctx, out, pwg ? pwg->separations : 0); + fz_write_int32be(ctx, out, pwg ? pwg->tray_switch : 0); + fz_write_int32be(ctx, out, pwg ? pwg->tumble : 0); + fz_write_int32be(ctx, out, w); /* Page image in pixels */ + fz_write_int32be(ctx, out, h); + fz_write_int32be(ctx, out, pwg ? pwg->media_type_num : 0); + fz_write_int32be(ctx, out, bpp < 8 ? 1 : 8); /* Bits per color */ + fz_write_int32be(ctx, out, bpp); /* Bits per pixel */ + fz_write_int32be(ctx, out, (w * bpp + 7)/8); /* Bytes per line */ + fz_write_int32be(ctx, out, 0); /* Chunky pixels */ switch (bpp) { - case 1: fz_write_int32be(out, 3); /* Black */ break; - case 8: fz_write_int32be(out, 18); /* Sgray */ break; - case 24: fz_write_int32be(out, 19); /* Srgb */ break; - case 32: fz_write_int32be(out, 6); /* Cmyk */ break; - default: fz_throw(out->ctx, FZ_ERROR_GENERIC, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg"); + case 1: fz_write_int32be(ctx, out, 3); /* Black */ break; + case 8: fz_write_int32be(ctx, out, 18); /* Sgray */ break; + case 24: fz_write_int32be(ctx, out, 19); /* Srgb */ break; + case 32: fz_write_int32be(ctx, out, 6); /* Cmyk */ break; + default: fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg"); } - fz_write_int32be(out, pwg ? pwg->compression : 0); - fz_write_int32be(out, pwg ? pwg->row_count : 0); - fz_write_int32be(out, pwg ? pwg->row_feed : 0); - fz_write_int32be(out, pwg ? pwg->row_step : 0); - fz_write_int32be(out, bpp <= 8 ? 1 : 3); /* Num Colors */ + fz_write_int32be(ctx, out, pwg ? pwg->compression : 0); + fz_write_int32be(ctx, out, pwg ? pwg->row_count : 0); + fz_write_int32be(ctx, out, pwg ? pwg->row_feed : 0); + fz_write_int32be(ctx, out, pwg ? pwg->row_step : 0); + fz_write_int32be(ctx, out, bpp <= 8 ? 1 : 3); /* Num Colors */ for (i=424; i < 452; i += 4) - fz_write(out, zero, 4); - fz_write_int32be(out, 1); /* TotalPageCount */ - fz_write_int32be(out, 1); /* CrossFeedTransform */ - fz_write_int32be(out, 1); /* FeedTransform */ - fz_write_int32be(out, 0); /* ImageBoxLeft */ - fz_write_int32be(out, 0); /* ImageBoxTop */ - fz_write_int32be(out, w); /* ImageBoxRight */ - fz_write_int32be(out, h); /* ImageBoxBottom */ + fz_write(ctx, out, zero, 4); + fz_write_int32be(ctx, out, 1); /* TotalPageCount */ + fz_write_int32be(ctx, out, 1); /* CrossFeedTransform */ + fz_write_int32be(ctx, out, 1); /* FeedTransform */ + fz_write_int32be(ctx, out, 0); /* ImageBoxLeft */ + fz_write_int32be(ctx, out, 0); /* ImageBoxTop */ + fz_write_int32be(ctx, out, w); /* ImageBoxRight */ + fz_write_int32be(ctx, out, h); /* ImageBoxBottom */ for (i=480; i < 1668; i += 4) - fz_write(out, zero, 4); - fz_write(out, pwg ? pwg->rendering_intent : zero, 64); - fz_write(out, pwg ? pwg->page_size_name : zero, 64); + fz_write(ctx, out, zero, 4); + fz_write(ctx, out, pwg ? pwg->rendering_intent : zero, 64); + fz_write(ctx, out, pwg ? pwg->page_size_name : zero, 64); } void -fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg) +fz_output_pwg_page(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg) { unsigned char *sp; int y, x, sn, dn, ss; - fz_context *ctx; if (!out || !pixmap) return; - ctx = out->ctx; - if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4 && pixmap->n != 5) fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale, rgb or cmyk to write as pwg"); @@ -106,7 +103,7 @@ fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options if (dn > 1) dn--; - output_header(out, pwg, pixmap->xres, pixmap->yres, pixmap->w, pixmap->h, dn*8); + output_header(ctx, out, pwg, pixmap->xres, pixmap->yres, pixmap->w, pixmap->h, dn*8); /* Now output the actual bitmap, using a packbits like compression */ sp = pixmap->samples; @@ -124,7 +121,7 @@ fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options if (memcmp(sp, sp + yrep * ss, ss) != 0) break; } - fz_write_byte(out, yrep-1); + fz_write_byte(ctx, out, yrep-1); /* Encode the line */ x = 0; @@ -152,18 +149,18 @@ fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options if (memcmp(sp, sp + xrep*sn, sn) != 0) break; } - fz_write_byte(out, xrep-1); - fz_write(out, sp, dn); + fz_write_byte(ctx, out, xrep-1); + fz_write(ctx, out, sp, dn); sp += sn*xrep; x += xrep; } else { - fz_write_byte(out, 257-d); + fz_write_byte(ctx, out, 257-d); x += d; while (d > 0) { - fz_write(out, sp, dn); + fz_write(ctx, out, sp, dn); sp += sn; d--; } @@ -177,7 +174,7 @@ fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options } void -fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg) +fz_output_pwg_bitmap_page(fz_context *ctx, fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg) { unsigned char *sp; int y, x, ss; @@ -186,7 +183,7 @@ fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_ if (!out || !bitmap) return; - output_header(out, pwg, bitmap->xres, bitmap->yres, bitmap->w, bitmap->h, 1); + output_header(ctx, out, pwg, bitmap->xres, bitmap->yres, bitmap->w, bitmap->h, 1); /* Now output the actual bitmap, using a packbits like compression */ sp = bitmap->samples; @@ -205,7 +202,7 @@ fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_ if (memcmp(sp, sp + yrep * ss, byte_width) != 0) break; } - fz_write_byte(out, yrep-1); + fz_write_byte(ctx, out, yrep-1); /* Encode the line */ x = 0; @@ -233,15 +230,15 @@ fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_ if (sp[0] != sp[xrep]) break; } - fz_write_byte(out, xrep-1); - fz_write(out, sp, 1); + fz_write_byte(ctx, out, xrep-1); + fz_write(ctx, out, sp, 1); sp += xrep; x += xrep; } else { - fz_write_byte(out, 257-d); - fz_write(out, sp, d); + fz_write_byte(ctx, out, 257-d); + fz_write(ctx, out, sp, d); sp += d; x += d; } @@ -254,10 +251,10 @@ fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_ } void -fz_output_pwg(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg) +fz_output_pwg(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg) { - fz_output_pwg_file_header(out); - fz_output_pwg_page(out, pixmap, pwg); + fz_output_pwg_file_header(ctx, out); + fz_output_pwg_page(ctx, out, pixmap, pwg); } void @@ -276,15 +273,14 @@ fz_write_pwg(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, con fz_try(ctx) { - out = fz_new_output_with_file(ctx, fp); + out = fz_new_output_with_file(ctx, fp, 1); if (!append) - fz_output_pwg_file_header(out); - fz_output_pwg_page(out, pixmap, pwg); + fz_output_pwg_file_header(ctx, out); + fz_output_pwg_page(ctx, out, pixmap, pwg); } fz_always(ctx) { - fz_close_output(out); - fclose(fp); + fz_drop_output(ctx, out); } fz_catch(ctx) { @@ -308,15 +304,14 @@ fz_write_pwg_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int appe fz_try(ctx) { - out = fz_new_output_with_file(ctx, fp); + out = fz_new_output_with_file(ctx, fp, 1); if (!append) - fz_output_pwg_file_header(out); - fz_output_pwg_bitmap_page(out, bitmap, pwg); + fz_output_pwg_file_header(ctx, out); + fz_output_pwg_bitmap_page(ctx, out, bitmap, pwg); } fz_always(ctx) { - fz_close_output(out); - fclose(fp); + fz_drop_output(ctx, out); } fz_catch(ctx) { |