summaryrefslogtreecommitdiff
blob: b8aa7e74b3236aa5401fc58eb7eda77ad6a58291 (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
/* Copyright (C) 2001-2020 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.
*/


/* TIFF and TIFF/fax devices */

#include "stdint_.h"   /* for tiff.h */
#include "gdevtifs.h"
#include "gdevprn.h"
#include "strimpl.h"
#include "scfx.h"
#include "gdevfax.h"

#include "gstiffio.h"
#include "gdevkrnlsclass.h" /* 'standard' built in subclasses, currently First/Last Page and obejct filter */

/* ---------------- TIFF/fax output ---------------- */

/* The device descriptors */

static dev_proc_open_device(tfax_open);
static dev_proc_close_device(tfax_close);
static dev_proc_get_params(tfax_get_params);
static dev_proc_put_params(tfax_put_params);
static dev_proc_print_page(tiffcrle_print_page);
static dev_proc_print_page(tiffg3_print_page);
static dev_proc_print_page(tiffg32d_print_page);
static dev_proc_print_page(tiffg4_print_page);

struct gx_device_tfax_s {
    gx_device_common;
    gx_prn_device_common;
    gx_fax_device_common;
    long MaxStripSize;          /* 0 = no limit, other is UNCOMPRESSED limit */
                                /* The type and range of FillOrder follows TIFF 6 spec  */
    bool  BigEndian;            /* true = big endian; false = little endian*/
    bool UseBigTIFF;
    uint16 Compression;         /* same values as TIFFTAG_COMPRESSION */
    bool write_datetime;
    TIFF *tif;                  /* For TIFF output only */
};
typedef struct gx_device_tfax_s gx_device_tfax;

/* Define procedures that adjust the paper size. */
static const gx_device_procs gdev_tfax_std_procs =
/* FIXME: From initial analysis this is NOT safe for bg_printing, but might be fixable */
    prn_params_procs(tfax_open, gdev_prn_output_page_seekable, tfax_close,
                     tfax_get_params, tfax_put_params);

#define TFAX_DEVICE(dname, print_page, compr)\
{\
    FAX_DEVICE_BODY(gx_device_tfax, gdev_tfax_std_procs, dname, print_page),\
    TIFF_DEFAULT_STRIP_SIZE     /* strip size byte count */,\
    ARCH_IS_BIG_ENDIAN          /* default to native endian (i.e. use big endian iff the platform is so*/,\
    false,                      /* default to not using bigtiff */\
    compr,\
    true, /* write_datetime */ \
    NULL \
}

const gx_device_tfax gs_tiffcrle_device =
    TFAX_DEVICE("tiffcrle", tiffcrle_print_page, COMPRESSION_CCITTRLE);

const gx_device_tfax gs_tiffg3_device =
    TFAX_DEVICE("tiffg3", tiffg3_print_page, COMPRESSION_CCITTFAX3);

const gx_device_tfax gs_tiffg32d_device =
    TFAX_DEVICE("tiffg32d", tiffg32d_print_page, COMPRESSION_CCITTFAX3);

const gx_device_tfax gs_tiffg4_device =
    TFAX_DEVICE("tiffg4", tiffg4_print_page, COMPRESSION_CCITTFAX4);

static int
tfax_open(gx_device * pdev)
{
    gx_device_printer * const ppdev = (gx_device_printer *)pdev;
    int code;

    /* Use our own warning and error message handlers in libtiff */
    tiff_set_handlers();

    ppdev->file = NULL;
    code = gdev_prn_allocate_memory(pdev, NULL, 0, 0);
    if (code < 0)
        return code;

    if (ppdev->OpenOutputFile)
        if ((code = gdev_prn_open_printer_seekable(pdev, 1, true)) < 0)
            return code;
    code = install_internal_subclass_devices((gx_device **)&pdev, NULL);
    return code;
}

static int
tfax_close(gx_device * pdev)
{
    gx_device_tfax *const tfdev = (gx_device_tfax *)pdev;

    if (tfdev->tif)
        TIFFCleanup(tfdev->tif);

    return gdev_prn_close(pdev);
}

static int
tfax_get_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
    int code = gdev_fax_get_params(dev, plist);
    int ecode = code;
    gs_param_string comprstr;

    if ((code = param_write_long(plist, "MaxStripSize", &tfdev->MaxStripSize)) < 0)
        ecode = code;
    if ((code = param_write_bool(plist, "BigEndian", &tfdev->BigEndian)) < 0)
        ecode = code;
#if (TIFFLIB_VERSION >= 20111221)
    if ((code = param_write_bool(plist, "UseBigTIFF", &tfdev->UseBigTIFF)) < 0)
        ecode = code;
#endif
    if ((code = param_write_bool(plist, "TIFFDateTime", &tfdev->write_datetime)) < 0)
        ecode = code;
    if ((code = tiff_compression_param_string(&comprstr, tfdev->Compression)) < 0 ||
        (code = param_write_string(plist, "Compression", &comprstr)) < 0)
        ecode = code;

    return ecode;
}

static int
tfax_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
    int ecode = 0;
    int code;
    long mss = tfdev->MaxStripSize;
    int fill_order = tfdev->FillOrder;
    const char *param_name;
    bool big_endian = tfdev->BigEndian;
    bool usebigtiff = tfdev->UseBigTIFF;
    bool write_datetime = tfdev->write_datetime;
    uint16 compr = tfdev->Compression;
    gs_param_string comprstr;

    switch (code = param_read_long(plist, (param_name = "MaxStripSize"), &mss)) {
        case 0:
            /*
             * Strip must be large enough to accommodate a raster line.
             * If the max strip size is too small, we still write a single
             * line per strip rather than giving an error.
             */
            if (mss >= 0)
                break;
            code = gs_error_rangecheck;
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 1:
            break;
    }

    /* Following TIFF spec, FillOrder is integer */
    switch (code = param_read_int(plist, (param_name = "FillOrder"), &fill_order)) {
        case 0:
            if (fill_order == 1 || fill_order == 2)
                break;
            code = gs_error_rangecheck;
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 1:
            break;
    }

    /* Read BigEndian option as bool */
    switch (code = param_read_bool(plist, (param_name = "BigEndian"), &big_endian)) {
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 0:
        case 1:
            break;
    }

    /* Read UseBigTIFF option as bool */
    switch (code = param_read_bool(plist, (param_name = "UseBigTIFF"), &usebigtiff)) {
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 0:
        case 1:
            break;
    }

#if !(TIFFLIB_VERSION >= 20111221)
    if (usebigtiff)
        dmlprintf(dev->memory, "Warning: this version of libtiff does not support BigTIFF, ignoring parameter\n");
    usebigtiff = false;
#endif

    switch (code = param_read_bool(plist, (param_name = "TIFFDateTime"), &write_datetime)) {
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
        case 0:
        case 1:
            break;
    }

    /* Read Compression */
    switch (code = param_read_string(plist, (param_name = "Compression"), &comprstr)) {
        case 0:
            if ((ecode = tiff_compression_id(&compr, &comprstr)) < 0 ||
                !tiff_compression_allowed(compr, dev->color_info.depth))
                param_signal_error(plist, param_name, ecode);
            break;
        case 1:
            break;
        default:
            ecode = code;
            param_signal_error(plist, param_name, ecode);
    }

    if (ecode < 0)
        return ecode;
    code = gdev_fax_put_params(dev, plist);
    if (code < 0)
        return code;

    tfdev->MaxStripSize = mss;
    tfdev->FillOrder = fill_order;
    tfdev->BigEndian = big_endian;
    tfdev->UseBigTIFF = usebigtiff;
    tfdev->Compression = compr;
    tfdev->write_datetime = write_datetime;
    return code;
}

/* ---------------- Other TIFF output ---------------- */

#include "slzwx.h"
#include "srlx.h"

/* Device descriptors for TIFF formats other than fax. */
static dev_proc_print_page(tifflzw_print_page);
static dev_proc_print_page(tiffpack_print_page);

const gx_device_tfax gs_tifflzw_device = {
    prn_device_std_body(gx_device_tfax, gdev_tfax_std_procs, "tifflzw",
                        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
                        X_DPI, Y_DPI,
                        0, 0, 0, 0, /* margins */
                        1, tifflzw_print_page),
    0/* AdjustWidth */,
    0                           /* MinFeatureSize */,
    1,                          /* FillOrder */
    true,                       /* BlackIs1 */
    TIFF_DEFAULT_STRIP_SIZE     /* strip size byte count */,
    ARCH_IS_BIG_ENDIAN          /* default to native endian (i.e. use big endian iff the platform is so*/,
    false                       /* defauilt to *not* UseBigTIFF */,
    COMPRESSION_LZW
};

const gx_device_tfax gs_tiffpack_device = {
    prn_device_std_body(gx_device_tfax, gdev_tfax_std_procs, "tiffpack",
                        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
                        X_DPI, Y_DPI,
                        0, 0, 0, 0, /* margins */
                        1, tiffpack_print_page),
    0                           /* AdjustWidth */,
    0                           /* MinFeatureSize */,
    1,                          /* FillOrder */
    true,                       /* BlackIs1 */
    TIFF_DEFAULT_STRIP_SIZE     /* strip size byte count */,
    ARCH_IS_BIG_ENDIAN          /* default to native endian (i.e. use big endian iff the platform is so*/,
    false                       /* defauilt to *not* UseBigTIFF */,
    COMPRESSION_PACKBITS
};

/* Forward references */
static int tfax_begin_page(gx_device_tfax * tfdev, gp_file * file);

static void
tfax_set_fields(gx_device_tfax *tfdev)
{
    short fillorder = tfdev->FillOrder == 1 ? FILLORDER_MSB2LSB : FILLORDER_LSB2MSB;

    TIFFSetField(tfdev->tif, TIFFTAG_BITSPERSAMPLE, 1);
    TIFFSetField(tfdev->tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    TIFFSetField(tfdev->tif, TIFFTAG_FILLORDER, fillorder);
    TIFFSetField(tfdev->tif, TIFFTAG_SAMPLESPERPIXEL, 1);

    tiff_set_compression((gx_device_printer *)tfdev,
                         tfdev->tif,
                         tfdev->Compression,
                         tfdev->MaxStripSize);
}

static int
tiffcrle_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);

    tfax_set_fields(tfdev);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

static int
tiffg3_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);

    tfax_set_fields(tfdev);
    if (tfdev->Compression == COMPRESSION_CCITTFAX3)
        TIFFSetField(tfdev->tif, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_FILLBITS);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

static int
tiffg32d_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);

    tfax_set_fields(tfdev);
    if (tfdev->Compression == COMPRESSION_CCITTFAX3)
        TIFFSetField(tfdev->tif, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_2DENCODING | GROUP3OPT_FILLBITS);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

static int
tiffg4_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);

    tfax_set_fields(tfdev);
    if (tfdev->Compression == COMPRESSION_CCITTFAX4)
        TIFFSetField(tfdev->tif, TIFFTAG_GROUP4OPTIONS, 0);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

/* Print an LZW page. */
static int
tifflzw_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *const tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);
    tfax_set_fields(tfdev);
    TIFFSetField(tfdev->tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

/* Print a PackBits page. */
static int
tiffpack_print_page(gx_device_printer * dev, gp_file * prn_stream)
{
    gx_device_tfax *const tfdev = (gx_device_tfax *)dev;

    tfax_begin_page(tfdev, prn_stream);
    tfax_set_fields(tfdev);
    TIFFSetField(tfdev->tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);

    return tiff_print_page(dev, tfdev->tif, tfdev->MinFeatureSize);
}

/* Begin a TIFF fax page. */
static int
tfax_begin_page(gx_device_tfax * tfdev, gp_file * file)
{
    gx_device_printer *const pdev = (gx_device_printer *)tfdev;
    int code;

    /* open the TIFF device */
    if (gdev_prn_file_is_new(pdev)) {
        tfdev->tif = tiff_from_filep(pdev, pdev->dname, file, tfdev->BigEndian, tfdev->UseBigTIFF);
        if (!tfdev->tif)
            return_error(gs_error_invalidfileaccess);
    }

    code = tiff_set_fields_for_printer(pdev, tfdev->tif, 1, tfdev->AdjustWidth, tfdev->write_datetime);
    return code;
}