zfdecode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zfdecode.c,v 1.6 2004/03/13 22:31:19 ray Exp $ */
  14. /* Additional decoding filter creation */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "gsparam.h"
  19. #include "gsstruct.h"
  20. #include "ialloc.h"
  21. #include "idict.h"
  22. #include "idparam.h"
  23. #include "ilevel.h" /* for LL3 test */
  24. #include "iparam.h"
  25. #include "store.h"
  26. #include "stream.h" /* for setting is_temp */
  27. #include "strimpl.h"
  28. #include "sfilter.h"
  29. #include "sa85x.h"
  30. #include "scfx.h"
  31. #include "scf.h"
  32. #include "slzwx.h"
  33. #include "spdiffx.h"
  34. #include "spngpx.h"
  35. #include "ifilter.h"
  36. #include "ifilter2.h"
  37. #include "ifrpred.h"
  38. /* ------ ASCII85 filters ------ */
  39. /* We include both encoding and decoding filters here, */
  40. /* because it would be a nuisance to separate them. */
  41. /* <target> ASCII85Encode/filter <file> */
  42. /* <target> <dict> ASCII85Encode/filter <file> */
  43. private int
  44. zA85E(i_ctx_t *i_ctx_p)
  45. {
  46. return filter_write_simple(i_ctx_p, &s_A85E_template);
  47. }
  48. /* <source> ASCII85Decode/filter <file> */
  49. /* <source> <dict> ASCII85Decode/filter <file> */
  50. private int
  51. zA85D(i_ctx_t *i_ctx_p)
  52. {
  53. return filter_read_simple(i_ctx_p, &s_A85D_template);
  54. }
  55. /* ------ CCITTFaxDecode filter ------ */
  56. /* Common setup for encoding and decoding filters. */
  57. extern stream_state_proc_put_params(s_CF_put_params, stream_CF_state);
  58. int
  59. zcf_setup(os_ptr op, stream_CF_state *pcfs, gs_ref_memory_t *imem)
  60. {
  61. dict_param_list list;
  62. int code = dict_param_list_read(&list, op, NULL, false, imem);
  63. if (code < 0)
  64. return code;
  65. s_CF_set_defaults_inline(pcfs);
  66. code = s_CF_put_params((gs_param_list *)&list, pcfs);
  67. iparam_list_release(&list);
  68. return code;
  69. }
  70. /* <source> <dict> CCITTFaxDecode/filter <file> */
  71. /* <source> CCITTFaxDecode/filter <file> */
  72. private int
  73. zCFD(i_ctx_t *i_ctx_p)
  74. {
  75. os_ptr op = osp;
  76. os_ptr dop;
  77. stream_CFD_state cfs;
  78. int code;
  79. if (r_has_type(op, t_dictionary)) {
  80. check_dict_read(*op);
  81. dop = op;
  82. } else
  83. dop = 0;
  84. code = zcf_setup(dop, (stream_CF_state *)&cfs, iimemory);
  85. if (code < 0)
  86. return code;
  87. return filter_read(i_ctx_p, 0, &s_CFD_template, (stream_state *)&cfs, 0);
  88. }
  89. /* ------ Common setup for possibly pixel-oriented decoding filters ------ */
  90. int
  91. filter_read_predictor(i_ctx_t *i_ctx_p, int npop,
  92. const stream_template * template, stream_state * st)
  93. {
  94. os_ptr op = osp;
  95. int predictor, code;
  96. stream_PDiff_state pds;
  97. stream_PNGP_state pps;
  98. if (r_has_type(op, t_dictionary)) {
  99. if ((code = dict_int_param(op, "Predictor", 0, 15, 1, &predictor)) < 0)
  100. return code;
  101. switch (predictor) {
  102. case 0: /* identity */
  103. predictor = 1;
  104. case 1: /* identity */
  105. break;
  106. case 2: /* componentwise horizontal differencing */
  107. code = zpd_setup(op, &pds);
  108. break;
  109. case 10:
  110. case 11:
  111. case 12:
  112. case 13:
  113. case 14:
  114. case 15:
  115. /* PNG prediction */
  116. code = zpp_setup(op, &pps);
  117. break;
  118. default:
  119. return_error(e_rangecheck);
  120. }
  121. if (code < 0)
  122. return code;
  123. } else
  124. predictor = 1;
  125. if (predictor == 1)
  126. return filter_read(i_ctx_p, npop, template, st, 0);
  127. {
  128. /* We need to cascade filters. */
  129. ref rsource, rdict;
  130. int code;
  131. /* Save the operands, just in case. */
  132. ref_assign(&rsource, op - 1);
  133. ref_assign(&rdict, op);
  134. code = filter_read(i_ctx_p, 1, template, st, 0);
  135. if (code < 0)
  136. return code;
  137. /* filter_read changed osp.... */
  138. op = osp;
  139. code =
  140. (predictor == 2 ?
  141. filter_read(i_ctx_p, 0, &s_PDiffD_template, (stream_state *) & pds, 0) :
  142. filter_read(i_ctx_p, 0, &s_PNGPD_template, (stream_state *) & pps, 0));
  143. if (code < 0) {
  144. /* Restore the operands. Don't bother trying to clean up */
  145. /* the first stream. */
  146. osp = ++op;
  147. ref_assign(op - 1, &rsource);
  148. ref_assign(op, &rdict);
  149. return code;
  150. }
  151. /*
  152. * Mark the compression stream as temporary, and propagate
  153. * CloseSource from it to the predictor stream.
  154. */
  155. filter_mark_strm_temp(op, 2);
  156. return code;
  157. }
  158. }
  159. /* ------ Generalized LZW/GIF decoding filter ------ */
  160. /* Common setup for encoding and decoding filters. */
  161. int
  162. zlz_setup(os_ptr op, stream_LZW_state * plzs)
  163. {
  164. int code;
  165. const ref *dop;
  166. if (r_has_type(op, t_dictionary)) {
  167. check_dict_read(*op);
  168. dop = op;
  169. } else
  170. dop = 0;
  171. if ( (code = dict_int_param(dop, "EarlyChange", 0, 1, 1,
  172. &plzs->EarlyChange)) < 0 ||
  173. /*
  174. * The following are not PostScript standard, although
  175. * LanguageLevel 3 provides the first two under different
  176. * names.
  177. */
  178. (code = dict_int_param(dop, "InitialCodeLength", 2, 11, 8,
  179. &plzs->InitialCodeLength)) < 0 ||
  180. (code = dict_bool_param(dop, "FirstBitLowOrder", false,
  181. &plzs->FirstBitLowOrder)) < 0 ||
  182. (code = dict_bool_param(dop, "BlockData", false,
  183. &plzs->BlockData)) < 0
  184. )
  185. return code;
  186. return 0;
  187. }
  188. /* <source> LZWDecode/filter <file> */
  189. /* <source> <dict> LZWDecode/filter <file> */
  190. private int
  191. zLZWD(i_ctx_t *i_ctx_p)
  192. {
  193. os_ptr op = osp;
  194. stream_LZW_state lzs;
  195. int code = zlz_setup(op, &lzs);
  196. if (code < 0)
  197. return code;
  198. if (LL3_ENABLED && r_has_type(op, t_dictionary)) {
  199. int unit_size;
  200. if ((code = dict_bool_param(op, "LowBitFirst", lzs.FirstBitLowOrder,
  201. &lzs.FirstBitLowOrder)) < 0 ||
  202. (code = dict_int_param(op, "UnitSize", 3, 8, 8,
  203. &unit_size)) < 0
  204. )
  205. return code;
  206. if (code == 0 /* UnitSize specified */ )
  207. lzs.InitialCodeLength = unit_size + 1;
  208. }
  209. return filter_read_predictor(i_ctx_p, 0, &s_LZWD_template,
  210. (stream_state *) & lzs);
  211. }
  212. /* ------ Color differencing filters ------ */
  213. /* We include both encoding and decoding filters here, */
  214. /* because it would be a nuisance to separate them. */
  215. /* Common setup for encoding and decoding filters. */
  216. int
  217. zpd_setup(os_ptr op, stream_PDiff_state * ppds)
  218. {
  219. int code, bpc;
  220. check_type(*op, t_dictionary);
  221. check_dict_read(*op);
  222. if ((code = dict_int_param(op, "Colors", 1, s_PDiff_max_Colors, 1,
  223. &ppds->Colors)) < 0 ||
  224. (code = dict_int_param(op, "BitsPerComponent", 1, 16, 8,
  225. &bpc)) < 0 ||
  226. (bpc & (bpc - 1)) != 0 ||
  227. (code = dict_int_param(op, "Columns", 1, max_int, 1,
  228. &ppds->Columns)) < 0
  229. )
  230. return (code < 0 ? code : gs_note_error(e_rangecheck));
  231. ppds->BitsPerComponent = bpc;
  232. return 0;
  233. }
  234. /* <target> <dict> PixelDifferenceEncode/filter <file> */
  235. private int
  236. zPDiffE(i_ctx_t *i_ctx_p)
  237. {
  238. os_ptr op = osp;
  239. stream_PDiff_state pds;
  240. int code = zpd_setup(op, &pds);
  241. if (code < 0)
  242. return code;
  243. return filter_write(i_ctx_p, 0, &s_PDiffE_template, (stream_state *) & pds, 0);
  244. }
  245. /* <source> <dict> PixelDifferenceDecode/filter <file> */
  246. private int
  247. zPDiffD(i_ctx_t *i_ctx_p)
  248. {
  249. os_ptr op = osp;
  250. stream_PDiff_state pds;
  251. int code = zpd_setup(op, &pds);
  252. if (code < 0)
  253. return code;
  254. return filter_read(i_ctx_p, 0, &s_PDiffD_template, (stream_state *) & pds, 0);
  255. }
  256. /* ------ PNG pixel predictor filters ------ */
  257. /* Common setup for encoding and decoding filters. */
  258. int
  259. zpp_setup(os_ptr op, stream_PNGP_state * ppps)
  260. {
  261. int code, bpc;
  262. check_type(*op, t_dictionary);
  263. check_dict_read(*op);
  264. if ((code = dict_int_param(op, "Colors", 1, 16, 1,
  265. &ppps->Colors)) < 0 ||
  266. (code = dict_int_param(op, "BitsPerComponent", 1, 16, 8,
  267. &bpc)) < 0 ||
  268. (bpc & (bpc - 1)) != 0 ||
  269. (code = dict_uint_param(op, "Columns", 1, max_uint, 1,
  270. &ppps->Columns)) < 0 ||
  271. (code = dict_int_param(op, "Predictor", 10, 15, 15,
  272. &ppps->Predictor)) < 0
  273. )
  274. return (code < 0 ? code : gs_note_error(e_rangecheck));
  275. ppps->BitsPerComponent = bpc;
  276. return 0;
  277. }
  278. /* <target> <dict> PNGPredictorEncode/filter <file> */
  279. private int
  280. zPNGPE(i_ctx_t *i_ctx_p)
  281. {
  282. os_ptr op = osp;
  283. stream_PNGP_state pps;
  284. int code = zpp_setup(op, &pps);
  285. if (code < 0)
  286. return code;
  287. return filter_write(i_ctx_p, 0, &s_PNGPE_template, (stream_state *) & pps, 0);
  288. }
  289. /* <source> <dict> PNGPredictorDecode/filter <file> */
  290. private int
  291. zPNGPD(i_ctx_t *i_ctx_p)
  292. {
  293. os_ptr op = osp;
  294. stream_PNGP_state pps;
  295. int code = zpp_setup(op, &pps);
  296. if (code < 0)
  297. return code;
  298. return filter_read(i_ctx_p, 0, &s_PNGPD_template, (stream_state *) & pps, 0);
  299. }
  300. /* ---------------- Initialization procedure ---------------- */
  301. const op_def zfdecode_op_defs[] = {
  302. op_def_begin_filter(),
  303. {"1ASCII85Encode", zA85E},
  304. {"1ASCII85Decode", zA85D},
  305. {"2CCITTFaxDecode", zCFD},
  306. {"1LZWDecode", zLZWD},
  307. {"2PixelDifferenceDecode", zPDiffD},
  308. {"2PixelDifferenceEncode", zPDiffE},
  309. {"2PNGPredictorDecode", zPNGPD},
  310. {"2PNGPredictorEncode", zPNGPE},
  311. op_def_end(0)
  312. };