| 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 | /* An implementation of MMR decoding. This is based on the |
| 21 | implementation in Fitz, which in turn is based on the one |
| 22 | in Ghostscript. |
| 23 | */ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include "config.h" |
| 27 | #endif |
| 28 | #include "os_types.h" |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include "jbig2.h" |
| 35 | #include "jbig2_priv.h" |
| 36 | #include "jbig2_arith.h" |
| 37 | #include "jbig2_generic.h" |
| 38 | #include "jbig2_image.h" |
| 39 | #include "jbig2_mmr.h" |
| 40 | #include "jbig2_segment.h" |
| 41 | |
| 42 | #if !defined (UINT32_MAX) |
| 43 | #define UINT32_MAX 0xffffffff |
| 44 | #endif |
| 45 | |
| 46 | typedef struct { |
| 47 | uint32_t width; |
| 48 | uint32_t height; |
| 49 | const byte *data; |
| 50 | size_t size; |
| 51 | uint32_t data_index; |
| 52 | uint32_t bit_index; |
| 53 | uint32_t word; |
| 54 | } Jbig2MmrCtx; |
| 55 | |
| 56 | #define MINUS1 UINT32_MAX |
| 57 | #define ERROR -1 |
| 58 | #define ZEROES -2 |
| 59 | #define UNCOMPRESSED -3 |
| 60 | |
| 61 | static void |
| 62 | jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size) |
| 63 | { |
| 64 | size_t i; |
| 65 | uint32_t word = 0; |
| 66 | |
| 67 | mmr->width = width; |
| 68 | mmr->height = height; |
| 69 | mmr->data = data; |
| 70 | mmr->size = size; |
| 71 | mmr->data_index = 0; |
| 72 | mmr->bit_index = 0; |
| 73 | |
| 74 | for (i = 0; i < size && i < 4; i++) |
| 75 | word |= (data[i] << ((3 - i) << 3)); |
| 76 | mmr->word = word; |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits) |
| 81 | { |
| 82 | mmr->word <<= n_bits; |
| 83 | mmr->bit_index += n_bits; |
| 84 | while (mmr->bit_index >= 8) { |
| 85 | mmr->bit_index -= 8; |
| 86 | if (mmr->data_index + 4 < mmr->size) |
| 87 | mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index); |
| 88 | mmr->data_index++; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | <raph> the first 2^(initialbits) entries map bit patterns to decodes |
| 94 | <raph> let's say initial_bits is 8 for the sake of example |
| 95 | <raph> and that the code is 1001 |
| 96 | <raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 } |
| 97 | <raph> because those are all the bytes that start with the code |
| 98 | <raph> and the 4 is the length of the code |
| 99 | ... if (n_bits > initial_bits) ... |
| 100 | <raph> anyway, in that case, it basically points to a mini table |
| 101 | <raph> the n_bits is the maximum length of all codes beginning with that byte |
| 102 | <raph> so 2^(n_bits - initial_bits) is the size of the mini-table |
| 103 | <raph> peter came up with this, and it makes sense |
| 104 | */ |
| 105 | |
| 106 | typedef struct { |
| 107 | short val; |
| 108 | short n_bits; |
| 109 | } mmr_table_node; |
| 110 | |
| 111 | /* white decode table (runlength huffman codes) */ |
| 112 | const mmr_table_node jbig2_mmr_white_decode[] = { |
| 113 | {256, 12}, |
| 114 | {272, 12}, |
| 115 | {29, 8}, |
| 116 | {30, 8}, |
| 117 | {45, 8}, |
| 118 | {46, 8}, |
| 119 | {22, 7}, |
| 120 | {22, 7}, |
| 121 | {23, 7}, |
| 122 | {23, 7}, |
| 123 | {47, 8}, |
| 124 | {48, 8}, |
| 125 | {13, 6}, |
| 126 | {13, 6}, |
| 127 | {13, 6}, |
| 128 | {13, 6}, |
| 129 | {20, 7}, |
| 130 | {20, 7}, |
| 131 | {33, 8}, |
| 132 | {34, 8}, |
| 133 | {35, 8}, |
| 134 | {36, 8}, |
| 135 | {37, 8}, |
| 136 | {38, 8}, |
| 137 | {19, 7}, |
| 138 | {19, 7}, |
| 139 | {31, 8}, |
| 140 | {32, 8}, |
| 141 | {1, 6}, |
| 142 | {1, 6}, |
| 143 | {1, 6}, |
| 144 | {1, 6}, |
| 145 | {12, 6}, |
| 146 | {12, 6}, |
| 147 | {12, 6}, |
| 148 | {12, 6}, |
| 149 | {53, 8}, |
| 150 | {54, 8}, |
| 151 | {26, 7}, |
| 152 | {26, 7}, |
| 153 | {39, 8}, |
| 154 | {40, 8}, |
| 155 | {41, 8}, |
| 156 | {42, 8}, |
| 157 | {43, 8}, |
| 158 | {44, 8}, |
| 159 | {21, 7}, |
| 160 | {21, 7}, |
| 161 | {28, 7}, |
| 162 | {28, 7}, |
| 163 | {61, 8}, |
| 164 | {62, 8}, |
| 165 | {63, 8}, |
| 166 | {0, 8}, |
| 167 | {320, 8}, |
| 168 | {384, 8}, |
| 169 | {10, 5}, |
| 170 | {10, 5}, |
| 171 | {10, 5}, |
| 172 | {10, 5}, |
| 173 | {10, 5}, |
| 174 | {10, 5}, |
| 175 | {10, 5}, |
| 176 | {10, 5}, |
| 177 | {11, 5}, |
| 178 | {11, 5}, |
| 179 | {11, 5}, |
| 180 | {11, 5}, |
| 181 | {11, 5}, |
| 182 | {11, 5}, |
| 183 | {11, 5}, |
| 184 | {11, 5}, |
| 185 | {27, 7}, |
| 186 | {27, 7}, |
| 187 | {59, 8}, |
| 188 | {60, 8}, |
| 189 | {288, 9}, |
| 190 | {290, 9}, |
| 191 | {18, 7}, |
| 192 | {18, 7}, |
| 193 | {24, 7}, |
| 194 | {24, 7}, |
| 195 | {49, 8}, |
| 196 | {50, 8}, |
| 197 | {51, 8}, |
| 198 | {52, 8}, |
| 199 | {25, 7}, |
| 200 | {25, 7}, |
| 201 | {55, 8}, |
| 202 | {56, 8}, |
| 203 | {57, 8}, |
| 204 | {58, 8}, |
| 205 | {192, 6}, |
| 206 | {192, 6}, |
| 207 | {192, 6}, |
| 208 | {192, 6}, |
| 209 | {1664, 6}, |
| 210 | {1664, 6}, |
| 211 | {1664, 6}, |
| 212 | {1664, 6}, |
| 213 | {448, 8}, |
| 214 | {512, 8}, |
| 215 | {292, 9}, |
| 216 | {640, 8}, |
| 217 | {576, 8}, |
| 218 | {294, 9}, |
| 219 | {296, 9}, |
| 220 | {298, 9}, |
| 221 | {300, 9}, |
| 222 | {302, 9}, |
| 223 | {256, 7}, |
| 224 | {256, 7}, |
| 225 | {2, 4}, |
| 226 | {2, 4}, |
| 227 | {2, 4}, |
| 228 | {2, 4}, |
| 229 | {2, 4}, |
| 230 | {2, 4}, |
| 231 | {2, 4}, |
| 232 | {2, 4}, |
| 233 | {2, 4}, |
| 234 | {2, 4}, |
| 235 | {2, 4}, |
| 236 | {2, 4}, |
| 237 | {2, 4}, |
| 238 | {2, 4}, |
| 239 | {2, 4}, |
| 240 | {2, 4}, |
| 241 | {3, 4}, |
| 242 | {3, 4}, |
| 243 | {3, 4}, |
| 244 | {3, 4}, |
| 245 | {3, 4}, |
| 246 | {3, 4}, |
| 247 | {3, 4}, |
| 248 | {3, 4}, |
| 249 | {3, 4}, |
| 250 | {3, 4}, |
| 251 | {3, 4}, |
| 252 | {3, 4}, |
| 253 | {3, 4}, |
| 254 | {3, 4}, |
| 255 | {3, 4}, |
| 256 | {3, 4}, |
| 257 | {128, 5}, |
| 258 | {128, 5}, |
| 259 | {128, 5}, |
| 260 | {128, 5}, |
| 261 | {128, 5}, |
| 262 | {128, 5}, |
| 263 | {128, 5}, |
| 264 | {128, 5}, |
| 265 | {8, 5}, |
| 266 | {8, 5}, |
| 267 | {8, 5}, |
| 268 | {8, 5}, |
| 269 | {8, 5}, |
| 270 | {8, 5}, |
| 271 | {8, 5}, |
| 272 | {8, 5}, |
| 273 | {9, 5}, |
| 274 | {9, 5}, |
| 275 | {9, 5}, |
| 276 | {9, 5}, |
| 277 | {9, 5}, |
| 278 | {9, 5}, |
| 279 | {9, 5}, |
| 280 | {9, 5}, |
| 281 | {16, 6}, |
| 282 | {16, 6}, |
| 283 | {16, 6}, |
| 284 | {16, 6}, |
| 285 | {17, 6}, |
| 286 | {17, 6}, |
| 287 | {17, 6}, |
| 288 | {17, 6}, |
| 289 | {4, 4}, |
| 290 | {4, 4}, |
| 291 | {4, 4}, |
| 292 | {4, 4}, |
| 293 | {4, 4}, |
| 294 | {4, 4}, |
| 295 | {4, 4}, |
| 296 | {4, 4}, |
| 297 | {4, 4}, |
| 298 | {4, 4}, |
| 299 | {4, 4}, |
| 300 | {4, 4}, |
| 301 | {4, 4}, |
| 302 | {4, 4}, |
| 303 | {4, 4}, |
| 304 | {4, 4}, |
| 305 | {5, 4}, |
| 306 | {5, 4}, |
| 307 | {5, 4}, |
| 308 | {5, 4}, |
| 309 | {5, 4}, |
| 310 | {5, 4}, |
| 311 | {5, 4}, |
| 312 | {5, 4}, |
| 313 | {5, 4}, |
| 314 | {5, 4}, |
| 315 | {5, 4}, |
| 316 | {5, 4}, |
| 317 | {5, 4}, |
| 318 | {5, 4}, |
| 319 | {5, 4}, |
| 320 | {5, 4}, |
| 321 | {14, 6}, |
| 322 | {14, 6}, |
| 323 | {14, 6}, |
| 324 | {14, 6}, |
| 325 | {15, 6}, |
| 326 | {15, 6}, |
| 327 | {15, 6}, |
| 328 | {15, 6}, |
| 329 | {64, 5}, |
| 330 | {64, 5}, |
| 331 | {64, 5}, |
| 332 | {64, 5}, |
| 333 | {64, 5}, |
| 334 | {64, 5}, |
| 335 | {64, 5}, |
| 336 | {64, 5}, |
| 337 | {6, 4}, |
| 338 | {6, 4}, |
| 339 | {6, 4}, |
| 340 | {6, 4}, |
| 341 | {6, 4}, |
| 342 | {6, 4}, |
| 343 | {6, 4}, |
| 344 | {6, 4}, |
| 345 | {6, 4}, |
| 346 | {6, 4}, |
| 347 | {6, 4}, |
| 348 | {6, 4}, |
| 349 | {6, 4}, |
| 350 | {6, 4}, |
| 351 | {6, 4}, |
| 352 | {6, 4}, |
| 353 | {7, 4}, |
| 354 | {7, 4}, |
| 355 | {7, 4}, |
| 356 | {7, 4}, |
| 357 | {7, 4}, |
| 358 | {7, 4}, |
| 359 | {7, 4}, |
| 360 | {7, 4}, |
| 361 | {7, 4}, |
| 362 | {7, 4}, |
| 363 | {7, 4}, |
| 364 | {7, 4}, |
| 365 | {7, 4}, |
| 366 | {7, 4}, |
| 367 | {7, 4}, |
| 368 | {7, 4}, |
| 369 | {-2, 3}, |
| 370 | {-2, 3}, |
| 371 | {-1, 0}, |
| 372 | {-1, 0}, |
| 373 | {-1, 0}, |
| 374 | {-1, 0}, |
| 375 | {-1, 0}, |
| 376 | {-1, 0}, |
| 377 | {-1, 0}, |
| 378 | {-1, 0}, |
| 379 | {-1, 0}, |
| 380 | {-1, 0}, |
| 381 | {-1, 0}, |
| 382 | {-1, 0}, |
| 383 | {-1, 0}, |
| 384 | {-3, 4}, |
| 385 | {1792, 3}, |
| 386 | {1792, 3}, |
| 387 | {1984, 4}, |
| 388 | {2048, 4}, |
| 389 | {2112, 4}, |
| 390 | {2176, 4}, |
| 391 | {2240, 4}, |
| 392 | {2304, 4}, |
| 393 | {1856, 3}, |
| 394 | {1856, 3}, |
| 395 | {1920, 3}, |
| 396 | {1920, 3}, |
| 397 | {2368, 4}, |
| 398 | {2432, 4}, |
| 399 | {2496, 4}, |
| 400 | {2560, 4}, |
| 401 | {1472, 1}, |
| 402 | {1536, 1}, |
| 403 | {1600, 1}, |
| 404 | {1728, 1}, |
| 405 | {704, 1}, |
| 406 | {768, 1}, |
| 407 | {832, 1}, |
| 408 | {896, 1}, |
| 409 | {960, 1}, |
| 410 | {1024, 1}, |
| 411 | {1088, 1}, |
| 412 | {1152, 1}, |
| 413 | {1216, 1}, |
| 414 | {1280, 1}, |
| 415 | {1344, 1}, |
| 416 | {1408, 1} |
| 417 | }; |
| 418 | |
| 419 | /* black decode table (runlength huffman codes) */ |
| 420 | const mmr_table_node jbig2_mmr_black_decode[] = { |
| 421 | {128, 12}, |
| 422 | {160, 13}, |
| 423 | {224, 12}, |
| 424 | {256, 12}, |
| 425 | {10, 7}, |
| 426 | {11, 7}, |
| 427 | {288, 12}, |
| 428 | {12, 7}, |
| 429 | {9, 6}, |
| 430 | {9, 6}, |
| 431 | {8, 6}, |
| 432 | {8, 6}, |
| 433 | {7, 5}, |
| 434 | {7, 5}, |
| 435 | {7, 5}, |
| 436 | {7, 5}, |
| 437 | {6, 4}, |
| 438 | {6, 4}, |
| 439 | {6, 4}, |
| 440 | {6, 4}, |
| 441 | {6, 4}, |
| 442 | {6, 4}, |
| 443 | {6, 4}, |
| 444 | {6, 4}, |
| 445 | {5, 4}, |
| 446 | {5, 4}, |
| 447 | {5, 4}, |
| 448 | {5, 4}, |
| 449 | {5, 4}, |
| 450 | {5, 4}, |
| 451 | {5, 4}, |
| 452 | {5, 4}, |
| 453 | {1, 3}, |
| 454 | {1, 3}, |
| 455 | {1, 3}, |
| 456 | {1, 3}, |
| 457 | {1, 3}, |
| 458 | {1, 3}, |
| 459 | {1, 3}, |
| 460 | {1, 3}, |
| 461 | {1, 3}, |
| 462 | {1, 3}, |
| 463 | {1, 3}, |
| 464 | {1, 3}, |
| 465 | {1, 3}, |
| 466 | {1, 3}, |
| 467 | {1, 3}, |
| 468 | {1, 3}, |
| 469 | {4, 3}, |
| 470 | {4, 3}, |
| 471 | {4, 3}, |
| 472 | {4, 3}, |
| 473 | {4, 3}, |
| 474 | {4, 3}, |
| 475 | {4, 3}, |
| 476 | {4, 3}, |
| 477 | {4, 3}, |
| 478 | {4, 3}, |
| 479 | {4, 3}, |
| 480 | {4, 3}, |
| 481 | {4, 3}, |
| 482 | {4, 3}, |
| 483 | {4, 3}, |
| 484 | {4, 3}, |
| 485 | {3, 2}, |
| 486 | {3, 2}, |
| 487 | {3, 2}, |
| 488 | {3, 2}, |
| 489 | {3, 2}, |
| 490 | {3, 2}, |
| 491 | {3, 2}, |
| 492 | {3, 2}, |
| 493 | {3, 2}, |
| 494 | {3, 2}, |
| 495 | {3, 2}, |
| 496 | {3, 2}, |
| 497 | {3, 2}, |
| 498 | {3, 2}, |
| 499 | {3, 2}, |
| 500 | {3, 2}, |
| 501 | {3, 2}, |
| 502 | {3, 2}, |
| 503 | {3, 2}, |
| 504 | {3, 2}, |
| 505 | {3, 2}, |
| 506 | {3, 2}, |
| 507 | {3, 2}, |
| 508 | {3, 2}, |
| 509 | {3, 2}, |
| 510 | {3, 2}, |
| 511 | {3, 2}, |
| 512 | {3, 2}, |
| 513 | {3, 2}, |
| 514 | {3, 2}, |
| 515 | {3, 2}, |
| 516 | {3, 2}, |
| 517 | {2, 2}, |
| 518 | {2, 2}, |
| 519 | {2, 2}, |
| 520 | {2, 2}, |
| 521 | {2, 2}, |
| 522 | {2, 2}, |
| 523 | {2, 2}, |
| 524 | {2, 2}, |
| 525 | {2, 2}, |
| 526 | {2, 2}, |
| 527 | {2, 2}, |
| 528 | {2, 2}, |
| 529 | {2, 2}, |
| 530 | {2, 2}, |
| 531 | {2, 2}, |
| 532 | {2, 2}, |
| 533 | {2, 2}, |
| 534 | {2, 2}, |
| 535 | {2, 2}, |
| 536 | {2, 2}, |
| 537 | {2, 2}, |
| 538 | {2, 2}, |
| 539 | {2, 2}, |
| 540 | {2, 2}, |
| 541 | {2, 2}, |
| 542 | {2, 2}, |
| 543 | {2, 2}, |
| 544 | {2, 2}, |
| 545 | {2, 2}, |
| 546 | {2, 2}, |
| 547 | {2, 2}, |
| 548 | {2, 2}, |
| 549 | {-2, 4}, |
| 550 | {-2, 4}, |
| 551 | {-1, 0}, |
| 552 | {-1, 0}, |
| 553 | {-1, 0}, |
| 554 | {-1, 0}, |
| 555 | {-1, 0}, |
| 556 | {-1, 0}, |
| 557 | {-1, 0}, |
| 558 | {-1, 0}, |
| 559 | {-1, 0}, |
| 560 | {-1, 0}, |
| 561 | {-1, 0}, |
| 562 | {-1, 0}, |
| 563 | {-1, 0}, |
| 564 | {-3, 5}, |
| 565 | {1792, 4}, |
| 566 | {1792, 4}, |
| 567 | {1984, 5}, |
| 568 | {2048, 5}, |
| 569 | {2112, 5}, |
| 570 | {2176, 5}, |
| 571 | {2240, 5}, |
| 572 | {2304, 5}, |
| 573 | {1856, 4}, |
| 574 | {1856, 4}, |
| 575 | {1920, 4}, |
| 576 | {1920, 4}, |
| 577 | {2368, 5}, |
| 578 | {2432, 5}, |
| 579 | {2496, 5}, |
| 580 | {2560, 5}, |
| 581 | {18, 3}, |
| 582 | {18, 3}, |
| 583 | {18, 3}, |
| 584 | {18, 3}, |
| 585 | {18, 3}, |
| 586 | {18, 3}, |
| 587 | {18, 3}, |
| 588 | {18, 3}, |
| 589 | {52, 5}, |
| 590 | {52, 5}, |
| 591 | {640, 6}, |
| 592 | {704, 6}, |
| 593 | {768, 6}, |
| 594 | {832, 6}, |
| 595 | {55, 5}, |
| 596 | {55, 5}, |
| 597 | {56, 5}, |
| 598 | {56, 5}, |
| 599 | {1280, 6}, |
| 600 | {1344, 6}, |
| 601 | {1408, 6}, |
| 602 | {1472, 6}, |
| 603 | {59, 5}, |
| 604 | {59, 5}, |
| 605 | {60, 5}, |
| 606 | {60, 5}, |
| 607 | {1536, 6}, |
| 608 | {1600, 6}, |
| 609 | {24, 4}, |
| 610 | {24, 4}, |
| 611 | {24, 4}, |
| 612 | {24, 4}, |
| 613 | {25, 4}, |
| 614 | {25, 4}, |
| 615 | {25, 4}, |
| 616 | {25, 4}, |
| 617 | {1664, 6}, |
| 618 | {1728, 6}, |
| 619 | {320, 5}, |
| 620 | {320, 5}, |
| 621 | {384, 5}, |
| 622 | {384, 5}, |
| 623 | {448, 5}, |
| 624 | {448, 5}, |
| 625 | {512, 6}, |
| 626 | {576, 6}, |
| 627 | {53, 5}, |
| 628 | {53, 5}, |
| 629 | {54, 5}, |
| 630 | {54, 5}, |
| 631 | {896, 6}, |
| 632 | {960, 6}, |
| 633 | {1024, 6}, |
| 634 | {1088, 6}, |
| 635 | {1152, 6}, |
| 636 | {1216, 6}, |
| 637 | {64, 3}, |
| 638 | {64, 3}, |
| 639 | {64, 3}, |
| 640 | {64, 3}, |
| 641 | {64, 3}, |
| 642 | {64, 3}, |
| 643 | {64, 3}, |
| 644 | {64, 3}, |
| 645 | {13, 1}, |
| 646 | {13, 1}, |
| 647 | {13, 1}, |
| 648 | {13, 1}, |
| 649 | {13, 1}, |
| 650 | {13, 1}, |
| 651 | {13, 1}, |
| 652 | {13, 1}, |
| 653 | {13, 1}, |
| 654 | {13, 1}, |
| 655 | {13, 1}, |
| 656 | {13, 1}, |
| 657 | {13, 1}, |
| 658 | {13, 1}, |
| 659 | {13, 1}, |
| 660 | {13, 1}, |
| 661 | {23, 4}, |
| 662 | {23, 4}, |
| 663 | {50, 5}, |
| 664 | {51, 5}, |
| 665 | {44, 5}, |
| 666 | {45, 5}, |
| 667 | {46, 5}, |
| 668 | {47, 5}, |
| 669 | {57, 5}, |
| 670 | {58, 5}, |
| 671 | {61, 5}, |
| 672 | {256, 5}, |
| 673 | {16, 3}, |
| 674 | {16, 3}, |
| 675 | {16, 3}, |
| 676 | {16, 3}, |
| 677 | {17, 3}, |
| 678 | {17, 3}, |
| 679 | {17, 3}, |
| 680 | {17, 3}, |
| 681 | {48, 5}, |
| 682 | {49, 5}, |
| 683 | {62, 5}, |
| 684 | {63, 5}, |
| 685 | {30, 5}, |
| 686 | {31, 5}, |
| 687 | {32, 5}, |
| 688 | {33, 5}, |
| 689 | {40, 5}, |
| 690 | {41, 5}, |
| 691 | {22, 4}, |
| 692 | {22, 4}, |
| 693 | {14, 1}, |
| 694 | {14, 1}, |
| 695 | {14, 1}, |
| 696 | {14, 1}, |
| 697 | {14, 1}, |
| 698 | {14, 1}, |
| 699 | {14, 1}, |
| 700 | {14, 1}, |
| 701 | {14, 1}, |
| 702 | {14, 1}, |
| 703 | {14, 1}, |
| 704 | {14, 1}, |
| 705 | {14, 1}, |
| 706 | {14, 1}, |
| 707 | {14, 1}, |
| 708 | {14, 1}, |
| 709 | {15, 2}, |
| 710 | {15, 2}, |
| 711 | {15, 2}, |
| 712 | {15, 2}, |
| 713 | {15, 2}, |
| 714 | {15, 2}, |
| 715 | {15, 2}, |
| 716 | {15, 2}, |
| 717 | {128, 5}, |
| 718 | {192, 5}, |
| 719 | {26, 5}, |
| 720 | {27, 5}, |
| 721 | {28, 5}, |
| 722 | {29, 5}, |
| 723 | {19, 4}, |
| 724 | {19, 4}, |
| 725 | {20, 4}, |
| 726 | {20, 4}, |
| 727 | {34, 5}, |
| 728 | {35, 5}, |
| 729 | {36, 5}, |
| 730 | {37, 5}, |
| 731 | {38, 5}, |
| 732 | {39, 5}, |
| 733 | {21, 4}, |
| 734 | {21, 4}, |
| 735 | {42, 5}, |
| 736 | {43, 5}, |
| 737 | {0, 3}, |
| 738 | {0, 3}, |
| 739 | {0, 3}, |
| 740 | {0, 3} |
| 741 | }; |
| 742 | |
| 743 | #define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 ) |
| 744 | |
| 745 | static uint32_t |
| 746 | jbig2_find_changing_element(const byte *line, uint32_t x, uint32_t w) |
| 747 | { |
| 748 | int a, b; |
| 749 | |
| 750 | if (line == NULL) |
| 751 | return w; |
| 752 | |
| 753 | if (x == MINUS1) { |
| 754 | a = 0; |
| 755 | x = 0; |
| 756 | } else if (x < w) { |
| 757 | a = getbit(line, x); |
| 758 | x++; |
| 759 | } else { |
| 760 | return x; |
| 761 | } |
| 762 | |
| 763 | while (x < w) { |
| 764 | b = getbit(line, x); |
| 765 | if (a != b) |
| 766 | break; |
| 767 | x++; |
| 768 | } |
| 769 | |
| 770 | return x; |
| 771 | } |
| 772 | |
| 773 | static uint32_t |
| 774 | jbig2_find_changing_element_of_color(const byte *line, uint32_t x, uint32_t w, int color) |
| 775 | { |
| 776 | if (line == NULL) |
| 777 | return w; |
| 778 | x = jbig2_find_changing_element(line, x, w); |
| 779 | if (x < w && getbit(line, x) != color) |
| 780 | x = jbig2_find_changing_element(line, x, w); |
| 781 | return x; |
| 782 | } |
| 783 | |
| 784 | static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 }; |
| 785 | static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; |
| 786 | |
| 787 | static void |
| 788 | jbig2_set_bits(byte *line, uint32_t x0, uint32_t x1) |
| 789 | { |
| 790 | uint32_t a0, a1, b0, b1, a; |
| 791 | |
| 792 | a0 = x0 >> 3; |
| 793 | a1 = x1 >> 3; |
| 794 | |
| 795 | b0 = x0 & 7; |
| 796 | b1 = x1 & 7; |
| 797 | |
| 798 | if (a0 == a1) { |
| 799 | line[a0] |= lm[b0] & rm[b1]; |
| 800 | } else { |
| 801 | line[a0] |= lm[b0]; |
| 802 | for (a = a0 + 1; a < a1; a++) |
| 803 | line[a] = 0xFF; |
| 804 | if (b1) |
| 805 | line[a1] |= rm[b1]; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits) |
| 811 | { |
| 812 | uint32_t word = mmr->word; |
| 813 | int table_ix = word >> (32 - initial_bits); |
| 814 | int val = table[table_ix].val; |
| 815 | int n_bits = table[table_ix].n_bits; |
| 816 | |
| 817 | if (n_bits > initial_bits) { |
| 818 | int mask = (1 << (32 - initial_bits)) - 1; |
| 819 | |
| 820 | table_ix = val + ((word & mask) >> (32 - n_bits)); |
| 821 | val = table[table_ix].val; |
| 822 | n_bits = initial_bits + table[table_ix].n_bits; |
| 823 | } |
| 824 | |
| 825 | jbig2_decode_mmr_consume(mmr, n_bits); |
| 826 | |
| 827 | return val; |
| 828 | } |
| 829 | |
| 830 | static int |
| 831 | jbig2_decode_get_run(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits) |
| 832 | { |
| 833 | int result = 0; |
| 834 | int val; |
| 835 | |
| 836 | do { |
| 837 | val = jbig2_decode_get_code(mmr, table, initial_bits); |
| 838 | if (val == ERROR) |
| 839 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "invalid code detected in MMR-coded data" ); |
| 840 | else if (val == UNCOMPRESSED) |
| 841 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "uncompressed code in MMR-coded data" ); |
| 842 | else if (val == ZEROES) |
| 843 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "zeroes code in MMR-coded data" ); |
| 844 | result += val; |
| 845 | } while (val >= 64); |
| 846 | |
| 847 | return result; |
| 848 | } |
| 849 | |
| 850 | static int |
| 851 | jbig2_decode_mmr_line(Jbig2Ctx *ctx, Jbig2MmrCtx *mmr, const byte *ref, byte *dst, int *eofb) |
| 852 | { |
| 853 | uint32_t a0 = MINUS1; |
| 854 | uint32_t a1, a2, b1, b2; |
| 855 | int c = 0; /* 0 is white, black is 1 */ |
| 856 | |
| 857 | while (1) { |
| 858 | uint32_t word = mmr->word; |
| 859 | |
| 860 | /* printf ("%08x\n", word); */ |
| 861 | |
| 862 | if (a0 != MINUS1 && a0 >= mmr->width) |
| 863 | break; |
| 864 | |
| 865 | if ((word >> (32 - 3)) == 1) { |
| 866 | int white_run, black_run; |
| 867 | |
| 868 | jbig2_decode_mmr_consume(mmr, 3); |
| 869 | |
| 870 | if (a0 == MINUS1) |
| 871 | a0 = 0; |
| 872 | |
| 873 | if (c == 0) { |
| 874 | white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8); |
| 875 | if (white_run < 0) |
| 876 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode white H run" ); |
| 877 | black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7); |
| 878 | if (black_run < 0) |
| 879 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode black H run" ); |
| 880 | /* printf ("H %d %d\n", white_run, black_run); */ |
| 881 | a1 = a0 + white_run; |
| 882 | a2 = a1 + black_run; |
| 883 | if (a1 > mmr->width) |
| 884 | a1 = mmr->width; |
| 885 | if (a2 > mmr->width) |
| 886 | a2 = mmr->width; |
| 887 | if (a2 < a1) { |
| 888 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative black H run" ); |
| 889 | a2 = a1; |
| 890 | } |
| 891 | if (a1 < mmr->width) |
| 892 | jbig2_set_bits(dst, a1, a2); |
| 893 | a0 = a2; |
| 894 | } else { |
| 895 | black_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_black_decode, 7); |
| 896 | if (black_run < 0) |
| 897 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode black H run" ); |
| 898 | white_run = jbig2_decode_get_run(ctx, mmr, jbig2_mmr_white_decode, 8); |
| 899 | if (white_run < 0) |
| 900 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode white H run" ); |
| 901 | /* printf ("H %d %d\n", black_run, white_run); */ |
| 902 | a1 = a0 + black_run; |
| 903 | a2 = a1 + white_run; |
| 904 | if (a1 > mmr->width) |
| 905 | a1 = mmr->width; |
| 906 | if (a2 > mmr->width) |
| 907 | a2 = mmr->width; |
| 908 | if (a1 < a0) { |
| 909 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative white H run" ); |
| 910 | a1 = a0; |
| 911 | } |
| 912 | if (a0 < mmr->width) |
| 913 | jbig2_set_bits(dst, a0, a1); |
| 914 | a0 = a2; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | else if ((word >> (32 - 4)) == 1) { |
| 919 | /* printf ("P\n"); */ |
| 920 | jbig2_decode_mmr_consume(mmr, 4); |
| 921 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 922 | b2 = jbig2_find_changing_element(ref, b1, mmr->width); |
| 923 | if (c) { |
| 924 | if (b2 < a0) { |
| 925 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative P run" ); |
| 926 | b2 = a0; |
| 927 | } |
| 928 | if (a0 < mmr->width) |
| 929 | jbig2_set_bits(dst, a0, b2); |
| 930 | } |
| 931 | a0 = b2; |
| 932 | } |
| 933 | |
| 934 | else if ((word >> (32 - 1)) == 1) { |
| 935 | /* printf ("V(0)\n"); */ |
| 936 | jbig2_decode_mmr_consume(mmr, 1); |
| 937 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 938 | if (c) { |
| 939 | if (b1 < a0) { |
| 940 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative V(0) run" ); |
| 941 | b1 = a0; |
| 942 | } |
| 943 | if (a0 < mmr->width) |
| 944 | jbig2_set_bits(dst, a0, b1); |
| 945 | } |
| 946 | a0 = b1; |
| 947 | c = !c; |
| 948 | } |
| 949 | |
| 950 | else if ((word >> (32 - 3)) == 3) { |
| 951 | /* printf ("VR(1)\n"); */ |
| 952 | jbig2_decode_mmr_consume(mmr, 3); |
| 953 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 954 | if (b1 + 1 <= mmr->width) |
| 955 | b1 += 1; |
| 956 | if (c) { |
| 957 | if (b1 < a0) { |
| 958 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VR(1) run" ); |
| 959 | b1 = a0; |
| 960 | } |
| 961 | if (a0 < mmr->width) |
| 962 | jbig2_set_bits(dst, a0, b1); |
| 963 | } |
| 964 | a0 = b1; |
| 965 | c = !c; |
| 966 | } |
| 967 | |
| 968 | else if ((word >> (32 - 6)) == 3) { |
| 969 | /* printf ("VR(2)\n"); */ |
| 970 | jbig2_decode_mmr_consume(mmr, 6); |
| 971 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 972 | if (b1 + 2 <= mmr->width) |
| 973 | b1 += 2; |
| 974 | if (c) { |
| 975 | if (b1 < a0) { |
| 976 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VR(2) run" ); |
| 977 | b1 = a0; |
| 978 | } |
| 979 | if (a0 < mmr->width) |
| 980 | jbig2_set_bits(dst, a0, b1); |
| 981 | } |
| 982 | a0 = b1; |
| 983 | c = !c; |
| 984 | } |
| 985 | |
| 986 | else if ((word >> (32 - 7)) == 3) { |
| 987 | /* printf ("VR(3)\n"); */ |
| 988 | jbig2_decode_mmr_consume(mmr, 7); |
| 989 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 990 | if (b1 + 3 <= mmr->width) |
| 991 | b1 += 3; |
| 992 | if (c) { |
| 993 | if (b1 < a0) { |
| 994 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VR(3) run" ); |
| 995 | b1 = a0; |
| 996 | } |
| 997 | if (a0 < mmr->width) |
| 998 | jbig2_set_bits(dst, a0, b1); |
| 999 | } |
| 1000 | a0 = b1; |
| 1001 | c = !c; |
| 1002 | } |
| 1003 | |
| 1004 | else if ((word >> (32 - 3)) == 2) { |
| 1005 | /* printf ("VL(1)\n"); */ |
| 1006 | jbig2_decode_mmr_consume(mmr, 3); |
| 1007 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 1008 | if (b1 >= 1) |
| 1009 | b1 -= 1; |
| 1010 | if (c) { |
| 1011 | if (b1 < a0) { |
| 1012 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VL(1) run" ); |
| 1013 | b1 = a0; |
| 1014 | } |
| 1015 | if (a0 < mmr->width) |
| 1016 | jbig2_set_bits(dst, a0, b1); |
| 1017 | } |
| 1018 | a0 = b1; |
| 1019 | c = !c; |
| 1020 | } |
| 1021 | |
| 1022 | else if ((word >> (32 - 6)) == 2) { |
| 1023 | /* printf ("VL(2)\n"); */ |
| 1024 | jbig2_decode_mmr_consume(mmr, 6); |
| 1025 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 1026 | if (b1 >= 2) |
| 1027 | b1 -= 2; |
| 1028 | if (c) { |
| 1029 | if (b1 < a0) { |
| 1030 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VL(2) run" ); |
| 1031 | b1 = a0; |
| 1032 | } |
| 1033 | if (a0 < mmr->width) |
| 1034 | jbig2_set_bits(dst, a0, b1); |
| 1035 | } |
| 1036 | a0 = b1; |
| 1037 | c = !c; |
| 1038 | } |
| 1039 | |
| 1040 | else if ((word >> (32 - 7)) == 2) { |
| 1041 | /* printf ("VL(3)\n"); */ |
| 1042 | jbig2_decode_mmr_consume(mmr, 7); |
| 1043 | b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c); |
| 1044 | if (b1 >= 3) |
| 1045 | b1 -= 3; |
| 1046 | if (c) { |
| 1047 | if (b1 < a0) { |
| 1048 | jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "ignoring negative VL(3) run" ); |
| 1049 | b1 = a0; |
| 1050 | } |
| 1051 | if (a0 < mmr->width) |
| 1052 | jbig2_set_bits(dst, a0, b1); |
| 1053 | } |
| 1054 | a0 = b1; |
| 1055 | c = !c; |
| 1056 | } |
| 1057 | |
| 1058 | else if ((word >> (32 - 24)) == 0x1001) { |
| 1059 | /* printf ("EOFB\n"); */ |
| 1060 | jbig2_decode_mmr_consume(mmr, 24); |
| 1061 | *eofb = 1; |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | else |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | int |
| 1073 | jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image) |
| 1074 | { |
| 1075 | Jbig2MmrCtx mmr; |
| 1076 | const uint32_t rowstride = image->stride; |
| 1077 | byte *dst = image->data; |
| 1078 | byte *ref = NULL; |
| 1079 | uint32_t y; |
| 1080 | int code = 0; |
| 1081 | int eofb = 0; |
| 1082 | |
| 1083 | jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size); |
| 1084 | |
| 1085 | for (y = 0; !eofb && y < image->height; y++) { |
| 1086 | memset(dst, 0, rowstride); |
| 1087 | code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb); |
| 1088 | if (code < 0) |
| 1089 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode mmr line" ); |
| 1090 | ref = dst; |
| 1091 | dst += rowstride; |
| 1092 | } |
| 1093 | |
| 1094 | if (eofb && y < image->height) { |
| 1095 | memset(dst, 0, rowstride * (image->height - y)); |
| 1096 | } |
| 1097 | |
| 1098 | return code; |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * jbig2_decode_halftone_mmr: decode mmr region inside of halftones |
| 1103 | * |
| 1104 | * @ctx: jbig2 decoder context |
| 1105 | * @params: parameters for decoding |
| 1106 | * @data: pointer to text region data to be decoded |
| 1107 | * @size: length of text region data |
| 1108 | * @image: return of decoded image |
| 1109 | * @consumed_bytes: return of consumed bytes from @data |
| 1110 | * |
| 1111 | * MMR decoding that consumes EOFB and returns consumed bytes (@consumed_bytes) |
| 1112 | * |
| 1113 | * returns: 0 |
| 1114 | **/ |
| 1115 | int |
| 1116 | jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes) |
| 1117 | { |
| 1118 | Jbig2MmrCtx mmr; |
| 1119 | const uint32_t rowstride = image->stride; |
| 1120 | byte *dst = image->data; |
| 1121 | byte *ref = NULL; |
| 1122 | uint32_t y; |
| 1123 | int code = 0; |
| 1124 | const uint32_t EOFB = 0x001001; |
| 1125 | int eofb = 0; |
| 1126 | |
| 1127 | jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size); |
| 1128 | |
| 1129 | for (y = 0; !eofb && y < image->height; y++) { |
| 1130 | memset(dst, 0, rowstride); |
| 1131 | code = jbig2_decode_mmr_line(ctx, &mmr, ref, dst, &eofb); |
| 1132 | if (code < 0) |
| 1133 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to decode halftone mmr line" ); |
| 1134 | ref = dst; |
| 1135 | dst += rowstride; |
| 1136 | } |
| 1137 | |
| 1138 | if (eofb && y < image->height) { |
| 1139 | memset(dst, 0, rowstride * (image->height - y)); |
| 1140 | } |
| 1141 | |
| 1142 | /* test for EOFB (see section 6.2.6) */ |
| 1143 | if (mmr.word >> 8 == EOFB) { |
| 1144 | jbig2_decode_mmr_consume(&mmr, 24); |
| 1145 | } |
| 1146 | |
| 1147 | *consumed_bytes += mmr.data_index + (mmr.bit_index >> 3) + (mmr.bit_index > 0 ? 1 : 0); |
| 1148 | return code; |
| 1149 | } |
| 1150 | |