jbig2_generic.c 63 KB

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