1/* Copyright (C) 2001-2019 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., 1305 Grant Avenue - Suite 200, Novato,
13 CA 94945, U.S.A., +1(415)492-9861, 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#if !defined (UINT32_MAX)
46#define UINT32_MAX 0xffffffff
47#endif
48
49/*
50This is an explanation of the unoptimized and optimized generic
51region decoder implementations below, wherein we try to explain
52all the magic numbers.
53
54The generic region decoders decode the output pixels one row at a
55time, top to bottom. Within each row the pixels are decoded left
56to right. The input for the arithmetic integer decoder used to
57decode each pixel is a context consisting of up to 16 previously
58decoded pixels. These pixels are chosen according to a predefined
59template placed relative to the location of the pixel to be
60decoded (6.2.5.3 figures 3, 4, 5 and 6). There are four different
61template that may be used (6.2.5.3). The template to use is
62determined by GBTEMPLATE. GBTEMPLATE is set in the symbol
63dictionary (6.5.8.1), generic region (7.4.6.4), or when decoding
64a halftone region's gray-scale image (annex C.5).
65
66Most of the pixels in each template have fixed locations relative
67to the pixel to be decoded. However, all templates have at least
68one adaptive pixel. The adaptive pixels have nominal locations,
69but these locations may be changed by GBAT. GBAT is set in the
70symbol dictionary (7.4.2.1.2), generic region (7.4.6.1), or hard
71coded as for halftone patterns (6.7.5).
72
73Adaptive pixels are restricted to fall within a field of
74previously decoded pixels relative to the pixel to be decoded
75(figure 7). The relative Y-coordinate for these adaptive pixels
76may vary between -128 and 0. The relative X-coordinate may vary
77between -128 and +127 (however, if the Y-coordinate is 0 the
78range of the X-coordinate is further restricted to -128 to -1
79since the pixels at locations 0 to +127 have not yet been
80decoded). If a template refers to a pixel location that reside
81outside of the image boundaries its value is assumed to be 0.
82
83UNOPTIMIZED DECODER
84
85The unoptimized decoders first check the contents of GBAT. If
86GBAT specifies that any of the adaptive pixels reside outside the
87allowed field the decoding is aborted. Next, each row is
88processed top to bottom, left to right, one pixel at a time. For
89each pixel a context is created containing the bit values of the
90pixels that fall inside the template.
91
92The order these bits are stored in the context is implementation
93dependent (6.2.5.3). We store the bit values in the CONTEXT
94variable from LSB to MSB, starting with the value of the pixel to
95the left of the current pixel, continuing right to left, bottom
96to top following the template. Using the CONTEXT created from
97these pixel values, the arithmetic integer decoder retrieves the
98pixel value, which is then written into the output image.
99
100Example when GBTEMPLATE is 2:
101
102The figure below represents a pixel grid of the output image.
103Each pixel is a single bit in the image. The pixel "OO" in the
104figure below is about to be decoded. The pixels "??" have not
105been decoded yet. The CONTEXT variable is constructed by
106combining the bit values from the pixels referred to by the
107template, shifted to their corresponding bit position.
108
109 . . . . . . . .
110 . . . . . . . .
111 ...+----+----+----+----+----+----+----+...
112 | | | X9 | X8 | X7 | | |
113 ...+----+----+----+----+----+----+----+...
114 | | X6 | X5 | X4 | X3 | A1 | |
115 ...+----+----+----+----+----+----+----+...
116 | | X2 | X1 | OO | ?? | ?? | ?? |
117 ...+----+----+----+----+----+----+----+...
118 . . . . . . . .
119 . . . . . . . .
120
121In the table below pixel OO is assumed to be at coordinate (x, y).
122
123Bit 9: Pixel at location (x-1, y-2) (This is fixed pixel X9)
124Bit 8: Pixel at location (x , y-2) (This is fixed pixel X8)
125Bit 7: Pixel at location (x+1, y-2) (This is fixed pixel X7)
126Bit 6: Pixel at location (x-2, y-1) (This is fixed pixel X6)
127Bit 5: Pixel at location (x-1, y-1) (This is fixed pixel X5)
128Bit 4: Pixel at location (x , y-1) (This is fixed pixel X4)
129Bit 3: Pixel at location (x+1, y-1) (This is fixed pixel X3)
130Bit 2: Pixel at location (x+2, y-1) (This is adaptive pixel A1)
131Bit 1: Pixel at location (x-2, y ) (This is fixed pixel X2)
132Bit 0: Pixel at location (x-1, y ) (This is fixed pixel X1)
133
134The location of adaptive pixel A1 may not always be at the
135nominal location (x+2, y-1). It could be at any pixel location to
136the left or above OO as specified by GBAT, e.g. at the location
137(x-128, y+127).
138
139OPTIMIZED DECODER
140
141The optimized decoders work differently. They strive to avoid
142recreating the arithmetic integer decoder context from scratch
143for every pixel decoded. Instead they reuse part of the CONTEXT
144used to compute the previous pixel (the pixel to left of the one
145now being decoded). They also keep two sliding windows of pixel
146bit values from the two rows of pixels immediately above the
147pixel to be decoded. These are stored in the 32-bit variables
148line_m1 (row above the pixel to be decoded) and line_m2 (row
149above that of line_m1). These optimized decoders ONLY work for
150the nominal adaptive pixel locations since these locations are
151hard-coded into the implementation.
152
153The bit ordering in the CONTEXT variable is identical to the
154unoptimized case described above.
155
156The optimized decoders decode the output pixels one row at a
157time, top to bottom. Within each row the pixels are decoded in
158batches of up to eight pixels at a time (except possibly the
159right most batch which may be less than eight pixels). The
160batches in a row are decoded in sequence from left to right.
161Within each such batch the pixels are decoded in sequence from
162left to right.
163
164Before decoding the pixels in a row the two sliding windows of
165pixel values are reset. The first eight pixels of the row above
166the pixel to be decoded is stored in line_m1, while line_m2
167stores the first eight pixels of the row above that of line_m1.
168
169The figure below illustrates the situation where the template has
170been placed so that the decoded pixel OO is the very first pixel
171of a row. It also gives labels to various pixels that we will
172refer to below.
173
174 . . . . . . . . . . .
175 | . . . . . . . . . .
176 + + +----+----+----+----+----+----+----+----+----+----+...
177 X9 | X8 | X7 | m1 | m2 | m3 | m4 | m5 | m6 | m7 | |
178 + + +----+----+----+----+----+----+----+----+----+----+...
179 X6 X5 | X4 | X3 | A1 | n1 | n2 | n3 | n4 | n5 | n6 | n7 |
180 + + +----+----+----+----+----+----+----+----+----+----+...
181 X2 X1 | OO | | | | | | | | | |
182 + + +----+----+----+----+----+----+----+----+----+----+...
183 | . . . . . . . . . .
184 . . . . . . . . . . .
185
186The pixels X1, X2, X5, X6 and X9 all reside outside the left edge
187of the image. These pixels (like all others outside the image)
188can according to 6.2.5.2 be assumed to be 0. line_m1 stores n5
189through n1 as well as A1, and X3 through X6. line_m2 stores m6
190through m1 as well as X7 through X9. The bits in line_m2 are also
191shifted left four bits as seen below.
192
19315 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 | bit position
194------------------------------------------------+------------------
195 0 0 0 0 0 0 X6 X5 X4 X3 A1 n1 n2 n3 n4 n5 | line_m1
196 0 0 0 X9 X8 X7 m1 m2 m3 m4 m5 m6 0 0 0 0 | line_m2
197
198The way line_m1 and line_m2 are stored means we can simply shift
199them by the same amount to move the sliding window.
200
201The bit order in line_m1 and line_m2 matches the ordering in the
202CONTEXT variable. Each bit for the 'A' and 'X' pixels in line_m1
203and line_m2 correspond to the equivalent bits in CONTEXT, only
204shifted right by 3 bits. Thus X3 is bit 3 in CONTEXT and bit 6 in
205line_m1, etc.
206
207The initial arithmetic integer decoder context is created and
208stored in the CONTEXT variable by masking, shifting, and bitwise
209ORing the contents of line_m1 and line_m2. The "CONTEXT contents"
210row is only shown for clarity, it is not present in the code.
211
21215 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 | bit position
213------------------------------------------------+---------------------------
214 0 0 0 0 0 0 0 0 0 X6 X5 X4 X3 A1 n1 n2 | line_m1 >> 3
215 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 | mask for line_m1 (0x7c)
216 0 0 0 0 0 0 0 0 0 X6 X5 X4 X3 A1 0 0 | line_m1 AND mask
217------------------------------------------------+---------------------------
218 0 0 0 0 0 0 X9 X8 X7 m1 m2 m3 m4 m5 m6 0 | line_m2 >> 3
219 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 | mask for line_m2 (0x380)
220 0 0 0 0 0 0 X9 X8 X7 0 0 0 0 0 0 0 | line_m2 AND mask
221------------------------------------------------+---------------------------
222 0 0 0 0 0 0 X9 X8 X7 X6 X5 X4 X3 A1 0 0 | CONTEXT = line_m1 OR line_m2
223------------------------------------------------+---------------------------
224 0 0 0 0 0 0 X9 X8 X7 X6 X5 X4 X3 A1 X2 X1 | CONTEXT contents
225
226Each batch is normally 8 bits, but at the right edge of the image
227we may have fewer pixels to decode. The minor_width is how many
228pixels the current batch should decode, with a counter variable
229x_minor to keep track of the current pixel being decoded.
230
231In order to process a new batch of pixels, unless we're at the
232rightmost batch of pixels, we need to refill the sliding window
233variables with eight new bits. Looking at the diagram above we
234can see that in order to decode eight pixels starting with O0
235we'll need to have bits up to pixel 'n7' for line_m1 and 'm7' for
236line_m2 available (A1 and X7 moved right 7 times). To do this
237simply and quickly, we shift line_m1 left by 8 bits, and OR in
238the next byte from corresponding row. Likewise for line_m2, but
239the next byte from the image is also shifted left by 4 bits to
240compensate for line_m2 having the four least significant bits
241unused.
242
243These new eight bits contain the bit values of the eight pixels
244to the right of those already present in line_m1 and line_m2. We
245call these new bits m7 through mE, and n6 through nD, as
246illustrated below.
247
24823 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
249------------------------------------------------------------------------+-------------
250 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
251 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
252 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
253------------------------------------------------------------------------+-------------
254 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
255 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
256 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
257
258 . . . . . . . . . . . . . . . . . . . .
259 | . . . . . . . . . . . . . . . . . . .
260 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
261 X9 | X8 | X7 | m1 | m2 | m3 | m4 | m5 | m6 | m7 | m8 | m9 | mA | mB | mC | mD | mE | | | |
262 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
263 X6 X5 | X4 | X3 | A1 | n1 | n2 | n3 | n4 | n5 | n6 | n7 | n8 | n9 | nA | nB | nC | nD | | | |
264 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
265 X2 X1 | OO | | | | | | | | | | | | | | | | | | |
266 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
267 | . . . . . . . . . . . . . . . . . . .
268 . . . . . . . . . . . . . . . . . . . .
269
270CONTEXT, line_m1 and line_m2 now contain all necessary bits to
271decode a full batch of eight pixels.
272
273The first pixel in the batch is decoded using this CONTEXT. After
274that, for each following pixel we need to update the CONTEXT
275using both the last decoded pixel value and new bits from line_m1
276and line_m2.
277
278 . . . . . . . . . . . . . . . . . . . .
279 | . . . . . . . . . . . . . . . . . . .
280 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
281 (X9)|_X8_|_X7_|>m1<| m2 | m3 | m4 | m5 | m6 | m7 | m8 | m9 | mA | mB | mC | mD | mE | | | |
282 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
283 (X6) _X5_|_X4_|_X3_|_A1_|>n1<| n2 | n3 | n4 | n5 | n6 | n7 | n8 | n9 | nA | nB | nC | nD | | | |
284 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
285 (X2) _X1_|>OO<| oo | | | | | | | | | | | | | | | | | |
286 + + +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+...
287 | . . . . . . . . . . . . . . . . . . .
288 . . . . . . . . . . . . . . . . . . . .
289
290This figure illustrates what happens when the same template is
291overlaid on itself shifted one pixel to the right in order to
292decode the next pixel. Pixels marked with _ _ are pixels that
293are present in both templates' CONTEXTs and can be reused. Pixels
294marked with ( ) are pixels from the first template that are no
295longer necessary and can be removed from CONTEXT. Pixels marked
296with > < are new pixels that were not part of the original
297CONTEXT, and so need to be moved into the CONTEXT at the
298appropriate locations. In general the leftmost pixels of each
299template row can be forgotten, while new pixels are needed at the
300right most location of each row.
301
302The CONTEXT corresponding to the current pixel OO and how it is
303masked is shown below. Note how the left most pixel of each row
304of the template is NOT propagated to the CONTEXT, these pixels
305are X2, X6 and X9. This is done by having the mask being 0 at the
306corresponding locations.
307
308 9 8 7 6 5 4 3 2 1 0 | bit position
309------------------------------+-------------
310X9 X8 X7 X6 X5 X4 X3 A1 X2 X1 | pixel values from CONTEXT
311 0 1 1 0 1 1 1 1 0 1 | reused pixel bit value mask (0x1bd)
312 0 X8 X7 0 X5 X4 X3 A1 0 X1 | reused pixel values from CONTEXT
313
314Next the CONTEXT is shifted left by one bit to make it reference
315the next pixel to be decoded. The pixel bit value we just decoded
316is then written into the bit corresponding to X1. The sliding
317windows in line_m1 and line_m2 are both shifted (10 - x_minor)
318bits to the right to make the needed pixels' bit values appear at
319the correct positions to be ORed into CONTEXT. Note that this
320shift amount depends on which bit in the batch is currently being
321computed, as is given by the x_minor counter. In the example
322below we assume that x_minor is 0.
323
324 9 8 7 6 5 4 3 2 1 0 | bit position
325------------------------------+--------------
326 0 X8 X7 0 X5 X4 X3 A1 0 0 | reused pixels from CONTEXT
327X8 X7 0 X5 X4 X3 A1 0 0 0 | reused pixels shifted left 1 bit
328------------------------------+--------------
329X8 X7 0 X5 X4 X3 A1 0 X1 OO | new CONTEXT with current pixel at LSB
330------------------------------+--------------
331 0 0 X6 X5 X4 X3 A1 n1 n2 n3 | line_m1 shifted (10 - x_minor) bits to the right
332 0 0 0 0 0 0 0 1 0 0 | mask for new adaptive pixel one row above (0x4)
333X8 X7 0 X5 X4 X3 A1 n1 X1 OO | new CONTEXT with new adaptive pixel
334------------------------------+--------------
335X8 X7 m1 m2 m3 m4 m5 m6 m7 m8 | line_m2 with new bits ORed in
336 0 0 1 0 0 0 0 0 0 0 | mask for new pixel two rows above (0x80)
337X8 X7 m1 X5 X4 X3 A1 n1 X1 OO | new CONTEXT with new pixel
338
339This makes the computation of the new CONTEXT be:
340
341NEWCONTEXT = (CONTEXT & 0x1bd) << 1
342NEWCONTEXT |= newbit;
343NEWCONTEXT |= (line_m1 >> (10-x_minor)) & 0x4;
344NEWCONTEXT |= (line_m2 >> (10-x_minor)) & 0x80;
345
346The optimized decoding functions for GBTEMPLATE 0, 1 and 3 all
347work similarly. */
348
349/* return the appropriate context size for the given template */
350int
351jbig2_generic_stats_size(Jbig2Ctx *ctx, int template)
352{
353 int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 13 : 1 << 10;
354
355 return stats_size;
356}
357
358static int
359jbig2_decode_generic_template0(Jbig2Ctx *ctx,
360 Jbig2Segment *segment,
361 const Jbig2GenericRegionParams *params, Jbig2ArithState *as,
362 Jbig2Image *image, Jbig2ArithCx *GB_stats)
363{
364 const uint32_t GBW = image->width;
365 const uint32_t GBH = image->height;
366 const uint32_t rowstride = image->stride;
367 uint32_t x, y;
368 byte *line2 = NULL;
369 byte *line1 = NULL;
370 byte *gbreg_line = (byte *) image->data;
371
372#ifdef OUTPUT_PBM
373 printf("P4\n%d %d\n", GBW, GBH);
374#endif
375
376 if (GBW <= 0)
377 return 0;
378
379 for (y = 0; y < GBH; y++) {
380 uint32_t CONTEXT;
381 uint32_t line_m1;
382 uint32_t line_m2;
383 uint32_t padded_width = (GBW + 7) & -8;
384 int code = 0;
385
386 line_m1 = line1 ? line1[0] : 0;
387 line_m2 = line2 ? line2[0] << 6 : 0;
388 CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
389
390 /* 6.2.5.7 3d */
391 for (x = 0; x < padded_width; x += 8) {
392 byte result = 0;
393 int x_minor;
394 int minor_width = GBW - x > 8 ? 8 : GBW - x;
395
396 if (line1)
397 line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
398
399 if (line2)
400 line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 6 : 0);
401
402 /* This is the speed-critical inner loop. */
403 for (x_minor = 0; x_minor < minor_width; x_minor++) {
404 bool bit;
405
406 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
407 if (code)
408 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 optimized");
409 result |= bit << (7 - x_minor);
410 CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
411 }
412 gbreg_line[x >> 3] = result;
413 }
414#ifdef OUTPUT_PBM
415 fwrite(gbreg_line, 1, rowstride, stdout);
416#endif
417 line2 = line1;
418 line1 = gbreg_line;
419 gbreg_line += rowstride;
420 }
421
422 return 0;
423}
424
425#define pixel_outside_field(x, y) \
426 ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
427
428static int
429jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx,
430 Jbig2Segment *segment,
431 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
432{
433 const uint32_t GBW = image->width;
434 const uint32_t GBH = image->height;
435 uint32_t CONTEXT;
436 uint32_t x, y;
437 bool bit;
438 int code = 0;
439
440 if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
441 pixel_outside_field(params->gbat[2], params->gbat[3]) ||
442 pixel_outside_field(params->gbat[4], params->gbat[5]) ||
443 pixel_outside_field(params->gbat[6], params->gbat[7]))
444 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
445 "adaptive template pixel is out of field");
446
447 /* this version is generic and easy to understand, but very slow */
448
449 for (y = 0; y < GBH; y++) {
450 for (x = 0; x < GBW; x++) {
451 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
452 jbig2_image_set_pixel(image, x, y, 0);
453 continue;
454 }
455 CONTEXT = 0;
456 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
457 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
458 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
459 CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
460 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
461 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
462 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
463 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 7;
464 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
465 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
466 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
467 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
468 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
469 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 13;
470 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
471 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
472 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
473 if (code)
474 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 unoptimized");
475 jbig2_image_set_pixel(image, x, y, bit);
476 }
477 }
478 return 0;
479}
480
481static int
482jbig2_decode_generic_template1_unopt(Jbig2Ctx *ctx,
483 Jbig2Segment *segment,
484 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
485{
486 const uint32_t GBW = image->width;
487 const uint32_t GBH = image->height;
488 uint32_t CONTEXT;
489 uint32_t x, y;
490 bool bit;
491 int code = 0;
492
493 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
494 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
495 "adaptive template pixel is out of field");
496
497 /* this version is generic and easy to understand, but very slow */
498
499 for (y = 0; y < GBH; y++) {
500 for (x = 0; x < GBW; x++) {
501 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
502 jbig2_image_set_pixel(image, x, y, 0);
503 continue;
504 }
505 CONTEXT = 0;
506 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
507 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
508 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
509 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
510 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4;
511 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
512 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6;
513 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
514 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
515 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9;
516 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10;
517 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 11;
518 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
519 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
520 if (code)
521 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 unoptimized");
522 jbig2_image_set_pixel(image, x, y, bit);
523 }
524 }
525 return 0;
526}
527
528static int
529jbig2_decode_generic_template1(Jbig2Ctx *ctx,
530 Jbig2Segment *segment,
531 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
532{
533 const uint32_t GBW = image->width;
534 const uint32_t GBH = image->height;
535 const uint32_t rowstride = image->stride;
536 uint32_t x, y;
537 byte *line2 = NULL;
538 byte *line1 = NULL;
539 byte *gbreg_line = (byte *) image->data;
540 int code = 0;
541
542#ifdef OUTPUT_PBM
543 printf("P4\n%d %d\n", GBW, GBH);
544#endif
545
546 if (GBW <= 0)
547 return 0;
548
549 for (y = 0; y < GBH; y++) {
550 uint32_t CONTEXT;
551 uint32_t line_m1;
552 uint32_t line_m2;
553 uint32_t padded_width = (GBW + 7) & -8;
554
555 line_m1 = line1 ? line1[0] : 0;
556 line_m2 = line2 ? line2[0] << 5 : 0;
557 CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
558
559 /* 6.2.5.7 3d */
560 for (x = 0; x < padded_width; x += 8) {
561 byte result = 0;
562 int x_minor;
563 int minor_width = GBW - x > 8 ? 8 : GBW - x;
564
565 if (line1)
566 line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
567
568 if (line2)
569 line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 5 : 0);
570
571 /* This is the speed-critical inner loop. */
572 for (x_minor = 0; x_minor < minor_width; x_minor++) {
573 bool bit;
574
575 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
576 if (code)
577 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 optimized");
578 result |= bit << (7 - x_minor);
579 CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
580 }
581 gbreg_line[x >> 3] = result;
582 }
583#ifdef OUTPUT_PBM
584 fwrite(gbreg_line, 1, rowstride, stdout);
585#endif
586 line2 = line1;
587 line1 = gbreg_line;
588 gbreg_line += rowstride;
589 }
590
591 return 0;
592}
593
594static int
595jbig2_decode_generic_template2_unopt(Jbig2Ctx *ctx,
596 Jbig2Segment *segment,
597 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
598{
599 const uint32_t GBW = image->width;
600 const uint32_t GBH = image->height;
601 uint32_t CONTEXT;
602 uint32_t x, y;
603 bool bit;
604 int code = 0;
605
606 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
607 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
608 "adaptive template pixel is out of field");
609
610 /* this version is generic and easy to understand, but very slow */
611
612 for (y = 0; y < GBH; y++) {
613 for (x = 0; x < GBW; x++) {
614 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
615 jbig2_image_set_pixel(image, x, y, 0);
616 continue;
617 }
618 CONTEXT = 0;
619 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
620 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
621 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
622 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3;
623 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 4;
624 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5;
625 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6;
626 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7;
627 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 8;
628 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
629 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
630 if (code)
631 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 unoptimized");
632 jbig2_image_set_pixel(image, x, y, bit);
633 }
634 }
635
636 return 0;
637}
638
639static int
640jbig2_decode_generic_template2(Jbig2Ctx *ctx,
641 Jbig2Segment *segment,
642 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
643{
644 const uint32_t GBW = image->width;
645 const uint32_t GBH = image->height;
646 const uint32_t rowstride = image->stride;
647 uint32_t x, y;
648 byte *line2 = NULL;
649 byte *line1 = NULL;
650 byte *gbreg_line = (byte *) image->data;
651 int code = 0;
652
653#ifdef OUTPUT_PBM
654 printf("P4\n%d %d\n", GBW, GBH);
655#endif
656
657 if (GBW <= 0)
658 return 0;
659
660 for (y = 0; y < GBH; y++) {
661 uint32_t CONTEXT;
662 uint32_t line_m1;
663 uint32_t line_m2;
664 uint32_t padded_width = (GBW + 7) & -8;
665
666 line_m1 = line1 ? line1[0] : 0;
667 line_m2 = line2 ? line2[0] << 4 : 0;
668 CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
669
670 /* 6.2.5.7 3d */
671 for (x = 0; x < padded_width; x += 8) {
672 byte result = 0;
673 int x_minor;
674 int minor_width = GBW - x > 8 ? 8 : GBW - x;
675
676 if (line1)
677 line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
678
679 if (line2)
680 line_m2 = (line_m2 << 8) | (x + 8 < GBW ? line2[(x >> 3) + 1] << 4 : 0);
681
682 /* This is the speed-critical inner loop. */
683 for (x_minor = 0; x_minor < minor_width; x_minor++) {
684 bool bit;
685
686 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
687 if (code)
688 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 optimized");
689 result |= bit << (7 - x_minor);
690 CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
691 }
692 gbreg_line[x >> 3] = result;
693 }
694#ifdef OUTPUT_PBM
695 fwrite(gbreg_line, 1, rowstride, stdout);
696#endif
697 line2 = line1;
698 line1 = gbreg_line;
699 gbreg_line += rowstride;
700 }
701
702 return 0;
703}
704
705static int
706jbig2_decode_generic_template3(Jbig2Ctx *ctx,
707 Jbig2Segment *segment,
708 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
709{
710 const uint32_t GBW = image->width;
711 const uint32_t GBH = image->height;
712 const uint32_t rowstride = image->stride;
713 byte *line1 = NULL;
714 byte *gbreg_line = (byte *) image->data;
715 uint32_t x, y;
716 int code;
717
718#ifdef OUTPUT_PBM
719 printf("P4\n%d %d\n", GBW, GBH);
720#endif
721
722 if (GBW <= 0)
723 return 0;
724
725 for (y = 0; y < GBH; y++) {
726 uint32_t CONTEXT;
727 uint32_t line_m1;
728 uint32_t padded_width = (GBW + 7) & -8;
729
730 line_m1 = line1 ? line1[0] : 0;
731 CONTEXT = (line_m1 >> 1) & 0x3f0;
732
733 /* 6.2.5.7 3d */
734 for (x = 0; x < padded_width; x += 8) {
735 byte result = 0;
736 int x_minor;
737 int minor_width = GBW - x > 8 ? 8 : GBW - x;
738
739 if (line1)
740 line_m1 = (line_m1 << 8) | (x + 8 < GBW ? line1[(x >> 3) + 1] : 0);
741
742 /* This is the speed-critical inner loop. */
743 for (x_minor = 0; x_minor < minor_width; x_minor++) {
744 bool bit;
745
746 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
747 if (code)
748 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 optimized");
749 result |= bit << (7 - x_minor);
750 CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x10);
751 }
752 gbreg_line[x >> 3] = result;
753 }
754#ifdef OUTPUT_PBM
755 fwrite(gbreg_line, 1, rowstride, stdout);
756#endif
757 line1 = gbreg_line;
758 gbreg_line += rowstride;
759 }
760
761 return 0;
762}
763
764static int
765jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx,
766 Jbig2Segment *segment,
767 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
768{
769 const uint32_t GBW = image->width;
770 const uint32_t GBH = image->height;
771 uint32_t CONTEXT;
772 uint32_t x, y;
773 bool bit;
774 int code = 0;
775
776 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
777 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
778 "adaptive template pixel is out of field");
779
780 /* this version is generic and easy to understand, but very slow */
781
782 for (y = 0; y < GBH; y++) {
783 for (x = 0; x < GBW; x++) {
784 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
785 jbig2_image_set_pixel(image, x, y, 0);
786 continue;
787 }
788 CONTEXT = 0;
789 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
790 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
791 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
792 CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
793 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
794 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
795 CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6;
796 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
797 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
798 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
799 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
800 if (code)
801 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 unoptimized");
802 jbig2_image_set_pixel(image, x, y, bit);
803 }
804 }
805 return 0;
806}
807
808static void
809copy_prev_row(Jbig2Image *image, int row)
810{
811 if (!row) {
812 /* no previous row */
813 memset(image->data, 0, image->stride);
814 } else {
815 /* duplicate data from the previous row */
816 uint8_t *src = image->data + (row - 1) * image->stride;
817
818 memcpy(src + image->stride, src, image->stride);
819 }
820}
821
822static int
823jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx,
824 Jbig2Segment *segment,
825 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
826{
827 const uint32_t GBW = image->width;
828 const uint32_t GBH = image->height;
829 uint32_t CONTEXT;
830 uint32_t x, y;
831 bool bit;
832 int LTP = 0;
833 int code = 0;
834
835 if (pixel_outside_field(params->gbat[0], params->gbat[1]) ||
836 pixel_outside_field(params->gbat[2], params->gbat[3]) ||
837 pixel_outside_field(params->gbat[4], params->gbat[5]) ||
838 pixel_outside_field(params->gbat[6], params->gbat[7]))
839 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
840 "adaptive template pixel is out of field");
841
842 for (y = 0; y < GBH; y++) {
843 LTP ^= jbig2_arith_decode(as, &GB_stats[0x9B25], &code);
844 if (code)
845 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON1");
846 if (!LTP) {
847 for (x = 0; x < GBW; x++) {
848 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
849 jbig2_image_set_pixel(image, x, y, 0);
850 continue;
851 }
852 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
853 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
854 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
855 CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
856 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
857 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
858 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
859 CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 7;
860 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
861 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
862 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
863 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
864 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
865 CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 13;
866 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
867 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
868 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
869 if (code)
870 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template0 TPGDON2");
871 jbig2_image_set_pixel(image, x, y, bit);
872 }
873 } else {
874 copy_prev_row(image, y);
875 }
876 }
877
878 return 0;
879}
880
881static int
882jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx,
883 Jbig2Segment *segment,
884 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
885{
886 const uint32_t GBW = image->width;
887 const uint32_t GBH = image->height;
888 uint32_t CONTEXT;
889 uint32_t x, y;
890 bool bit;
891 int LTP = 0;
892 int code = 0;
893
894 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
895 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
896 "adaptive template pixel is out of field");
897
898 for (y = 0; y < GBH; y++) {
899 LTP ^= jbig2_arith_decode(as, &GB_stats[0x0795], &code);
900 if (code)
901 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON1");
902 if (!LTP) {
903 for (x = 0; x < GBW; x++) {
904 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
905 jbig2_image_set_pixel(image, x, y, 0);
906 continue;
907 }
908 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
909 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
910 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
911 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
912 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4;
913 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
914 CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6;
915 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
916 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
917 CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9;
918 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10;
919 CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 11;
920 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
921 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
922 if (code)
923 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template1 TPGDON2");
924 jbig2_image_set_pixel(image, x, y, bit);
925 }
926 } else {
927 copy_prev_row(image, y);
928 }
929 }
930
931 return 0;
932}
933
934static int
935jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx,
936 Jbig2Segment *segment,
937 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
938{
939 const uint32_t GBW = image->width;
940 const uint32_t GBH = image->height;
941 uint32_t CONTEXT;
942 uint32_t x, y;
943 bool bit;
944 int LTP = 0;
945 int code = 0;
946
947 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
948 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
949 "adaptive template pixel is out of field");
950
951 for (y = 0; y < GBH; y++) {
952 LTP ^= jbig2_arith_decode(as, &GB_stats[0xE5], &code);
953 if (code)
954 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON1");
955 if (!LTP) {
956 for (x = 0; x < GBW; x++) {
957 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
958 jbig2_image_set_pixel(image, x, y, 0);
959 continue;
960 }
961 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
962 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
963 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
964 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3;
965 CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 4;
966 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5;
967 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6;
968 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7;
969 CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 8;
970 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
971 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
972 if (code)
973 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template2 TPGDON2");
974 jbig2_image_set_pixel(image, x, y, bit);
975 }
976 } else {
977 copy_prev_row(image, y);
978 }
979 }
980
981 return 0;
982}
983
984static int
985jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx,
986 Jbig2Segment *segment,
987 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
988{
989 const uint32_t GBW = image->width;
990 const uint32_t GBH = image->height;
991 uint32_t CONTEXT;
992 uint32_t x, y;
993 bool bit;
994 int LTP = 0;
995 int code = 0;
996
997 if (pixel_outside_field(params->gbat[0], params->gbat[1]))
998 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
999 "adaptive template pixel is out of field");
1000
1001 for (y = 0; y < GBH; y++) {
1002 LTP ^= jbig2_arith_decode(as, &GB_stats[0x0195], &code);
1003 if (code)
1004 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON1");
1005 if (!LTP) {
1006 for (x = 0; x < GBW; x++) {
1007 if (params->USESKIP && jbig2_image_get_pixel(params->SKIP, x, y)) {
1008 jbig2_image_set_pixel(image, x, y, 0);
1009 continue;
1010 }
1011 CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
1012 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
1013 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
1014 CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
1015 CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
1016 CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
1017 CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6;
1018 CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
1019 CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
1020 CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
1021 bit = jbig2_arith_decode(as, &GB_stats[CONTEXT], &code);
1022 if (code)
1023 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode arithmetic code when handling generic template3 TPGDON2");
1024 jbig2_image_set_pixel(image, x, y, bit);
1025 }
1026 } else {
1027 copy_prev_row(image, y);
1028 }
1029 }
1030
1031 return 0;
1032}
1033
1034static int
1035jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx,
1036 Jbig2Segment *segment,
1037 const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1038{
1039 switch (params->GBTEMPLATE) {
1040 case 0:
1041 return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
1042 case 1:
1043 return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
1044 case 2:
1045 return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
1046 case 3:
1047 return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
1048 }
1049
1050 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported GBTEMPLATE (%d)", params->GBTEMPLATE);
1051}
1052
1053/**
1054 * jbig2_decode_generic_region: Decode a generic region.
1055 * @ctx: The context for allocation and error reporting.
1056 * @segment: A segment reference for error reporting.
1057 * @params: Decoding parameter set.
1058 * @as: Arithmetic decoder state.
1059 * @image: Where to store the decoded data.
1060 * @GB_stats: Arithmetic stats.
1061 *
1062 * Decodes a generic region, according to section 6.2. The caller should
1063 * pass an already allocated Jbig2Image object for @image
1064 *
1065 * Because this API is based on an arithmetic decoding state, it is
1066 * not suitable for MMR decoding.
1067 *
1068 * Return code: 0 on success.
1069 **/
1070int
1071jbig2_decode_generic_region(Jbig2Ctx *ctx,
1072 Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
1073{
1074 const int8_t *gbat = params->gbat;
1075
1076 if (image->stride * image->height > (1 << 26) && segment->data_length < image->stride * image->height / (1 << 16)) {
1077 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
1078 "region is far larger than data provided (%d << %d), aborting to prevent DOS", segment->data_length, image->stride * image->height);
1079 }
1080
1081 if (!params->MMR && params->TPGDON)
1082 return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
1083
1084 if (!params->MMR && params->GBTEMPLATE == 0) {
1085 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)
1086 return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
1087 else
1088 return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
1089 } else if (!params->MMR && params->GBTEMPLATE == 1) {
1090 if (!params->USESKIP && gbat[0] == +3 && gbat[1] == -1)
1091 return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
1092 else
1093 return jbig2_decode_generic_template1_unopt(ctx, segment, params, as, image, GB_stats);
1094 }
1095 else if (!params->MMR && params->GBTEMPLATE == 2) {
1096 if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1097 return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
1098 else
1099 return jbig2_decode_generic_template2_unopt(ctx, segment, params, as, image, GB_stats);
1100 } else if (!params->MMR && params->GBTEMPLATE == 3) {
1101 if (!params->USESKIP && gbat[0] == 2 && gbat[1] == -1)
1102 return jbig2_decode_generic_template3(ctx, segment, params, as, image, GB_stats);
1103 else
1104 return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
1105 }
1106
1107 {
1108 int i;
1109
1110 for (i = 0; i < 8; i++)
1111 jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]);
1112 }
1113
1114 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unsupported generic region (MMR=%d, GBTEMPLATE=%d)", params->MMR, params->GBTEMPLATE);
1115}
1116
1117/**
1118 * Handler for immediate generic region segments
1119 */
1120int
1121jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
1122{
1123 Jbig2RegionSegmentInfo rsi;
1124 byte seg_flags;
1125 int8_t gbat[8];
1126 int offset;
1127 uint32_t gbat_bytes = 0;
1128 Jbig2GenericRegionParams params;
1129 int code = 0;
1130 Jbig2Image *image = NULL;
1131 Jbig2WordStream *ws = NULL;
1132 Jbig2ArithState *as = NULL;
1133 Jbig2ArithCx *GB_stats = NULL;
1134 uint32_t height;
1135 Jbig2Page *page = &ctx->pages[ctx->current_page];
1136
1137 /* 7.4.6 */
1138 if (segment->data_length < 18)
1139 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1140
1141 jbig2_get_region_segment_info(&rsi, segment_data);
1142 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);
1143
1144 /* 7.4.6.4 */
1145 height = rsi.height;
1146 if (segment->rows != UINT32_MAX) {
1147 if (segment->rows > rsi.height)
1148 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment contains more rows than stated in header");
1149 height = segment->rows;
1150 }
1151
1152 /* 7.4.6.2 */
1153 seg_flags = segment_data[17];
1154 jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
1155 if ((seg_flags & 1) && (seg_flags & 6))
1156 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
1157
1158 /* 7.4.6.3 */
1159 if (!(seg_flags & 1)) {
1160 gbat_bytes = (seg_flags & 6) ? 2 : 8;
1161 if (18 + gbat_bytes > segment->data_length)
1162 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
1163 memcpy(gbat, segment_data + 18, gbat_bytes);
1164 jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
1165 }
1166
1167 offset = 18 + gbat_bytes;
1168
1169 /* Check for T.88 amendment 2 */
1170 if ((seg_flags >> 5) & 1)
1171 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment uses 12 adaptive template pixels (NYI)");
1172
1173 /* Table 34 */
1174 params.MMR = seg_flags & 1;
1175 params.GBTEMPLATE = (seg_flags & 6) >> 1;
1176 params.TPGDON = (seg_flags & 8) >> 3;
1177 params.USESKIP = 0;
1178 memcpy(params.gbat, gbat, gbat_bytes);
1179
1180 if (page->height == 0xffffffff && page->striped && page->stripe_size > 0) {
1181 if (rsi.y >= page->end_row + page->stripe_size) {
1182 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);
1183 return 0;
1184 }
1185 if (height > page->end_row + page->stripe_size) {
1186 height = page->end_row + page->stripe_size;
1187 }
1188 } else {
1189 if (rsi.y >= page->height) {
1190 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);
1191 return 0;
1192 }
1193 if (height > page->height - rsi .y) {
1194 height = page->height - rsi.y;
1195 }
1196 }
1197 if (height == 0) {
1198 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "nothing remains of region, ignoring");
1199 return 0;
1200 }
1201
1202 image = jbig2_image_new(ctx, rsi.width, height);
1203 if (image == NULL)
1204 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate generic image");
1205 jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, height);
1206
1207 if (params.MMR) {
1208 code = jbig2_decode_generic_mmr(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image);
1209 if (code < 0) {
1210 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode MMR-coded generic region");
1211 goto cleanup;
1212 }
1213 } else {
1214 int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
1215
1216 GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
1217 if (GB_stats == NULL) {
1218 code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states when handling immediate generic region");
1219 goto cleanup;
1220 }
1221 memset(GB_stats, 0, stats_size);
1222
1223 ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
1224 if (ws == NULL) {
1225 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocated word stream when handling immediate generic region");
1226 goto cleanup;
1227 }
1228 as = jbig2_arith_new(ctx, ws);
1229 if (as == NULL) {
1230 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling immediate generic region");
1231 goto cleanup;
1232 }
1233 code = jbig2_decode_generic_region(ctx, segment, &params, as, image, GB_stats);
1234 if (code < 0) {
1235 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
1236 goto cleanup;
1237 }
1238 }
1239
1240 code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
1241 if (code < 0)
1242 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add result to page");
1243
1244cleanup:
1245 jbig2_free(ctx->allocator, as);
1246 jbig2_word_stream_buf_free(ctx, ws);
1247 jbig2_free(ctx->allocator, GB_stats);
1248 jbig2_image_release(ctx, image);
1249
1250 return code;
1251}
1252