1 #include "mupdf/fitz.h"
2 
3 #include "z-imp.h"
4 
5 typedef 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 
16 void
fz_write_ps_file_header(fz_context * ctx,fz_output * out)17 fz_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 
39 void
fz_write_ps_file_trailer(fz_context * ctx,fz_output * out,int pages)40 fz_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 
45 static void
ps_write_header(fz_context * ctx,fz_band_writer * writer_,fz_colorspace * cs)46 ps_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 
117 static void
ps_write_trailer(fz_context * ctx,fz_band_writer * writer_)118 ps_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 
141 static void
ps_drop_band_writer(fz_context * ctx,fz_band_writer * writer_)142 ps_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 
fz_write_pixmap_as_ps(fz_context * ctx,fz_output * out,const fz_pixmap * pixmap)157 void 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 
fz_save_pixmap_as_ps(fz_context * ctx,fz_pixmap * pixmap,char * filename,int append)182 void 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 
196 static void
ps_write_band(fz_context * ctx,fz_band_writer * writer_,int stride,int band_start,int band_height,const unsigned char * samples)197 ps_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 = Memento_label(fz_malloc(ctx, required_input), "pswriter_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 = Memento_label(fz_malloc(ctx, required_output), "pswriter_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 
fz_new_ps_band_writer(fz_context * ctx,fz_output * out)257 fz_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 
271 typedef struct
272 {
273 	fz_document_writer super;
274 	fz_draw_options draw;
275 	fz_pixmap *pixmap;
276 	fz_output *out;
277 	int count;
278 } fz_ps_writer;
279 
280 static fz_device *
ps_begin_page(fz_context * ctx,fz_document_writer * wri_,fz_rect mediabox)281 ps_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
282 {
283 	fz_ps_writer *wri = (fz_ps_writer*)wri_;
284 	wri->count++;
285 	return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
286 }
287 
288 static void
ps_end_page(fz_context * ctx,fz_document_writer * wri_,fz_device * dev)289 ps_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
290 {
291 	fz_ps_writer *wri = (fz_ps_writer*)wri_;
292 	fz_pixmap *pix = wri->pixmap;
293 	fz_band_writer *bw;
294 
295 	fz_try(ctx)
296 	{
297 		fz_close_device(ctx, dev);
298 		bw = fz_new_ps_band_writer(ctx, wri->out);
299 		fz_write_header(ctx, bw, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, 0, pix->colorspace, pix->seps);
300 		fz_write_band(ctx, bw, pix->stride, pix->h, pix->samples);
301 	}
302 	fz_always(ctx)
303 	{
304 		fz_drop_device(ctx, dev);
305 		fz_drop_band_writer(ctx, bw);
306 		fz_drop_pixmap(ctx, wri->pixmap);
307 		wri->pixmap = NULL;
308 	}
309 	fz_catch(ctx)
310 		fz_rethrow(ctx);
311 }
312 
313 static void
ps_close_writer(fz_context * ctx,fz_document_writer * wri_)314 ps_close_writer(fz_context *ctx, fz_document_writer *wri_)
315 {
316 	fz_ps_writer *wri = (fz_ps_writer*)wri_;
317 	fz_write_ps_file_trailer(ctx, wri->out, wri->count);
318 	fz_close_output(ctx, wri->out);
319 }
320 
321 static void
ps_drop_writer(fz_context * ctx,fz_document_writer * wri_)322 ps_drop_writer(fz_context *ctx, fz_document_writer *wri_)
323 {
324 	fz_ps_writer *wri = (fz_ps_writer*)wri_;
325 	fz_drop_pixmap(ctx, wri->pixmap);
326 	fz_drop_output(ctx, wri->out);
327 }
328 
329 fz_document_writer *
fz_new_ps_writer_with_output(fz_context * ctx,fz_output * out,const char * options)330 fz_new_ps_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
331 {
332 	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);
333 
334 	fz_try(ctx)
335 	{
336 		fz_parse_draw_options(ctx, &wri->draw, options);
337 		wri->out = out;
338 		fz_write_ps_file_header(ctx, wri->out);
339 	}
340 	fz_catch(ctx)
341 	{
342 		fz_free(ctx, wri);
343 		fz_rethrow(ctx);
344 	}
345 
346 	return (fz_document_writer*)wri;
347 }
348 
349 fz_document_writer *
fz_new_ps_writer(fz_context * ctx,const char * path,const char * options)350 fz_new_ps_writer(fz_context *ctx, const char *path, const char *options)
351 {
352 	fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.ps", 0);
353 	fz_document_writer *wri = NULL;
354 	fz_try(ctx)
355 		wri = fz_new_ps_writer_with_output(ctx, out, options);
356 	fz_catch(ctx)
357 	{
358 		fz_drop_output(ctx, out);
359 		fz_rethrow(ctx);
360 	}
361 	return wri;
362 }
363