Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/sjpx_openjpeg.c
Line
Count
Source
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
192k
{
38
192k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
39
192k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
40
41
192k
    ctx->sjpxd_private = gx_monitor_label(gx_monitor_alloc(mem), "sjpxd_monitor");
42
192k
    if (ctx->sjpxd_private == NULL)
43
0
        return gs_error_VMerror;
44
192k
#endif
45
192k
    return 0;
46
192k
}
47
48
void sjpxd_destroy(gs_memory_t *mem)
49
192k
{
50
192k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
51
192k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
52
53
192k
    gx_monitor_free((gx_monitor_t *)ctx->sjpxd_private);
54
192k
    ctx->sjpxd_private = NULL;
55
192k
#endif
56
192k
}
57
58
static int opj_lock(gs_memory_t *mem)
59
676k
{
60
676k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
61
676k
    int ret;
62
63
676k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
64
65
676k
    ret = gx_monitor_enter((gx_monitor_t *)ctx->sjpxd_private);
66
676k
    assert(opj_memory == NULL);
67
676k
    opj_memory = mem->non_gc_memory;
68
676k
    return ret;
69
#else
70
    return 0;
71
#endif
72
676k
}
73
74
static int opj_unlock(gs_memory_t *mem)
75
676k
{
76
676k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
77
676k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
78
79
676k
    assert(opj_memory != NULL);
80
676k
    opj_memory = NULL;
81
676k
    return gx_monitor_leave((gx_monitor_t *)ctx->sjpxd_private);
82
#else
83
    return 0;
84
#endif
85
676k
}
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
13.5M
{
91
13.5M
    if (size == 0)
92
0
        return NULL;
93
94
13.5M
    assert(opj_memory != NULL);
95
96
13.5M
    if (size > (size_t) ARCH_MAX_UINT)
97
0
      return NULL;
98
99
13.5M
    return (void *)gs_alloc_bytes(opj_memory, size, "opj_malloc");
100
13.5M
}
101
102
void *opj_calloc(size_t n, size_t size)
103
8.18M
{
104
8.18M
    void *ptr;
105
106
    /* FIXME: Check for overflow? */
107
8.18M
    size *= n;
108
109
8.18M
    ptr = opj_malloc(size);
110
8.18M
    if (ptr)
111
8.18M
        memset(ptr, 0, size);
112
8.18M
    return ptr;
113
8.18M
}
114
115
void *opj_realloc(void *ptr, size_t size)
116
1.61M
{
117
1.61M
    if (ptr == NULL)
118
695k
        return opj_malloc(size);
119
120
917k
    if (size == 0)
121
0
    {
122
0
        opj_free(ptr);
123
0
        return NULL;
124
0
    }
125
126
917k
    return gs_resize_object(opj_memory, ptr, size, "opj_malloc");
127
917k
}
128
129
void opj_free(void *ptr)
130
13.6M
{
131
13.6M
    gs_free_object(opj_memory, ptr, "opj_malloc");
132
13.6M
}
133
134
static inline void * opj_aligned_malloc_n(size_t size, size_t align)
135
791k
{
136
791k
    uint8_t *ptr;
137
791k
    int off;
138
139
791k
    if (size == 0)
140
0
        return NULL;
141
142
791k
    size += align + sizeof(uint8_t);
143
791k
    ptr = opj_malloc(size);
144
791k
    if (ptr == NULL)
145
0
        return NULL;
146
791k
    off = align - (((int)(intptr_t)ptr) & (align - 1));
147
791k
    ptr[off-1] = off;
148
791k
    return ptr + off;
149
791k
}
150
151
void * opj_aligned_malloc(size_t size)
152
786k
{
153
786k
    return opj_aligned_malloc_n(size, 16);
154
786k
}
155
156
void *opj_aligned_32_malloc(size_t size)
157
5.49k
{
158
5.49k
    return opj_aligned_malloc_n(size, 32);
159
5.49k
}
160
161
void opj_aligned_free(void* ptr_)
162
4.69M
{
163
4.69M
    uint8_t *ptr = (uint8_t *)ptr_;
164
4.69M
    uint8_t off;
165
4.69M
    if (ptr == NULL)
166
3.90M
        return;
167
168
791k
    off = ptr[-1];
169
791k
    opj_free((void *)(((unsigned char *)ptr) - off));
170
791k
}
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
26.6k
{
189
26.6k
  stream_block *sb = (stream_block *)p_user_data;
190
26.6k
  OPJ_SIZE_T len;
191
192
26.6k
  len = sb->fill - sb->pos;
193
26.6k
        if (sb->fill < sb->pos)
194
0
    len = 0;
195
26.6k
  if (len == 0)
196
1.85k
    return (OPJ_SIZE_T)-1;  /* End of file! */
197
24.8k
  if ((OPJ_SIZE_T)len > p_nb_bytes)
198
0
    len = p_nb_bytes;
199
24.8k
  memcpy(p_buffer, sb->data + sb->pos, len);
200
24.8k
  sb->pos += len;
201
24.8k
  return len;
202
26.6k
}
203
204
static OPJ_OFF_T sjpx_stream_skip(OPJ_OFF_T skip, void * p_user_data)
205
26
{
206
26
  stream_block *sb = (stream_block *)p_user_data;
207
208
26
  if (skip > sb->fill - sb->pos)
209
26
    skip = sb->fill - sb->pos;
210
26
  sb->pos += skip;
211
26
  return sb->pos;
212
26
}
213
214
static OPJ_BOOL sjpx_stream_seek(OPJ_OFF_T seek_pos, void * p_user_data)
215
7.43k
{
216
7.43k
  stream_block *sb = (stream_block *)p_user_data;
217
218
7.43k
  if (seek_pos > sb->fill)
219
123
    return OPJ_FALSE;
220
7.31k
  sb->pos = seek_pos;
221
7.31k
  return OPJ_TRUE;
222
7.43k
}
223
224
static void sjpx_error_callback(const char *msg, void *ptr)
225
29.6k
{
226
29.6k
  dlprintf1("openjpeg error: %s", msg);
227
29.6k
}
228
229
static void sjpx_info_callback(const char *msg, void *ptr)
230
122k
{
231
#ifdef DEBUG
232
  /* prevent too many messages during normal build */
233
  dlprintf1("openjpeg info: %s", msg);
234
#endif
235
122k
}
236
237
static void sjpx_warning_callback(const char *msg, void *ptr)
238
754
{
239
#ifdef DEBUG
240
  /* prevent too many messages during normal build */
241
  dlprintf1("openjpeg warning: %s", msg);
242
#endif
243
754
}
244
245
/* initialize the stream */
246
static int
247
s_opjd_init(stream_state * ss)
248
18.5k
{
249
18.5k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
250
18.5k
    state->codec = NULL;
251
252
18.5k
    state->image = NULL;
253
18.5k
    state->sb.data= NULL;
254
18.5k
    state->sb.size = 0;
255
18.5k
    state->sb.pos = 0;
256
18.5k
    state->sb.fill = 0;
257
18.5k
    state->out_offset = 0;
258
18.5k
    state->pdata = NULL;
259
18.5k
    state->sign_comps = NULL;
260
18.5k
    state->stream = NULL;
261
18.5k
    state->row_data = NULL;
262
263
18.5k
    return 0;
264
18.5k
}
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
17.8k
{
273
17.8k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
274
17.8k
    opj_dparameters_t parameters; /* decompression parameters */
275
276
    /* set decoding parameters to default values */
277
17.8k
    opj_set_default_decoder_parameters(&parameters);
278
279
    /* get a decoder handle */
280
17.8k
    state->codec = opj_create_decompress(format);
281
17.8k
    if (state->codec == NULL)
282
0
        return_error(gs_error_VMerror);
283
284
    /* catch events using our callbacks */
285
17.8k
    opj_set_error_handler(state->codec, sjpx_error_callback, stderr);
286
17.8k
    opj_set_info_handler(state->codec, sjpx_info_callback, stderr);
287
17.8k
    opj_set_warning_handler(state->codec, sjpx_warning_callback, stderr);
288
289
17.8k
    if (state->colorspace == gs_jpx_cs_indexed) {
290
74
        parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
291
74
    }
292
293
    /* setup the decoder decoding parameters using user parameters */
294
17.8k
    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
17.8k
    state->stream = opj_stream_default_create(OPJ_TRUE);
302
17.8k
    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
17.8k
    opj_stream_set_read_function(state->stream, sjpx_stream_read);
309
17.8k
    opj_stream_set_skip_function(state->stream, sjpx_stream_skip);
310
17.8k
    opj_stream_set_seek_function(state->stream, sjpx_stream_seek);
311
312
17.8k
    return 0;
313
17.8k
}
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
17.8k
{
409
17.8k
    int numprimcomp = 0, alpha_comp = -1, compno, rowbytes, bps;
410
411
    /* read header */
412
17.8k
    if (!opj_read_header(state->stream, state->codec, &(state->image)))
413
1.39k
    {
414
1.39k
      dlprintf("openjpeg: failed to read header\n");
415
1.39k
      return ERRC;
416
1.39k
    }
417
418
    /* decode the stream and fill the image structure */
419
16.5k
    if (!opj_decode(state->codec, state->stream, state->image))
420
9.51k
    {
421
9.51k
        dlprintf("openjpeg: failed to decode image!\n");
422
9.51k
        return ERRC;
423
9.51k
    }
424
425
    /* check dimension and prec */
426
6.99k
    if (state->image->numcomps == 0)
427
0
        return ERRC;
428
429
6.99k
    state->width = state->image->comps[0].w;
430
6.99k
    state->height = state->image->comps[0].h;
431
6.99k
    state->bpp = state->image->comps[0].prec;
432
6.99k
    state->samescale = true;
433
15.0k
    for(compno = 1; compno < state->image->numcomps; compno++)
434
8.07k
    {
435
8.07k
        if (state->bpp != state->image->comps[compno].prec)
436
0
            return ERRC; /* Not supported. */
437
8.07k
        if (state->width < state->image->comps[compno].w)
438
0
            state->width = state->image->comps[compno].w;
439
8.07k
        if (state->height < state->image->comps[compno].h)
440
0
            state->height = state->image->comps[compno].h;
441
8.07k
        if (state->image->comps[compno].dx != state->image->comps[0].dx ||
442
8.07k
                state->image->comps[compno].dy != state->image->comps[0].dy)
443
0
            state->samescale = false;
444
8.07k
    }
445
446
    /* find alpha component and regular colour component by channel definition */
447
22.0k
    for (compno = 0; compno < state->image->numcomps; compno++)
448
15.0k
    {
449
15.0k
        if (state->image->comps[compno].alpha == 0x00)
450
14.9k
            numprimcomp++;
451
95
        else if (state->image->comps[compno].alpha == 0x01 || state->image->comps[compno].alpha == 0x02)
452
95
            alpha_comp = compno;
453
15.0k
    }
454
455
    /* color space and number of components */
456
6.99k
    switch(state->image->color_space)
457
6.99k
    {
458
2.96k
        case OPJ_CLRSPC_GRAY:
459
2.96k
            state->colorspace = gs_jpx_cs_gray;
460
2.96k
            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
2.96k
            break;
465
4.00k
        case OPJ_CLRSPC_SRGB:
466
4.00k
        case OPJ_CLRSPC_SYCC:
467
4.00k
        case OPJ_CLRSPC_EYCC:
468
4.00k
            state->colorspace = gs_jpx_cs_rgb;
469
4.00k
            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
4.00k
           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
6
        case OPJ_CLRSPC_UNSPECIFIED:
483
14
        case OPJ_CLRSPC_UNKNOWN:
484
14
            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
11
            } 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
11
            } else {
491
                /* Note, numprimcomp > 4 possible here. Bug 694909.
492
                   Trust that it is RGB though.  Do not set numprimcomp */
493
11
                dmprintf1(state->memory, "openjpeg warning: unspec CS. %d components. Assuming data RGB.\n", numprimcomp);
494
11
                state->colorspace = gs_jpx_cs_rgb;
495
11
            }
496
14
            break;
497
6.99k
    }
498
499
6.99k
    state->alpha_comp = -1;
500
6.99k
    if (state->alpha)
501
0
    {
502
0
        state->alpha_comp = alpha_comp;
503
0
        state->out_numcomps = 1;
504
0
    }
505
6.99k
    else
506
6.99k
        state->out_numcomps = numprimcomp;
507
508
    /* round up bpp 12->16 */
509
6.99k
    if (state->bpp == 12)
510
0
        state->bpp = 16;
511
512
    /* calculate  total data */
513
6.99k
    bps = state->bpp * state->out_numcomps;
514
515
    /* Overflow check, almost certainly superfluous, but let's be certain */
516
6.99k
    if (state->out_numcomps != 0 && bps / state->out_numcomps != state->bpp)
517
0
        return_error(gs_error_rangecheck);
518
519
6.99k
    rowbytes = state->width * bps;
520
    /* Overflow check */
521
6.99k
    if ((bps != 0 && rowbytes / bps != state->width) || rowbytes > max_int - 8)
522
0
        return_error(gs_error_rangecheck);
523
524
6.99k
    rowbytes = (rowbytes + 7) / 8;
525
526
6.99k
    state->totalbytes = (ulong)rowbytes*state->height;
527
    /* Overflow check */
528
6.99k
    if (rowbytes != 0 && state->totalbytes / rowbytes != state->height)
529
0
        return_error(gs_error_rangecheck);
530
531
6.99k
    state->pdata = (int **)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int*)*(size_t)state->image->numcomps, 1, "decode_image(pdata)");
532
6.99k
    if (!state->pdata)
533
0
        return_error(gs_error_VMerror);
534
535
    /* compensate for signed data (signed => unsigned) */
536
6.99k
    state->sign_comps = (int *)gs_alloc_byte_array(state->memory->non_gc_memory, sizeof(int)*(size_t)state->image->numcomps, 1, "decode_image(sign_comps)");
537
6.99k
    if (!state->sign_comps)
538
0
        return_error(gs_error_VMerror);
539
540
22.0k
    for(compno = 0; compno < state->image->numcomps; compno++)
541
15.0k
    {
542
15.0k
        if (state->image->comps[compno].sgnd)
543
0
            state->sign_comps[compno] = ((state->bpp%8)==0) ? 0x80 : (1<<(state->bpp-1));
544
15.0k
        else
545
15.0k
            state->sign_comps[compno] = 0;
546
15.0k
    }
547
548
6.99k
    return 0;
549
6.99k
}
550
551
static int process_one_trunk(stream_jpxd_state * const state, stream_cursor_write * pw)
552
1.97M
{
553
    /* read data from image to pw */
554
1.97M
    unsigned long out_size = pw->limit - pw->ptr;
555
1.97M
    int bytepp1 = state->bpp/8; /* bytes / pixel for one output component */
556
1.97M
    int bytepp = state->out_numcomps*state->bpp/8; /* bytes / pixel all components */
557
1.97M
    unsigned long write_size = min(out_size-(bytepp?(out_size%bytepp):0), state->totalbytes-state->out_offset);
558
1.97M
    int shift_bit = state->bpp-state->image->comps[0].prec; /*difference between input and output bit-depth*/
559
1.97M
    int img_numcomps = min(state->out_numcomps, state->image->numcomps); /* the actual number of channel data used */
560
1.97M
    int compno;
561
1.97M
    unsigned long il;
562
1.97M
    int i;
563
1.97M
    int b;
564
1.97M
    byte *row;
565
1.97M
    unsigned int x_offset;
566
1.97M
    unsigned int y_offset;
567
1.97M
    unsigned int row_size = (state->width * state->out_numcomps * state->bpp + 7)>>3;
568
569
    /* If nothing to write, nothing to do */
570
1.97M
    if (write_size == 0)
571
0
        return 0;
572
573
1.97M
    if (state->row_data == NULL)
574
6.99k
    {
575
6.99k
        state->row_data = gs_alloc_byte_array(state->memory->non_gc_memory, row_size, 1, "jpxd_openjpeg(row_data)");
576
6.99k
        if (state->row_data == NULL)
577
0
            return gs_error_VMerror;
578
6.99k
    }
579
580
4.27M
    while (state->out_offset != state->totalbytes)
581
4.27M
    {
582
4.27M
        y_offset = state->out_offset / row_size;
583
4.27M
        x_offset = state->out_offset % row_size;
584
585
4.27M
        if (x_offset == 0)
586
2.65M
        {
587
            /* Decode another rows worth */
588
2.65M
            row = state->row_data;
589
2.65M
            if (state->alpha && state->alpha_comp == -1)
590
0
            {
591
                /* return 0xff for all */
592
0
                memset(row, 0xff, row_size);
593
0
            }
594
2.65M
            else if (state->samescale)
595
2.65M
            {
596
2.65M
                if (state->alpha)
597
0
                    state->pdata[0] = &(state->image->comps[state->alpha_comp].data[y_offset * state->width]);
598
2.65M
                else
599
2.65M
                {
600
8.41M
                    for (compno=0; compno<img_numcomps; compno++)
601
5.75M
                        state->pdata[compno] = &(state->image->comps[compno].data[y_offset * state->width]);
602
2.65M
                }
603
2.65M
                if (shift_bit == 0 && state->bpp == 8) /* optimized for the most common case */
604
2.64M
                {
605
937M
                    for (i = state->width; i > 0; i--)
606
2.97G
                        for (compno=0; compno<img_numcomps; compno++)
607
2.04G
                            *row++ = *(state->pdata[compno]++) + state->sign_comps[compno]; /* copy input buffer to output */
608
2.64M
                }
609
17.8k
                else if ((state->bpp%8)==0)
610
15.8k
                {
611
8.72M
                    for (i = state->width; i > 0; i--)
612
8.70M
                    {
613
17.4M
                        for (compno=0; compno<img_numcomps; compno++)
614
8.70M
                        {
615
26.1M
                            for (b=0; b<bytepp1; b++)
616
17.4M
                                *row++ = (((*(state->pdata[compno]) << shift_bit) >> (8*(bytepp1-b-1))))
617
17.4M
                                                        + (b==0 ? state->sign_comps[compno] : 0); /* split and shift input int to output bytes */
618
8.70M
                            state->pdata[compno]++;
619
8.70M
                        }
620
8.70M
                    }
621
15.8k
                }
622
2.07k
                else
623
2.07k
                {
624
                    /* shift_bit = 0, bpp < 8 */
625
2.07k
                    int bt=0;
626
2.07k
                    int bit_pos = 0;
627
1.41M
                    for (i = state->width; i > 0; i--)
628
1.41M
                    {
629
2.93M
                        for (compno=0; compno<img_numcomps; compno++)
630
1.51M
                        {
631
1.51M
                            bt <<= state->bpp;
632
1.51M
                            bt += *(state->pdata[compno]++) + state->sign_comps[compno];
633
1.51M
                            bit_pos += state->bpp;
634
1.51M
                            if (bit_pos >= 8)
635
759k
                            {
636
759k
                                *row++ = bt >> (bit_pos-8);
637
759k
                                bit_pos -= 8;
638
759k
                                bt &= (1<<bit_pos)-1;
639
759k
                            }
640
1.51M
                        }
641
1.41M
                    }
642
2.07k
                    if (bit_pos != 0)
643
0
                    {
644
                        /* row padding */
645
0
                        *row++ = bt << (8 - bit_pos);
646
0
                        bit_pos = 0;
647
0
                        bt = 0;
648
0
                    }
649
2.07k
                }
650
2.65M
            }
651
0
            else if ((state->bpp%8)==0)
652
0
            {
653
                /* sampling required */
654
0
                if (state->alpha)
655
0
                {
656
0
                    for (i = 0; i < state->width; i++)
657
0
                    {
658
0
                        int dx = state->image->comps[state->alpha_comp].dx;
659
0
                        int dy = state->image->comps[state->alpha_comp].dy;
660
0
                        int w = state->image->comps[state->alpha_comp].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[state->alpha_comp].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1))))
664
0
                                                                     + (b==0 ? state->sign_comps[state->alpha_comp] : 0);
665
0
                    }
666
0
                }
667
0
                else
668
0
                {
669
0
                    for (i = 0; i < state->width; i++)
670
0
                    {
671
0
                        for (compno=0; compno<img_numcomps; compno++)
672
0
                        {
673
0
                            int dx = state->image->comps[compno].dx;
674
0
                            int dy = state->image->comps[compno].dy;
675
0
                            int w = state->image->comps[compno].w;
676
0
                            int in_offset_scaled = (y_offset/dy * w) + i / dx;
677
0
                            for (b=0; b<bytepp1; b++)
678
0
                                *row++ = (((state->image->comps[compno].data[in_offset_scaled] << shift_bit) >> (8*(bytepp1-b-1))))
679
0
                                                                                + (b==0 ? state->sign_comps[compno] : 0);
680
0
                        }
681
0
                    }
682
0
                }
683
0
            }
684
0
            else
685
0
            {
686
0
                int compno = state->alpha ? state->alpha_comp : 0;
687
0
                int bt=0;
688
0
                int ppbyte1 = 8/state->bpp;
689
                /* sampling required */
690
                /* only grayscale can have such bit-depth, also shift_bit = 0, bpp < 8 */
691
0
                for (i = 0; i < state->width; i++)
692
0
                {
693
0
                    for (b=0; b<ppbyte1; b++)
694
0
                    {
695
0
                        int dx = state->image->comps[compno].dx;
696
0
                        int dy = state->image->comps[compno].dy;
697
0
                        int w = state->image->comps[compno].w;
698
0
                        int in_offset_scaled = (y_offset/dy * w) + i / dx;
699
0
                        bt = bt<<state->bpp;
700
0
                        bt += state->image->comps[compno].data[in_offset_scaled] + state->sign_comps[compno];
701
0
                    }
702
0
                    *row++ = bt;
703
0
                }
704
0
            }
705
706
            /* 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 */
707
2.65M
            if ((state->image->color_space == OPJ_CLRSPC_SYCC || state->image->color_space == OPJ_CLRSPC_EYCC) && state->image->numcomps == 3)
708
0
            {
709
                /* bpp >= 8 always, as bpp < 8 only for grayscale */
710
0
                if (state->bpp == 8)
711
0
                    ycc_to_rgb_8(state->row_data, row_size);
712
0
                else
713
0
                    ycc_to_rgb_16(state->row_data, row_size);
714
0
            }
715
2.65M
        }
716
717
4.27M
        pw->ptr++;
718
4.27M
        il = (write_size > (unsigned long)(row_size - x_offset)) ? (row_size - x_offset) : (unsigned int)write_size;
719
4.27M
        memcpy(pw->ptr, &state->row_data[x_offset], il);
720
4.27M
        pw->ptr += il;
721
4.27M
        pw->ptr--;
722
4.27M
        state->out_offset += il;
723
4.27M
        write_size -= il;
724
4.27M
        if (write_size == 0)
725
1.97M
            break;
726
4.27M
    }
727
728
1.97M
    if (state->out_offset == state->totalbytes)
729
6.86k
        return EOFC; /* all data returned */
730
1.97M
    else
731
1.97M
        return 1; /* need more calls */
732
1.97M
}
733
734
/* process a section of the input and return any decoded data.
735
   see strimpl.h for return codes.
736
 */
737
static int
738
s_opjd_process(stream_state * ss, stream_cursor_read * pr,
739
                 stream_cursor_write * pw, bool last)
740
2.64M
{
741
2.64M
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
742
2.64M
    long in_size = pr->limit - pr->ptr;
743
2.64M
    int locked = 0;
744
2.64M
    int code;
745
746
2.64M
    if (in_size > 0)
747
658k
    {
748
658k
        if (state->PassThrough && state->PassThroughfn) {
749
167k
            if (state->PassThrough && state->PassThroughfn && !state->StartedPassThrough) {
750
3.39k
                state->StartedPassThrough = 1;
751
3.39k
                (state->PassThroughfn)(state->device, NULL, 1);
752
3.39k
            }
753
167k
            (state->PassThroughfn)(state->device, (byte *)pr->ptr + 1, (byte *)pr->limit - (byte *)pr->ptr);
754
167k
        }
755
756
        /* buffer available data */
757
658k
        code = opj_lock(ss->memory);
758
658k
        if (code < 0) return code;
759
658k
        locked = 1;
760
761
658k
        code = s_opjd_accumulate_input(state, pr);
762
658k
        if (code < 0) {
763
0
            (void)opj_unlock(ss->memory);
764
0
            return code;
765
0
        }
766
767
658k
        if (state->codec == NULL) {
768
            /* state->sb.size is non-zero after successful
769
               accumulate_input(); 1 is probably extremely rare */
770
17.8k
            if (state->sb.data[0] == 0xFF && ((state->sb.size == 1) || (state->sb.data[1] == 0x4F)))
771
18
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_J2K);
772
17.8k
            else
773
17.8k
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_JP2);
774
17.8k
            if (code < 0)
775
0
            {
776
0
                (void)opj_unlock(ss->memory);
777
0
                return code;
778
0
            }
779
17.8k
        }
780
658k
    }
781
782
2.64M
    if (last == 1)
783
1.98M
    {
784
1.98M
        if (state->image == NULL)
785
17.8k
        {
786
17.8k
            int ret;
787
788
17.8k
            if (locked == 0)
789
12
            {
790
12
                ret = opj_lock(ss->memory);
791
12
                if (ret < 0) return ret;
792
12
                locked = 1;
793
12
            }
794
795
17.8k
#if OPJ_VERSION_MAJOR >= 2 && OPJ_VERSION_MINOR >= 1
796
17.8k
            opj_stream_set_user_data(state->stream, &(state->sb), NULL);
797
#else
798
            opj_stream_set_user_data(state->stream, &(state->sb));
799
#endif
800
17.8k
            opj_stream_set_user_data_length(state->stream, state->sb.size);
801
17.8k
            ret = decode_image(state);
802
17.8k
            if (ret != 0)
803
10.9k
            {
804
10.9k
                (void)opj_unlock(ss->memory);
805
10.9k
                return ret;
806
10.9k
            }
807
17.8k
        }
808
809
1.97M
        if (locked)
810
6.99k
        {
811
6.99k
            code = opj_unlock(ss->memory);
812
6.99k
            if (code < 0) return code;
813
6.99k
        }
814
815
        /* copy out available data */
816
1.97M
        return process_one_trunk(state, pw);
817
818
1.97M
    }
819
820
657k
    if (locked)
821
640k
        return opj_unlock(ss->memory);
822
823
    /* ask for more data */
824
17.1k
    return 0;
825
657k
}
826
827
/* Set the defaults */
828
static void
829
37.0k
s_opjd_set_defaults(stream_state * ss) {
830
37.0k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
831
832
37.0k
    state->alpha = false;
833
37.0k
    state->colorspace = gs_jpx_cs_rgb;
834
37.0k
    state->StartedPassThrough = 0;
835
37.0k
    state->PassThrough = 0;
836
37.0k
    state->PassThroughfn = NULL;
837
37.0k
    state->device = (void *)NULL;
838
37.0k
}
839
840
/* stream release.
841
   free all our decoder state.
842
 */
843
static void
844
s_opjd_release(stream_state *ss)
845
18.5k
{
846
18.5k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
847
848
18.5k
    if (state->PassThrough && state->PassThroughfn && state->StartedPassThrough) {
849
3.39k
        state->StartedPassThrough = 0;
850
3.39k
        (state->PassThroughfn)(state->device, NULL, 0);
851
3.39k
    }
852
    /* empty stream or failed to accumulate */
853
18.5k
    if (state->codec == NULL)
854
631
        return;
855
856
17.8k
    (void)opj_lock(ss->memory);
857
858
    /* free image data structure */
859
17.8k
    if (state->image)
860
16.5k
        opj_image_destroy(state->image);
861
862
    /* free stream */
863
17.8k
    if (state->stream)
864
17.8k
        opj_stream_destroy(state->stream);
865
866
    /* free decoder handle */
867
17.8k
    if (state->codec)
868
17.8k
  opj_destroy_codec(state->codec);
869
870
17.8k
    (void)opj_unlock(ss->memory);
871
872
    /* free input buffer */
873
17.8k
    if (state->sb.data)
874
17.8k
        gs_free_object(state->memory->non_gc_memory, state->sb.data, "s_opjd_release(sb.data)");
875
876
17.8k
    if (state->pdata)
877
6.99k
        gs_free_object(state->memory->non_gc_memory, state->pdata, "s_opjd_release(pdata)");
878
879
17.8k
    if (state->sign_comps)
880
6.99k
        gs_free_object(state->memory->non_gc_memory, state->sign_comps, "s_opjd_release(sign_comps)");
881
882
17.8k
    if (state->row_data)
883
6.99k
        gs_free_object(state->memory->non_gc_memory, state->row_data, "s_opjd_release(row_data)");
884
17.8k
}
885
886
887
static int
888
s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr)
889
658k
{
890
658k
    long in_size = pr->limit - pr->ptr;
891
892
    /* grow the input buffer if needed */
893
658k
    if (state->sb.size < state->sb.fill + in_size)
894
82.9k
    {
895
82.9k
        unsigned char *new_buf;
896
82.9k
        unsigned long new_size = state->sb.size==0 ? in_size : state->sb.size;
897
898
148k
        while (new_size < state->sb.fill + in_size)
899
65.7k
            new_size = new_size << 1;
900
901
82.9k
        if_debug1('s', "[s]opj growing input buffer to %lu bytes\n",
902
82.9k
                new_size);
903
82.9k
        if (state->sb.data == NULL)
904
17.8k
            new_buf = (byte *) gs_alloc_byte_array(state->memory->non_gc_memory, new_size, 1, "s_opjd_accumulate_input(alloc)");
905
65.0k
        else
906
65.0k
            new_buf = (byte *) gs_resize_object(state->memory->non_gc_memory, state->sb.data, new_size, "s_opjd_accumulate_input(resize)");
907
82.9k
        if (new_buf == NULL) return_error( gs_error_VMerror);
908
909
82.9k
        state->sb.data = new_buf;
910
82.9k
        state->sb.size = new_size;
911
82.9k
    }
912
913
    /* copy the available input into our buffer */
914
    /* note that the gs stream library uses offset-by-one
915
        indexing of its buffers while we use zero indexing */
916
658k
    memcpy(state->sb.data + state->sb.fill, pr->ptr + 1, in_size);
917
658k
    state->sb.fill += in_size;
918
658k
    pr->ptr += in_size;
919
920
658k
    return 0;
921
658k
}
922
923
/* stream template */
924
const stream_template s_jpxd_template = {
925
    &st_jpxd_state,
926
    s_opjd_init,
927
    s_opjd_process,
928
    1024, 1024,   /* min in and out buffer sizes we can handle
929
                     should be ~32k,64k for efficiency? */
930
    s_opjd_release,
931
    s_opjd_set_defaults
932
};