summaryrefslogtreecommitdiff
path: root/jbig2dec/jbig2_mmr.c
blob: efea950b84fa64870ac4e05ce6fb0ce636c7a86d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/*
    jbig2dec
*/

/* An implementation of MMR decoding. This is based on the
   implementation in Fitz, which in turn is based on the one
   in Ghostscript.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_arith.h"
#include "jbig2_generic.h"
#include "jbig2_image.h"
#include "jbig2_mmr.h"
#include "jbig2_segment.h"

typedef struct {
    uint32_t width;
    uint32_t height;
    const byte *data;
    size_t size;
    size_t consumed_bits;
    uint32_t data_index;
    uint32_t bit_index;
    uint32_t word;
} Jbig2MmrCtx;

#define MINUS1 UINT32_MAX
#define ERROR -1
#define ZEROES -2
#define UNCOMPRESSED -3

static void
jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
{
    mmr->width = width;
    mmr->height = height;
    mmr->data = data;
    mmr->size = size;
    mmr->data_index = 0;
    mmr->bit_index = 32;
    mmr->word = 0;
    mmr->consumed_bits = 0;

    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
        mmr->bit_index -= 8;
        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
        mmr->data_index++;
    }
}

static void
jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
{
    mmr->consumed_bits += n_bits;
    if (mmr->consumed_bits > mmr->size * 8)
        mmr->consumed_bits = mmr->size * 8;

    mmr->word <<= n_bits;
    mmr->bit_index += n_bits;
    while (mmr->bit_index >= 8 && mmr->data_index < mmr->size) {
        mmr->bit_index -= 8;
        mmr->word |= (mmr->data[mmr->data_index] << mmr->bit_index);
        mmr->data_index++;
    }
}

/*
<raph> the first 2^(initialbits) entries map bit patterns to decodes
<raph> let's say initial_bits is 8 for the sake of example
<raph> and that the code is 1001
<raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
<raph> because those are all the bytes that start with the code
<raph> and the 4 is the length of the code
... if (n_bits > initial_bits) ...
<raph> anyway, in that case, it basically points to a mini table
<raph> the n_bits is the maximum length of all codes beginning with that byte
<raph> so 2^(n_bits - initial_bits) is the size of the mini-table
<raph> peter came up with this, and it makes sense
*/

typedef struct {
    short val;
    short n_bits;
} mmr_table_node;

/* white decode table (runlength huffman codes) */
const mmr_table_node jbig2_mmr_white_decode[] = {
    {256, 12},
    {272, 12},
    {29, 8},
    {30, 8},
    {45, 8},
    {46, 8},
    {22, 7},
    {22, 7},
    {23, 7},
    {23, 7},
    {47, 8},
    {48, 8},
    {13, 6},
    {13, 6},
    {13, 6},
    {13, 6},
    {20, 7},
    {20, 7},
    {33, 8},
    {34, 8},
    {35, 8},
    {36, 8},
    {37, 8},
    {38, 8},
    {19, 7},
    {19, 7},
    {31, 8},
    {32, 8},
    {1, 6},
    {1, 6},
    {1, 6},
    {1, 6},
    {12, 6},
    {12, 6},
    {12, 6},
    {12, 6},
    {53, 8},
    {54, 8},
    {26, 7},
    {26, 7},
    {39, 8},
    {40, 8},
    {41, 8},
    {42, 8},
    {43, 8},
    {44, 8},
    {21, 7},
    {21, 7},
    {28, 7},
    {28, 7},
    {61, 8},
    {62, 8},
    {63, 8},
    {0, 8},
    {320, 8},
    {384, 8},
    {10, 5},
    {10, 5},
    {10, 5},
    {10, 5},
    {10, 5},
    {10, 5},
    {10, 5},
    {10, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {11, 5},
    {27, 7},
    {27, 7},
    {59, 8},
    {60, 8},
    {288, 9},
    {290, 9},
    {18, 7},
    {18, 7},
    {24, 7},
    {24, 7},
    {49, 8},
    {50, 8},
    {51, 8},
    {52, 8},
    {25, 7},
    {25, 7},
    {55, 8},
    {56, 8},
    {57, 8},
    {58, 8},
    {192, 6},
    {192, 6},
    {192, 6},
    {192, 6},
    {1664, 6},
    {1664, 6},
    {1664, 6},
    {1664, 6},
    {448, 8},
    {512, 8},
    {292, 9},
    {640, 8},
    {576, 8},
    {294, 9},
    {296, 9},
    {298, 9},
    {300, 9},
    {302, 9},
    {256, 7},
    {256, 7},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {2, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {3, 4},
    {128, 5},
    {128, 5},
    {128, 5},
    {128, 5},
    {128, 5},
    {128, 5},
    {128, 5},
    {128, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {8, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {9, 5},
    {16, 6},
    {16, 6},
    {16, 6},
    {16, 6},
    {17, 6},
    {17, 6},
    {17, 6},
    {17, 6},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {4, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {14, 6},
    {14, 6},
    {14, 6},
    {14, 6},
    {15, 6},
    {15, 6},
    {15, 6},
    {15, 6},
    {64, 5},
    {64, 5},
    {64, 5},
    {64, 5},
    {64, 5},
    {64, 5},
    {64, 5},
    {64, 5},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {7, 4},
    {-2, 3},
    {-2, 3},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-3, 4},
    {1792, 3},
    {1792, 3},
    {1984, 4},
    {2048, 4},
    {2112, 4},
    {2176, 4},
    {2240, 4},
    {2304, 4},
    {1856, 3},
    {1856, 3},
    {1920, 3},
    {1920, 3},
    {2368, 4},
    {2432, 4},
    {2496, 4},
    {2560, 4},
    {1472, 1},
    {1536, 1},
    {1600, 1},
    {1728, 1},
    {704, 1},
    {768, 1},
    {832, 1},
    {896, 1},
    {960, 1},
    {1024, 1},
    {1088, 1},
    {1152, 1},
    {1216, 1},
    {1280, 1},
    {1344, 1},
    {1408, 1}
};

/* black decode table (runlength huffman codes) */
const mmr_table_node jbig2_mmr_black_decode[] = {
    {128, 12},
    {160, 13},
    {224, 12},
    {256, 12},
    {10, 7},
    {11, 7},
    {288, 12},
    {12, 7},
    {9, 6},
    {9, 6},
    {8, 6},
    {8, 6},
    {7, 5},
    {7, 5},
    {7, 5},
    {7, 5},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {6, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {5, 4},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {1, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {4, 3},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {3, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {2, 2},
    {-2, 4},
    {-2, 4},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-1, 0},
    {-3, 5},
    {1792, 4},
    {1792, 4},
    {1984, 5},
    {2048, 5},
    {2112, 5},
    {2176, 5},
    {2240, 5},
    {2304, 5},
    {1856, 4},
    {1856, 4},
    {1920, 4},
    {1920, 4},
    {2368, 5},
    {2432, 5},
    {2496, 5},
    {2560, 5},
    {18, 3},
    {18, 3},
    {18, 3},
    {18, 3},
    {18, 3},
    {18, 3},
    {18, 3},
    {18, 3},
    {52, 5},
    {52, 5},
    {640, 6},
    {704, 6},
    {768, 6},
    {832, 6},
    {55, 5},
    {55, 5},
    {56, 5},
    {56, 5},
    {1280, 6},
    {1344, 6},
    {1408, 6},
    {1472, 6},
    {59, 5},
    {59, 5},
    {60, 5},
    {60, 5},
    {1536, 6},
    {1600, 6},
    {24, 4},
    {24, 4},
    {24, 4},
    {24, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {25, 4},
    {1664, 6},
    {1728, 6},
    {320, 5},
    {320, 5},
    {384, 5},
    {384, 5},
    {448, 5},
    {448, 5},
    {512, 6},
    {576, 6},
    {53, 5},
    {53, 5},
    {54, 5},
    {54, 5},
    {896, 6},
    {960, 6},
    {1024, 6},
    {1088, 6},
    {1152, 6},
    {1216, 6},
    {64, 3},
    {64, 3},
    {64, 3},
    {64, 3},
    {64, 3},
    {64, 3},
    {64, 3},
    {64, 3},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {13, 1},
    {23, 4},
    {23, 4},
    {50, 5},
    {51, 5},
    {44, 5},
    {45, 5},
    {46, 5},
    {47, 5},
    {57, 5},
    {58, 5},
    {61, 5},
    {256, 5},
    {16, 3},
    {16, 3},
    {16, 3},
    {16, 3},
    {17, 3},
    {17, 3},
    {17, 3},
    {17, 3},
    {48, 5},
    {49, 5},
    {62, 5},
    {63, 5},
    {30, 5},
    {31, 5},
    {32, 5},
    {33, 5},
    {40, 5},
    {41, 5},
    {22, 4},
    {22, 4},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {14, 1},
    {15, 2},
    {15, 2},
    {15, 2},
    {15, 2},
    {15, 2},
    {15, 2},
    {15, 2},
    {15, 2},
    {128, 5},
    {192, 5},
    {26, 5},
    {27, 5},
    {28, 5},
    {29, 5},
    {19, 4},
    {19, 4},
    {20, 4},
    {20, 4},
    {34, 5},
    {35, 5},
    {36, 5},
    {37, 5},
    {38, 5},
    {39, 5},
    {21, 4},
    {21, 4},
    {42, 5},
    {43, 5},
    {0, 3},
    {0, 3},
    {0, 3},
    {0, 3}
};

#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )

/* On platforms that enforce aligned memory accesses, we can't just
 * cast the byte * to the type of object we are accessing, we have
 * unpack the requisite number of bytes, and deal with it that way.
 * Note that the comments below about being 16/32 bit boundaries
 * is referring to offsets into the byte stream, *not* memory
 * addresses.
 */
#define getword16(b)  ((uint16_t)(b[0] | (b[1] << 8)))
#define getword32(b)  ((uint32_t)(getword16(b) | (getword16((b + 2)) << 16)))

static uint32_t
jbig2_find_changing_element(const byte *line, uint32_t x, uint32_t w)
{
    int a;
    uint8_t     all8;
    uint16_t    all16;
    uint32_t    all32;

    if (line == NULL)
        return w;

    if (x == MINUS1) {
        a = 0;
        x = 0;
    } else if (x < w) {
        a = getbit(line, x);
        x++;
    } else {
        return x;
    }

    /* We will be looking for a uint8 or uint16 or uint32 that has at least one
    bit different from <a>, so prepare some useful values for comparison. */
    all8  = (a) ? 0xff : 0;
    all16 = (a) ? 0xffff : 0;
    all32 = (a) ? 0xffffffff : 0;

    /* Check individual bits up to next 8-bit boundary.

    [Would it be worth looking at top 4 bits, then at 2 bits then at 1 bit,
    instead of iterating over all 8 bits? */

    if ( ((uint8_t*) line)[ x / 8] == all8) {
        /* Don't bother checking individual bits if the enclosing uint8 equals
        all8 - just move to the next byte. */
        x = x / 8 * 8 + 8;
        if (x >= w) {
            x = w;
            goto end;
        }
    } else {
        for(;;) {
            if (x == w) {
                goto end;
            }
            if (x % 8 == 0) {
                break;
            }
            if (getbit(line, x) != a) {
                goto end;
            }
            x += 1;
        }
    }

    assert(x % 8 == 0);
    /* Check next uint8 if we are not on 16-bit boundary. */
    if (x % 16) {
        if (w - x < 8) {
            goto check1;
        }
        if ( ((uint8_t*) line)[ x / 8] != all8) {
            goto check1;
        }
        x += 8; /* This will make x a multiple of 16. */
    }

    assert(x % 16 == 0);
    /* Check next uint16 if we are not on 32-bit boundary. */
    if (x % 32) {
        if (w - x < 16) {
            goto check8;
        }
        if ( getword16((line + (x / 8))) != all16) {
            goto check8_no_eof;
        }
        x += 16; /* This will make x a multiple of 32. */
    }

    /* We are now on a 32-bit boundary. Check uint32's until we reach last
    sub-32-bit region. */
    assert(x % 32 == 0);
    for(;;) {
        if (w - x < 32) {
            /* We could still look at the uint32 here - if it equals all32, we
            know there is no match before <w> so could do {x = w; goto end;}.

            But for now we simply fall into the epilogue checking, which will
            look at the next uint16, then uint8, then last 8 bits. */
            goto check16;
        }
        if ( getword32((line + (x / 8))) != all32) {
            goto check16_no_eof;
        }
        x += 32;
    }

    /* Check next uint16. */
check16:
    assert(x % 16 == 0);
    if (w - x < 16) {
        goto check8;
    }
check16_no_eof:
    assert(w - x >= 16);
    if ( getword16((line + (x / 8))) != all16) {
        goto check8_no_eof;
    }
    x += 16;

    /* Check next uint8. */
check8:
    assert(x % 8 == 0);
    if (w - x < 8) {
        goto check1;
    }
check8_no_eof:
    assert(w - x >= 8);
    if ( ((uint8_t*) line)[x/8] != all8) {
        goto check1;
    }
    x += 8;

    /* Check up to the next 8 bits. */
check1:
    assert(x % 8 == 0);
    if ( ((uint8_t*) line)[ x / 8] == all8) {
        x = w;
        goto end;
    }
    {
        for(;;) {
            if (x == w) {
                goto end;
            }
            if (getbit(line, x) != a) {
                goto end;
            }
            x += 1;
        }
    }

end:
    return x;
}

#undef getword16
#undef getword32

static uint32_t
jbig2_find_changing_element_of_color(const byte *line, uint32_t x, uint32_t w, int color)
{
    if (line == NULL)
        return w;
    x = jbig2_find_changing_element(line, x, w);
    if (x < w && getbit(line, x) != color)
        x = jbig2_find_changing_element(line, x, w);
    return x;
}

static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };

static void
jbig2_set_bits(byte *line, uint32_t x0, uint32_t x1)
{
    uint32_t a0, a1, b0, b1, a;

    a0 = x0 >> 3;
    a1 = x1 >> 3;

    b0 = x0 & 7;
    b1 = x1 & 7;

    if (a0 == a1) {
        line[a0] |= lm[b0] & rm[b1];
    } else {
        line[a0] |= lm[b0];
        for (a = a0 + 1; a < a1; a++)
            line[a] = 0xFF;
        if (b1)
            line[a1] |= rm[b1];
    }
}

static int
jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
{
    uint32_t word = mmr->word;
    int table_ix = word >> (32 - initial_bits);
    int val = table[table_ix].val;
    int n_bits = table[table_ix].n_bits;

    if (n_bits > initial_bits) {
        int mask = (1 << (32 - initial_bits)) - 1;

        table_ix = val + ((word & mask) >> (32 - n_bits));
        val = table[table_ix].val;
        n_bits = initial_bits + table[table_ix].n_bits;
    }

    jbig2_decode_mmr_consume(mmr, n_bits);

    return val;
}

static int
jbig2_decode_get_run(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
{
    int result = 0;
    int val;

    do {
        val = jbig2_decode_get_code(mmr, table, initial_bits);
        if (val == ERROR)
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "invalid code detected in MMR-coded data");
        else if (val == UNCOMPRESSED)
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "uncompressed code in MMR-coded data");
        else if (val == ZEROES)
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zeroes code in MMR-coded data");
        result += val;
    } while (val >= 64);

    return result;
}

static int
jbig2_decode_mmr_line(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const byte *ref, byte *dst, int *eofb)
{
    uint32_t a0 = MINUS1;
    uint32_t a1, a2, b1, b2;
    int c = 0;                  /* 0 is white, black is 1 */

    while (1) {
        uint32_t word = mmr->word;

        /* printf ("%08x\n", word); */

        if (a0 != MINUS1 && a0 >= mmr->width)
            break;

        if ((word >> (32 - 3)) == 1) {
            int white_run, black_run;

            jbig2_decode_mmr_consume(mmr, 3);

            if (a0 == MINUS1)
                a0 = 0;

            if (c == 0) {
                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
                if (white_run < 0)
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
                if (black_run < 0)
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
                /* printf ("H %d %d\n", white_run, black_run); */
                a1 = a0 + white_run;
                a2 = a1 + black_run;
                if (a1 > mmr->width)
                    a1 = mmr->width;
                if (a2 > mmr->width)
                    a2 = mmr->width;
                if (a2 < a1) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative black H run");
                    a2 = a1;
                }
                if (a1 < mmr->width)
                    jbig2_set_bits(dst, a1, a2);
                a0 = a2;
            } else {
                black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7);
                if (black_run < 0)
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode black H run");
                white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8);
                if (white_run < 0)
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode white H run");
                /* printf ("H %d %d\n", black_run, white_run); */
                a1 = a0 + black_run;
                a2 = a1 + white_run;
                if (a1 > mmr->width)
                    a1 = mmr->width;
                if (a2 > mmr->width)
                    a2 = mmr->width;
                if (a1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative white H run");
                    a1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, a1);
                a0 = a2;
            }
        }

        else if ((word >> (32 - 4)) == 1) {
            /* printf ("P\n"); */
            jbig2_decode_mmr_consume(mmr, 4);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            b2 = jbig2_find_changing_element(ref, b1, mmr->width);
            if (c) {
                if (b2 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative P run");
                    b2 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b2);
            }
            a0 = b2;
        }

        else if ((word >> (32 - 1)) == 1) {
            /* printf ("V(0)\n"); */
            jbig2_decode_mmr_consume(mmr, 1);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative V(0) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 3)) == 3) {
            /* printf ("VR(1)\n"); */
            jbig2_decode_mmr_consume(mmr, 3);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 + 1 <= mmr->width)
                b1 += 1;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(1) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 6)) == 3) {
            /* printf ("VR(2)\n"); */
            jbig2_decode_mmr_consume(mmr, 6);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 + 2 <= mmr->width)
                b1 += 2;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(2) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 7)) == 3) {
            /* printf ("VR(3)\n"); */
            jbig2_decode_mmr_consume(mmr, 7);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 + 3 <= mmr->width)
                b1 += 3;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VR(3) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 3)) == 2) {
            /* printf ("VL(1)\n"); */
            jbig2_decode_mmr_consume(mmr, 3);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 >= 1)
                b1 -= 1;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(1) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 6)) == 2) {
            /* printf ("VL(2)\n"); */
            jbig2_decode_mmr_consume(mmr, 6);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 >= 2)
                b1 -= 2;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(2) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 7)) == 2) {
            /* printf ("VL(3)\n"); */
            jbig2_decode_mmr_consume(mmr, 7);
            b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
            if (b1 >= 3)
                b1 -= 3;
            if (c) {
                if (b1 < a0) {
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ignoring negative VL(3) run");
                    b1 = a0;
                }
                if (a0 < mmr->width)
                    jbig2_set_bits(dst, a0, b1);
            }
            a0 = b1;
            c = !c;
        }

        else if ((word >> (32 - 24)) == 0x1001) {
            /* printf ("EOFB\n"); */
            jbig2_decode_mmr_consume(mmr, 24);
            *eofb = 1;
            break;
        }

        else
            break;
    }

    return 0;
}

int
jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image)
{
    Jbig2MmrCtx mmr;
    const uint32_t rowstride = image->stride;
    byte *dst = image->data;
    byte *ref = NULL;
    uint32_t y;
    int code = 0;
    int eofb = 0;

    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);

    for (y = 0; !eofb && y < image->height; y++) {
        memset(dst, 0, rowstride);
        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
        if (code < 0)
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line");
        ref = dst;
        dst += rowstride;
    }

    if (eofb && y < image->height) {
        memset(dst, 0, rowstride * (image->height - y));
    }

    return code;
}

/**
 * jbig2_decode_halftone_mmr: decode mmr region inside of halftones
 *
 * @ctx: jbig2 decoder context
 * @params: parameters for decoding
 * @data: pointer to text region data to be decoded
 * @size: length of text region data
 * @image: return of decoded image
 * @consumed_bytes: return of consumed bytes from @data
 *
 * MMR decoding that consumes EOFB and returns consumed bytes (@consumed_bytes)
 *
 * returns: 0
 **/
int
jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes)
{
    Jbig2MmrCtx mmr;
    const uint32_t rowstride = image->stride;
    byte *dst = image->data;
    byte *ref = NULL;
    uint32_t y;
    int code = 0;
    const uint32_t EOFB = 0x001001;
    int eofb = 0;

    jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);

    for (y = 0; !eofb && y < image->height; y++) {
        memset(dst, 0, rowstride);
        code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb);
        if (code < 0)
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode halftone mmr line");
        ref = dst;
        dst += rowstride;
    }

    if (eofb && y < image->height) {
        memset(dst, 0, rowstride * (image->height - y));
    }

    /* test for EOFB (see section 6.2.6) */
    if (mmr.word >> 8 == EOFB) {
        jbig2_decode_mmr_consume(&mmr, 24);
    }

    *consumed_bytes += (mmr.consumed_bits + 7) / 8;
    return code;
}