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