jbig2_refinement.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 Refinement 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. #include <stdio.h>
  25. #include "jbig2.h"
  26. #include "jbig2_priv.h"
  27. #include "jbig2_arith.h"
  28. #include "jbig2_generic.h"
  29. #include "jbig2_image.h"
  30. #include "jbig2_page.h"
  31. #include "jbig2_refinement.h"
  32. #include "jbig2_segment.h"
  33. #define pixel_outside_field(x, y) \
  34. ((y) < -128 || (y) > 0 || (x) < -128 || ((y) < 0 && (x) > 127) || ((y) == 0 && (x) >= 0))
  35. #define refpixel_outside_field(x, y) \
  36. ((y) < -128 || (y) > 127 || (x) < -128 || (x) > 127)
  37. static int
  38. jbig2_decode_refinement_template0_unopt(Jbig2Ctx *ctx,
  39. Jbig2Segment *segment,
  40. const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
  41. {
  42. const int GRW = image->width;
  43. const int GRH = image->height;
  44. Jbig2Image *ref = params->GRREFERENCE;
  45. const int dx = params->GRREFERENCEDX;
  46. const int dy = params->GRREFERENCEDY;
  47. uint32_t CONTEXT;
  48. int x, y;
  49. int bit;
  50. if (pixel_outside_field(params->grat[0], params->grat[1]) ||
  51. refpixel_outside_field(params->grat[2], params->grat[3]))
  52. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  53. "adaptive template pixel is out of field");
  54. for (y = 0; y < GRH; y++) {
  55. for (x = 0; x < GRW; x++) {
  56. CONTEXT = 0;
  57. CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  58. CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  59. CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  60. CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
  61. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  62. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  63. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
  64. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
  65. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
  66. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
  67. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
  68. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
  69. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
  70. bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  71. if (bit < 0)
  72. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
  73. jbig2_image_set_pixel(image, x, y, bit);
  74. }
  75. }
  76. #ifdef JBIG2_DEBUG_DUMP
  77. {
  78. static count = 0;
  79. char name[32];
  80. int code;
  81. snprintf(name, 32, "refin-%d.pbm", count);
  82. code = jbig2_image_write_pbm_file(ref, name);
  83. if (code < 0)
  84. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement input");
  85. snprintf(name, 32, "refout-%d.pbm", count);
  86. code = jbig2_image_write_pbm_file(image, name);
  87. if (code < 0)
  88. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed write refinement output");
  89. count++;
  90. }
  91. #endif
  92. return 0;
  93. }
  94. static int
  95. jbig2_decode_refinement_template1_unopt(Jbig2Ctx *ctx,
  96. Jbig2Segment *segment,
  97. const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
  98. {
  99. const int GRW = image->width;
  100. const int GRH = image->height;
  101. Jbig2Image *ref = params->GRREFERENCE;
  102. const int dx = params->GRREFERENCEDX;
  103. const int dy = params->GRREFERENCEDY;
  104. uint32_t CONTEXT;
  105. int x, y;
  106. int bit;
  107. for (y = 0; y < GRH; y++) {
  108. for (x = 0; x < GRW; x++) {
  109. CONTEXT = 0;
  110. CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  111. CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  112. CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  113. CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  114. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  115. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  116. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  117. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  118. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  119. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  120. bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  121. if (bit < 0)
  122. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template0");
  123. jbig2_image_set_pixel(image, x, y, bit);
  124. }
  125. }
  126. #ifdef JBIG2_DEBUG_DUMP
  127. {
  128. static count = 0;
  129. char name[32];
  130. snprintf(name, 32, "refin-%d.pbm", count);
  131. code = jbig2_image_write_pbm_file(ref, name);
  132. if (code < 0)
  133. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement input");
  134. snprintf(name, 32, "refout-%d.pbm", count);
  135. code = jbig2_image_write_pbm_file(image, name);
  136. if (code < 0)
  137. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write refinement output");
  138. count++;
  139. }
  140. #endif
  141. return 0;
  142. }
  143. #if 0 /* currently not used */
  144. static int
  145. jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
  146. Jbig2Segment *segment,
  147. const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
  148. {
  149. const int GRW = image->width;
  150. const int GRH = image->height;
  151. const int stride = image->stride;
  152. const int refstride = params->reference->stride;
  153. const int dy = params->DY;
  154. byte *grreg_line = (byte *) image->data;
  155. byte *grref_line = (byte *) params->reference->data;
  156. int x, y;
  157. for (y = 0; y < GRH; y++) {
  158. const int padded_width = (GRW + 7) & -8;
  159. uint32_t CONTEXT;
  160. uint32_t refline_m1; /* previous line of the reference bitmap */
  161. uint32_t refline_0; /* current line of the reference bitmap */
  162. uint32_t refline_1; /* next line of the reference bitmap */
  163. uint32_t line_m1; /* previous line of the decoded bitmap */
  164. line_m1 = (y >= 1) ? grreg_line[-stride] : 0;
  165. refline_m1 = ((y - dy) >= 1) ? grref_line[(-1 - dy) * stride] << 2 : 0;
  166. refline_0 = (((y - dy) > 0) && ((y - dy) < GRH)) ? grref_line[(0 - dy) * stride] << 4 : 0;
  167. refline_1 = (y < GRH - 1) ? grref_line[(+1 - dy) * stride] << 7 : 0;
  168. CONTEXT = ((line_m1 >> 5) & 0x00e) | ((refline_1 >> 5) & 0x030) | ((refline_0 >> 5) & 0x1c0) | ((refline_m1 >> 5) & 0x200);
  169. for (x = 0; x < padded_width; x += 8) {
  170. byte result = 0;
  171. int x_minor;
  172. const int minor_width = GRW - x > 8 ? 8 : GRW - x;
  173. if (y >= 1) {
  174. line_m1 = (line_m1 << 8) | (x + 8 < GRW ? grreg_line[-stride + (x >> 3) + 1] : 0);
  175. refline_m1 = (refline_m1 << 8) | (x + 8 < GRW ? grref_line[-refstride + (x >> 3) + 1] << 2 : 0);
  176. }
  177. refline_0 = (refline_0 << 8) | (x + 8 < GRW ? grref_line[(x >> 3) + 1] << 4 : 0);
  178. if (y < GRH - 1)
  179. refline_1 = (refline_1 << 8) | (x + 8 < GRW ? grref_line[+refstride + (x >> 3) + 1] << 7 : 0);
  180. else
  181. refline_1 = 0;
  182. /* this is the speed critical inner-loop */
  183. for (x_minor = 0; x_minor < minor_width; x_minor++) {
  184. int bit;
  185. bit = jbig2_arith_decode(ctx, as, &GR_stats[CONTEXT]);
  186. if (bit < 0)
  187. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode arithmetic code when handling refinement template1");
  188. result |= bit << (7 - x_minor);
  189. CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
  190. ((line_m1 >> (9 - x_minor)) & 0x002) |
  191. ((refline_1 >> (9 - x_minor)) & 0x010) | ((refline_0 >> (9 - x_minor)) & 0x040) | ((refline_m1 >> (9 - x_minor)) & 0x200);
  192. }
  193. grreg_line[x >> 3] = result;
  194. }
  195. grreg_line += stride;
  196. grref_line += refstride;
  197. }
  198. return 0;
  199. }
  200. #endif
  201. typedef uint32_t(*ContextBuilder)(const Jbig2RefinementRegionParams *, Jbig2Image *, int, int);
  202. static int
  203. implicit_value(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
  204. {
  205. Jbig2Image *ref = params->GRREFERENCE;
  206. int i = x - params->GRREFERENCEDX;
  207. int j = y - params->GRREFERENCEDY;
  208. int m = jbig2_image_get_pixel(ref, i, j);
  209. return ((jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
  210. (jbig2_image_get_pixel(ref, i, j - 1) == m) &&
  211. (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
  212. (jbig2_image_get_pixel(ref, i - 1, j) == m) &&
  213. (jbig2_image_get_pixel(ref, i + 1, j) == m) &&
  214. (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) &&
  215. (jbig2_image_get_pixel(ref, i, j + 1) == m) &&
  216. (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
  217. )? m : -1;
  218. }
  219. static uint32_t
  220. mkctx0(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
  221. {
  222. Jbig2Image *ref = params->GRREFERENCE;
  223. const int dx = params->GRREFERENCEDX;
  224. const int dy = params->GRREFERENCEDY;
  225. uint32_t CONTEXT;
  226. CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  227. CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  228. CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  229. CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
  230. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  231. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  232. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
  233. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
  234. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
  235. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
  236. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
  237. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
  238. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
  239. return CONTEXT;
  240. }
  241. static uint32_t
  242. mkctx1(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
  243. {
  244. Jbig2Image *ref = params->GRREFERENCE;
  245. const int dx = params->GRREFERENCEDX;
  246. const int dy = params->GRREFERENCEDY;
  247. uint32_t CONTEXT;
  248. CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
  249. CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  250. CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  251. CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  252. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
  253. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
  254. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
  255. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
  256. CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
  257. CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
  258. return CONTEXT;
  259. }
  260. static int
  261. jbig2_decode_refinement_TPGRON(Jbig2Ctx *ctx, const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
  262. {
  263. const int GRW = image->width;
  264. const int GRH = image->height;
  265. int x, y, iv, LTP = 0;
  266. uint32_t start_context = (params->GRTEMPLATE ? 0x40 : 0x100);
  267. ContextBuilder mkctx = (params->GRTEMPLATE ? mkctx1 : mkctx0);
  268. if (params->GRTEMPLATE == 0 &&
  269. (pixel_outside_field(params->grat[0], params->grat[1]) ||
  270. refpixel_outside_field(params->grat[2], params->grat[3])))
  271. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER,
  272. "adaptive template pixel is out of field");
  273. for (y = 0; y < GRH; y++) {
  274. int bit = jbig2_arith_decode(ctx, as, &GR_stats[start_context]);
  275. if (bit < 0)
  276. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  277. LTP ^= bit;
  278. if (!LTP) {
  279. for (x = 0; x < GRW; x++) {
  280. bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  281. if (bit < 0)
  282. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  283. jbig2_image_set_pixel(image, x, y, bit);
  284. }
  285. } else {
  286. for (x = 0; x < GRW; x++) {
  287. iv = implicit_value(params, image, x, y);
  288. if (iv < 0) {
  289. int bit = jbig2_arith_decode(ctx, as, &GR_stats[mkctx(params, image, x, y)]);
  290. if (bit < 0)
  291. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode arithmetic code when handling refinement TPGRON1");
  292. jbig2_image_set_pixel(image, x, y, bit);
  293. } else
  294. jbig2_image_set_pixel(image, x, y, iv);
  295. }
  296. }
  297. }
  298. return 0;
  299. }
  300. /**
  301. * jbig2_decode_refinement_region: Decode a generic refinement region.
  302. * @ctx: The context for allocation and error reporting.
  303. * @segment: A segment reference for error reporting.
  304. * @params: Decoding parameter set.
  305. * @as: Arithmetic decoder state.
  306. * @image: Where to store the decoded image.
  307. * @GR_stats: Arithmetic stats.
  308. *
  309. * Decodes a generic refinement region, according to section 6.3.
  310. * an already allocated Jbig2Image object in @image for the result.
  311. *
  312. * Because this API is based on an arithmetic decoding state, it is
  313. * not suitable for MMR decoding.
  314. *
  315. * Return code: 0 on success.
  316. **/
  317. int
  318. jbig2_decode_refinement_region(Jbig2Ctx *ctx,
  319. Jbig2Segment *segment,
  320. const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
  321. {
  322. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  323. "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
  324. params->GRREFERENCEDX, params->GRREFERENCEDY, params->GRTEMPLATE, params->TPGRON);
  325. if (params->TPGRON)
  326. return jbig2_decode_refinement_TPGRON(ctx, params, as, image, GR_stats);
  327. if (params->GRTEMPLATE)
  328. return jbig2_decode_refinement_template1_unopt(ctx, segment, params, as, image, GR_stats);
  329. else
  330. return jbig2_decode_refinement_template0_unopt(ctx, segment, params, as, image, GR_stats);
  331. }
  332. /**
  333. * Find the first referred-to intermediate region segment
  334. * with a non-NULL result for use as a reference image
  335. */
  336. static Jbig2Segment *
  337. jbig2_region_find_referred(Jbig2Ctx *ctx, Jbig2Segment *segment)
  338. {
  339. const int nsegments = segment->referred_to_segment_count;
  340. Jbig2Segment *rsegment;
  341. int index;
  342. for (index = 0; index < nsegments; index++) {
  343. rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
  344. if (rsegment == NULL) {
  345. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find referred to segment %d", segment->referred_to_segments[index]);
  346. continue;
  347. }
  348. switch (rsegment->flags & 63) {
  349. case 4: /* intermediate text region */
  350. case 20: /* intermediate halftone region */
  351. case 36: /* intermediate generic region */
  352. case 40: /* intermediate generic refinement region */
  353. if (rsegment->result)
  354. return rsegment;
  355. break;
  356. default: /* keep looking */
  357. break;
  358. }
  359. }
  360. /* no appropriate reference was found. */
  361. return NULL;
  362. }
  363. /**
  364. * Handler for generic refinement region segments
  365. */
  366. int
  367. jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
  368. {
  369. Jbig2RefinementRegionParams params;
  370. Jbig2RegionSegmentInfo rsi;
  371. int offset = 0;
  372. byte seg_flags;
  373. int code = 0;
  374. /* 7.4.7 */
  375. if (segment->data_length < 18)
  376. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  377. jbig2_get_region_segment_info(&rsi, segment_data);
  378. 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);
  379. /* 7.4.7.2 */
  380. seg_flags = segment_data[17];
  381. params.GRTEMPLATE = seg_flags & 0x01;
  382. params.TPGRON = seg_flags & 0x02 ? 1 : 0;
  383. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  384. "segment flags = %02x %s%s", seg_flags, params.GRTEMPLATE ? " GRTEMPLATE" : "", params.TPGRON ? " TPGRON" : "");
  385. if (seg_flags & 0xFC)
  386. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved segment flag bits are non-zero");
  387. offset += 18;
  388. /* 7.4.7.3 */
  389. if (!params.GRTEMPLATE) {
  390. if (segment->data_length < 22)
  391. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
  392. params.grat[0] = segment_data[offset + 0];
  393. params.grat[1] = segment_data[offset + 1];
  394. params.grat[2] = segment_data[offset + 2];
  395. params.grat[3] = segment_data[offset + 3];
  396. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  397. "grat1: (%d, %d) grat2: (%d, %d)", params.grat[0], params.grat[1], params.grat[2], params.grat[3]);
  398. offset += 4;
  399. }
  400. /* 7.4.7.4 - set up the reference image */
  401. if (segment->referred_to_segment_count) {
  402. Jbig2Segment *ref;
  403. ref = jbig2_region_find_referred(ctx, segment);
  404. if (ref == NULL)
  405. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to find reference bitmap");
  406. if (ref->result == NULL)
  407. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference bitmap has no decoded image");
  408. /* the reference bitmap is the result of a previous
  409. intermediate region segment; the reference selection
  410. rules say to use the first one available, and not to
  411. reuse any intermediate result, so we simply take another
  412. reference to it and free the original to keep track of this. */
  413. params.GRREFERENCE = jbig2_image_reference(ctx, (Jbig2Image *) ref->result);
  414. jbig2_image_release(ctx, (Jbig2Image *) ref->result);
  415. ref->result = NULL;
  416. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "found reference bitmap in segment %d", ref->number);
  417. } else {
  418. /* the reference is just (a subset of) the page buffer */
  419. if (ctx->pages[ctx->current_page].image == NULL)
  420. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference page bitmap has no decoded image");
  421. params.GRREFERENCE = jbig2_image_reference(ctx, ctx->pages[ctx->current_page].image);
  422. /* TODO: subset the image if appropriate */
  423. }
  424. /* 7.4.7.5 */
  425. params.GRREFERENCEDX = 0;
  426. params.GRREFERENCEDY = 0;
  427. {
  428. Jbig2WordStream *ws = NULL;
  429. Jbig2ArithState *as = NULL;
  430. Jbig2ArithCx *GR_stats = NULL;
  431. int stats_size;
  432. Jbig2Image *image = NULL;
  433. image = jbig2_image_new(ctx, rsi.width, rsi.height);
  434. if (image == NULL) {
  435. code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate refinement image");
  436. goto cleanup;
  437. }
  438. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
  439. stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
  440. GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
  441. if (GR_stats == NULL) {
  442. code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder state for generic refinement regions");
  443. goto cleanup;
  444. }
  445. memset(GR_stats, 0, stats_size);
  446. ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
  447. if (ws == NULL) {
  448. code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling refinement region");
  449. goto cleanup;
  450. }
  451. as = jbig2_arith_new(ctx, ws);
  452. if (as == NULL) {
  453. code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling refinement region");
  454. goto cleanup;
  455. }
  456. code = jbig2_decode_refinement_region(ctx, segment, &params, as, image, GR_stats);
  457. if (code < 0) {
  458. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
  459. goto cleanup;
  460. }
  461. if ((segment->flags & 63) == 40) {
  462. /* intermediate region. save the result for later */
  463. segment->result = jbig2_image_reference(ctx, image);
  464. } else {
  465. /* immediate region. composite onto the page */
  466. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  467. "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
  468. code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
  469. if (code < 0) {
  470. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add refinement region to page");
  471. goto cleanup;
  472. }
  473. }
  474. cleanup:
  475. jbig2_image_release(ctx, image);
  476. jbig2_image_release(ctx, params.GRREFERENCE);
  477. jbig2_free(ctx->allocator, as);
  478. jbig2_word_stream_buf_free(ctx, ws);
  479. jbig2_free(ctx->allocator, GR_stats);
  480. }
  481. return code;
  482. }