comparison mupdf-source/thirdparty/jbig2dec/jbig2_mmr.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* Copyright (C) 2001-2023 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /*
17 jbig2dec
18 */
19
20 /* An implementation of MMR decoding. This is based on the
21 implementation in Fitz, which in turn is based on the one
22 in Ghostscript.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "os_types.h"
29
30 #include <assert.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "jbig2.h"
36 #include "jbig2_priv.h"
37 #include "jbig2_arith.h"
38 #include "jbig2_generic.h"
39 #include "jbig2_image.h"
40 #include "jbig2_mmr.h"
41 #include "jbig2_segment.h"
42
43 typedef struct {
44 uint32_t width;
45 uint32_t height;
46 const byte *data;
47 size_t size;
48 size_t consumed_bits;
49 uint32_t data_index;
50 uint32_t bit_index;
51 uint32_t word;
52 } Jbig2MmrCtx;
53
54 #define MINUS1 UINT32_MAX
55 #define ERROR -1
56 #define ZEROES -2
57 #define UNCOMPRESSED -3
58
59 static void
60 jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
61 {
62 mmr->width = width;
63 mmr->height = height;
64 mmr->data = data;
65 mmr->size = size;
66 mmr->data_index = 0;
67 mmr->bit_index = 32;
68 mmr->word = 0;
69 mmr->consumed_bits = 0;
70
71 while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
72 mmr->bit_index -= 8;
73 mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
74 mmr->data_index++;
75 }
76 }
77
78 static void
79 jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
80 {
81 mmr->consumed_bits += n_bits;
82 if (mmr->consumed_bits > mmr->size * 8)
83 mmr->consumed_bits = mmr->size * 8;
84
85 mmr->word <<= n_bits;
86 mmr->bit_index += n_bits;
87 while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
88 mmr->bit_index -= 8;
89 mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
90 mmr->data_index++;
91 }
92 }
93
94 /*
95 <raph> the first 2^(initialbits) entries map bit patterns to decodes
96 <raph> let's say initial_bits is 8 for the sake of example
97 <raph> and that the code is 1001
98 <raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
99 <raph> because those are all the bytes that start with the code
100 <raph> and the 4 is the length of the code
101 ... if (n_bits > initial_bits) ...
102 <raph> anyway, in that case, it basically points to a mini table
103 <raph> the n_bits is the maximum length of all codes beginning with that byte
104 <raph> so 2^(n_bits - initial_bits) is the size of the mini-table
105 <raph> peter came up with this, and it makes sense
106 */
107
108 typedef struct {
109 short val;
110 short n_bits;
111 } mmr_table_node;
112
113 /* white decode table (runlength huffman codes) */
114 const mmr_table_node jbig2_mmr_white_decode[] = {
115 {256, 12},
116 {272, 12},
117 {29, 8},
118 {30, 8},
119 {45, 8},
120 {46, 8},
121 {22, 7},
122 {22, 7},
123 {23, 7},
124 {23, 7},
125 {47, 8},
126 {48, 8},
127 {13, 6},
128 {13, 6},
129 {13, 6},
130 {13, 6},
131 {20, 7},
132 {20, 7},
133 {33, 8},
134 {34, 8},
135 {35, 8},
136 {36, 8},
137 {37, 8},
138 {38, 8},
139 {19, 7},
140 {19, 7},
141 {31, 8},
142 {32, 8},
143 {1, 6},
144 {1, 6},
145 {1, 6},
146 {1, 6},
147 {12, 6},
148 {12, 6},
149 {12, 6},
150 {12, 6},
151 {53, 8},
152 {54, 8},
153 {26, 7},
154 {26, 7},
155 {39, 8},
156 {40, 8},
157 {41, 8},
158 {42, 8},
159 {43, 8},
160 {44, 8},
161 {21, 7},
162 {21, 7},
163 {28, 7},
164 {28, 7},
165 {61, 8},
166 {62, 8},
167 {63, 8},
168 {0, 8},
169 {320, 8},
170 {384, 8},
171 {10, 5},
172 {10, 5},
173 {10, 5},
174 {10, 5},
175 {10, 5},
176 {10, 5},
177 {10, 5},
178 {10, 5},
179 {11, 5},
180 {11, 5},
181 {11, 5},
182 {11, 5},
183 {11, 5},
184 {11, 5},
185 {11, 5},
186 {11, 5},
187 {27, 7},
188 {27, 7},
189 {59, 8},
190 {60, 8},
191 {288, 9},
192 {290, 9},
193 {18, 7},
194 {18, 7},
195 {24, 7},
196 {24, 7},
197 {49, 8},
198 {50, 8},
199 {51, 8},
200 {52, 8},
201 {25, 7},
202 {25, 7},
203 {55, 8},
204 {56, 8},
205 {57, 8},
206 {58, 8},
207 {192, 6},
208 {192, 6},
209 {192, 6},
210 {192, 6},
211 {1664, 6},
212 {1664, 6},
213 {1664, 6},
214 {1664, 6},
215 {448, 8},
216 {512, 8},
217 {292, 9},
218 {640, 8},
219 {576, 8},
220 {294, 9},
221 {296, 9},
222 {298, 9},
223 {300, 9},
224 {302, 9},
225 {256, 7},
226 {256, 7},
227 {2, 4},
228 {2, 4},
229 {2, 4},
230 {2, 4},
231 {2, 4},
232 {2, 4},
233 {2, 4},
234 {2, 4},
235 {2, 4},
236 {2, 4},
237 {2, 4},
238 {2, 4},
239 {2, 4},
240 {2, 4},
241 {2, 4},
242 {2, 4},
243 {3, 4},
244 {3, 4},
245 {3, 4},
246 {3, 4},
247 {3, 4},
248 {3, 4},
249 {3, 4},
250 {3, 4},
251 {3, 4},
252 {3, 4},
253 {3, 4},
254 {3, 4},
255 {3, 4},
256 {3, 4},
257 {3, 4},
258 {3, 4},
259 {128, 5},
260 {128, 5},
261 {128, 5},
262 {128, 5},
263 {128, 5},
264 {128, 5},
265 {128, 5},
266 {128, 5},
267 {8, 5},
268 {8, 5},
269 {8, 5},
270 {8, 5},
271 {8, 5},
272 {8, 5},
273 {8, 5},
274 {8, 5},
275 {9, 5},
276 {9, 5},
277 {9, 5},
278 {9, 5},
279 {9, 5},
280 {9, 5},
281 {9, 5},
282 {9, 5},
283 {16, 6},
284 {16, 6},
285 {16, 6},
286 {16, 6},
287 {17, 6},
288 {17, 6},
289 {17, 6},
290 {17, 6},
291 {4, 4},
292 {4, 4},
293 {4, 4},
294 {4, 4},
295 {4, 4},
296 {4, 4},
297 {4, 4},
298 {4, 4},
299 {4, 4},
300 {4, 4},
301 {4, 4},
302 {4, 4},
303 {4, 4},
304 {4, 4},
305 {4, 4},
306 {4, 4},
307 {5, 4},
308 {5, 4},
309 {5, 4},
310 {5, 4},
311 {5, 4},
312 {5, 4},
313 {5, 4},
314 {5, 4},
315 {5, 4},
316 {5, 4},
317 {5, 4},
318 {5, 4},
319 {5, 4},
320 {5, 4},
321 {5, 4},
322 {5, 4},
323 {14, 6},
324 {14, 6},
325 {14, 6},
326 {14, 6},
327 {15, 6},
328 {15, 6},
329 {15, 6},
330 {15, 6},
331 {64, 5},
332 {64, 5},
333 {64, 5},
334 {64, 5},
335 {64, 5},
336 {64, 5},
337 {64, 5},
338 {64, 5},
339 {6, 4},
340 {6, 4},
341 {6, 4},
342 {6, 4},
343 {6, 4},
344 {6, 4},
345 {6, 4},
346 {6, 4},
347 {6, 4},
348 {6, 4},
349 {6, 4},
350 {6, 4},
351 {6, 4},
352 {6, 4},
353 {6, 4},
354 {6, 4},
355 {7, 4},
356 {7, 4},
357 {7, 4},
358 {7, 4},
359 {7, 4},
360 {7, 4},
361 {7, 4},
362 {7, 4},
363 {7, 4},
364 {7, 4},
365 {7, 4},
366 {7, 4},
367 {7, 4},
368 {7, 4},
369 {7, 4},
370 {7, 4},
371 {-2, 3},
372 {-2, 3},
373 {-1, 0},
374 {-1, 0},
375 {-1, 0},
376 {-1, 0},
377 {-1, 0},
378 {-1, 0},
379 {-1, 0},
380 {-1, 0},
381 {-1, 0},
382 {-1, 0},
383 {-1, 0},
384 {-1, 0},
385 {-1, 0},
386 {-3, 4},
387 {1792, 3},
388 {1792, 3},
389 {1984, 4},
390 {2048, 4},
391 {2112, 4},
392 {2176, 4},
393 {2240, 4},
394 {2304, 4},
395 {1856, 3},
396 {1856, 3},
397 {1920, 3},
398 {1920, 3},
399 {2368, 4},
400 {2432, 4},
401 {2496, 4},
402 {2560, 4},
403 {1472, 1},
404 {1536, 1},
405 {1600, 1},
406 {1728, 1},
407 {704, 1},
408 {768, 1},
409 {832, 1},
410 {896, 1},
411 {960, 1},
412 {1024, 1},
413 {1088, 1},
414 {1152, 1},
415 {1216, 1},
416 {1280, 1},
417 {1344, 1},
418 {1408, 1}
419 };
420
421 /* black decode table (runlength huffman codes) */
422 const mmr_table_node jbig2_mmr_black_decode[] = {
423 {128, 12},
424 {160, 13},
425 {224, 12},
426 {256, 12},
427 {10, 7},
428 {11, 7},
429 {288, 12},
430 {12, 7},
431 {9, 6},
432 {9, 6},
433 {8, 6},
434 {8, 6},
435 {7, 5},
436 {7, 5},
437 {7, 5},
438 {7, 5},
439 {6, 4},
440 {6, 4},
441 {6, 4},
442 {6, 4},
443 {6, 4},
444 {6, 4},
445 {6, 4},
446 {6, 4},
447 {5, 4},
448 {5, 4},
449 {5, 4},
450 {5, 4},
451 {5, 4},
452 {5, 4},
453 {5, 4},
454 {5, 4},
455 {1, 3},
456 {1, 3},
457 {1, 3},
458 {1, 3},
459 {1, 3},
460 {1, 3},
461 {1, 3},
462 {1, 3},
463 {1, 3},
464 {1, 3},
465 {1, 3},
466 {1, 3},
467 {1, 3},
468 {1, 3},
469 {1, 3},
470 {1, 3},
471 {4, 3},
472 {4, 3},
473 {4, 3},
474 {4, 3},
475 {4, 3},
476 {4, 3},
477 {4, 3},
478 {4, 3},
479 {4, 3},
480 {4, 3},
481 {4, 3},
482 {4, 3},
483 {4, 3},
484 {4, 3},
485 {4, 3},
486 {4, 3},
487 {3, 2},
488 {3, 2},
489 {3, 2},
490 {3, 2},
491 {3, 2},
492 {3, 2},
493 {3, 2},
494 {3, 2},
495 {3, 2},
496 {3, 2},
497 {3, 2},
498 {3, 2},
499 {3, 2},
500 {3, 2},
501 {3, 2},
502 {3, 2},
503 {3, 2},
504 {3, 2},
505 {3, 2},
506 {3, 2},
507 {3, 2},
508 {3, 2},
509 {3, 2},
510 {3, 2},
511 {3, 2},
512 {3, 2},
513 {3, 2},
514 {3, 2},
515 {3, 2},
516 {3, 2},
517 {3, 2},
518 {3, 2},
519 {2, 2},
520 {2, 2},
521 {2, 2},
522 {2, 2},
523 {2, 2},
524 {2, 2},
525 {2, 2},
526 {2, 2},
527 {2, 2},
528 {2, 2},
529 {2, 2},
530 {2, 2},
531 {2, 2},
532 {2, 2},
533 {2, 2},
534 {2, 2},
535 {2, 2},
536 {2, 2},
537 {2, 2},
538 {2, 2},
539 {2, 2},
540 {2, 2},
541 {2, 2},
542 {2, 2},
543 {2, 2},
544 {2, 2},
545 {2, 2},
546 {2, 2},
547 {2, 2},
548 {2, 2},
549 {2, 2},
550 {2, 2},
551 {-2, 4},
552 {-2, 4},
553 {-1, 0},
554 {-1, 0},
555 {-1, 0},
556 {-1, 0},
557 {-1, 0},
558 {-1, 0},
559 {-1, 0},
560 {-1, 0},
561 {-1, 0},
562 {-1, 0},
563 {-1, 0},
564 {-1, 0},
565 {-1, 0},
566 {-3, 5},
567 {1792, 4},
568 {1792, 4},
569 {1984, 5},
570 {2048, 5},
571 {2112, 5},
572 {2176, 5},
573 {2240, 5},
574 {2304, 5},
575 {1856, 4},
576 {1856, 4},
577 {1920, 4},
578 {1920, 4},
579 {2368, 5},
580 {2432, 5},
581 {2496, 5},
582 {2560, 5},
583 {18, 3},
584 {18, 3},
585 {18, 3},
586 {18, 3},
587 {18, 3},
588 {18, 3},
589 {18, 3},
590 {18, 3},
591 {52, 5},
592 {52, 5},
593 {640, 6},
594 {704, 6},
595 {768, 6},
596 {832, 6},
597 {55, 5},
598 {55, 5},
599 {56, 5},
600 {56, 5},
601 {1280, 6},
602 {1344, 6},
603 {1408, 6},
604 {1472, 6},
605 {59, 5},
606 {59, 5},
607 {60, 5},
608 {60, 5},
609 {1536, 6},
610 {1600, 6},
611 {24, 4},
612 {24, 4},
613 {24, 4},
614 {24, 4},
615 {25, 4},
616 {25, 4},
617 {25, 4},
618 {25, 4},
619 {1664, 6},
620 {1728, 6},
621 {320, 5},
622 {320, 5},
623 {384, 5},
624 {384, 5},
625 {448, 5},
626 {448, 5},
627 {512, 6},
628 {576, 6},
629 {53, 5},
630 {53, 5},
631 {54, 5},
632 {54, 5},
633 {896, 6},
634 {960, 6},
635 {1024, 6},
636 {1088, 6},
637 {1152, 6},
638 {1216, 6},
639 {64, 3},
640 {64, 3},
641 {64, 3},
642 {64, 3},
643 {64, 3},
644 {64, 3},
645 {64, 3},
646 {64, 3},
647 {13, 1},
648 {13, 1},
649 {13, 1},
650 {13, 1},
651 {13, 1},
652 {13, 1},
653 {13, 1},
654 {13, 1},
655 {13, 1},
656 {13, 1},
657 {13, 1},
658 {13, 1},
659 {13, 1},
660 {13, 1},
661 {13, 1},
662 {13, 1},
663 {23, 4},
664 {23, 4},
665 {50, 5},
666 {51, 5},
667 {44, 5},
668 {45, 5},
669 {46, 5},
670 {47, 5},
671 {57, 5},
672 {58, 5},
673 {61, 5},
674 {256, 5},
675 {16, 3},
676 {16, 3},
677 {16, 3},
678 {16, 3},
679 {17, 3},
680 {17, 3},
681 {17, 3},
682 {17, 3},
683 {48, 5},
684 {49, 5},
685 {62, 5},
686 {63, 5},
687 {30, 5},
688 {31, 5},
689 {32, 5},
690 {33, 5},
691 {40, 5},
692 {41, 5},
693 {22, 4},
694 {22, 4},
695 {14, 1},
696 {14, 1},
697 {14, 1},
698 {14, 1},
699 {14, 1},
700 {14, 1},
701 {14, 1},
702 {14, 1},
703 {14, 1},
704 {14, 1},
705 {14, 1},
706 {14, 1},
707 {14, 1},
708 {14, 1},
709 {14, 1},
710 {14, 1},
711 {15, 2},
712 {15, 2},
713 {15, 2},
714 {15, 2},
715 {15, 2},
716 {15, 2},
717 {15, 2},
718 {15, 2},
719 {128, 5},
720 {192, 5},
721 {26, 5},
722 {27, 5},
723 {28, 5},
724 {29, 5},
725 {19, 4},
726 {19, 4},
727 {20, 4},
728 {20, 4},
729 {34, 5},
730 {35, 5},
731 {36, 5},
732 {37, 5},
733 {38, 5},
734 {39, 5},
735 {21, 4},
736 {21, 4},
737 {42, 5},
738 {43, 5},
739 {0, 3},
740 {0, 3},
741 {0, 3},
742 {0, 3}
743 };
744
745 #define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
746
747 /* On platforms that enforce aligned memory accesses, we can't just
748 * cast the byte * to the type of object we are accessing, we have
749 * unpack the requisite number of bytes, and deal with it that way.
750 * Note that the comments below about being 16/32 bit boundaries
751 * is referring to offsets into the byte stream, *not* memory
752 * addresses.
753 */
754 #define getword16(b) ((uint16_t)(b[0] | (b[1] << 8)))
755 #define getword32(b) ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))
756
757 static uint32_t
758 jbig2_find_changing_element(const byte *line, uint32_t x, uint32_t w)
759 {
760 int a;
761 uint8_t all8;
762 uint16_t all16;
763 uint32_t all32;
764
765 if (line == NULL)
766 return w;
767
768 if (x == MINUS1) {
769 a = 0;
770 x = 0;
771 } else if (x < w) {
772 a = getbit(line, x);
773 x++;
774 } else {
775 return x;
776 }
777
778 /* We will be looking for a uint8 or uint16 or uint32 that has at least one
779 bit different from <a>, so prepare some useful values for comparison. */
780 all8 = (a) ? 0xff : 0;
781 all16 = (a) ? 0xffff : 0;
782 all32 = (a) ? 0xffffffff : 0;
783
784 /* Check individual bits up to next 8-bit boundary.
785
786 [Would it be worth looking at top 4 bits, then at 2 bits then at 1 bit,
787 instead of iterating over all 8 bits? */
788
789 if ( ((uint8_t*) line)[ x / 8] == all8) {
790 /* Don't bother checking individual bits if the enclosing uint8 equals
791 all8 - just move to the next byte. */
792 x = x / 8 * 8 + 8;
793 if (x >= w) {
794 x = w;
795 goto end;
796 }
797 } else {
798 for(;;) {
799 if (x == w) {
800 goto end;
801 }
802 if (x % 8 == 0) {
803 break;
804 }
805 if (getbit(line, x) != a) {
806 goto end;
807 }
808 x += 1;
809 }
810 }
811
812 assert(x % 8 == 0);
813 /* Check next uint8 if we are not on 16-bit boundary. */
814 if (x % 16) {
815 if (w - x < 8) {
816 goto check1;
817 }
818 if ( ((uint8_t*) line)[ x / 8] != all8) {
819 goto check1;
820 }
821 x += 8; /* This will make x a multiple of 16. */
822 }
823
824 assert(x % 16 == 0);
825 /* Check next uint16 if we are not on 32-bit boundary. */
826 if (x % 32) {
827 if (w - x < 16) {
828 goto check8;
829 }
830 if ( getword16((line + (x / 8))) != all16) {
831 goto check8_no_eof;
832 }
833 x += 16; /* This will make x a multiple of 32. */
834 }
835
836 /* We are now on a 32-bit boundary. Check uint32's until we reach last
837 sub-32-bit region. */
838 assert(x % 32 == 0);
839 for(;;) {
840 if (w - x < 32) {
841 /* We could still look at the uint32 here - if it equals all32, we
842 know there is no match before <w> so could do {x = w; goto end;}.
843
844 But for now we simply fall into the epilogue checking, which will
845 look at the next uint16, then uint8, then last 8 bits. */
846 goto check16;
847 }
848 if ( getword32((line + (x / 8))) != all32) {
849 goto check16_no_eof;
850 }
851 x += 32;
852 }
853
854 /* Check next uint16. */
855 check16:
856 assert(x % 16 == 0);
857 if (w - x < 16) {
858 goto check8;
859 }
860 check16_no_eof:
861 assert(w - x >= 16);
862 if ( getword16((line + (x / 8))) != all16) {
863 goto check8_no_eof;
864 }
865 x += 16;
866
867 /* Check next uint8. */
868 check8:
869 assert(x % 8 == 0);
870 if (w - x < 8) {
871 goto check1;
872 }
873 check8_no_eof:
874 assert(w - x >= 8);
875 if ( ((uint8_t*) line)[x/8] != all8) {
876 goto check1;
877 }
878 x += 8;
879
880 /* Check up to the next 8 bits. */
881 check1:
882 assert(x % 8 == 0);
883 if ( ((uint8_t*) line)[ x / 8] == all8) {
884 x = w;
885 goto end;
886 }
887 {
888 for(;;) {
889 if (x == w) {
890 goto end;
891 }
892 if (getbit(line, x) != a) {
893 goto end;
894 }
895 x += 1;
896 }
897 }
898
899 end:
900 return x;
901 }
902
903 #undef getword16
904 #undef getword32
905
906 static uint32_t
907 jbig2_find_changing_element_of_color(const byte *line, uint32_t x, uint32_t w, int color)
908 {
909 if (line == NULL)
910 return w;
911 x = jbig2_find_changing_element(line, x, w);
912 if (x < w && getbit(line, x) != color)
913 x = jbig2_find_changing_element(line, x, w);
914 return x;
915 }
916
917 static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
918 static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
919
920 static void
921 jbig2_set_bits(byte *line, uint32_t x0, uint32_t x1)
922 {
923 uint32_t a0, a1, b0, b1, a;
924
925 a0 = x0 >> 3;
926 a1 = x1 >> 3;
927
928 b0 = x0 & 7;
929 b1 = x1 & 7;
930
931 if (a0 == a1) {
932 line[a0] |= lm[b0] & rm[b1];
933 } else {
934 line[a0] |= lm[b0];
935 for (a = a0 + 1; a < a1; a++)
936 line[a] = 0xFF;
937 if (b1)
938 line[a1] |= rm[b1];
939 }
940 }
941
942 static int
943 jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
944 {
945 uint32_t word = mmr->word;
946 int table_ix = word >> (32 - initial_bits);
947 int val = table[table_ix].val;
948 int n_bits = table[table_ix].n_bits;
949
950 if (n_bits > initial_bits) {
951 int mask = (1 << (32 - initial_bits)) - 1;
952
953 table_ix = val + ((word & mask) >> (32 - n_bits));
954 val = table[table_ix].val;
955 n_bits = initial_bits + table[table_ix].n_bits;
956 }
957
958 jbig2_decode_mmr_consume(mmr, n_bits);
959
960 return val;
961 }
962
963 static int
964 jbig2_decode_get_run(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
965 {
966 int result = 0;
967 int val;
968
969 do {
970 val = jbig2_decode_get_code(mmr, table, initial_bits);
971 if (val == ERROR)
972 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "invalid code detected in MMR-coded data");
973 else if (val == UNCOMPRESSED)
974 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "uncompressed code in MMR-coded data");
975 else if (val == ZEROES)
976 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zeroes code in MMR-coded data");
977 result += val;
978 } while (val >= 64);
979
980 return result;
981 }
982
983 static int
984 jbig2_decode_mmr_line(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const byte *ref, byte *dst, int *eofb)
985 {
986 uint32_t a0 = MINUS1;
987 uint32_t a1, a2, b1, b2;
988 int c = 0; /* 0 is white, black is 1 */
989
990 while (1) {
991 uint32_t word = mmr->word;
992
993 /* printf ("%08x\n", word); */
994
995 if (a0 != MINUS1 && a0 >= mmr->width)
996 break;
997
998 if ((word >> (32 - 3)) == 1) {
999 int white_run, black_run;
1000
1001 jbig2_decode_mmr_consume(mmr, 3);
1002
1003 if (a0 == MINUS1)
1004 a0 = 0;
1005
1006 if (c == 0) {
1007 white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
1008 if (white_run < 0)
1009 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
1010 black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
1011 if (black_run < 0)
1012 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
1013 /* printf ("H %d %d\n", white_run, black_run); */
1014 a1 = a0 + white_run;
1015 a2 = a1 + black_run;
1016 if (a1 > mmr->width)
1017 a1 = mmr->width;
1018 if (a2 > mmr->width)
1019 a2 = mmr->width;
1020 if (a2 < a1) {
1021 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative black H run");
1022 a2 = a1;
1023 }
1024 if (a1 < mmr->width)
1025 jbig2_set_bits(dst, a1, a2);
1026 a0 = a2;
1027 } else {
1028 black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
1029 if (black_run < 0)
1030 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
1031 white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
1032 if (white_run < 0)
1033 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
1034 /* printf ("H %d %d\n", black_run, white_run); */
1035 a1 = a0 + black_run;
1036 a2 = a1 + white_run;
1037 if (a1 > mmr->width)
1038 a1 = mmr->width;
1039 if (a2 > mmr->width)
1040 a2 = mmr->width;
1041 if (a1 < a0) {
1042 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative white H run");
1043 a1 = a0;
1044 }
1045 if (a0 < mmr->width)
1046 jbig2_set_bits(dst, a0, a1);
1047 a0 = a2;
1048 }
1049 }
1050
1051 else if ((word >> (32 - 4)) == 1) {
1052 /* printf ("P\n"); */
1053 jbig2_decode_mmr_consume(mmr, 4);
1054 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1055 b2 = jbig2_find_changing_element(ref, b1, mmr->width);
1056 if (c) {
1057 if (b2 < a0) {
1058 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative P run");
1059 b2 = a0;
1060 }
1061 if (a0 < mmr->width)
1062 jbig2_set_bits(dst, a0, b2);
1063 }
1064 a0 = b2;
1065 }
1066
1067 else if ((word >> (32 - 1)) == 1) {
1068 /* printf ("V(0)\n"); */
1069 jbig2_decode_mmr_consume(mmr, 1);
1070 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1071 if (c) {
1072 if (b1 < a0) {
1073 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative V(0) run");
1074 b1 = a0;
1075 }
1076 if (a0 < mmr->width)
1077 jbig2_set_bits(dst, a0, b1);
1078 }
1079 a0 = b1;
1080 c = !c;
1081 }
1082
1083 else if ((word >> (32 - 3)) == 3) {
1084 /* printf ("VR(1)\n"); */
1085 jbig2_decode_mmr_consume(mmr, 3);
1086 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1087 if (b1 + 1 <= mmr->width)
1088 b1 += 1;
1089 if (c) {
1090 if (b1 < a0) {
1091 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(1) run");
1092 b1 = a0;
1093 }
1094 if (a0 < mmr->width)
1095 jbig2_set_bits(dst, a0, b1);
1096 }
1097 a0 = b1;
1098 c = !c;
1099 }
1100
1101 else if ((word >> (32 - 6)) == 3) {
1102 /* printf ("VR(2)\n"); */
1103 jbig2_decode_mmr_consume(mmr, 6);
1104 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1105 if (b1 + 2 <= mmr->width)
1106 b1 += 2;
1107 if (c) {
1108 if (b1 < a0) {
1109 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(2) run");
1110 b1 = a0;
1111 }
1112 if (a0 < mmr->width)
1113 jbig2_set_bits(dst, a0, b1);
1114 }
1115 a0 = b1;
1116 c = !c;
1117 }
1118
1119 else if ((word >> (32 - 7)) == 3) {
1120 /* printf ("VR(3)\n"); */
1121 jbig2_decode_mmr_consume(mmr, 7);
1122 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1123 if (b1 + 3 <= mmr->width)
1124 b1 += 3;
1125 if (c) {
1126 if (b1 < a0) {
1127 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(3) run");
1128 b1 = a0;
1129 }
1130 if (a0 < mmr->width)
1131 jbig2_set_bits(dst, a0, b1);
1132 }
1133 a0 = b1;
1134 c = !c;
1135 }
1136
1137 else if ((word >> (32 - 3)) == 2) {
1138 /* printf ("VL(1)\n"); */
1139 jbig2_decode_mmr_consume(mmr, 3);
1140 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1141 if (b1 >= 1)
1142 b1 -= 1;
1143 if (c) {
1144 if (b1 < a0) {
1145 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(1) run");
1146 b1 = a0;
1147 }
1148 if (a0 < mmr->width)
1149 jbig2_set_bits(dst, a0, b1);
1150 }
1151 a0 = b1;
1152 c = !c;
1153 }
1154
1155 else if ((word >> (32 - 6)) == 2) {
1156 /* printf ("VL(2)\n"); */
1157 jbig2_decode_mmr_consume(mmr, 6);
1158 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1159 if (b1 >= 2)
1160 b1 -= 2;
1161 if (c) {
1162 if (b1 < a0) {
1163 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(2) run");
1164 b1 = a0;
1165 }
1166 if (a0 < mmr->width)
1167 jbig2_set_bits(dst, a0, b1);
1168 }
1169 a0 = b1;
1170 c = !c;
1171 }
1172
1173 else if ((word >> (32 - 7)) == 2) {
1174 /* printf ("VL(3)\n"); */
1175 jbig2_decode_mmr_consume(mmr, 7);
1176 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
1177 if (b1 >= 3)
1178 b1 -= 3;
1179 if (c) {
1180 if (b1 < a0) {
1181 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(3) run");
1182 b1 = a0;
1183 }
1184 if (a0 < mmr->width)
1185 jbig2_set_bits(dst, a0, b1);
1186 }
1187 a0 = b1;
1188 c = !c;
1189 }
1190
1191 else if ((word >> (32 - 24)) == 0x1001) {
1192 /* printf ("EOFB\n"); */
1193 jbig2_decode_mmr_consume(mmr, 24);
1194 *eofb = 1;
1195 break;
1196 }
1197
1198 else
1199 break;
1200 }
1201
1202 return 0;
1203 }
1204
1205 int
1206 jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image)
1207 {
1208 Jbig2MmrCtx mmr;
1209 const uint32_t rowstride = image->stride;
1210 byte *dst = image->data;
1211 byte *ref = NULL;
1212 uint32_t y;
1213 int code = 0;
1214 int eofb = 0;
1215
1216 jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
1217
1218 for (y = 0; !eofb && y < image->height; y++) {
1219 memset(dst, 0, rowstride);
1220 code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
1221 if (code < 0)
1222 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
1223 ref = dst;
1224 dst += rowstride;
1225 }
1226
1227 if (eofb && y < image->height) {
1228 memset(dst, 0, rowstride * (image->height - y));
1229 }
1230
1231 return code;
1232 }
1233
1234 /**
1235 * jbig2_decode_halftone_mmr: decode mmr region inside of halftones
1236 *
1237 * @ctx: jbig2 decoder context
1238 * @params: parameters for decoding
1239 * @data: pointer to text region data to be decoded
1240 * @size: length of text region data
1241 * @image: return of decoded image
1242 * @consumed_bytes: return of consumed bytes from @data
1243 *
1244 * MMR decoding that consumes EOFB and returns consumed bytes (@consumed_bytes)
1245 *
1246 * returns: 0
1247 **/
1248 int
1249 jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes)
1250 {
1251 Jbig2MmrCtx mmr;
1252 const uint32_t rowstride = image->stride;
1253 byte *dst = image->data;
1254 byte *ref = NULL;
1255 uint32_t y;
1256 int code = 0;
1257 const uint32_t EOFB = 0x001001;
1258 int eofb = 0;
1259
1260 jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
1261
1262 for (y = 0; !eofb && y < image->height; y++) {
1263 memset(dst, 0, rowstride);
1264 code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
1265 if (code < 0)
1266 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode halftone mmr line");
1267 ref = dst;
1268 dst += rowstride;
1269 }
1270
1271 if (eofb && y < image->height) {
1272 memset(dst, 0, rowstride * (image->height - y));
1273 }
1274
1275 /* test for EOFB (see section 6.2.6) */
1276 if (mmr.word >> 8 == EOFB) {
1277 jbig2_decode_mmr_consume(&mmr, 24);
1278 }
1279
1280 *consumed_bytes += (mmr.consumed_bits + 7) / 8;
1281 return code;
1282 }