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