zfilter2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 1991, 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: zfilter2.c,v 1.6 2004/09/26 16:19:27 ray Exp $ */
  14. /* Additional filter creation */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "gsstruct.h"
  19. #include "ialloc.h"
  20. #include "idict.h"
  21. #include "idparam.h"
  22. #include "store.h"
  23. #include "strimpl.h"
  24. #include "sfilter.h"
  25. #include "scfx.h"
  26. #include "slzwx.h"
  27. #include "spdiffx.h"
  28. #include "spngpx.h"
  29. #include "ifilter.h"
  30. #include "ifilter2.h"
  31. #include "ifwpred.h"
  32. /* ------ CCITTFaxEncode filter ------ */
  33. /* <target> <dict> CCITTFaxEncode/filter <file> */
  34. private int
  35. zCFE(i_ctx_t *i_ctx_p)
  36. {
  37. os_ptr op = osp;
  38. stream_CFE_state cfs;
  39. int code;
  40. check_type(*op, t_dictionary);
  41. check_dict_read(*op);
  42. code = zcf_setup(op, (stream_CF_state *)&cfs, iimemory);
  43. if (code < 0)
  44. return code;
  45. return filter_write(i_ctx_p, 0, &s_CFE_template, (stream_state *)&cfs, 0);
  46. }
  47. /* ------ Common setup for possibly pixel-oriented encoding filters ------ */
  48. int
  49. filter_write_predictor(i_ctx_t *i_ctx_p, int npop,
  50. const stream_template * template, stream_state * st)
  51. {
  52. os_ptr op = osp;
  53. int predictor, code;
  54. stream_PDiff_state pds;
  55. stream_PNGP_state pps;
  56. if (r_has_type(op, t_dictionary)) {
  57. if ((code = dict_int_param(op, "Predictor", 0, 15, 1, &predictor)) < 0)
  58. return code;
  59. switch (predictor) {
  60. case 0: /* identity */
  61. predictor = 1;
  62. case 1: /* identity */
  63. break;
  64. case 2: /* componentwise horizontal differencing */
  65. code = zpd_setup(op, &pds);
  66. break;
  67. case 10:
  68. case 11:
  69. case 12:
  70. case 13:
  71. case 14:
  72. case 15:
  73. /* PNG prediction */
  74. code = zpp_setup(op, &pps);
  75. break;
  76. default:
  77. return_error(e_rangecheck);
  78. }
  79. if (code < 0)
  80. return code;
  81. } else
  82. predictor = 1;
  83. if (predictor == 1)
  84. return filter_write(i_ctx_p, npop, template, st, 0);
  85. {
  86. /* We need to cascade filters. */
  87. ref rtarget, rdict;
  88. int code;
  89. /* Save the operands, just in case. */
  90. ref_assign(&rtarget, op - 1);
  91. ref_assign(&rdict, op);
  92. code = filter_write(i_ctx_p, npop, template, st, 0);
  93. if (code < 0)
  94. return code;
  95. /* filter_write changed osp.... */
  96. op = osp;
  97. code =
  98. (predictor == 2 ?
  99. filter_write(i_ctx_p, 0, &s_PDiffE_template, (stream_state *)&pds, 0) :
  100. filter_write(i_ctx_p, 0, &s_PNGPE_template, (stream_state *)&pps, 0));
  101. if (code < 0) {
  102. /* Restore the operands. Don't bother trying to clean up */
  103. /* the first stream. */
  104. osp = ++op;
  105. ref_assign(op - 1, &rtarget);
  106. ref_assign(op, &rdict);
  107. return code;
  108. }
  109. /*
  110. * Mark the compression stream as temporary, and propagate
  111. * CloseTarget from it to the predictor stream.
  112. */
  113. filter_mark_strm_temp(op, 2);
  114. return code;
  115. }
  116. }
  117. /* ------ LZW encoding filter ------ */
  118. /* <target> LZWEncode/filter <file> */
  119. /* <target> <dict> LZWEncode/filter <file> */
  120. private int
  121. zLZWE(i_ctx_t *i_ctx_p)
  122. {
  123. os_ptr op = osp;
  124. stream_LZW_state lzs;
  125. int code = zlz_setup(op, &lzs);
  126. if (code < 0)
  127. return code;
  128. return filter_write_predictor(i_ctx_p, 0, &s_LZWE_template,
  129. (stream_state *) & lzs);
  130. }
  131. /* ================ Initialization procedure ================ */
  132. const op_def zfilter2_op_defs[] =
  133. {
  134. op_def_begin_filter(),
  135. {"2CCITTFaxEncode", zCFE},
  136. {"1LZWEncode", zLZWE},
  137. op_def_end(0)
  138. };