]>
git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/zfilterx.c
1 /* Copyright (C) 1995, 1996, 1998 Aladdin Enterprises. All rights reserved.
3 This file is part of GNU Ghostscript.
5 GNU Ghostscript is distributed in the hope that it will be useful, but
6 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
7 to anyone for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing. Refer
9 to the GNU General Public License for full details.
11 Everyone is granted permission to copy, modify and redistribute GNU
12 Ghostscript, but only under the conditions described in the GNU General
13 Public License. A copy of this license is supposed to have been given
14 to you along with GNU Ghostscript so you can know your rights and
15 responsibilities. It should be in a file named COPYING. Among other
16 things, the copyright notice and this notice must be preserved on all
19 Aladdin Enterprises supports the work of the GNU Project, but is not
20 affiliated with the Free Software Foundation or the GNU Project. GNU
21 Ghostscript, as distributed by Aladdin Enterprises, does not require any
22 GNU software to build or run it.
26 /* Extended (non-standard) filter creation */
45 /* ------ Bounded Huffman code filters ------ */
47 /* Common setup for encoding and decoding filters */
49 bhc_setup(os_ptr op
, stream_BHC_state
* pbhcs
)
53 int data
[max_hc_length
+ 1 + 256 + max_zero_run
+ 1];
56 uint num_values
, accum
;
60 check_type(*op
, t_dictionary
);
62 if ((code
= dict_bool_param(op
, "FirstBitLowOrder", false,
63 &pbhcs
->FirstBitLowOrder
)) < 0 ||
64 (code
= dict_int_param(op
, "MaxCodeLength", 1, max_hc_length
,
65 max_hc_length
, &num_counts
)) < 0 ||
66 (code
= dict_bool_param(op
, "EndOfData", true,
67 &pbhcs
->EndOfData
)) < 0 ||
68 (code
= dict_uint_param(op
, "EncodeZeroRuns", 2, 256,
69 256, &pbhcs
->EncodeZeroRuns
)) < 0 ||
70 /* Note: the code returned from the following call */
71 /* is actually the number of elements in the array. */
72 (code
= dict_int_array_param(op
, "Tables", countof(data
),
75 return (code
< 0 ? code
: gs_note_error(e_rangecheck
));
77 if (dsize
<= num_counts
+ 2)
78 return_error(e_rangecheck
);
79 for (i
= 0, num_values
= 0, accum
= 0; i
<= num_counts
;
85 return_error(e_rangecheck
);
89 if (dsize
!= num_counts
+ 1 + num_values
||
90 accum
!= 1 << (num_counts
+ 1) ||
91 pbhcs
->EncodeZeroRuns
>
92 (pbhcs
->EndOfData
? num_values
- 1 : num_values
)
94 return_error(e_rangecheck
);
95 for (; i
< num_counts
+ 1 + num_values
; i
++) {
98 if (value
< 0 || value
>= num_values
)
99 return_error(e_rangecheck
);
101 pbhcs
->definition
.counts
= counts
=
102 (ushort
*) ialloc_byte_array(num_counts
+ 1, sizeof(ushort
),
103 "bhc_setup(counts)");
104 pbhcs
->definition
.values
= values
=
105 (ushort
*) ialloc_byte_array(num_values
, sizeof(ushort
),
106 "bhc_setup(values)");
107 if (counts
== 0 || values
== 0) {
108 ifree_object(values
, "bhc_setup(values)");
109 ifree_object(counts
, "bhc_setup(counts)");
110 return_error(e_VMerror
);
112 for (i
= 0; i
<= num_counts
; i
++)
114 pbhcs
->definition
.counts
= counts
;
115 pbhcs
->definition
.num_counts
= num_counts
;
116 for (i
= 0; i
< num_values
; i
++)
117 values
[i
] = data
[i
+ num_counts
+ 1];
118 pbhcs
->definition
.values
= values
;
119 pbhcs
->definition
.num_values
= num_values
;
123 /* <target> <dict> BoundedHuffmanEncode/filter <file> */
127 stream_BHCE_state bhcs
;
128 int code
= bhc_setup(op
, (stream_BHC_state
*)&bhcs
);
132 return filter_write(op
, 0, &s_BHCE_template
, (stream_state
*)&bhcs
, 0);
135 /* <source> <dict> BoundedHuffmanDecode/filter <file> */
139 stream_BHCD_state bhcs
;
140 int code
= bhc_setup(op
, (stream_BHC_state
*)&bhcs
);
144 return filter_read(op
, 0, &s_BHCD_template
, (stream_state
*)&bhcs
, 0);
147 /* <array> <max_length> .computecodes <array> */
148 /* The first max_length+1 elements of the array will be filled in with */
149 /* the code counts; the remaining elements will be replaced with */
150 /* the code values. This is the form needed for the Tables element of */
151 /* the dictionary parameter for the BoundedHuffman filters. */
153 zcomputecodes(os_ptr op
)
162 check_type(*op
, t_integer
);
163 check_write_type(*op1
, t_array
);
165 if (op
->value
.intval
< 1 || op
->value
.intval
> max_hc_length
)
166 return_error(e_rangecheck
);
167 def
.num_counts
= op
->value
.intval
;
168 if (asize
< def
.num_counts
+ 2)
169 return_error(e_rangecheck
);
170 def
.num_values
= asize
- (def
.num_counts
+ 1);
171 data
= (ushort
*) gs_alloc_byte_array(imemory
, asize
, sizeof(ushort
),
173 freqs
= (long *)gs_alloc_byte_array(imemory
, def
.num_values
,
175 "zcomputecodes(freqs)");
177 if (data
== 0 || freqs
== 0)
178 code
= gs_note_error(e_VMerror
);
183 def
.values
= data
+ (def
.num_counts
+ 1);
184 for (i
= 0; i
< def
.num_values
; i
++) {
185 const ref
*pf
= op1
->value
.const_refs
+ i
+ def
.num_counts
+ 1;
187 if (!r_has_type(pf
, t_integer
)) {
188 code
= gs_note_error(e_typecheck
);
191 freqs
[i
] = pf
->value
.intval
;
194 code
= hc_compute(&def
, freqs
, imemory
);
196 /* Copy back results. */
197 for (i
= 0; i
< asize
; i
++)
198 make_int(op1
->value
.refs
+ i
, data
[i
]);
202 gs_free_object(imemory
, freqs
, "zcomputecodes(freqs)");
203 gs_free_object(imemory
, data
, "zcomputecodes");
210 /* ------ Burrows/Wheeler block sorting filters ------ */
212 /* Common setup for encoding and decoding filters */
214 bwbs_setup(os_ptr op
, stream_BWBS_state
* pbwbss
)
217 dict_int_param(op
, "BlockSize", 1, max_int
/ sizeof(int) - 10, 16384,
225 /* <target> <dict> BWBlockSortEncode/filter <file> */
229 stream_BWBSE_state bwbss
;
232 check_type(*op
, t_dictionary
);
233 check_dict_read(*op
);
234 code
= bwbs_setup(op
, (stream_BWBS_state
*)&bwbss
);
237 return filter_write(op
, 0, &s_BWBSE_template
, (stream_state
*)&bwbss
, 0);
240 /* <source> <dict> BWBlockSortDecode/filter <file> */
244 stream_BWBSD_state bwbss
;
245 int code
= bwbs_setup(op
, (stream_BWBS_state
*)&bwbss
);
249 return filter_read(op
, 0, &s_BWBSD_template
, (stream_state
*)&bwbss
, 0);
252 /* ------ Byte translation filters ------ */
256 bt_setup(os_ptr op
, stream_BT_state
* pbts
)
258 check_read_type(*op
, t_string
);
259 if (r_size(op
) != 256)
260 return_error(e_rangecheck
);
261 memcpy(pbts
->table
, op
->value
.const_bytes
, 256);
265 /* <target> <table> ByteTranslateEncode/filter <file> */
266 /* <target> <table> <dict> ByteTranslateEncode/filter <file> */
271 int code
= bt_setup(op
, &bts
);
275 return filter_write(op
, 0, &s_BTE_template
, (stream_state
*)&bts
, 0);
278 /* <target> <table> ByteTranslateDecode/filter <file> */
279 /* <target> <table> <dict> ByteTranslateDecode/filter <file> */
284 int code
= bt_setup(op
, &bts
);
288 return filter_read(op
, 0, &s_BTD_template
, (stream_state
*)&bts
, 0);
291 /* ------ Move-to-front filters ------ */
293 /* <target> MoveToFrontEncode/filter <file> */
294 /* <target> <dict> MoveToFrontEncode/filter <file> */
298 return filter_write_simple(op
, &s_MTFE_template
);
301 /* <source> MoveToFrontDecode/filter <file> */
302 /* <source> <dict> MoveToFrontDecode/filter <file> */
306 return filter_read_simple(op
, &s_MTFD_template
);
309 /* ------ PCX decoding filter ------ */
311 /* <source> PCXDecode/filter <file> */
312 /* <source> <dict> PCXDecode/filter <file> */
316 return filter_read_simple(op
, &s_PCXD_template
);
319 /* ================ Initialization procedure ================ */
321 const op_def zfilterx_op_defs
[] =
323 {"2.computecodes", zcomputecodes
}, /* not a filter */
324 op_def_begin_filter(),
325 /* Non-standard filters */
326 {"2BoundedHuffmanEncode", zBHCE
},
327 {"2BoundedHuffmanDecode", zBHCD
},
328 {"2BWBlockSortEncode", zBWBSE
},
329 {"2BWBlockSortDecode", zBWBSD
},
330 {"2ByteTranslateEncode", zBTE
},
331 {"2ByteTranslateDecode", zBTD
},
332 {"1MoveToFrontEncode", zMTFE
},
333 {"1MoveToFrontDecode", zMTFD
},
334 {"1PCXDecode", zPCXD
},