summaryrefslogtreecommitdiff
blob: 229e6ccb36e3c1edae78f6065b571e7973deb896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* Copyright (C) 2001-2019 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/

/* JPEG output driver */
#include "stdio_.h"		/* for jpeglib.h */
#include "jpeglib_.h"
#include "gdevprn.h"
#include "stream.h"
#include "strimpl.h"
#include "sdct.h"
#include "sjpeg.h"
#include "gxdownscale.h"

/* Structure for the JPEG-writing device. */
typedef struct gx_device_jpeg_s {
    gx_device_common;
    gx_prn_device_common;
    /* Additional parameters */
    int JPEGQ;			/* quality on IJG scale */
    float QFactor;		/* quality per DCTEncode conventions */
    /* JPEGQ overrides QFactor if both are specified. */

    /** 1.0 default 2.0 is twice as big
     */
    gs_point ViewScale;

    /** translation needs to have scalefactor multiplied in.
     */
    gs_point ViewTrans;

    gx_downscaler_params downscale;
} gx_device_jpeg;

/* The device descriptor */
static dev_proc_get_params(jpeg_get_params);
static dev_proc_get_initial_matrix(jpeg_get_initial_matrix);
static dev_proc_put_params(jpeg_put_params);
static dev_proc_print_page(jpeg_print_page);
static dev_proc_map_color_rgb(jpegcmyk_map_color_rgb);
static dev_proc_map_cmyk_color(jpegcmyk_map_cmyk_color);

/* ------ The device descriptors ------ */

/* Default X and Y resolution. */
#ifndef X_DPI
#  define X_DPI 72
#endif
#ifndef Y_DPI
#  define Y_DPI 72
#endif

/* 24-bit color */

static const gx_device_procs jpeg_procs =
{
    gdev_prn_open,
    jpeg_get_initial_matrix,	/* get_initial_matrix */
    NULL,			/* sync_output */
/* Since the print_page doesn't alter the device, this device can print in the background */
    gdev_prn_bg_output_page,
    gdev_prn_close,
    gx_default_rgb_map_rgb_color,/* map_rgb_color */
    gx_default_rgb_map_color_rgb,
    NULL,			/* fill_rectangle */
    NULL,			/* tile_rectangle */
    NULL,			/* copy_mono */
    NULL,			/* copy_color */
    NULL,			/* draw_line */
    NULL,			/* get_bits */
    jpeg_get_params,
    jpeg_put_params,
    NULL,
    NULL,			/* get_xfont_procs */
    NULL,			/* get_xfont_device */
    NULL,			/* map_rgb_alpha_color */
    gx_page_device_get_page_device
};

const gx_device_jpeg gs_jpeg_device =
{prn_device_std_body(gx_device_jpeg, jpeg_procs, "jpeg",
                     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
                     X_DPI, Y_DPI, 0, 0, 0, 0, 24, jpeg_print_page),
 0,				/* JPEGQ: 0 indicates not specified */
 0.0,				/* QFactor: 0 indicates not specified */
 { 1.0, 1.0 },                  /* ViewScale 1 to 1 */
 { 0.0, 0.0 },                  /* translation 0 */
 GX_DOWNSCALER_PARAMS_DEFAULTS
};

/* 8-bit gray */

static const gx_device_procs jpeggray_procs =
{
    gdev_prn_open,
    jpeg_get_initial_matrix,	/* get_initial_matrix */
    NULL,			/* sync_output */
/* Since the print_page doesn't alter the device, this device can print in the background */
    gdev_prn_bg_output_page,
    gdev_prn_close,
    gx_default_gray_map_rgb_color,/* map_rgb_color */
    gx_default_gray_map_color_rgb,
    NULL,			/* fill_rectangle */
    NULL,			/* tile_rectangle */
    NULL,			/* copy_mono */
    NULL,			/* copy_color */
    NULL,			/* draw_line */
    NULL,			/* get_bits */
    jpeg_get_params,
    jpeg_put_params,
    NULL,
    NULL,			/* get_xfont_procs */
    NULL,			/* get_xfont_device */
    NULL,			/* map_rgb_alpha_color */
    gx_page_device_get_page_device
};

const gx_device_jpeg gs_jpeggray_device =
{prn_device_body(gx_device_jpeg, jpeggray_procs, "jpeggray",
                 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
                 X_DPI, Y_DPI, 0, 0, 0, 0,
                 1, 8, 255, 0, 256, 0,
                 jpeg_print_page),
 0,				/* JPEGQ: 0 indicates not specified */
 0.0,				/* QFactor: 0 indicates not specified */
 { 1.0, 1.0 },                  /* ViewScale 1 to 1 */
 { 0.0, 0.0 },                   /* translation 0 */
 GX_DOWNSCALER_PARAMS_DEFAULTS
};
/* 32-bit CMYK */

static const gx_device_procs jpegcmyk_procs =
{	gdev_prn_open,
        gx_default_get_initial_matrix,
/* Since the print_page doesn't alter the device, this device can print in the background */
        NULL,	/* sync_output */
        gdev_prn_bg_output_page,
        gdev_prn_close,
        NULL,
        jpegcmyk_map_color_rgb,
        NULL,	/* fill_rectangle */
        NULL,	/* tile_rectangle */
        NULL,	/* copy_mono */
        NULL,	/* copy_color */
        NULL,	/* draw_line */
        NULL,	/* get_bits */
        jpeg_get_params,
        jpeg_put_params,
        jpegcmyk_map_cmyk_color,
        NULL,	/* get_xfont_procs */
        NULL,	/* get_xfont_device */
        NULL,	/* map_rgb_alpha_color */
        gx_page_device_get_page_device	/* get_page_device */
};

const gx_device_jpeg gs_jpegcmyk_device =
{prn_device_std_body(gx_device_jpeg, jpegcmyk_procs, "jpegcmyk",
                     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
                     X_DPI, Y_DPI, 0, 0, 0, 0, 32, jpeg_print_page),
 0,				/* JPEGQ: 0 indicates not specified */
 0.0,				/* QFactor: 0 indicates not specified */
 { 1.0, 1.0 },                  /* ViewScale 1 to 1 */
 { 0.0, 0.0 },                   /* translation 0 */
 GX_DOWNSCALER_PARAMS_DEFAULTS
};

/* Apparently Adobe Photoshop and some other applications that	*/
/* accept JPEG CMYK images expect color values to be inverted.	*/
static int
jpegcmyk_map_color_rgb(gx_device * dev, gx_color_index color,
                        gx_color_value prgb[3])
{
    int
        not_k = color & 0xff,
        r = not_k - ~(color >> 24),
        g = not_k - ~((color >> 16) & 0xff),
        b = not_k - ~((color >> 8) & 0xff);

    prgb[0] = (r < 0 ? 0 : gx_color_value_from_byte(r));
    prgb[1] = (g < 0 ? 0 : gx_color_value_from_byte(g));
    prgb[2] = (b < 0 ? 0 : gx_color_value_from_byte(b));
    return 0;
}

static gx_color_index
jpegcmyk_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{
    gx_color_index color = ~(
        gx_color_value_to_byte(cv[3]) +
        ((uint)gx_color_value_to_byte(cv[2]) << 8) +
        ((uint)gx_color_value_to_byte(cv[1]) << 16) +
        ((uint)gx_color_value_to_byte(cv[0]) << 24));

    return (color == gx_no_color_index ? color ^ 1 : color);
}

/* Get parameters. */
static int
jpeg_get_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_jpeg *jdev = (gx_device_jpeg *) dev;
    int code = gdev_prn_get_params(dev, plist);
    int ecode;
    float float2double;
    if (code < 0)
        return code;

    ecode = 0;
    if ((code = gx_downscaler_write_params(plist, &jdev->downscale, 0)) < 0)
        ecode = code;
    if ((ecode = param_write_int(plist, "JPEGQ", &jdev->JPEGQ)) < 0)
        code = ecode;
    if ((ecode = param_write_float(plist, "QFactor", &jdev->QFactor)) < 0)
        code = ecode;
    float2double = jdev->ViewScale.x;
    if ((ecode = param_write_float(plist, "ViewScaleX", &float2double)) < 0)
        code = ecode;
    float2double = jdev->ViewScale.y;
    if ((ecode = param_write_float(plist, "ViewScaleY", &float2double)) < 0)
        code = ecode;
    float2double = jdev->ViewTrans.x;
    if ((ecode = param_write_float(plist, "ViewTransX", &float2double)) < 0)
        code = ecode;
    float2double = jdev->ViewTrans.y;
    if ((ecode = param_write_float(plist, "ViewTransY", &float2double)) < 0)
        code = ecode;

    return code;
}

/* Put parameters. */
static int
jpeg_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_jpeg *jdev = (gx_device_jpeg *) dev;
    int ecode = 0;
    int code;
    gs_param_name param_name;
    int jq = jdev->JPEGQ;
    float qf = jdev->QFactor;
    float fparam;

    ecode = gx_downscaler_read_params(plist, &jdev->downscale, 0);

    switch (code = param_read_int(plist, (param_name = "JPEGQ"), &jq)) {
        case 0:
            if (jq < 0 || jq > 100)
                ecode = gs_error_limitcheck;
            else
                break;
            goto jqe;
        default:
            ecode = code;
          jqe:param_signal_error(plist, param_name, ecode);
        case 1:
            break;
    }

    switch (code = param_read_float(plist, (param_name = "QFactor"), &qf)) {
        case 0:
            if (qf < 0.0 || qf > 1.0e6)
                ecode = gs_error_limitcheck;
            else
                break;
            goto qfe;
        default:
            ecode = code;
          qfe:param_signal_error(plist, param_name, ecode);
        case 1:
            break;
    }

    code = param_read_float(plist, (param_name = "ViewScaleX"), &fparam);
    if ( code == 0 ) {
        if (fparam < 1.0)
            param_signal_error(plist, param_name, gs_error_limitcheck);
        else
            jdev->ViewScale.x = fparam;
    }
    else if ( code < 1 ) {
        ecode = code;
        param_signal_error(plist, param_name, code);
    }

    code = param_read_float(plist, (param_name = "ViewScaleY"), &fparam);
    if ( code == 0 ) {
        if (fparam < 1.0)
            param_signal_error(plist, param_name, gs_error_limitcheck);
        else
            jdev->ViewScale.y = fparam;
    }
    else if ( code < 1 ) {
        ecode = code;
        param_signal_error(plist, param_name, code);
    }

    /* pixels in desired dpi, auto negative ( moves up and left ) */
    code = param_read_float(plist, (param_name = "ViewTransX"), &fparam);
    if ( code == 0 ) {
        jdev->ViewTrans.x = fparam;
    }
    else if ( code < 1 ) {
        ecode = code;
        param_signal_error(plist, param_name, code);
    }

    code = param_read_float(plist, (param_name = "ViewTransY"), &fparam);
    if ( code == 0 ) {
        jdev->ViewTrans.y = fparam;
    }
    else if ( code < 1 ) {
        ecode = code;
        param_signal_error(plist, param_name, code);
    }
    code = gdev_prn_put_params(dev, plist);
    if (code < 0)
        return code;

    if (ecode < 0)
        return ecode;

    jdev->JPEGQ = jq;
    jdev->QFactor = qf;
    return 0;
}

/******************************************************************
 This device supports translation and scaling.

0123456

0PPPPPPP  0 is origin
PPPPPPPP  1 is x1,y1 (2,2)
PP1vvvPP  2 is x2,y2 (6,6)
PPvvvvPP  v is viewport, P is original page
PPvvvvPP
PPPPPP2P
PPPPPPPP

Given a view port in pixels starting at x1,y1
where x1 < width, y1 < height in pixels

ViewScaleX = desired Resolution / HWResolution ; 1.0 default
ViewScaleY = desired Resolution / HWResolution

HWResolutionX = desired dpi at 1:1 scaling     ; 72dpi default
HWResolutionY = desired dpi at 1:1 scaling

ViewTransX = x1 * ViewScaleX                   ; 0.0 default
ViewTransY = y1 * ViewScaleY

if initial matrix multiplies ViewScaleX in then translation is limited to
multiples of the HWResolution.

***************************************************************************/

static void
jpeg_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
{
    gx_device_jpeg *pdev = (gx_device_jpeg *)dev;
    double fs_res = (dev->HWResolution[0] / 72.0) * pdev->ViewScale.x;
    double ss_res = (dev->HWResolution[1] / 72.0) * pdev->ViewScale.y;

    /* NB this device has no paper margins */

    switch(pdev->LeadingEdge & LEADINGEDGE_MASK) {
    case 1:
        pmat->xx = 0;
        pmat->xy = -ss_res;
        pmat->yx = -fs_res;
        pmat->yy = 0;
        pmat->tx = (pdev->width * pdev->ViewScale.x) - pdev->ViewTrans.x;
        pmat->ty = (pdev->height * pdev->ViewScale.y) - pdev->ViewTrans.y;
        break;
    case 2:
        pmat->xx = -fs_res;
        pmat->xy = 0;
        pmat->yx = 0;
        pmat->yy = ss_res;
        pmat->tx = (pdev->width * pdev->ViewScale.x) - pdev->ViewTrans.x;
        pmat->ty = -pdev->ViewTrans.x;
        break;
    case 3:
        pmat->xx = 0;
        pmat->xy = ss_res;
        pmat->yx = fs_res;
        pmat->yy = 0;
        pmat->tx = -pdev->ViewTrans.x;
        pmat->ty = -pdev->ViewTrans.y;
        break;
    default:
    case 0:
        pmat->xx = fs_res;
        pmat->xy = 0;
        pmat->yx = 0;
        pmat->yy = -ss_res;
        pmat->tx = -pdev->ViewTrans.x;
        pmat->ty = (pdev->height * pdev->ViewScale.y) - pdev->ViewTrans.y;
        break;
    }

}

/* Send the page to the file. */
static int
jpeg_print_page(gx_device_printer * pdev, gp_file * prn_stream)
{
    gx_device_jpeg *jdev = (gx_device_jpeg *) pdev;
    gs_memory_t *mem = pdev->memory;
    int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
    byte *in = gs_alloc_bytes(mem, line_size, "jpeg_print_page(in)");
    jpeg_compress_data *jcdp = gs_alloc_struct_immovable(mem, jpeg_compress_data,
      &st_jpeg_compress_data, "jpeg_print_page(jpeg_compress_data)");
    byte *fbuf = 0;
    uint fbuf_size;
    byte *jbuf = 0;
    uint jbuf_size;
    int lnum;
    int code;
    stream_DCT_state state;
    stream fstrm, jstrm;
    gx_downscaler_t ds;

    if (jcdp == 0 || in == 0) {
        code = gs_note_error(gs_error_VMerror);
        goto fail;
    }
    code = gx_downscaler_init(&ds, (gx_device *)jdev, 8, 8,
                              jdev->color_info.depth/8, jdev->downscale.downscale_factor, 0, NULL, 0);
    if (code < 0) {
        gs_free_object(mem, jcdp, "jpeg_print_page(jpeg_compress_data)");
        jcdp = NULL;
        goto fail;
    }

    /* Create the DCT encoder state. */
    jcdp->templat = s_DCTE_template;
    s_init_state((stream_state *)&state, &jcdp->templat, 0);
    if (state.templat->set_defaults) {
        state.memory = mem;
        (*state.templat->set_defaults) ((stream_state *) & state);
        state.memory = NULL;
    }
    state.QFactor = 1.0;	/* disable quality adjustment in zfdcte.c */
    state.ColorTransform = 1;	/* default for RGB */
    /* We insert no markers, allowing the IJG library to emit */
    /* the format it thinks best. */
    state.NoMarker = true;	/* do not insert our own Adobe marker */
    state.Markers.data = 0;
    state.Markers.size = 0;
    state.data.compress = jcdp;
    /* Add in ICC profile */
    state.icc_profile = NULL; /* In case it is not set here */
    if (pdev->icc_struct != NULL && pdev->icc_struct->device_profile[0] != NULL) {
        cmm_profile_t *icc_profile = pdev->icc_struct->device_profile[0];
        if (icc_profile->num_comps == pdev->color_info.num_components &&
            !(pdev->icc_struct->usefastcolor)) {
            state.icc_profile = icc_profile;
        }
    }
    /* We need state.memory for gs_jpeg_create_compress().... */
    jcdp->memory = state.jpeg_memory = state.memory = mem;
    if ((code = gs_jpeg_create_compress(&state)) < 0)
    {
        gx_downscaler_fin(&ds);
        goto fail;
    }
    /* ....but we need it to be NULL so we don't try to free
     * the stack based state...
     */
    state.memory = NULL;
    jcdp->cinfo.image_width = gx_downscaler_scale(pdev->width, jdev->downscale.downscale_factor);
    jcdp->cinfo.image_height = gx_downscaler_scale(pdev->height, jdev->downscale.downscale_factor);
    switch (pdev->color_info.depth) {
        case 32:
            jcdp->cinfo.input_components = 4;
            jcdp->cinfo.in_color_space = JCS_CMYK;
            break;
        case 24:
            jcdp->cinfo.input_components = 3;
            jcdp->cinfo.in_color_space = JCS_RGB;
            break;
        case 8:
            jcdp->cinfo.input_components = 1;
            jcdp->cinfo.in_color_space = JCS_GRAYSCALE;
            break;
    }
    /* Set compression parameters. */
    if ((code = gs_jpeg_set_defaults(&state)) < 0)
        goto done;
    if (jdev->JPEGQ > 0) {
        code = gs_jpeg_set_quality(&state, jdev->JPEGQ, TRUE);
        if (code < 0)
            goto done;
    } else if (jdev->QFactor > 0.0) {
        code = gs_jpeg_set_linear_quality(&state,
                                          (int)(min(jdev->QFactor, 100.0)
                                                * 100.0 + 0.5),
                                          TRUE);
        if (code < 0)
            goto done;
    }
    jcdp->cinfo.restart_interval = 0;
    jcdp->cinfo.density_unit = 1;	/* dots/inch (no #define or enum) */
    jcdp->cinfo.X_density = (UINT16)pdev->HWResolution[0];
    jcdp->cinfo.Y_density = (UINT16)pdev->HWResolution[1];
    /* Create the filter. */
    /* Make sure we get at least a full scan line of input. */
    state.scan_line_size = jcdp->cinfo.input_components *
        jcdp->cinfo.image_width;
    jcdp->templat.min_in_size =
        max(s_DCTE_template.min_in_size, state.scan_line_size);
    /* Make sure we can write the user markers in a single go. */
    jcdp->templat.min_out_size =
        max(s_DCTE_template.min_out_size, state.Markers.size);

    /* Set up the streams. */
    fbuf_size = max(512 /* arbitrary */ , jcdp->templat.min_out_size);
    jbuf_size = jcdp->templat.min_in_size;
    if ((fbuf = gs_alloc_bytes(mem, fbuf_size, "jpeg_print_page(fbuf)")) == 0 ||
        (jbuf = gs_alloc_bytes(mem, jbuf_size, "jpeg_print_page(jbuf)")) == 0
        ) {
        code = gs_note_error(gs_error_VMerror);
        goto done;
    }
    s_init(&fstrm, mem);
    swrite_file(&fstrm, prn_stream, fbuf, fbuf_size);
    s_init(&jstrm, mem);
    s_std_init(&jstrm, jbuf, jbuf_size, &s_filter_write_procs,
               s_mode_write);
    jstrm.state = (stream_state *) & state;
    jstrm.procs.process = state.templat->process;
    jstrm.strm = &fstrm;
    if (state.templat->init)
        (*state.templat->init) (jstrm.state);

    /* Copy the data to the output. */
    for (lnum = 0; lnum < jcdp->cinfo.image_height; ++lnum) {
        uint ignore_used;

        if (jstrm.end_status) {
            code = gs_note_error(gs_error_ioerror);
            goto done;
        }
        gx_downscaler_getbits(&ds, in, lnum);
        sputs(&jstrm, in, state.scan_line_size, &ignore_used);
    }

    /* Wrap up. */
    sclose(&jstrm);
    sflush(&fstrm);
  done:
    gs_free_object(mem, jbuf, "jpeg_print_page(jbuf)");
    gs_free_object(mem, fbuf, "jpeg_print_page(fbuf)");
    if (jcdp) {
        gs_jpeg_destroy(&state);
        gs_free_object(mem, jcdp, "jpeg_print_page(jpeg_compress_data)");
    }
    gx_downscaler_fin(&ds);
    gs_free_object(mem, in, "jpeg_print_page(in)");
    return code;
  fail:
    if (jcdp)
        gs_free_object(mem, jcdp, "jpeg_print_page(jpeg_compress_data)");
    gs_free_object(mem, in, "jpeg_print_page(in)");
    return code;
}