diff options
-rw-r--r-- | include/mupdf/fitz/writer.h | 2 | ||||
-rw-r--r-- | source/fitz/draw-device.c | 2 | ||||
-rw-r--r-- | source/fitz/output-cbz.c | 4 | ||||
-rw-r--r-- | source/fitz/output-png.c | 68 | ||||
-rw-r--r-- | source/fitz/writer.c | 4 | ||||
-rw-r--r-- | source/pdf/pdf-write.c | 7 | ||||
-rw-r--r-- | source/tools/muconvert.c | 17 |
7 files changed, 88 insertions, 16 deletions
diff --git a/include/mupdf/fitz/writer.h b/include/mupdf/fitz/writer.h index 064b8465..98b0824a 100644 --- a/include/mupdf/fitz/writer.h +++ b/include/mupdf/fitz/writer.h @@ -26,9 +26,11 @@ void fz_close_document_writer(fz_context *ctx, fz_document_writer *wri); void fz_drop_document_writer(fz_context *ctx, fz_document_writer *wri); fz_document_writer *fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options); +fz_document_writer *fz_new_png_writer(fz_context *ctx, const char *path, const char *options); fz_document_writer *fz_new_pdf_writer(fz_context *ctx, const char *path, const char *options); extern const char *fz_cbz_write_options_usage; +extern const char *fz_png_write_options_usage; extern const char *fz_pdf_write_options_usage; #endif diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c index f725fd41..126f715d 100644 --- a/source/fitz/draw-device.c +++ b/source/fitz/draw-device.c @@ -9,7 +9,7 @@ const char *fz_draw_options_usage = "\theight=N: render pages to fit N pixels tall (ignore resolution option)\n" "\tcolorspace=(gray|rgb|cmyk): render using specified colorspace\n" "\talpha: render pages with alpha channel and transparent background\n" - ; + "\n"; static int opteq(const char *a, const char *b) { diff --git a/source/fitz/output-cbz.c b/source/fitz/output-cbz.c index ebfded4c..ba7ca196 100644 --- a/source/fitz/output-cbz.c +++ b/source/fitz/output-cbz.c @@ -7,10 +7,10 @@ typedef struct fz_cbz_writer_s fz_cbz_writer; struct fz_cbz_writer_s { fz_document_writer super; - fz_zip_writer *zip; fz_draw_options options; fz_pixmap *pixmap; int count; + fz_zip_writer *zip; }; const char *fz_cbz_write_options_usage = ""; @@ -72,7 +72,7 @@ fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options) fz_try(ctx) { fz_parse_draw_options(ctx, &wri->options, options); - wri->zip = fz_new_zip_writer(ctx, path); + wri->zip = fz_new_zip_writer(ctx, path ? path : "out.cbz"); } fz_catch(ctx) { diff --git a/source/fitz/output-png.c b/source/fitz/output-png.c index 9fe58ef0..2f19611c 100644 --- a/source/fitz/output-png.c +++ b/source/fitz/output-png.c @@ -292,3 +292,71 @@ fz_new_buffer_from_pixmap_as_png(fz_context *ctx, fz_pixmap *pix) { return png_from_pixmap(ctx, pix, 0); } + +/* PNG output writer */ + +typedef struct fz_png_writer_s fz_png_writer; + +struct fz_png_writer_s +{ + fz_document_writer super; + fz_draw_options options; + fz_pixmap *pixmap; + int count; + char *path; +}; + +const char *fz_png_write_options_usage = ""; + +static fz_device * +png_begin_page(fz_context *ctx, fz_document_writer *wri_, const fz_rect *mediabox, fz_matrix *transform) +{ + fz_png_writer *wri = (fz_png_writer*)wri_; + return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, transform, &wri->pixmap); +} + +static void +png_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) +{ + fz_png_writer *wri = (fz_png_writer*)wri_; + char path[PATH_MAX]; + + wri->count += 1; + + fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count); + fz_save_pixmap_as_png(ctx, wri->pixmap, path); + fz_drop_pixmap(ctx, wri->pixmap); + wri->pixmap = NULL; +} + +static void +png_close(fz_context *ctx, fz_document_writer *wri_) +{ + fz_png_writer *wri = (fz_png_writer*)wri_; + fz_drop_pixmap(ctx, wri->pixmap); + fz_free(ctx, wri->path); +} + +fz_document_writer * +fz_new_png_writer(fz_context *ctx, const char *path, const char *options) +{ + fz_png_writer *wri; + + wri = fz_malloc_struct(ctx, fz_png_writer); + wri->super.begin_page = png_begin_page; + wri->super.end_page = png_end_page; + wri->super.close = png_close; + + fz_try(ctx) + { + fz_parse_draw_options(ctx, &wri->options, options); + wri->path = fz_strdup(ctx, path ? path : "out-%04d.png"); + } + fz_catch(ctx) + { + fz_free(ctx, wri); + fz_rethrow(ctx); + } + + return (fz_document_writer*)wri; +} diff --git a/source/fitz/writer.c b/source/fitz/writer.c index 2a344e52..927ddffe 100644 --- a/source/fitz/writer.c +++ b/source/fitz/writer.c @@ -54,12 +54,14 @@ fz_new_document_writer(fz_context *ctx, const char *path, const char *format, co if (!fz_strcasecmp(format, "cbz")) return fz_new_cbz_writer(ctx, path, options); + if (!fz_strcasecmp(format, "png")) + return fz_new_png_writer(ctx, path, options); #if FZ_ENABLE_PDF if (!fz_strcasecmp(format, "pdf")) return fz_new_pdf_writer(ctx, path, options); #endif - fz_throw(ctx, FZ_ERROR_GENERIC, "unknown document format: %s", format); + fz_throw(ctx, FZ_ERROR_GENERIC, "unknown output document format: %s", format); } void diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c index b160002e..b6e37d39 100644 --- a/source/pdf/pdf-write.c +++ b/source/pdf/pdf-write.c @@ -2766,7 +2766,7 @@ const char *fz_pdf_write_options_usage = "\tgarbage: garbage collect unused objects\n" "\tor garbage=compact: ... and compact cross reference table\n" "\tor garbage=deduplicate: ... and remove duplicate objects\n" - ; + "\n"; pdf_write_options * pdf_parse_write_options(fz_context *ctx, pdf_write_options *opts, const char *args) @@ -3264,7 +3264,8 @@ fz_new_pdf_writer(fz_context *ctx, const char *path, const char *options) fz_try(ctx) { - wri->filename = fz_strdup(ctx, path); + pdf_parse_write_options(ctx, &wri->opts, options); + wri->filename = fz_strdup(ctx, path ? path : "out.pdf"); wri->pdf = pdf_create_document(ctx); } fz_catch(ctx) @@ -3275,7 +3276,5 @@ fz_new_pdf_writer(fz_context *ctx, const char *path, const char *options) fz_rethrow(ctx); } - pdf_parse_write_options(ctx, &wri->opts, options); - return (fz_document_writer*)wri; } diff --git a/source/tools/muconvert.c b/source/tools/muconvert.c index 8d655bd3..3fd97cfd 100644 --- a/source/tools/muconvert.c +++ b/source/tools/muconvert.c @@ -13,7 +13,7 @@ static float layout_em = 12; static char *layout_css = NULL; /* output options */ -static const char *output = "out.pdf"; +static const char *output = NULL; static const char *format = NULL; static const char *options = ""; @@ -37,16 +37,17 @@ static void usage(void) "\n" "\t-o -\toutput file name (%%d for page number)\n" "\t-F -\toutput format (default inferred from output file name)\n" - "\t\tcbz, pdf\n" + "\t\tcbz, pdf, png\n" "\t-O -\tcomma separated list of options for output format\n" "\n" - "\tpages\tcomma separated list of page numbers and ranges, where N is the last page\n" + "\tpages\tcomma separated list of page ranges (N=last page)\n" "\n" ); - fprintf(stderr, "%s", fz_draw_options_usage); - fprintf(stderr, "%s", fz_cbz_write_options_usage); + fputs(fz_draw_options_usage, stderr); + fputs(fz_cbz_write_options_usage, stderr); + fputs(fz_png_write_options_usage, stderr); #if FZ_ENABLE_PDF - fprintf(stderr, "%s", fz_pdf_write_options_usage); + fputs(fz_pdf_write_options_usage, stderr); #endif exit(1); } @@ -104,7 +105,7 @@ int muconvert_main(int argc, char **argv) } } - if (fz_optind == argc) + if (fz_optind == argc || (!format && !output)) usage(); /* Create a context to hold the exception stack and various caches. */ @@ -140,7 +141,7 @@ int muconvert_main(int argc, char **argv) out = fz_new_document_writer(ctx, output, format, options); fz_catch(ctx) { - fprintf(stderr, "cannot open document: %s\n", fz_caught_message(ctx)); + fprintf(stderr, "cannot create document: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } |