Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2001-2002 Artifex Software, Inc.
  5.  
  6.     This software is provided AS-IS with no warranty,
  7.     either express or implied.
  8.  
  9.     This software is distributed under license and may not
  10.     be copied, modified or distributed except as expressly
  11.     authorized under the terms of the license contained in
  12.     the file LICENSE in this distribution.
  13.  
  14.     For further licensing information refer to http://artifex.com/ or
  15.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  16.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  17. */
  18.  
  19. /* An implementation of MMR decoding. This is based on the
  20.    implementation in Fitz, which in turn is based on the one
  21.    in Ghostscript.
  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 <stdio.h>
  31. #include <string.h>
  32.  
  33. #include "jbig2.h"
  34. #include "jbig2_priv.h"
  35. #include "jbig2_arith.h"
  36. #include "jbig2_generic.h"
  37. #include "jbig2_mmr.h"
  38.  
  39. typedef struct {
  40.         int width;
  41.         int height;
  42.         const byte *data;
  43.         size_t size;
  44.         int data_index;
  45.         int bit_index;
  46.         uint32_t word;
  47. } Jbig2MmrCtx;
  48.  
  49.  
  50. static void
  51. jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
  52. {
  53.         int i;
  54.         uint32_t word = 0;
  55.  
  56.         mmr->width = width;
  57.         mmr->height = height;
  58.         mmr->data = data;
  59.         mmr->size = size;
  60.         mmr->data_index = 0;
  61.         mmr->bit_index = 0;
  62.  
  63.         for (i = 0; i < size && i < 4; i++)
  64.                 word |= (data[i] << ((3 - i) << 3));
  65.         mmr->word = word;
  66. }
  67.  
  68. static void
  69. jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
  70. {
  71.         mmr->word <<= n_bits;
  72.         mmr->bit_index += n_bits;
  73.         while (mmr->bit_index >= 8) {
  74.                 mmr->bit_index -= 8;
  75.                 if (mmr->data_index + 4 < mmr->size)
  76.                         mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index);
  77.                 mmr->data_index++;
  78.         }
  79. }
  80.  
  81. /*
  82. <raph> the first 2^(initialbits) entries map bit patterns to decodes
  83. <raph> let's say initial_bits is 8 for the sake of example
  84. <raph> and that the code is 1001
  85. <raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
  86. <raph> because those are all the bytes that start with the code
  87. <raph> and the 4 is the length of the code
  88. ... if (n_bits > initial_bits) ...
  89. <raph> anyway, in that case, it basically points to a mini table
  90. <raph> the n_bits is the maximum length of all codes beginning with that byte
  91. <raph> so 2^(n_bits - initial_bits) is the size of the mini-table
  92. <raph> peter came up with this, and it makes sense
  93. */
  94.  
  95. typedef struct {
  96.         short val;
  97.         short n_bits;
  98. } mmr_table_node;
  99.  
  100. /* white decode table (runlength huffman codes) */
  101. const mmr_table_node jbig2_mmr_white_decode[] = {
  102. { 256, 12 },
  103. { 272, 12 },
  104. { 29, 8 },
  105. { 30, 8 },
  106. { 45, 8 },
  107. { 46, 8 },
  108. { 22, 7 },
  109. { 22, 7 },
  110. { 23, 7 },
  111. { 23, 7 },
  112. { 47, 8 },
  113. { 48, 8 },
  114. { 13, 6 },
  115. { 13, 6 },
  116. { 13, 6 },
  117. { 13, 6 },
  118. { 20, 7 },
  119. { 20, 7 },
  120. { 33, 8 },
  121. { 34, 8 },
  122. { 35, 8 },
  123. { 36, 8 },
  124. { 37, 8 },
  125. { 38, 8 },
  126. { 19, 7 },
  127. { 19, 7 },
  128. { 31, 8 },
  129. { 32, 8 },
  130. { 1, 6 },
  131. { 1, 6 },
  132. { 1, 6 },
  133. { 1, 6 },
  134. { 12, 6 },
  135. { 12, 6 },
  136. { 12, 6 },
  137. { 12, 6 },
  138. { 53, 8 },
  139. { 54, 8 },
  140. { 26, 7 },
  141. { 26, 7 },
  142. { 39, 8 },
  143. { 40, 8 },
  144. { 41, 8 },
  145. { 42, 8 },
  146. { 43, 8 },
  147. { 44, 8 },
  148. { 21, 7 },
  149. { 21, 7 },
  150. { 28, 7 },
  151. { 28, 7 },
  152. { 61, 8 },
  153. { 62, 8 },
  154. { 63, 8 },
  155. { 0, 8 },
  156. { 320, 8 },
  157. { 384, 8 },
  158. { 10, 5 },
  159. { 10, 5 },
  160. { 10, 5 },
  161. { 10, 5 },
  162. { 10, 5 },
  163. { 10, 5 },
  164. { 10, 5 },
  165. { 10, 5 },
  166. { 11, 5 },
  167. { 11, 5 },
  168. { 11, 5 },
  169. { 11, 5 },
  170. { 11, 5 },
  171. { 11, 5 },
  172. { 11, 5 },
  173. { 11, 5 },
  174. { 27, 7 },
  175. { 27, 7 },
  176. { 59, 8 },
  177. { 60, 8 },
  178. { 288, 9 },
  179. { 290, 9 },
  180. { 18, 7 },
  181. { 18, 7 },
  182. { 24, 7 },
  183. { 24, 7 },
  184. { 49, 8 },
  185. { 50, 8 },
  186. { 51, 8 },
  187. { 52, 8 },
  188. { 25, 7 },
  189. { 25, 7 },
  190. { 55, 8 },
  191. { 56, 8 },
  192. { 57, 8 },
  193. { 58, 8 },
  194. { 192, 6 },
  195. { 192, 6 },
  196. { 192, 6 },
  197. { 192, 6 },
  198. { 1664, 6 },
  199. { 1664, 6 },
  200. { 1664, 6 },
  201. { 1664, 6 },
  202. { 448, 8 },
  203. { 512, 8 },
  204. { 292, 9 },
  205. { 640, 8 },
  206. { 576, 8 },
  207. { 294, 9 },
  208. { 296, 9 },
  209. { 298, 9 },
  210. { 300, 9 },
  211. { 302, 9 },
  212. { 256, 7 },
  213. { 256, 7 },
  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. { 2, 4 },
  227. { 2, 4 },
  228. { 2, 4 },
  229. { 2, 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. { 3, 4 },
  243. { 3, 4 },
  244. { 3, 4 },
  245. { 3, 4 },
  246. { 128, 5 },
  247. { 128, 5 },
  248. { 128, 5 },
  249. { 128, 5 },
  250. { 128, 5 },
  251. { 128, 5 },
  252. { 128, 5 },
  253. { 128, 5 },
  254. { 8, 5 },
  255. { 8, 5 },
  256. { 8, 5 },
  257. { 8, 5 },
  258. { 8, 5 },
  259. { 8, 5 },
  260. { 8, 5 },
  261. { 8, 5 },
  262. { 9, 5 },
  263. { 9, 5 },
  264. { 9, 5 },
  265. { 9, 5 },
  266. { 9, 5 },
  267. { 9, 5 },
  268. { 9, 5 },
  269. { 9, 5 },
  270. { 16, 6 },
  271. { 16, 6 },
  272. { 16, 6 },
  273. { 16, 6 },
  274. { 17, 6 },
  275. { 17, 6 },
  276. { 17, 6 },
  277. { 17, 6 },
  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. { 4, 4 },
  291. { 4, 4 },
  292. { 4, 4 },
  293. { 4, 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. { 5, 4 },
  307. { 5, 4 },
  308. { 5, 4 },
  309. { 5, 4 },
  310. { 14, 6 },
  311. { 14, 6 },
  312. { 14, 6 },
  313. { 14, 6 },
  314. { 15, 6 },
  315. { 15, 6 },
  316. { 15, 6 },
  317. { 15, 6 },
  318. { 64, 5 },
  319. { 64, 5 },
  320. { 64, 5 },
  321. { 64, 5 },
  322. { 64, 5 },
  323. { 64, 5 },
  324. { 64, 5 },
  325. { 64, 5 },
  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. { 6, 4 },
  339. { 6, 4 },
  340. { 6, 4 },
  341. { 6, 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. { 7, 4 },
  355. { 7, 4 },
  356. { 7, 4 },
  357. { 7, 4 },
  358. { -2, 3 },
  359. { -2, 3 },
  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. { -1, 0 },
  370. { -1, 0 },
  371. { -1, 0 },
  372. { -1, 0 },
  373. { -3, 4 },
  374. { 1792, 3 },
  375. { 1792, 3 },
  376. { 1984, 4 },
  377. { 2048, 4 },
  378. { 2112, 4 },
  379. { 2176, 4 },
  380. { 2240, 4 },
  381. { 2304, 4 },
  382. { 1856, 3 },
  383. { 1856, 3 },
  384. { 1920, 3 },
  385. { 1920, 3 },
  386. { 2368, 4 },
  387. { 2432, 4 },
  388. { 2496, 4 },
  389. { 2560, 4 },
  390. { 1472, 1 },
  391. { 1536, 1 },
  392. { 1600, 1 },
  393. { 1728, 1 },
  394. { 704, 1 },
  395. { 768, 1 },
  396. { 832, 1 },
  397. { 896, 1 },
  398. { 960, 1 },
  399. { 1024, 1 },
  400. { 1088, 1 },
  401. { 1152, 1 },
  402. { 1216, 1 },
  403. { 1280, 1 },
  404. { 1344, 1 },
  405. { 1408, 1 }
  406. };
  407.  
  408. /* black decode table (runlength huffman codes) */
  409. const mmr_table_node jbig2_mmr_black_decode[] = {
  410. { 128, 12 },
  411. { 160, 13 },
  412. { 224, 12 },
  413. { 256, 12 },
  414. { 10, 7 },
  415. { 11, 7 },
  416. { 288, 12 },
  417. { 12, 7 },
  418. { 9, 6 },
  419. { 9, 6 },
  420. { 8, 6 },
  421. { 8, 6 },
  422. { 7, 5 },
  423. { 7, 5 },
  424. { 7, 5 },
  425. { 7, 5 },
  426. { 6, 4 },
  427. { 6, 4 },
  428. { 6, 4 },
  429. { 6, 4 },
  430. { 6, 4 },
  431. { 6, 4 },
  432. { 6, 4 },
  433. { 6, 4 },
  434. { 5, 4 },
  435. { 5, 4 },
  436. { 5, 4 },
  437. { 5, 4 },
  438. { 5, 4 },
  439. { 5, 4 },
  440. { 5, 4 },
  441. { 5, 4 },
  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. { 1, 3 },
  454. { 1, 3 },
  455. { 1, 3 },
  456. { 1, 3 },
  457. { 1, 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. { 4, 3 },
  470. { 4, 3 },
  471. { 4, 3 },
  472. { 4, 3 },
  473. { 4, 3 },
  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. { 3, 2 },
  502. { 3, 2 },
  503. { 3, 2 },
  504. { 3, 2 },
  505. { 3, 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, 2 },
  534. { 2, 2 },
  535. { 2, 2 },
  536. { 2, 2 },
  537. { 2, 2 },
  538. { -2, 4 },
  539. { -2, 4 },
  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. { -1, 0 },
  549. { -1, 0 },
  550. { -1, 0 },
  551. { -1, 0 },
  552. { -1, 0 },
  553. { -3, 5 },
  554. { 1792, 4 },
  555. { 1792, 4 },
  556. { 1984, 5 },
  557. { 2048, 5 },
  558. { 2112, 5 },
  559. { 2176, 5 },
  560. { 2240, 5 },
  561. { 2304, 5 },
  562. { 1856, 4 },
  563. { 1856, 4 },
  564. { 1920, 4 },
  565. { 1920, 4 },
  566. { 2368, 5 },
  567. { 2432, 5 },
  568. { 2496, 5 },
  569. { 2560, 5 },
  570. { 18, 3 },
  571. { 18, 3 },
  572. { 18, 3 },
  573. { 18, 3 },
  574. { 18, 3 },
  575. { 18, 3 },
  576. { 18, 3 },
  577. { 18, 3 },
  578. { 52, 5 },
  579. { 52, 5 },
  580. { 640, 6 },
  581. { 704, 6 },
  582. { 768, 6 },
  583. { 832, 6 },
  584. { 55, 5 },
  585. { 55, 5 },
  586. { 56, 5 },
  587. { 56, 5 },
  588. { 1280, 6 },
  589. { 1344, 6 },
  590. { 1408, 6 },
  591. { 1472, 6 },
  592. { 59, 5 },
  593. { 59, 5 },
  594. { 60, 5 },
  595. { 60, 5 },
  596. { 1536, 6 },
  597. { 1600, 6 },
  598. { 24, 4 },
  599. { 24, 4 },
  600. { 24, 4 },
  601. { 24, 4 },
  602. { 25, 4 },
  603. { 25, 4 },
  604. { 25, 4 },
  605. { 25, 4 },
  606. { 1664, 6 },
  607. { 1728, 6 },
  608. { 320, 5 },
  609. { 320, 5 },
  610. { 384, 5 },
  611. { 384, 5 },
  612. { 448, 5 },
  613. { 448, 5 },
  614. { 512, 6 },
  615. { 576, 6 },
  616. { 53, 5 },
  617. { 53, 5 },
  618. { 54, 5 },
  619. { 54, 5 },
  620. { 896, 6 },
  621. { 960, 6 },
  622. { 1024, 6 },
  623. { 1088, 6 },
  624. { 1152, 6 },
  625. { 1216, 6 },
  626. { 64, 3 },
  627. { 64, 3 },
  628. { 64, 3 },
  629. { 64, 3 },
  630. { 64, 3 },
  631. { 64, 3 },
  632. { 64, 3 },
  633. { 64, 3 },
  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. { 13, 1 },
  646. { 13, 1 },
  647. { 13, 1 },
  648. { 13, 1 },
  649. { 13, 1 },
  650. { 23, 4 },
  651. { 23, 4 },
  652. { 50, 5 },
  653. { 51, 5 },
  654. { 44, 5 },
  655. { 45, 5 },
  656. { 46, 5 },
  657. { 47, 5 },
  658. { 57, 5 },
  659. { 58, 5 },
  660. { 61, 5 },
  661. { 256, 5 },
  662. { 16, 3 },
  663. { 16, 3 },
  664. { 16, 3 },
  665. { 16, 3 },
  666. { 17, 3 },
  667. { 17, 3 },
  668. { 17, 3 },
  669. { 17, 3 },
  670. { 48, 5 },
  671. { 49, 5 },
  672. { 62, 5 },
  673. { 63, 5 },
  674. { 30, 5 },
  675. { 31, 5 },
  676. { 32, 5 },
  677. { 33, 5 },
  678. { 40, 5 },
  679. { 41, 5 },
  680. { 22, 4 },
  681. { 22, 4 },
  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. { 14, 1 },
  694. { 14, 1 },
  695. { 14, 1 },
  696. { 14, 1 },
  697. { 14, 1 },
  698. { 15, 2 },
  699. { 15, 2 },
  700. { 15, 2 },
  701. { 15, 2 },
  702. { 15, 2 },
  703. { 15, 2 },
  704. { 15, 2 },
  705. { 15, 2 },
  706. { 128, 5 },
  707. { 192, 5 },
  708. { 26, 5 },
  709. { 27, 5 },
  710. { 28, 5 },
  711. { 29, 5 },
  712. { 19, 4 },
  713. { 19, 4 },
  714. { 20, 4 },
  715. { 20, 4 },
  716. { 34, 5 },
  717. { 35, 5 },
  718. { 36, 5 },
  719. { 37, 5 },
  720. { 38, 5 },
  721. { 39, 5 },
  722. { 21, 4 },
  723. { 21, 4 },
  724. { 42, 5 },
  725. { 43, 5 },
  726. { 0, 3 },
  727. { 0, 3 },
  728. { 0, 3 },
  729. { 0, 3 }
  730. };
  731.  
  732. #define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
  733.  
  734. static int
  735. jbig2_find_changing_element(const byte *line, int x, int w)
  736. {
  737.         int a, b;
  738.  
  739.         if (line == 0)
  740.                 return w;
  741.  
  742.         if (x == -1) {
  743.                 a = 0;
  744.                 x = 0;
  745.         }
  746.         else {
  747.                 a = getbit(line, x);
  748.                 x ++;
  749.         }
  750.  
  751.         while (x < w) {
  752.                 b = getbit(line, x);
  753.                 if (a != b)
  754.                         break;
  755.                 x++;
  756.         }
  757.  
  758.         return x;
  759. }
  760.  
  761. static int
  762. jbig2_find_changing_element_of_color(const byte *line, int x, int w, int color)
  763. {
  764.         if (line == 0)
  765.                 return w;
  766.         x = jbig2_find_changing_element(line, x, w);
  767.         if (x < w && getbit(line, x) != color)
  768.                 x = jbig2_find_changing_element(line, x, w);
  769.         return x;
  770. }
  771.  
  772. static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
  773. static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
  774.  
  775. static void
  776. jbig2_set_bits(byte *line, int x0, int x1)
  777. {
  778.         int a0, a1, b0, b1, a;
  779.  
  780.         a0 = x0 >> 3;
  781.         a1 = x1 >> 3;
  782.  
  783.         b0 = x0 & 7;
  784.         b1 = x1 & 7;
  785.  
  786.         if (a0 == a1) {
  787.                 line[a0] |= lm[b0] & rm[b1];
  788.         }
  789.         else {
  790.                 line[a0] |= lm[b0];
  791.                 for (a = a0 + 1; a < a1; a++)
  792.                         line[a] = 0xFF;
  793.                 line[a1] |= rm[b1];
  794.         }
  795. }
  796.  
  797.  
  798. static int
  799. jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
  800. {
  801.         uint32_t word = mmr->word;
  802.         int table_ix = word >> (32 - initial_bits);
  803.         int val = table[table_ix].val;
  804.         int n_bits = table[table_ix].n_bits;
  805.  
  806.         if (n_bits > initial_bits) {
  807.                 int mask = (1 << (32 - initial_bits)) - 1;
  808.                 table_ix = val + ((word & mask) >> (32 - n_bits));
  809.                 val = table[table_ix].val;
  810.                 n_bits = initial_bits + table[table_ix].n_bits;
  811.         }
  812.  
  813.         jbig2_decode_mmr_consume(mmr, n_bits);
  814.  
  815.         return val;
  816. }
  817.  
  818. static int
  819. jbig2_decode_get_run(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
  820. {
  821.         int result = 0;
  822.         int val;
  823.  
  824.         do {
  825.                 val = jbig2_decode_get_code(mmr, table, initial_bits);
  826.                 result += val;
  827.         } while (val >= 64);
  828.  
  829.         return result;
  830. }
  831.  
  832. static void
  833. jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst)
  834. {
  835.         int a0, a1, a2, b1, b2;
  836.         int c;
  837.  
  838.         a0 = -1;
  839.         c = 0;          /* 0 is white, black is 1 */
  840.  
  841.         while (1)
  842.         {
  843.                 uint32_t word = mmr->word;
  844.                 /* printf ("%08x\n", word); */
  845.  
  846.                 if (a0 >= mmr->width)
  847.                         break;
  848.  
  849.                 if ((word >> (32 - 3)) == 1)
  850.                 {
  851.                         int white_run, black_run;
  852.  
  853.                         jbig2_decode_mmr_consume(mmr, 3);
  854.  
  855.                         if (a0 == -1)
  856.                                 a0 = 0;
  857.  
  858.                         if (c == 0) {
  859.                                 white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
  860.                                 black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
  861.                                 a1 = a0 + white_run;
  862.                                 a2 = a1 + black_run;
  863.                                 if (a1 > mmr->width) a1 = mmr->width;
  864.                                 if (a2 > mmr->width) a2 = mmr->width;
  865.                                 jbig2_set_bits(dst, a1, a2);
  866.                                 a0 = a2;
  867.                                 /* printf ("H %d %d\n", white_run, black_run); */
  868.                         }
  869.                         else
  870.                         {
  871.                                 black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
  872.                                 white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
  873.                                 a1 = a0 + black_run;
  874.                                 a2 = a1 + white_run;
  875.                                 if (a1 > mmr->width) a1 = mmr->width;
  876.                                 if (a2 > mmr->width) a2 = mmr->width;
  877.                                 jbig2_set_bits(dst, a0, a1);
  878.                                 a0 = a2;
  879.                                 /* printf ("H %d %d\n", black_run, white_run); */
  880.                         }
  881.                 }
  882.  
  883.                 else if ((word >> (32 - 4)) == 1)
  884.                 {
  885.                         /* printf ("P\n"); */
  886.                         jbig2_decode_mmr_consume(mmr, 4);
  887.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  888.                         b2 = jbig2_find_changing_element(ref, b1, mmr->width);
  889.                         if (c) jbig2_set_bits(dst, a0, b2);
  890.                         a0 = b2;
  891.                 }
  892.  
  893.                 else if ((word >> (32 - 1)) == 1)
  894.                 {
  895.                         /* printf ("V(0)\n"); */
  896.                         jbig2_decode_mmr_consume(mmr, 1);
  897.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  898.                         if (c) jbig2_set_bits(dst, a0, b1);
  899.                         a0 = b1;
  900.                         c = !c;
  901.                 }
  902.  
  903.                 else if ((word >> (32 - 3)) == 3)
  904.                 {
  905.                         /* printf ("VR(1)\n"); */
  906.                         jbig2_decode_mmr_consume(mmr, 3);
  907.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  908.                         if (b1 + 1 > mmr->width) break;
  909.                         if (c) jbig2_set_bits(dst, a0, b1 + 1);
  910.                         a0 = b1 + 1;
  911.                         c = !c;
  912.                 }
  913.  
  914.                 else if ((word >> (32 - 6)) == 3)
  915.                 {
  916.                         /* printf ("VR(2)\n"); */
  917.                         jbig2_decode_mmr_consume(mmr, 6);
  918.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  919.                         if (b1 + 2 > mmr->width) break;
  920.                         if (c) jbig2_set_bits(dst, a0, b1 + 2);
  921.                         a0 = b1 + 2;
  922.                         c = !c;
  923.                 }
  924.  
  925.                 else if ((word >> (32 - 7)) == 3)
  926.                 {
  927.                         /* printf ("VR(3)\n"); */
  928.                         jbig2_decode_mmr_consume(mmr, 7);
  929.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  930.                         if (b1 + 3 > mmr->width) break;
  931.                         if (c) jbig2_set_bits(dst, a0, b1 + 3);
  932.                         a0 = b1 + 3;
  933.                         c = !c;
  934.                 }
  935.  
  936.                 else if ((word >> (32 - 3)) == 2)
  937.                 {
  938.                         /* printf ("VL(1)\n"); */
  939.                         jbig2_decode_mmr_consume(mmr, 3);
  940.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  941.                         if (b1 - 1 < 0) break;
  942.                         if (c) jbig2_set_bits(dst, a0, b1 - 1);
  943.                         a0 = b1 - 1;
  944.                         c = !c;
  945.                 }
  946.  
  947.                 else if ((word >> (32 - 6)) == 2)
  948.                 {
  949.                         /* printf ("VL(2)\n"); */
  950.                         jbig2_decode_mmr_consume(mmr, 6);
  951.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  952.                         if (b1 - 2 < 0) break;
  953.                         if (c) jbig2_set_bits(dst, a0, b1 - 2);
  954.                         a0 = b1 - 2;
  955.                         c = !c;
  956.                 }
  957.  
  958.                 else if ((word >> (32 - 7)) == 2)
  959.                 {
  960.                         /* printf ("VL(3)\n"); */
  961.                         jbig2_decode_mmr_consume(mmr, 7);
  962.                         b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
  963.                         if (b1 - 3 < 0) break;
  964.                         if (c) jbig2_set_bits(dst, a0, b1 - 3);
  965.                         a0 = b1 - 3;
  966.                         c = !c;
  967.                 }
  968.  
  969.                 else
  970.                         break;
  971.         }
  972. }
  973.  
  974. int
  975. jbig2_decode_generic_mmr(Jbig2Ctx *ctx,
  976.         Jbig2Segment *segment,
  977.         const Jbig2GenericRegionParams *params,
  978.         const byte *data, size_t size,
  979.         Jbig2Image *image)
  980. {
  981.         Jbig2MmrCtx mmr;
  982.         const int rowstride = image->stride;
  983.         byte *dst = image->data;
  984.         byte *ref = NULL;
  985.         int y;
  986.  
  987.         jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
  988.  
  989.         for (y = 0; y < image->height; y++) {
  990.                 memset(dst, 0, rowstride);
  991.                 jbig2_decode_mmr_line(&mmr, ref, dst);
  992.                 ref = dst;
  993.                 dst += rowstride;
  994.         }
  995.  
  996.         return 0;
  997. }
  998.  
  999.