jbig2_mmr.c 27 KB

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