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