pdfrecolor.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2004-2024 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. /* PDF recoloring tool. */
  23. #include "mupdf/fitz.h"
  24. #include "mupdf/pdf.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. static int
  29. usage(void)
  30. {
  31. fprintf(stderr, "usage: mutool recolor [options] <input filename>\n");
  32. fprintf(stderr, "\t-c -\tOutput colorspace (gray(default), rgb, cmyk)\n");
  33. fprintf(stderr, "\t-r\tRemove OutputIntent(s)\n");
  34. fprintf(stderr, "\t-o -\tOutput file\n");
  35. return 1;
  36. }
  37. int pdfrecolor_main(int argc, char **argv)
  38. {
  39. fz_context *ctx = NULL;
  40. pdf_document *pdf = NULL;
  41. fz_document *doc = NULL;
  42. pdf_write_options opts = pdf_default_write_options;
  43. pdf_recolor_options ropts = { 0 };
  44. int n, i, c;
  45. char *infile = NULL;
  46. char *outputfile = NULL;
  47. int code = EXIT_SUCCESS;
  48. const char *colorspace = NULL;
  49. int remove_oi = 0;
  50. while ((c = fz_getopt(argc, argv, "c:o:r")) != -1)
  51. {
  52. switch (c)
  53. {
  54. default: return usage();
  55. // color convert
  56. case 'c': colorspace = fz_optarg; break;
  57. case 'o': outputfile = fz_optarg; break;
  58. case 'r': remove_oi = 1; break;
  59. }
  60. }
  61. if (fz_optind == argc || !outputfile)
  62. return usage();
  63. infile = argv[fz_optind];
  64. if (colorspace == NULL || !strcmp(colorspace, "gray"))
  65. ropts.num_comp = 1;
  66. else if (!strcmp(colorspace, "rgb"))
  67. ropts.num_comp = 3;
  68. else if (!strcmp(colorspace, "cmyk"))
  69. ropts.num_comp = 4;
  70. else
  71. {
  72. fprintf(stderr, "Unknown colorspace\n");
  73. return usage();
  74. }
  75. /* Set up the options for the file saving. */
  76. #if 1
  77. opts.do_compress = 1;
  78. opts.do_compress_images = 1;
  79. opts.do_compress_fonts = 1;
  80. opts.do_garbage = 3;
  81. opts.do_use_objstms = 1;
  82. #else
  83. opts.do_compress = 0;
  84. opts.do_pretty = 1;
  85. opts.do_compress = 0;
  86. opts.do_compress_images = 1;
  87. opts.do_compress_fonts = 0;
  88. opts.do_garbage = 0;
  89. opts.do_clean = 1;
  90. #endif
  91. /* Create a MuPDF library context. */
  92. ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
  93. if (!ctx)
  94. {
  95. fprintf(stderr, "Could not create global context.\n");
  96. return EXIT_FAILURE;
  97. }
  98. /* Register the document handlers (only really need PDF, but this is
  99. * the simplest way. */
  100. fz_register_document_handlers(ctx);
  101. fz_try(ctx)
  102. {
  103. /* Load the input document. */
  104. doc = fz_open_document(ctx, infile);
  105. /* Get a PDF specific pointer, and count the pages. */
  106. pdf = pdf_document_from_fz_document(ctx, doc);
  107. n = fz_count_pages(ctx, doc);
  108. for (i = 0; i < n; i++)
  109. pdf_recolor_page(ctx, pdf, i, &ropts);
  110. if (remove_oi)
  111. pdf_remove_output_intents(ctx, pdf);
  112. pdf_save_document(ctx, pdf, outputfile, &opts);
  113. }
  114. fz_always(ctx)
  115. {
  116. fz_drop_document(ctx, doc);
  117. }
  118. fz_catch(ctx)
  119. {
  120. fz_report_error(ctx);
  121. code = EXIT_FAILURE;
  122. }
  123. fz_drop_context(ctx);
  124. return code;
  125. }