Coverage Report

Created: 2026-03-20 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jbig2dec/jbig2_generic.c
Line
Count
Source
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
/**
21
 * Generic region handlers.
22
 **/
23
24
#ifdef HAVE_CONFIG_H
25
#include "config.h"
26
#endif
27
#include "os_types.h"
28
29
#include <stddef.h>
30
#include <string.h>             /* memcpy(), memset() */
31
32
#ifdef OUTPUT_PBM
33
#include <stdio.h>
34
#endif
35
36
#include "jbig2.h"
37
#include "jbig2_priv.h"
38
#include "jbig2_arith.h"
39
#include "jbig2_generic.h"
40
#include "jbig2_image.h"
41
#include "jbig2_mmr.h"
42
#include "jbig2_page.h"
43
#include "jbig2_segment.h"
44
45
/*
46
This is an explanation of the unoptimized and optimized generic
47
region decoder implementations below, wherein we try to explain
48
all the magic numbers.
49
50
The generic region decoders decode the output pixels one row at a
51
time, top to bottom. Within each row the pixels are decoded left
52
to right. The input for the arithmetic integer decoder used to
53
decode each pixel is a context consisting of up to 16 previously
54
decoded pixels. These pixels are chosen according to a predefined
55
template placed relative to the location of the pixel to be
56
decoded (6.2.5.3 figures 3, 4, 5 and 6). There are four different
57
template that may be used (6.2.5.3). The template to use is
58
determined by GBTEMPLATE. GBTEMPLATE is set in the symbol
59
dictionary (6.5.8.1), generic region (7.4.6.4), or when decoding
60
a halftone region's gray-scale image (annex C.5).
61
62
Most of the pixels in each template have fixed locations relative
63
to the pixel to be decoded. However, all templates have at least
64
one adaptive pixel. The adaptive pixels have nominal locations,
65
but these locations may be changed by GBAT. GBAT is set in the
66
symbol dictionary (7.4.2.1.2), generic region (7.4.6.1), or hard
67
coded as for halftone patterns (6.7.5).
68
69
Adaptive pixels are restricted to fall within a field of
70
previously decoded pixels relative to the pixel to be decoded
71
(figure 7). The relative Y-coordinate for these adaptive pixels
72
may vary between -128 and 0. The relative X-coordinate may vary
73
between -128 and +127 (however, if the Y-coordinate is 0 the
74
range of the X-coordinate is further restricted to -128 to -1
75
since the pixels at locations 0 to +127 have not yet been
76
decoded). If a template refers to a pixel location that reside
77
outside of the image boundaries its value is assumed to be 0.
78
79
UNOPTIMIZED DECODER
80
81
The unoptimized decoders first check the contents of GBAT. If
82
GBAT specifies that any of the adaptive pixels reside outside the
83
allowed field the decoding is aborted. Next, each row is
84
processed top to bottom, left to right, one pixel at a time. For
85
each pixel a context is created containing the bit values of the
86
pixels that fall inside the template.
87
88
The order these bits are stored in the context is implementation
89
dependent (6.2.5.3). We store the bit values in the CONTEXT
90
variable from LSB to MSB, starting with the value of the pixel to
91
the left of the current pixel, continuing right to left, bottom
92
to top following the template. Using the CONTEXT created from
93
these pixel values, the arithmetic integer decoder retrieves the
94
pixel value, which is then written into the output image.
95
96
Example when GBTEMPLATE is 2:
97
98
The figure below represents a pixel grid of the output image.
99
Each pixel is a single bit in the image. The pixel "OO" in the
100
figure below is about to be decoded. The pixels "??" have not
101
been decoded yet. The CONTEXT variable is constructed by
102
combining the bit values from the pixels referred to by the
103
template, shifted to their corresponding bit position.
104
105
     .    .    .    .    .    .    .    .
106
     .    .    .    .    .    .    .    .
107
  ...+----+----+----+----+----+----+----+...
108
     |    |    | X9 | X8 | X7 |    |    |
109
  ...+----+----+----+----+----+----+----+...
110
     |    | X6 | X5 | X4 | X3 | A1 |    |
111
  ...+----+----+----+----+----+----+----+...
112
     |    | X2 | X1 | OO | ?? | ?? | ?? |
113
  ...+----+----+----+----+----+----+----+...
114
     .    .    .    .    .    .    .    .
115
     .    .    .    .    .    .    .    .
116
117
In the table below pixel OO is assumed to be at coordinate (x, y).
118
119
Bit 9: Pixel at location (x-1, y-2) (This is fixed pixel X9)
120
Bit 8: Pixel at location (x  , y-2) (This is fixed pixel X8)
121
Bit 7: Pixel at location (x+1, y-2) (This is fixed pixel X7)
122
Bit 6: Pixel at location (x-2, y-1) (This is fixed pixel X6)
123
Bit 5: Pixel at location (x-1, y-1) (This is fixed pixel X5)
124
Bit 4: Pixel at location (x  , y-1) (This is fixed pixel X4)
125
Bit 3: Pixel at location (x+1, y-1) (This is fixed pixel X3)
126
Bit 2: Pixel at location (x+2, y-1) (This is adaptive pixel A1)
127
Bit 1: Pixel at location (x-2, y  ) (This is fixed pixel X2)
128
Bit 0: Pixel at location (x-1, y  ) (This is fixed pixel X1)
129
130
The location of adaptive pixel A1 may not always be at the
131
nominal location (x+2, y-1). It could be at any pixel location to
132
the left or above OO as specified by GBAT, e.g. at the location
133
(x-128, y+127).
134
135
OPTIMIZED DECODER
136
137
The optimized decoders work differently. They strive to avoid
138
recreating the arithmetic integer decoder context from scratch
139
for every pixel decoded. Instead they reuse part of the CONTEXT
140
used to compute the previous pixel (the pixel to left of the one
141
now being decoded). They also keep two sliding windows of pixel
142
bit values from the two rows of pixels immediately above the
143
pixel to be decoded. These are stored in the 32-bit variables
144
line_m1 (row above the pixel to be decoded) and line_m2 (row
145
above that of line_m1). These optimized decoders ONLY work for
146
the nominal adaptive pixel locations since these locations are
147
hard-coded into the implementation.
148
149
The bit ordering in the CONTEXT variable is identical to the
150
unoptimized case described above.
151
152
The optimized decoders decode the output pixels one row at a
153
time, top to bottom. Within each row the pixels are decoded in
154
batches of up to eight pixels at a time (except possibly the
155
right most batch which may be less than eight pixels). The
156
batches in a row are decoded in sequence from left to right.
157
Within each such batch the pixels are decoded in sequence from
158
left to right.
159
160
Before decoding the pixels in a row the two sliding windows of
161
pixel values are reset. The first eight pixels of the row above
162
the pixel to be decoded is stored in line_m1, while line_m2
163
stores the first eight pixels of the row above that of line_m1.
164
165
The figure below illustrates the situation where the template has
166
been placed so that the decoded pixel OO is the very first pixel
167
of a row. It also gives labels to various pixels that we will
168
refer to below.
169
170
             .    .    .    .    .    .    .    .    .    .    .
171
             |    .    .    .    .    .    .    .    .    .    .
172
   +    +    +----+----+----+----+----+----+----+----+----+----+...
173
          X9 | X8 | X7 | m1 | m2 | m3 | m4 | m5 | m6 | m7 |    |
174
   +    +    +----+----+----+----+----+----+----+----+----+----+...
175
     X6   X5 | X4 | X3 | A1 | n1 | n2 | n3 | n4 | n5 | n6 | n7 |
176
   +    +    +----+----+----+----+----+----+----+----+----+----+...
177
     X2   X1 | OO |    |    |    |    |    |    |    |    |    |
178
   +    +    +----+----+----+----+----+----+----+----+----+----+...
179
             |    .    .    .    .    .    .    .    .    .    .
180
             .    .    .    .    .    .    .    .    .    .    .
181
182
The pixels X1, X2, X5, X6 and X9 all reside outside the left edge
183
of the image. These pixels (like all others outside the image)
184
can according to 6.2.5.2 be assumed to be 0. line_m1 stores n5
185
through n1 as well as A1, and X3 through X6. line_m2 stores m6
186
through m1 as well as X7 through X9. The bits in line_m2 are also
187
shifted left four bits as seen below.
188
189
15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 | bit position
190
------------------------------------------------+------------------
191
 0  0  0  0  0  0 X6 X5 X4 X3 A1 n1 n2 n3 n4 n5 | line_m1
192
 0  0  0 X9 X8 X7 m1 m2 m3 m4 m5 m6  0  0  0  0 | line_m2
193
194
The way line_m1 and line_m2 are stored means we can simply shift
195
them by the same amount to move the sliding window.
196
197
The bit order in line_m1 and line_m2 matches the ordering in the
198
CONTEXT variable. Each bit for the 'A' and 'X' pixels in line_m1
199
and line_m2 correspond to the equivalent bits in CONTEXT, only
200
shifted right by 3 bits. Thus X3 is bit 3 in CONTEXT and bit 6 in
201
line_m1, etc.
202
203
The initial arithmetic integer decoder context is created and
204
stored in the CONTEXT variable by masking, shifting, and bitwise
205
ORing the contents of line_m1 and line_m2. The "CONTEXT contents"
206
row is only shown for clarity, it is not present in the code.
207
208
15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 | bit position
209
------------------------------------------------+---------------------------
210
 0  0  0  0  0  0  0  0  0 X6 X5 X4 X3 A1 n1 n2 | line_m1 >> 3
211
 0  0  0  0  0  0  0  0  0  1  1  1  1  1  0  0 | mask for line_m1 (0x7c)
212
 0  0  0  0  0  0  0  0  0 X6 X5 X4 X3 A1  0  0 | line_m1 AND mask
213
------------------------------------------------+---------------------------
214
 0  0  0  0  0  0 X9 X8 X7 m1 m2 m3 m4 m5 m6  0 | line_m2 >> 3
215
 0  0  0  0  0  0  1  1  1  0  0  0  0  0  0  0 | mask for line_m2 (0x380)
216
 0  0  0  0  0  0 X9 X8 X7  0  0  0  0  0  0  0 | line_m2 AND mask
217
------------------------------------------------+---------------------------
218
 0  0  0  0  0  0 X9 X8 X7 X6 X5 X4 X3 A1  0  0 | CONTEXT = line_m1 OR line_m2
219
------------------------------------------------+---------------------------
220
 0  0  0  0  0  0 X9 X8 X7 X6 X5 X4 X3 A1 X2 X1 | CONTEXT contents
221
222
Each batch is normally 8 bits, but at the right edge of the image
223
we may have fewer pixels to decode. The minor_width is how many
224
pixels the current batch should decode, with a counter variable
225
x_minor to keep track of the current pixel being decoded.
226
227
In order to process a new batch of pixels, unless we're at the
228
rightmost batch of pixels, we need to refill the sliding window
229
variables with eight new bits. Looking at the diagram above we
230
can see that in order to decode eight pixels starting with O0
231
we'll need to have bits up to pixel 'n7' for line_m1 and 'm7' for
232
line_m2 available (A1 and X7 moved right 7 times). To do this
233
simply and quickly, we shift line_m1 left by 8 bits, and OR in
234
the next byte from corresponding row. Likewise for line_m2, but
235
the next byte from the image is also shifted left by 4 bits to
236
compensate for line_m2 having the four least significant bits
237
unused.
238
239
These new eight bits contain the bit values of the eight pixels
240
to the right of those already present in line_m1 and line_m2. We
241
call these new bits m7 through mE, and n6 through nD, as
242
illustrated below.
243
244
23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 | bit position
245
------------------------------------------------------------------------+-------------
246
 0  0  0  0  0  0  0  0  0  0  0  0  0  0 X6 X5 X4 X3 A1 n1 n2 n3 n4 n5 | original line_m1
247
 0  0  0  0  0  0 X6 X5 X4 X3 A1 n1 n2 n3 n4 n5  0  0  0  0  0  0  0  0 | line_m1 shifted left by 8
248
 0  0  0  0  0  0 X6 X5 X4 X3 A1 n1 n2 n3 n4 n5 n6 n7 n8 n9 nA nB nC nD | line_m1 with new bits ORed in
249
------------------------------------------------------------------------+-------------
250
 0  0  0  0  0  0  0  0  0  0  0 X9 X8 X7 m1 m2 m3 m4 m5 m6  0  0  0  0 | original line_m2
251
 0  0  0 X9 X8 X7 m1 m2 m3 m4 m5 m6  0  0  0  0  0  0  0  0  0  0  0  0 | line_m2 shifted left by 8
252
 0  0  0 X9 X8 X7 m1 m2 m3 m4 m5 m6 m7 m8 m9 mA mB mC mD mE  0  0  0  0 | line_m2 with new bits ORed in
253
254
             .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
255
             |    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
256
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
257
          X9 | X8 | X7 | m1 | m2 | m3 | m4 | m5 | m6 | m7 | m8 | m9 | mA | mB | mC | mD | mE |    |    |    |
258
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
259
     X6   X5 | X4 | X3 | A1 | n1 | n2 | n3 | n4 | n5 | n6 | n7 | n8 | n9 | nA | nB | nC | nD |    |    |    |
260
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
261
     X2   X1 | OO |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
262
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
263
             |    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
264
             .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
265
266
CONTEXT, line_m1 and line_m2 now contain all necessary bits to
267
decode a full batch of eight pixels.
268
269
The first pixel in the batch is decoded using this CONTEXT. After
270
that, for each following pixel we need to update the CONTEXT
271
using both the last decoded pixel value and new bits from line_m1
272
and line_m2.
273
274
             .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
275
             |    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
276
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
277
         (X9)|_X8_|_X7_|>m1<| m2 | m3 | m4 | m5 | m6 | m7 | m8 | m9 | mA | mB | mC | mD | mE |    |    |    |
278
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
279
    (X6) _X5_|_X4_|_X3_|_A1_|>n1<| n2 | n3 | n4 | n5 | n6 | n7 | n8 | n9 | nA | nB | nC | nD |    |    |    |
280
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
281
    (X2) _X1_|>OO<| oo |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
282
   +    +    +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
283
             |    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
284
             .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .    .
285
286
This figure illustrates what happens when the same template is
287
overlaid on itself shifted one pixel to the right in order to
288
decode the next pixel. Pixels marked with _  _ are pixels that
289
are present in both templates' CONTEXTs and can be reused. Pixels
290
marked with (  ) are pixels from the first template that are no
291
longer necessary and can be removed from CONTEXT. Pixels marked
292
with >  < are new pixels that were not part of the original
293
CONTEXT, and so need to be moved into the CONTEXT at the
294
appropriate locations. In general the leftmost pixels of each
295
template row can be forgotten, while new pixels are needed at the
296
right most location of each row.
297
298
The CONTEXT corresponding to the current pixel OO and how it is
299
masked is shown below. Note how the left most pixel of each row
300
of the template is NOT propagated to the CONTEXT, these pixels
301
are X2, X6 and X9. This is done by having the mask being 0 at the
302
corresponding locations.
303
304
 9  8  7  6  5  4  3  2  1  0 | bit position
305
------------------------------+-------------
306
X9 X8 X7 X6 X5 X4 X3 A1 X2 X1 | pixel values from CONTEXT
307
 0  1  1  0  1  1  1  1  0  1 | reused pixel bit value mask (0x1bd)
308
 0 X8 X7  0 X5 X4 X3 A1  0 X1 | reused pixel values from CONTEXT
309
310
Next the CONTEXT is shifted left by one bit to make it reference
311
the next pixel to be decoded. The pixel bit value we just decoded
312
is then written into the bit corresponding to X1. The sliding
313
windows in line_m1 and line_m2 are both shifted (10 - x_minor)
314
bits to the right to make the needed pixels' bit values appear at
315
the correct positions to be ORed into CONTEXT. Note that this
316
shift amount depends on which bit in the batch is currently being
317
computed, as is given by the x_minor counter. In the example
318
below we assume that x_minor is 0.
319
320
 9  8  7  6  5  4  3  2  1  0 | bit position
321
------------------------------+--------------
322
 0 X8 X7  0 X5 X4 X3 A1  0  0 | reused pixels from CONTEXT
323
X8 X7  0 X5 X4 X3 A1  0  0  0 | reused pixels shifted left 1 bit
324
------------------------------+--------------
325
X8 X7  0 X5 X4 X3 A1  0 X1 OO | new CONTEXT with current pixel at LSB
326
------------------------------+--------------
327
 0  0 X6 X5 X4 X3 A1 n1 n2 n3 | line_m1 shifted (10 - x_minor) bits to the right
328
 0  0  0  0  0  0  0  1  0  0 | mask for new adaptive pixel one row above (0x4)
329
X8 X7  0 X5 X4 X3 A1 n1 X1 OO | new CONTEXT with new adaptive pixel
330
------------------------------+--------------
331
X8 X7 m1 m2 m3 m4 m5 m6 m7 m8 | line_m2 with new bits ORed in
332
 0  0  1  0  0  0  0  0  0  0 | mask for new pixel two rows above (0x80)
333
X8 X7 m1 X5 X4 X3 A1 n1 X1 OO | new CONTEXT with new pixel
334
335
This makes the computation of the new CONTEXT be:
336
337
NEWCONTEXT = (CONTEXT & 0x1bd) << 1
338
NEWCONTEXT |= newbit;
339
NEWCONTEXT |= (line_m1 >> (10-x_minor)) & 0x4;
340
NEWCONTEXT |= (line_m2 >> (10-x_minor)) & 0x80;
341
342
The optimized decoding functions for GBTEMPLATE 0, 1 and 3 all
343
work similarly. */
344
345
/* Get a bit. No bounds checking. */
346
static inline int
347
jbig2_image_get_pixel_fast(Jbig2Image *image, int x, int y)
348
462M
{
349
462M
    const int byte = (x >> 3) + y * image->stride;
350
462M
    const int bit = 7 - (x & 7);
351
352
462M
    return ((image->data[byte] >> bit) & 1);
353
462M
}
354
355
/* return the appropriate context size for the given template */
356
int
357
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template)
358
16.8k
{
359
16.8k
    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
360
361
16.8k
    return stats_size;
362
16.8k
}
363
364
static int
365
jbig2_decode_generic_template0(Jbig2Ctx *ctx,
366
                               Jbig2Segment *segment,
367
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as,
368
                               Jbig2Image *image, Jbig2ArithCx *GB_stats)
369
20.9k
{
370
20.9k
    const uint32_t GBW = image->width;
371
20.9k
    const uint32_t GBH = image->height;
372
20.9k
    const uint32_t rowstride = image->stride;
373
20.9k
    uint32_t x, y;
374
20.9k
    byte *line2 = NULL;
375
20.9k
    byte *line1 = NULL;
376
20.9k
    byte *gbreg_line = (byte *) image->data;
377
378
#ifdef OUTPUT_PBM
379
    printf("P4\n%d %d\n", GBW, GBH);
380
#endif
381
382
20.9k
    if (GBW <= 0)
383
0
        return 0;
384
385
216k
    for (y = 0; y < GBH; y++) {
386
195k
        uint32_t CONTEXT;
387
195k
        uint32_t line_m1;
388
195k
        uint32_t line_m2;
389
195k
        uint32_t padded_width = (GBW + 7) & -8;
390
391
195k
        line_m1 = line1 ? line1[0] : 0;
392
195k
        line_m2 = line2 ? line2[0] << 6 : 0;
393
195k
        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
394
395
        /* 6.2.5.7 3d */
396
33.6M
        for (x = 0; x < padded_width; x += 8) {
397
33.4M
            byte result = 0;
398
33.4M
            int x_minor;
399
33.4M
            int minor_width = GBW - x > 8 ? 8 : GBW - x;
400
401
33.4M
            if (line1)
402
13.1M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
403
404
33.4M
            if (line2)
405
7.64M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
406
407
            /* This is the speed-critical inner loop. */
408
300M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
409
266M
                int bit;
410
411
266M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
412
266M
                if (bit < 0)
413
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
414
266M
                result |= bit << (7 - x_minor);
415
266M
                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
416
266M
            }
417
33.4M
            gbreg_line[x >> 3] = result;
418
33.4M
        }
419
#ifdef OUTPUT_PBM
420
        fwrite(gbreg_line, 1, rowstride, stdout);
421
#endif
422
195k
        line2 = line1;
423
195k
        line1 = gbreg_line;
424
195k
        gbreg_line += rowstride;
425
195k
    }
426
427
20.9k
    return 0;
428
20.9k
}
429
430
#define pixel_outside_field(x, y) \
431
217k
    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
432
433
static int
434
jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx,
435
                                     Jbig2Segment *segment,
436
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
437
12.5k
{
438
12.5k
    const uint32_t GBW = image->width;
439
12.5k
    const uint32_t GBH = image->height;
440
12.5k
    uint32_t CONTEXT;
441
12.5k
    uint32_t x, y;
442
12.5k
    int bit;
443
444
12.5k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
445
12.5k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
446
12.5k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
447
12.5k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
448
42
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
449
42
                           "adaptive template pixel is out of field");
450
451
23.3M
    for (y = 0; y < GBH; y++) {
452
23.3M
        uint32_t out_byte = 0;
453
23.3M
        int out_bits_to_go_in_byte = 8;
454
23.3M
        uint8_t *d = &image->data[image->stride * y];
455
23.3M
        uint32_t pd = 0;
456
23.3M
        uint32_t ppd = 0;
457
23.3M
        uint8_t *pline = NULL;
458
23.3M
        uint8_t *ppline = NULL;
459
23.3M
        if (y >= 1)
460
23.3M
        {
461
23.3M
            pline  = &image->data[image->stride * (y-1)];
462
23.3M
            pd = (*pline++ << 8);
463
23.3M
            if (GBW > 8)
464
646k
                pd |= *pline++;
465
23.3M
        }
466
23.3M
        if (y >= 2) {
467
23.3M
            ppline = &image->data[image->stride * (y-2)];
468
23.3M
            ppd = (*ppline++ << 8);
469
23.3M
            if (GBW > 8)
470
637k
                ppd |= *ppline++;
471
23.3M
        }
472
410M
        for (x = 0; x < GBW; x++) {
473
387M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
474
98.4M
                bit = 0;
475
289M
            } else {
476
289M
                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
477
289M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
478
289M
                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
479
289M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
480
289M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
481
289M
                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
482
289M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
483
289M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
484
289M
                if (bit < 0)
485
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
486
289M
            }
487
387M
            pd = pd<<1;
488
387M
            ppd = ppd<<1;
489
387M
            out_byte = (out_byte<<1) | bit;
490
387M
            out_bits_to_go_in_byte--;
491
387M
            *d = out_byte<<out_bits_to_go_in_byte;
492
387M
            if (out_bits_to_go_in_byte == 0) {
493
42.3M
                out_bits_to_go_in_byte = 8;
494
42.3M
                d++;
495
42.3M
                if (x+9 < GBW && pline != NULL) {
496
29.4M
                    pd |= *pline++;
497
29.4M
                    if (ppline != NULL)
498
23.2M
                        ppd |= *ppline++;
499
29.4M
                }
500
42.3M
            }
501
387M
        }
502
23.3M
        if (out_bits_to_go_in_byte != 8)
503
23.2M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
504
23.3M
    }
505
12.5k
    return 0;
506
12.5k
}
507
508
static int
509
jbig2_decode_generic_template1_unopt(Jbig2Ctx *ctx,
510
                                     Jbig2Segment *segment,
511
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
512
3.52k
{
513
3.52k
    const uint32_t GBW = image->width;
514
3.52k
    const uint32_t GBH = image->height;
515
3.52k
    uint32_t CONTEXT;
516
3.52k
    uint32_t x, y;
517
3.52k
    int bit;
518
519
3.52k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
520
7
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
521
7
                           "adaptive template pixel is out of field");
522
523
101M
    for (y = 0; y < GBH; y++) {
524
101M
        uint32_t out_byte = 0;
525
101M
        int out_bits_to_go_in_byte = 8;
526
101M
        uint8_t *d = &image->data[image->stride * y];
527
101M
        uint32_t pd = 0;
528
101M
        uint32_t ppd = 0;
529
101M
        uint8_t *pline = NULL;
530
101M
        uint8_t *ppline = NULL;
531
101M
        if (y >= 1) {
532
101M
            pline  = &image->data[image->stride * (y-1)];
533
101M
            pd = (*pline++ << 8);
534
101M
            if (GBW > 8)
535
413k
                pd |= *pline++;
536
101M
        }
537
101M
        if (y >= 2) {
538
101M
            ppline = &image->data[image->stride * (y-2)];
539
101M
            ppd = (*ppline++ << 8);
540
101M
            if (GBW > 8)
541
411k
                ppd |= *ppline++;
542
101M
        }
543
905M
        for (x = 0; x < GBW; x++) {
544
803M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
545
32.7M
                bit = 0;
546
770M
            } else {
547
770M
                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
548
770M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
549
770M
                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
550
770M
                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
551
770M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
552
770M
                if (bit < 0)
553
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
554
770M
            }
555
803M
            pd = pd<<1;
556
803M
            ppd = ppd<<1;
557
803M
            out_byte = (out_byte<<1) | bit;
558
803M
            out_bits_to_go_in_byte--;
559
803M
            *d = out_byte<<out_bits_to_go_in_byte;
560
803M
            if (out_bits_to_go_in_byte == 0) {
561
82.4M
                out_bits_to_go_in_byte = 8;
562
82.4M
                d++;
563
82.4M
                if (x+9 < GBW && pline != NULL) {
564
37.9M
                    pd |= *pline++;
565
37.9M
                    if (ppline != NULL)
566
30.0M
                        ppd |= *ppline++;
567
37.9M
                }
568
82.4M
            }
569
803M
        }
570
101M
        if (out_bits_to_go_in_byte != 8)
571
91.0M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
572
101M
    }
573
3.51k
    return 0;
574
3.51k
}
575
576
static int
577
jbig2_decode_generic_template1(Jbig2Ctx *ctx,
578
                               Jbig2Segment *segment,
579
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
580
1.25k
{
581
1.25k
    const uint32_t GBW = image->width;
582
1.25k
    const uint32_t GBH = image->height;
583
1.25k
    const uint32_t rowstride = image->stride;
584
1.25k
    uint32_t x, y;
585
1.25k
    byte *line2 = NULL;
586
1.25k
    byte *line1 = NULL;
587
1.25k
    byte *gbreg_line = (byte *) image->data;
588
589
#ifdef OUTPUT_PBM
590
    printf("P4\n%d %d\n", GBW, GBH);
591
#endif
592
593
1.25k
    if (GBW <= 0)
594
0
        return 0;
595
596
9.55k
    for (y = 0; y < GBH; y++) {
597
8.30k
        uint32_t CONTEXT;
598
8.30k
        uint32_t line_m1;
599
8.30k
        uint32_t line_m2;
600
8.30k
        uint32_t padded_width = (GBW + 7) & -8;
601
602
8.30k
        line_m1 = line1 ? line1[0] : 0;
603
8.30k
        line_m2 = line2 ? line2[0] << 5 : 0;
604
8.30k
        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
605
606
        /* 6.2.5.7 3d */
607
10.7M
        for (x = 0; x < padded_width; x += 8) {
608
10.7M
            byte result = 0;
609
10.7M
            int x_minor;
610
10.7M
            int minor_width = GBW - x > 8 ? 8 : GBW - x;
611
612
10.7M
            if (line1)
613
5.68M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
614
615
10.7M
            if (line2)
616
1.03M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
617
618
            /* This is the speed-critical inner loop. */
619
96.7M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
620
85.9M
                int bit;
621
622
85.9M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
623
85.9M
                if (bit < 0)
624
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
625
85.9M
                result |= bit << (7 - x_minor);
626
85.9M
                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
627
85.9M
            }
628
10.7M
            gbreg_line[x >> 3] = result;
629
10.7M
        }
630
#ifdef OUTPUT_PBM
631
        fwrite(gbreg_line, 1, rowstride, stdout);
632
#endif
633
8.30k
        line2 = line1;
634
8.30k
        line1 = gbreg_line;
635
8.30k
        gbreg_line += rowstride;
636
8.30k
    }
637
638
1.25k
    return 0;
639
1.25k
}
640
641
static int
642
jbig2_decode_generic_template2_unopt(Jbig2Ctx *ctx,
643
                               Jbig2Segment *segment,
644
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
645
39.4k
{
646
39.4k
    const uint32_t GBW = image->width;
647
39.4k
    const uint32_t GBH = image->height;
648
39.4k
    uint32_t CONTEXT;
649
39.4k
    uint32_t x, y;
650
39.4k
    int bit;
651
652
39.4k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
653
4
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
654
4
                           "adaptive template pixel is out of field");
655
656
89.6M
    for (y = 0; y < GBH; y++) {
657
89.6M
        uint32_t out_byte = 0;
658
89.6M
        int out_bits_to_go_in_byte = 8;
659
89.6M
        uint8_t *d = &image->data[image->stride * y];
660
89.6M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
661
89.6M
        uint8_t *ppline = &image->data[image->stride * (y-2)];
662
89.6M
        uint32_t pd = 0;
663
89.6M
        uint32_t ppd = 0;
664
89.6M
        if (y > 0) {
665
89.6M
            pd = (*pline++ << 8);
666
89.6M
            if (GBW > 8)
667
309k
                pd |= *pline++;
668
89.6M
            if (y > 1) {
669
89.5M
                ppd = (*ppline++ << 8);
670
89.5M
                if (GBW > 8)
671
270k
                    ppd |= *ppline++;
672
89.5M
            }
673
89.6M
        }
674
800M
        for (x = 0; x < GBW; x++) {
675
710M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
676
112M
                bit = 0;
677
598M
            } else {
678
598M
                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
679
598M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
680
598M
                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
681
598M
                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
682
598M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
683
598M
                if (bit < 0)
684
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
685
598M
            }
686
710M
            pd = pd<<1;
687
710M
            ppd = ppd<<1;
688
710M
            out_byte = (out_byte<<1) | bit;
689
710M
            out_bits_to_go_in_byte--;
690
710M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
691
710M
            if (out_bits_to_go_in_byte == 0) {
692
69.1M
                out_bits_to_go_in_byte = 8;
693
69.1M
                d++;
694
69.1M
                if (x+9 < GBW && y > 0) {
695
40.5M
                    pd |= *pline++;
696
40.5M
                    if (y > 1)
697
32.9M
                        ppd |= *ppline++;
698
40.5M
                }
699
69.1M
            }
700
710M
        }
701
89.6M
        if (out_bits_to_go_in_byte != 8)
702
89.3M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
703
89.6M
    }
704
705
39.4k
    return 0;
706
39.4k
}
707
708
static int
709
jbig2_decode_generic_template2(Jbig2Ctx *ctx,
710
                                Jbig2Segment *segment,
711
                                const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
712
13.0k
{
713
13.0k
    const uint32_t GBW = image->width;
714
13.0k
    const uint32_t GBH = image->height;
715
13.0k
    const uint32_t rowstride = image->stride;
716
13.0k
    uint32_t x, y;
717
13.0k
    byte *line2 = NULL;
718
13.0k
    byte *line1 = NULL;
719
13.0k
    byte *gbreg_line = (byte *) image->data;
720
721
#ifdef OUTPUT_PBM
722
    printf("P4\n%d %d\n", GBW, GBH);
723
#endif
724
725
13.0k
    if (GBW <= 0)
726
0
        return 0;
727
728
39.3M
    for (y = 0; y < GBH; y++) {
729
39.3M
        uint32_t CONTEXT;
730
39.3M
        uint32_t line_m1;
731
39.3M
        uint32_t line_m2;
732
39.3M
        uint32_t padded_width = (GBW + 7) & -8;
733
734
39.3M
        line_m1 = line1 ? line1[0] : 0;
735
39.3M
        line_m2 = line2 ? line2[0] << 4 : 0;
736
39.3M
        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
737
738
        /* 6.2.5.7 3d */
739
111M
        for (x = 0; x < padded_width; x += 8) {
740
71.8M
            byte result = 0;
741
71.8M
            int x_minor;
742
71.8M
            int minor_width = GBW - x > 8 ? 8 : GBW - x;
743
744
71.8M
            if (line1)
745
57.1M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
746
747
71.8M
            if (line2)
748
48.7M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
749
750
            /* This is the speed-critical inner loop. */
751
375M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
752
303M
                int bit;
753
754
303M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
755
303M
                if (bit < 0)
756
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
757
303M
                result |= bit << (7 - x_minor);
758
303M
                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
759
303M
            }
760
71.8M
            gbreg_line[x >> 3] = result;
761
71.8M
        }
762
#ifdef OUTPUT_PBM
763
        fwrite(gbreg_line, 1, rowstride, stdout);
764
#endif
765
39.3M
        line2 = line1;
766
39.3M
        line1 = gbreg_line;
767
39.3M
        gbreg_line += rowstride;
768
39.3M
    }
769
770
13.0k
    return 0;
771
13.0k
}
772
773
static int
774
jbig2_decode_generic_template3(Jbig2Ctx *ctx,
775
                               Jbig2Segment *segment,
776
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
777
3.48k
{
778
3.48k
    const uint32_t GBW = image->width;
779
3.48k
    const uint32_t GBH = image->height;
780
3.48k
    const uint32_t rowstride = image->stride;
781
3.48k
    byte *line1 = NULL;
782
3.48k
    byte *gbreg_line = (byte *) image->data;
783
3.48k
    uint32_t x, y;
784
785
#ifdef OUTPUT_PBM
786
    printf("P4\n%d %d\n", GBW, GBH);
787
#endif
788
789
3.48k
    if (GBW <= 0)
790
0
        return 0;
791
792
34.8k
    for (y = 0; y < GBH; y++) {
793
31.4k
        uint32_t CONTEXT;
794
31.4k
        uint32_t line_m1;
795
31.4k
        uint32_t padded_width = (GBW + 7) & -8;
796
797
31.4k
        line_m1 = line1 ? line1[0] : 0;
798
31.4k
        CONTEXT = (line_m1 >> 1) & 0x3f0;
799
800
        /* 6.2.5.7 3d */
801
57.7M
        for (x = 0; x < padded_width; x += 8) {
802
57.6M
            byte result = 0;
803
57.6M
            int x_minor;
804
57.6M
            int minor_width = GBW - x > 8 ? 8 : GBW - x;
805
806
57.6M
            if (line1)
807
45.5M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
808
809
            /* This is the speed-critical inner loop. */
810
519M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
811
461M
                int bit;
812
813
461M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
814
461M
                if (bit < 0)
815
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
816
461M
                result |= bit << (7 - x_minor);
817
461M
                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
818
461M
            }
819
57.6M
            gbreg_line[x >> 3] = result;
820
57.6M
        }
821
#ifdef OUTPUT_PBM
822
        fwrite(gbreg_line, 1, rowstride, stdout);
823
#endif
824
31.4k
        line1 = gbreg_line;
825
31.4k
        gbreg_line += rowstride;
826
31.4k
    }
827
828
3.48k
    return 0;
829
3.48k
}
830
831
static int
832
jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx,
833
                                     Jbig2Segment *segment,
834
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
835
2.87k
{
836
2.87k
    const uint32_t GBW = image->width;
837
2.87k
    const uint32_t GBH = image->height;
838
2.87k
    uint32_t CONTEXT;
839
2.87k
    uint32_t x, y;
840
2.87k
    int bit;
841
842
2.87k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
843
3
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
844
3
                           "adaptive template pixel is out of field");
845
846
29.7M
    for (y = 0; y < GBH; y++) {
847
29.7M
        uint32_t out_byte = 0;
848
29.7M
        int out_bits_to_go_in_byte = 8;
849
29.7M
        uint8_t *d = &image->data[image->stride * y];
850
29.7M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
851
29.7M
        uint32_t pd = 0;
852
29.7M
        if (y > 0) {
853
29.7M
            pd = (*pline++ << 8);
854
29.7M
            if (GBW > 8)
855
1.95M
                pd |= *pline++;
856
29.7M
        }
857
533M
        for (x = 0; x < GBW; x++) {
858
503M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
859
64.3M
                bit = 0;
860
439M
            } else {
861
439M
                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
862
439M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
863
439M
                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
864
439M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
865
439M
                if (bit < 0)
866
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
867
439M
            }
868
503M
            pd = pd<<1;
869
503M
            out_byte = (out_byte<<1) | bit;
870
503M
            out_bits_to_go_in_byte--;
871
503M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
872
503M
            if (out_bits_to_go_in_byte == 0) {
873
55.5M
                out_bits_to_go_in_byte = 8;
874
55.5M
                d++;
875
55.5M
                if (x+9 < GBW && y > 0)
876
38.6M
                    pd |= *pline++;
877
55.5M
            }
878
503M
        }
879
29.7M
        if (out_bits_to_go_in_byte != 8)
880
29.1M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
881
29.7M
    }
882
2.87k
    return 0;
883
2.87k
}
884
885
static void
886
copy_prev_row(Jbig2Image *image, int row)
887
175M
{
888
175M
    if (!row) {
889
        /* no previous row */
890
7.53k
        memset(image->data, 0, image->stride);
891
175M
    } else {
892
        /* duplicate data from the previous row */
893
175M
        uint8_t *src = image->data + (row - 1) * image->stride;
894
895
175M
        memcpy(src + image->stride, src, image->stride);
896
175M
    }
897
175M
}
898
899
static int
900
jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx,
901
                                      Jbig2Segment *segment,
902
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
903
11.3k
{
904
11.3k
    const uint32_t GBW = image->width;
905
11.3k
    const uint32_t GBH = image->height;
906
11.3k
    uint32_t CONTEXT;
907
11.3k
    uint32_t x, y;
908
11.3k
    int LTP = 0;
909
11.3k
    int gmin, gmax;
910
11.3k
    uint32_t left, right, top;
911
912
11.3k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
913
11.3k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
914
11.3k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
915
11.3k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
916
37
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
917
37
                           "adaptive template pixel is out of field");
918
919
    /* JBig2 has 'standard' values for gbat (see 6.2.5.4 of the spec).
920
     * Have an optimised version for those locations. This greatly
921
     * simplifies some of the fetches. It's almost like they thought
922
     * it through. */
923
11.3k
    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
924
3.81k
        params->gbat[2] == -3 && params->gbat[3] == -1 &&
925
2.91k
        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
926
2.61k
        params->gbat[6] == -2 && params->gbat[7] == -2)
927
1.41k
    {
928
55.4M
        for (y = 0; y < GBH; y++) {
929
55.4M
            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
930
55.4M
            if (bit < 0)
931
0
                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
932
55.4M
            LTP ^= bit;
933
55.4M
            if (!LTP) {
934
11.3M
                uint32_t out_byte = 0;
935
11.3M
                int out_bits_to_go_in_byte = 8;
936
11.3M
                uint8_t *d = &image->data[image->stride * y];
937
11.3M
                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
938
11.3M
                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
939
11.3M
                uint32_t pd = 0;
940
11.3M
                uint32_t ppd = 0;
941
11.3M
                if (y > 0 && pline) {
942
11.3M
                    pd = (*pline++ << 8);
943
11.3M
                    if (GBW > 8)
944
31.1k
                        pd |= *pline++;
945
11.3M
                    if (y > 1 && ppline) {
946
11.3M
                        ppd = (*ppline++ << 8);
947
11.3M
                        if (GBW > 8)
948
29.9k
                            ppd |= *ppline++;
949
11.3M
                    }
950
11.3M
                }
951
230M
                for (x = 0; x < GBW; x++) {
952
219M
                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
953
0
                        bit = 0;
954
219M
                    } else {
955
219M
                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
956
219M
                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
957
219M
                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
958
219M
                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
959
219M
                        if (bit < 0)
960
0
                            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
961
219M
                    }
962
219M
                    pd = pd<<1;
963
219M
                    ppd = ppd<<1;
964
219M
                    out_byte = (out_byte<<1) | bit;
965
219M
                    out_bits_to_go_in_byte--;
966
219M
                    if (out_bits_to_go_in_byte == 0) {
967
26.2M
                        out_bits_to_go_in_byte = 8;
968
26.2M
                        *d++ = (uint8_t)out_byte;
969
26.2M
                        if (x+9 < GBW && y > 0 && pline) {
970
5.64M
                            pd |= *pline++;
971
5.64M
                            if (y > 1 && ppline)
972
5.35M
                                ppd |= *ppline++;
973
5.64M
                        }
974
26.2M
                    }
975
219M
                }
976
11.3M
                if (out_bits_to_go_in_byte != 8)
977
8.69M
                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
978
44.0M
            } else {
979
44.0M
                copy_prev_row(image, y);
980
44.0M
            }
981
55.4M
        }
982
1.41k
        return 0;
983
1.41k
    }
984
985
    /* We divide the width into 3 regions 0..left...right...GBW,
986
     * between left and right, we know that our accesses will never
987
     * step outside the image, enabling us to use faster accessors. */
988
9.91k
    left = 4;
989
9.91k
    right = 2;
990
9.91k
    gmin = gmax = params->gbat[0];
991
9.91k
    if (params->gbat[2] < gmin)
992
1.91k
        gmin = params->gbat[2];
993
9.91k
    if (gmax < params->gbat[2])
994
4.89k
        gmax = params->gbat[2];
995
9.91k
    if (params->gbat[4] < gmin)
996
894
        gmin = params->gbat[4];
997
9.91k
    if (gmax < params->gbat[4])
998
1.90k
        gmax = params->gbat[4];
999
9.91k
    if (params->gbat[6] < gmin)
1000
889
        gmin = params->gbat[6];
1001
9.91k
    if (gmax < params->gbat[6])
1002
4.86k
        gmax = params->gbat[6];
1003
9.91k
    if ((int)left < -gmin)
1004
8.05k
        left = -gmin;
1005
9.91k
    if ((int)right < gmax)
1006
3.52k
        right = gmax;
1007
9.91k
    if (right > GBW)
1008
2.36k
        right = GBW;
1009
9.91k
    right = GBW - right;
1010
    /* So 0 <= x < left or right <= x < GBW needs bounds checking. */
1011
1012
    /* Now we do the same for the height, but here there is no bottom
1013
     * region, as we only ever look up for y. */
1014
9.91k
    top = 2;
1015
9.91k
    gmin = params->gbat[1];
1016
9.91k
    if (params->gbat[3] < gmin)
1017
2.68k
        gmin = params->gbat[3];
1018
9.91k
    if (params->gbat[5] < gmin)
1019
4.84k
        gmin = params->gbat[5];
1020
9.91k
    if (params->gbat[7] < gmin)
1021
1.50k
        gmin = params->gbat[7];
1022
9.91k
    if ((int)top < -gmin)
1023
6.30k
        top = -gmin;
1024
    /* So 0 <= y < top needs bounds checking. */
1025
1026
60.5M
    for (y = 0; y < GBH; y++) {
1027
60.5M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
1028
60.5M
        if (bit < 0)
1029
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
1030
60.5M
        LTP ^= bit;
1031
60.5M
        if (!LTP) {
1032
30.4M
            uint32_t out_byte = 0;
1033
30.4M
            int out_bits_to_go_in_byte = 8;
1034
30.4M
            uint8_t *d = &image->data[image->stride * y];
1035
30.4M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1036
30.4M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1037
30.4M
            uint32_t pd = 0;
1038
30.4M
            uint32_t ppd = 0;
1039
30.4M
            if (y > 0 && pline) {
1040
30.4M
                pd = (*pline++ << 8);
1041
30.4M
                if (GBW > 8)
1042
1.25M
                    pd |= *pline++;
1043
30.4M
                if (y > 1 && ppline) {
1044
30.4M
                    ppd = (*ppline++ << 8);
1045
30.4M
                    if (GBW > 8)
1046
1.24M
                        ppd |= *ppline++;
1047
30.4M
                }
1048
30.4M
            }
1049
413M
            for (x = 0; x < GBW; x++) {
1050
382M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1051
0
                    bit = 0;
1052
382M
                } else {
1053
382M
                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
1054
382M
                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
1055
382M
                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
1056
382M
                    if (y >= top && x >= left && x < right)
1057
115M
                    {
1058
115M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1059
115M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1060
115M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1061
115M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1062
115M
                    }
1063
267M
                    else
1064
267M
                    {
1065
267M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1066
267M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1067
267M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1068
267M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1069
267M
                    }
1070
382M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1071
382M
                    if (bit < 0)
1072
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
1073
382M
                }
1074
382M
                pd = pd<<1;
1075
382M
                ppd = ppd<<1;
1076
382M
                out_byte = (out_byte<<1) | bit;
1077
382M
                out_bits_to_go_in_byte--;
1078
382M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1079
382M
                if (out_bits_to_go_in_byte == 0) {
1080
35.8M
                    out_bits_to_go_in_byte = 8;
1081
35.8M
                    d++;
1082
35.8M
                    if (x+9 < GBW && y > 0 && pline) {
1083
26.4M
                        pd |= *pline++;
1084
26.4M
                        if (y > 1 && ppline)
1085
23.1M
                            ppd |= *ppline++;
1086
26.4M
                    }
1087
35.8M
                }
1088
382M
            }
1089
30.4M
            if (out_bits_to_go_in_byte != 8)
1090
29.2M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1091
30.4M
        } else {
1092
30.1M
            copy_prev_row(image, y);
1093
30.1M
        }
1094
60.5M
    }
1095
1096
9.91k
    return 0;
1097
9.91k
}
1098
1099
static int
1100
jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx,
1101
                                      Jbig2Segment *segment,
1102
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1103
2.55k
{
1104
2.55k
    const uint32_t GBW = image->width;
1105
2.55k
    const uint32_t GBH = image->height;
1106
2.55k
    uint32_t CONTEXT;
1107
2.55k
    uint32_t x, y;
1108
2.55k
    int LTP = 0;
1109
1110
2.55k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1111
5
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1112
5
                           "adaptive template pixel is out of field");
1113
1114
54.5M
    for (y = 0; y < GBH; y++) {
1115
54.5M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
1116
54.5M
        if (bit < 0)
1117
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
1118
54.5M
        LTP ^= bit;
1119
54.5M
        if (!LTP) {
1120
35.8M
            uint32_t out_byte = 0;
1121
35.8M
            int out_bits_to_go_in_byte = 8;
1122
35.8M
            uint8_t *d = &image->data[image->stride * y];
1123
35.8M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1124
35.8M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1125
35.8M
            uint32_t pd = 0;
1126
35.8M
            uint32_t ppd = 0;
1127
35.8M
            if (y > 0 && pline) {
1128
35.8M
                pd = (*pline++ << 8);
1129
35.8M
                if (GBW > 8)
1130
1.23M
                    pd |= *pline++;
1131
35.8M
                if (y > 1 && ppline) {
1132
35.8M
                    ppd = (*ppline++ << 8);
1133
35.8M
                    if (GBW > 8)
1134
1.23M
                        ppd |= *ppline++;
1135
35.8M
                }
1136
35.8M
            }
1137
385M
            for (x = 0; x < GBW; x++) {
1138
349M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1139
0
                    bit = 0;
1140
349M
                } else {
1141
349M
                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
1142
349M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
1143
349M
                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
1144
349M
                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
1145
349M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1146
349M
                    if (bit < 0)
1147
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
1148
349M
                }
1149
349M
                pd = pd<<1;
1150
349M
                ppd = ppd<<1;
1151
349M
                out_byte = (out_byte<<1) | bit;
1152
349M
                out_bits_to_go_in_byte--;
1153
349M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1154
349M
                if (out_bits_to_go_in_byte == 0) {
1155
38.9M
                    out_bits_to_go_in_byte = 8;
1156
38.9M
                    d++;
1157
38.9M
                    if (x+9 < GBW && y > 0 && pline) {
1158
30.8M
                        pd |= *pline++;
1159
30.8M
                        if (y > 1 && ppline)
1160
30.4M
                            ppd |= *ppline++;
1161
30.8M
                    }
1162
38.9M
                }
1163
349M
            }
1164
35.8M
            if (out_bits_to_go_in_byte != 8)
1165
35.4M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1166
35.8M
        } else {
1167
18.7M
            copy_prev_row(image, y);
1168
18.7M
        }
1169
54.5M
    }
1170
1171
2.54k
    return 0;
1172
2.54k
}
1173
1174
static int
1175
jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx,
1176
                                      Jbig2Segment *segment,
1177
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1178
1.16k
{
1179
1.16k
    const uint32_t GBW = image->width;
1180
1.16k
    const uint32_t GBH = image->height;
1181
1.16k
    uint32_t CONTEXT;
1182
1.16k
    uint32_t x, y;
1183
1.16k
    int LTP = 0;
1184
1185
1.16k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1186
3
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1187
3
                           "adaptive template pixel is out of field");
1188
1189
86.0M
    for (y = 0; y < GBH; y++) {
1190
86.0M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
1191
86.0M
        if (bit < 0)
1192
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
1193
86.0M
        LTP ^= bit;
1194
86.0M
        if (!LTP) {
1195
32.0M
            uint32_t out_byte = 0;
1196
32.0M
            int out_bits_to_go_in_byte = 8;
1197
32.0M
            uint8_t *d = &image->data[image->stride * y];
1198
32.0M
            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1199
32.0M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1200
32.0M
            uint32_t pd = 0;
1201
32.0M
            uint32_t ppd = 0;
1202
32.0M
            if (y > 0 && pline) {
1203
32.0M
                pd = (*pline++ << 8);
1204
32.0M
                if (GBW > 8)
1205
199k
                    pd |= *pline++;
1206
32.0M
                if (y > 1 && ppline) {
1207
32.0M
                    ppd = (*ppline++ << 8);
1208
32.0M
                    if (GBW > 8)
1209
198k
                        ppd |= *ppline++;
1210
32.0M
                }
1211
32.0M
            }
1212
286M
            for (x = 0; x < GBW; x++) {
1213
254M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1214
0
                    bit = 0;
1215
254M
                } else {
1216
254M
                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
1217
254M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
1218
254M
                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
1219
254M
                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
1220
254M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1221
254M
                    if (bit < 0)
1222
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
1223
254M
                }
1224
254M
                pd = pd<<1;
1225
254M
                ppd = ppd<<1;
1226
254M
                out_byte = (out_byte<<1) | bit;
1227
254M
                out_bits_to_go_in_byte--;
1228
254M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1229
254M
                if (out_bits_to_go_in_byte == 0) {
1230
27.8M
                    out_bits_to_go_in_byte = 8;
1231
27.8M
                    d++;
1232
27.8M
                    if (x+9 < GBW && y > 0 && pline) {
1233
18.6M
                        pd |= *pline++;
1234
18.6M
                        if (y > 1 && ppline)
1235
17.6M
                            ppd |= *ppline++;
1236
18.6M
                    }
1237
27.8M
                }
1238
254M
            }
1239
32.0M
            if (out_bits_to_go_in_byte != 8)
1240
24.0M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1241
54.0M
        } else {
1242
54.0M
            copy_prev_row(image, y);
1243
54.0M
        }
1244
86.0M
    }
1245
1246
1.15k
    return 0;
1247
1.15k
}
1248
1249
static int
1250
jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx,
1251
                                      Jbig2Segment *segment,
1252
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1253
1.06k
{
1254
1.06k
    const uint32_t GBW = image->width;
1255
1.06k
    const uint32_t GBH = image->height;
1256
1.06k
    uint32_t CONTEXT;
1257
1.06k
    uint32_t x, y;
1258
1.06k
    int LTP = 0;
1259
1260
1.06k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1261
5
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1262
5
                           "adaptive template pixel is out of field");
1263
1264
33.6M
    for (y = 0; y < GBH; y++) {
1265
33.6M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
1266
33.6M
        if (bit < 0)
1267
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
1268
33.6M
        LTP ^= bit;
1269
33.6M
        if (!LTP) {
1270
5.27M
            uint32_t out_byte = 0;
1271
5.27M
            int out_bits_to_go_in_byte = 8;
1272
5.27M
            uint8_t *d = &image->data[image->stride * y];
1273
5.27M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1274
5.27M
            uint32_t pd = 0;
1275
5.27M
            if (y > 0 && pline) {
1276
5.27M
                pd = (*pline++ << 8);
1277
5.27M
                if (GBW > 8)
1278
194k
                    pd |= *pline++;
1279
5.27M
            }
1280
350M
            for (x = 0; x < GBW; x++) {
1281
344M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1282
0
                    bit = 0;
1283
344M
                } else {
1284
344M
                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
1285
344M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1286
344M
                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
1287
344M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1288
344M
                    if (bit < 0)
1289
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
1290
344M
                }
1291
344M
                pd = pd<<1;
1292
344M
                out_byte = (out_byte<<1) | bit;
1293
344M
                out_bits_to_go_in_byte--;
1294
344M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1295
344M
                if (out_bits_to_go_in_byte == 0) {
1296
41.9M
                    out_bits_to_go_in_byte = 8;
1297
41.9M
                    d++;
1298
41.9M
                    if (x+9 < GBW && y > 0 && pline)
1299
26.7M
                        pd |= *pline++;
1300
41.9M
                }
1301
344M
            }
1302
5.27M
            if (out_bits_to_go_in_byte != 8)
1303
1.85M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1304
28.4M
        } else {
1305
28.4M
            copy_prev_row(image, y);
1306
28.4M
        }
1307
33.6M
    }
1308
1309
1.05k
    return 0;
1310
1.05k
}
1311
1312
static int
1313
jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx,
1314
                                   Jbig2Segment *segment,
1315
                                   const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1316
16.1k
{
1317
16.1k
    switch (params->GBTEMPLATE) {
1318
11.3k
    case 0:
1319
11.3k
        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
1320
2.55k
    case 1:
1321
2.55k
        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
1322
1.16k
    case 2:
1323
1.16k
        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
1324
1.06k
    case 3:
1325
1.06k
        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
1326
16.1k
    }
1327
1328
0
    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
1329
16.1k
}
1330
1331
/**
1332
 * jbig2_decode_generic_region: Decode a generic region.
1333
 * @ctx: The context for allocation and error reporting.
1334
 * @segment: A segment reference for error reporting.
1335
 * @params: Decoding parameter set.
1336
 * @as: Arithmetic decoder state.
1337
 * @image: Where to store the decoded data.
1338
 * @GB_stats: Arithmetic stats.
1339
 *
1340
 * Decodes a generic region, according to section 6.2. The caller should
1341
 * pass an already allocated Jbig2Image object for @image
1342
 *
1343
 * Because this API is based on an arithmetic decoding state, it is
1344
 * not suitable for MMR decoding.
1345
 *
1346
 * Return code: 0 on success.
1347
 **/
1348
int
1349
jbig2_decode_generic_region(Jbig2Ctx *ctx,
1350
                            Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1351
113k
{
1352
113k
    const int8_t *gbat = params->gbat;
1353
1354
113k
    if (!params->MMR && params->TPGDON)
1355
16.1k
        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
1356
1357
97.1k
    if (!params->MMR && params->GBTEMPLATE == 0) {
1358
33.5k
        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1 && gbat[2] == -3 && gbat[3] == -1 && gbat[4] == +2 && gbat[5] == -2 && gbat[6] == -2 && gbat[7] == -2)
1359
20.9k
            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
1360
12.5k
        else
1361
12.5k
            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
1362
63.6k
    } else if (!params->MMR && params->GBTEMPLATE == 1) {
1363
4.78k
        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
1364
1.25k
            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
1365
3.52k
        else
1366
3.52k
            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
1367
4.78k
    }
1368
58.8k
    else if (!params->MMR && params->GBTEMPLATE == 2) {
1369
52.4k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1370
13.0k
            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
1371
39.4k
        else
1372
39.4k
            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
1373
52.4k
    } else if (!params->MMR && params->GBTEMPLATE == 3) {
1374
6.36k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1375
3.48k
            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
1376
2.87k
        else
1377
2.87k
            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
1378
6.36k
    }
1379
1380
0
    {
1381
0
        int i;
1382
1383
0
        for (i = 0; i < 8; i++)
1384
0
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]);
1385
0
    }
1386
1387
0
    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported generic region (MMR=%d, GBTEMPLATE=%d)", params->MMR, params->GBTEMPLATE);
1388
97.1k
}
1389
1390
/**
1391
 * Handler for immediate generic region segments
1392
 */
1393
int
1394
jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
1395
21.9k
{
1396
21.9k
    Jbig2RegionSegmentInfo rsi;
1397
21.9k
    byte seg_flags;
1398
21.9k
    int8_t gbat[8];
1399
21.9k
    int offset;
1400
21.9k
    uint32_t gbat_bytes = 0;
1401
21.9k
    Jbig2GenericRegionParams params;
1402
21.9k
    int code = 0;
1403
21.9k
    Jbig2Image *image = NULL;
1404
21.9k
    Jbig2WordStream *ws = NULL;
1405
21.9k
    Jbig2ArithState *as = NULL;
1406
21.9k
    Jbig2ArithCx *GB_stats = NULL;
1407
21.9k
    uint32_t height;
1408
21.9k
    Jbig2Page *page = &ctx->pages[ctx->current_page];
1409
1410
    /* 7.4.6 */
1411
21.9k
    if (segment->data_length < 18)
1412
47
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1413
1414
21.8k
    jbig2_get_region_segment_info(&rsi, segment_data);
1415
21.8k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "generic region: %u x %u @ (%u, %u), flags = %02x", rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
1416
1417
    /* 7.4.6.4 */
1418
21.8k
    height = rsi.height;
1419
21.8k
    if (segment->rows != UINT32_MAX) {
1420
16.5k
        if (segment->rows > rsi.height)
1421
37
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
1422
16.4k
        height = segment->rows;
1423
16.4k
    }
1424
1425
    /* 7.4.6.2 */
1426
21.8k
    seg_flags = segment_data[17];
1427
21.8k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
1428
21.8k
    if ((seg_flags & 1) && (seg_flags & 6))
1429
361
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
1430
1431
    /* 7.4.6.3 */
1432
21.8k
    if (!(seg_flags & 1)) {
1433
20.8k
        gbat_bytes = (seg_flags & 6) ? 2 : 8;
1434
20.8k
        if (18 + gbat_bytes > segment->data_length)
1435
9
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1436
20.8k
        memcpy(gbat, segment_data + 18, gbat_bytes);
1437
20.8k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
1438
20.8k
    }
1439
1440
21.8k
    offset = 18 + gbat_bytes;
1441
1442
    /* Check for T.88 amendment 2 */
1443
21.8k
    if ((seg_flags >> 5) & 1)
1444
17
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
1445
1446
    /* Table 34 */
1447
21.8k
    params.MMR = seg_flags & 1;
1448
21.8k
    params.GBTEMPLATE = (seg_flags & 6) >> 1;
1449
21.8k
    params.TPGDON = (seg_flags & 8) >> 3;
1450
21.8k
    params.USESKIP = 0;
1451
21.8k
    memcpy(params.gbat, gbat, gbat_bytes);
1452
1453
21.8k
    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
1454
13.6k
        if (rsi.y >= page->end_row + page->stripe_size) {
1455
3.37k
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring %u x %u region at (%u, %u) outside of stripe at row %u covering %u rows, on page of height %u", rsi.width, rsi.height, rsi.x, rsi.y, page->end_row, page->stripe_size, page->image->height);
1456
3.37k
            return 0;
1457
3.37k
        }
1458
10.3k
        if (height > page->end_row + page->stripe_size) {
1459
7.76k
            height = page->end_row + page->stripe_size;
1460
7.76k
        }
1461
10.3k
    } else {
1462
8.13k
        if (rsi.y >= page->height) {
1463
989
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring %u x %u region at (%u, %u) outside of page of height %u", rsi.width, rsi.height, rsi.x, rsi.y, page->height);
1464
989
            return 0;
1465
989
        }
1466
7.14k
        if (height > page->height - rsi .y) {
1467
6.03k
            height = page->height - rsi.y;
1468
6.03k
        }
1469
7.14k
    }
1470
17.4k
    if (height == 0) {
1471
618
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
1472
618
        return 0;
1473
618
    }
1474
1475
16.8k
    image = jbig2_image_new(ctx, rsi.width, height);
1476
16.8k
    if (image == NULL)
1477
45
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
1478
16.7k
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
1479
1480
16.7k
    if (params.MMR) {
1481
474
        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
1482
474
        if (code < 0) {
1483
19
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
1484
19
            goto cleanup;
1485
19
        }
1486
16.3k
    } else {
1487
16.3k
        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
1488
1489
16.3k
        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
1490
16.3k
        if (GB_stats == NULL) {
1491
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states when handling immediate generic region");
1492
1
            goto cleanup;
1493
1
        }
1494
16.3k
        memset(GB_stats, 0, stats_size);
1495
1496
16.3k
        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
1497
16.3k
        if (ws == NULL) {
1498
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocated word stream when handling immediate generic region");
1499
1
            goto cleanup;
1500
1
        }
1501
16.3k
        as = jbig2_arith_new(ctx, ws);
1502
16.3k
        if (as == NULL) {
1503
3
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
1504
3
            goto cleanup;
1505
3
        }
1506
16.3k
        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
1507
16.3k
        if (code < 0) {
1508
65
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
1509
65
            goto cleanup;
1510
65
        }
1511
16.3k
    }
1512
1513
16.6k
    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
1514
16.6k
    if (code < 0)
1515
470
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
1516
1517
16.7k
cleanup:
1518
16.7k
    jbig2_free(ctx->allocator, as);
1519
16.7k
    jbig2_word_stream_buf_free(ctx, ws);
1520
16.7k
    jbig2_free(ctx->allocator, GB_stats);
1521
16.7k
    jbig2_image_release(ctx, image);
1522
1523
16.7k
    return code;
1524
16.6k
}