Coverage Report

Created: 2026-07-14 06:52

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