summaryrefslogtreecommitdiff
blob: 8e65a5e157f460a7eb89205490cf8e48c808c9ea (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
/* 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.
*/


/* Additional decoding filter creation */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "gsparam.h"
#include "gsstruct.h"
#include "ialloc.h"
#include "idict.h"
#include "idparam.h"
#include "ilevel.h"		/* for LL3 test */
#include "iparam.h"
#include "store.h"
#include "stream.h"		/* for setting is_temp */
#include "strimpl.h"
#include "sfilter.h"
#include "sa85x.h"
#include "scfx.h"
#include "scf.h"
#include "slzwx.h"
#include "spdiffx.h"
#include "spngpx.h"
#include "ifilter.h"
#include "ifilter2.h"
#include "ifrpred.h"

/* ------ ASCII85 filters ------ */

/* We include both encoding and decoding filters here, */
/* because it would be a nuisance to separate them. */

/* <target> ASCII85Encode/filter <file> */
/* <target> <dict> ASCII85Encode/filter <file> */
static int
zA85E(i_ctx_t *i_ctx_p)
{
    return filter_write_simple(i_ctx_p, &s_A85E_template);
}

/* <source> ASCII85Decode/filter <file> */
/* <source> <dict> ASCII85Decode/filter <file> */
static int
zA85D(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_A85D_state ss;
    int code;

    if (r_has_type(op, t_dictionary)) {
        check_dict_read(*op);
        if ((code = dict_bool_param(op, "PDFRules", false, &ss.pdf_rules)) < 0)
            return code;
    } else {
        ss.pdf_rules = false;
    }
    return filter_read(i_ctx_p, 0, &s_A85D_template, (stream_state *)&ss, 0);
}

/* ------ CCITTFaxDecode filter ------ */

/* Common setup for encoding and decoding filters. */
extern stream_state_proc_put_params(s_CF_put_params, stream_CF_state);
int
zcf_setup(os_ptr op, stream_CF_state *pcfs, gs_ref_memory_t *imem)
{
    dict_param_list list;
    int code = dict_param_list_read(&list, op, NULL, false, imem);

    if (code < 0)
        return code;
    s_CF_set_defaults_inline(pcfs);
    code = s_CF_put_params((gs_param_list *)&list, pcfs);
    iparam_list_release(&list);
    return code;
}

/* <source> <dict> CCITTFaxDecode/filter <file> */
/* <source> CCITTFaxDecode/filter <file> */
static int
zCFD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr dop;
    stream_CFD_state cfs;
    int code;

    if (r_has_type(op, t_dictionary)) {
        check_dict_read(*op);
        dop = op;
    } else
        dop = 0;
    code = zcf_setup(dop, (stream_CF_state *)&cfs, iimemory);
    if (code < 0)
        return code;
    return filter_read(i_ctx_p, 0, &s_CFD_template, (stream_state *)&cfs, 0);
}

/* ------ Common setup for possibly pixel-oriented decoding filters ------ */

int
filter_read_predictor(i_ctx_t *i_ctx_p, int npop,
                      const stream_template * templat, stream_state * st)
{
    os_ptr op = osp;
    int predictor, code;
    stream_PDiff_state pds;
    stream_PNGP_state pps;

    if (r_has_type(op, t_dictionary)) {
        if ((code = dict_int_param(op, "Predictor", 0, 15, 1, &predictor)) < 0)
            return code;
        switch (predictor) {
            case 0:		/* identity */
                predictor = 1;
            case 1:		/* identity */
                break;
            case 2:		/* componentwise horizontal differencing */
                code = zpd_setup(op, &pds);
                break;
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
                /* PNG prediction */
                code = zpp_setup(op, &pps);
                break;
            default:
                return_error(gs_error_rangecheck);
        }
        if (code < 0)
            return code;
    } else
        predictor = 1;
    if (predictor == 1)
        return filter_read(i_ctx_p, npop, templat, st, 0);
    {
        /* We need to cascade filters. */
        ref rsource, rdict;
        int code;

        /* Save the operands, just in case. */
        ref_assign(&rsource, op - 1);
        ref_assign(&rdict, op);
        code = filter_read(i_ctx_p, 1, templat, st, 0);
        if (code < 0)
            return code;
        /* filter_read changed osp.... */
        op = osp;
        code =
            (predictor == 2 ?
         filter_read(i_ctx_p, 0, &s_PDiffD_template, (stream_state *) & pds, 0) :
          filter_read(i_ctx_p, 0, &s_PNGPD_template, (stream_state *) & pps, 0));
        if (code < 0) {
            /* Restore the operands.  Don't bother trying to clean up */
            /* the first stream. */
            osp = ++op;
            ref_assign(op - 1, &rsource);
            ref_assign(op, &rdict);
            return code;
        }
        /*
         * Mark the compression stream as temporary, and propagate
         * CloseSource from it to the predictor stream.
         */
        filter_mark_strm_temp(op, 2);
        return code;
    }
}

/* ------ Generalized LZW/GIF decoding filter ------ */

/* Common setup for encoding and decoding filters. */
int
zlz_setup(os_ptr op, stream_LZW_state * plzs)
{
    int code;
    const ref *dop;

    s_LZW_set_defaults_inline(plzs);
    if (r_has_type(op, t_dictionary)) {
        check_dict_read(*op);
        dop = op;
    } else
        dop = 0;
    if (   (code = dict_int_param(dop, "EarlyChange", 0, 1, 1,
                                  &plzs->EarlyChange)) < 0 ||
           /*
            * The following are not PostScript standard, although
            * LanguageLevel 3 provides the first two under different
            * names.
            */
           (code = dict_int_param(dop, "InitialCodeLength", 2, 11, 8,
                                  &plzs->InitialCodeLength)) < 0 ||
           (code = dict_bool_param(dop, "FirstBitLowOrder", false,
                                   &plzs->FirstBitLowOrder)) < 0 ||
           (code = dict_bool_param(dop, "BlockData", false,
                                   &plzs->BlockData)) < 0
        )
        return code;
    return 0;
}

/* <source> LZWDecode/filter <file> */
/* <source> <dict> LZWDecode/filter <file> */
static int
zLZWD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_LZW_state lzs;
    int code = zlz_setup(op, &lzs);

    if (code < 0)
        return code;
    if (LL3_ENABLED && r_has_type(op, t_dictionary)) {
        int unit_size;

        if ((code = dict_bool_param(op, "LowBitFirst", lzs.FirstBitLowOrder,
                                    &lzs.FirstBitLowOrder)) < 0 ||
            (code = dict_int_param(op, "UnitSize", 3, 8, 8,
                                   &unit_size)) < 0
            )
            return code;
        if (code == 0 /* UnitSize specified */ )
            lzs.InitialCodeLength = unit_size + 1;
    }
    return filter_read_predictor(i_ctx_p, 0, &s_LZWD_template,
                                 (stream_state *) & lzs);
}

/* ------ Color differencing filters ------ */

/* We include both encoding and decoding filters here, */
/* because it would be a nuisance to separate them. */

/* Common setup for encoding and decoding filters. */
int
zpd_setup(os_ptr op, stream_PDiff_state * ppds)
{
    int code, bpc;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    if ((code = dict_int_param(op, "Colors", 1, s_PDiff_max_Colors, 1,
                               &ppds->Colors)) < 0 ||
        (code = dict_int_param(op, "BitsPerComponent", 1, 16, 8,
                               &bpc)) < 0 ||
        (bpc & (bpc - 1)) != 0 ||
        (code = dict_int_param(op, "Columns", 1, max_int, 1,
                               &ppds->Columns)) < 0
        )
        return (code < 0 ? code : gs_note_error(gs_error_rangecheck));
    ppds->BitsPerComponent = bpc;
    return 0;
}

/* <target> <dict> PixelDifferenceEncode/filter <file> */
static int
zPDiffE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_PDiff_state pds;
    int code = zpd_setup(op, &pds);

    if (code < 0)
        return code;
    return filter_write(i_ctx_p, 0, &s_PDiffE_template, (stream_state *) & pds, 0);
}

/* <source> <dict> PixelDifferenceDecode/filter <file> */
static int
zPDiffD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_PDiff_state pds;
    int code = zpd_setup(op, &pds);

    if (code < 0)
        return code;
    return filter_read(i_ctx_p, 0, &s_PDiffD_template, (stream_state *) & pds, 0);
}

/* ------ PNG pixel predictor filters ------ */

/* Common setup for encoding and decoding filters. */
int
zpp_setup(os_ptr op, stream_PNGP_state * ppps)
{
    int code, bpc;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    if ((code = dict_int_param(op, "Colors", 1, s_PNG_max_Colors, 1,
                               &ppps->Colors)) < 0 ||
        (code = dict_int_param(op, "BitsPerComponent", 1, 16, 8,
                               &bpc)) < 0 ||
        (bpc & (bpc - 1)) != 0 ||
        (code = dict_uint_param(op, "Columns", 1, max_uint, 1,
                                &ppps->Columns)) < 0 ||
        (code = dict_int_param(op, "Predictor", 10, 15, 15,
                               &ppps->Predictor)) < 0
        )
        return (code < 0 ? code : gs_note_error(gs_error_rangecheck));
    ppps->BitsPerComponent = bpc;
    return 0;
}

/* <target> <dict> PNGPredictorEncode/filter <file> */
static int
zPNGPE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_PNGP_state pps;
    int code = zpp_setup(op, &pps);

    if (code < 0)
        return code;
    return filter_write(i_ctx_p, 0, &s_PNGPE_template, (stream_state *) & pps, 0);
}

/* <source> <dict> PNGPredictorDecode/filter <file> */
static int
zPNGPD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_PNGP_state pps;
    int code = zpp_setup(op, &pps);

    if (code < 0)
        return code;
    return filter_read(i_ctx_p, 0, &s_PNGPD_template, (stream_state *) & pps, 0);
}

/* ---------------- Initialization procedure ---------------- */

const op_def zfdecode_op_defs[] = {
    op_def_begin_filter(),
    {"1ASCII85Encode", zA85E},
    {"1ASCII85Decode", zA85D},
    {"2CCITTFaxDecode", zCFD},
    {"1LZWDecode", zLZWD},
    {"2PixelDifferenceDecode", zPDiffD},
    {"2PixelDifferenceEncode", zPDiffE},
    {"2PNGPredictorDecode", zPNGPD},
    {"2PNGPredictorEncode", zPNGPE},
    op_def_end(0)
};