Coverage Report

Created: 2026-10-19 06:56

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, int64_t x, int64_t y)
348
1.74G
{
349
1.74G
    size_t sx = (size_t) x;
350
1.74G
    size_t sy = (size_t) y;
351
1.74G
    size_t byte = (sx >> 3) + sy * image->stride;
352
1.74G
    int bit = 7 - ((int) (sx & 7));
353
354
1.74G
    return ((image->data[byte] >> bit) & 1);
355
1.74G
}
356
357
/* return the appropriate context size for the given template */
358
int
359
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template)
360
16.9k
{
361
16.9k
    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
362
363
16.9k
    (void) ctx;
364
365
16.9k
    return stats_size;
366
16.9k
}
367
368
static int
369
jbig2_decode_generic_template0(Jbig2Ctx *ctx,
370
                               Jbig2Segment *segment,
371
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as,
372
                               Jbig2Image *image, Jbig2ArithCx *GB_stats)
373
6.92k
{
374
6.92k
    const int64_t GBW = image->width;
375
6.92k
    const int64_t GBH = image->height;
376
6.92k
    const uint32_t rowstride = image->stride;
377
6.92k
    int64_t x, y;
378
6.92k
    byte *line2 = NULL;
379
6.92k
    byte *line1 = NULL;
380
6.92k
    byte *gbreg_line = (byte *) image->data;
381
382
6.92k
    (void) ctx;
383
6.92k
    (void) params;
384
385
#ifdef OUTPUT_PBM
386
    printf("P4\n%u %u\n",
387
        (uint32_t) GBW,
388
        (uint32_t) GBH);
389
#endif
390
391
6.92k
    if (GBW <= 0)
392
0
        return 0;
393
394
148M
    for (y = 0; y < GBH; y++) {
395
148M
        uint32_t CONTEXT;
396
148M
        uint32_t line_m1;
397
148M
        uint32_t line_m2;
398
148M
        int64_t padded_width = (GBW + 7) & -8;
399
400
148M
        line_m1 = line1 ? line1[0] : 0;
401
148M
        line_m2 = line2 ? line2[0] << 6 : 0;
402
148M
        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
403
404
        /* 6.2.5.7 3d */
405
374M
        for (x = 0; x < padded_width; x += 8) {
406
225M
            byte result = 0;
407
225M
            int64_t x_minor;
408
225M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
409
410
225M
            if (line1)
411
199M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
412
413
225M
            if (line2)
414
186M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
415
416
            /* This is the speed-critical inner loop. */
417
1.35G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
418
1.12G
                int bit;
419
420
1.12G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
421
1.12G
                if (bit < 0)
422
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
423
1.12G
                result |= bit << (7 - x_minor);
424
1.12G
                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
425
1.12G
            }
426
225M
            gbreg_line[x >> 3] = result;
427
225M
        }
428
#ifdef OUTPUT_PBM
429
        fwrite(gbreg_line, 1, rowstride, stdout);
430
#endif
431
148M
        line2 = line1;
432
148M
        line1 = gbreg_line;
433
148M
        gbreg_line += rowstride;
434
148M
    }
435
436
6.92k
    return 0;
437
6.92k
}
438
439
#define pixel_outside_field(x, y) \
440
214k
    ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
441
442
static int
443
jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx,
444
                                     Jbig2Segment *segment,
445
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
446
13.8k
{
447
13.8k
    const int64_t GBW = image->width;
448
13.8k
    const int64_t GBH = image->height;
449
13.8k
    uint32_t CONTEXT;
450
13.8k
    int64_t x, y;
451
13.8k
    int bit;
452
453
13.8k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
454
13.8k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
455
13.8k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
456
13.8k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
457
31
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
458
31
                           "adaptive template pixel is out of field");
459
460
132M
    for (y = 0; y < GBH; y++) {
461
132M
        uint32_t out_byte = 0;
462
132M
        int out_bits_to_go_in_byte = 8;
463
132M
        uint8_t *d = &image->data[image->stride * y];
464
132M
        uint32_t pd = 0;
465
132M
        uint32_t ppd = 0;
466
132M
        uint8_t *pline = NULL;
467
132M
        uint8_t *ppline = NULL;
468
132M
        if (y >= 1)
469
132M
        {
470
132M
            pline  = &image->data[image->stride * (y-1)];
471
132M
            pd = (*pline++ << 8);
472
132M
            if (GBW > 8)
473
779k
                pd |= *pline++;
474
132M
        }
475
132M
        if (y >= 2) {
476
132M
            ppline = &image->data[image->stride * (y-2)];
477
132M
            ppd = (*ppline++ << 8);
478
132M
            if (GBW > 8)
479
768k
                ppd |= *ppline++;
480
132M
        }
481
1.03G
        for (x = 0; x < GBW; x++) {
482
903M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
483
172M
                bit = 0;
484
730M
            } else {
485
730M
                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
486
730M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
487
730M
                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
488
730M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
489
730M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
490
730M
                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
491
730M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
492
730M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
493
730M
                if (bit < 0)
494
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
495
730M
            }
496
903M
            pd = pd<<1;
497
903M
            ppd = ppd<<1;
498
903M
            out_byte = (out_byte<<1) | bit;
499
903M
            out_bits_to_go_in_byte--;
500
903M
            *d = out_byte<<out_bits_to_go_in_byte;
501
903M
            if (out_bits_to_go_in_byte == 0) {
502
80.5M
                out_bits_to_go_in_byte = 8;
503
80.5M
                d++;
504
80.5M
                if (x+9 < GBW && pline != NULL) {
505
38.9M
                    pd |= *pline++;
506
38.9M
                    if (ppline != NULL)
507
33.2M
                        ppd |= *ppline++;
508
38.9M
                }
509
80.5M
            }
510
903M
        }
511
132M
        if (out_bits_to_go_in_byte != 8)
512
130M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
513
132M
    }
514
13.8k
    return 0;
515
13.8k
}
516
517
static int
518
jbig2_decode_generic_template1_unopt(Jbig2Ctx *ctx,
519
                                     Jbig2Segment *segment,
520
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
521
5.73k
{
522
5.73k
    const int64_t GBW = image->width;
523
5.73k
    const int64_t GBH = image->height;
524
5.73k
    uint32_t CONTEXT;
525
5.73k
    int64_t x, y;
526
5.73k
    int bit;
527
528
5.73k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
529
8
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
530
8
                           "adaptive template pixel is out of field");
531
532
180M
    for (y = 0; y < GBH; y++) {
533
180M
        uint32_t out_byte = 0;
534
180M
        int out_bits_to_go_in_byte = 8;
535
180M
        uint8_t *d = &image->data[image->stride * y];
536
180M
        uint32_t pd = 0;
537
180M
        uint32_t ppd = 0;
538
180M
        uint8_t *pline = NULL;
539
180M
        uint8_t *ppline = NULL;
540
180M
        if (y >= 1) {
541
180M
            pline  = &image->data[image->stride * (y-1)];
542
180M
            pd = (*pline++ << 8);
543
180M
            if (GBW > 8)
544
697k
                pd |= *pline++;
545
180M
        }
546
180M
        if (y >= 2) {
547
180M
            ppline = &image->data[image->stride * (y-2)];
548
180M
            ppd = (*ppline++ << 8);
549
180M
            if (GBW > 8)
550
694k
                ppd |= *ppline++;
551
180M
        }
552
1.39G
        for (x = 0; x < GBW; x++) {
553
1.21G
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
554
30.9M
                bit = 0;
555
1.18G
            } else {
556
1.18G
                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
557
1.18G
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
558
1.18G
                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
559
1.18G
                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
560
1.18G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
561
1.18G
                if (bit < 0)
562
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
563
1.18G
            }
564
1.21G
            pd = pd<<1;
565
1.21G
            ppd = ppd<<1;
566
1.21G
            out_byte = (out_byte<<1) | bit;
567
1.21G
            out_bits_to_go_in_byte--;
568
1.21G
            *d = out_byte<<out_bits_to_go_in_byte;
569
1.21G
            if (out_bits_to_go_in_byte == 0) {
570
126M
                out_bits_to_go_in_byte = 8;
571
126M
                d++;
572
126M
                if (x+9 < GBW && pline != NULL) {
573
46.0M
                    pd |= *pline++;
574
46.0M
                    if (ppline != NULL)
575
37.5M
                        ppd |= *ppline++;
576
46.0M
                }
577
126M
            }
578
1.21G
        }
579
180M
        if (out_bits_to_go_in_byte != 8)
580
167M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
581
180M
    }
582
5.72k
    return 0;
583
5.72k
}
584
585
static int
586
jbig2_decode_generic_template1(Jbig2Ctx *ctx,
587
                               Jbig2Segment *segment,
588
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
589
5.43k
{
590
5.43k
    const int64_t GBW = image->width;
591
5.43k
    const int64_t GBH = image->height;
592
5.43k
    const uint32_t rowstride = image->stride;
593
5.43k
    int64_t x, y;
594
5.43k
    byte *line2 = NULL;
595
5.43k
    byte *line1 = NULL;
596
5.43k
    byte *gbreg_line = (byte *) image->data;
597
598
5.43k
    (void) params;
599
600
#ifdef OUTPUT_PBM
601
    printf("P4\n%u %u\n",
602
        (uint32_t) GBW,
603
        (uint32_t) GBH);
604
#endif
605
606
5.43k
    if (GBW <= 0)
607
0
        return 0;
608
609
98.4M
    for (y = 0; y < GBH; y++) {
610
98.4M
        uint32_t CONTEXT;
611
98.4M
        uint32_t line_m1;
612
98.4M
        uint32_t line_m2;
613
98.4M
        int64_t padded_width = (GBW + 7) & -8;
614
615
98.4M
        line_m1 = line1 ? line1[0] : 0;
616
98.4M
        line_m2 = line2 ? line2[0] << 5 : 0;
617
98.4M
        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
618
619
        /* 6.2.5.7 3d */
620
325M
        for (x = 0; x < padded_width; x += 8) {
621
226M
            byte result = 0;
622
226M
            int64_t x_minor;
623
226M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
624
625
226M
            if (line1)
626
195M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
627
628
226M
            if (line2)
629
180M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
630
631
            /* This is the speed-critical inner loop. */
632
1.37G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
633
1.14G
                int bit;
634
635
1.14G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
636
1.14G
                if (bit < 0)
637
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
638
1.14G
                result |= bit << (7 - x_minor);
639
1.14G
                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
640
1.14G
            }
641
226M
            gbreg_line[x >> 3] = result;
642
226M
        }
643
#ifdef OUTPUT_PBM
644
        fwrite(gbreg_line, 1, rowstride, stdout);
645
#endif
646
98.4M
        line2 = line1;
647
98.4M
        line1 = gbreg_line;
648
98.4M
        gbreg_line += rowstride;
649
98.4M
    }
650
651
5.43k
    return 0;
652
5.43k
}
653
654
static int
655
jbig2_decode_generic_template2_unopt(Jbig2Ctx *ctx,
656
                               Jbig2Segment *segment,
657
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
658
38.3k
{
659
38.3k
    const int64_t GBW = image->width;
660
38.3k
    const int64_t GBH = image->height;
661
38.3k
    uint32_t CONTEXT;
662
38.3k
    int64_t x, y;
663
38.3k
    int bit;
664
665
38.3k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
666
7
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
667
7
                           "adaptive template pixel is out of field");
668
669
199M
    for (y = 0; y < GBH; y++) {
670
199M
        uint32_t out_byte = 0;
671
199M
        int out_bits_to_go_in_byte = 8;
672
199M
        uint8_t *d = &image->data[image->stride * y];
673
199M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
674
199M
        uint8_t *ppline = &image->data[image->stride * (y-2)];
675
199M
        uint32_t pd = 0;
676
199M
        uint32_t ppd = 0;
677
199M
        if (y > 0) {
678
199M
            pd = (*pline++ << 8);
679
199M
            if (GBW > 8)
680
793k
                pd |= *pline++;
681
199M
            if (y > 1) {
682
199M
                ppd = (*ppline++ << 8);
683
199M
                if (GBW > 8)
684
755k
                    ppd |= *ppline++;
685
199M
            }
686
199M
        }
687
1.84G
        for (x = 0; x < GBW; x++) {
688
1.64G
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
689
107M
                bit = 0;
690
1.53G
            } else {
691
1.53G
                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
692
1.53G
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
693
1.53G
                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
694
1.53G
                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
695
1.53G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
696
1.53G
                if (bit < 0)
697
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
698
1.53G
            }
699
1.64G
            pd = pd<<1;
700
1.64G
            ppd = ppd<<1;
701
1.64G
            out_byte = (out_byte<<1) | bit;
702
1.64G
            out_bits_to_go_in_byte--;
703
1.64G
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
704
1.64G
            if (out_bits_to_go_in_byte == 0) {
705
162M
                out_bits_to_go_in_byte = 8;
706
162M
                d++;
707
162M
                if (x+9 < GBW && y > 0) {
708
92.2M
                    pd |= *pline++;
709
92.2M
                    if (y > 1)
710
68.3M
                        ppd |= *ppline++;
711
92.2M
                }
712
162M
            }
713
1.64G
        }
714
199M
        if (out_bits_to_go_in_byte != 8)
715
199M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
716
199M
    }
717
718
38.3k
    return 0;
719
38.3k
}
720
721
static int
722
jbig2_decode_generic_template2(Jbig2Ctx *ctx,
723
                                Jbig2Segment *segment,
724
                                const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
725
82.3k
{
726
82.3k
    const int64_t GBW = image->width;
727
82.3k
    const int64_t GBH = image->height;
728
82.3k
    const uint32_t rowstride = image->stride;
729
82.3k
    int64_t x, y;
730
82.3k
    byte *line2 = NULL;
731
82.3k
    byte *line1 = NULL;
732
82.3k
    byte *gbreg_line = (byte *) image->data;
733
734
82.3k
    (void) params;
735
736
#ifdef OUTPUT_PBM
737
    printf("P4\n%u %u\n",
738
        (uint32_t) GBW,
739
        (uint32_t) GBH);
740
#endif
741
742
82.3k
    if (GBW <= 0)
743
0
        return 0;
744
745
132M
    for (y = 0; y < GBH; y++) {
746
131M
        uint32_t CONTEXT;
747
131M
        uint32_t line_m1;
748
131M
        uint32_t line_m2;
749
131M
        int64_t padded_width = (GBW + 7) & -8;
750
751
131M
        line_m1 = line1 ? line1[0] : 0;
752
131M
        line_m2 = line2 ? line2[0] << 4 : 0;
753
131M
        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
754
755
        /* 6.2.5.7 3d */
756
384M
        for (x = 0; x < padded_width; x += 8) {
757
252M
            byte result = 0;
758
252M
            int64_t x_minor;
759
252M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
760
761
252M
            if (line1)
762
190M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
763
764
252M
            if (line2)
765
160M
                line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
766
767
            /* This is the speed-critical inner loop. */
768
1.38G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
769
1.13G
                int bit;
770
771
1.13G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
772
1.13G
                if (bit < 0)
773
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
774
1.13G
                result |= bit << (7 - x_minor);
775
1.13G
                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
776
1.13G
            }
777
252M
            gbreg_line[x >> 3] = result;
778
252M
        }
779
#ifdef OUTPUT_PBM
780
        fwrite(gbreg_line, 1, rowstride, stdout);
781
#endif
782
131M
        line2 = line1;
783
131M
        line1 = gbreg_line;
784
131M
        gbreg_line += rowstride;
785
131M
    }
786
787
82.3k
    return 0;
788
82.3k
}
789
790
static int
791
jbig2_decode_generic_template3(Jbig2Ctx *ctx,
792
                               Jbig2Segment *segment,
793
                               const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
794
4.67k
{
795
4.67k
    const int64_t GBW = image->width;
796
4.67k
    const int64_t GBH = image->height;
797
4.67k
    const uint32_t rowstride = image->stride;
798
4.67k
    byte *line1 = NULL;
799
4.67k
    byte *gbreg_line = (byte *) image->data;
800
4.67k
    int64_t x, y;
801
802
4.67k
    (void) params;
803
804
#ifdef OUTPUT_PBM
805
    printf("P4\n%u %u\n",
806
        (uint32_t) GBW,
807
        (uint32_t) GBH);
808
#endif
809
810
4.67k
    if (GBW <= 0)
811
0
        return 0;
812
813
78.2k
    for (y = 0; y < GBH; y++) {
814
73.5k
        uint32_t CONTEXT;
815
73.5k
        uint32_t line_m1;
816
73.5k
        int64_t padded_width = (GBW + 7) & -8;
817
818
73.5k
        line_m1 = line1 ? line1[0] : 0;
819
73.5k
        CONTEXT = (line_m1 >> 1) & 0x3f0;
820
821
        /* 6.2.5.7 3d */
822
92.2M
        for (x = 0; x < padded_width; x += 8) {
823
92.2M
            byte result = 0;
824
92.2M
            int64_t x_minor;
825
92.2M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
826
827
92.2M
            if (line1)
828
72.4M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
829
830
            /* This is the speed-critical inner loop. */
831
829M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
832
737M
                int bit;
833
834
737M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
835
737M
                if (bit < 0)
836
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
837
737M
                result |= bit << (7 - x_minor);
838
737M
                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
839
737M
            }
840
92.2M
            gbreg_line[x >> 3] = result;
841
92.2M
        }
842
#ifdef OUTPUT_PBM
843
        fwrite(gbreg_line, 1, rowstride, stdout);
844
#endif
845
73.5k
        line1 = gbreg_line;
846
73.5k
        gbreg_line += rowstride;
847
73.5k
    }
848
849
4.67k
    return 0;
850
4.67k
}
851
852
static int
853
jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx,
854
                                     Jbig2Segment *segment,
855
                                     const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
856
6.04k
{
857
6.04k
    const int64_t GBW = image->width;
858
6.04k
    const int64_t GBH = image->height;
859
6.04k
    uint32_t CONTEXT;
860
6.04k
    int64_t x, y;
861
6.04k
    int bit;
862
863
6.04k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
864
6
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
865
6
                           "adaptive template pixel is out of field");
866
867
107M
    for (y = 0; y < GBH; y++) {
868
107M
        uint32_t out_byte = 0;
869
107M
        int out_bits_to_go_in_byte = 8;
870
107M
        uint8_t *d = &image->data[image->stride * y];
871
107M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
872
107M
        uint32_t pd = 0;
873
107M
        if (y > 0) {
874
107M
            pd = (*pline++ << 8);
875
107M
            if (GBW > 8)
876
1.92M
                pd |= *pline++;
877
107M
        }
878
1.08G
        for (x = 0; x < GBW; x++) {
879
977M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
880
74.5M
                bit = 0;
881
903M
            } else {
882
903M
                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
883
903M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
884
903M
                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
885
903M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
886
903M
                if (bit < 0)
887
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
888
903M
            }
889
977M
            pd = pd<<1;
890
977M
            out_byte = (out_byte<<1) | bit;
891
977M
            out_bits_to_go_in_byte--;
892
977M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
893
977M
            if (out_bits_to_go_in_byte == 0) {
894
104M
                out_bits_to_go_in_byte = 8;
895
104M
                d++;
896
104M
                if (x+9 < GBW && y > 0)
897
42.7M
                    pd |= *pline++;
898
104M
            }
899
977M
        }
900
107M
        if (out_bits_to_go_in_byte != 8)
901
104M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
902
107M
    }
903
6.03k
    return 0;
904
6.03k
}
905
906
static void
907
copy_prev_row(Jbig2Image *image, int64_t row)
908
240M
{
909
240M
    if (!row) {
910
        /* no previous row */
911
5.28k
        memset(image->data, 0, image->stride);
912
240M
    } else {
913
        /* duplicate data from the previous row */
914
240M
        uint8_t *src = image->data + (row - 1) * image->stride;
915
916
240M
        memcpy(src + image->stride, src, image->stride);
917
240M
    }
918
240M
}
919
920
static int
921
jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx,
922
                                      Jbig2Segment *segment,
923
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
924
8.57k
{
925
8.57k
    const int64_t GBW = image->width;
926
8.57k
    const int64_t GBH = image->height;
927
8.57k
    uint32_t CONTEXT;
928
8.57k
    int64_t x, y;
929
8.57k
    int LTP = 0;
930
8.57k
    int gmin, gmax;
931
8.57k
    int64_t left, right, top;
932
933
8.57k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
934
8.56k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
935
8.55k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
936
8.54k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
937
44
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
938
44
                           "adaptive template pixel is out of field");
939
940
    /* JBig2 has 'standard' values for gbat (see 6.2.5.4 of the spec).
941
     * Have an optimised version for those locations. This greatly
942
     * simplifies some of the fetches. It's almost like they thought
943
     * it through. */
944
8.53k
    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
945
4.69k
        params->gbat[2] == -3 && params->gbat[3] == -1 &&
946
3.20k
        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
947
2.71k
        params->gbat[6] == -2 && params->gbat[7] == -2)
948
1.34k
    {
949
54.3M
        for (y = 0; y < GBH; y++) {
950
54.3M
            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
951
54.3M
            if (bit < 0)
952
0
                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
953
54.3M
            LTP ^= bit;
954
54.3M
            if (!LTP) {
955
27.1M
                uint32_t out_byte = 0;
956
27.1M
                int out_bits_to_go_in_byte = 8;
957
27.1M
                uint8_t *d = &image->data[image->stride * y];
958
27.1M
                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
959
27.1M
                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
960
27.1M
                uint32_t pd = 0;
961
27.1M
                uint32_t ppd = 0;
962
27.1M
                if (y > 0 && pline) {
963
27.1M
                    pd = (*pline++ << 8);
964
27.1M
                    if (GBW > 8)
965
291k
                        pd |= *pline++;
966
27.1M
                    if (y > 1 && ppline) {
967
27.1M
                        ppd = (*ppline++ << 8);
968
27.1M
                        if (GBW > 8)
969
290k
                            ppd |= *ppline++;
970
27.1M
                    }
971
27.1M
                }
972
670M
                for (x = 0; x < GBW; x++) {
973
643M
                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
974
0
                        bit = 0;
975
643M
                    } else {
976
643M
                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
977
643M
                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
978
643M
                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
979
643M
                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
980
643M
                        if (bit < 0)
981
0
                            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
982
643M
                    }
983
643M
                    pd = pd<<1;
984
643M
                    ppd = ppd<<1;
985
643M
                    out_byte = (out_byte<<1) | bit;
986
643M
                    out_bits_to_go_in_byte--;
987
643M
                    if (out_bits_to_go_in_byte == 0) {
988
79.1M
                        out_bits_to_go_in_byte = 8;
989
79.1M
                        *d++ = (uint8_t)out_byte;
990
79.1M
                        if (x+9 < GBW && y > 0 && pline) {
991
23.5M
                            pd |= *pline++;
992
23.5M
                            if (y > 1 && ppline)
993
20.8M
                                ppd |= *ppline++;
994
23.5M
                        }
995
79.1M
                    }
996
643M
                }
997
27.1M
                if (out_bits_to_go_in_byte != 8)
998
8.51M
                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
999
27.1M
            } else {
1000
27.1M
                copy_prev_row(image, y);
1001
27.1M
            }
1002
54.3M
        }
1003
1.34k
        return 0;
1004
1.34k
    }
1005
1006
    /* We divide the width into 3 regions 0..left...right...GBW,
1007
     * between left and right, we know that our accesses will never
1008
     * step outside the image, enabling us to use faster accessors. */
1009
7.19k
    left = 4;
1010
7.19k
    right = 2;
1011
7.19k
    gmin = gmax = params->gbat[0];
1012
7.19k
    if (params->gbat[2] < gmin)
1013
2.71k
        gmin = params->gbat[2];
1014
7.19k
    if (gmax < params->gbat[2])
1015
3.27k
        gmax = params->gbat[2];
1016
7.19k
    if (params->gbat[4] < gmin)
1017
994
        gmin = params->gbat[4];
1018
7.19k
    if (gmax < params->gbat[4])
1019
1.03k
        gmax = params->gbat[4];
1020
7.19k
    if (params->gbat[6] < gmin)
1021
1.10k
        gmin = params->gbat[6];
1022
7.19k
    if (gmax < params->gbat[6])
1023
2.98k
        gmax = params->gbat[6];
1024
7.19k
    if (left < -((int64_t) gmin))
1025
4.46k
        left = -((int64_t) gmin);
1026
7.19k
    if (right < gmax)
1027
3.86k
        right = gmax;
1028
7.19k
    if (right > GBW)
1029
2.49k
        right = GBW;
1030
7.19k
    right = GBW - right;
1031
    /* So 0 <= x < left or right <= x < GBW needs bounds checking. */
1032
1033
    /* Now we do the same for the height, but here there is no bottom
1034
     * region, as we only ever look up for y. */
1035
7.19k
    top = 2;
1036
7.19k
    gmin = params->gbat[1];
1037
7.19k
    if (params->gbat[3] < gmin)
1038
2.17k
        gmin = params->gbat[3];
1039
7.19k
    if (params->gbat[5] < gmin)
1040
3.91k
        gmin = params->gbat[5];
1041
7.19k
    if (params->gbat[7] < gmin)
1042
1.51k
        gmin = params->gbat[7];
1043
7.19k
    if (top < -((int64_t) gmin))
1044
4.47k
        top = -((int64_t) gmin);
1045
    /* So 0 <= y < top needs bounds checking. */
1046
1047
142M
    for (y = 0; y < GBH; y++) {
1048
142M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
1049
142M
        if (bit < 0)
1050
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
1051
142M
        LTP ^= bit;
1052
142M
        if (!LTP) {
1053
93.7M
            uint32_t out_byte = 0;
1054
93.7M
            int out_bits_to_go_in_byte = 8;
1055
93.7M
            uint8_t *d = &image->data[image->stride * y];
1056
93.7M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1057
93.7M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1058
93.7M
            uint32_t pd = 0;
1059
93.7M
            uint32_t ppd = 0;
1060
93.7M
            if (y > 0 && pline) {
1061
93.7M
                pd = (*pline++ << 8);
1062
93.7M
                if (GBW > 8)
1063
3.24M
                    pd |= *pline++;
1064
93.7M
                if (y > 1 && ppline) {
1065
93.7M
                    ppd = (*ppline++ << 8);
1066
93.7M
                    if (GBW > 8)
1067
3.24M
                        ppd |= *ppline++;
1068
93.7M
                }
1069
93.7M
            }
1070
1.07G
            for (x = 0; x < GBW; x++) {
1071
983M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1072
0
                    bit = 0;
1073
983M
                } else {
1074
983M
                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
1075
983M
                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
1076
983M
                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
1077
983M
                    if (y >= top && x >= left && x < right)
1078
435M
                    {
1079
435M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1080
435M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1081
435M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1082
435M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1083
435M
                    }
1084
547M
                    else
1085
547M
                    {
1086
547M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1087
547M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1088
547M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1089
547M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1090
547M
                    }
1091
983M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1092
983M
                    if (bit < 0)
1093
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
1094
983M
                }
1095
983M
                pd = pd<<1;
1096
983M
                ppd = ppd<<1;
1097
983M
                out_byte = (out_byte<<1) | bit;
1098
983M
                out_bits_to_go_in_byte--;
1099
983M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1100
983M
                if (out_bits_to_go_in_byte == 0) {
1101
100M
                    out_bits_to_go_in_byte = 8;
1102
100M
                    d++;
1103
100M
                    if (x+9 < GBW && y > 0 && pline) {
1104
74.8M
                        pd |= *pline++;
1105
74.8M
                        if (y > 1 && ppline)
1106
69.8M
                            ppd |= *ppline++;
1107
74.8M
                    }
1108
100M
                }
1109
983M
            }
1110
93.7M
            if (out_bits_to_go_in_byte != 8)
1111
92.5M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1112
93.7M
        } else {
1113
49.1M
            copy_prev_row(image, y);
1114
49.1M
        }
1115
142M
    }
1116
1117
7.19k
    return 0;
1118
7.19k
}
1119
1120
static int
1121
jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx,
1122
                                      Jbig2Segment *segment,
1123
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1124
1.62k
{
1125
1.62k
    const int64_t GBW = image->width;
1126
1.62k
    const int64_t GBH = image->height;
1127
1.62k
    uint32_t CONTEXT;
1128
1.62k
    int64_t x, y;
1129
1.62k
    int LTP = 0;
1130
1131
1.62k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1132
6
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1133
6
                           "adaptive template pixel is out of field");
1134
1135
106M
    for (y = 0; y < GBH; y++) {
1136
106M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
1137
106M
        if (bit < 0)
1138
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
1139
106M
        LTP ^= bit;
1140
106M
        if (!LTP) {
1141
52.4M
            uint32_t out_byte = 0;
1142
52.4M
            int out_bits_to_go_in_byte = 8;
1143
52.4M
            uint8_t *d = &image->data[image->stride * y];
1144
52.4M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1145
52.4M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1146
52.4M
            uint32_t pd = 0;
1147
52.4M
            uint32_t ppd = 0;
1148
52.4M
            if (y > 0 && pline) {
1149
52.4M
                pd = (*pline++ << 8);
1150
52.4M
                if (GBW > 8)
1151
3.14M
                    pd |= *pline++;
1152
52.4M
                if (y > 1 && ppline) {
1153
52.4M
                    ppd = (*ppline++ << 8);
1154
52.4M
                    if (GBW > 8)
1155
3.14M
                        ppd |= *ppline++;
1156
52.4M
                }
1157
52.4M
            }
1158
1.56G
            for (x = 0; x < GBW; x++) {
1159
1.51G
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1160
0
                    bit = 0;
1161
1.51G
                } else {
1162
1.51G
                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
1163
1.51G
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
1164
1.51G
                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
1165
1.51G
                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
1166
1.51G
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1167
1.51G
                    if (bit < 0)
1168
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
1169
1.51G
                }
1170
1.51G
                pd = pd<<1;
1171
1.51G
                ppd = ppd<<1;
1172
1.51G
                out_byte = (out_byte<<1) | bit;
1173
1.51G
                out_bits_to_go_in_byte--;
1174
1.51G
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1175
1.51G
                if (out_bits_to_go_in_byte == 0) {
1176
165M
                    out_bits_to_go_in_byte = 8;
1177
165M
                    d++;
1178
165M
                    if (x+9 < GBW && y > 0 && pline) {
1179
141M
                        pd |= *pline++;
1180
141M
                        if (y > 1 && ppline)
1181
101M
                            ppd |= *ppline++;
1182
141M
                    }
1183
165M
                }
1184
1.51G
            }
1185
52.4M
            if (out_bits_to_go_in_byte != 8)
1186
50.2M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1187
54.0M
        } else {
1188
54.0M
            copy_prev_row(image, y);
1189
54.0M
        }
1190
106M
    }
1191
1192
1.62k
    return 0;
1193
1.62k
}
1194
1195
static int
1196
jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx,
1197
                                      Jbig2Segment *segment,
1198
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1199
4.97k
{
1200
4.97k
    const int64_t GBW = image->width;
1201
4.97k
    const int64_t GBH = image->height;
1202
4.97k
    uint32_t CONTEXT;
1203
4.97k
    int64_t x, y;
1204
4.97k
    int LTP = 0;
1205
1206
4.97k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1207
7
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1208
7
                           "adaptive template pixel is out of field");
1209
1210
79.3M
    for (y = 0; y < GBH; y++) {
1211
79.3M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
1212
79.3M
        if (bit < 0)
1213
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
1214
79.3M
        LTP ^= bit;
1215
79.3M
        if (!LTP) {
1216
28.7M
            uint32_t out_byte = 0;
1217
28.7M
            int out_bits_to_go_in_byte = 8;
1218
28.7M
            uint8_t *d = &image->data[image->stride * y];
1219
28.7M
            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1220
28.7M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1221
28.7M
            uint32_t pd = 0;
1222
28.7M
            uint32_t ppd = 0;
1223
28.7M
            if (y > 0 && pline) {
1224
28.7M
                pd = (*pline++ << 8);
1225
28.7M
                if (GBW > 8)
1226
1.18M
                    pd |= *pline++;
1227
28.7M
                if (y > 1 && ppline) {
1228
28.7M
                    ppd = (*ppline++ << 8);
1229
28.7M
                    if (GBW > 8)
1230
1.18M
                        ppd |= *ppline++;
1231
28.7M
                }
1232
28.7M
            }
1233
379M
            for (x = 0; x < GBW; x++) {
1234
350M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1235
0
                    bit = 0;
1236
350M
                } else {
1237
350M
                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
1238
350M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
1239
350M
                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
1240
350M
                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
1241
350M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1242
350M
                    if (bit < 0)
1243
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
1244
350M
                }
1245
350M
                pd = pd<<1;
1246
350M
                ppd = ppd<<1;
1247
350M
                out_byte = (out_byte<<1) | bit;
1248
350M
                out_bits_to_go_in_byte--;
1249
350M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1250
350M
                if (out_bits_to_go_in_byte == 0) {
1251
39.8M
                    out_bits_to_go_in_byte = 8;
1252
39.8M
                    d++;
1253
39.8M
                    if (x+9 < GBW && y > 0 && pline) {
1254
30.5M
                        pd |= *pline++;
1255
30.5M
                        if (y > 1 && ppline)
1256
27.1M
                            ppd |= *ppline++;
1257
30.5M
                    }
1258
39.8M
                }
1259
350M
            }
1260
28.7M
            if (out_bits_to_go_in_byte != 8)
1261
27.2M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1262
50.5M
        } else {
1263
50.5M
            copy_prev_row(image, y);
1264
50.5M
        }
1265
79.3M
    }
1266
1267
4.96k
    return 0;
1268
4.96k
}
1269
1270
static int
1271
jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx,
1272
                                      Jbig2Segment *segment,
1273
                                      const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1274
746
{
1275
746
    const int64_t GBW = image->width;
1276
746
    const int64_t GBH = image->height;
1277
746
    uint32_t CONTEXT;
1278
746
    int64_t x, y;
1279
746
    int LTP = 0;
1280
1281
746
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1282
5
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1283
5
                           "adaptive template pixel is out of field");
1284
1285
185M
    for (y = 0; y < GBH; y++) {
1286
185M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
1287
185M
        if (bit < 0)
1288
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
1289
185M
        LTP ^= bit;
1290
185M
        if (!LTP) {
1291
125M
            uint32_t out_byte = 0;
1292
125M
            int out_bits_to_go_in_byte = 8;
1293
125M
            uint8_t *d = &image->data[image->stride * y];
1294
125M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1295
125M
            uint32_t pd = 0;
1296
125M
            if (y > 0 && pline) {
1297
125M
                pd = (*pline++ << 8);
1298
125M
                if (GBW > 8)
1299
475k
                    pd |= *pline++;
1300
125M
            }
1301
1.13G
            for (x = 0; x < GBW; x++) {
1302
1.01G
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1303
0
                    bit = 0;
1304
1.01G
                } else {
1305
1.01G
                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
1306
1.01G
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1307
1.01G
                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
1308
1.01G
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1309
1.01G
                    if (bit < 0)
1310
0
                        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
1311
1.01G
                }
1312
1.01G
                pd = pd<<1;
1313
1.01G
                out_byte = (out_byte<<1) | bit;
1314
1.01G
                out_bits_to_go_in_byte--;
1315
1.01G
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1316
1.01G
                if (out_bits_to_go_in_byte == 0) {
1317
96.2M
                    out_bits_to_go_in_byte = 8;
1318
96.2M
                    d++;
1319
96.2M
                    if (x+9 < GBW && y > 0 && pline)
1320
62.3M
                        pd |= *pline++;
1321
96.2M
                }
1322
1.01G
            }
1323
125M
            if (out_bits_to_go_in_byte != 8)
1324
125M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1325
125M
        } else {
1326
59.3M
            copy_prev_row(image, y);
1327
59.3M
        }
1328
185M
    }
1329
1330
741
    return 0;
1331
741
}
1332
1333
static int
1334
jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx,
1335
                                   Jbig2Segment *segment,
1336
                                   const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1337
15.9k
{
1338
15.9k
    switch (params->GBTEMPLATE) {
1339
8.57k
    case 0:
1340
8.57k
        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
1341
1.62k
    case 1:
1342
1.62k
        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
1343
4.97k
    case 2:
1344
4.97k
        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
1345
746
    case 3:
1346
746
        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
1347
15.9k
    }
1348
1349
0
    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
1350
15.9k
}
1351
1352
/**
1353
 * jbig2_decode_generic_region: Decode a generic region.
1354
 * @ctx: The context for allocation and error reporting.
1355
 * @segment: A segment reference for error reporting.
1356
 * @params: Decoding parameter set.
1357
 * @as: Arithmetic decoder state.
1358
 * @image: Where to store the decoded data.
1359
 * @GB_stats: Arithmetic stats.
1360
 *
1361
 * Decodes a generic region, according to section 6.2. The caller should
1362
 * pass an already allocated Jbig2Image object for @image
1363
 *
1364
 * Because this API is based on an arithmetic decoding state, it is
1365
 * not suitable for MMR decoding.
1366
 *
1367
 * Return code: 0 on success.
1368
 **/
1369
int
1370
jbig2_decode_generic_region(Jbig2Ctx *ctx,
1371
                            Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1372
179k
{
1373
179k
    const int8_t *gbat = params->gbat;
1374
1375
179k
    if (!params->MMR && params->TPGDON)
1376
15.9k
        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
1377
1378
163k
    if (!params->MMR && params->GBTEMPLATE == 0) {
1379
20.8k
        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)
1380
6.92k
            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
1381
13.8k
        else
1382
13.8k
            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
1383
142k
    } else if (!params->MMR && params->GBTEMPLATE == 1) {
1384
11.1k
        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
1385
5.43k
            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
1386
5.73k
        else
1387
5.73k
            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
1388
11.1k
    }
1389
131k
    else if (!params->MMR && params->GBTEMPLATE == 2) {
1390
120k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1391
82.3k
            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
1392
38.3k
        else
1393
38.3k
            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
1394
120k
    } else if (!params->MMR && params->GBTEMPLATE == 3) {
1395
10.7k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1396
4.67k
            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
1397
6.04k
        else
1398
6.04k
            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
1399
10.7k
    }
1400
1401
0
    {
1402
0
        int i;
1403
1404
0
        for (i = 0; i < 8; i++)
1405
0
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]);
1406
0
    }
1407
1408
0
    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported generic region (MMR=%d, GBTEMPLATE=%d)", params->MMR, params->GBTEMPLATE);
1409
163k
}
1410
1411
/**
1412
 * Handler for immediate generic region segments
1413
 */
1414
int
1415
jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
1416
20.9k
{
1417
20.9k
    Jbig2RegionSegmentInfo rsi;
1418
20.9k
    byte seg_flags;
1419
20.9k
    int8_t gbat[8];
1420
20.9k
    int offset;
1421
20.9k
    uint32_t gbat_bytes = 0;
1422
20.9k
    Jbig2GenericRegionParams params;
1423
20.9k
    int code = 0;
1424
20.9k
    Jbig2Image *image = NULL;
1425
20.9k
    Jbig2WordStream *ws = NULL;
1426
20.9k
    Jbig2ArithState *as = NULL;
1427
20.9k
    Jbig2ArithCx *GB_stats = NULL;
1428
20.9k
    uint32_t height;
1429
20.9k
    Jbig2Page *page = &ctx->pages[ctx->current_page];
1430
1431
    /* 7.4.6 */
1432
20.9k
    if (segment->data_length < 18)
1433
46
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1434
1435
20.8k
    jbig2_get_region_segment_info(&rsi, segment_data);
1436
20.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);
1437
1438
    /* 7.4.6.4 */
1439
20.8k
    height = rsi.height;
1440
20.8k
    if (segment->rows != UINT32_MAX) {
1441
14.5k
        if (segment->rows > rsi.height)
1442
42
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
1443
14.5k
        height = segment->rows;
1444
14.5k
    }
1445
1446
    /* 7.4.6.2 */
1447
20.8k
    seg_flags = segment_data[17];
1448
20.8k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
1449
20.8k
    if ((seg_flags & 1) && (seg_flags & 6))
1450
673
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
1451
1452
    /* 7.4.6.3 */
1453
20.8k
    if (!(seg_flags & 1)) {
1454
19.2k
        gbat_bytes = (seg_flags & 6) ? 2 : 8;
1455
19.2k
        if (18 + gbat_bytes > segment->data_length)
1456
12
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1457
19.2k
        memcpy(gbat, segment_data + 18, gbat_bytes);
1458
19.2k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
1459
19.2k
    }
1460
1461
20.8k
    offset = 18 + gbat_bytes;
1462
1463
    /* Check for T.88 amendment 2 */
1464
20.8k
    if ((seg_flags >> 5) & 1)
1465
34
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
1466
1467
    /* Table 34 */
1468
20.8k
    params.MMR = seg_flags & 1;
1469
20.8k
    params.GBTEMPLATE = (seg_flags & 6) >> 1;
1470
20.8k
    params.TPGDON = (seg_flags & 8) >> 3;
1471
20.8k
    params.USESKIP = 0;
1472
20.8k
    memcpy(params.gbat, gbat, gbat_bytes);
1473
1474
20.8k
    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
1475
12.0k
        if (rsi.y >= page->end_row + page->stripe_size) {
1476
2.30k
            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);
1477
2.30k
            return 0;
1478
2.30k
        }
1479
9.78k
        if (height > page->end_row + page->stripe_size) {
1480
8.28k
            height = page->end_row + page->stripe_size;
1481
8.28k
        }
1482
9.78k
    } else {
1483
8.71k
        if (rsi.y >= page->height) {
1484
732
            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);
1485
732
            return 0;
1486
732
        }
1487
7.98k
        if (height > page->height - rsi .y) {
1488
6.08k
            height = page->height - rsi.y;
1489
6.08k
        }
1490
7.98k
    }
1491
17.7k
    if (height == 0) {
1492
1.05k
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
1493
1.05k
        return 0;
1494
1.05k
    }
1495
1496
16.7k
    image = jbig2_image_new(ctx, rsi.width, height);
1497
16.7k
    if (image == NULL)
1498
70
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
1499
16.6k
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
1500
1501
16.6k
    if (params.MMR) {
1502
430
        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
1503
430
        if (code < 0) {
1504
29
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
1505
29
            goto cleanup;
1506
29
        }
1507
16.2k
    } else {
1508
16.2k
        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
1509
1510
16.2k
        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
1511
16.2k
        if (GB_stats == NULL) {
1512
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states when handling immediate generic region");
1513
1
            goto cleanup;
1514
1
        }
1515
16.2k
        memset(GB_stats, 0, stats_size);
1516
1517
16.2k
        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
1518
16.2k
        if (ws == NULL) {
1519
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocated word stream when handling immediate generic region");
1520
1
            goto cleanup;
1521
1
        }
1522
16.2k
        as = jbig2_arith_new(ctx, ws);
1523
16.2k
        if (as == NULL) {
1524
5
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
1525
5
            goto cleanup;
1526
5
        }
1527
16.2k
        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
1528
16.2k
        if (code < 0) {
1529
76
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
1530
76
            goto cleanup;
1531
76
        }
1532
16.2k
    }
1533
1534
16.5k
    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
1535
16.5k
    if (code < 0)
1536
729
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
1537
1538
16.6k
cleanup:
1539
16.6k
    jbig2_free(ctx->allocator, as);
1540
16.6k
    jbig2_word_stream_buf_free(ctx, ws);
1541
16.6k
    jbig2_free(ctx->allocator, GB_stats);
1542
16.6k
    jbig2_image_release(ctx, image);
1543
1544
16.6k
    return code;
1545
16.5k
}