]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/fitz/output-pwg.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / fitz / output-pwg.c
1 #include "mupdf/fitz.h"
2
3 void
4 fz_output_pwg_file_header(fz_output *out)
5 {
6         static const unsigned char pwgsig[4] = { 'R', 'a', 'S', '2' };
7
8         /* Sync word */
9         fz_write(out, pwgsig, 4);
10 }
11
12 static void
13 output_header(fz_output *out, const fz_pwg_options *pwg, int xres, int yres, int w, int h, int bpp)
14 {
15         static const char zero[64] = { 0 };
16         int i;
17
18         /* Page Header: */
19         fz_write(out, pwg ? pwg->media_class : zero, 64);
20         fz_write(out, pwg ? pwg->media_color : zero, 64);
21         fz_write(out, pwg ? pwg->media_type : zero, 64);
22         fz_write(out, pwg ? pwg->output_type : zero, 64);
23         fz_write_int32be(out, pwg ? pwg->advance_distance : 0);
24         fz_write_int32be(out, pwg ? pwg->advance_media : 0);
25         fz_write_int32be(out, pwg ? pwg->collate : 0);
26         fz_write_int32be(out, pwg ? pwg->cut_media : 0);
27         fz_write_int32be(out, pwg ? pwg->duplex : 0);
28         fz_write_int32be(out, xres);
29         fz_write_int32be(out, yres);
30         /* CUPS format says that 284->300 are supposed to be the bbox of the
31          * page in points. PWG says 'Reserved'. */
32         for (i=284; i < 300; i += 4)
33                 fz_write(out, zero, 4);
34         fz_write_int32be(out, pwg ? pwg->insert_sheet : 0);
35         fz_write_int32be(out, pwg ? pwg->jog : 0);
36         fz_write_int32be(out, pwg ? pwg->leading_edge : 0);
37         /* CUPS format says that 312->320 are supposed to be the margins of
38          * the lower left hand edge of page in points. PWG says 'Reserved'. */
39         for (i=312; i < 320; i += 4)
40                 fz_write(out, zero, 4);
41         fz_write_int32be(out, pwg ? pwg->manual_feed : 0);
42         fz_write_int32be(out, pwg ? pwg->media_position : 0);
43         fz_write_int32be(out, pwg ? pwg->media_weight : 0);
44         fz_write_int32be(out, pwg ? pwg->mirror_print : 0);
45         fz_write_int32be(out, pwg ? pwg->negative_print : 0);
46         fz_write_int32be(out, pwg ? pwg->num_copies : 0);
47         fz_write_int32be(out, pwg ? pwg->orientation : 0);
48         fz_write_int32be(out, pwg ? pwg->output_face_up : 0);
49         fz_write_int32be(out, w * 72/ xres);    /* Page size in points */
50         fz_write_int32be(out, h * 72/ yres);
51         fz_write_int32be(out, pwg ? pwg->separations : 0);
52         fz_write_int32be(out, pwg ? pwg->tray_switch : 0);
53         fz_write_int32be(out, pwg ? pwg->tumble : 0);
54         fz_write_int32be(out, w); /* Page image in pixels */
55         fz_write_int32be(out, h);
56         fz_write_int32be(out, pwg ? pwg->media_type_num : 0);
57         fz_write_int32be(out, bpp < 8 ? 1 : 8); /* Bits per color */
58         fz_write_int32be(out, bpp); /* Bits per pixel */
59         fz_write_int32be(out, (w * bpp + 7)/8); /* Bytes per line */
60         fz_write_int32be(out, 0); /* Chunky pixels */
61         switch (bpp)
62         {
63         case 1: fz_write_int32be(out, 3); /* Black */ break;
64         case 8: fz_write_int32be(out, 18); /* Sgray */ break;
65         case 24: fz_write_int32be(out, 19); /* Srgb */ break;
66         case 32: fz_write_int32be(out, 6); /* Cmyk */ break;
67         default: fz_throw(out->ctx, FZ_ERROR_GENERIC, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg");
68         }
69         fz_write_int32be(out, pwg ? pwg->compression : 0);
70         fz_write_int32be(out, pwg ? pwg->row_count : 0);
71         fz_write_int32be(out, pwg ? pwg->row_feed : 0);
72         fz_write_int32be(out, pwg ? pwg->row_step : 0);
73         fz_write_int32be(out, bpp <= 8 ? 1 : 3); /* Num Colors */
74         for (i=424; i < 452; i += 4)
75                 fz_write(out, zero, 4);
76         fz_write_int32be(out, 1); /* TotalPageCount */
77         fz_write_int32be(out, 1); /* CrossFeedTransform */
78         fz_write_int32be(out, 1); /* FeedTransform */
79         fz_write_int32be(out, 0); /* ImageBoxLeft */
80         fz_write_int32be(out, 0); /* ImageBoxTop */
81         fz_write_int32be(out, w); /* ImageBoxRight */
82         fz_write_int32be(out, h); /* ImageBoxBottom */
83         for (i=480; i < 1668; i += 4)
84                 fz_write(out, zero, 4);
85         fz_write(out, pwg ? pwg->rendering_intent : zero, 64);
86         fz_write(out, pwg ? pwg->page_size_name : zero, 64);
87 }
88
89 void
90 fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
91 {
92         unsigned char *sp;
93         int y, x, sn, dn, ss;
94         fz_context *ctx;
95
96         if (!out || !pixmap)
97                 return;
98
99         ctx = out->ctx;
100
101         if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4 && pixmap->n != 5)
102                 fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale, rgb or cmyk to write as pwg");
103
104         sn = pixmap->n;
105         dn = pixmap->n;
106         if (dn > 1)
107                 dn--;
108
109         output_header(out, pwg, pixmap->xres, pixmap->yres, pixmap->w, pixmap->h, dn*8);
110
111         /* Now output the actual bitmap, using a packbits like compression */
112         sp = pixmap->samples;
113         ss = pixmap->w * sn;
114         y = 0;
115         while (y < pixmap->h)
116         {
117                 int yrep;
118
119                 assert(sp == pixmap->samples + y * ss);
120
121                 /* Count the number of times this line is repeated */
122                 for (yrep = 1; yrep < 256 && y+yrep < pixmap->h; yrep++)
123                 {
124                         if (memcmp(sp, sp + yrep * ss, ss) != 0)
125                                 break;
126                 }
127                 fz_write_byte(out, yrep-1);
128
129                 /* Encode the line */
130                 x = 0;
131                 while (x < pixmap->w)
132                 {
133                         int d;
134
135                         assert(sp == pixmap->samples + y * ss + x * sn);
136
137                         /* How far do we have to look to find a repeated value? */
138                         for (d = 1; d < 128 && x+d < pixmap->w; d++)
139                         {
140                                 if (memcmp(sp + (d-1)*sn, sp + d*sn, sn) == 0)
141                                         break;
142                         }
143                         if (d == 1)
144                         {
145                                 int xrep;
146
147                                 /* We immediately have a repeat (or we've hit
148                                  * the end of the line). Count the number of
149                                  * times this value is repeated. */
150                                 for (xrep = 1; xrep < 128 && x+xrep < pixmap->w; xrep++)
151                                 {
152                                         if (memcmp(sp, sp + xrep*sn, sn) != 0)
153                                                 break;
154                                 }
155                                 fz_write_byte(out, xrep-1);
156                                 fz_write(out, sp, dn);
157                                 sp += sn*xrep;
158                                 x += xrep;
159                         }
160                         else
161                         {
162                                 fz_write_byte(out, 257-d);
163                                 x += d;
164                                 while (d > 0)
165                                 {
166                                         fz_write(out, sp, dn);
167                                         sp += sn;
168                                         d--;
169                                 }
170                         }
171                 }
172
173                 /* Move to the next line */
174                 sp += ss*(yrep-1);
175                 y += yrep;
176         }
177 }
178
179 void
180 fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg)
181 {
182         unsigned char *sp;
183         int y, x, ss;
184         int byte_width;
185
186         if (!out || !bitmap)
187                 return;
188
189         output_header(out, pwg, bitmap->xres, bitmap->yres, bitmap->w, bitmap->h, 1);
190
191         /* Now output the actual bitmap, using a packbits like compression */
192         sp = bitmap->samples;
193         ss = bitmap->stride;
194         byte_width = (bitmap->w+7)/8;
195         y = 0;
196         while (y < bitmap->h)
197         {
198                 int yrep;
199
200                 assert(sp == bitmap->samples + y * ss);
201
202                 /* Count the number of times this line is repeated */
203                 for (yrep = 1; yrep < 256 && y+yrep < bitmap->h; yrep++)
204                 {
205                         if (memcmp(sp, sp + yrep * ss, byte_width) != 0)
206                                 break;
207                 }
208                 fz_write_byte(out, yrep-1);
209
210                 /* Encode the line */
211                 x = 0;
212                 while (x < byte_width)
213                 {
214                         int d;
215
216                         assert(sp == bitmap->samples + y * ss + x);
217
218                         /* How far do we have to look to find a repeated value? */
219                         for (d = 1; d < 128 && x+d < byte_width; d++)
220                         {
221                                 if (sp[d-1] == sp[d])
222                                         break;
223                         }
224                         if (d == 1)
225                         {
226                                 int xrep;
227
228                                 /* We immediately have a repeat (or we've hit
229                                  * the end of the line). Count the number of
230                                  * times this value is repeated. */
231                                 for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++)
232                                 {
233                                         if (sp[0] != sp[xrep])
234                                                 break;
235                                 }
236                                 fz_write_byte(out, xrep-1);
237                                 fz_write(out, sp, 1);
238                                 sp += xrep;
239                                 x += xrep;
240                         }
241                         else
242                         {
243                                 fz_write_byte(out, 257-d);
244                                 fz_write(out, sp, d);
245                                 sp += d;
246                                 x += d;
247                         }
248                 }
249
250                 /* Move to the next line */
251                 sp += ss*yrep - byte_width;
252                 y += yrep;
253         }
254 }
255
256 void
257 fz_output_pwg(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
258 {
259         fz_output_pwg_file_header(out);
260         fz_output_pwg_page(out, pixmap, pwg);
261 }
262
263 void
264 fz_write_pwg(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pwg_options *pwg)
265 {
266         FILE *fp;
267         fz_output *out = NULL;
268
269         fp = fopen(filename, append ? "ab" : "wb");
270         if (!fp)
271         {
272                 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
273         }
274
275         fz_var(out);
276
277         fz_try(ctx)
278         {
279                 out = fz_new_output_with_file(ctx, fp);
280                 if (!append)
281                         fz_output_pwg_file_header(out);
282                 fz_output_pwg_page(out, pixmap, pwg);
283         }
284         fz_always(ctx)
285         {
286                 fz_close_output(out);
287                 fclose(fp);
288         }
289         fz_catch(ctx)
290         {
291                 fz_rethrow(ctx);
292         }
293 }
294
295 void
296 fz_write_pwg_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, const fz_pwg_options *pwg)
297 {
298         FILE *fp;
299         fz_output *out = NULL;
300
301         fp = fopen(filename, append ? "ab" : "wb");
302         if (!fp)
303         {
304                 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
305         }
306
307         fz_var(out);
308
309         fz_try(ctx)
310         {
311                 out = fz_new_output_with_file(ctx, fp);
312                 if (!append)
313                         fz_output_pwg_file_header(out);
314                 fz_output_pwg_bitmap_page(out, bitmap, pwg);
315         }
316         fz_always(ctx)
317         {
318                 fz_close_output(out);
319                 fclose(fp);
320         }
321         fz_catch(ctx)
322         {
323                 fz_rethrow(ctx);
324         }
325 }