Coverage Report

Created: 2025-06-10 07:27

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