Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/gpdl/jpgtop.c
Line
Count
Source
1
/* Copyright (C) 2019-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
/* jpgtop.c */
17
/* Top-level API implementation of "JPG" Language Interface */
18
19
#include "pltop.h"
20
#include "gserrors.h"
21
#include "gxdevice.h"
22
#include "gsstate.h"
23
#include "strimpl.h"
24
#include "gscoord.h"
25
#include "gsicc_manage.h"
26
#include "gspaint.h"
27
#include "plmain.h"
28
#include "jpeglib.h"
29
#include "setjmp_.h"
30
#include "sjpeg.h"
31
32
/* Forward decls */
33
34
/************************************************************/
35
/******** Language wrapper implementation (see pltop.h) *****/
36
/************************************************************/
37
38
typedef enum
39
{
40
    ii_state_identifying = 0,
41
    ii_state_jpeg,
42
    ii_state_jpeg_header,
43
    ii_state_jpeg_start,
44
    ii_state_jpeg_rows,
45
    ii_state_jpeg_finish,
46
    ii_state_flush
47
} ii_state;
48
49
typedef struct {
50
    struct jpeg_error_mgr pub;
51
    jmp_buf *setjmp_buffer;
52
} jpg_error_mgr;
53
54
/*
55
 * Jpg interpreter instance
56
 */
57
typedef struct jpg_interp_instance_s {
58
    gs_memory_t       *memory;
59
    gs_memory_t       *cmemory;
60
    gx_device         *dev;
61
    gx_device         *nulldev;
62
63
    gs_color_space    *gray;
64
    gs_color_space    *rgb;
65
    gs_color_space    *cmyk;
66
67
    /* Jpg parser state machine */
68
    ii_state           state;
69
70
    int                pages;
71
72
    uint8_t            bpp;
73
    uint8_t            cs;
74
    uint32_t           width;
75
    uint32_t           height;
76
    uint32_t           xresolution;
77
    uint32_t           yresolution;
78
79
    uint32_t           num_comps;
80
    uint32_t           byte_width;
81
    uint32_t           y;
82
83
    uint32_t           bytes_available_on_entry;
84
85
    gs_image_t         image;
86
    gs_image_enum     *penum;
87
    gs_gstate         *pgs;
88
89
    struct jpeg_decompress_struct cinfo;
90
    struct jpeg_source_mgr        jsrc;
91
    size_t             bytes_to_skip;
92
    jpg_error_mgr      jerr;
93
94
    struct
95
    {
96
        jmp_buf setjmp_buffer;
97
        unsigned char pad[16];
98
    } aligned_jmpbuf;
99
100
101
    byte              *samples;
102
103
} jpg_interp_instance_t;
104
105
static int
106
jpg_detect_language(const char *s, int len)
107
34.2k
{
108
34.2k
    const byte *hdr = (const byte *)s;
109
34.2k
    if (len >= 11) {
110
33.5k
        if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
111
179
            strncmp("JFIF", s+6, 5) == 0)
112
166
            return 100;
113
33.5k
    }
114
115
34.0k
    return 0;
116
34.2k
}
117
118
static const pl_interp_characteristics_t jpg_characteristics = {
119
    "JPG",
120
    jpg_detect_language,
121
};
122
123
/* GS's fakakta jpeg integration insists on putting a
124
 * memory structure pointer in the decompress structs client data.
125
 * This is no good to find our instance from. We therefore find
126
 * it by offsetting back from the address of the cinfo. This is
127
 * a nasty bit of casting, so wrap it in a macro. */
128
#define JPG_FROM_CINFO(CINFO) \
129
1.57k
    (jpg_interp_instance_t *)(((char *)(CINFO))-offsetof(jpg_interp_instance_t,cinfo))
130
131
132
/* Get implementation's characteristics */
133
static const pl_interp_characteristics_t * /* always returns a descriptor */
134
jpg_impl_characteristics(const pl_interp_implementation_t *impl)     /* implementation of interpreter to alloc */
135
72.2k
{
136
72.2k
  return &jpg_characteristics;
137
72.2k
}
138
139
static void
140
jpg_deallocate(jpg_interp_instance_t *jpg)
141
18.4k
{
142
18.4k
    if (jpg == NULL)
143
0
        return;
144
145
18.4k
    rc_decrement_cs(jpg->gray, "jpg_deallocate");
146
18.4k
    rc_decrement_cs(jpg->rgb, "jpg_deallocate");
147
18.4k
    rc_decrement_cs(jpg->cmyk, "jpg_deallocate");
148
149
18.4k
    if (jpg->pgs != NULL)
150
18.4k
        gs_gstate_free_chain(jpg->pgs);
151
18.4k
    gs_free_object(jpg->memory, jpg, "jpg_impl_allocate_interp_instance");
152
18.4k
}
153
154
/* Deallocate a interpreter instance */
155
static int
156
jpg_impl_deallocate_interp_instance(pl_interp_implementation_t *impl)
157
18.4k
{
158
18.4k
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
159
160
18.4k
    jpg_deallocate(jpg);
161
18.4k
    impl->interp_client_data = NULL;
162
163
18.4k
    return 0;
164
18.4k
}
165
166
/* Do per-instance interpreter allocation/init. */
167
static int
168
jpg_impl_allocate_interp_instance(pl_interp_implementation_t *impl, gs_memory_t *mem)
169
18.4k
{
170
18.4k
    int code;
171
18.4k
    jpg_interp_instance_t *jpg
172
18.4k
        = (jpg_interp_instance_t *)gs_alloc_bytes(mem,
173
18.4k
                                                  sizeof(jpg_interp_instance_t),
174
18.4k
                                                  "jpg_impl_allocate_interp_instance");
175
18.4k
    if (!jpg)
176
0
        return_error(gs_error_VMerror);
177
18.4k
    memset(jpg, 0, sizeof(*jpg));
178
179
18.4k
    jpg->memory = mem;
180
18.4k
    jpg->pgs = gs_gstate_alloc(mem);
181
18.4k
    if (jpg->pgs == NULL)
182
0
        goto failVM;
183
184
    /* Push one save level onto the stack to assuage the memory handling */
185
18.4k
    code = gs_gsave(jpg->pgs);
186
18.4k
    if (code < 0)
187
0
        goto fail;
188
189
18.4k
    code = gsicc_init_iccmanager(jpg->pgs);
190
18.4k
    if (code < 0)
191
0
        goto fail;
192
193
18.4k
    jpg->gray = gs_cspace_new_ICC(mem, jpg->pgs, 1);
194
18.4k
    jpg->rgb  = gs_cspace_new_ICC(mem, jpg->pgs, 3);
195
18.4k
    jpg->cmyk = gs_cspace_new_ICC(mem, jpg->pgs, 4);
196
197
18.4k
    impl->interp_client_data = jpg;
198
199
18.4k
    return 0;
200
201
0
failVM:
202
0
    code = gs_note_error(gs_error_VMerror);
203
0
fail:
204
0
    (void)jpg_deallocate(jpg);
205
0
    return code;
206
0
}
207
208
/*
209
 * Get the allocator with which to allocate a device
210
 */
211
static gs_memory_t *
212
jpg_impl_get_device_memory(pl_interp_implementation_t *impl)
213
0
{
214
0
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
215
216
0
    return jpg->dev ? jpg->dev->memory : NULL;
217
0
}
218
219
#if 0 /* UNUSED */
220
static int
221
jpg_impl_set_param(pl_interp_implementation_t *impl,
222
                   pl_set_param_type           type,
223
                   const char                 *param,
224
                   const void                 *val)
225
{
226
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
227
228
    /* No params set here */
229
    return 0;
230
}
231
232
static int
233
jpg_impl_add_path(pl_interp_implementation_t *impl,
234
                  const char                 *path)
235
{
236
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
237
238
    /* No paths to add */
239
    return 0;
240
}
241
242
static int
243
jpg_impl_post_args_init(pl_interp_implementation_t *impl)
244
{
245
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
246
247
    /* No post args processing */
248
    return 0;
249
}
250
#endif
251
252
/* Prepare interp instance for the next "job" */
253
static int
254
jpg_impl_init_job(pl_interp_implementation_t *impl,
255
                  gx_device                  *device)
256
166
{
257
166
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
258
259
166
    jpg->dev = device;
260
166
    jpg->state = ii_state_identifying;
261
262
166
    return 0;
263
166
}
264
265
#if 0 /* UNUSED */
266
static int
267
jpg_impl_run_prefix_commands(pl_interp_implementation_t *impl,
268
                             const char                 *prefix)
269
{
270
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
271
272
    return 0;
273
}
274
275
static int
276
jpg_impl_process_file(pl_interp_implementation_t *impl, const char *filename)
277
{
278
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
279
280
    return 0;
281
}
282
#endif
283
284
/* Do any setup for parser per-cursor */
285
static int                      /* ret 0 or +ve if ok, else -ve error code */
286
jpg_impl_process_begin(pl_interp_implementation_t * impl)
287
166
{
288
166
    return 0;
289
166
}
290
291
/* Ensure we have 'required' bytes to read, and further ensure
292
 * that we have no UEL's within those bytes. */
293
static int
294
ensure_bytes(jpg_interp_instance_t *jpg, stream_cursor_read *pr, int required)
295
166
{
296
166
    int n;
297
166
    const uint8_t *p = pr->ptr+1;
298
166
    const uint8_t *q;
299
166
    int avail;
300
301
    /* Find out how many bytes we need to check */
302
166
    n = pr->limit - pr->ptr;
303
166
    if (n > required)
304
166
        n = required;
305
306
    /* Make sure there are no UELs in that block */
307
166
    q = p + n;
308
166
    while (p != q) {
309
1.99k
        while (p != q && *p != '\033')
310
1.82k
            p++;
311
166
        if (p == q)
312
166
            break;
313
0
        avail = pr->limit - pr->ptr;
314
0
        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
315
            /* At least a partial match to a UEL */
316
0
            return avail < 9 ? gs_error_NeedInput : gs_error_InterpreterExit;
317
0
        }
318
0
        p++;
319
0
    }
320
321
    /* If we have enough bytes, great, if not, get some more */
322
166
    return (n < required) ? gs_error_NeedInput : 0;
323
166
}
324
325
static int
326
flush_to_uel(stream_cursor_read *pr)
327
355
{
328
355
    const uint8_t *p = pr->ptr+1;
329
355
    const uint8_t *q = pr->limit+1;
330
355
    int avail;
331
332
1.29k
    while (p != q) {
333
548k
        while (p != q && *p != '\033')
334
547k
            p++;
335
1.27k
        if (p == q)
336
340
            break;
337
939
        avail = pr->limit - pr->ptr;
338
939
        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
339
            /* At least a partial match to a UEL. Bin everything to
340
             * the start of the match. */
341
0
            pr->ptr = p-1;
342
0
            if (avail == 9) /* Complete match. Exit! */
343
0
                return gs_error_InterpreterExit;
344
            /* Partial match. Get more data. */
345
0
            return gs_error_NeedInput;
346
0
        }
347
939
        p++;
348
939
    }
349
350
355
    pr->ptr = pr->limit;
351
352
355
    return 0;
353
355
}
354
355
static int
356
bytes_until_uel(const stream_cursor_read *pr)
357
8.90k
{
358
8.90k
    const uint8_t *p = pr->ptr+1;
359
8.90k
    const uint8_t *q = pr->limit+1;
360
8.90k
    int avail;
361
362
9.88k
    while (p != q) {
363
10.4M
        while (p != q && *p != '\033')
364
10.3M
            p++;
365
9.28k
        if (p == q)
366
8.30k
            break;
367
980
        avail = q - p;
368
980
        if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
369
            /* At least a partial match to a UEL. Everything up to
370
             * the start of the match is up for grabs. */
371
2
            return p - (pr->ptr+1);
372
2
        }
373
978
        p++;
374
978
    }
375
376
8.90k
    return pr->limit - pr->ptr;
377
8.90k
}
378
379
static void
380
jpg_jpeg_init_source(j_decompress_ptr cinfo)
381
166
{
382
    /* We've already inited the source */
383
166
}
384
385
static boolean
386
jpg_fill_input_buffer(j_decompress_ptr cinfo)
387
654
{
388
654
    return FALSE; /* We've filled the buffer already */
389
654
}
390
391
static void
392
jpg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
393
1.57k
{
394
1.57k
    jpg_interp_instance_t *jpg = JPG_FROM_CINFO(cinfo);
395
1.57k
    size_t n = cinfo->src->bytes_in_buffer;
396
397
1.57k
    if (n > num_bytes)
398
1.51k
        n = num_bytes;
399
1.57k
    jpg->jsrc.next_input_byte += num_bytes;
400
1.57k
    jpg->jsrc.bytes_in_buffer -= n;
401
1.57k
}
402
403
static boolean
404
jpg_resync_to_restart(j_decompress_ptr cinfo, int desired)
405
1
{
406
1
    return FALSE;
407
1
}
408
409
static void
410
jpg_term_source(j_decompress_ptr cinfo)
411
18
{
412
18
}
413
414
OPTIMIZE_SETJMP
415
static void
416
jpg_error_exit(j_common_ptr cinfo)
417
76
{
418
76
    jpg_error_mgr *jerr = (jpg_error_mgr *)cinfo->err;
419
420
76
    longjmp(*jerr->setjmp_buffer, 1);
421
76
}
422
423
static int
424
fill_jpeg_source(jpg_interp_instance_t *jpg, stream_cursor_read * pr)
425
8.90k
{
426
8.90k
    size_t n = bytes_until_uel(pr);
427
8.90k
    size_t skip = jpg->bytes_to_skip;
428
429
    /* Skip any bytes we are supposed to be skipping. */
430
8.90k
    if (skip > 0) {
431
151
        if (skip > n)
432
117
            skip = n;
433
151
        pr->ptr += skip;
434
151
        n -= skip;
435
151
        jpg->bytes_to_skip -= skip;
436
151
        if (jpg->bytes_to_skip != 0)
437
117
            return 1; /* Still more to skip */
438
151
    }
439
440
    /* Set up for the call into the jpeg lib */
441
8.79k
    jpg->jsrc.next_input_byte = pr->ptr+1;
442
8.79k
    jpg->jsrc.bytes_in_buffer = pr->limit - pr->ptr;
443
8.79k
    jpg->bytes_available_on_entry = jpg->jsrc.bytes_in_buffer;
444
445
8.79k
    return 0;
446
8.90k
}
447
448
static int
449
consume_jpeg_data(jpg_interp_instance_t *jpg, stream_cursor_read *pr)
450
8.71k
{
451
8.71k
    size_t bytes_read = jpg->jsrc.next_input_byte - (pr->ptr+1);
452
8.71k
    size_t n = pr->limit - pr->ptr;
453
454
8.71k
    if (n > bytes_read)
455
7.90k
        n = bytes_read;
456
8.71k
    pr->ptr += n;
457
8.71k
    bytes_read -= n;
458
459
    /* We need to skip the next bytes_read bytes */
460
8.71k
    jpg->bytes_to_skip = bytes_read;
461
462
8.71k
    return (pr->limit - pr->ptr == jpg->bytes_available_on_entry);
463
8.71k
}
464
465
/* Horrible hack. Windows appears to expect the jmpbuf to be 16 byte
466
 * aligned. Most 64bit platforms appear to return blocks from malloc
467
 * that are aligned to 16 byte boundaries. Our malloc routines only
468
 * return things to be 8 byte aligned on 64bit platforms, so we may
469
 * fail. Accordingly, we allocate the jmpbuf in a larger structure,
470
 * and align it.
471
 *
472
 * This is only a calculation we have to do once, so we just do it on
473
 * all platforms. */
474
static jmp_buf *
475
align_setjmp_buffer(void *ptr)
476
166
{
477
166
    intptr_t offset = (intptr_t)ptr;
478
166
    char *aligned;
479
166
    offset &= 15;
480
166
    offset = (16-offset) & 15;
481
482
166
    aligned = ((char *)ptr) + offset;
483
484
166
    return (jmp_buf *)(void *)aligned;
485
166
}
486
487
OPTIMIZE_SETJMP
488
static int
489
jpg_impl_process(pl_interp_implementation_t * impl, stream_cursor_read * pr)
490
770
{
491
770
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
492
770
    int code = 0;
493
770
    int need_more_data;
494
495
770
    do
496
9.59k
    {
497
9.59k
        need_more_data = 0;
498
9.59k
        switch(jpg->state)
499
9.59k
        {
500
166
        case ii_state_identifying:
501
166
        {
502
166
            const byte *hdr;
503
504
166
            jpg->jerr.setjmp_buffer = align_setjmp_buffer(&jpg->aligned_jmpbuf);
505
506
            /* Try and get us 11 bytes */
507
166
            code = ensure_bytes(jpg, pr, 11);
508
166
            if (code < 0)
509
0
                return code;
510
166
            hdr = pr->ptr+1;
511
166
            if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
512
166
                memcmp("JFIF", hdr+6, 5) == 0)
513
166
            {
514
166
                jpg->state = ii_state_jpeg;
515
166
                break;
516
166
            }
517
0
            jpg->state = ii_state_flush;
518
0
            break;
519
166
        }
520
166
        case ii_state_jpeg:
521
166
            code = gs_jpeg_mem_init(jpg->memory, (j_common_ptr)&jpg->cinfo);
522
166
            if (code < 0) {
523
0
                jpg->state = ii_state_flush;
524
0
                break;
525
0
            }
526
527
166
            jpg->cinfo.err = jpeg_std_error(&jpg->jerr.pub);
528
166
            jpg->jerr.pub.error_exit = jpg_error_exit;
529
166
            if (setjmp(*jpg->jerr.setjmp_buffer)) {
530
0
                jpg->state = ii_state_flush;
531
0
                break;
532
0
            }
533
534
166
            jpeg_create_decompress(&jpg->cinfo);
535
536
166
            jpg->cinfo.src = &jpg->jsrc;
537
166
            jpg->jsrc.init_source = jpg_jpeg_init_source;
538
166
            jpg->jsrc.fill_input_buffer = jpg_fill_input_buffer;
539
166
            jpg->jsrc.skip_input_data = jpg_skip_input_data;
540
166
            jpg->jsrc.resync_to_restart = jpg_resync_to_restart;
541
166
            jpg->jsrc.term_source = jpg_term_source;
542
543
166
            jpg->state = ii_state_jpeg_header;
544
166
            jpg->y = 0;
545
166
            break;
546
387
        case ii_state_jpeg_header:
547
387
        {
548
387
            int ok;
549
387
            gs_color_space *cs;
550
387
            float scale, xext, yext, xoffset, yoffset;
551
552
387
            if (fill_jpeg_source(jpg, pr)) {
553
78
                need_more_data = 1;
554
78
                break; /* No bytes left after skipping */
555
78
            }
556
309
            if (setjmp(*jpg->jerr.setjmp_buffer)) {
557
14
                jpeg_destroy_decompress(&jpg->cinfo);
558
14
                jpg->state = ii_state_flush;
559
14
                break;
560
14
            }
561
295
            ok = jpeg_read_header(&jpg->cinfo, TRUE);
562
295
            need_more_data = consume_jpeg_data(jpg, pr);
563
295
            if (ok == JPEG_SUSPENDED)
564
189
                break;
565
106
            need_more_data = 0;
566
567
106
            jpg->width = jpg->cinfo.image_width;
568
106
            jpg->height = jpg->cinfo.image_height;
569
106
            jpg->num_comps = jpg->cinfo.num_components;
570
106
            jpg->bpp = 8 * jpg->num_comps;
571
106
            switch(jpg->num_comps) {
572
1
            default:
573
6
            case 1:
574
6
                cs = jpg->gray;
575
6
                jpg->cinfo.out_color_space = JCS_GRAYSCALE;
576
6
                break;
577
97
            case 3:
578
97
                cs = jpg->rgb;
579
97
                jpg->cinfo.out_color_space = JCS_RGB;
580
97
                break;
581
3
            case 4:
582
3
                cs = jpg->cmyk;
583
3
                jpg->cinfo.out_color_space = JCS_CMYK;
584
3
                break;
585
106
            }
586
587
            /* Find us some X and Y resolutions */
588
106
            jpg->xresolution = jpg->cinfo.X_density;
589
106
            jpg->yresolution = jpg->cinfo.Y_density;
590
106
            if (jpg->xresolution == 0)
591
9
                jpg->xresolution = jpg->yresolution;
592
106
            if (jpg->yresolution == 0)
593
7
                jpg->yresolution = jpg->xresolution;
594
106
            if (jpg->xresolution == 0)
595
1
                jpg->xresolution = 72;
596
106
            if (jpg->yresolution == 0)
597
1
                jpg->yresolution = 72;
598
599
            /* Scale to fit, if too large. */
600
106
            scale = 1.0f;
601
106
            if (jpg->width * jpg->dev->HWResolution[0] > jpg->dev->width * jpg->xresolution)
602
11
                scale = ((float)jpg->dev->width * jpg->xresolution) / (jpg->width * jpg->dev->HWResolution[0]);
603
106
            if (scale * jpg->height * jpg->dev->HWResolution[1] > jpg->dev->height * jpg->yresolution)
604
6
                scale = ((float)jpg->dev->height * jpg->yresolution) / (jpg->height * jpg->dev->HWResolution[1]);
605
606
            /* Centre - Extents and offsets are all calculated in points (1/72 of an inch) */
607
106
            xext = ((float)jpg->width * 72 * scale / jpg->xresolution);
608
106
            xoffset = (jpg->dev->width * 72 / jpg->dev->HWResolution[0] - xext)/2;
609
106
            yext = ((float)jpg->height * 72 * scale / jpg->yresolution);
610
106
            yoffset = (jpg->dev->height * 72 / jpg->dev->HWResolution[1] - yext)/2;
611
612
            /* Now we've got the data from the image header, we can
613
             * make the gs image instance */
614
106
            jpg->byte_width = (jpg->bpp>>3)*jpg->width;
615
106
            if (jpg->width != 0 && jpg->byte_width / (jpg->bpp>>3) != jpg->width)
616
0
                return gs_note_error(gs_error_undefinedresult);
617
618
106
            jpg->nulldev = gs_currentdevice(jpg->pgs);
619
106
            rc_increment(jpg->nulldev);
620
106
            code = gs_setdevice_no_erase(jpg->pgs, jpg->dev);
621
106
            if (code < 0) {
622
0
                jpg->state = ii_state_flush;
623
0
                break;
624
0
            }
625
106
            gs_initmatrix(jpg->pgs);
626
627
            /* By default the ctm is set to:
628
             *   xres/72   0
629
             *   0         -yres/72
630
             *   0         dev->height * yres/72
631
             * i.e. it moves the origin from being top right to being bottom left.
632
             * We want to move it back, as without this, the image will be displayed
633
             * upside down.
634
             */
635
106
            code = gs_translate(jpg->pgs, 0.0, jpg->dev->height * 72 / jpg->dev->HWResolution[1]);
636
106
            if (code >= 0)
637
106
                code = gs_translate(jpg->pgs, xoffset, -yoffset);
638
106
            if (code >= 0)
639
106
                code = gs_scale(jpg->pgs, scale, -scale);
640
            /* At this point, the ctm is set to:
641
             *   xres/72  0
642
             *   0        yres/72
643
             *   0        0
644
             */
645
106
            if (code >= 0)
646
106
                code = gs_erasepage(jpg->pgs);
647
106
            if (code < 0) {
648
0
                jpg->state = ii_state_flush;
649
0
                break;
650
0
            }
651
652
106
            jpg->samples = gs_alloc_bytes(jpg->memory, jpg->byte_width, "jpg_impl_process(samples)");
653
106
            if (jpg->samples == NULL) {
654
0
                jpg->state = ii_state_flush;
655
0
                break;
656
0
            }
657
658
106
            memset(&jpg->image, 0, sizeof(jpg->image));
659
106
            gs_image_t_init(&jpg->image, cs);
660
106
            jpg->image.BitsPerComponent = jpg->bpp/jpg->num_comps;
661
106
            jpg->image.Width = jpg->width;
662
106
            jpg->image.Height = jpg->height;
663
664
106
            jpg->image.ImageMatrix.xx = jpg->xresolution / 72.0f;
665
106
            jpg->image.ImageMatrix.yy = jpg->yresolution / 72.0f;
666
667
106
            jpg->penum = gs_image_enum_alloc(jpg->memory, "jpg_impl_process(penum)");
668
106
            if (jpg->penum == NULL) {
669
0
                code = gs_note_error(gs_error_VMerror);
670
0
                jpg->state = ii_state_flush;
671
0
                return code;
672
0
            }
673
674
106
            code = gs_image_init(jpg->penum,
675
106
                                 &jpg->image,
676
106
                                 false,
677
106
                                 false,
678
106
                                 jpg->pgs);
679
106
            if (code < 0) {
680
0
                jpg->state = ii_state_flush;
681
0
                return code;
682
0
            }
683
684
106
            jpg->state = ii_state_jpeg_start;
685
106
            break;
686
106
        }
687
553
        case ii_state_jpeg_start:
688
553
        {
689
553
            int ok;
690
553
            if (fill_jpeg_source(jpg, pr)) {
691
39
                need_more_data = 1;
692
39
                break; /* No bytes left after skipping */
693
39
            }
694
514
            if (setjmp(*jpg->jerr.setjmp_buffer)) {
695
56
                jpeg_destroy_decompress(&jpg->cinfo);
696
56
                jpg->state = ii_state_flush;
697
56
                break;
698
56
            }
699
458
            ok = jpeg_start_decompress(&jpg->cinfo);
700
458
            (void)consume_jpeg_data(jpg, pr);
701
458
            if (ok == FALSE) {
702
433
                if (jpg->jsrc.bytes_in_buffer == jpg->bytes_available_on_entry)
703
209
                    need_more_data = 1;
704
433
                break;
705
433
            }
706
25
            jpg->state = ii_state_jpeg_rows;
707
25
            break;
708
458
        }
709
7.94k
        case ii_state_jpeg_rows:
710
7.94k
        {
711
7.94k
            int rows_decoded;
712
7.94k
            unsigned int used;
713
714
7.94k
            if (fill_jpeg_source(jpg, pr)) {
715
0
                need_more_data = 1;
716
0
                break; /* No bytes left after skipping */
717
0
            }
718
7.94k
            if (setjmp(*jpg->jerr.setjmp_buffer)) {
719
0
                jpeg_destroy_decompress(&jpg->cinfo);
720
0
                jpg->state = ii_state_flush;
721
0
                break;
722
0
            }
723
7.94k
            rows_decoded = jpeg_read_scanlines(&jpg->cinfo, &jpg->samples, 1);
724
7.94k
            need_more_data = consume_jpeg_data(jpg, pr);
725
7.94k
            if (rows_decoded == 0)
726
28
                break; /* Not enough data for a scanline yet */
727
7.91k
            need_more_data = 0;
728
729
7.91k
            code = gs_image_next(jpg->penum, jpg->samples, jpg->byte_width, &used);
730
7.91k
            if (code < 0) {
731
0
                jpg->state = ii_state_flush;
732
0
                break;
733
0
            }
734
7.91k
            jpg->y++;
735
7.91k
            if (jpg->y == jpg->height) {
736
25
                code = gs_image_cleanup_and_free_enum(jpg->penum, jpg->pgs);
737
25
                jpg->penum = NULL;
738
25
                if (code < 0) {
739
0
                    jpg->state = ii_state_flush;
740
0
                    break;
741
0
                }
742
25
                code = pl_finish_page(jpg->memory->gs_lib_ctx->top_of_system,
743
25
                                      jpg->pgs, 1, true);
744
25
                if (code < 0) {
745
0
                    jpg->state = ii_state_flush;
746
0
                    break;
747
0
                }
748
25
                jpg->state = ii_state_jpeg_finish;
749
25
            }
750
7.91k
            break;
751
7.91k
        }
752
7.91k
        case ii_state_jpeg_finish:
753
26
        {
754
26
            int ok;
755
756
26
            if (fill_jpeg_source(jpg, pr)) {
757
0
                need_more_data = 1;
758
0
                break; /* No bytes left after skipping */
759
0
            }
760
26
            if (setjmp(*jpg->jerr.setjmp_buffer)) {
761
6
                jpeg_destroy_decompress(&jpg->cinfo);
762
6
                jpg->state = ii_state_flush;
763
6
                break;
764
6
            }
765
20
            ok = jpeg_finish_decompress(&jpg->cinfo);
766
20
            need_more_data = consume_jpeg_data(jpg, pr);
767
20
            if (ok == FALSE)
768
2
                break;
769
18
            need_more_data = 0;
770
18
            jpg->state = ii_state_flush;
771
18
            break;
772
20
        }
773
0
        default:
774
355
        case ii_state_flush:
775
355
            if (setjmp(*jpg->jerr.setjmp_buffer))
776
0
                break;
777
355
            jpeg_destroy_decompress(&jpg->cinfo);
778
779
355
            gs_jpeg_mem_term((j_common_ptr)&jpg->cinfo);
780
781
355
            if (jpg->penum) {
782
56
                (void)gs_image_cleanup_and_free_enum(jpg->penum, jpg->pgs);
783
56
                jpg->penum = NULL;
784
56
            }
785
786
355
            gs_free_object(jpg->memory, jpg->samples, "jpg_impl_process(samples)");
787
355
            jpg->samples = NULL;
788
            /* We want to bin any data we get up to, but not including
789
             * a UEL. */
790
355
            return flush_to_uel(pr);
791
9.59k
        }
792
9.59k
    } while (!need_more_data);
793
794
415
    return code;
795
770
}
796
797
static int
798
jpg_impl_process_end(pl_interp_implementation_t * impl)
799
166
{
800
166
    return 0;
801
166
}
802
803
/* Not implemented */
804
static int
805
jpg_impl_flush_to_eoj(pl_interp_implementation_t *impl, stream_cursor_read *cursor)
806
0
{
807
0
    const byte *p = cursor->ptr;
808
0
    const byte *rlimit = cursor->limit;
809
810
    /* Skip to, but leave UEL in buffer for PJL to find later */
811
0
    for (; p < rlimit; ++p)
812
0
        if (p[1] == '\033') {
813
0
            uint avail = rlimit - p;
814
815
0
            if (memcmp(p + 1, "\033%-12345X", min(avail, 9)))
816
0
                continue;
817
0
            if (avail < 9)
818
0
                break;
819
0
            cursor->ptr = p;
820
0
            return 1;           /* found eoj */
821
0
        }
822
0
    cursor->ptr = p;
823
0
    return 0;                   /* need more */
824
0
}
825
826
/* Parser action for end-of-file */
827
static int
828
jpg_impl_process_eof(pl_interp_implementation_t *impl)
829
166
{
830
166
    return 0;
831
166
}
832
833
/* Report any errors after running a job */
834
static int
835
jpg_impl_report_errors(pl_interp_implementation_t *impl,          /* interp instance to wrap up job in */
836
                       int                         code,          /* prev termination status */
837
                       long                        file_position, /* file position of error, -1 if unknown */
838
                       bool                        force_to_cout  /* force errors to cout */
839
)
840
0
{
841
0
    return 0;
842
0
}
843
844
/* Wrap up interp instance after a "job" */
845
static int
846
jpg_impl_dnit_job(pl_interp_implementation_t *impl)
847
166
{
848
166
    jpg_interp_instance_t *jpg = (jpg_interp_instance_t *)impl->interp_client_data;
849
850
166
    if (jpg->nulldev) {
851
106
        int code = gs_setdevice(jpg->pgs, jpg->nulldev);
852
106
        jpg->dev = NULL;
853
106
        rc_decrement(jpg->nulldev, "jpg_impl_dnit_job(nulldevice)");
854
106
        jpg->nulldev = NULL;
855
106
        return code;
856
106
    }
857
60
    return 0;
858
166
}
859
860
/* Parser implementation descriptor */
861
const pl_interp_implementation_t jpg_implementation = {
862
  jpg_impl_characteristics,
863
  jpg_impl_allocate_interp_instance,
864
  jpg_impl_get_device_memory,
865
  NULL, /* jpg_impl_set_param */
866
  NULL, /* jpg_impl_add_path */
867
  NULL, /* jpg_impl_post_args_init */
868
  jpg_impl_init_job,
869
  NULL, /* jpg_impl_run_prefix_commands */
870
  NULL, /* jpg_impl_process_file */
871
  jpg_impl_process_begin,
872
  jpg_impl_process,
873
  jpg_impl_process_end,
874
  jpg_impl_flush_to_eoj,
875
  jpg_impl_process_eof,
876
  jpg_impl_report_errors,
877
  jpg_impl_dnit_job,
878
  jpg_impl_deallocate_interp_instance,
879
  NULL, /* jpg_impl_reset */
880
  NULL  /* interp_client_data */
881
};