1/* Copyright (C) 2001-2019 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
13 CA 94945, U.S.A., +1(415)492-9861, for further information.
14*/
15
16/*
17 jbig2dec
18*/
19
20/* JBIG2 Pattern Dictionary and Halftone Region decoding */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#include "os_types.h"
26
27#include <string.h> /* memset() */
28
29#include "jbig2.h"
30#include "jbig2_priv.h"
31#include "jbig2_arith.h"
32#include "jbig2_generic.h"
33#include "jbig2_image.h"
34#include "jbig2_halftone.h"
35#include "jbig2_mmr.h"
36#include "jbig2_page.h"
37#include "jbig2_segment.h"
38
39/**
40 * jbig2_hd_new: create a new dictionary from a collective bitmap
41 */
42static Jbig2PatternDict *
43jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params, Jbig2Image *image)
44{
45 Jbig2PatternDict *new;
46 const uint32_t N = params->GRAYMAX + 1;
47 const uint32_t HPW = params->HDPW;
48 const uint32_t HPH = params->HDPH;
49 int code;
50 uint32_t i;
51 int j;
52
53 /* allocate a new struct */
54 new = jbig2_new(ctx, Jbig2PatternDict, 1);
55 if (new != NULL) {
56 new->patterns = jbig2_new(ctx, Jbig2Image *, N);
57 if (new->patterns == NULL) {
58 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate pattern in collective bitmap dictionary");
59 jbig2_free(ctx->allocator, new);
60 return NULL;
61 }
62 new->n_patterns = N;
63 new->HPW = HPW;
64 new->HPH = HPH;
65
66 /* 6.7.5(4) - copy out the individual pattern images */
67 for (i = 0; i < N; i++) {
68 new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
69 if (new->patterns[i] == NULL) {
70 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate pattern element image");
71 for (j = 0; j < i; j++)
72 jbig2_free(ctx->allocator, new->patterns[j]);
73 jbig2_free(ctx->allocator, new);
74 return NULL;
75 }
76 /* compose with the REPLACE operator; the source
77 will be clipped to the destination, selecting the
78 proper sub image */
79 code = jbig2_image_compose(ctx, new->patterns[i], image, -i * (int32_t) HPW, 0, JBIG2_COMPOSE_REPLACE);
80 if (code < 0) {
81 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to compose image into collective bitmap dictionary");
82 for (j = 0; j < i; j++)
83 jbig2_free(ctx->allocator, new->patterns[j]);
84 jbig2_free(ctx->allocator, new);
85 return NULL;
86 }
87 }
88 } else {
89 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate collective bitmap dictionary");
90 }
91
92 return new;
93}
94
95/**
96 * jbig2_hd_release: release a pattern dictionary
97 */
98void
99jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict)
100{
101 int i;
102
103 if (dict == NULL)
104 return;
105 if (dict->patterns != NULL)
106 for (i = 0; i < dict->n_patterns; i++)
107 jbig2_image_release(ctx, dict->patterns[i]);
108 jbig2_free(ctx->allocator, dict->patterns);
109 jbig2_free(ctx->allocator, dict);
110}
111
112/**
113 * jbig2_decode_pattern_dict: decode pattern dictionary data
114 *
115 * @ctx: jbig2 decoder context
116 * @segment: jbig2 segment (header) structure
117 * @params: parameters from the pattern dictionary header
118 * @data: pointer to text region data to be decoded
119 * @size: length of text region data
120 * @GB_stats: arithmetic coding context to use
121 *
122 * Implements the pattern dictionary decoding procedure
123 * described in section 6.7 of the JBIG2 spec.
124 *
125 * returns: a pointer to the resulting dictionary on success
126 * returns: 0 on failure
127 **/
128static Jbig2PatternDict *
129jbig2_decode_pattern_dict(Jbig2Ctx *ctx, Jbig2Segment *segment,
130 const Jbig2PatternDictParams *params, const byte *data, const size_t size, Jbig2ArithCx *GB_stats)
131{
132 Jbig2PatternDict *hd = NULL;
133 Jbig2Image *image = NULL;
134 Jbig2GenericRegionParams rparams;
135 int code = 0;
136
137 /* allocate the collective image */
138 image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH);
139 if (image == NULL) {
140 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dictionary");
141 return NULL;
142 }
143
144 /* fill out the generic region decoder parameters */
145 rparams.MMR = params->HDMMR;
146 rparams.GBTEMPLATE = params->HDTEMPLATE;
147 rparams.TPGDON = 0; /* not used if HDMMR = 1 */
148 rparams.USESKIP = 0;
149 rparams.gbat[0] = -(int8_t) params->HDPW;
150 rparams.gbat[1] = 0;
151 rparams.gbat[2] = -3;
152 rparams.gbat[3] = -1;
153 rparams.gbat[4] = 2;
154 rparams.gbat[5] = -2;
155 rparams.gbat[6] = -2;
156 rparams.gbat[7] = -2;
157
158 if (params->HDMMR) {
159 code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image);
160 } else {
161 Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
162
163 if (ws != NULL) {
164 Jbig2ArithState *as = jbig2_arith_new(ctx, ws);
165
166 if (as != NULL) {
167 code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats);
168 } else {
169 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling halftone dictionary");
170 }
171
172 jbig2_free(ctx->allocator, as);
173 jbig2_word_stream_buf_free(ctx, ws);
174 } else {
175 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling halftone dictionary");
176 }
177 }
178
179 if (code == 0)
180 hd = jbig2_hd_new(ctx, params, image);
181 else
182 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode immediate generic region");
183 jbig2_image_release(ctx, image);
184
185 return hd;
186}
187
188/* 7.4.4 */
189int
190jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
191{
192 Jbig2PatternDictParams params;
193 Jbig2ArithCx *GB_stats = NULL;
194 byte flags;
195 int offset = 0;
196
197 /* 7.4.4.1 - Data header */
198 if (segment->data_length < 7) {
199 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
200 }
201 flags = segment_data[0];
202 params.HDMMR = flags & 1;
203 params.HDTEMPLATE = (flags & 6) >> 1;
204 params.HDPW = segment_data[1];
205 params.HDPH = segment_data[2];
206 params.GRAYMAX = jbig2_get_uint32(segment_data + 3);
207 offset += 7;
208
209 jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
210 "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH);
211
212 if (params.HDMMR && params.HDTEMPLATE) {
213 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR);
214 }
215 if (flags & 0xf8) {
216 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved flag bits non-zero");
217 }
218
219 /* 7.4.4.2 */
220 if (!params.HDMMR) {
221 /* allocate and zero arithmetic coding stats */
222 int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
223
224 GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
225 if (GB_stats == NULL)
226 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when handling pattern dictionary");
227 memset(GB_stats, 0, stats_size);
228 }
229
230 segment->result = jbig2_decode_pattern_dict(ctx, segment, &params, segment_data + offset, segment->data_length - offset, GB_stats);
231
232 /* todo: retain GB_stats? */
233 if (!params.HDMMR) {
234 jbig2_free(ctx->allocator, GB_stats);
235 }
236
237 return (segment->result != NULL) ? 0 : -1;
238}
239
240/**
241 * jbig2_decode_gray_scale_image: decode gray-scale image
242 *
243 * @ctx: jbig2 decoder context
244 * @segment: jbig2 segment (header) structure
245 * @data: pointer to text region data to be decoded
246 * @size: length of text region data
247 * @GSMMR: if MMR is used
248 * @GSW: width of gray-scale image
249 * @GSH: height of gray-scale image
250 * @GSBPP: number of bitplanes/Jbig2Images to use
251 * @GSKIP: mask indicating which values should be skipped
252 * @GSTEMPLATE: template used to code the gray-scale bitplanes
253 * @GB_stats: arithmetic coding context to use
254 *
255 * Implements the decoding a gray-scale image described in
256 * annex C.5. This is part of the halftone region decoding.
257 *
258 * returns: array of gray-scale values with GSW x GSH width/height
259 * 0 on failure
260 **/
261static uint16_t **
262jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment,
263 const byte *data, const size_t size,
264 bool GSMMR, uint32_t GSW, uint32_t GSH,
265 uint32_t GSBPP, bool GSUSESKIP, Jbig2Image *GSKIP, int GSTEMPLATE, Jbig2ArithCx *GB_stats)
266{
267 uint16_t **GSVALS = NULL;
268 size_t consumed_bytes = 0;
269 uint32_t i, j, stride, x, y;
270 int code;
271 Jbig2Image **GSPLANES;
272 Jbig2GenericRegionParams rparams;
273 Jbig2WordStream *ws = NULL;
274 Jbig2ArithState *as = NULL;
275
276 /* allocate GSPLANES */
277 GSPLANES = jbig2_new(ctx, Jbig2Image *, GSBPP);
278 if (GSPLANES == NULL) {
279 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %d bytes for GSPLANES", GSBPP);
280 return NULL;
281 }
282
283 for (i = 0; i < GSBPP; ++i) {
284 GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
285 if (GSPLANES[i] == NULL) {
286 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
287 /* free already allocated */
288 for (j = i; j > 0;)
289 jbig2_image_release(ctx, GSPLANES[--j]);
290 jbig2_free(ctx->allocator, GSPLANES);
291 return NULL;
292 }
293 }
294
295 /* C.5 step 1. Decode GSPLANES[GSBPP-1] */
296 /* fill generic region decoder parameters */
297 rparams.MMR = GSMMR;
298 rparams.GBTEMPLATE = GSTEMPLATE;
299 rparams.TPGDON = 0;
300 rparams.USESKIP = GSUSESKIP;
301 rparams.SKIP = GSKIP;
302 rparams.gbat[0] = (GSTEMPLATE <= 1 ? 3 : 2);
303 rparams.gbat[1] = -1;
304 rparams.gbat[2] = -3;
305 rparams.gbat[3] = -1;
306 rparams.gbat[4] = 2;
307 rparams.gbat[5] = -2;
308 rparams.gbat[6] = -2;
309 rparams.gbat[7] = -2;
310
311 if (GSMMR) {
312 code = jbig2_decode_halftone_mmr(ctx, &rparams, data, size, GSPLANES[GSBPP - 1], &consumed_bytes);
313 } else {
314 ws = jbig2_word_stream_buf_new(ctx, data, size);
315 if (ws == NULL) {
316 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when decoding gray scale image");
317 goto cleanup;
318 }
319
320 as = jbig2_arith_new(ctx, ws);
321 if (as == NULL) {
322 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding state when decoding gray scale image");
323 goto cleanup;
324 }
325
326 code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[GSBPP - 1], GB_stats);
327 }
328 if (code < 0) {
329 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error decoding GSPLANES for halftone image");
330 goto cleanup;
331 }
332
333 /* C.5 step 2. Set j = GSBPP-2 */
334 j = GSBPP - 1;
335 /* C.5 step 3. decode loop */
336 while (j > 0) {
337 j--;
338 /* C.5 step 3. (a) */
339 if (GSMMR) {
340 code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes);
341 } else {
342 code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[j], GB_stats);
343 }
344 if (code < 0) {
345 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode GSPLANES for halftone image");
346 goto cleanup;
347 }
348
349 /* C.5 step 3. (b):
350 * for each [x,y]
351 * GSPLANES[j][x][y] = GSPLANES[j+1][x][y] XOR GSPLANES[j][x][y] */
352 stride = GSPLANES[j]->stride;
353 for (i = 0; i < stride * GSH; ++i)
354 GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i];
355
356 /* C.5 step 3. (c) */
357 }
358
359 /* allocate GSVALS */
360 GSVALS = jbig2_new(ctx, uint16_t *, GSW);
361 if (GSVALS == NULL) {
362 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSW);
363 goto cleanup;
364 }
365 for (i = 0; i < GSW; ++i) {
366 GSVALS[i] = jbig2_new(ctx, uint16_t, GSH);
367 if (GSVALS[i] == NULL) {
368 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW);
369 /* free already allocated */
370 for (j = i; j > 0;)
371 jbig2_free(ctx->allocator, GSVALS[--j]);
372 jbig2_free(ctx->allocator, GSVALS);
373 GSVALS = NULL;
374 goto cleanup;
375 }
376 }
377
378 /* C.5 step 4. */
379 for (x = 0; x < GSW; ++x) {
380 for (y = 0; y < GSH; ++y) {
381 GSVALS[x][y] = 0;
382
383 for (j = 0; j < GSBPP; ++j)
384 GSVALS[x][y] += jbig2_image_get_pixel(GSPLANES[j], x, y) << j;
385 }
386 }
387
388cleanup:
389 /* free memory */
390 if (!GSMMR) {
391 jbig2_free(ctx->allocator, as);
392 jbig2_word_stream_buf_free(ctx, ws);
393 }
394 for (i = 0; i < GSBPP; ++i)
395 jbig2_image_release(ctx, GSPLANES[i]);
396
397 jbig2_free(ctx->allocator, GSPLANES);
398
399 return GSVALS;
400}
401
402/**
403 * jbig2_decode_ht_region_get_hpats: get pattern dictionary
404 *
405 * @ctx: jbig2 decoder context
406 * @segment: jbig2 halftone region segment
407 *
408 * Returns the first referred pattern dictionary of segment
409 *
410 * returns: pattern dictionary
411 * 0 if search failed
412 **/
413static Jbig2PatternDict *
414jbig2_decode_ht_region_get_hpats(Jbig2Ctx *ctx, Jbig2Segment *segment)
415{
416 int index = 0;
417 Jbig2PatternDict *pattern_dict = NULL;
418 Jbig2Segment *rsegment = NULL;
419
420 /* loop through all referred segments */
421 while (!pattern_dict && segment->referred_to_segment_count > index) {
422 rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
423 if (rsegment) {
424 /* segment type is pattern dictionary and result is not empty */
425 if ((rsegment->flags & 0x3f) == 16 && rsegment->result) {
426 pattern_dict = (Jbig2PatternDict *) rsegment->result;
427 return pattern_dict;
428 }
429 }
430 index++;
431 }
432 return pattern_dict;
433}
434
435/**
436 * jbig2_decode_halftone_region: decode a halftone region
437 *
438 * @ctx: jbig2 decoder context
439 * @segment: jbig2 halftone region segment
440 * @params: parameters
441 * @data: pointer to halftone region data to be decoded
442 * @size: length of halftone region data
443 * @GB_stats: arithmetic coding context to use
444 *
445 * Implements the halftone region decoding procedure
446 * described in section 6.6.5 of the JBIG2 spec.
447 *
448 * returns: 0 on success
449 * <0 on failure
450 **/
451static int
452jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
453 Jbig2HalftoneRegionParams *params, const byte *data, const size_t size, Jbig2Image *image, Jbig2ArithCx *GB_stats)
454{
455 uint32_t HBPP;
456 uint32_t HNUMPATS;
457 uint16_t **GI = NULL;
458 Jbig2Image *HSKIP = NULL;
459 Jbig2PatternDict *HPATS;
460 uint32_t i;
461 uint32_t mg, ng;
462 int32_t x, y;
463 uint16_t gray_val;
464 int code = 0;
465
466 /* We need the patterns used in this region, get them from the referred pattern dictionary */
467 HPATS = jbig2_decode_ht_region_get_hpats(ctx, segment);
468 if (!HPATS) {
469 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no pattern dictionary found, skipping halftone image");
470 goto cleanup;
471 }
472
473 /* 6.6.5 point 1. Fill bitmap with HDEFPIXEL */
474 memset(image->data, params->HDEFPIXEL, image->stride * image->height);
475
476 /* 6.6.5 point 2. compute HSKIP according to 6.6.5.1 */
477 if (params->HENABLESKIP == 1) {
478 HSKIP = jbig2_image_new(ctx, params->HGW, params->HGH);
479 if (HSKIP == NULL)
480 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate skip image");
481
482 for (mg = 0; mg < params->HGH; ++mg) {
483 for (ng = 0; ng < params->HGW; ++ng) {
484 x = (params->HGX + mg * (int32_t) params->HRY + ng * (int32_t) params->HRX) >> 8;
485 y = (params->HGY + mg * (int32_t) params->HRX - ng * (int32_t) params->HRY) >> 8;
486
487 if (x + HPATS->HPW <= 0 || x >= (int32_t) image->width || y + HPATS->HPH <= 0 || y >= (int32_t) image->height) {
488 jbig2_image_set_pixel(HSKIP, ng, mg, 1);
489 } else {
490 jbig2_image_set_pixel(HSKIP, ng, mg, 0);
491 }
492 }
493 }
494 }
495
496 /* 6.6.5 point 3. set HBPP to ceil(log2(HNUMPATS)): */
497 HNUMPATS = HPATS->n_patterns;
498 HBPP = 0;
499 while (HNUMPATS > (1U << ++HBPP));
500 if (HBPP > 16) {
501 code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "HBPP is larger than supported (%u)", HBPP);
502 goto cleanup;
503 }
504
505 /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */
506 GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
507 params->HMMR, params->HGW, params->HGH, HBPP, params->HENABLESKIP, HSKIP, params->HTEMPLATE, GB_stats);
508 if (!GI) {
509 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to acquire gray-scale image, skipping halftone image");
510 goto cleanup;
511 }
512
513 /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
514 for (mg = 0; mg < params->HGH; ++mg) {
515 for (ng = 0; ng < params->HGW; ++ng) {
516 x = (params->HGX + mg * (int32_t) params->HRY + ng * (int32_t) params->HRX) >> 8;
517 y = (params->HGY + mg * (int32_t) params->HRX - ng * (int32_t) params->HRY) >> 8;
518
519 /* prevent pattern index >= HNUMPATS */
520 gray_val = GI[ng][mg];
521 if (gray_val >= HNUMPATS) {
522 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "gray-scale index %d out of range, using largest index", gray_val);
523 /* use highest available pattern */
524 gray_val = HNUMPATS - 1;
525 }
526 code = jbig2_image_compose(ctx, image, HPATS->patterns[gray_val], x, y, params->HCOMBOP);
527 if (code < 0) {
528 code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose pattern with gray-scale image");
529 goto cleanup;
530 }
531 }
532 }
533
534cleanup:
535 if (GI) {
536 for (i = 0; i < params->HGW; ++i) {
537 jbig2_free(ctx->allocator, GI[i]);
538 }
539 }
540 jbig2_free(ctx->allocator, GI);
541 jbig2_image_release(ctx, HSKIP);
542
543 return code;
544}
545
546/**
547 * jbig2_halftone_region: read a halftone region segment header
548 **/
549int
550jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
551{
552 int offset = 0;
553 Jbig2RegionSegmentInfo region_info;
554 Jbig2HalftoneRegionParams params;
555 Jbig2Image *image = NULL;
556 Jbig2ArithCx *GB_stats = NULL;
557 int code = 0;
558
559 /* 7.4.5.1 */
560 if (segment->data_length < 17)
561 goto too_short;
562 jbig2_get_region_segment_info(&region_info, segment_data);
563 offset += 17;
564
565 if (segment->data_length < 18)
566 goto too_short;
567
568 /* 7.4.5.1.1 Figure 42 */
569 params.flags = segment_data[offset];
570 params.HMMR = params.flags & 1;
571 params.HTEMPLATE = (params.flags & 6) >> 1;
572 params.HENABLESKIP = (params.flags & 8) >> 3;
573 params.HCOMBOP = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
574 params.HDEFPIXEL = (params.flags & 0x80) >> 7;
575 offset += 1;
576
577 jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
578 "halftone region: %u x %u @ (%u, %u), flags = %02x", region_info.width, region_info.height, region_info.x, region_info.y, params.flags);
579
580 if (params.HMMR && params.HTEMPLATE) {
581 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HTEMPLATE is %d when HMMR is %d, contrary to spec", params.HTEMPLATE, params.HMMR);
582 }
583 if (params.HMMR && params.HENABLESKIP) {
584 jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HENABLESKIP is %d when HMMR is %d, contrary to spec", params.HENABLESKIP, params.HMMR);
585 }
586
587 /* 7.4.5.1.2 Figure 43 */
588 if (segment->data_length - offset < 16)
589 goto too_short;
590 params.HGW = jbig2_get_uint32(segment_data + offset);
591 params.HGH = jbig2_get_uint32(segment_data + offset + 4);
592 params.HGX = jbig2_get_int32(segment_data + offset + 8);
593 params.HGY = jbig2_get_int32(segment_data + offset + 12);
594 offset += 16;
595
596 /* 7.4.5.1.3 Figure 44 */
597 if (segment->data_length - offset < 4)
598 goto too_short;
599 params.HRX = jbig2_get_uint16(segment_data + offset);
600 params.HRY = jbig2_get_uint16(segment_data + offset + 2);
601 offset += 4;
602
603 jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
604 "grid %d x %d @ (%d.%d,%d.%d) vector (%d.%d,%d.%d)",
605 params.HGW, params.HGH,
606 params.HGX >> 8, params.HGX & 0xff,
607 params.HGY >> 8, params.HGY & 0xff,
608 params.HRX >> 8, params.HRX & 0xff,
609 params.HRY >> 8, params.HRY & 0xff);
610
611 /* 7.4.5.2 */
612 if (!params.HMMR) {
613 /* allocate and zero arithmetic coding stats */
614 int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
615
616 GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
617 if (GB_stats == NULL) {
618 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate arithmetic decoder states in halftone region");
619 }
620 memset(GB_stats, 0, stats_size);
621 }
622
623 image = jbig2_image_new(ctx, region_info.width, region_info.height);
624 if (image == NULL) {
625 jbig2_free(ctx->allocator, GB_stats);
626 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate halftone image");
627 }
628
629 code = jbig2_decode_halftone_region(ctx, segment, &params, segment_data + offset, segment->data_length - offset, image, GB_stats);
630 if (code < 0) {
631 jbig2_image_release(ctx, image);
632 jbig2_free(ctx->allocator, GB_stats);
633 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode halftone region");
634 }
635
636 /* todo: retain GB_stats? */
637 if (!params.HMMR) {
638 jbig2_free(ctx->allocator, GB_stats);
639 }
640
641 code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
642 if (code < 0) {
643 jbig2_image_release(ctx, image);
644 return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add halftone region to page");
645 }
646
647 jbig2_image_release(ctx, image);
648
649 return code;
650
651too_short:
652 return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
653}
654