Coverage Report

Created: 2025-08-28 07:06

/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
189k
{
38
189k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
39
189k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
40
41
189k
    ctx->sjpxd_private = gx_monitor_label(gx_monitor_alloc(mem), "sjpxd_monitor");
42
189k
    if (ctx->sjpxd_private == NULL)
43
0
        return gs_error_VMerror;
44
189k
#endif
45
189k
    return 0;
46
189k
}
47
48
void sjpxd_destroy(gs_memory_t *mem)
49
189k
{
50
189k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
51
189k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
52
53
189k
    gx_monitor_free((gx_monitor_t *)ctx->sjpxd_private);
54
189k
    ctx->sjpxd_private = NULL;
55
189k
#endif
56
189k
}
57
58
static int opj_lock(gs_memory_t *mem)
59
639k
{
60
639k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
61
639k
    int ret;
62
63
639k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
64
65
639k
    ret = gx_monitor_enter((gx_monitor_t *)ctx->sjpxd_private);
66
639k
    assert(opj_memory == NULL);
67
639k
    opj_memory = mem->non_gc_memory;
68
639k
    return ret;
69
#else
70
    return 0;
71
#endif
72
639k
}
73
74
static int opj_unlock(gs_memory_t *mem)
75
639k
{
76
639k
#if !defined(SHARE_JPX) || (SHARE_JPX == 0)
77
639k
    gs_lib_ctx_t *ctx = mem->gs_lib_ctx;
78
79
639k
    assert(opj_memory != NULL);
80
639k
    opj_memory = NULL;
81
639k
    return gx_monitor_leave((gx_monitor_t *)ctx->sjpxd_private);
82
#else
83
    return 0;
84
#endif
85
639k
}
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.7M
{
91
11.7M
    if (size == 0)
92
0
        return NULL;
93
94
11.7M
    assert(opj_memory != NULL);
95
96
11.7M
    if (size > (size_t) ARCH_MAX_UINT)
97
0
      return NULL;
98
99
11.7M
    return (void *)gs_alloc_bytes(opj_memory, size, "opj_malloc");
100
11.7M
}
101
102
void *opj_calloc(size_t n, size_t size)
103
7.06M
{
104
7.06M
    void *ptr;
105
106
    /* FIXME: Check for overflow? */
107
7.06M
    size *= n;
108
109
7.06M
    ptr = opj_malloc(size);
110
7.06M
    if (ptr)
111
7.06M
        memset(ptr, 0, size);
112
7.06M
    return ptr;
113
7.06M
}
114
115
void *opj_realloc(void *ptr, size_t size)
116
1.41M
{
117
1.41M
    if (ptr == NULL)
118
604k
        return opj_malloc(size);
119
120
812k
    if (size == 0)
121
0
    {
122
0
        opj_free(ptr);
123
0
        return NULL;
124
0
    }
125
126
812k
    return gs_resize_object(opj_memory, ptr, size, "opj_malloc");
127
812k
}
128
129
void opj_free(void *ptr)
130
11.8M
{
131
11.8M
    gs_free_object(opj_memory, ptr, "opj_malloc");
132
11.8M
}
133
134
static inline void * opj_aligned_malloc_n(size_t size, size_t align)
135
673k
{
136
673k
    uint8_t *ptr;
137
673k
    int off;
138
139
673k
    if (size == 0)
140
0
        return NULL;
141
142
673k
    size += align + sizeof(uint8_t);
143
673k
    ptr = opj_malloc(size);
144
673k
    if (ptr == NULL)
145
0
        return NULL;
146
673k
    off = align - (((int)(intptr_t)ptr) & (align - 1));
147
673k
    ptr[off-1] = off;
148
673k
    return ptr + off;
149
673k
}
150
151
void * opj_aligned_malloc(size_t size)
152
670k
{
153
670k
    return opj_aligned_malloc_n(size, 16);
154
670k
}
155
156
void *opj_aligned_32_malloc(size_t size)
157
3.20k
{
158
3.20k
    return opj_aligned_malloc_n(size, 32);
159
3.20k
}
160
161
void opj_aligned_free(void* ptr_)
162
4.06M
{
163
4.06M
    uint8_t *ptr = (uint8_t *)ptr_;
164
4.06M
    uint8_t off;
165
4.06M
    if (ptr == NULL)
166
3.39M
        return;
167
168
673k
    off = ptr[-1];
169
673k
    opj_free((void *)(((unsigned char *)ptr) - off));
170
673k
}
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
23.4k
{
189
23.4k
  stream_block *sb = (stream_block *)p_user_data;
190
23.4k
  OPJ_SIZE_T len;
191
192
23.4k
  len = sb->fill - sb->pos;
193
23.4k
        if (sb->fill < sb->pos)
194
0
    len = 0;
195
23.4k
  if (len == 0)
196
1.71k
    return (OPJ_SIZE_T)-1;  /* End of file! */
197
21.7k
  if ((OPJ_SIZE_T)len > p_nb_bytes)
198
0
    len = p_nb_bytes;
199
21.7k
  memcpy(p_buffer, sb->data + sb->pos, len);
200
21.7k
  sb->pos += len;
201
21.7k
  return len;
202
23.4k
}
203
204
static OPJ_OFF_T sjpx_stream_skip(OPJ_OFF_T skip, void * p_user_data)
205
22
{
206
22
  stream_block *sb = (stream_block *)p_user_data;
207
208
22
  if (skip > sb->fill - sb->pos)
209
22
    skip = sb->fill - sb->pos;
210
22
  sb->pos += skip;
211
22
  return sb->pos;
212
22
}
213
214
static OPJ_BOOL sjpx_stream_seek(OPJ_OFF_T seek_pos, void * p_user_data)
215
6.51k
{
216
6.51k
  stream_block *sb = (stream_block *)p_user_data;
217
218
6.51k
  if (seek_pos > sb->fill)
219
103
    return OPJ_FALSE;
220
6.41k
  sb->pos = seek_pos;
221
6.41k
  return OPJ_TRUE;
222
6.51k
}
223
224
static void sjpx_error_callback(const char *msg, void *ptr)
225
26.3k
{
226
26.3k
  dlprintf1("openjpeg error: %s", msg);
227
26.3k
}
228
229
static void sjpx_info_callback(const char *msg, void *ptr)
230
107k
{
231
#ifdef DEBUG
232
  /* prevent too many messages during normal build */
233
  dlprintf1("openjpeg info: %s", msg);
234
#endif
235
107k
}
236
237
static void sjpx_warning_callback(const char *msg, void *ptr)
238
667
{
239
#ifdef DEBUG
240
  /* prevent too many messages during normal build */
241
  dlprintf1("openjpeg warning: %s", msg);
242
#endif
243
667
}
244
245
/* initialize the stream */
246
static int
247
s_opjd_init(stream_state * ss)
248
16.2k
{
249
16.2k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
250
16.2k
    state->codec = NULL;
251
252
16.2k
    state->image = NULL;
253
16.2k
    state->sb.data= NULL;
254
16.2k
    state->sb.size = 0;
255
16.2k
    state->sb.pos = 0;
256
16.2k
    state->sb.fill = 0;
257
16.2k
    state->out_offset = 0;
258
16.2k
    state->pdata = NULL;
259
16.2k
    state->sign_comps = NULL;
260
16.2k
    state->stream = NULL;
261
16.2k
    state->row_data = NULL;
262
263
16.2k
    return 0;
264
16.2k
}
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
15.6k
{
273
15.6k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
274
15.6k
    opj_dparameters_t parameters; /* decompression parameters */
275
276
    /* set decoding parameters to default values */
277
15.6k
    opj_set_default_decoder_parameters(&parameters);
278
279
    /* get a decoder handle */
280
15.6k
    state->codec = opj_create_decompress(format);
281
15.6k
    if (state->codec == NULL)
282
0
        return_error(gs_error_VMerror);
283
284
    /* catch events using our callbacks */
285
15.6k
    opj_set_error_handler(state->codec, sjpx_error_callback, stderr);
286
15.6k
    opj_set_info_handler(state->codec, sjpx_info_callback, stderr);
287
15.6k
    opj_set_warning_handler(state->codec, sjpx_warning_callback, stderr);
288
289
15.6k
    if (state->colorspace == gs_jpx_cs_indexed) {
290
26
        parameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
291
26
    }
292
293
    /* setup the decoder decoding parameters using user parameters */
294
15.6k
    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
15.6k
    state->stream = opj_stream_default_create(OPJ_TRUE);
302
15.6k
    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
15.6k
    opj_stream_set_read_function(state->stream, sjpx_stream_read);
309
15.6k
    opj_stream_set_skip_function(state->stream, sjpx_stream_skip);
310
15.6k
    opj_stream_set_seek_function(state->stream, sjpx_stream_seek);
311
312
15.6k
    return 0;
313
15.6k
}
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
15.6k
{
409
15.6k
    int numprimcomp = 0, alpha_comp = -1, compno, rowbytes, bps;
410
411
    /* read header */
412
15.6k
    if (!opj_read_header(state->stream, state->codec, &(state->image)))
413
1.21k
    {
414
1.21k
      dlprintf("openjpeg: failed to read header\n");
415
1.21k
      return ERRC;
416
1.21k
    }
417
418
    /* decode the stream and fill the image structure */
419
14.4k
    if (!opj_decode(state->codec, state->stream, state->image))
420
8.46k
    {
421
8.46k
        dlprintf("openjpeg: failed to decode image!\n");
422
8.46k
        return ERRC;
423
8.46k
    }
424
425
    /* check dimension and prec */
426
6.00k
    if (state->image->numcomps == 0)
427
0
        return ERRC;
428
429
6.00k
    state->width = state->image->comps[0].w;
430
6.00k
    state->height = state->image->comps[0].h;
431
6.00k
    state->bpp = state->image->comps[0].prec;
432
6.00k
    state->samescale = true;
433
12.8k
    for(compno = 1; compno < state->image->numcomps; compno++)
434
6.86k
    {
435
6.86k
        if (state->bpp != state->image->comps[compno].prec)
436
0
            return ERRC; /* Not supported. */
437
6.86k
        if (state->width < state->image->comps[compno].w)
438
0
            state->width = state->image->comps[compno].w;
439
6.86k
        if (state->height < state->image->comps[compno].h)
440
0
            state->height = state->image->comps[compno].h;
441
6.86k
        if (state->image->comps[compno].dx != state->image->comps[0].dx ||
442
6.86k
                state->image->comps[compno].dy != state->image->comps[0].dy)
443
0
            state->samescale = false;
444
6.86k
    }
445
446
    /* find alpha component and regular colour component by channel definition */
447
18.8k
    for (compno = 0; compno < state->image->numcomps; compno++)
448
12.8k
    {
449
12.8k
        if (state->image->comps[compno].alpha == 0x00)
450
12.8k
            numprimcomp++;
451
6
        else if (state->image->comps[compno].alpha == 0x01 || state->image->comps[compno].alpha == 0x02)
452
6
            alpha_comp = compno;
453
12.8k
    }
454
455
    /* color space and number of components */
456
6.00k
    switch(state->image->color_space)
457
6.00k
    {
458
2.55k
        case OPJ_CLRSPC_GRAY:
459
2.55k
            state->colorspace = gs_jpx_cs_gray;
460
2.55k
            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.55k
            break;
465
3.43k
        case OPJ_CLRSPC_SRGB:
466
3.43k
        case OPJ_CLRSPC_SYCC:
467
3.44k
        case OPJ_CLRSPC_EYCC:
468
3.44k
            state->colorspace = gs_jpx_cs_rgb;
469
3.44k
            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
3.44k
           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
6.00k
    }
498
499
6.00k
    state->alpha_comp = -1;
500
6.00k
    if (state->alpha)
501
0
    {
502
0
        state->alpha_comp = alpha_comp;
503
0
        state->out_numcomps = 1;
504
0
    }
505
6.00k
    else
506
6.00k
        state->out_numcomps = numprimcomp;
507
508
    /* round up bpp 12->16 */
509
6.00k
    if (state->bpp == 12)
510
0
        state->bpp = 16;
511
512
    /* calculate  total data */
513
6.00k
    bps = state->bpp * state->out_numcomps;
514
515
    /* Overflow check, almost certainly superfluous, but let's be certain */
516
6.00k
    if (state->out_numcomps != 0 && bps / state->out_numcomps != state->bpp)
517
0
        return_error(gs_error_rangecheck);
518
519
6.00k
    rowbytes = state->width * bps;
520
    /* Overflow check */
521
6.00k
    if ((bps != 0 && rowbytes / bps != state->width) || rowbytes > max_int - 8)
522
0
        return_error(gs_error_rangecheck);
523
524
6.00k
    rowbytes = (rowbytes + 7) / 8;
525
526
6.00k
    state->totalbytes = (ulong)rowbytes*state->height;
527
    /* Overflow check */
528
6.00k
    if (rowbytes != 0 && state->totalbytes / rowbytes != state->height)
529
0
        return_error(gs_error_rangecheck);
530
531
6.00k
    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.00k
    if (!state->pdata)
533
0
        return_error(gs_error_VMerror);
534
535
    /* compensate for signed data (signed => unsigned) */
536
6.00k
    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.00k
    if (!state->sign_comps)
538
0
        return_error(gs_error_VMerror);
539
540
18.8k
    for(compno = 0; compno < state->image->numcomps; compno++)
541
12.8k
    {
542
12.8k
        if (state->image->comps[compno].sgnd)
543
0
            state->sign_comps[compno] = ((state->bpp%8)==0) ? 0x80 : (1<<(state->bpp-1));
544
12.8k
        else
545
12.8k
            state->sign_comps[compno] = 0;
546
12.8k
    }
547
548
6.00k
    return 0;
549
6.00k
}
550
551
static int process_one_trunk(stream_jpxd_state * const state, stream_cursor_write * pw)
552
1.73M
{
553
    /* read data from image to pw */
554
1.73M
    unsigned long out_size = pw->limit - pw->ptr;
555
1.73M
    int bytepp1 = state->bpp/8; /* bytes / pixel for one output component */
556
1.73M
    int bytepp = state->out_numcomps*state->bpp/8; /* bytes / pixel all components */
557
1.73M
    unsigned long write_size = min(out_size-(bytepp?(out_size%bytepp):0), state->totalbytes-state->out_offset);
558
1.73M
    int shift_bit = state->bpp-state->image->comps[0].prec; /*difference between input and output bit-depth*/
559
1.73M
    int img_numcomps = min(state->out_numcomps, state->image->numcomps); /* the actual number of channel data used */
560
1.73M
    int compno;
561
1.73M
    unsigned long il;
562
1.73M
    int i;
563
1.73M
    int b;
564
1.73M
    byte *row;
565
1.73M
    unsigned int x_offset;
566
1.73M
    unsigned int y_offset;
567
1.73M
    unsigned int row_size = (state->width * state->out_numcomps * state->bpp + 7)>>3;
568
569
    /* If nothing to write, nothing to do */
570
1.73M
    if (write_size == 0)
571
0
        return 0;
572
573
1.73M
    if (state->row_data == NULL)
574
6.00k
    {
575
6.00k
        state->row_data = gs_alloc_byte_array(state->memory->non_gc_memory, row_size, 1, "jpxd_openjpeg(row_data)");
576
6.00k
        if (state->row_data == NULL)
577
0
            return gs_error_VMerror;
578
6.00k
    }
579
580
3.75M
    while (state->out_offset != state->totalbytes)
581
3.75M
    {
582
3.75M
        y_offset = state->out_offset / row_size;
583
3.75M
        x_offset = state->out_offset % row_size;
584
585
3.75M
        if (x_offset == 0)
586
2.34M
        {
587
            /* Decode another rows worth */
588
2.34M
            row = state->row_data;
589
2.34M
            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.34M
            else if (state->samescale)
595
2.34M
            {
596
2.34M
                if (state->alpha)
597
0
                    state->pdata[0] = &(state->image->comps[state->alpha_comp].data[y_offset * state->width]);
598
2.34M
                else
599
2.34M
                {
600
7.43M
                    for (compno=0; compno<img_numcomps; compno++)
601
5.09M
                        state->pdata[compno] = &(state->image->comps[compno].data[y_offset * state->width]);
602
2.34M
                }
603
2.34M
                if (shift_bit == 0 && state->bpp == 8) /* optimized for the most common case */
604
2.34M
                {
605
834M
                    for (i = state->width; i > 0; i--)
606
2.64G
                        for (compno=0; compno<img_numcomps; compno++)
607
1.81G
                            *row++ = *(state->pdata[compno]++) + state->sign_comps[compno]; /* copy input buffer to output */
608
2.34M
                }
609
1.97k
                else if ((state->bpp%8)==0)
610
1.01k
                {
611
560k
                    for (i = state->width; i > 0; i--)
612
559k
                    {
613
1.11M
                        for (compno=0; compno<img_numcomps; compno++)
614
559k
                        {
615
1.67M
                            for (b=0; b<bytepp1; b++)
616
1.11M
                                *row++ = (((*(state->pdata[compno]) << shift_bit) >> (8*(bytepp1-b-1))))
617
1.11M
                                                        + (b==0 ? state->sign_comps[compno] : 0); /* split and shift input int to output bytes */
618
559k
                            state->pdata[compno]++;
619
559k
                        }
620
559k
                    }
621
1.01k
                }
622
962
                else
623
962
                {
624
                    /* shift_bit = 0, bpp < 8 */
625
962
                    int bt=0;
626
962
                    int bit_pos = 0;
627
658k
                    for (i = state->width; i > 0; i--)
628
658k
                    {
629
1.31M
                        for (compno=0; compno<img_numcomps; compno++)
630
658k
                        {
631
658k
                            bt <<= state->bpp;
632
658k
                            bt += *(state->pdata[compno]++) + state->sign_comps[compno];
633
658k
                            bit_pos += state->bpp;
634
658k
                            if (bit_pos >= 8)
635
329k
                            {
636
329k
                                *row++ = bt >> (bit_pos-8);
637
329k
                                bit_pos -= 8;
638
329k
                                bt &= (1<<bit_pos)-1;
639
329k
                            }
640
658k
                        }
641
658k
                    }
642
962
                    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
962
                }
650
2.34M
            }
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.34M
            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.34M
        }
716
717
3.75M
        pw->ptr++;
718
3.75M
        il = (write_size > (unsigned long)(row_size - x_offset)) ? (row_size - x_offset) : (unsigned int)write_size;
719
3.75M
        memcpy(pw->ptr, &state->row_data[x_offset], il);
720
3.75M
        pw->ptr += il;
721
3.75M
        pw->ptr--;
722
3.75M
        state->out_offset += il;
723
3.75M
        write_size -= il;
724
3.75M
        if (write_size == 0)
725
1.73M
            break;
726
3.75M
    }
727
728
1.73M
    if (state->out_offset == state->totalbytes)
729
5.97k
        return EOFC; /* all data returned */
730
1.73M
    else
731
1.73M
        return 1; /* need more calls */
732
1.73M
}
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.37M
{
741
2.37M
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
742
2.37M
    long in_size = pr->limit - pr->ptr;
743
2.37M
    int locked = 0;
744
2.37M
    int code;
745
746
2.37M
    if (in_size > 0)
747
623k
    {
748
623k
        if (state->PassThrough && state->PassThroughfn) {
749
170k
            if (state->PassThrough && state->PassThroughfn && !state->StartedPassThrough) {
750
3.31k
                state->StartedPassThrough = 1;
751
3.31k
                (state->PassThroughfn)(state->device, NULL, 1);
752
3.31k
            }
753
170k
            (state->PassThroughfn)(state->device, (byte *)pr->ptr + 1, (byte *)pr->limit - (byte *)pr->ptr);
754
170k
        }
755
756
        /* buffer available data */
757
623k
        code = opj_lock(ss->memory);
758
623k
        if (code < 0) return code;
759
623k
        locked = 1;
760
761
623k
        code = s_opjd_accumulate_input(state, pr);
762
623k
        if (code < 0) {
763
0
            (void)opj_unlock(ss->memory);
764
0
            return code;
765
0
        }
766
767
623k
        if (state->codec == NULL) {
768
            /* state->sb.size is non-zero after successful
769
               accumulate_input(); 1 is probably extremely rare */
770
15.6k
            if (state->sb.data[0] == 0xFF && ((state->sb.size == 1) || (state->sb.data[1] == 0x4F)))
771
12
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_J2K);
772
15.6k
            else
773
15.6k
                code = s_opjd_set_codec_format(ss, OPJ_CODEC_JP2);
774
15.6k
            if (code < 0)
775
0
            {
776
0
                (void)opj_unlock(ss->memory);
777
0
                return code;
778
0
            }
779
15.6k
        }
780
623k
    }
781
782
2.37M
    if (last == 1)
783
1.74M
    {
784
1.74M
        if (state->image == NULL)
785
15.6k
        {
786
15.6k
            int ret;
787
788
15.6k
            if (locked == 0)
789
11
            {
790
11
                ret = opj_lock(ss->memory);
791
11
                if (ret < 0) return ret;
792
11
                locked = 1;
793
11
            }
794
795
15.6k
#if OPJ_VERSION_MAJOR >= 2 && OPJ_VERSION_MINOR >= 1
796
15.6k
            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
15.6k
            opj_stream_set_user_data_length(state->stream, state->sb.size);
801
15.6k
            ret = decode_image(state);
802
15.6k
            if (ret != 0)
803
9.67k
            {
804
9.67k
                (void)opj_unlock(ss->memory);
805
9.67k
                return ret;
806
9.67k
            }
807
15.6k
        }
808
809
1.73M
        if (locked)
810
6.00k
        {
811
6.00k
            code = opj_unlock(ss->memory);
812
6.00k
            if (code < 0) return code;
813
6.00k
        }
814
815
        /* copy out available data */
816
1.73M
        return process_one_trunk(state, pw);
817
818
1.73M
    }
819
820
622k
    if (locked)
821
607k
        return opj_unlock(ss->memory);
822
823
    /* ask for more data */
824
14.9k
    return 0;
825
622k
}
826
827
/* Set the defaults */
828
static void
829
32.5k
s_opjd_set_defaults(stream_state * ss) {
830
32.5k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
831
832
32.5k
    state->alpha = false;
833
32.5k
    state->colorspace = gs_jpx_cs_rgb;
834
32.5k
    state->StartedPassThrough = 0;
835
32.5k
    state->PassThrough = 0;
836
32.5k
    state->PassThroughfn = NULL;
837
32.5k
    state->device = (void *)NULL;
838
32.5k
}
839
840
/* stream release.
841
   free all our decoder state.
842
 */
843
static void
844
s_opjd_release(stream_state *ss)
845
16.2k
{
846
16.2k
    stream_jpxd_state *const state = (stream_jpxd_state *) ss;
847
848
16.2k
    if (state->PassThrough && state->PassThroughfn && state->StartedPassThrough) {
849
3.31k
        state->StartedPassThrough = 0;
850
3.31k
        (state->PassThroughfn)(state->device, NULL, 0);
851
3.31k
    }
852
    /* empty stream or failed to accumulate */
853
16.2k
    if (state->codec == NULL)
854
597
        return;
855
856
15.6k
    (void)opj_lock(ss->memory);
857
858
    /* free image data structure */
859
15.6k
    if (state->image)
860
14.4k
        opj_image_destroy(state->image);
861
862
    /* free stream */
863
15.6k
    if (state->stream)
864
15.6k
        opj_stream_destroy(state->stream);
865
866
    /* free decoder handle */
867
15.6k
    if (state->codec)
868
15.6k
  opj_destroy_codec(state->codec);
869
870
15.6k
    (void)opj_unlock(ss->memory);
871
872
    /* free input buffer */
873
15.6k
    if (state->sb.data)
874
15.6k
        gs_free_object(state->memory->non_gc_memory, state->sb.data, "s_opjd_release(sb.data)");
875
876
15.6k
    if (state->pdata)
877
6.00k
        gs_free_object(state->memory->non_gc_memory, state->pdata, "s_opjd_release(pdata)");
878
879
15.6k
    if (state->sign_comps)
880
6.00k
        gs_free_object(state->memory->non_gc_memory, state->sign_comps, "s_opjd_release(sign_comps)");
881
882
15.6k
    if (state->row_data)
883
6.00k
        gs_free_object(state->memory->non_gc_memory, state->row_data, "s_opjd_release(row_data)");
884
15.6k
}
885
886
887
static int
888
s_opjd_accumulate_input(stream_jpxd_state *state, stream_cursor_read * pr)
889
623k
{
890
623k
    long in_size = pr->limit - pr->ptr;
891
892
    /* grow the input buffer if needed */
893
623k
    if (state->sb.size < state->sb.fill + in_size)
894
74.9k
    {
895
74.9k
        unsigned char *new_buf;
896
74.9k
        unsigned long new_size = state->sb.size==0 ? in_size : state->sb.size;
897
898
134k
        while (new_size < state->sb.fill + in_size)
899
59.9k
            new_size = new_size << 1;
900
901
74.9k
        if_debug1('s', "[s]opj growing input buffer to %lu bytes\n",
902
74.9k
                new_size);
903
74.9k
        if (state->sb.data == NULL)
904
15.6k
            new_buf = (byte *) gs_alloc_byte_array(state->memory->non_gc_memory, new_size, 1, "s_opjd_accumulate_input(alloc)");
905
59.3k
        else
906
59.3k
            new_buf = (byte *) gs_resize_object(state->memory->non_gc_memory, state->sb.data, new_size, "s_opjd_accumulate_input(resize)");
907
74.9k
        if (new_buf == NULL) return_error( gs_error_VMerror);
908
909
74.9k
        state->sb.data = new_buf;
910
74.9k
        state->sb.size = new_size;
911
74.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
623k
    memcpy(state->sb.data + state->sb.fill, pr->ptr + 1, in_size);
917
623k
    state->sb.fill += in_size;
918
623k
    pr->ptr += in_size;
919
920
623k
    return 0;
921
623k
}
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
};