Coverage Report

Created: 2023-06-07 06:20

/src/mupdf/thirdparty/jbig2dec/jbig2_image.c
Line
Count
Source (jump to first uncovered line)
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
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
23
#include "os_types.h"
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>             /* memcpy() */
28
29
#include "jbig2.h"
30
#include "jbig2_priv.h"
31
#include "jbig2_image.h"
32
33
/* allocate a Jbig2Image structure and its associated bitmap */
34
Jbig2Image *
35
jbig2_image_new(Jbig2Ctx *ctx, uint32_t width, uint32_t height)
36
14.0M
{
37
14.0M
    Jbig2Image *image;
38
14.0M
    uint32_t stride;
39
40
14.0M
    if (width == 0 || height == 0) {
41
42
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to create zero sized image");
42
42
        return NULL;
43
42
    }
44
45
14.0M
    image = jbig2_new(ctx, Jbig2Image, 1);
46
14.0M
    if (image == NULL) {
47
0
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image");
48
0
        return NULL;
49
0
    }
50
51
14.0M
    stride = ((width - 1) >> 3) + 1;    /* generate a byte-aligned stride */
52
53
    /* check for integer multiplication overflow */
54
14.0M
    if (height > (INT32_MAX / stride)) {
55
44
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow (stride=%u, height=%u)", stride, height);
56
44
        jbig2_free(ctx->allocator, image);
57
44
        return NULL;
58
44
    }
59
14.0M
    image->data = jbig2_new(ctx, uint8_t, (size_t) height * stride);
60
14.0M
    if (image->data == NULL) {
61
15
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate image data buffer (stride=%u, height=%u)", stride, height);
62
15
        jbig2_free(ctx->allocator, image);
63
15
        return NULL;
64
15
    }
65
66
14.0M
    image->width = width;
67
14.0M
    image->height = height;
68
14.0M
    image->stride = stride;
69
14.0M
    image->refcount = 1;
70
71
14.0M
    return image;
72
14.0M
}
73
74
/* bump the reference count for an image pointer */
75
Jbig2Image *
76
jbig2_image_reference(Jbig2Ctx *ctx, Jbig2Image *image)
77
4.31M
{
78
4.31M
    if (image)
79
4.31M
        image->refcount++;
80
4.31M
    return image;
81
4.31M
}
82
83
/* release an image pointer, freeing it it appropriate */
84
void
85
jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image)
86
1.29G
{
87
1.29G
    if (image == NULL)
88
1.27G
        return;
89
18.3M
    image->refcount--;
90
18.3M
    if (image->refcount == 0)
91
14.0M
        jbig2_image_free(ctx, image);
92
18.3M
}
93
94
/* free a Jbig2Image structure and its associated memory */
95
void
96
jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
97
14.0M
{
98
14.0M
    if (image != NULL) {
99
14.0M
        jbig2_free(ctx->allocator, image->data);
100
14.0M
        jbig2_free(ctx->allocator, image);
101
14.0M
    }
102
14.0M
}
103
104
/* resize a Jbig2Image */
105
Jbig2Image *
106
jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, uint32_t width, uint32_t height, int value)
107
0
{
108
0
    if (width == image->width) {
109
0
        uint8_t *data;
110
111
        /* check for integer multiplication overflow */
112
0
        if (image->height > (INT32_MAX / image->stride)) {
113
0
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "integer multiplication overflow during resize (stride=%u, height=%u)", image->stride, height);
114
0
            return NULL;
115
0
        }
116
        /* use the same stride, just change the length */
117
0
        data = jbig2_renew(ctx, image->data, uint8_t, (size_t) height * image->stride);
118
0
        if (data == NULL) {
119
0
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to reallocate image");
120
0
            return NULL;
121
0
        }
122
0
        image->data = data;
123
0
        if (height > image->height) {
124
0
            const uint8_t fill = value ? 0xFF : 0x00;
125
0
            memset(image->data + (size_t) image->height * image->stride, fill, ((size_t) height - image->height) * image->stride);
126
0
        }
127
0
        image->height = height;
128
129
0
    } else {
130
0
        Jbig2Image *newimage;
131
0
        int code;
132
133
        /* Unoptimized implementation, but it works. */
134
135
0
        newimage = jbig2_image_new(ctx, width, height);
136
0
        if (newimage == NULL) {
137
0
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate resized image");
138
0
            return NULL;
139
0
        }
140
0
        jbig2_image_clear(ctx, newimage, value);
141
142
0
        code = jbig2_image_compose(ctx, newimage, image, 0, 0, JBIG2_COMPOSE_REPLACE);
143
0
        if (code < 0) {
144
0
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image buffers when resizing");
145
0
            jbig2_image_release(ctx, newimage);
146
0
            return NULL;
147
0
        }
148
149
        /* if refcount > 1 the original image, its pointer must
150
        be kept, so simply replaces its innards, and throw away
151
        the empty new image shell. */
152
0
        jbig2_free(ctx->allocator, image->data);
153
0
        image->width = newimage->width;
154
0
        image->height = newimage->height;
155
0
        image->stride = newimage->stride;
156
0
        image->data = newimage->data;
157
0
        jbig2_free(ctx->allocator, newimage);
158
0
    }
159
160
0
    return image;
161
0
}
162
163
static inline void
164
template_image_compose_opt(const uint8_t * JBIG2_RESTRICT ss, uint8_t * JBIG2_RESTRICT dd, int early, int late, uint8_t leftmask, uint8_t rightmask, uint32_t bytewidth_, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride, Jbig2ComposeOp op)
165
14.6M
{
166
14.6M
    int i;
167
14.6M
    uint32_t j;
168
14.6M
    int bytewidth = (int)bytewidth_;
169
170
14.6M
    if (bytewidth == 1) {
171
196M
        for (j = 0; j < h; j++) {
172
            /* Only 1 byte! */
173
191M
            uint8_t v = (((early ? 0 : ss[0]<<8) | (late ? 0 : ss[1]))>>shift);
174
191M
            if (op == JBIG2_COMPOSE_OR)
175
618k
                *dd |= v & leftmask;
176
190M
            else if (op == JBIG2_COMPOSE_AND)
177
0
                *dd &= (v & leftmask) | ~leftmask;
178
190M
            else if (op == JBIG2_COMPOSE_XOR)
179
0
                *dd ^= v & leftmask;
180
190M
            else if (op == JBIG2_COMPOSE_XNOR)
181
0
                *dd ^= (~v) & leftmask;
182
190M
            else /* Replace */
183
190M
                *dd = (v & leftmask) | (*dd & ~leftmask);
184
191M
            dd += dstride;
185
191M
            ss += sstride;
186
191M
        }
187
5.32M
        return;
188
5.32M
    }
189
9.33M
    bytewidth -= 2;
190
9.33M
    if (shift == 0) {
191
8.30M
        ss++;
192
1.50G
        for (j = 0; j < h; j++) {
193
            /* Left byte */
194
1.49G
            const uint8_t * JBIG2_RESTRICT s = ss;
195
1.49G
            uint8_t * JBIG2_RESTRICT d = dd;
196
1.49G
            if (op == JBIG2_COMPOSE_OR)
197
64
                *d++ |= *s++ & leftmask;
198
1.49G
            else if (op == JBIG2_COMPOSE_AND)
199
0
                *d++ &= (*s++ & leftmask) | ~leftmask;
200
1.49G
            else if (op == JBIG2_COMPOSE_XOR)
201
213k
                *d++ ^= *s++ & leftmask;
202
1.49G
            else if (op == JBIG2_COMPOSE_XNOR)
203
0
                *d++ ^= (~*s++) & leftmask;
204
1.49G
            else /* Replace */
205
1.49G
                *d = (*s++ & leftmask) | (*d & ~leftmask), d++;
206
            /* Central run */
207
3.29G
            for (i = bytewidth; i != 0; i--) {
208
1.79G
                if (op == JBIG2_COMPOSE_OR)
209
702
                    *d++ |= *s++;
210
1.79G
                else if (op == JBIG2_COMPOSE_AND)
211
0
                    *d++ &= *s++;
212
1.79G
                else if (op == JBIG2_COMPOSE_XOR)
213
65.8M
                    *d++ ^= *s++;
214
1.73G
                else if (op == JBIG2_COMPOSE_XNOR)
215
0
                    *d++ ^= ~*s++;
216
1.73G
                else /* Replace */
217
1.73G
                    *d++ = *s++;
218
1.79G
            }
219
            /* Right byte */
220
1.49G
            if (op == JBIG2_COMPOSE_OR)
221
64
                *d |= *s & rightmask;
222
1.49G
            else if (op == JBIG2_COMPOSE_AND)
223
0
                *d &= (*s & rightmask) | ~rightmask;
224
1.49G
            else if (op == JBIG2_COMPOSE_XOR)
225
213k
                *d ^= *s & rightmask;
226
1.49G
            else if (op == JBIG2_COMPOSE_XNOR)
227
0
                *d ^= (~*s) & rightmask;
228
1.49G
            else /* Replace */
229
1.49G
                *d = (*s & rightmask) | (*d & ~rightmask);
230
1.49G
            dd += dstride;
231
1.49G
            ss += sstride;
232
1.49G
        }
233
8.30M
    } else {
234
216M
        for (j = 0; j < h; j++) {
235
            /* Left byte */
236
215M
            const uint8_t * JBIG2_RESTRICT s = ss;
237
215M
            uint8_t * JBIG2_RESTRICT d = dd;
238
215M
            uint8_t s0, s1, v;
239
215M
            s0 = early ? 0 : *s;
240
215M
            s++;
241
215M
            s1 = *s++;
242
215M
            v = ((s0<<8) | s1)>>shift;
243
215M
            if (op == JBIG2_COMPOSE_OR)
244
255
                *d++ |= v & leftmask;
245
215M
            else if (op == JBIG2_COMPOSE_AND)
246
0
                *d++ &= (v & leftmask) | ~leftmask;
247
215M
            else if (op == JBIG2_COMPOSE_XOR)
248
0
                *d++ ^= v & leftmask;
249
215M
            else if (op == JBIG2_COMPOSE_XNOR)
250
0
                *d++ ^= (~v) & leftmask;
251
215M
            else /* Replace */
252
215M
                *d = (v & leftmask) | (*d & ~leftmask), d++;
253
            /* Central run */
254
6.44G
            for (i = bytewidth; i > 0; i--) {
255
6.22G
                s0 = s1; s1 = *s++;
256
6.22G
                v = ((s0<<8) | s1)>>shift;
257
6.22G
                if (op == JBIG2_COMPOSE_OR)
258
1.91k
                    *d++ |= v;
259
6.22G
                else if (op == JBIG2_COMPOSE_AND)
260
0
                    *d++ &= v;
261
6.22G
                else if (op == JBIG2_COMPOSE_XOR)
262
0
                    *d++ ^= v;
263
6.22G
                else if (op == JBIG2_COMPOSE_XNOR)
264
0
                    *d++ ^= ~v;
265
6.22G
                else /* Replace */
266
6.22G
                    *d++ = v;
267
6.22G
            }
268
            /* Right byte */
269
215M
            s0 = s1; s1 = (late ? 0 : *s);
270
215M
            v = (((s0<<8) | s1)>>shift);
271
215M
            if (op == JBIG2_COMPOSE_OR)
272
255
                *d |= v & rightmask;
273
215M
            else if (op == JBIG2_COMPOSE_AND)
274
0
                *d &= (v & rightmask) | ~rightmask;
275
215M
            else if (op == JBIG2_COMPOSE_XOR)
276
0
                *d ^= v & rightmask;
277
215M
            else if (op == JBIG2_COMPOSE_XNOR)
278
0
                *d ^= ~v & rightmask;
279
215M
            else /* Replace */
280
215M
                *d = (v & rightmask) | (*d & ~rightmask);
281
215M
            dd += dstride;
282
215M
            ss += sstride;
283
215M
        }
284
1.03M
    }
285
9.33M
}
286
287
static void
288
jbig2_image_compose_opt_OR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
289
618k
{
290
618k
    if (early || late)
291
618k
        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
292
3
    else
293
3
        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_OR);
294
618k
}
295
296
static void
297
jbig2_image_compose_opt_AND(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
298
0
{
299
0
    if (early || late)
300
0
        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
301
0
    else
302
0
        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_AND);
303
0
}
304
305
static void
306
jbig2_image_compose_opt_XOR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
307
61
{
308
61
    if (early || late)
309
61
        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
310
0
    else
311
0
        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XOR);
312
61
}
313
314
static void
315
jbig2_image_compose_opt_XNOR(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
316
0
{
317
0
    if (early || late)
318
0
        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
319
0
    else
320
0
        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_XNOR);
321
0
}
322
323
static void
324
jbig2_image_compose_opt_REPLACE(const uint8_t *s, uint8_t *d, int early, int late, uint8_t mask, uint8_t rightmask, uint32_t bytewidth, uint32_t h, uint32_t shift, uint32_t dstride, uint32_t sstride)
325
14.0M
{
326
14.0M
    if (early || late)
327
4.29k
        template_image_compose_opt(s, d, early, late, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
328
14.0M
    else
329
14.0M
        template_image_compose_opt(s, d, 0, 0, mask, rightmask, bytewidth, h, shift, dstride, sstride, JBIG2_COMPOSE_REPLACE);
330
14.0M
}
331
332
/* composite one jbig2_image onto another */
333
int
334
jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op)
335
18.8M
{
336
18.8M
    uint32_t w, h;
337
18.8M
    uint32_t shift;
338
18.8M
    uint32_t leftbyte;
339
18.8M
    uint8_t *ss;
340
18.8M
    uint8_t *dd;
341
18.8M
    uint8_t leftmask, rightmask;
342
18.8M
    int early = x >= 0;
343
18.8M
    int late;
344
18.8M
    uint32_t bytewidth;
345
18.8M
    uint32_t syoffset = 0;
346
347
18.8M
    if (src == NULL)
348
507k
        return 0;
349
350
18.3M
    if ((UINT32_MAX - src->width  < (uint32_t) (x > 0 ? x : -x)) ||
351
18.3M
        (UINT32_MAX - src->height < (uint32_t) (y > 0 ? y : -y)))
352
0
    {
353
#ifdef JBIG2_DEBUG
354
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "overflow in compose_image");
355
#endif
356
0
        return 0;
357
0
    }
358
359
    /* This code takes a src image and combines it onto dst at offset (x,y), with operation op. */
360
361
    /* Data is packed msb first within a byte, so with bits numbered: 01234567.
362
     * Second byte is: 89abcdef. So to combine into a run, we use:
363
     *       (s[0]<<8) | s[1] == 0123456789abcdef.
364
     * To read from src into dst at offset 3, we need to read:
365
     *    read:      0123456789abcdef...
366
     *    write:  0123456798abcdef...
367
     * In general, to read from src and write into dst at offset x, we need to shift
368
     * down by (x&7) bits to allow for bit alignment. So shift = x&7.
369
     * So the 'central' part of our runs will see us doing:
370
     *   *d++ op= ((s[0]<<8)|s[1])>>shift;
371
     * with special cases on the left and right edges of the run to mask.
372
     * With the left hand edge, we have to be careful not to 'underread' the start of
373
     * the src image; this is what the early flag is about. Similarly we have to be
374
     * careful not to read off the right hand edge; this is what the late flag is for.
375
     */
376
377
    /* clip */
378
18.3M
    w = src->width;
379
18.3M
    h = src->height;
380
18.3M
    shift = (x & 7);
381
18.3M
    ss = src->data - early;
382
383
18.3M
    if (x < 0) {
384
15.1M
        if (w < (uint32_t) -x)
385
1.14M
            w = 0;
386
14.0M
        else
387
14.0M
            w += x;
388
15.1M
        ss += (-x-1)>>3;
389
15.1M
        x = 0;
390
15.1M
    }
391
18.3M
    if (y < 0) {
392
957k
        if (h < (uint32_t) -y)
393
897k
            h = 0;
394
59.7k
        else
395
59.7k
            h += y;
396
957k
        syoffset = -y * src->stride;
397
957k
        y = 0;
398
957k
    }
399
18.3M
    if ((uint32_t)x + w > dst->width)
400
16.5M
    {
401
16.5M
        if (dst->width < (uint32_t)x)
402
2.39M
            w = 0;
403
14.1M
        else
404
14.1M
            w = dst->width - x;
405
16.5M
    }
406
18.3M
    if ((uint32_t)y + h > dst->height)
407
1.98M
    {
408
1.98M
        if (dst->height < (uint32_t)y)
409
1.63M
            h = 0;
410
344k
        else
411
344k
            h = dst->height - y;
412
1.98M
    }
413
#ifdef JBIG2_DEBUG
414
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "compositing %dx%d at (%d, %d) after clipping", w, h, x, y);
415
#endif
416
417
    /* check for zero clipping region */
418
18.3M
    if ((w <= 0) || (h <= 0)) {
419
#ifdef JBIG2_DEBUG
420
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "zero clipping region");
421
#endif
422
3.69M
        return 0;
423
3.69M
    }
424
425
14.6M
    leftbyte = (uint32_t) x >> 3;
426
14.6M
    dd = dst->data + y * dst->stride + leftbyte;
427
14.6M
    bytewidth = (((uint32_t) x + w - 1) >> 3) - leftbyte + 1;
428
14.6M
    leftmask = 255>>(x&7);
429
14.6M
    rightmask = (((x+w)&7) == 0) ? 255 : ~(255>>((x+w)&7));
430
14.6M
    if (bytewidth == 1)
431
5.32M
        leftmask &= rightmask;
432
14.6M
    late = (ss + bytewidth >= src->data + ((src->width+7)>>3));
433
14.6M
    ss += syoffset;
434
435
14.6M
    switch(op)
436
14.6M
    {
437
618k
    case JBIG2_COMPOSE_OR:
438
618k
        jbig2_image_compose_opt_OR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
439
618k
        break;
440
0
    case JBIG2_COMPOSE_AND:
441
0
        jbig2_image_compose_opt_AND(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
442
0
        break;
443
61
    case JBIG2_COMPOSE_XOR:
444
61
        jbig2_image_compose_opt_XOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
445
61
        break;
446
0
    case JBIG2_COMPOSE_XNOR:
447
0
        jbig2_image_compose_opt_XNOR(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
448
0
        break;
449
14.0M
    case JBIG2_COMPOSE_REPLACE:
450
14.0M
        jbig2_image_compose_opt_REPLACE(ss, dd, early, late, leftmask, rightmask, bytewidth, h, shift, dst->stride, src->stride);
451
14.0M
        break;
452
14.6M
    }
453
454
14.6M
    return 0;
455
14.6M
}
456
457
/* initialize an image bitmap to a constant value */
458
void
459
jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value)
460
9.99k
{
461
9.99k
    const uint8_t fill = value ? 0xFF : 0x00;
462
463
9.99k
    memset(image->data, fill, image->stride * image->height);
464
9.99k
}
465
466
/* look up a pixel value in an image.
467
   returns 0 outside the image frame for the convenience of
468
   the template code
469
*/
470
int
471
jbig2_image_get_pixel(Jbig2Image *image, int x, int y)
472
551M
{
473
551M
    const int w = image->width;
474
551M
    const int h = image->height;
475
551M
    const int byte = (x >> 3) + y * image->stride;
476
551M
    const int bit = 7 - (x & 7);
477
478
551M
    if ((x < 0) || (x >= w))
479
12.1M
        return 0;
480
538M
    if ((y < 0) || (y >= h))
481
236M
        return 0;
482
483
302M
    return ((image->data[byte] >> bit) & 1);
484
538M
}
485
486
/* set an individual pixel value in an image */
487
void
488
jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value)
489
16.6k
{
490
16.6k
    const int w = image->width;
491
16.6k
    const int h = image->height;
492
16.6k
    int scratch, mask;
493
16.6k
    int bit, byte;
494
495
16.6k
    if ((x < 0) || (x >= w))
496
0
        return;
497
16.6k
    if ((y < 0) || (y >= h))
498
0
        return;
499
500
16.6k
    byte = (x >> 3) + y * image->stride;
501
16.6k
    bit = 7 - (x & 7);
502
16.6k
    mask = (1 << bit) ^ 0xff;
503
504
16.6k
    scratch = image->data[byte] & mask;
505
16.6k
    image->data[byte] = scratch | (value << bit);
506
16.6k
}