Debian Codegraph @

texk/web2c/luatexdir/image/writejbig2.c

raw
1 /*
2
3 writejbig2.c
4
5 Copyright 1996-2006 Han The Thanh <[email protected]>
6 Copyright 2006-2013 Taco Hoekwater <[email protected]>
7 Copyright 2003-2013 Hartmut Henkel <[email protected]>
8
9 This file is part of LuaTeX.
10
11 LuaTeX is free software; you can redistribute it and/or modify it under the terms
12 of the GNU General Public License as published by the Free Software Foundation;
13 either version 2 of the License, or (at your option) any later version.
14
15 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along with
20 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 /*tex
25
26 This is experimental JBIG2 image support to pdfTeX. JBIG2 image decoding is part
27 of Adobe PDF-1.4, and requires Acroread 5.0 or later.
28
29 References
30 ==========
31
32 * 14492 FCD: Information technology -- coded representation of picture
33 and audio information -- lossy/lossless coding of bi-level images /
34 JBIG committee, 1999 July 16. This JBIG2 Working Draft is available from
35 http://www.jpeg.org/public/fcd14492.pdf. The references in the C-code
36 correspond to the sections of this document.
37
38 * PDF Reference, 5th edition, version 1.6, 1985--2005 Adobe Systems
39 Incorporated. Available online:
40
41 http://partners.adobe.com/public/developer/en/pdf/PDFReference16.pdf
42
43 News
44 ====
45
46 31 May 2006: no need to wait for |endoffileflag| in sequential access
47 organization.
48
49 10 May 2006: |ygetc()| for some catching of broken JBIG2 files; modify to
50 accept Example 3.4 from PDFRef 5th ed. with short end-of-file segment.
51
52 09 May 2006: |pages_maketree()| and |segments_maketree()| by AVL tree,
53 some cleaning up.
54
55 06 May 2006: File list replaced by AVL tree; |new_fileinfo()|,
56 |new_pageinfo()|.
57
58 04 May 2006: Updated for pdftex-1.40-beta-20060213.
59
60 08 Jan. 2003: Added |flushjbig2page0objects()| function. Now at the end
61 of the pdfTeX run all pending page0 objects are written out.
62
63 08 Jan. 2003: Release on private webpage.
64
65 04 Jan. 2003: Completely rewritten. Now with some data structures.
66 Rudimentary local file and image bookkeeping. Multiple image inclusion
67 from one JBIG2 file. Only required page0 segments are marked for
68 inclusion.
69
70 13 Nov. 2002: pdfcrypting removed.
71
72 08 Dec. 2002: bug in page 0 stream writing repaired.
73 Strategy for multiple page inclusion from same JBIG2 file: When writing
74 1st image, create fresh PDF object for page 0, and include any page
75 0 segments from complete file (even if these segments are not needed
76 for image). When writing next image, check by filename comparison if
77 PDF object for page 0 of this JBIG2 file has already been written. This
78 can only remember the file name for the direct predecessor JBIG2 image
79 (but images of other types might come inbetween). If such page 0 PDF
80 object exists, reference it. Else create fresh one.
81
82 09 Dec. 2002: JBIG2 seg. page numbers > 0 are now set to 1, see PDF Ref.
83
84 */
85
86 #undef DEBUG
87
88 #include "ptexlib.h"
89 #include <stdlib.h>
90 #include <stdio.h>
91 #include <assert.h>
92 #include "image/image.h"
93
94 /*tex Table 7.3: Segment types */
95
96 #define M_SymbolDictionary 0
97 #define M_IntermediateTextRegion 4
98 #define M_ImmediateTextRegion 6
99 #define M_ImmediateLosslessTextRegion 7
100 #define M_PatternDictionary 16
101 #define M_IntermediateHalftoneRegion 20
102 #define M_ImmediateHalftoneRegion 22
103 #define M_ImmediateLosslessHalftoneRegion 23
104 #define M_IntermediateGenericRegion 36
105 #define M_ImmediateGenericRegion 38
106 #define M_ImmediateLosslessGenericRegion 39
107 #define M_IntermediateGenericRefinementRegion 40
108 #define M_ImmediateGenericRefinementRegion 42
109 #define M_ImmediateLosslessGenericRefinementRegion 43
110 #define M_PageInformation 48
111 #define M_EndOfPage 49
112 #define M_EndOfStripe 50
113 #define M_EndOfFile 51
114 #define M_Profiles 52
115 #define M_Tables 53
116 #define M_Extension 62
117
118 typedef enum { INITIAL, HAVEINFO, WRITEPDF } PHASE;
119
120 typedef struct _LITEM {
121 struct _LITEM *prev;
122 struct _LITEM *next;
123 void *d;
124 } LITEM;
125
126 typedef struct _LIST {
127 LITEM *first;
128 LITEM *last;
129 struct avl_table *tree;
130 } LIST;
131
132 typedef struct _SEGINFO {
133 unsigned long segnum;
134 boolean isrefered;
135 boolean refers;
136 /*tex Set by |readseghdr|: */
137 unsigned int seghdrflags;
138 boolean pageassocsizeflag;
139 unsigned int reftosegcount;
140 unsigned int countofrefered;
141 unsigned int fieldlen;
142 unsigned int segnumwidth;
143 long segpage;
144 unsigned long segdatalen;
145 unsigned long hdrstart;
146 unsigned long hdrend;
147 unsigned long datastart;
148 unsigned long dataend;
149 /*tex Set by |checkseghdrflags|: */
150 boolean endofstripeflag;
151 boolean endofpageflag;
152 boolean pageinfoflag;
153 boolean endoffileflag;
154 } SEGINFO;
155
156 typedef struct _PAGEINFO {
157 LIST segments;
158 unsigned long pagenum;
159 unsigned int width;
160 unsigned int height;
161 unsigned int xres;
162 unsigned int yres;
163 unsigned int pagesegmentflags;
164 unsigned int stripinginfo;
165 unsigned int stripedheight;
166 } PAGEINFO;
167
168 typedef struct _FILEINFO {
169 FILE *file;
170 char *filepath;
171 long filesize;
172 /*tex Not including |page0|: */
173 LIST pages;
174 LIST page0;
175 /*tex Set by |readfilehdr| */
176 unsigned int filehdrflags;
177 boolean sequentialaccess;
178 unsigned long numofpages;
179 /*tex Set by |get_jbig2_info| */
180 unsigned long streamstart;
181 unsigned long pdfpage0objnum;
182 PHASE phase;
183 } FILEINFO;
184
185 static struct avl_table *file_tree = NULL;
186
187 static int comp_file_entry(const void *pa, const void *pb, void *p)
188 {
189 (void) p;
190 return strcmp(((const FILEINFO *) pa)->filepath,((const FILEINFO *) pb)->filepath);
191 }
192
193 static int comp_page_entry(const void *pa, const void *pb, void *p)
194 {
195 (void) p;
196 return (int) (((const PAGEINFO *) pa)->pagenum - ((const PAGEINFO *) pb)->pagenum);
197 }
198
199 static int comp_segment_entry(const void *pa, const void *pb, void *p)
200 {
201 (void) p;
202 return (int) (((const SEGINFO *) pa)->segnum - ((const SEGINFO *) pb)->segnum);
203 }
204
205 static int ygetc(FILE * stream)
206 {
207 int c = getc(stream);
208 if (c < 0) {
209 if (c == EOF)
210 normal_error("readjbig2","premature end file");
211 else
212 normal_error("readjbig2","can't happen");
213 }
214 return c;
215 }
216
217 static void initlinkedlist(LIST * lp)
218 {
219 lp->first = NULL;
220 lp->last = NULL;
221 lp->tree = NULL;
222 }
223
224 static LIST *litem_append(LIST * lp)
225 {
226 LITEM *ip;
227 ip = xtalloc(1, LITEM);
228 if (lp->first == NULL) {
229 lp->first = ip;
230 ip->prev = NULL;
231 } else {
232 lp->last->next = ip;
233 ip->prev = lp->last;
234 }
235 lp->last = ip;
236 ip->next = NULL;
237 ip->d = NULL;
238 return lp;
239 }
240
241 static FILEINFO *new_fileinfo(void)
242 {
243 FILEINFO *fip;
244 fip = xtalloc(1, FILEINFO);
245 fip->file = NULL;
246 fip->filepath = NULL;
247 fip->filesize = 0;
248 initlinkedlist(&(fip->pages));
249 initlinkedlist(&(fip->page0));
250 fip->filehdrflags = 0;
251 fip->sequentialaccess = false;
252 fip->numofpages = 0;
253 fip->streamstart = 0;
254 fip->pdfpage0objnum = 0;
255 fip->phase = INITIAL;
256 return fip;
257 }
258
259 static PAGEINFO *new_pageinfo(void)
260 {
261 PAGEINFO *pip;
262 pip = xtalloc(1, PAGEINFO);
263 initlinkedlist(&(pip->segments));
264 pip->pagenum = 0;
265 pip->width = 0;
266 pip->height = 0;
267 pip->xres = 0;
268 pip->yres = 0;
269 pip->pagesegmentflags = 0;
270 pip->stripinginfo = 0;
271 pip->stripedheight = 0;
272 return pip;
273 }
274
275 static void init_seginfo(SEGINFO * sip)
276 {
277 sip->segnum = 0;
278 sip->isrefered = false;
279 sip->refers = false;
280 sip->seghdrflags = 0;
281 sip->pageassocsizeflag = false;
282 sip->reftosegcount = 0;
283 sip->countofrefered = 0;
284 sip->fieldlen = 0;
285 sip->segnumwidth = 0;
286 sip->segpage = 0;
287 sip->segdatalen = 0;
288 sip->hdrstart = 0;
289 sip->hdrend = 0;
290 sip->datastart = 0;
291 sip->dataend = 0;
292 sip->endofstripeflag = false;
293 sip->endofpageflag = false;
294 sip->pageinfoflag = false;
295 sip->endoffileflag = false;
296 }
297
298 static void pages_maketree(LIST * plp)
299 {
300 LITEM *ip;
301 void **aa;
302 assert(plp->tree == NULL);
303 plp->tree = avl_create(comp_page_entry, NULL, &avl_xallocator);
304 assert(plp->tree != NULL);
305 for (ip = plp->first; ip != NULL; ip = ip->next) {
306 aa = avl_probe(plp->tree, (PAGEINFO *) ip->d);
307 assert(aa != NULL);
308 }
309 }
310
311 static void segments_maketree(LIST * slp)
312 {
313 LITEM *ip;
314 void **aa;
315 assert(slp->tree == NULL);
316 slp->tree = avl_create(comp_segment_entry, NULL, &avl_xallocator);
317 assert(slp->tree != NULL);
318 for (ip = slp->first; ip != NULL; ip = ip->next) {
319 aa = avl_probe(slp->tree, (SEGINFO *) ip->d);
320 assert(aa != NULL);
321 }
322 }
323
324 static PAGEINFO *find_pageinfo(LIST * plp, unsigned long pagenum)
325 {
326 PAGEINFO tmp;
327 tmp.pagenum = pagenum;
328 assert(plp->tree != NULL);
329 return (PAGEINFO *) avl_find(plp->tree, &tmp);
330 }
331
332 static SEGINFO *find_seginfo(LIST * slp, unsigned long segnum)
333 {
334 SEGINFO tmp;
335 tmp.segnum = segnum;
336 assert(slp->tree != NULL);
337 return (SEGINFO *) avl_find(slp->tree, &tmp);
338 }
339
340 unsigned int read2bytes(FILE * f)
341 {
342 unsigned int c = (unsigned int) ygetc(f);
343 return (c << 8) + (unsigned int) ygetc(f);
344 }
345
346 unsigned int read4bytes(FILE * f)
347 {
348 unsigned int l = read2bytes(f);
349 return (l << 16) + read2bytes(f);
350 }
351
352 static unsigned long getstreamlen(LITEM * slip, boolean refer)
353 {
354 SEGINFO *sip;
355 unsigned long len = 0;
356 for (; slip != NULL; slip = slip->next) {
357 sip = slip->d;
358 if (refer || sip->isrefered)
359 len += sip->hdrend - sip->hdrstart + sip->dataend - sip->datastart;
360 }
361 return len;
362 }
363
364 static void readfilehdr(FILEINFO * fip)
365 {
366 unsigned int i;
367 /*tex Annex D.4: File header syntax */
368 /*tex Annex D.4.1: ID string */
369 unsigned char jbig2_id[] = { 0x97, 'J', 'B', '2', 0x0d, 0x0a, 0x1a, 0x0a };
370 xfseek(fip->file, 0, SEEK_SET, fip->filepath);
371 for (i = 0; i < 8; i++)
372 if (ygetc(fip->file) != jbig2_id[i])
373 normal_error("readjbig2","ID string missing");
374 /*tex Annex D.4.2: File header flags */
375 fip->filehdrflags = (unsigned int) ygetc(fip->file);
376 fip->sequentialaccess = (fip->filehdrflags & 0x01) ? true : false;
377 if (fip->sequentialaccess) {
378 /*tex Annex D.1 vs. Annex D.2 */
379 xfseek(fip->file, 0, SEEK_END, fip->filepath);
380 fip->filesize = (long) xftello(fip->file, fip->filepath);
381 xfseek(fip->file, 9, SEEK_SET, fip->filepath);
382 }
383 /*tex Annex D.4.3: Number of pages */
384 if (( !(fip->filehdrflags >> 1)) & 0x01) {
385 /*tex The known number of pages: */
386 fip->numofpages = read4bytes(fip->file);
387 }
388 /*tex End of file header */
389 }
390
391 static void checkseghdrflags(SEGINFO * sip)
392 {
393 sip->endofstripeflag = false;
394 sip->endofpageflag = false;
395 sip->pageinfoflag = false;
396 sip->endoffileflag = false;
397 /*tex Table 7.3: Segment types */
398 switch (sip->seghdrflags & 0x3f) {
399 case M_SymbolDictionary:
400 case M_IntermediateTextRegion:
401 case M_ImmediateTextRegion:
402 case M_ImmediateLosslessTextRegion:
403 case M_PatternDictionary:
404 case M_IntermediateHalftoneRegion:
405 case M_ImmediateHalftoneRegion:
406 case M_ImmediateLosslessHalftoneRegion:
407 case M_IntermediateGenericRegion:
408 case M_ImmediateGenericRegion:
409 case M_ImmediateLosslessGenericRegion:
410 case M_IntermediateGenericRefinementRegion:
411 case M_ImmediateGenericRefinementRegion:
412 case M_ImmediateLosslessGenericRefinementRegion:
413 break;
414 case M_PageInformation:
415 sip->pageinfoflag = true;
416 break;
417 case M_EndOfPage:
418 sip->endofpageflag = true;
419 break;
420 case M_EndOfStripe:
421 sip->endofstripeflag = true;
422 break;
423 case M_EndOfFile:
424 sip->endoffileflag = true;
425 break;
426 case M_Profiles:
427 case M_Tables:
428 case M_Extension:
429 break;
430 default:
431 normal_error("readjbig2","unknown segment type file");
432 break;
433 }
434 }
435
436 /*tex
437
438 For first reading of file; return value tells if header been read.
439
440 */
441
442 static boolean readseghdr(FILEINFO * fip, SEGINFO * sip)
443 {
444 unsigned int i;
445 sip->hdrstart = xftell(fip->file, fip->filepath);
446 if (fip->sequentialaccess && sip->hdrstart == (unsigned) fip->filesize) {
447 /*tex No endoffileflag is ok for sequential access. */
448 return false;
449 }
450 /*tex Table 7.2.2: Segment number */
451 sip->segnum = read4bytes(fip->file);
452 /*tex Table 7.2.3: Segment header flags */
453 sip->seghdrflags = (unsigned int) ygetc(fip->file);
454 checkseghdrflags(sip);
455 if (fip->sequentialaccess && sip->endoffileflag) {
456 /*
457 Accept shorter segment, makes it compliant with Example 3.4 of
458 PDFRef. 5th ed.
459 */
460 return true;
461 }
462 sip->pageassocsizeflag = ((sip->seghdrflags >> 6) & 0x01) ? true : false;
463 /*tex Table 7.2.4: Referred-to segment count and retention flags */
464 sip->reftosegcount = (unsigned int) ygetc(fip->file);
465 sip->countofrefered = sip->reftosegcount >> 5;
466 if (sip->countofrefered < 5)
467 sip->fieldlen = 1;
468 else {
469 sip->fieldlen = 5 + sip->countofrefered / 8;
470 xfseek(fip->file, sip->fieldlen - 1, SEEK_CUR, fip->filepath);
471 }
472 /*tex Table 7.2.5: Referred-to segment numbers */
473 if (sip->segnum <= 256)
474 sip->segnumwidth = 1;
475 else if (sip->segnum <= 65536)
476 sip->segnumwidth = 2;
477 else
478 sip->segnumwidth = 4;
479 for (i = 0; i < sip->countofrefered; i++) {
480 switch (sip->segnumwidth) {
481 case 1:
482 (void) ygetc(fip->file);
483 break;
484 case 2:
485 (void) read2bytes(fip->file);
486 break;
487 case 4:
488 (void) read4bytes(fip->file);
489 break;
490 }
491 }
492 /*tex Table 7.2.6: Segment page association */
493 if (sip->pageassocsizeflag)
494 sip->segpage = read4bytes(fip->file);
495 else
496 sip->segpage = ygetc(fip->file);
497 /*tex Table 7.2.7: Segment data length */
498 sip->segdatalen = read4bytes(fip->file);
499 sip->hdrend = (unsigned long) xftello(fip->file, fip->filepath);
500 /*tex End of segment header. */
501 return true;
502 }
503
504 static void checkseghdr(FILEINFO * fip, SEGINFO * sip);
505
506 static void markpage0seg(FILEINFO * fip, unsigned long referedseg)
507 {
508 PAGEINFO *pip;
509 SEGINFO *sip;
510 pip = fip->page0.first->d;
511 sip = find_seginfo(&(pip->segments), referedseg);
512 if (sip != NULL) {
513 if (!sip->refers && sip->countofrefered > 0)
514 checkseghdr(fip, sip);
515 sip->isrefered = true;
516 }
517 }
518
519 /*tex
520
521 For writing, marks refered page0 segments, sets segpage larger than
522 zero to one.
523
524 */
525
526 static void writeseghdr(PDF pdf, FILEINFO * fip, SEGINFO * sip)
527 {
528 unsigned int i;
529 unsigned long referedseg = 0;
530 /*tex Table 7.2.2: Segment number */
531 /*tex Table 7.2.3: Segment header flags */
532 /*tex Table 7.2.4: Referred-to segment count and retention flags */
533 for (i = 0; i < 5 + sip->fieldlen; i++)
534 pdf_out(pdf, ygetc(fip->file));
535 /*tex Table 7.2.5: Referred-to segment numbers */
536 for (i = 0; i < sip->countofrefered; i++) {
537 switch (sip->segnumwidth) {
538 case 1:
539 referedseg = (unsigned long) ygetc(fip->file);
540 pdf_out(pdf, referedseg);
541 break;
542 case 2:
543 referedseg = read2bytes(fip->file);
544 pdf_out(pdf, (referedseg >> 8) & 0xff);
545 pdf_out(pdf, referedseg & 0xff);
546 break;
547 case 4:
548 referedseg = read4bytes(fip->file);
549 pdf_out(pdf, (referedseg >> 24) & 0xff);
550 pdf_out(pdf, (referedseg >> 16) & 0xff);
551 pdf_out(pdf, (referedseg >> 8) & 0xff);
552 pdf_out(pdf, referedseg & 0xff);
553 break;
554 }
555 if (fip->page0.last != NULL && !sip->refers)
556 markpage0seg(fip, referedseg);
557 }
558 if (sip->countofrefered > 0)
559 sip->refers = true;
560 /*tex Table 7.2.6: Segment page association */
561 if (sip->pageassocsizeflag)
562 for (i = 0; i < 3; i++) {
563 (void) ygetc(fip->file);
564 pdf_out(pdf, 0);
565 }
566 (void) ygetc(fip->file);
567 pdf_out(pdf, (unsigned char) ((sip->segpage > 0) ? 1 : 0));
568 /*tex Table 7.2.7: Segment data length */
569 for (i = 0; i < 4; i++)
570 pdf_out(pdf, ygetc(fip->file));
571 /* End of segment header. */
572 }
573
574 /*tex
575
576 For recursive marking of refered page0 segments:
577
578 */
579
580 static void checkseghdr(FILEINFO * fip, SEGINFO * sip)
581 {
582 unsigned int i;
583 unsigned long referedseg = 0;
584 /*tex Table 7.2.2: Segment number */
585 /*tex Table 7.2.3: Segment header flags */
586 /*tex Table 7.2.4: Referred-to segment count and retention flags */
587 xfseek(fip->file, 5 + sip->fieldlen, SEEK_CUR, fip->filepath);
588 /*tex Table 7.2.5: Referred-to segment numbers */
589 for (i = 0; i < sip->countofrefered; i++) {
590 switch (sip->segnumwidth) {
591 case 1:
592 referedseg = (unsigned long) ygetc(fip->file);
593 break;
594 case 2:
595 referedseg = read2bytes(fip->file);
596 break;
597 case 4:
598 referedseg = read4bytes(fip->file);
599 break;
600 }
601 if (!sip->refers)
602 markpage0seg(fip, referedseg);
603 }
604 if (sip->countofrefered > 0)
605 sip->refers = true;
606 /*tex Table 7.2.6: Segment page association */
607 /*tex Table 7.2.7: Segment data length */
608 if (sip->pageassocsizeflag)
609 xfseek(fip->file, 8, SEEK_CUR, fip->filepath);
610 else
611 xfseek(fip->file, 5, SEEK_CUR, fip->filepath);
612 /*tex End of segment header. */
613 }
614
615 static unsigned long findstreamstart(FILEINFO * fip)
616 {
617 SEGINFO tmp;
618 /*tex Table D.2: Random-access organisation */
619 do {
620 /*tex Find random-access stream start. */
621 (void) readseghdr(fip, &tmp);
622 } while (!tmp.endoffileflag);
623 fip->streamstart = tmp.hdrend;
624 readfilehdr(fip);
625 return fip->streamstart;
626 }
627
628 static void rd_jbig2_info(FILEINFO * fip)
629 {
630 /*tex For sequential-access only: */
631 unsigned long seekdist = 0;
632 /*tex For random-access only: */
633 unsigned long streampos = 0;
634 unsigned long currentpage = 0;
635 boolean sipavail = false;
636 PAGEINFO *pip;
637 SEGINFO *sip = NULL;
638 LIST *plp, *slp;
639 fip->file = xfopen(fip->filepath, FOPEN_RBIN_MODE);
640 readfilehdr(fip);
641 if (!fip->sequentialaccess) {
642 /*tex Table D.2: Random-access organisation */
643 streampos = findstreamstart(fip);
644 }
645 while (true) {
646 /*tex Loop over segments: */
647 if (!sipavail) {
648 sip = xtalloc(1, SEGINFO);
649 sipavail = true;
650 }
651 init_seginfo(sip);
652 if (!readseghdr(fip, sip) || sip->endoffileflag)
653 break;
654 if (sip->segpage > 0) {
655 if (sip->segpage > (int) currentpage) {
656 plp = litem_append(&(fip->pages));
657 plp->last->d = new_pageinfo();
658 currentpage = (unsigned long) sip->segpage;
659 }
660 pip = fip->pages.last->d;
661 } else {
662 if (fip->page0.last == NULL) {
663 plp = litem_append(&(fip->page0));
664 plp->last->d = new_pageinfo();
665 }
666 pip = fip->page0.last->d;
667 }
668 if (!sip->endofpageflag) {
669 slp = litem_append(&(pip->segments));
670 slp->last->d = sip;
671 sipavail = false;
672 }
673 if (!fip->sequentialaccess)
674 sip->datastart = streampos;
675 else
676 sip->datastart = sip->hdrend;
677 sip->dataend = sip->datastart + sip->segdatalen;
678 if (!fip->sequentialaccess && (sip->pageinfoflag || sip->endofstripeflag))
679 xfseeko(fip->file, (off_t) sip->datastart, SEEK_SET, fip->filepath);
680 seekdist = sip->segdatalen;
681 /*tex Table 7.4.8: Page information segment syntax */
682 if (sip->pageinfoflag) {
683 pip->pagenum = (unsigned long) sip->segpage;
684 pip->width = read4bytes(fip->file);
685 pip->height = read4bytes(fip->file);
686 pip->xres = read4bytes(fip->file);
687 pip->yres = read4bytes(fip->file);
688 pip->pagesegmentflags = (unsigned) ygetc(fip->file);
689 /*tex Table 7.4.8.6: Page striping information */
690 pip->stripinginfo = read2bytes(fip->file);
691 seekdist -= 19;
692 }
693 if (sip->endofstripeflag) {
694 pip->stripedheight = read4bytes(fip->file);
695 seekdist -= 4;
696 }
697 if (!fip->sequentialaccess
698 && (sip->pageinfoflag || sip->endofstripeflag))
699 xfseeko(fip->file, (off_t) sip->hdrend, SEEK_SET, fip->filepath);
700 if (!fip->sequentialaccess)
701 streampos += sip->segdatalen;
702 if (fip->sequentialaccess)
703 xfseeko(fip->file, (off_t) seekdist, SEEK_CUR, fip->filepath);
704 if (sip->endofpageflag && currentpage && (pip->stripinginfo >> 15))
705 pip->height = pip->stripedheight;
706 }
707 fip->phase = HAVEINFO;
708 if (sipavail)
709 xfree(sip);
710 xfclose(fip->file, fip->filepath);
711 }
712
713 static void wr_jbig2(PDF pdf, image_dict * idict, FILEINFO * fip, unsigned long page)
714 {
715 LITEM *slip;
716 PAGEINFO *pip;
717 SEGINFO *sip;
718 unsigned long i;
719 if (page > 0) {
720 assert(idict != NULL);
721 pip = find_pageinfo(&(fip->pages), page);
722 assert(pip != NULL);
723 pdf_begin_obj(pdf, img_objnum(idict), OBJSTM_NEVER);
724 pdf_begin_dict(pdf);
725 pdf_dict_add_name(pdf, "Type", "XObject");
726 pdf_dict_add_name(pdf, "Subtype", "Image");
727 pdf_dict_add_img_filename(pdf, idict);
728 pdf_dict_add_int(pdf, "Width", pip->width);
729 pdf_dict_add_int(pdf, "Height", pip->height);
730 pdf_dict_add_name(pdf, "ColorSpace", "DeviceGray");
731 pdf_dict_add_int(pdf, "BitsPerComponent", 1);
732 pdf_dict_add_int(pdf, "Length", getstreamlen(pip->segments.first, true));
733 pdf_add_name(pdf, "Filter");
734 pdf_begin_array(pdf);
735 pdf_add_name(pdf, "JBIG2Decode");
736 pdf_end_array(pdf);
737 if (fip->page0.last != NULL) {
738 if (fip->pdfpage0objnum == 0) {
739 fip->pdfpage0objnum = (unsigned long) pdf_create_obj(pdf, obj_type_others, 0);
740 }
741 pdf_add_name(pdf, "DecodeParms");
742 pdf_begin_array(pdf);
743 pdf_begin_dict(pdf);
744 pdf_dict_add_ref(pdf, "JBIG2Globals", fip->pdfpage0objnum);
745 pdf_end_dict(pdf);
746 pdf_end_array(pdf);
747 }
748 pdf_end_dict(pdf);
749 } else {
750 assert(idict == NULL);
751 pip = find_pageinfo(&(fip->page0), page);
752 assert(pip != NULL);
753 pdf_begin_obj(pdf, (int) fip->pdfpage0objnum, OBJSTM_NEVER);
754 pdf_begin_dict(pdf);
755 pdf_dict_add_int(pdf, "Length", getstreamlen(pip->segments.first, false));
756 pdf_end_dict(pdf);
757 }
758 pdf_begin_stream(pdf);
759 fip->file = xfopen(fip->filepath, FOPEN_RBIN_MODE);
760 for (slip = pip->segments.first; slip != NULL; slip = slip->next) {
761 /*tex Loop over page segments. */
762 sip = slip->d;
763 if (sip->isrefered || page > 0) {
764 xfseeko(fip->file, (off_t) sip->hdrstart, SEEK_SET, fip->filepath);
765 /*tex Mark refered-to page 0 segments, change segpages > 1 to 1. */
766 writeseghdr(pdf, fip, sip);
767 xfseeko(fip->file, (off_t) sip->datastart, SEEK_SET, fip->filepath);
768 for (i = sip->datastart; i < sip->dataend; i++)
769 pdf_out(pdf, ygetc(fip->file));
770 }
771 }
772 pdf_end_stream(pdf);
773 pdf_end_obj(pdf);
774 xfclose(fip->file, fip->filepath);
775 }
776
777 boolean supported_jbig2(image_dict * idict)
778 {
779 if (img_pdfmajorversion(idict) < 2 && img_pdfminorversion(idict) < 4) {
780 normal_error("readjbig2","you need to generate at least PDF 1.4");
781 return false;
782 } else {
783 return true;
784 }
785 }
786
787 void flush_jbig2_info(image_dict * idict)
788 {
789 /*tex Todo (or not). */
790 }
791
792 void read_jbig2_info(image_dict * idict)
793 {
794 FILEINFO *fip, tmp;
795 PAGEINFO *pip;
796 /*tex Already set probably, see other |read_|. */
797 img_type(idict) = IMG_TYPE_JBIG2;
798 if (! supported_jbig2(idict)) {
799 /*tex Already an error seen? */
800 }
801 if (img_pagenum(idict) < 1) {
802 normal_error("readjbig2","page must be > 0");
803 }
804 if (file_tree == NULL) {
805 file_tree = avl_create(comp_file_entry, NULL, &avl_xallocator);
806 }
807 tmp.filepath = img_filepath(idict);
808 fip = (FILEINFO *) avl_find(file_tree, &tmp);
809 if (fip == NULL) {
810 fip = new_fileinfo();
811 fip->filepath = xstrdup(img_filepath(idict));
812 avl_probe(file_tree, fip);
813 }
814 if (fip->phase == INITIAL) {
815 rd_jbig2_info(fip);
816 pages_maketree(&(fip->pages));
817 if (fip->page0.last != NULL) {
818 pages_maketree(&(fip->page0));
819 pip = fip->page0.first->d;
820 segments_maketree(&(pip->segments));
821 }
822 }
823 pip = find_pageinfo(&(fip->pages), (unsigned long) img_pagenum(idict));
824 if (pip == NULL) {
825 formatted_error("readjbig2","page %d not found in image file",(int) img_pagenum(idict));
826 }
827 img_totalpages(idict) = (int) fip->numofpages;
828 img_xsize(idict) = (int) pip->width;
829 img_ysize(idict) = (int) pip->height;
830 img_xres(idict) = (int) (pip->xres * 0.0254 + 0.5);
831 img_yres(idict) = (int) (pip->yres * 0.0254 + 0.5);
832 img_colordepth(idict) = 1;
833 }
834
835 void write_jbig2(PDF pdf, image_dict * idict)
836 {
837 FILEINFO *fip, tmp;
838 PAGEINFO *pip;
839 tmp.filepath = img_filepath(idict);
840 fip = (FILEINFO *) avl_find(file_tree, &tmp);
841 /*tex Don't write before |rd_jbig2_info()| call. */
842 pip = find_pageinfo(&(fip->pages), (unsigned long) img_pagenum(idict));
843 assert(pip != NULL);
844 wr_jbig2(pdf, idict, fip, pip->pagenum);
845 img_file(idict) = NULL;
846 }
847
848 void flush_jbig2_page0_objects(PDF pdf)
849 {
850 FILEINFO *fip;
851 struct avl_traverser t;
852 if (file_tree != NULL) {
853 avl_t_init(&t, file_tree);
854 for (fip = avl_t_first(&t, file_tree); fip != NULL;
855 fip = avl_t_next(&t)) {
856 if (fip->page0.last != NULL)
857 /*tex |NULL|: page0 */
858 wr_jbig2(pdf, NULL, fip, 0);
859 }
860 }
861 }