File: zfilterx.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9+squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (324 lines) | stat: -rw-r--r-- 8,887 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2001-2006 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: zfilterx.c 9043 2008-08-28 22:48:19Z giles $ */
/* Extended (non-standard) filter creation */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "gsstruct.h"
#include "ialloc.h"
#include "idict.h"
#include "idparam.h"
#include "store.h"
#include "strimpl.h"
#include "sfilter.h"
#include "sbwbs.h"
#include "sbhc.h"
#include "sbtx.h"
#include "shcgen.h"
#include "smtf.h"
#include "ifilter.h"

/* ------ Bounded Huffman code filters ------ */

/* Common setup for encoding and decoding filters */
static int
bhc_setup(os_ptr op, stream_BHC_state * pbhcs)
{
    int code;
    int num_counts;
    int data[max_hc_length + 1 + 256 + max_zero_run + 1];
    uint dsize;
    int i;
    uint num_values, accum;
    ushort *counts;
    ushort *values;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    if ((code = dict_bool_param(op, "FirstBitLowOrder", false,
				&pbhcs->FirstBitLowOrder)) < 0 ||
	(code = dict_int_param(op, "MaxCodeLength", 1, max_hc_length,
			       max_hc_length, &num_counts)) < 0 ||
	(code = dict_bool_param(op, "EndOfData", true,
				&pbhcs->EndOfData)) < 0 ||
	(code = dict_uint_param(op, "EncodeZeroRuns", 2, 256,
				256, &pbhcs->EncodeZeroRuns)) < 0 ||
    /* Note: the code returned from the following call */
    /* is actually the number of elements in the array. */
	(code = dict_int_array_param(imemory, op, "Tables", countof(data),
				     data)) <= 0
	)
	return (code < 0 ? code : gs_note_error(e_rangecheck));
    dsize = code;
    if (dsize <= num_counts + 2)
	return_error(e_rangecheck);
    for (i = 0, num_values = 0, accum = 0; i <= num_counts;
	 i++, accum <<= 1
	) {
	int count = data[i];

	if (count < 0)
	    return_error(e_rangecheck);
	num_values += count;
	accum += count;
    }
    if (dsize != num_counts + 1 + num_values ||
	accum != 1 << (num_counts + 1) ||
	pbhcs->EncodeZeroRuns >
	(pbhcs->EndOfData ? num_values - 1 : num_values)
	)
	return_error(e_rangecheck);
    for (; i < num_counts + 1 + num_values; i++) {
	int value = data[i];

	if (value < 0 || value >= num_values)
	    return_error(e_rangecheck);
    }
    pbhcs->definition.counts = counts =
	(ushort *) ialloc_byte_array(num_counts + 1, sizeof(ushort),
				     "bhc_setup(counts)");
    pbhcs->definition.values = values =
	(ushort *) ialloc_byte_array(num_values, sizeof(ushort),
				     "bhc_setup(values)");
    if (counts == 0 || values == 0) {
	ifree_object(values, "bhc_setup(values)");
	ifree_object(counts, "bhc_setup(counts)");
	return_error(e_VMerror);
    }
    for (i = 0; i <= num_counts; i++)
	counts[i] = data[i];
    pbhcs->definition.counts = counts;
    pbhcs->definition.num_counts = num_counts;
    for (i = 0; i < num_values; i++)
	values[i] = data[i + num_counts + 1];
    pbhcs->definition.values = values;
    pbhcs->definition.num_values = num_values;
    return 0;
}

/* <target> <dict> BoundedHuffmanEncode/filter <file> */
static int
zBHCE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BHCE_state bhcs;
    int code = bhc_setup(op, (stream_BHC_state *)&bhcs);

    if (code < 0)
	return code;
    return filter_write(op, 0, &s_BHCE_template, (stream_state *)&bhcs, 0);
}

/* <source> <dict> BoundedHuffmanDecode/filter <file> */
static int
zBHCD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BHCD_state bhcs;
    int code = bhc_setup(op, (stream_BHC_state *)&bhcs);

    if (code < 0)
	return code;
    return filter_read(i_ctx_p, 0, &s_BHCD_template, (stream_state *)&bhcs, 0);
}

/* <array> <max_length> .computecodes <array> */
/* The first max_length+1 elements of the array will be filled in with */
/* the code counts; the remaining elements will be replaced with */
/* the code values.  This is the form needed for the Tables element of */
/* the dictionary parameter for the BoundedHuffman filters. */
static int
zcomputecodes(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    uint asize;
    hc_definition def;
    ushort *data;
    long *freqs;
    int code = 0;

    check_type(*op, t_integer);
    check_write_type(*op1, t_array);
    asize = r_size(op1);
    if (op->value.intval < 1 || op->value.intval > max_hc_length)
	return_error(e_rangecheck);
    def.num_counts = op->value.intval;
    if (asize < def.num_counts + 2)
	return_error(e_rangecheck);
    def.num_values = asize - (def.num_counts + 1);
    data = (ushort *) gs_alloc_byte_array(imemory, asize, sizeof(ushort),
					  "zcomputecodes");
    freqs = (long *)gs_alloc_byte_array(imemory, def.num_values,
					sizeof(long),
					"zcomputecodes(freqs)");

    if (data == 0 || freqs == 0)
	code = gs_note_error(e_VMerror);
    else {
	uint i;

	def.counts = data;
	def.values = data + (def.num_counts + 1);
	for (i = 0; i < def.num_values; i++) {
	    const ref *pf = op1->value.const_refs + i + def.num_counts + 1;

	    if (!r_has_type(pf, t_integer)) {
		code = gs_note_error(e_typecheck);
		break;
	    }
	    freqs[i] = pf->value.intval;
	}
	if (!code) {
	    code = hc_compute(&def, freqs, imemory);
	    if (code >= 0) {
		/* Copy back results. */
		for (i = 0; i < asize; i++)
		    make_int(op1->value.refs + i, data[i]);
	    }
	}
    }
    gs_free_object(imemory, freqs, "zcomputecodes(freqs)");
    gs_free_object(imemory, data, "zcomputecodes");
    if (code < 0)
	return code;
    pop(1);
    return code;
}

/* ------ Burrows/Wheeler block sorting filters ------ */

/* Common setup for encoding and decoding filters */
static int
bwbs_setup(os_ptr op, stream_BWBS_state * pbwbss)
{
    int code =
	dict_int_param(op, "BlockSize", 1, max_int / sizeof(int) - 10, 16384,
		       &pbwbss->BlockSize);

    if (code < 0)
	return code;
    return 0;
}

/* <target> <dict> BWBlockSortEncode/filter <file> */
static int
zBWBSE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BWBSE_state bwbss;
    int code;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);
    if (code < 0)
	return code;
    return filter_write(op, 0, &s_BWBSE_template, (stream_state *)&bwbss, 0);
}

/* <source> <dict> BWBlockSortDecode/filter <file> */
static int
zBWBSD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BWBSD_state bwbss;
    int code = bwbs_setup(op, (stream_BWBS_state *)&bwbss);

    if (code < 0)
	return code;
    return filter_read(i_ctx_p, 0, &s_BWBSD_template, (stream_state *)&bwbss, 0);
}

/* ------ Byte translation filters ------ */

/* Common setup */
static int
bt_setup(os_ptr op, stream_BT_state * pbts)
{
    check_read_type(*op, t_string);
    if (r_size(op) != 256)
	return_error(e_rangecheck);
    memcpy(pbts->table, op->value.const_bytes, 256);
    return 0;
}

/* <target> <table> ByteTranslateEncode/filter <file> */
/* <target> <table> <dict> ByteTranslateEncode/filter <file> */
static int
zBTE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BT_state bts;
    int code = bt_setup(op, &bts);

    if (code < 0)
	return code;
    return filter_write(op, 0, &s_BTE_template, (stream_state *)&bts, 0);
}

/* <target> <table> ByteTranslateDecode/filter <file> */
/* <target> <table> <dict> ByteTranslateDecode/filter <file> */
static int
zBTD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_BT_state bts;
    int code = bt_setup(op, &bts);

    if (code < 0)
	return code;
    return filter_read(i_ctx_p, 0, &s_BTD_template, (stream_state *)&bts, 0);
}

/* ------ Move-to-front filters ------ */

/* <target> MoveToFrontEncode/filter <file> */
/* <target> <dict> MoveToFrontEncode/filter <file> */
static int
zMTFE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    return filter_write_simple(op, &s_MTFE_template);
}

/* <source> MoveToFrontDecode/filter <file> */
/* <source> <dict> MoveToFrontDecode/filter <file> */
static int
zMTFD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    return filter_read_simple(op, &s_MTFD_template);
}

/* ================ Initialization procedure ================ */

const op_def zfilterx_op_defs[] =
{
    {"2.computecodes", zcomputecodes},	/* not a filter */
    op_def_begin_filter(),
		/* Non-standard filters */
    {"2BoundedHuffmanEncode", zBHCE},
    {"2BoundedHuffmanDecode", zBHCD},
    {"2BWBlockSortEncode", zBWBSE},
    {"2BWBlockSortDecode", zBWBSD},
    {"2ByteTranslateEncode", zBTE},
    {"2ByteTranslateDecode", zBTD},
    {"1MoveToFrontEncode", zMTFE},
    {"1MoveToFrontDecode", zMTFD},
    op_def_end(0)
};