1#include "mupdf/fitz.h"
2
3/*
4 * Write pixmap to PNM file (without alpha channel)
5 */
6static void
7pnm_write_header(fz_context *ctx, fz_band_writer *writer, fz_colorspace *cs)
8{
9 fz_output *out = writer->out;
10 int w = writer->w;
11 int h = writer->h;
12 int n = writer->n;
13 int alpha = writer->alpha;
14
15 if (writer->s != 0)
16 fz_throw(ctx, FZ_ERROR_GENERIC, "PNM writer cannot cope with spot colors");
17
18 if (alpha)
19 fz_throw(ctx, FZ_ERROR_GENERIC, "PNM writer cannot cope with alpha");
20
21 n -= alpha;
22 if (n != 1 && n != 3)
23 fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pnm");
24
25 if (n == 1)
26 fz_write_printf(ctx, out, "P5\n");
27 if (n == 3)
28 fz_write_printf(ctx, out, "P6\n");
29 fz_write_printf(ctx, out, "%d %d\n", w, h);
30 fz_write_printf(ctx, out, "255\n");
31}
32
33static void
34pnm_write_band(fz_context *ctx, fz_band_writer *writer, int stride, int band_start, int band_height, const unsigned char *p)
35{
36 fz_output *out = writer->out;
37 int w = writer->w;
38 int h = writer->h;
39 int n = writer->n;
40 int len;
41 int end = band_start + band_height;
42
43 if (n != 1 && n != 3)
44 fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pnm");
45
46 if (!out)
47 return;
48
49 if (end > h)
50 end = h;
51 end -= band_start;
52
53 /* Tests show that writing single bytes out at a time
54 * is appallingly slow. We get a huge improvement
55 * by collating stuff into buffers first. */
56
57 while (end--)
58 {
59 len = w;
60 while (len)
61 {
62 int num_written = len;
63
64 switch (n)
65 {
66 case 1:
67 /* No collation required */
68 fz_write_data(ctx, out, p, num_written);
69 p += num_written;
70 break;
71 case 3:
72 fz_write_data(ctx, out, p, num_written*3);
73 p += num_written*3;
74 break;
75 }
76 len -= num_written;
77 }
78 p += stride - w*n;
79 }
80}
81
82fz_band_writer *fz_new_pnm_band_writer(fz_context *ctx, fz_output *out)
83{
84 fz_band_writer *writer = fz_new_band_writer(ctx, fz_band_writer, out);
85
86 writer->header = pnm_write_header;
87 writer->band = pnm_write_band;
88
89 return writer;
90}
91
92void
93fz_write_pixmap_as_pnm(fz_context *ctx, fz_output *out, fz_pixmap *pixmap)
94{
95 fz_band_writer *writer = fz_new_pnm_band_writer(ctx, out);
96 fz_try(ctx)
97 {
98 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0, pixmap->colorspace, pixmap->seps);
99 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
100 }
101 fz_always(ctx)
102 fz_drop_band_writer(ctx, writer);
103 fz_catch(ctx)
104 fz_rethrow(ctx);
105}
106
107void
108fz_save_pixmap_as_pnm(fz_context *ctx, fz_pixmap *pixmap, const char *filename)
109{
110 fz_band_writer *writer = NULL;
111 fz_output *out = fz_new_output_with_path(ctx, filename, 0);
112
113 fz_var(writer);
114
115 fz_try(ctx)
116 {
117 writer = fz_new_pnm_band_writer(ctx, out);
118 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0, pixmap->colorspace, pixmap->seps);
119 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
120 fz_close_output(ctx, out);
121 }
122 fz_always(ctx)
123 {
124 fz_drop_band_writer(ctx, writer);
125 fz_drop_output(ctx, out);
126 }
127 fz_catch(ctx)
128 fz_rethrow(ctx);
129}
130
131/*
132 * Write pixmap to PAM file (with or without alpha channel)
133 */
134
135static void
136pam_write_header(fz_context *ctx, fz_band_writer *writer, fz_colorspace *cs)
137{
138 fz_output *out = writer->out;
139 int w = writer->w;
140 int h = writer->h;
141 int n = writer->n;
142 int alpha = writer->alpha;
143
144 if (writer->s != 0)
145 fz_throw(ctx, FZ_ERROR_GENERIC, "PAM writer cannot cope with spot colors");
146
147 fz_write_printf(ctx, out, "P7\n");
148 fz_write_printf(ctx, out, "WIDTH %d\n", w);
149 fz_write_printf(ctx, out, "HEIGHT %d\n", h);
150 fz_write_printf(ctx, out, "DEPTH %d\n", n);
151 fz_write_printf(ctx, out, "MAXVAL 255\n");
152
153 n -= alpha;
154
155 if (n == 0 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n");
156 else if (n == 1 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n");
157 else if (n == 1 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE_ALPHA\n");
158 else if (n == 3 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB\n");
159 else if (n == 3 && alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB_ALPHA\n");
160 else if (n == 4 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE CMYK\n");
161 else if (n == 4 && alpha) fz_write_printf(ctx, out, "TUPLTYPE CMYK_ALPHA\n");
162 fz_write_printf(ctx, out, "ENDHDR\n");
163}
164
165static void
166pam_write_band(fz_context *ctx, fz_band_writer *writer, int stride, int band_start, int band_height, const unsigned char *sp)
167{
168 fz_output *out = writer->out;
169 int w = writer->w;
170 int h = writer->h;
171 int n = writer->n;
172 int alpha = writer->alpha;
173 int x, y;
174 int end = band_start + band_height;
175
176 if (!out)
177 return;
178
179 if (end > h)
180 end = h;
181 end -= band_start;
182
183 if (alpha)
184 {
185 /* Buffer must be a multiple of 2, 3 and 5 at least. */
186 /* Also, for the generic case, it must be bigger than FZ_MAX_COLORS */
187 char buffer[2*3*4*5*6];
188 char *b = buffer;
189 stride -= n * w;
190 switch (n)
191 {
192 case 2:
193 for (y = 0; y < end; y++)
194 {
195 for (x = 0; x < w; x++)
196 {
197 int a = sp[1];
198 *b++ = a ? (sp[0] * 255 + (a>>1))/a : 0;
199 *b++ = a;
200 sp += 2;
201 if (b == &buffer[sizeof(buffer)])
202 {
203 fz_write_data(ctx, out, buffer, sizeof(buffer));
204 b = buffer;
205 }
206 }
207 sp += stride;
208 }
209 if (b != buffer)
210 fz_write_data(ctx, out, buffer, b - buffer);
211 break;
212 case 4:
213 for (y = 0; y < end; y++)
214 {
215 for (x = 0; x < w; x++)
216 {
217 int a = sp[3];
218 int inva = a ? 256 * 255 / a : 0;
219 *b++ = (sp[0] * inva + 128)>>8;
220 *b++ = (sp[1] * inva + 128)>>8;
221 *b++ = (sp[2] * inva + 128)>>8;
222 *b++ = a;
223 sp += 4;
224 if (b == &buffer[sizeof(buffer)])
225 {
226 fz_write_data(ctx, out, buffer, sizeof(buffer));
227 b = buffer;
228 }
229 }
230 sp += stride;
231 }
232 if (b != buffer)
233 fz_write_data(ctx, out, buffer, b - buffer);
234 break;
235 case 5:
236 for (y = 0; y < end; y++)
237 {
238 for (x = 0; x < w; x++)
239 {
240 int a = sp[4];
241 int inva = a ? 256 * 255 / a : 0;
242 *b++ = (sp[0] * inva + 128)>>8;
243 *b++ = (sp[1] * inva + 128)>>8;
244 *b++ = (sp[2] * inva + 128)>>8;
245 *b++ = (sp[3] * inva + 128)>>8;
246 *b++ = a;
247 sp += 5;
248 if (b == &buffer[sizeof(buffer)])
249 {
250 fz_write_data(ctx, out, buffer, sizeof(buffer));
251 b = buffer;
252 }
253 }
254 sp += stride;
255 }
256 if (b != buffer)
257 fz_write_data(ctx, out, buffer, b - buffer);
258 break;
259 default:
260 for (y = 0; y < end; y++)
261 {
262 for (x = 0; x < w; x++)
263 {
264 int a = sp[n-1];
265 int inva = a ? 256 * 255 / a : 0;
266 int k;
267 for (k = 0; k < n-1; k++)
268 *b++ = (*sp++ * inva + 128)>>8;
269 *b++ = a;
270 sp++;
271 if (b >= &buffer[sizeof(buffer)] - n)
272 {
273 fz_write_data(ctx, out, buffer, b - buffer);
274 b = buffer;
275 }
276 }
277 sp += stride;
278 }
279 if (b != buffer)
280 fz_write_data(ctx, out, buffer, b - buffer);
281 break;
282 }
283 }
284 else
285 for (y = 0; y < end; y++)
286 {
287 fz_write_data(ctx, out, sp, w * n);
288 sp += stride;
289 }
290}
291
292fz_band_writer *fz_new_pam_band_writer(fz_context *ctx, fz_output *out)
293{
294 fz_band_writer *writer = fz_new_band_writer(ctx, fz_band_writer, out);
295
296 writer->header = pam_write_header;
297 writer->band = pam_write_band;
298
299 return writer;
300}
301
302void
303fz_write_pixmap_as_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap)
304{
305 fz_band_writer *writer = fz_new_pam_band_writer(ctx, out);
306 fz_try(ctx)
307 {
308 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0, pixmap->colorspace, pixmap->seps);
309 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
310 }
311 fz_always(ctx)
312 fz_drop_band_writer(ctx, writer);
313 fz_catch(ctx)
314 fz_rethrow(ctx);
315}
316
317void
318fz_save_pixmap_as_pam(fz_context *ctx, fz_pixmap *pixmap, const char *filename)
319{
320 fz_band_writer *writer = NULL;
321 fz_output *out = fz_new_output_with_path(ctx, filename, 0);
322
323 fz_var(writer);
324
325 fz_try(ctx)
326 {
327 writer = fz_new_pam_band_writer(ctx, out);
328 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0, pixmap->colorspace, pixmap->seps);
329 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
330 fz_close_output(ctx, out);
331 }
332 fz_always(ctx)
333 {
334 fz_drop_band_writer(ctx, writer);
335 fz_drop_output(ctx, out);
336 }
337 fz_catch(ctx)
338 fz_rethrow(ctx);
339}
340