Coverage Report

Created: 2026-02-14 07:09

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