output-pclm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Copyright (C) 2004-2021 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. #include "mupdf/fitz.h"
  23. #include <string.h>
  24. #include <limits.h>
  25. const char *fz_pclm_write_options_usage =
  26. "PCLm output options:\n"
  27. "\tcompression=none: No compression (default)\n"
  28. "\tcompression=flate: Flate compression\n"
  29. "\tstrip-height=N: Strip height (default 16)\n"
  30. "\n";
  31. fz_pclm_options *
  32. fz_parse_pclm_options(fz_context *ctx, fz_pclm_options *opts, const char *args)
  33. {
  34. const char *val;
  35. memset(opts, 0, sizeof *opts);
  36. if (fz_has_option(ctx, args, "compression", &val))
  37. {
  38. if (fz_option_eq(val, "none"))
  39. opts->compress = 0;
  40. else if (fz_option_eq(val, "flate"))
  41. opts->compress = 1;
  42. else
  43. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported PCLm compression %s (none, or flate only)", val);
  44. }
  45. if (fz_has_option(ctx, args, "strip-height", &val))
  46. {
  47. int i = fz_atoi(val);
  48. if (i <= 0)
  49. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unsupported PCLm strip height %d (suggest 16)", i);
  50. opts->strip_height = i;
  51. }
  52. return opts;
  53. }
  54. void
  55. fz_write_pixmap_as_pclm(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pclm_options *pclm)
  56. {
  57. fz_band_writer *writer;
  58. if (!pixmap || !out)
  59. return;
  60. writer = fz_new_pclm_band_writer(ctx, out, pclm);
  61. fz_try(ctx)
  62. {
  63. fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
  64. fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
  65. fz_close_band_writer(ctx, writer);
  66. }
  67. fz_always(ctx)
  68. fz_drop_band_writer(ctx, writer);
  69. fz_catch(ctx)
  70. fz_rethrow(ctx);
  71. }
  72. typedef struct pclm_band_writer_s
  73. {
  74. fz_band_writer super;
  75. fz_pclm_options options;
  76. int obj_num;
  77. int xref_max;
  78. int64_t *xref;
  79. int pages;
  80. int page_max;
  81. int *page_obj;
  82. unsigned char *stripbuf;
  83. unsigned char *compbuf;
  84. size_t complen;
  85. } pclm_band_writer;
  86. static int
  87. new_obj(fz_context *ctx, pclm_band_writer *writer)
  88. {
  89. int64_t pos = fz_tell_output(ctx, writer->super.out);
  90. if (writer->obj_num >= writer->xref_max)
  91. {
  92. int new_max = writer->xref_max * 2;
  93. if (new_max < writer->obj_num + 8)
  94. new_max = writer->obj_num + 8;
  95. writer->xref = fz_realloc_array(ctx, writer->xref, new_max, int64_t);
  96. writer->xref_max = new_max;
  97. }
  98. writer->xref[writer->obj_num] = pos;
  99. return writer->obj_num++;
  100. }
  101. static void
  102. pclm_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
  103. {
  104. pclm_band_writer *writer = (pclm_band_writer *)writer_;
  105. fz_output *out = writer->super.out;
  106. int w = writer->super.w;
  107. int h = writer->super.h;
  108. int n = writer->super.n;
  109. int s = writer->super.s;
  110. int a = writer->super.alpha;
  111. int xres = writer->super.xres;
  112. int yres = writer->super.yres;
  113. int sh = writer->options.strip_height;
  114. int strips = (h + sh-1)/sh;
  115. int i;
  116. size_t len;
  117. unsigned char *data;
  118. fz_buffer *buf = NULL;
  119. if (a != 0)
  120. fz_throw(ctx, FZ_ERROR_ARGUMENT, "PCLm cannot write alpha channel");
  121. if (s != 0)
  122. fz_throw(ctx, FZ_ERROR_ARGUMENT, "PCLm cannot write spot colors");
  123. if (n != 3 && n != 1)
  124. fz_throw(ctx, FZ_ERROR_ARGUMENT, "PCLm expected to be Grayscale or RGB");
  125. fz_free(ctx, writer->stripbuf);
  126. writer->stripbuf = NULL;
  127. fz_free(ctx, writer->compbuf);
  128. writer->compbuf = NULL;
  129. writer->stripbuf = Memento_label(fz_malloc(ctx, (size_t)w * sh * n), "pclm_stripbuf");
  130. writer->complen = fz_deflate_bound(ctx, (size_t)w * sh * n);
  131. writer->compbuf = Memento_label(fz_malloc(ctx, writer->complen), "pclm_compbuf");
  132. /* Send the file header on the first page */
  133. if (writer->pages == 0)
  134. fz_write_string(ctx, out, "%PDF-1.4\n%PCLm-1.0\n");
  135. if (writer->page_max <= writer->pages)
  136. {
  137. int new_max = writer->page_max * 2;
  138. if (new_max == 0)
  139. new_max = writer->pages + 8;
  140. writer->page_obj = fz_realloc_array(ctx, writer->page_obj, new_max, int);
  141. writer->page_max = new_max;
  142. }
  143. writer->page_obj[writer->pages] = writer->obj_num;
  144. writer->pages++;
  145. /* Send the Page Object */
  146. fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Resources <<\n/XObject <<\n", new_obj(ctx, writer));
  147. for (i = 0; i < strips; i++)
  148. fz_write_printf(ctx, out, "/Image%d %d 0 R\n", i, writer->obj_num + 1 + i);
  149. fz_write_printf(ctx, out, ">>\n>>\n/MediaBox[ 0 0 %g %g ]\n/Contents [ %d 0 R ]\n>>\nendobj\n",
  150. w * 72.0f / xres, h * 72.0f / yres, writer->obj_num);
  151. /* And the Page contents */
  152. /* We need the length to this, so write to a buffer first */
  153. fz_var(buf);
  154. fz_try(ctx)
  155. {
  156. buf = fz_new_buffer(ctx, 0);
  157. fz_append_printf(ctx, buf, "%g 0 0 %g 0 0 cm\n", 72.0f/xres, 72.0f/yres);
  158. for (i = 0; i < strips; i++)
  159. {
  160. int at = h - (i+1)*sh;
  161. int this_sh = sh;
  162. if (at < 0)
  163. {
  164. this_sh += at;
  165. at = 0;
  166. }
  167. fz_append_printf(ctx, buf, "/P <</MCID 0>> BDC q\n%d 0 0 %d 0 %d cm\n/Image%d Do Q\n",
  168. w, this_sh, at, i);
  169. }
  170. len = fz_buffer_storage(ctx, buf, &data);
  171. fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Length %zd\n>>\nstream\n", new_obj(ctx, writer), len);
  172. fz_write_data(ctx, out, data, len);
  173. fz_drop_buffer(ctx, buf);
  174. buf = NULL;
  175. fz_write_string(ctx, out, "\nendstream\nendobj\n");
  176. }
  177. fz_catch(ctx)
  178. {
  179. fz_drop_buffer(ctx, buf);
  180. fz_rethrow(ctx);
  181. }
  182. }
  183. static void
  184. flush_strip(fz_context *ctx, pclm_band_writer *writer, int fill)
  185. {
  186. unsigned char *data = writer->stripbuf;
  187. fz_output *out = writer->super.out;
  188. int w = writer->super.w;
  189. int n = writer->super.n;
  190. size_t len = (size_t)w*n*fill;
  191. /* Buffer is full, compress it and write it. */
  192. if (writer->options.compress)
  193. {
  194. size_t destLen = writer->complen;
  195. fz_deflate(ctx, writer->compbuf, &destLen, data, len, FZ_DEFLATE_DEFAULT);
  196. len = destLen;
  197. data = writer->compbuf;
  198. }
  199. fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Width %d\n/ColorSpace /Device%s\n/Height %d\n%s/Subtype /Image\n",
  200. new_obj(ctx, writer), w, n == 1 ? "Gray" : "RGB", fill, writer->options.compress ? "/Filter /FlateDecode\n" : "");
  201. fz_write_printf(ctx, out, "/Length %zd\n/Type /XObject\n/BitsPerComponent 8\n>>\nstream\n", len);
  202. fz_write_data(ctx, out, data, len);
  203. fz_write_string(ctx, out, "\nendstream\nendobj\n");
  204. }
  205. static void
  206. pclm_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *sp)
  207. {
  208. pclm_band_writer *writer = (pclm_band_writer *)writer_;
  209. fz_output *out = writer->super.out;
  210. int w = writer->super.w;
  211. int h = writer->super.h;
  212. int n = writer->super.n;
  213. int sh = writer->options.strip_height;
  214. int line;
  215. if (!out)
  216. return;
  217. for (line = 0; line < band_height; line++)
  218. {
  219. int dstline = (band_start+line) % sh;
  220. memcpy(writer->stripbuf + (size_t)w*n*dstline,
  221. sp + (size_t)line * w * n,
  222. (size_t)w * n);
  223. if (dstline+1 == sh)
  224. flush_strip(ctx, writer, dstline+1);
  225. }
  226. if (band_start + band_height == h && h % sh != 0)
  227. flush_strip(ctx, writer, h % sh);
  228. }
  229. static void
  230. pclm_write_trailer(fz_context *ctx, fz_band_writer *writer_)
  231. {
  232. }
  233. static void
  234. pclm_close_band_writer(fz_context *ctx, fz_band_writer *writer_)
  235. {
  236. pclm_band_writer *writer = (pclm_band_writer *)writer_;
  237. fz_output *out = writer->super.out;
  238. int i;
  239. /* We actually do the trailer writing in the close */
  240. if (writer->xref_max > 2)
  241. {
  242. int64_t t_pos;
  243. /* Catalog */
  244. writer->xref[1] = fz_tell_output(ctx, out);
  245. fz_write_printf(ctx, out, "1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n");
  246. /* Page table */
  247. writer->xref[2] = fz_tell_output(ctx, out);
  248. fz_write_printf(ctx, out, "2 0 obj\n<<\n/Count %d\n/Kids [ ", writer->pages);
  249. for (i = 0; i < writer->pages; i++)
  250. fz_write_printf(ctx, out, "%d 0 R ", writer->page_obj[i]);
  251. fz_write_string(ctx, out, "]\n/Type /Pages\n>>\nendobj\n");
  252. /* Xref */
  253. t_pos = fz_tell_output(ctx, out);
  254. fz_write_printf(ctx, out, "xref\n0 %d\n0000000000 65535 f \n", writer->obj_num);
  255. for (i = 1; i < writer->obj_num; i++)
  256. fz_write_printf(ctx, out, "%010zd 00000 n \n", writer->xref[i]);
  257. fz_write_printf(ctx, out, "trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\nstartxref\n%ld\n%%%%EOF\n", writer->obj_num, t_pos);
  258. }
  259. }
  260. static void
  261. pclm_drop_band_writer(fz_context *ctx, fz_band_writer *writer_)
  262. {
  263. pclm_band_writer *writer = (pclm_band_writer *)writer_;
  264. fz_free(ctx, writer->stripbuf);
  265. fz_free(ctx, writer->compbuf);
  266. fz_free(ctx, writer->page_obj);
  267. fz_free(ctx, writer->xref);
  268. }
  269. fz_band_writer *fz_new_pclm_band_writer(fz_context *ctx, fz_output *out, const fz_pclm_options *options)
  270. {
  271. pclm_band_writer *writer = fz_new_band_writer(ctx, pclm_band_writer, out);
  272. writer->super.header = pclm_write_header;
  273. writer->super.band = pclm_write_band;
  274. writer->super.trailer = pclm_write_trailer;
  275. writer->super.close = pclm_close_band_writer;
  276. writer->super.drop = pclm_drop_band_writer;
  277. if (options)
  278. writer->options = *options;
  279. else
  280. memset(&writer->options, 0, sizeof(writer->options));
  281. if (writer->options.strip_height == 0)
  282. writer->options.strip_height = 16;
  283. writer->obj_num = 3; /* 1 reserved for catalog, 2 for pages tree. */
  284. return &writer->super;
  285. }
  286. void
  287. fz_save_pixmap_as_pclm(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int append, const fz_pclm_options *pclm)
  288. {
  289. fz_output *out = fz_new_output_with_path(ctx, filename, append);
  290. fz_try(ctx)
  291. {
  292. fz_write_pixmap_as_pclm(ctx, out, pixmap, pclm);
  293. fz_close_output(ctx, out);
  294. }
  295. fz_always(ctx)
  296. fz_drop_output(ctx, out);
  297. fz_catch(ctx)
  298. fz_rethrow(ctx);
  299. }
  300. /* High-level document writer interface */
  301. typedef struct
  302. {
  303. fz_document_writer super;
  304. fz_draw_options draw;
  305. fz_pclm_options pclm;
  306. fz_pixmap *pixmap;
  307. fz_band_writer *bander;
  308. fz_output *out;
  309. int pagenum;
  310. } fz_pclm_writer;
  311. static fz_device *
  312. pclm_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
  313. {
  314. fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
  315. return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
  316. }
  317. static void
  318. pclm_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
  319. {
  320. fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
  321. fz_pixmap *pix = wri->pixmap;
  322. fz_try(ctx)
  323. {
  324. fz_close_device(ctx, dev);
  325. fz_write_header(ctx, wri->bander, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, wri->pagenum++, pix->colorspace, pix->seps);
  326. fz_write_band(ctx, wri->bander, pix->stride, pix->h, pix->samples);
  327. }
  328. fz_always(ctx)
  329. {
  330. fz_drop_device(ctx, dev);
  331. fz_drop_pixmap(ctx, pix);
  332. wri->pixmap = NULL;
  333. }
  334. fz_catch(ctx)
  335. fz_rethrow(ctx);
  336. }
  337. static void
  338. pclm_close_writer(fz_context *ctx, fz_document_writer *wri_)
  339. {
  340. fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
  341. fz_close_band_writer(ctx, wri->bander);
  342. fz_close_output(ctx, wri->out);
  343. }
  344. static void
  345. pclm_drop_writer(fz_context *ctx, fz_document_writer *wri_)
  346. {
  347. fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
  348. fz_drop_pixmap(ctx, wri->pixmap);
  349. fz_drop_output(ctx, wri->out);
  350. fz_drop_band_writer(ctx, wri->bander);
  351. }
  352. fz_document_writer *
  353. fz_new_pclm_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
  354. {
  355. fz_pclm_writer *wri = NULL;
  356. fz_var(wri);
  357. fz_try(ctx)
  358. {
  359. wri = fz_new_derived_document_writer(ctx, fz_pclm_writer, pclm_begin_page, pclm_end_page, pclm_close_writer, pclm_drop_writer);
  360. fz_parse_draw_options(ctx, &wri->draw, options);
  361. fz_parse_pclm_options(ctx, &wri->pclm, options);
  362. wri->out = out;
  363. wri->bander = fz_new_pclm_band_writer(ctx, wri->out, &wri->pclm);
  364. }
  365. fz_catch(ctx)
  366. {
  367. fz_drop_output(ctx, out);
  368. fz_free(ctx, wri);
  369. fz_rethrow(ctx);
  370. }
  371. return (fz_document_writer*)wri;
  372. }
  373. fz_document_writer *
  374. fz_new_pclm_writer(fz_context *ctx, const char *path, const char *options)
  375. {
  376. fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.pclm", 0);
  377. return fz_new_pclm_writer_with_output(ctx, out, options);
  378. }