Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/sjpx_openjpeg.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 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
18
/* opj filter implementation using OpenJPeg library */
19
20
#include "memory_.h"
21
#include "gserrors.h"
22
#include "gdebug.h"
23
#include "strimpl.h"
24
#include "sjpx_openjpeg.h"
25
#include "gxsync.h"
26
#include "assert_.h"
27
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
28
#include "opj_malloc.h"
29
#endif
30
/* Some locking to get around the criminal lack of context
31
 * in the openjpeg library. */
32
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
33
static gs_memory_t *opj_memory;
34
#endif
35
36
int sjpxd_create(gs_memory_t *mem)
37
162k
{
38
162k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
39
162k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
40
41
162k
    ctx->sjpxd_private = gx_monitor_label(gx_monitor_alloc(mem), "sjpxd_monitor");
42
162k
    if (ctx->sjpxd_private == NULL)
43
0
        return gs_error_VMerror;
44
162k
#endif
45
162k
    return 0;
46
162k
}
47
48
void sjpxd_destroy(gs_memory_t *mem)
49
162k
{
50
162k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
51
162k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
52
53
162k
    gx_monitor_free((gx_monitor_t *)ctx->sjpxd_private);
54
162k
    ctx->sjpxd_private = NULL;
55
162k
#endif
56
162k
}
57
58
static int opj_lock(gs_memory_t *mem)
59
650k
{
60
650k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
61
650k
    int ret;
62
63
650k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
64
65
650k
    ret = gx_monitor_enter((gx_monitor_t *)ctx->sjpxd_private);
66
650k
    assert(opj_memory == NULL);
67
650k
    opj_memory = mem->non_gc_memory;
68
650k
    return ret;
69
#else
70
    return 0;
71
#endif
72
650k
}
73
74
static int opj_unlock(gs_memory_t *mem)
75
650k
{
76
650k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
77
650k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
78
79
650k
    assert(opj_memory != NULL);
80
650k
    opj_memory = NULL;
81
650k
    return gx_monitor_leave((gx_monitor_t *)ctx->sjpxd_private);
82
#else
83
    return 0;
84
#endif
85
650k
}
86
87
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
88
/* Allocation routines that use the memory pointer given above */
89
void *opj_malloc(size_t size)
90
11.3M
{
91
11.3M
    if (size == 0)
92
0
        return NULL;
93
94
11.3M
    assert(opj_memory != NULL);
95
96
11.3M
    if (size > (size_t) ARCH_MAX_UINT)
97
0
      return NULL;
98
99
11.3M
    return (void *)gs_alloc_bytes(opj_memory, size, "opj_malloc");
100
11.3M
}
101
102
void *opj_calloc(size_t n, size_t size)
103
6.46M
{
104
6.46M
    void *ptr;
105
106
    /* FIXME: Check for overflow? */
107
6.46M
    size *= n;
108
109
6.46M
    ptr = opj_malloc(size);
110
6.46M
    if (ptr)
111
6.46M
        memset(ptr, 0, size);
112
6.46M
    return ptr;
113
6.46M
}
114
115
void *opj_realloc(void *ptr, size_t size)
116
1.41M
{
117
1.41M
    if (ptr == NULL)
118
586k
        return opj_malloc(size);
119
120
825k
    if (size == 0)
121
0
    {
122
0
        opj_free(ptr);
123
0
        return NULL;
124
0
    }
125
126
825k
    return gs_resize_object(opj_memory, ptr, size, "opj_malloc");
127
825k
}
128
129
void opj_free(void *ptr)
130
11.4M
{
131
11.4M
    gs_free_object(opj_memory, ptr, "opj_malloc");
132
11.4M
}
133
134
static inline void * opj_aligned_malloc_n(size_t size, size_t align)
135
591k
{
136
591k
    uint8_t *ptr;
137
591k
    int off;
138
139
591k
    if (size == 0)
140
0
        return NULL;
141
142
591k
    size += align + sizeof(uint8_t);
143
591k
    ptr = opj_malloc(size);
144
591k
    if (ptr == NULL)
145
0
        return NULL;
146
591k
    off = align - (((int)(intptr_t)ptr) & (align - 1));
147
591k
    ptr[off-1] = off;
148
591k
    return ptr + off;
149
591k
}
150
151
void * opj_aligned_malloc(size_t size)
152
589k
{
153
589k
    return opj_aligned_malloc_n(size, 16);
154
589k
}
155
156
void *opj_aligned_32_malloc(size_t size)
157
2.37k
{
158
2.37k
    return opj_aligned_malloc_n(size, 32);
159
2.37k
}
160
161
void opj_aligned_free(void* ptr_)
162
3.59M
{
163
3.59M
    uint8_t *ptr = (uint8_t *)ptr_;
164
3.59M
    uint8_t off;
165
3.59M
    if (ptr == NULL)
166
2.99M
        return;
167
168
591k
    off = ptr[-1];
169
591k
    opj_free((void *)(((unsigned char *)ptr) - off));
170
591k
}
171
172
#if 0
173
/* UNUSED currently, and moderately tricky, so deferred until required */
174
void * opj_aligned_realloc(void *ptr, size_t size)
175
{
176
  return opj_realloc(ptr, size);
177
}
178
#endif
179
#endif
180
181
gs_private_st_simple(st_jpxd_state, stream_jpxd_state,
182
    "JPXDecode filter state"); /* creates a gc object for our state,
183
                            defined in sjpx.h */
184
185
static int s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr);
186
187
static OPJ_SIZE_T sjpx_stream_read(void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
188
24.8k
{
189
24.8k
  stream_block *sb = (stream_block *)p_user_data;
190
24.8k
  OPJ_SIZE_T len;
191
192
24.8k
  len = sb->fill - sb->pos;
193
24.8k
        if (sb->fill < sb->pos)
194
0
    len = 0;
195
24.8k
  if (len == 0)
196
2.97k
    return (OPJ_SIZE_T)-1;  /* End of file! */
197
21.8k
  if ((OPJ_SIZE_T)len > p_nb_bytes)
198
0
    len = p_nb_bytes;
199
21.8k
  memcpy(p_buffer, sb->data + sb->pos, len);
200
21.8k
  sb->pos += len;
201
21.8k
  return len;
202
24.8k
}
203
204
static OPJ_OFF_T sjpx_stream_skip(OPJ_OFF_T skip, void * p_user_data)
205
249
{
206
249
  stream_block *sb = (stream_block *)p_user_data;
207
208
249
  if (skip > sb->fill - sb->pos)
209
249
    skip = sb->fill - sb->pos;
210
249
  sb->pos += skip;
211
249
  return sb->pos;
212
249
}
213
214
static OPJ_BOOL sjpx_stream_seek(OPJ_OFF_T seek_pos, void * p_user_data)
215
10.5k
{
216
10.5k
  stream_block *sb = (stream_block *)p_user_data;
217
218
10.5k
  if (seek_pos > sb->fill)
219
126
    return OPJ_FALSE;
220
10.3k
  sb->pos = seek_pos;
221
10.3k
  return OPJ_TRUE;
222
10.5k
}
223
224
static void sjpx_error_callback(const char *msg, void *ptr)
225
22.8k
{
226
22.8k
  dlprintf1("openjpeg error: %s", msg);
227
22.8k
}
228
229
static void sjpx_info_callback(const char *msg, void *ptr)
230
92.6k
{
231
#ifdef DEBUG
232
  /* prevent too many messages during normal build */
233
  dlprintf1("openjpeg info: %s", msg);
234
#endif
235
92.6k
}
236
237
static void sjpx_warning_callback(const char *msg, void *ptr)
238
564k
{
239
#ifdef DEBUG
240
  /* prevent too many messages during normal build */
241
  dlprintf1("openjpeg warning: %s", msg);
242
#endif
243
564k
}
244
245
/* initialize the stream */
246
static int
247
s_opjd_init(stream_state * ss)
248
13.7k
{
249
13.7k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
250
13.7k
    state->codec = NULL;
251
252
13.7k
    state->image = NULL;
253
13.7k
    state->sb.data= NULL;
254
13.7k
    state->sb.size = 0;
255
13.7k
    state->sb.pos = 0;
256
13.7k
    state->sb.fill = 0;
257
13.7k
    state->out_offset = 0;
258
13.7k
    state->pdata = NULL;
259
13.7k
    state->sign_comps = NULL;
260
13.7k
    state->stream = NULL;
261
13.7k
    state->row_data = NULL;
262
263
13.7k
    return 0;
264
13.7k
}
265
266
/* setting the codec format,
267
   allocating the stream and image structures, and
268
   initializing the decoder.
269
 */
270
static int
271
s_opjd_set_codec_format(stream_state * ss, OPJ_CODEC_FORMAT format)
272
13.2k
{
273
13.2k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
274
13.2k
    opj_dparameters_t parameters; /* decompression parameters */
275
276
    /* set decoding parameters to default values */
277
13.2k
    opj_set_default_decoder_parameters(&parameters);
278
279
    /* get a decoder handle */
280
13.2k
    state->codec = opj_create_decompress(format);
281
13.2k
    if (state->codec == NULL)
282
0
        return_error(gs_error_VMerror);
283
284
    /* catch events using our callbacks */
285
13.2k
    opj_set_error_handler(state->codec, sjpx_error_callback, stderr);
286
13.2k
    opj_set_info_handler(state->codec, sjpx_info_callback, stderr);
287
13.2k
    opj_set_warning_handler(state->codec, sjpx_warning_callback, stderr);
288
289
13.2k
    if (state->colorspace == gs_jpx_cs_indexed) {
290
5
        parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
291
5
    }
292
293
    /* setup the decoder decoding parameters using user parameters */
294
13.2k
    if (!opj_setup_decoder(state->codec, &parameters))
295
0
    {
296
0
        dlprintf("openjpeg: failed to setup the decoder!\n");
297
0
        return ERRC;
298
0
    }
299
300
    /* open a byte stream */
301
13.2k
    state->stream = opj_stream_default_create(OPJ_TRUE);
302
13.2k
    if (state->stream == NULL)
303
0
    {
304
0
        dlprintf("openjpeg: failed to open a byte stream!\n");
305
0
        return ERRC;
306
0
    }
307
308
13.2k
    opj_stream_set_read_function(state->stream, sjpx_stream_read);
309
13.2k
    opj_stream_set_skip_function(state->stream, sjpx_stream_skip);
310
13.2k
    opj_stream_set_seek_function(state->stream, sjpx_stream_seek);
311
312
13.2k
    return 0;
313
13.2k
}
314
315
static void
316
ycc_to_rgb_8(unsigned char *row, unsigned long row_size)
317
0
{
318
0
    unsigned char y;
319
0
    signed char u, v;
320
0
    int r,g,b;
321
322
    /* extra code for OSS-fuzz 414383025, the test file should not get here now
323
     * but as a belt and braces approach, check the width of the row is a
324
     * multiple of 3. If it isn't, reduce it until it is. This prevents
325
     * row_size being decreased *below* 0, failing to exit the loop and
326
     * reading from illegal memory locations
327
     */
328
0
    if (row_size < 3)
329
0
        return;
330
0
    row_size = row_size - row_size % 3;
331
332
0
    do
333
0
    {
334
0
        y = row[0];
335
0
        u = row[1] - 128;
336
0
        v = row[2] - 128;
337
0
        r = (int)((double)y + 1.402 * v);
338
0
        if (r < 0)
339
0
            r = 0;
340
0
        if (r > 255)
341
0
            r = 255;
342
0
        g = (int)((double)y - 0.34413 * u - 0.71414 * v);
343
0
        if (g < 0)
344
0
            g = 0;
345
0
        if (g > 255)
346
0
            g = 255;
347
0
        b = (int)((double)y + 1.772 * u);
348
0
        if (b < 0)
349
0
            b = 0;
350
0
        if (b > 255)
351
0
            b = 255;
352
0
        row[0] = r;
353
0
        row[1] = g;
354
0
        row[2] = b;
355
0
        row += 3;
356
0
        row_size -= 3;
357
0
    }
358
0
    while (row_size);
359
0
}
360
361
static void
362
ycc_to_rgb_16(unsigned char *row, unsigned long row_size)
363
0
{
364
0
    unsigned short y;
365
0
    signed short u, v;
366
0
    int r,g,b;
367
368
    /* As per the 8-bit case above, make sure that row-size is a mutiple of 6
369
     * so that we don't decrement it below 0 in the loop.
370
     */
371
0
    if (row_size < 6)
372
0
        return;
373
0
    row_size = row_size - row_size % 6;
374
375
0
    do
376
0
    {
377
0
        y = (row[0]<<8) | row[1];
378
0
        u = ((row[2]<<8) | row[3]) - 32768;
379
0
        v = ((row[4]<<8) | row[5]) - 32768;
380
0
        r = (int)((double)y + 1.402 * v);
381
0
        if (r < 0)
382
0
            r = 0;
383
0
        if (r > 65535)
384
0
            r = 65535;
385
0
        g = (int)((double)y - 0.34413 * u - 0.71414 * v);
386
0
        if (g < 0)
387
0
            g = 0;
388
0
        if (g > 65535)
389
0
            g = 65535;
390
0
        b = (int)((double)y + 1.772 * u);
391
0
        if (b < 0)
392
0
            b = 0;
393
0
        if (b > 65535)
394
0
            b = 65535;
395
0
        row[0] = r>>8;
396
0
        row[1] = r;
397
0
        row[2] = g>>8;
398
0
        row[3] = g;
399
0
        row[4] = b>>8;
400
0
        row[5] = b;
401
0
        row += 6;
402
0
        row_size -= 6;
403
0
    }
404
0
    while (row_size);
405
0
}
406
407
static int decode_image(stream_jpxd_state * const state)
408
13.2k
{
409
13.2k
    int numprimcomp = 0, alpha_comp = -1, compno, rowbytes;
410
411
    /* read header */
412
13.2k
    if (!opj_read_header(state->stream, state->codec, &(state->image)))
413
1.04k
    {
414
1.04k
      dlprintf("openjpeg: failed to read header\n");
415
1.04k
      return ERRC;
416
1.04k
    }
417
418
    /* decode the stream and fill the image structure */
419
12.1k
    if (!opj_decode(state->codec, state->stream, state->image))
420
7.39k
    {
421
7.39k
        dlprintf("openjpeg: failed to decode image!\n");
422
7.39k
        return ERRC;
423
7.39k
    }
424
425
    /* check dimension and prec */
426
4.77k
    if (state->image->numcomps == 0)
427
0
        return ERRC;
428
429
4.77k
    state->width = state->image->comps[0].w;
430
4.77k
    state->height = state->image->comps[0].h;
431
4.77k
    state->bpp = state->image->comps[0].prec;
432
4.77k
    state->samescale = true;
433
10.6k
    for(compno = 1; compno < state->image->numcomps; compno++)
434
5.82k
    {
435
5.82k
        if (state->bpp != state->image->comps[compno].prec)
436
0
            return ERRC; /* Not supported. */
437
5.82k
        if (state->width < state->image->comps[compno].w)
438
0
            state->width = state->image->comps[compno].w;
439
5.82k
        if (state->height < state->image->comps[compno].h)
440
0
            state->height = state->image->comps[compno].h;
441
5.82k
        if (state->image->comps[compno].dx != state->image->comps[0].dx ||
442
5.82k
                state->image->comps[compno].dy != state->image->comps[0].dy)
443
0
            state->samescale = false;
444
5.82k
    }
445
446
    /* find alpha component and regular colour component by channel definition */
447
15.3k
    for (compno = 0; compno < state->image->numcomps; compno++)
448
10.6k
    {
449
10.6k
        if (state->image->comps[compno].alpha == 0x00)
450
10.6k
            numprimcomp++;
451
4
        else if (state->image->comps[compno].alpha == 0x01 || state->image->comps[compno].alpha == 0x02)
452
4
            alpha_comp = compno;
453
10.6k
    }
454
455
    /* color space and number of components */
456
4.77k
    switch(state->image->color_space)
457
4.77k
    {
458
1.85k
        case OPJ_CLRSPC_GRAY:
459
1.85k
            state->colorspace = gs_jpx_cs_gray;
460
1.85k
            if (numprimcomp > 1) {
461
0
                dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component Gray data.\n", numprimcomp);
462
0
                numprimcomp = 1;
463
0
            }
464
1.85k
            break;
465
2.91k
        case OPJ_CLRSPC_SRGB:
466
2.91k
        case OPJ_CLRSPC_SYCC:
467
2.91k
        case OPJ_CLRSPC_EYCC:
468
2.91k
            state->colorspace = gs_jpx_cs_rgb;
469
2.91k
            if (numprimcomp > 3) {
470
0
                dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component RGB data.\n", numprimcomp);
471
0
                numprimcomp = 3;
472
0
            }
473
2.91k
           break;
474
0
        case OPJ_CLRSPC_CMYK:
475
0
            state->colorspace = gs_jpx_cs_cmyk;
476
0
            if (numprimcomp > 4) {
477
0
                dmprintf1(state->memory, "openjpeg warning: Ignoring extra components for %d component CMYK data.\n", numprimcomp);
478
0
                numprimcomp = 4;
479
0
            }
480
0
            break;
481
0
        default:
482
1
        case OPJ_CLRSPC_UNSPECIFIED:
483
5
        case OPJ_CLRSPC_UNKNOWN:
484
5
            if (numprimcomp == 1) {
485
3
                dmprintf1(state->memory, "openjpeg warning: unspec CS. %d component so assuming gray.\n", numprimcomp);
486
3
                state->colorspace = gs_jpx_cs_gray;
487
3
            } else if (numprimcomp == 4) {
488
0
                dmprintf1(state->memory, "openjpeg warning: unspec CS. %d components so assuming CMYK.\n", numprimcomp);
489
0
                state->colorspace = gs_jpx_cs_cmyk;
490
2
            } else {
491
                /* Note, numprimcomp > 4 possible here. Bug 694909.
492
                   Trust that it is RGB though.  Do not set numprimcomp */
493
2
                dmprintf1(state->memory, "openjpeg warning: unspec CS. %d components. Assuming data RGB.\n", numprimcomp);
494
2
                state->colorspace = gs_jpx_cs_rgb;
495
2
            }
496
5
            break;
497
4.77k
    }
498
499
4.77k
    state->alpha_comp = -1;
500
4.77k
    if (state->alpha)
501
0
    {
502
0
        state->alpha_comp = alpha_comp;
503
0
        state->out_numcomps = 1;
504
0
    }
505
4.77k
    else
506
4.77k
        state->out_numcomps = numprimcomp;
507
508
    /* round up bpp 12->16 */
509
4.77k
    if (state->bpp == 12)
510
0
        state->bpp = 16;
511
512
    /* calculate  total data */
513
4.77k
    rowbytes =  (state->width*state->bpp*state->out_numcomps+7)/8;
514
4.77k
    state->totalbytes = (ulong)rowbytes*state->height;
515
516
4.77k
    state->pdata = (int **)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int*)*state->image->numcomps, 1, "decode_image(pdata)");
517
4.77k
    if (!state->pdata)
518
0
        return_error(gs_error_VMerror);
519
520
    /* compensate for signed data (signed => unsigned) */
521
4.77k
    state->sign_comps = (int *)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int)*state->image->numcomps, 1, "decode_image(sign_comps)");
522
4.77k
    if (!state->sign_comps)
523
0
        return_error(gs_error_VMerror);
524
525
15.3k
    for(compno = 0; compno < state->image->numcomps; compno++)
526
10.6k
    {
527
10.6k
        if (state->image->comps[compno].sgnd)
528
0
            state->sign_comps[compno] = ((state->bpp%8)==0) ? 0x80 : (1<<(state->bpp-1));
529
10.6k
        else
530
10.6k
            state->sign_comps[compno] = 0;
531
10.6k
    }
532
533
4.77k
    return 0;
534
4.77k
}
535
536
static int process_one_trunk(stream_jpxd_state * const state, stream_cursor_write * pw)
537
1.56M
{
538
    /* read data from image to pw */
539
1.56M
    unsigned long out_size = pw->limit - pw->ptr;
540
1.56M
    int bytepp1 = state->bpp/8; /* bytes / pixel for one output component */
541
1.56M
    int bytepp = state->out_numcomps*state->bpp/8; /* bytes / pixel all components */
542
1.56M
    unsigned long write_size = min(out_size-(bytepp?(out_size%bytepp):0), state->totalbytes-state->out_offset);
543
1.56M
    int shift_bit = state->bpp-state->image->comps[0].prec; /*difference between input and output bit-depth*/
544
1.56M
    int img_numcomps = min(state->out_numcomps, state->image->numcomps); /* the actual number of channel data used */
545
1.56M
    int compno;
546
1.56M
    unsigned long il;
547
1.56M
    int i;
548
1.56M
    int b;
549
1.56M
    byte *row;
550
1.56M
    unsigned int x_offset;
551
1.56M
    unsigned int y_offset;
552
1.56M
    unsigned int row_size = (state->width * state->out_numcomps * state->bpp + 7)>>3;
553
554
    /* If nothing to write, nothing to do */
555
1.56M
    if (write_size == 0)
556
0
        return 0;
557
558
1.56M
    if (state->row_data == NULL)
559
4.77k
    {
560
4.77k
        state->row_data = gs_alloc_byte_array(state->memory->non_gc_memory, row_size, 1, "jpxd_openjpeg(row_data)");
561
4.77k
        if (state->row_data == NULL)
562
0
            return gs_error_VMerror;
563
4.77k
    }
564
565
3.18M
    while (state->out_offset != state->totalbytes)
566
3.18M
    {
567
3.18M
        y_offset = state->out_offset / row_size;
568
3.18M
        x_offset = state->out_offset % row_size;
569
570
3.18M
        if (x_offset == 0)
571
1.89M
        {
572
            /* Decode another rows worth */
573
1.89M
            row = state->row_data;
574
1.89M
            if (state->alpha && state->alpha_comp == -1)
575
0
            {
576
                /* return 0xff for all */
577
0
                memset(row, 0xff, row_size);
578
0
            }
579
1.89M
            else if (state->samescale)
580
1.89M
            {
581
1.89M
                if (state->alpha)
582
0
                    state->pdata[0] = &(state->image->comps[state->alpha_comp].data[y_offset * state->width]);
583
1.89M
                else
584
1.89M
                {
585
6.16M
                    for (compno=0; compno<img_numcomps; compno++)
586
4.26M
                        state->pdata[compno] = &(state->image->comps[compno].data[y_offset * state->width]);
587
1.89M
                }
588
1.89M
                if (shift_bit == 0 && state->bpp == 8) /* optimized for the most common case */
589
1.89M
                {
590
701M
                    for (i = state->width; i > 0; i--)
591
2.29G
                        for (compno=0; compno<img_numcomps; compno++)
592
1.59G
                            *row++ = *(state->pdata[compno]++) + state->sign_comps[compno]; /* copy input buffer to output */
593
1.89M
                }
594
1.05k
                else if ((state->bpp%8)==0)
595
680
                {
596
375k
                    for (i = state->width; i > 0; i--)
597
374k
                    {
598
749k
                        for (compno=0; compno<img_numcomps; compno++)
599
374k
                        {
600
1.12M
                            for (b=0; b<bytepp1; b++)
601
749k
                                *row++ = (((*(state->pdata[compno]) << shift_bit) >> (8*(bytepp1-b-1))))
602
749k
                                                        + (b==0 ? state->sign_comps[compno] : 0); /* split and shift input int to output bytes */
603
374k
                            state->pdata[compno]++;
604
374k
                        }
605
374k
                    }
606
680
                }
607
370
                else
608
370
                {
609
                    /* shift_bit = 0, bpp < 8 */
610
370
                    int bt=0;
611
370
                    int bit_pos = 0;
612
253k
                    for (i = state->width; i > 0; i--)
613
253k
                    {
614
506k
                        for (compno=0; compno<img_numcomps; compno++)
615
253k
                        {
616
253k
                            bt <<= state->bpp;
617
253k
                            bt += *(state->pdata[compno]++) + state->sign_comps[compno];
618
253k
                            bit_pos += state->bpp;
619
253k
                            if (bit_pos >= 8)
620
126k
                            {
621
126k
                                *row++ = bt >> (bit_pos-8);
622
126k
                                bit_pos -= 8;
623
126k
                                bt &= (1<<bit_pos)-1;
624
126k
                            }
625
253k
                        }
626
253k
                    }
627
370
                    if (bit_pos != 0)
628
0
                    {
629
                        /* row padding */
630
0
                        *row++ = bt << (8 - bit_pos);
631
0
                        bit_pos = 0;
632
0
                        bt = 0;
633
0
                    }
634
370
                }
635
1.89M
            }
636
0
            else if ((state->bpp%8)==0)
637
0
            {
638
                /* sampling required */
639
0
                if (state->alpha)
640
0
                {
641
0
                    for (i = 0; i < state->width; i++)
642
0
                    {
643
0
                        int dx = state->image->comps[state->alpha_comp].dx;
644
0
                        int dy = state->image->comps[state->alpha_comp].dy;
645
0
                        int w = state->image->comps[state->alpha_comp].w;
646
0
                        int in_offset_scaled = (y_offset/dy * w) + i / dx;
647
0
                        for (b=0; b<bytepp1; b++)
648
0
                            *row++ = (((state->image->comps[state->alpha_comp].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1))))
649
0
                                                                     + (b==0 ? state->sign_comps[state->alpha_comp] : 0);
650
0
                    }
651
0
                }
652
0
                else
653
0
                {
654
0
                    for (i = 0; i < state->width; i++)
655
0
                    {
656
0
                        for (compno=0; compno<img_numcomps; compno++)
657
0
                        {
658
0
                            int dx = state->image->comps[compno].dx;
659
0
                            int dy = state->image->comps[compno].dy;
660
0
                            int w = state->image->comps[compno].w;
661
0
                            int in_offset_scaled = (y_offset/dy * w) + i / dx;
662
0
                            for (b=0; b<bytepp1; b++)
663
0
                                *row++ = (((state->image->comps[compno].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1))))
664
0
                                                                                + (b==0 ? state->sign_comps[compno] : 0);
665
0
                        }
666
0
                    }
667
0
                }
668
0
            }
669
0
            else
670
0
            {
671
0
                int compno = state->alpha ? state->alpha_comp : 0;
672
0
                int bt=0;
673
0
                int ppbyte1 = 8/state->bpp;
674
                /* sampling required */
675
                /* only grayscale can have such bit-depth, also shift_bit = 0, bpp < 8 */
676
0
                for (i = 0; i < state->width; i++)
677
0
                {
678
0
                    for (b=0; b<ppbyte1; b++)
679
0
                    {
680
0
                        int dx = state->image->comps[compno].dx;
681
0
                        int dy = state->image->comps[compno].dy;
682
0
                        int w = state->image->comps[compno].w;
683
0
                        int in_offset_scaled = (y_offset/dy * w) + i / dx;
684
0
                        bt = bt<<state->bpp;
685
0
                        bt += state->image->comps[compno].data[in_offset_scaled] + state->sign_comps[compno];
686
0
                    }
687
0
                    *row++ = bt;
688
0
                }
689
0
            }
690
691
            /* Check the number of components, if it's not 3 then we could be dealing with an indexed space and must not do this conversion */
692
1.89M
            if ((state->image->color_space == OPJ_CLRSPC_SYCC || state->image->color_space == OPJ_CLRSPC_EYCC) && state->image->numcomps == 3)
693
0
            {
694
                /* bpp >= 8 always, as bpp < 8 only for grayscale */
695
0
                if (state->bpp == 8)
696
0
                    ycc_to_rgb_8(state->row_data, row_size);
697
0
                else
698
0
                    ycc_to_rgb_16(state->row_data, row_size);
699
0
            }
700
1.89M
        }
701
702
3.18M
        pw->ptr++;
703
3.18M
        il = (write_size > (unsigned long)(row_size - x_offset)) ? (row_size - x_offset) : (unsigned int)write_size;
704
3.18M
        memcpy(pw->ptr, &state->row_data[x_offset], il);
705
3.18M
        pw->ptr += il;
706
3.18M
        pw->ptr--;
707
3.18M
        state->out_offset += il;
708
3.18M
        write_size -= il;
709
3.18M
        if (write_size == 0)
710
1.56M
            break;
711
3.18M
    }
712
713
1.56M
    if (state->out_offset == state->totalbytes)
714
4.77k
        return EOFC; /* all data returned */
715
1.55M
    else
716
1.55M
        return 1; /* need more calls */
717
1.56M
}
718
719
/* process a section of the input and return any decoded data.
720
   see strimpl.h for return codes.
721
 */
722
static int
723
s_opjd_process(stream_state * ss, stream_cursor_read * pr,
724
                 stream_cursor_write * pw, bool last)
725
2.20M
{
726
2.20M
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
727
2.20M
    long in_size = pr->limit - pr->ptr;
728
2.20M
    int locked = 0;
729
2.20M
    int code;
730
731
2.20M
    if (in_size > 0)
732
637k
    {
733
637k
        if (state->PassThrough && state->PassThroughfn) {
734
153k
            if (state->PassThrough && state->PassThroughfn && !state->StartedPassThrough) {
735
1.86k
                state->StartedPassThrough = 1;
736
1.86k
                (state->PassThroughfn)(state->device, NULL, 1);
737
1.86k
            }
738
153k
            (state->PassThroughfn)(state->device, (byte *)pr->ptr + 1, (byte *)pr->limit - (byte *)pr->ptr);
739
153k
        }
740
741
        /* buffer available data */
742
637k
        code = opj_lock(ss->memory);
743
637k
        if (code < 0) return code;
744
637k
        locked = 1;
745
746
637k
        code = s_opjd_accumulate_input(state, pr);
747
637k
        if (code < 0) {
748
0
            (void)opj_unlock(ss->memory);
749
0
            return code;
750
0
        }
751
752
637k
        if (state->codec == NULL) {
753
            /* state->sb.size is non-zero after successful
754
               accumulate_input(); 1 is probably extremely rare */
755
13.2k
            if (state->sb.data[0] == 0xFF && ((state->sb.size == 1) || (state->sb.data[1] == 0x4F)))
756
10
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_J2K);
757
13.2k
            else
758
13.2k
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_JP2);
759
13.2k
            if (code < 0)
760
0
            {
761
0
                (void)opj_unlock(ss->memory);
762
0
                return code;
763
0
            }
764
13.2k
        }
765
637k
    }
766
767
2.20M
    if (last == 1)
768
1.57M
    {
769
1.57M
        if (state->image == NULL)
770
13.2k
        {
771
13.2k
            int ret;
772
773
13.2k
            if (locked == 0)
774
8
            {
775
8
                ret = opj_lock(ss->memory);
776
8
                if (ret < 0) return ret;
777
8
                locked = 1;
778
8
            }
779
780
13.2k
#if OPJ_VERSION_MAJOR >= 2 && OPJ_VERSION_MINOR >= 1
781
13.2k
            opj_stream_set_user_data(state->stream, &(state->sb), NULL);
782
#else
783
            opj_stream_set_user_data(state->stream, &(state->sb));
784
#endif
785
13.2k
            opj_stream_set_user_data_length(state->stream, state->sb.size);
786
13.2k
            ret = decode_image(state);
787
13.2k
            if (ret != 0)
788
8.44k
            {
789
8.44k
                (void)opj_unlock(ss->memory);
790
8.44k
                return ret;
791
8.44k
            }
792
13.2k
        }
793
794
1.56M
        if (locked)
795
4.77k
        {
796
4.77k
            code = opj_unlock(ss->memory);
797
4.77k
            if (code < 0) return code;
798
4.77k
        }
799
800
        /* copy out available data */
801
1.56M
        return process_one_trunk(state, pw);
802
803
1.56M
    }
804
805
636k
    if (locked)
806
624k
        return opj_unlock(ss->memory);
807
808
    /* ask for more data */
809
12.6k
    return 0;
810
636k
}
811
812
/* Set the defaults */
813
static void
814
27.4k
s_opjd_set_defaults(stream_state * ss) {
815
27.4k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
816
817
27.4k
    state->alpha = false;
818
27.4k
    state->colorspace = gs_jpx_cs_rgb;
819
27.4k
    state->StartedPassThrough = 0;
820
27.4k
    state->PassThrough = 0;
821
27.4k
    state->PassThroughfn = NULL;
822
27.4k
    state->device = (void *)NULL;
823
27.4k
}
824
825
/* stream release.
826
   free all our decoder state.
827
 */
828
static void
829
s_opjd_release(stream_state *ss)
830
13.7k
{
831
13.7k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
832
833
13.7k
    if (state->PassThrough && state->PassThroughfn && state->StartedPassThrough) {
834
1.86k
        state->StartedPassThrough = 0;
835
1.86k
        (state->PassThroughfn)(state->device, NULL, 0);
836
1.86k
    }
837
    /* empty stream or failed to accumulate */
838
13.7k
    if (state->codec == NULL)
839
528
        return;
840
841
13.2k
    (void)opj_lock(ss->memory);
842
843
    /* free image data structure */
844
13.2k
    if (state->image)
845
12.1k
        opj_image_destroy(state->image);
846
847
    /* free stream */
848
13.2k
    if (state->stream)
849
13.2k
        opj_stream_destroy(state->stream);
850
851
    /* free decoder handle */
852
13.2k
    if (state->codec)
853
13.2k
  opj_destroy_codec(state->codec);
854
855
13.2k
    (void)opj_unlock(ss->memory);
856
857
    /* free input buffer */
858
13.2k
    if (state->sb.data)
859
13.2k
        gs_free_object(state->memory->non_gc_memory, state->sb.data, "s_opjd_release(sb.data)");
860
861
13.2k
    if (state->pdata)
862
4.77k
        gs_free_object(state->memory->non_gc_memory, state->pdata, "s_opjd_release(pdata)");
863
864
13.2k
    if (state->sign_comps)
865
4.77k
        gs_free_object(state->memory->non_gc_memory, state->sign_comps, "s_opjd_release(sign_comps)");
866
867
13.2k
    if (state->row_data)
868
4.77k
        gs_free_object(state->memory->non_gc_memory, state->row_data, "s_opjd_release(row_data)");
869
13.2k
}
870
871
872
static int
873
s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr)
874
637k
{
875
637k
    long in_size = pr->limit - pr->ptr;
876
877
    /* grow the input buffer if needed */
878
637k
    if (state->sb.size < state->sb.fill + in_size)
879
66.0k
    {
880
66.0k
        unsigned char *new_buf;
881
66.0k
        unsigned long new_size = state->sb.size==0 ? in_size : state->sb.size;
882
883
119k
        while (new_size < state->sb.fill + in_size)
884
53.3k
            new_size = new_size << 1;
885
886
66.0k
        if_debug1('s', "[s]opj growing input buffer to %lu bytes\n",
887
66.0k
                new_size);
888
66.0k
        if (state->sb.data == NULL)
889
13.2k
            new_buf = (byte *) gs_alloc_byte_array(state->memory->non_gc_memory, new_size, 1, "s_opjd_accumulate_input(alloc)");
890
52.7k
        else
891
52.7k
            new_buf = (byte *) gs_resize_object(state->memory->non_gc_memory, state->sb.data, new_size, "s_opjd_accumulate_input(resize)");
892
66.0k
        if (new_buf == NULL) return_error( gs_error_VMerror);
893
894
66.0k
        state->sb.data = new_buf;
895
66.0k
        state->sb.size = new_size;
896
66.0k
    }
897
898
    /* copy the available input into our buffer */
899
    /* note that the gs stream library uses offset-by-one
900
        indexing of its buffers while we use zero indexing */
901
637k
    memcpy(state->sb.data + state->sb.fill, pr->ptr + 1, in_size);
902
637k
    state->sb.fill += in_size;
903
637k
    pr->ptr += in_size;
904
905
637k
    return 0;
906
637k
}
907
908
/* stream template */
909
const stream_template s_jpxd_template = {
910
    &st_jpxd_state,
911
    s_opjd_init,
912
    s_opjd_process,
913
    1024, 1024,   /* min in and out buffer sizes we can handle
914
                     should be ~32k,64k for efficiency? */
915
    s_opjd_release,
916
    s_opjd_set_defaults
917
};