output-ps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 "z-imp.h"
  24. #include <limits.h>
  25. typedef struct ps_band_writer_s
  26. {
  27. fz_band_writer super;
  28. z_stream stream;
  29. int stream_started;
  30. int stream_ended;
  31. size_t input_size;
  32. unsigned char *input;
  33. size_t output_size;
  34. unsigned char *output;
  35. } ps_band_writer;
  36. void
  37. fz_write_ps_file_header(fz_context *ctx, fz_output *out)
  38. {
  39. fz_write_printf(ctx, out,
  40. "%%!PS-Adobe-3.0\n"
  41. //"%%%%BoundingBox: 0 0 612 792\n"
  42. //"%%%%HiResBoundingBox: 0 0 612 792\n"
  43. "%%%%Creator: MuPDF\n"
  44. "%%%%LanguageLevel: 2\n"
  45. "%%%%CreationDate: D:20160318101706Z00'00'\n"
  46. "%%%%DocumentData: Binary\n"
  47. "%%%%Pages: (atend)\n"
  48. "%%%%EndComments\n"
  49. "\n"
  50. "%%%%BeginProlog\n"
  51. "%%%%EndProlog\n"
  52. "\n"
  53. "%%%%BeginSetup\n"
  54. "%%%%EndSetup\n"
  55. "\n"
  56. );
  57. }
  58. void
  59. fz_write_ps_file_trailer(fz_context *ctx, fz_output *out, int pages)
  60. {
  61. fz_write_printf(ctx, out, "%%%%Trailer\n%%%%Pages: %d\n%%%%EOF\n", pages);
  62. }
  63. static void
  64. ps_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
  65. {
  66. ps_band_writer *writer = (ps_band_writer *)writer_;
  67. fz_output *out = writer->super.out;
  68. int w = writer->super.w;
  69. int h = writer->super.h;
  70. int n = writer->super.n;
  71. int alpha = writer->super.alpha;
  72. int xres = writer->super.xres;
  73. int yres = writer->super.yres;
  74. int pagenum = writer->super.pagenum;
  75. int w_points = (w * 72 + (xres>>1)) / xres;
  76. int h_points = (h * 72 + (yres>>1)) / yres;
  77. float sx = (float) w / w_points;
  78. float sy = (float) h / h_points;
  79. int err;
  80. if (writer->super.s != 0)
  81. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Postscript writer cannot cope with spot colors");
  82. if (alpha != 0)
  83. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Postscript output cannot have alpha");
  84. writer->super.w = w;
  85. writer->super.h = h;
  86. writer->super.n = n;
  87. writer->stream.zalloc = fz_zlib_alloc;
  88. writer->stream.zfree = fz_zlib_free;
  89. writer->stream.opaque = ctx;
  90. writer->stream_started = 1;
  91. err = deflateInit(&writer->stream, Z_DEFAULT_COMPRESSION);
  92. if (err != Z_OK)
  93. fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
  94. fz_write_printf(ctx, out, "%%%%Page: %d %d\n", pagenum, pagenum);
  95. fz_write_printf(ctx, out, "%%%%PageBoundingBox: 0 0 %d %d\n", w_points, h_points);
  96. fz_write_printf(ctx, out, "%%%%BeginPageSetup\n");
  97. fz_write_printf(ctx, out, "<</PageSize [%d %d]>> setpagedevice\n", w_points, h_points);
  98. fz_write_printf(ctx, out, "%%%%EndPageSetup\n\n");
  99. fz_write_printf(ctx, out, "/DataFile currentfile /FlateDecode filter def\n\n");
  100. switch(n)
  101. {
  102. case 1:
  103. fz_write_string(ctx, out, "/DeviceGray setcolorspace\n");
  104. break;
  105. case 3:
  106. fz_write_string(ctx, out, "/DeviceRGB setcolorspace\n");
  107. break;
  108. case 4:
  109. fz_write_string(ctx, out, "/DeviceCMYK setcolorspace\n");
  110. break;
  111. default:
  112. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unexpected colorspace for ps output");
  113. }
  114. fz_write_printf(ctx, out,
  115. "<<\n"
  116. "/ImageType 1\n"
  117. "/Width %d\n"
  118. "/Height %d\n"
  119. "/ImageMatrix [ %g 0 0 -%g 0 %d ]\n"
  120. "/MultipleDataSources false\n"
  121. "/DataSource DataFile\n"
  122. "/BitsPerComponent 8\n"
  123. //"/Decode [0 1]\n"
  124. "/Interpolate false\n"
  125. ">>\n"
  126. "image\n"
  127. , w, h, sx, sy, h);
  128. }
  129. static void
  130. ps_write_trailer(fz_context *ctx, fz_band_writer *writer_)
  131. {
  132. ps_band_writer *writer = (ps_band_writer *)writer_;
  133. fz_output *out = writer->super.out;
  134. int err;
  135. writer->stream_ended = 1;
  136. err = deflateEnd(&writer->stream);
  137. if (err != Z_OK)
  138. fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
  139. fz_write_data(ctx, out, writer->output, writer->output_size - writer->stream.avail_out);
  140. fz_write_string(ctx, out, "\nshowpage\n%%%%PageTrailer\n%%%%EndPageTrailer\n\n");
  141. }
  142. static void
  143. ps_drop_band_writer(fz_context *ctx, fz_band_writer *writer_)
  144. {
  145. ps_band_writer *writer = (ps_band_writer *)writer_;
  146. if (writer->stream_started && !writer->stream_ended)
  147. {
  148. int err = deflateEnd(&writer->stream);
  149. if (err != Z_OK)
  150. fz_warn(ctx, "ignoring compression error %d", err);
  151. }
  152. fz_free(ctx, writer->input);
  153. fz_free(ctx, writer->output);
  154. }
  155. void fz_write_pixmap_as_ps(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap)
  156. {
  157. fz_band_writer *writer;
  158. fz_write_ps_file_header(ctx, out);
  159. writer = fz_new_ps_band_writer(ctx, out);
  160. fz_try(ctx)
  161. {
  162. fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
  163. fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
  164. fz_close_band_writer(ctx, writer);
  165. }
  166. fz_always(ctx)
  167. {
  168. fz_drop_band_writer(ctx, writer);
  169. }
  170. fz_catch(ctx)
  171. {
  172. fz_rethrow(ctx);
  173. }
  174. fz_write_ps_file_trailer(ctx, out, 1);
  175. }
  176. void fz_save_pixmap_as_ps(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append)
  177. {
  178. fz_output *out = fz_new_output_with_path(ctx, filename, append);
  179. fz_try(ctx)
  180. {
  181. fz_write_pixmap_as_ps(ctx, out, pixmap);
  182. fz_close_output(ctx, out);
  183. }
  184. fz_always(ctx)
  185. fz_drop_output(ctx, out);
  186. fz_catch(ctx)
  187. fz_rethrow(ctx);
  188. }
  189. static void
  190. ps_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *samples)
  191. {
  192. ps_band_writer *writer = (ps_band_writer *)writer_;
  193. fz_output *out = writer->super.out;
  194. int w = writer->super.w;
  195. int h = writer->super.h;
  196. int n = writer->super.n;
  197. int x, y, i, err, finalband;
  198. size_t required_input;
  199. size_t required_output;
  200. size_t remain;
  201. unsigned char *o;
  202. if (!out)
  203. return;
  204. finalband = (band_start+band_height >= h);
  205. if (finalband)
  206. band_height = h - band_start;
  207. required_input = w;
  208. if (required_input > SIZE_MAX / n)
  209. fz_throw(ctx, FZ_ERROR_LIMIT, "ps data too large.");
  210. required_input = required_input * n;
  211. if (required_input > SIZE_MAX / band_height)
  212. fz_throw(ctx, FZ_ERROR_LIMIT, "ps data too large.");
  213. required_input *= band_height;
  214. required_output = required_input >= UINT_MAX ? UINT_MAX : deflateBound(&writer->stream, (uLong)required_input);
  215. if (required_output < required_input || required_output > UINT_MAX)
  216. required_output = UINT_MAX;
  217. if (writer->input == NULL || writer->input_size < required_input)
  218. {
  219. fz_free(ctx, writer->input);
  220. writer->input = NULL;
  221. writer->input = Memento_label(fz_malloc(ctx, required_input), "pswriter_input");
  222. writer->input_size = required_input;
  223. }
  224. if (writer->output == NULL || writer->output_size < required_output)
  225. {
  226. fz_free(ctx, writer->output);
  227. writer->output = NULL;
  228. writer->output = Memento_label(fz_malloc(ctx, required_output), "pswriter_output");
  229. writer->output_size = required_output;
  230. }
  231. o = writer->input;
  232. for (y = 0; y < band_height; y++)
  233. {
  234. for (x = 0; x < w; x++)
  235. {
  236. for (i = n; i > 0; i--)
  237. *o++ = *samples++;
  238. }
  239. samples += stride - w*n;
  240. }
  241. remain = o - writer->input;
  242. o = writer->input;
  243. do
  244. {
  245. size_t eaten;
  246. writer->stream.next_in = o;
  247. writer->stream.avail_in = (uInt)(remain <= UINT_MAX ? remain : UINT_MAX);
  248. writer->stream.next_out = writer->output;
  249. writer->stream.avail_out = writer->output_size <= UINT_MAX ? (uInt)writer->output_size : UINT_MAX;
  250. err = deflate(&writer->stream, (finalband && remain == writer->stream.avail_in) ? Z_FINISH : Z_NO_FLUSH);
  251. if (err != Z_OK && err != Z_STREAM_END)
  252. fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
  253. /* We are guaranteed that writer->stream.next_in will have been updated for the
  254. * data that has been eaten. */
  255. eaten = (writer->stream.next_in - o);
  256. remain -= eaten;
  257. o += eaten;
  258. /* We are guaranteed that writer->stream.next_out will have been updated for the
  259. * data that has been written. */
  260. if (writer->stream.next_out != writer->output)
  261. fz_write_data(ctx, out, writer->output, writer->output_size - writer->stream.avail_out);
  262. /* Zlib only guarantees to have finished when we have no more data to feed in, and
  263. * the last call to deflate did not return with avail_out == 0. (i.e. no more is
  264. * buffered internally.) */
  265. }
  266. while (remain != 0 || writer->stream.avail_out == 0);
  267. }
  268. fz_band_writer *fz_new_ps_band_writer(fz_context *ctx, fz_output *out)
  269. {
  270. ps_band_writer *writer = fz_new_band_writer(ctx, ps_band_writer, out);
  271. writer->super.header = ps_write_header;
  272. writer->super.band = ps_write_band;
  273. writer->super.trailer = ps_write_trailer;
  274. writer->super.drop = ps_drop_band_writer;
  275. return &writer->super;
  276. }
  277. /* High-level document writer interface */
  278. typedef struct
  279. {
  280. fz_document_writer super;
  281. fz_draw_options draw;
  282. fz_pixmap *pixmap;
  283. fz_output *out;
  284. int count;
  285. } fz_ps_writer;
  286. static fz_device *
  287. ps_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
  288. {
  289. fz_ps_writer *wri = (fz_ps_writer*)wri_;
  290. wri->count++;
  291. return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
  292. }
  293. static void
  294. ps_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
  295. {
  296. fz_ps_writer *wri = (fz_ps_writer*)wri_;
  297. fz_pixmap *pix = wri->pixmap;
  298. fz_band_writer *bw;
  299. fz_try(ctx)
  300. {
  301. fz_close_device(ctx, dev);
  302. bw = fz_new_ps_band_writer(ctx, wri->out);
  303. fz_write_header(ctx, bw, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, 0, pix->colorspace, pix->seps);
  304. fz_write_band(ctx, bw, pix->stride, pix->h, pix->samples);
  305. fz_close_band_writer(ctx, bw);
  306. }
  307. fz_always(ctx)
  308. {
  309. fz_drop_device(ctx, dev);
  310. fz_drop_band_writer(ctx, bw);
  311. fz_drop_pixmap(ctx, wri->pixmap);
  312. wri->pixmap = NULL;
  313. }
  314. fz_catch(ctx)
  315. fz_rethrow(ctx);
  316. }
  317. static void
  318. ps_close_writer(fz_context *ctx, fz_document_writer *wri_)
  319. {
  320. fz_ps_writer *wri = (fz_ps_writer*)wri_;
  321. fz_write_ps_file_trailer(ctx, wri->out, wri->count);
  322. fz_close_output(ctx, wri->out);
  323. }
  324. static void
  325. ps_drop_writer(fz_context *ctx, fz_document_writer *wri_)
  326. {
  327. fz_ps_writer *wri = (fz_ps_writer*)wri_;
  328. fz_drop_pixmap(ctx, wri->pixmap);
  329. fz_drop_output(ctx, wri->out);
  330. }
  331. fz_document_writer *
  332. fz_new_ps_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
  333. {
  334. fz_ps_writer *wri = NULL;
  335. fz_var(wri);
  336. fz_try(ctx)
  337. {
  338. wri = fz_new_derived_document_writer(ctx, fz_ps_writer, ps_begin_page, ps_end_page, ps_close_writer, ps_drop_writer);
  339. fz_parse_draw_options(ctx, &wri->draw, options);
  340. wri->out = out;
  341. fz_write_ps_file_header(ctx, wri->out);
  342. }
  343. fz_catch(ctx)
  344. {
  345. fz_drop_output(ctx, out);
  346. fz_free(ctx, wri);
  347. fz_rethrow(ctx);
  348. }
  349. return (fz_document_writer*)wri;
  350. }
  351. fz_document_writer *
  352. fz_new_ps_writer(fz_context *ctx, const char *path, const char *options)
  353. {
  354. fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.ps", 0);
  355. return fz_new_ps_writer_with_output(ctx, out, options);
  356. }