Coverage Report

Created: 2026-10-30 06:45

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.80G
{
349
1.80G
    size_t sx = (size_t) x;
350
1.80G
    size_t sy = (size_t) y;
351
1.80G
    size_t byte = (sx >> 3) + sy * image->stride;
352
1.80G
    int bit = 7 - ((int) (sx & 7));
353
354
1.80G
    return ((image->data[byte] >> bit) & 1);
355
1.80G
}
356
357
/* return the appropriate context size for the given template */
358
int
359
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template)
360
22.7k
{
361
22.7k
    int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
362
363
22.7k
    (void) ctx;
364
365
22.7k
    return stats_size;
366
22.7k
}
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
7.70k
{
374
7.70k
    const int64_t GBW = image->width;
375
7.70k
    const int64_t GBH = image->height;
376
7.70k
    const uint32_t rowstride = image->stride;
377
7.70k
    int64_t x, y;
378
7.70k
    byte *line2 = NULL;
379
7.70k
    byte *line1 = NULL;
380
7.70k
    byte *gbreg_line = (byte *) image->data;
381
382
7.70k
    (void) ctx;
383
7.70k
    (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
7.70k
    if (GBW <= 0)
392
0
        return 0;
393
394
152M
    for (y = 0; y < GBH; y++) {
395
152M
        uint32_t CONTEXT;
396
152M
        uint32_t line_m1;
397
152M
        uint32_t line_m2;
398
152M
        int64_t padded_width = (GBW + 7) & -8;
399
400
152M
        line_m1 = line1 ? line1[0] : 0;
401
152M
        line_m2 = line2 ? line2[0] << 6 : 0;
402
152M
        CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
403
404
        /* 6.2.5.7 3d */
405
404M
        for (x = 0; x < padded_width; x += 8) {
406
251M
            byte result = 0;
407
251M
            int64_t x_minor;
408
251M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
409
410
251M
            if (line1)
411
218M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
412
413
251M
            if (line2)
414
197M
                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.66G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
418
1.41G
                int bit;
419
420
1.41G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
421
1.41G
                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.41G
                result |= bit << (7 - x_minor);
424
1.41G
                CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
425
1.41G
            }
426
251M
            gbreg_line[x >> 3] = result;
427
251M
        }
428
#ifdef OUTPUT_PBM
429
        fwrite(gbreg_line, 1, rowstride, stdout);
430
#endif
431
152M
        line2 = line1;
432
152M
        line1 = gbreg_line;
433
152M
        gbreg_line += rowstride;
434
152M
    }
435
436
7.70k
    return 0;
437
7.70k
}
438
439
#define pixel_outside_field(x, y) \
440
223k
    ((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
15.4k
{
447
15.4k
    const int64_t GBW = image->width;
448
15.4k
    const int64_t GBH = image->height;
449
15.4k
    uint32_t CONTEXT;
450
15.4k
    int64_t x, y;
451
15.4k
    int bit;
452
453
15.4k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
454
15.4k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
455
15.4k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
456
15.4k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
457
40
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
458
40
                           "adaptive template pixel is out of field");
459
460
115M
    for (y = 0; y < GBH; y++) {
461
114M
        uint32_t out_byte = 0;
462
114M
        int out_bits_to_go_in_byte = 8;
463
114M
        uint8_t *d = &image->data[image->stride * y];
464
114M
        uint32_t pd = 0;
465
114M
        uint32_t ppd = 0;
466
114M
        uint8_t *pline = NULL;
467
114M
        uint8_t *ppline = NULL;
468
114M
        if (y >= 1)
469
114M
        {
470
114M
            pline  = &image->data[image->stride * (y-1)];
471
114M
            pd = (*pline++ << 8);
472
114M
            if (GBW > 8)
473
868k
                pd |= *pline++;
474
114M
        }
475
114M
        if (y >= 2) {
476
114M
            ppline = &image->data[image->stride * (y-2)];
477
114M
            ppd = (*ppline++ << 8);
478
114M
            if (GBW > 8)
479
856k
                ppd |= *ppline++;
480
114M
        }
481
991M
        for (x = 0; x < GBW; x++) {
482
876M
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
483
141M
                bit = 0;
484
735M
            } else {
485
735M
                CONTEXT  = out_byte & 0x000F; /* First 4 pixels */
486
735M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
487
735M
                CONTEXT |= (pd>>8) & 0x03E0; /* Next 5 pixels */
488
735M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
489
735M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
490
735M
                CONTEXT |= (ppd>>2) & 0x7000; /* Next 3 pixels */
491
735M
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
492
735M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
493
735M
                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
735M
            }
496
876M
            pd = pd<<1;
497
876M
            ppd = ppd<<1;
498
876M
            out_byte = (out_byte<<1) | bit;
499
876M
            out_bits_to_go_in_byte--;
500
876M
            *d = out_byte<<out_bits_to_go_in_byte;
501
876M
            if (out_bits_to_go_in_byte == 0) {
502
79.1M
                out_bits_to_go_in_byte = 8;
503
79.1M
                d++;
504
79.1M
                if (x+9 < GBW && pline != NULL) {
505
45.9M
                    pd |= *pline++;
506
45.9M
                    if (ppline != NULL)
507
37.8M
                        ppd |= *ppline++;
508
45.9M
                }
509
79.1M
            }
510
876M
        }
511
114M
        if (out_bits_to_go_in_byte != 8)
512
114M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
513
114M
    }
514
15.4k
    return 0;
515
15.4k
}
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
6.80k
{
522
6.80k
    const int64_t GBW = image->width;
523
6.80k
    const int64_t GBH = image->height;
524
6.80k
    uint32_t CONTEXT;
525
6.80k
    int64_t x, y;
526
6.80k
    int bit;
527
528
6.80k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
529
9
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
530
9
                           "adaptive template pixel is out of field");
531
532
205M
    for (y = 0; y < GBH; y++) {
533
205M
        uint32_t out_byte = 0;
534
205M
        int out_bits_to_go_in_byte = 8;
535
205M
        uint8_t *d = &image->data[image->stride * y];
536
205M
        uint32_t pd = 0;
537
205M
        uint32_t ppd = 0;
538
205M
        uint8_t *pline = NULL;
539
205M
        uint8_t *ppline = NULL;
540
205M
        if (y >= 1) {
541
205M
            pline  = &image->data[image->stride * (y-1)];
542
205M
            pd = (*pline++ << 8);
543
205M
            if (GBW > 8)
544
739k
                pd |= *pline++;
545
205M
        }
546
205M
        if (y >= 2) {
547
205M
            ppline = &image->data[image->stride * (y-2)];
548
205M
            ppd = (*ppline++ << 8);
549
205M
            if (GBW > 8)
550
737k
                ppd |= *ppline++;
551
205M
        }
552
1.61G
        for (x = 0; x < GBW; x++) {
553
1.41G
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
554
28.5M
                bit = 0;
555
1.38G
            } else {
556
1.38G
                CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
557
1.38G
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
558
1.38G
                CONTEXT |= (pd>>9) & 0x01F0; /* Next 5 pixels */
559
1.38G
                CONTEXT |= (ppd>>4) & 0x1E00; /* Next 4 pixels */
560
1.38G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
561
1.38G
                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.38G
            }
564
1.41G
            pd = pd<<1;
565
1.41G
            ppd = ppd<<1;
566
1.41G
            out_byte = (out_byte<<1) | bit;
567
1.41G
            out_bits_to_go_in_byte--;
568
1.41G
            *d = out_byte<<out_bits_to_go_in_byte;
569
1.41G
            if (out_bits_to_go_in_byte == 0) {
570
142M
                out_bits_to_go_in_byte = 8;
571
142M
                d++;
572
142M
                if (x+9 < GBW && pline != NULL) {
573
63.2M
                    pd |= *pline++;
574
63.2M
                    if (ppline != NULL)
575
51.3M
                        ppd |= *ppline++;
576
63.2M
                }
577
142M
            }
578
1.41G
        }
579
205M
        if (out_bits_to_go_in_byte != 8)
580
192M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
581
205M
    }
582
6.80k
    return 0;
583
6.80k
}
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
1.24k
{
590
1.24k
    const int64_t GBW = image->width;
591
1.24k
    const int64_t GBH = image->height;
592
1.24k
    const uint32_t rowstride = image->stride;
593
1.24k
    int64_t x, y;
594
1.24k
    byte *line2 = NULL;
595
1.24k
    byte *line1 = NULL;
596
1.24k
    byte *gbreg_line = (byte *) image->data;
597
598
1.24k
    (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
1.24k
    if (GBW <= 0)
607
0
        return 0;
608
609
113M
    for (y = 0; y < GBH; y++) {
610
113M
        uint32_t CONTEXT;
611
113M
        uint32_t line_m1;
612
113M
        uint32_t line_m2;
613
113M
        int64_t padded_width = (GBW + 7) & -8;
614
615
113M
        line_m1 = line1 ? line1[0] : 0;
616
113M
        line_m2 = line2 ? line2[0] << 5 : 0;
617
113M
        CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
618
619
        /* 6.2.5.7 3d */
620
349M
        for (x = 0; x < padded_width; x += 8) {
621
235M
            byte result = 0;
622
235M
            int64_t x_minor;
623
235M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
624
625
235M
            if (line1)
626
192M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
627
628
235M
            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.42G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
633
1.18G
                int bit;
634
635
1.18G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
636
1.18G
                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.18G
                result |= bit << (7 - x_minor);
639
1.18G
                CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
640
1.18G
            }
641
235M
            gbreg_line[x >> 3] = result;
642
235M
        }
643
#ifdef OUTPUT_PBM
644
        fwrite(gbreg_line, 1, rowstride, stdout);
645
#endif
646
113M
        line2 = line1;
647
113M
        line1 = gbreg_line;
648
113M
        gbreg_line += rowstride;
649
113M
    }
650
651
1.24k
    return 0;
652
1.24k
}
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.6k
{
659
38.6k
    const int64_t GBW = image->width;
660
38.6k
    const int64_t GBH = image->height;
661
38.6k
    uint32_t CONTEXT;
662
38.6k
    int64_t x, y;
663
38.6k
    int bit;
664
665
38.6k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
666
8
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
667
8
                           "adaptive template pixel is out of field");
668
669
185M
    for (y = 0; y < GBH; y++) {
670
185M
        uint32_t out_byte = 0;
671
185M
        int out_bits_to_go_in_byte = 8;
672
185M
        uint8_t *d = &image->data[image->stride * y];
673
185M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
674
185M
        uint8_t *ppline = &image->data[image->stride * (y-2)];
675
185M
        uint32_t pd = 0;
676
185M
        uint32_t ppd = 0;
677
185M
        if (y > 0) {
678
185M
            pd = (*pline++ << 8);
679
185M
            if (GBW > 8)
680
587k
                pd |= *pline++;
681
185M
            if (y > 1) {
682
185M
                ppd = (*ppline++ << 8);
683
185M
                if (GBW > 8)
684
549k
                    ppd |= *ppline++;
685
185M
            }
686
185M
        }
687
1.56G
        for (x = 0; x < GBW; x++) {
688
1.37G
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
689
106M
                bit = 0;
690
1.26G
            } else {
691
1.26G
                CONTEXT  = out_byte & 0x003; /* First 2 pixels */
692
1.26G
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
693
1.26G
                CONTEXT |= (pd>>11) & 0x078; /* Next 4 pixels */
694
1.26G
                CONTEXT |= (ppd>>7) & 0x380; /* Next 3 pixels */
695
1.26G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
696
1.26G
                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.26G
            }
699
1.37G
            pd = pd<<1;
700
1.37G
            ppd = ppd<<1;
701
1.37G
            out_byte = (out_byte<<1) | bit;
702
1.37G
            out_bits_to_go_in_byte--;
703
1.37G
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
704
1.37G
            if (out_bits_to_go_in_byte == 0) {
705
132M
                out_bits_to_go_in_byte = 8;
706
132M
                d++;
707
132M
                if (x+9 < GBW && y > 0) {
708
78.9M
                    pd |= *pline++;
709
78.9M
                    if (y > 1)
710
70.1M
                        ppd |= *ppline++;
711
78.9M
                }
712
132M
            }
713
1.37G
        }
714
185M
        if (out_bits_to_go_in_byte != 8)
715
185M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
716
185M
    }
717
718
38.6k
    return 0;
719
38.6k
}
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
76.0k
{
726
76.0k
    const int64_t GBW = image->width;
727
76.0k
    const int64_t GBH = image->height;
728
76.0k
    const uint32_t rowstride = image->stride;
729
76.0k
    int64_t x, y;
730
76.0k
    byte *line2 = NULL;
731
76.0k
    byte *line1 = NULL;
732
76.0k
    byte *gbreg_line = (byte *) image->data;
733
734
76.0k
    (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
76.0k
    if (GBW <= 0)
743
0
        return 0;
744
745
152M
    for (y = 0; y < GBH; y++) {
746
152M
        uint32_t CONTEXT;
747
152M
        uint32_t line_m1;
748
152M
        uint32_t line_m2;
749
152M
        int64_t padded_width = (GBW + 7) & -8;
750
751
152M
        line_m1 = line1 ? line1[0] : 0;
752
152M
        line_m2 = line2 ? line2[0] << 4 : 0;
753
152M
        CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
754
755
        /* 6.2.5.7 3d */
756
412M
        for (x = 0; x < padded_width; x += 8) {
757
259M
            byte result = 0;
758
259M
            int64_t x_minor;
759
259M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
760
761
259M
            if (line1)
762
198M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
763
764
259M
            if (line2)
765
169M
                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.30G
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
769
1.04G
                int bit;
770
771
1.04G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
772
1.04G
                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.04G
                result |= bit << (7 - x_minor);
775
1.04G
                CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
776
1.04G
            }
777
259M
            gbreg_line[x >> 3] = result;
778
259M
        }
779
#ifdef OUTPUT_PBM
780
        fwrite(gbreg_line, 1, rowstride, stdout);
781
#endif
782
152M
        line2 = line1;
783
152M
        line1 = gbreg_line;
784
152M
        gbreg_line += rowstride;
785
152M
    }
786
787
76.0k
    return 0;
788
76.0k
}
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.61k
{
795
4.61k
    const int64_t GBW = image->width;
796
4.61k
    const int64_t GBH = image->height;
797
4.61k
    const uint32_t rowstride = image->stride;
798
4.61k
    byte *line1 = NULL;
799
4.61k
    byte *gbreg_line = (byte *) image->data;
800
4.61k
    int64_t x, y;
801
802
4.61k
    (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.61k
    if (GBW <= 0)
811
0
        return 0;
812
813
45.6k
    for (y = 0; y < GBH; y++) {
814
41.0k
        uint32_t CONTEXT;
815
41.0k
        uint32_t line_m1;
816
41.0k
        int64_t padded_width = (GBW + 7) & -8;
817
818
41.0k
        line_m1 = line1 ? line1[0] : 0;
819
41.0k
        CONTEXT = (line_m1 >> 1) & 0x3f0;
820
821
        /* 6.2.5.7 3d */
822
100M
        for (x = 0; x < padded_width; x += 8) {
823
100M
            byte result = 0;
824
100M
            int64_t x_minor;
825
100M
            int64_t minor_width = GBW - x > 8 ? 8 : GBW - x;
826
827
100M
            if (line1)
828
60.3M
                line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
829
830
            /* This is the speed-critical inner loop. */
831
902M
            for (x_minor = 0; x_minor < minor_width; x_minor++) {
832
801M
                int bit;
833
834
801M
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
835
801M
                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
801M
                result |= bit << (7 - x_minor);
838
801M
                CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
839
801M
            }
840
100M
            gbreg_line[x >> 3] = result;
841
100M
        }
842
#ifdef OUTPUT_PBM
843
        fwrite(gbreg_line, 1, rowstride, stdout);
844
#endif
845
41.0k
        line1 = gbreg_line;
846
41.0k
        gbreg_line += rowstride;
847
41.0k
    }
848
849
4.61k
    return 0;
850
4.61k
}
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
4.01k
{
857
4.01k
    const int64_t GBW = image->width;
858
4.01k
    const int64_t GBH = image->height;
859
4.01k
    uint32_t CONTEXT;
860
4.01k
    int64_t x, y;
861
4.01k
    int bit;
862
863
4.01k
    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
138M
    for (y = 0; y < GBH; y++) {
868
138M
        uint32_t out_byte = 0;
869
138M
        int out_bits_to_go_in_byte = 8;
870
138M
        uint8_t *d = &image->data[image->stride * y];
871
138M
        uint8_t *pline  = &image->data[image->stride * (y-1)];
872
138M
        uint32_t pd = 0;
873
138M
        if (y > 0) {
874
138M
            pd = (*pline++ << 8);
875
138M
            if (GBW > 8)
876
1.79M
                pd |= *pline++;
877
138M
        }
878
1.25G
        for (x = 0; x < GBW; x++) {
879
1.11G
            if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
880
86.6M
                bit = 0;
881
1.02G
            } else {
882
1.02G
                CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
883
1.02G
                CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
884
1.02G
                CONTEXT |= (pd>>9) & 0x3E0; /* Next 5 pixels */
885
1.02G
                bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
886
1.02G
                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
1.02G
            }
889
1.11G
            pd = pd<<1;
890
1.11G
            out_byte = (out_byte<<1) | bit;
891
1.11G
            out_bits_to_go_in_byte--;
892
1.11G
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
893
1.11G
            if (out_bits_to_go_in_byte == 0) {
894
117M
                out_bits_to_go_in_byte = 8;
895
117M
                d++;
896
117M
                if (x+9 < GBW && y > 0)
897
59.6M
                    pd |= *pline++;
898
117M
            }
899
1.11G
        }
900
138M
        if (out_bits_to_go_in_byte != 8)
901
137M
            *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
902
138M
    }
903
4.01k
    return 0;
904
4.01k
}
905
906
static void
907
copy_prev_row(Jbig2Image *image, int64_t row)
908
268M
{
909
268M
    if (!row) {
910
        /* no previous row */
911
5.13k
        memset(image->data, 0, image->stride);
912
268M
    } else {
913
        /* duplicate data from the previous row */
914
268M
        uint8_t *src = image->data + (row - 1) * image->stride;
915
916
268M
        memcpy(src + image->stride, src, image->stride);
917
268M
    }
918
268M
}
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
7.32k
{
925
7.32k
    const int64_t GBW = image->width;
926
7.32k
    const int64_t GBH = image->height;
927
7.32k
    uint32_t CONTEXT;
928
7.32k
    int64_t x, y;
929
7.32k
    int LTP = 0;
930
7.32k
    int gmin, gmax;
931
7.32k
    int64_t left, right, top;
932
933
7.32k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
934
7.31k
        pixel_outside_field(params->gbat[2], params->gbat[3]) ||
935
7.30k
        pixel_outside_field(params->gbat[4], params->gbat[5]) ||
936
7.30k
        pixel_outside_field(params->gbat[6], params->gbat[7]))
937
40
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
938
40
                           "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
7.28k
    if (params->gbat[0] ==  3 && params->gbat[1] == -1 &&
945
4.64k
        params->gbat[2] == -3 && params->gbat[3] == -1 &&
946
3.04k
        params->gbat[4] ==  2 && params->gbat[5] == -2 &&
947
2.51k
        params->gbat[6] == -2 && params->gbat[7] == -2)
948
1.13k
    {
949
85.9M
        for (y = 0; y < GBH; y++) {
950
85.9M
            int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
951
85.9M
            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
85.9M
            LTP ^= bit;
954
85.9M
            if (!LTP) {
955
43.0M
                uint32_t out_byte = 0;
956
43.0M
                int out_bits_to_go_in_byte = 8;
957
43.0M
                uint8_t *d = &image->data[image->stride * y];
958
43.0M
                uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
959
43.0M
                uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
960
43.0M
                uint32_t pd = 0;
961
43.0M
                uint32_t ppd = 0;
962
43.0M
                if (y > 0 && pline) {
963
43.0M
                    pd = (*pline++ << 8);
964
43.0M
                    if (GBW > 8)
965
8.57M
                        pd |= *pline++;
966
43.0M
                    if (y > 1 && ppline) {
967
43.0M
                        ppd = (*ppline++ << 8);
968
43.0M
                        if (GBW > 8)
969
8.57M
                            ppd |= *ppline++;
970
43.0M
                    }
971
43.0M
                }
972
832M
                for (x = 0; x < GBW; x++) {
973
789M
                    if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
974
0
                        bit = 0;
975
789M
                    } else {
976
789M
                        CONTEXT  = out_byte & 0x00F; /* First 4 pixels */
977
789M
                        CONTEXT |= (pd>>8) & 0x7F0; /* Next 7 pixels */
978
789M
                        CONTEXT |= (ppd>>2) & 0xF800; /* Final 5 pixels */
979
789M
                        bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
980
789M
                        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
789M
                    }
983
789M
                    pd = pd<<1;
984
789M
                    ppd = ppd<<1;
985
789M
                    out_byte = (out_byte<<1) | bit;
986
789M
                    out_bits_to_go_in_byte--;
987
789M
                    if (out_bits_to_go_in_byte == 0) {
988
95.3M
                        out_bits_to_go_in_byte = 8;
989
95.3M
                        *d++ = (uint8_t)out_byte;
990
95.3M
                        if (x+9 < GBW && y > 0 && pline) {
991
25.0M
                            pd |= *pline++;
992
25.0M
                            if (y > 1 && ppline)
993
20.4M
                                ppd |= *ppline++;
994
25.0M
                        }
995
95.3M
                    }
996
789M
                }
997
43.0M
                if (out_bits_to_go_in_byte != 8)
998
16.8M
                    *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
999
43.0M
            } else {
1000
42.9M
                copy_prev_row(image, y);
1001
42.9M
            }
1002
85.9M
        }
1003
1.13k
        return 0;
1004
1.13k
    }
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
6.15k
    left = 4;
1010
6.15k
    right = 2;
1011
6.15k
    gmin = gmax = params->gbat[0];
1012
6.15k
    if (params->gbat[2] < gmin)
1013
2.84k
        gmin = params->gbat[2];
1014
6.15k
    if (gmax < params->gbat[2])
1015
2.30k
        gmax = params->gbat[2];
1016
6.15k
    if (params->gbat[4] < gmin)
1017
1.02k
        gmin = params->gbat[4];
1018
6.15k
    if (gmax < params->gbat[4])
1019
609
        gmax = params->gbat[4];
1020
6.15k
    if (params->gbat[6] < gmin)
1021
1.13k
        gmin = params->gbat[6];
1022
6.15k
    if (gmax < params->gbat[6])
1023
2.86k
        gmax = params->gbat[6];
1024
6.15k
    if (left < -((int64_t) gmin))
1025
3.30k
        left = -((int64_t) gmin);
1026
6.15k
    if (right < gmax)
1027
3.81k
        right = gmax;
1028
6.15k
    if (right > GBW)
1029
2.42k
        right = GBW;
1030
6.15k
    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
6.15k
    top = 2;
1036
6.15k
    gmin = params->gbat[1];
1037
6.15k
    if (params->gbat[3] < gmin)
1038
1.56k
        gmin = params->gbat[3];
1039
6.15k
    if (params->gbat[5] < gmin)
1040
3.36k
        gmin = params->gbat[5];
1041
6.15k
    if (params->gbat[7] < gmin)
1042
1.52k
        gmin = params->gbat[7];
1043
6.15k
    if (top < -((int64_t) gmin))
1044
4.01k
        top = -((int64_t) gmin);
1045
    /* So 0 <= y < top needs bounds checking. */
1046
1047
121M
    for (y = 0; y < GBH; y++) {
1048
121M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x9B25]);
1049
121M
        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
121M
        LTP ^= bit;
1052
121M
        if (!LTP) {
1053
68.5M
            uint32_t out_byte = 0;
1054
68.5M
            int out_bits_to_go_in_byte = 8;
1055
68.5M
            uint8_t *d = &image->data[image->stride * y];
1056
68.5M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1057
68.5M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1058
68.5M
            uint32_t pd = 0;
1059
68.5M
            uint32_t ppd = 0;
1060
68.5M
            if (y > 0 && pline) {
1061
68.5M
                pd = (*pline++ << 8);
1062
68.5M
                if (GBW > 8)
1063
3.27M
                    pd |= *pline++;
1064
68.5M
                if (y > 1 && ppline) {
1065
68.5M
                    ppd = (*ppline++ << 8);
1066
68.5M
                    if (GBW > 8)
1067
3.27M
                        ppd |= *ppline++;
1068
68.5M
                }
1069
68.5M
            }
1070
1.03G
            for (x = 0; x < GBW; x++) {
1071
969M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1072
0
                    bit = 0;
1073
969M
                } else {
1074
969M
                    CONTEXT = out_byte & 0x000F; /* First 4 pixels */
1075
969M
                    CONTEXT |= (pd>>8) & 0x03E0; /* Skip one, next 5 pixels */
1076
969M
                    CONTEXT |= (ppd>>2) & 0x7000; /* Skip 2, next 3 pixels, skip one */
1077
969M
                    if (y >= top && x >= left && x < right)
1078
450M
                    {
1079
450M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1080
450M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1081
450M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1082
450M
                        CONTEXT |= jbig2_image_get_pixel_fast(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1083
450M
                    }
1084
518M
                    else
1085
518M
                    {
1086
518M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1087
518M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
1088
518M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
1089
518M
                        CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
1090
518M
                    }
1091
969M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1092
969M
                    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
969M
                }
1095
969M
                pd = pd<<1;
1096
969M
                ppd = ppd<<1;
1097
969M
                out_byte = (out_byte<<1) | bit;
1098
969M
                out_bits_to_go_in_byte--;
1099
969M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1100
969M
                if (out_bits_to_go_in_byte == 0) {
1101
103M
                    out_bits_to_go_in_byte = 8;
1102
103M
                    d++;
1103
103M
                    if (x+9 < GBW && y > 0 && pline) {
1104
80.6M
                        pd |= *pline++;
1105
80.6M
                        if (y > 1 && ppline)
1106
73.5M
                            ppd |= *ppline++;
1107
80.6M
                    }
1108
103M
                }
1109
969M
            }
1110
68.5M
            if (out_bits_to_go_in_byte != 8)
1111
66.3M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1112
68.5M
        } else {
1113
52.7M
            copy_prev_row(image, y);
1114
52.7M
        }
1115
121M
    }
1116
1117
6.15k
    return 0;
1118
6.15k
}
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.43k
{
1125
1.43k
    const int64_t GBW = image->width;
1126
1.43k
    const int64_t GBH = image->height;
1127
1.43k
    uint32_t CONTEXT;
1128
1.43k
    int64_t x, y;
1129
1.43k
    int LTP = 0;
1130
1131
1.43k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1132
5
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1133
5
                           "adaptive template pixel is out of field");
1134
1135
118M
    for (y = 0; y < GBH; y++) {
1136
118M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0795]);
1137
118M
        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
118M
        LTP ^= bit;
1140
118M
        if (!LTP) {
1141
59.8M
            uint32_t out_byte = 0;
1142
59.8M
            int out_bits_to_go_in_byte = 8;
1143
59.8M
            uint8_t *d = &image->data[image->stride * y];
1144
59.8M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1145
59.8M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1146
59.8M
            uint32_t pd = 0;
1147
59.8M
            uint32_t ppd = 0;
1148
59.8M
            if (y > 0 && pline) {
1149
59.8M
                pd = (*pline++ << 8);
1150
59.8M
                if (GBW > 8)
1151
3.11M
                    pd |= *pline++;
1152
59.8M
                if (y > 1 && ppline) {
1153
59.8M
                    ppd = (*ppline++ << 8);
1154
59.8M
                    if (GBW > 8)
1155
3.11M
                        ppd |= *ppline++;
1156
59.8M
                }
1157
59.8M
            }
1158
1.65G
            for (x = 0; x < GBW; x++) {
1159
1.59G
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1160
0
                    bit = 0;
1161
1.59G
                } else {
1162
1.59G
                    CONTEXT  = out_byte & 0x0007; /* First 3 pixels */
1163
1.59G
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
1164
1.59G
                    CONTEXT |= (pd>>9) & 0x01F0; /* next 5 pixels */
1165
1.59G
                    CONTEXT |= (ppd>>4) & 0x1E00; /* next 4 pixels */
1166
1.59G
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1167
1.59G
                    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.59G
                }
1170
1.59G
                pd = pd<<1;
1171
1.59G
                ppd = ppd<<1;
1172
1.59G
                out_byte = (out_byte<<1) | bit;
1173
1.59G
                out_bits_to_go_in_byte--;
1174
1.59G
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1175
1.59G
                if (out_bits_to_go_in_byte == 0) {
1176
177M
                    out_bits_to_go_in_byte = 8;
1177
177M
                    d++;
1178
177M
                    if (x+9 < GBW && y > 0 && pline) {
1179
156M
                        pd |= *pline++;
1180
156M
                        if (y > 1 && ppline)
1181
110M
                            ppd |= *ppline++;
1182
156M
                    }
1183
177M
                }
1184
1.59G
            }
1185
59.8M
            if (out_bits_to_go_in_byte != 8)
1186
51.9M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1187
59.8M
        } else {
1188
58.4M
            copy_prev_row(image, y);
1189
58.4M
        }
1190
118M
    }
1191
1192
1.43k
    return 0;
1193
1.43k
}
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
11.5k
{
1200
11.5k
    const int64_t GBW = image->width;
1201
11.5k
    const int64_t GBH = image->height;
1202
11.5k
    uint32_t CONTEXT;
1203
11.5k
    int64_t x, y;
1204
11.5k
    int LTP = 0;
1205
1206
11.5k
    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
73.7M
    for (y = 0; y < GBH; y++) {
1211
73.7M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0xE5]);
1212
73.7M
        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
73.7M
        LTP ^= bit;
1215
73.7M
        if (!LTP) {
1216
27.6M
            uint32_t out_byte = 0;
1217
27.6M
            int out_bits_to_go_in_byte = 8;
1218
27.6M
            uint8_t *d = &image->data[image->stride * y];
1219
27.6M
            uint8_t *pline  =  y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1220
27.6M
            uint8_t *ppline = y > 1 ? &image->data[image->stride * (y-2)] : NULL;
1221
27.6M
            uint32_t pd = 0;
1222
27.6M
            uint32_t ppd = 0;
1223
27.6M
            if (y > 0 && pline) {
1224
27.6M
                pd = (*pline++ << 8);
1225
27.6M
                if (GBW > 8)
1226
837k
                    pd |= *pline++;
1227
27.6M
                if (y > 1 && ppline) {
1228
27.6M
                    ppd = (*ppline++ << 8);
1229
27.6M
                    if (GBW > 8)
1230
837k
                        ppd |= *ppline++;
1231
27.6M
                }
1232
27.6M
            }
1233
494M
            for (x = 0; x < GBW; x++) {
1234
467M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1235
0
                    bit = 0;
1236
467M
                } else {
1237
467M
                    CONTEXT  = out_byte & 0x003; /* First 2 pixels */
1238
467M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
1239
467M
                    CONTEXT |= (pd>>11) & 0x078; /* next 4 pixels */
1240
467M
                    CONTEXT |= (ppd>>7) & 0x380; /* next 3 pixels */
1241
467M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1242
467M
                    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
467M
                }
1245
467M
                pd = pd<<1;
1246
467M
                ppd = ppd<<1;
1247
467M
                out_byte = (out_byte<<1) | bit;
1248
467M
                out_bits_to_go_in_byte--;
1249
467M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1250
467M
                if (out_bits_to_go_in_byte == 0) {
1251
54.9M
                    out_bits_to_go_in_byte = 8;
1252
54.9M
                    d++;
1253
54.9M
                    if (x+9 < GBW && y > 0 && pline) {
1254
38.9M
                        pd |= *pline++;
1255
38.9M
                        if (y > 1 && ppline)
1256
37.5M
                            ppd |= *ppline++;
1257
38.9M
                    }
1258
54.9M
                }
1259
467M
            }
1260
27.6M
            if (out_bits_to_go_in_byte != 8)
1261
22.4M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1262
46.1M
        } else {
1263
46.1M
            copy_prev_row(image, y);
1264
46.1M
        }
1265
73.7M
    }
1266
1267
11.5k
    return 0;
1268
11.5k
}
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
1.40k
{
1275
1.40k
    const int64_t GBW = image->width;
1276
1.40k
    const int64_t GBH = image->height;
1277
1.40k
    uint32_t CONTEXT;
1278
1.40k
    int64_t x, y;
1279
1.40k
    int LTP = 0;
1280
1281
1.40k
    if (pixel_outside_field(params->gbat[0], params->gbat[1]))
1282
4
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1283
4
                           "adaptive template pixel is out of field");
1284
1285
175M
    for (y = 0; y < GBH; y++) {
1286
175M
        int bit = jbig2_arith_decode(ctx, as, &GB_stats[0x0195]);
1287
175M
        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
175M
        LTP ^= bit;
1290
175M
        if (!LTP) {
1291
107M
            uint32_t out_byte = 0;
1292
107M
            int out_bits_to_go_in_byte = 8;
1293
107M
            uint8_t *d = &image->data[image->stride * y];
1294
107M
            uint8_t *pline  = y > 0 ? &image->data[image->stride * (y-1)] : NULL;
1295
107M
            uint32_t pd = 0;
1296
107M
            if (y > 0 && pline) {
1297
107M
                pd = (*pline++ << 8);
1298
107M
                if (GBW > 8)
1299
567k
                    pd |= *pline++;
1300
107M
            }
1301
968M
            for (x = 0; x < GBW; x++) {
1302
860M
                if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1303
0
                    bit = 0;
1304
860M
                } else {
1305
860M
                    CONTEXT  = out_byte & 0x0F; /* First 4 pixels */
1306
860M
                    CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1307
860M
                    CONTEXT |= (pd>>9) & 0x3E0; /* next 5 pixels */
1308
860M
                    bit = jbig2_arith_decode(ctx, as, &GB_stats[CONTEXT]);
1309
860M
                    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
860M
                }
1312
860M
                pd = pd<<1;
1313
860M
                out_byte = (out_byte<<1) | bit;
1314
860M
                out_bits_to_go_in_byte--;
1315
860M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1316
860M
                if (out_bits_to_go_in_byte == 0) {
1317
88.5M
                    out_bits_to_go_in_byte = 8;
1318
88.5M
                    d++;
1319
88.5M
                    if (x+9 < GBW && y > 0 && pline)
1320
48.0M
                        pd |= *pline++;
1321
88.5M
                }
1322
860M
            }
1323
107M
            if (out_bits_to_go_in_byte != 8)
1324
106M
                *d = (uint8_t)out_byte<<out_bits_to_go_in_byte;
1325
107M
        } else {
1326
68.4M
            copy_prev_row(image, y);
1327
68.4M
        }
1328
175M
    }
1329
1330
1.39k
    return 0;
1331
1.39k
}
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
21.7k
{
1338
21.7k
    switch (params->GBTEMPLATE) {
1339
7.32k
    case 0:
1340
7.32k
        return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
1341
1.43k
    case 1:
1342
1.43k
        return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
1343
11.5k
    case 2:
1344
11.5k
        return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
1345
1.40k
    case 3:
1346
1.40k
        return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
1347
21.7k
    }
1348
1349
0
    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
1350
21.7k
}
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
176k
{
1373
176k
    const int8_t *gbat = params->gbat;
1374
1375
176k
    if (!params->MMR && params->TPGDON)
1376
21.7k
        return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
1377
1378
154k
    if (!params->MMR && params->GBTEMPLATE == 0) {
1379
23.1k
        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
7.70k
            return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
1381
15.4k
        else
1382
15.4k
            return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
1383
131k
    } else if (!params->MMR && params->GBTEMPLATE == 1) {
1384
8.05k
        if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
1385
1.24k
            return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
1386
6.80k
        else
1387
6.80k
            return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
1388
8.05k
    }
1389
123k
    else if (!params->MMR && params->GBTEMPLATE == 2) {
1390
114k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1391
76.0k
            return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
1392
38.6k
        else
1393
38.6k
            return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
1394
114k
    } else if (!params->MMR && params->GBTEMPLATE == 3) {
1395
8.63k
        if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1396
4.61k
            return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
1397
4.01k
        else
1398
4.01k
            return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
1399
8.63k
    }
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
154k
}
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
26.0k
{
1417
26.0k
    Jbig2RegionSegmentInfo rsi;
1418
26.0k
    byte seg_flags;
1419
26.0k
    int8_t gbat[8];
1420
26.0k
    int offset;
1421
26.0k
    uint32_t gbat_bytes = 0;
1422
26.0k
    Jbig2GenericRegionParams params;
1423
26.0k
    int code = 0;
1424
26.0k
    Jbig2Image *image = NULL;
1425
26.0k
    Jbig2WordStream *ws = NULL;
1426
26.0k
    Jbig2ArithState *as = NULL;
1427
26.0k
    Jbig2ArithCx *GB_stats = NULL;
1428
26.0k
    uint32_t height;
1429
26.0k
    Jbig2Page *page = &ctx->pages[ctx->current_page];
1430
1431
    /* 7.4.6 */
1432
26.0k
    if (segment->data_length < 18)
1433
40
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1434
1435
25.9k
    jbig2_get_region_segment_info(&rsi, segment_data);
1436
25.9k
    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
25.9k
    height = rsi.height;
1440
25.9k
    if (segment->rows != UINT32_MAX) {
1441
19.6k
        if (segment->rows > rsi.height)
1442
36
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
1443
19.6k
        height = segment->rows;
1444
19.6k
    }
1445
1446
    /* 7.4.6.2 */
1447
25.9k
    seg_flags = segment_data[17];
1448
25.9k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
1449
25.9k
    if ((seg_flags & 1) && (seg_flags & 6))
1450
664
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
1451
1452
    /* 7.4.6.3 */
1453
25.9k
    if (!(seg_flags & 1)) {
1454
24.4k
        gbat_bytes = (seg_flags & 6) ? 2 : 8;
1455
24.4k
        if (18 + gbat_bytes > segment->data_length)
1456
12
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1457
24.3k
        memcpy(gbat, segment_data + 18, gbat_bytes);
1458
24.3k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
1459
24.3k
    }
1460
1461
25.9k
    offset = 18 + gbat_bytes;
1462
1463
    /* Check for T.88 amendment 2 */
1464
25.9k
    if ((seg_flags >> 5) & 1)
1465
36
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
1466
1467
    /* Table 34 */
1468
25.8k
    params.MMR = seg_flags & 1;
1469
25.8k
    params.GBTEMPLATE = (seg_flags & 6) >> 1;
1470
25.8k
    params.TPGDON = (seg_flags & 8) >> 3;
1471
25.8k
    params.USESKIP = 0;
1472
25.8k
    memcpy(params.gbat, gbat, gbat_bytes);
1473
1474
25.8k
    if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
1475
16.5k
        if (rsi.y >= page->end_row + page->stripe_size) {
1476
1.51k
            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
1.51k
            return 0;
1478
1.51k
        }
1479
15.0k
        if (height > page->end_row + page->stripe_size) {
1480
13.7k
            height = page->end_row + page->stripe_size;
1481
13.7k
        }
1482
15.0k
    } else {
1483
9.29k
        if (rsi.y >= page->height) {
1484
837
            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
837
            return 0;
1486
837
        }
1487
8.45k
        if (height > page->height - rsi .y) {
1488
6.64k
            height = page->height - rsi.y;
1489
6.64k
        }
1490
8.45k
    }
1491
23.5k
    if (height == 0) {
1492
952
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
1493
952
        return 0;
1494
952
    }
1495
1496
22.5k
    image = jbig2_image_new(ctx, rsi.width, height);
1497
22.5k
    if (image == NULL)
1498
79
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
1499
22.5k
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
1500
1501
22.5k
    if (params.MMR) {
1502
488
        code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
1503
488
        if (code < 0) {
1504
35
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
1505
35
            goto cleanup;
1506
35
        }
1507
22.0k
    } else {
1508
22.0k
        int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
1509
1510
22.0k
        GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
1511
22.0k
        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
22.0k
        memset(GB_stats, 0, stats_size);
1516
1517
22.0k
        ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
1518
22.0k
        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
22.0k
        as = jbig2_arith_new(ctx, ws);
1523
22.0k
        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
22.0k
        code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
1528
22.0k
        if (code < 0) {
1529
75
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
1530
75
            goto cleanup;
1531
75
        }
1532
22.0k
    }
1533
1534
22.3k
    code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
1535
22.3k
    if (code < 0)
1536
773
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
1537
1538
22.5k
cleanup:
1539
22.5k
    jbig2_free(ctx->allocator, as);
1540
22.5k
    jbig2_word_stream_buf_free(ctx, ws);
1541
22.5k
    jbig2_free(ctx->allocator, GB_stats);
1542
22.5k
    jbig2_image_release(ctx, image);
1543
1544
22.5k
    return code;
1545
22.3k
}