Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/tools/pdfrecolor.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2025 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 /* PDF recoloring tool. */ | |
| 24 | |
| 25 #include "mupdf/fitz.h" | |
| 26 #include "mupdf/pdf.h" | |
| 27 | |
| 28 #include <stdio.h> | |
| 29 #include <stdlib.h> | |
| 30 #include <string.h> | |
| 31 | |
| 32 | |
| 33 static int | |
| 34 usage(void) | |
| 35 { | |
| 36 fprintf(stderr, "usage: mutool recolor [options] <input filename>\n"); | |
| 37 fprintf(stderr, "\t-c -\tOutput colorspace (gray(default), rgb, cmyk)\n"); | |
| 38 fprintf(stderr, "\t-r\tRemove OutputIntent(s)\n"); | |
| 39 fprintf(stderr, "\t-o -\tOutput file\n"); | |
| 40 return 1; | |
| 41 } | |
| 42 | |
| 43 int pdfrecolor_main(int argc, char **argv) | |
| 44 { | |
| 45 fz_context *ctx = NULL; | |
| 46 pdf_document *pdf = NULL; | |
| 47 fz_document *doc = NULL; | |
| 48 pdf_write_options opts = pdf_default_write_options; | |
| 49 pdf_recolor_options ropts = { 0 }; | |
| 50 int n, i, c; | |
| 51 char *infile = NULL; | |
| 52 char *outputfile = "out.pdf"; | |
| 53 int code = EXIT_SUCCESS; | |
| 54 const char *colorspace = NULL; | |
| 55 int remove_oi = 0; | |
| 56 | |
| 57 while ((c = fz_getopt(argc, argv, "c:o:r")) != -1) | |
| 58 { | |
| 59 switch (c) | |
| 60 { | |
| 61 default: return usage(); | |
| 62 | |
| 63 // color convert | |
| 64 case 'c': colorspace = fz_optarg; break; | |
| 65 case 'o': outputfile = fz_optpath(fz_optarg); break; | |
| 66 case 'r': remove_oi = 1; break; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 if (fz_optind == argc || !outputfile) | |
| 71 return usage(); | |
| 72 | |
| 73 infile = argv[fz_optind]; | |
| 74 | |
| 75 if (colorspace == NULL || !strcmp(colorspace, "gray")) | |
| 76 ropts.num_comp = 1; | |
| 77 else if (!strcmp(colorspace, "rgb")) | |
| 78 ropts.num_comp = 3; | |
| 79 else if (!strcmp(colorspace, "cmyk")) | |
| 80 ropts.num_comp = 4; | |
| 81 else | |
| 82 { | |
| 83 fprintf(stderr, "Unknown colorspace\n"); | |
| 84 return usage(); | |
| 85 } | |
| 86 | |
| 87 /* Set up the options for the file saving. */ | |
| 88 #if 1 | |
| 89 opts.do_compress = 1; | |
| 90 opts.do_compress_images = 1; | |
| 91 opts.do_compress_fonts = 1; | |
| 92 opts.do_garbage = 3; | |
| 93 opts.do_use_objstms = 1; | |
| 94 #else | |
| 95 opts.do_compress = 0; | |
| 96 opts.do_pretty = 1; | |
| 97 opts.do_compress = 0; | |
| 98 opts.do_compress_images = 1; | |
| 99 opts.do_compress_fonts = 0; | |
| 100 opts.do_garbage = 0; | |
| 101 opts.do_clean = 1; | |
| 102 #endif | |
| 103 | |
| 104 /* Create a MuPDF library context. */ | |
| 105 ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); | |
| 106 if (!ctx) | |
| 107 { | |
| 108 fprintf(stderr, "Could not create global context.\n"); | |
| 109 return EXIT_FAILURE; | |
| 110 } | |
| 111 | |
| 112 /* Register the document handlers (only really need PDF, but this is | |
| 113 * the simplest way. */ | |
| 114 fz_register_document_handlers(ctx); | |
| 115 | |
| 116 fz_try(ctx) | |
| 117 { | |
| 118 /* Load the input document. */ | |
| 119 doc = fz_open_document(ctx, infile); | |
| 120 | |
| 121 /* Get a PDF specific pointer, and count the pages. */ | |
| 122 pdf = pdf_document_from_fz_document(ctx, doc); | |
| 123 n = fz_count_pages(ctx, doc); | |
| 124 | |
| 125 for (i = 0; i < n; i++) | |
| 126 pdf_recolor_page(ctx, pdf, i, &ropts); | |
| 127 | |
| 128 if (remove_oi) | |
| 129 pdf_remove_output_intents(ctx, pdf); | |
| 130 | |
| 131 pdf_save_document(ctx, pdf, outputfile, &opts); | |
| 132 } | |
| 133 fz_always(ctx) | |
| 134 { | |
| 135 fz_drop_document(ctx, doc); | |
| 136 } | |
| 137 fz_catch(ctx) | |
| 138 { | |
| 139 fz_report_error(ctx); | |
| 140 code = EXIT_FAILURE; | |
| 141 } | |
| 142 fz_drop_context(ctx); | |
| 143 | |
| 144 return code; | |
| 145 } |
