home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZFILTER2.C < prev    next >
C/C++ Source or Header  |  1992-08-02  |  7KB  |  249 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfilter2.c */
  21. /* Additional filter creation for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "dict.h"
  27. #include "dparam.h"
  28. #include "stream.h"
  29.  
  30. /* Imported from zfilter.c */
  31. int filter_read(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  32. int filter_write(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  33.  
  34. /* Names of keys in CCITTFax filter dictionary: */
  35. static ref name_Uncompressed;
  36. static ref name_K;
  37. static ref name_EndOfLine;
  38. static ref name_EncodedByteAlign;
  39. static ref name_Columns;
  40. static ref name_Rows;
  41. static ref name_EndOfBlock;
  42. static ref name_BlackIs1;
  43. static ref name_DamagedRowsBeforeError;
  44.  
  45. /* Initialization */
  46. private void
  47. zfilter2_init()
  48. {    static const names_def faxn[] = {
  49.  
  50.     /* Create the names of the known entries in */
  51.     /* a CCITTFax filter dictionary. */
  52.        { "Uncompressed", &name_Uncompressed },
  53.        { "K", &name_K },
  54.        { "EndOfLine", &name_EndOfLine },
  55.        { "EncodedByteAlign", &name_EncodedByteAlign },
  56.        { "Columns", &name_Columns },
  57.        { "Rows", &name_Rows },
  58.        { "EndOfBlock", &name_EndOfBlock },
  59.        { "BlackIs1", &name_BlackIs1 },
  60.        { "DamagedRowsBeforeError", &name_DamagedRowsBeforeError },
  61.  
  62.     /* Mark the end of the initalized name list. */
  63.        names_def_end
  64.     };
  65.  
  66.     init_names(faxn);
  67. }
  68.  
  69. /* ------ ASCII85 filters ------ */
  70.  
  71. /* .filterASCII85Encode */
  72. extern const stream_procs s_A85E_procs;
  73. int
  74. zA85E(os_ptr op)
  75. {    return filter_write(op, &s_A85E_procs, NULL, 0);
  76. }
  77.  
  78. /* .filterASCII85Decode */
  79. extern const stream_procs s_A85D_procs;
  80. int
  81. zA85D(os_ptr op)
  82. {    return filter_read(op, &s_A85D_procs, NULL, 0);
  83. }
  84.  
  85. /* ------ CCITTFax filters ------ */
  86.  
  87. /* Common setup for encoding and decoding filters. */
  88. private int
  89. cf_setup(os_ptr op, CCITTFax_state *pcfs)
  90. {    int code;
  91.     check_type(*op, t_dictionary);
  92.     check_dict_read(*op);
  93.     if ( (code = dict_bool_param(op, &name_Uncompressed, 0,
  94.                      &pcfs->Uncompressed)) < 0 ||
  95.          (code = dict_int_param(op, &name_K, -9999, 9999, 0,
  96.                     &pcfs->K)) < 0 ||
  97.          (code = dict_bool_param(op, &name_EndOfLine, 0,
  98.                      &pcfs->EndOfLine)) < 0 ||
  99.          (code = dict_bool_param(op, &name_EncodedByteAlign, 0,
  100.                      &pcfs->EncodedByteAlign)) < 0 ||
  101.          (code = dict_int_param(op, &name_Columns, 0, 9999, 1728,
  102.                     &pcfs->Columns)) < 0 ||
  103.          (code = dict_int_param(op, &name_Rows, 0, 9999, 0,
  104.                     &pcfs->Rows)) < 0 ||
  105.          (code = dict_bool_param(op, &name_EndOfBlock, 1,
  106.                      &pcfs->EndOfBlock)) < 0 ||
  107.          (code = dict_bool_param(op, &name_BlackIs1, 0,
  108.                      &pcfs->BlackIs1)) < 0 ||
  109.          (code = dict_int_param(op, &name_DamagedRowsBeforeError, 0, 9999,
  110.                     0, &pcfs->DamagedRowsBeforeError)) < 0
  111.        )
  112.         return code;
  113.     pcfs->raster = (pcfs->Columns + 7) >> 3;
  114.     return 0;
  115. }
  116.  
  117. /* .filterCCITTFaxEncode */
  118. extern const stream_procs s_CFE_procs;
  119. extern void s_CFE_init(P2(stream *, CCITTFax_state *));
  120. int
  121. zCFE(os_ptr op)
  122. {    CCITTFax_state cfs;
  123.     stream *s;
  124.     int code = cf_setup(op, &cfs);
  125.     if ( code < 0 ) return code;
  126.     /* We need room for 2 full scan lines + 1 byte to handle 2-D. */
  127.     code = filter_write(op - 1, &s_CFE_procs, &s, cfs.raster * 2 + 1);
  128.     if ( code < 0 ) return code;
  129.     s_CFE_init(s, &cfs);
  130.     pop(1);
  131.     return 0;
  132. }
  133.  
  134. /* .filterCCITTFaxDecode */
  135. extern const stream_procs s_CFD_procs;
  136. extern void s_CFD_init(P2(stream *, CCITTFax_state *));
  137. int
  138. zCFD(os_ptr op)
  139. {    CCITTFax_state cfs;
  140.     stream *s;
  141.     int code = cf_setup(op, &cfs);
  142.     if ( code < 0 ) return code;
  143.     /* We need room for 3 full scan lines to handle 2-D. */
  144.     code = filter_read(op - 1, &s_CFD_procs, &s, cfs.raster * 3 + 1);
  145.     if ( code < 0 ) return code;
  146.     s_CFD_init(s, &cfs);
  147.     pop(1);
  148.     return 0;
  149. }
  150.  
  151. /* ------ LZW filters ------ */
  152.  
  153. /* .filterLZWEncode */
  154. extern const stream_procs s_LZWE_procs;
  155. extern const uint s_LZWE_table_sizeof;
  156. typedef struct lzw_encode_table_s lzw_encode_table;
  157. extern void s_LZWE_init(P3(stream *, lzw_encode_table *, int));
  158. int
  159. zLZWE_open(os_ptr op, int enhanced)
  160. {    stream *s;
  161.     int code = filter_write(op, &s_LZWE_procs, &s, 0);
  162.     lzw_encode_table *table;
  163.     if ( code < 0 ) return code;
  164.     table = (lzw_encode_table *)alloc(1, s_LZWE_table_sizeof,
  165.                       "filterLZWEncode(table)");
  166.     if ( table == 0 ) return e_VMerror;
  167.     s_LZWE_init(s, table, enhanced);
  168.     return code;
  169. }
  170. int
  171. zLZWE(os_ptr op)
  172. {    return zLZWE_open(op, 0);
  173. }
  174. int
  175. zALZWE(os_ptr op)
  176. {    return zLZWE_open(op, 1);
  177. }
  178.  
  179. /* .filterLZWDecode */
  180. extern const stream_procs s_LZWD_procs;
  181. extern const uint s_LZWD_table_sizeof;
  182. typedef struct lzw_decode_table_s lzw_decode_table;
  183. extern void s_LZWD_init(P3(stream *, lzw_decode_table *, int));
  184. int
  185. zLZWD_open(os_ptr op, int enhanced)
  186. {    stream *s;
  187.     int code = filter_read(op, &s_LZWD_procs, &s, 0);
  188.     lzw_decode_table *table;
  189.     if ( code < 0 ) return code;
  190.     table = (lzw_decode_table *)alloc(1, s_LZWD_table_sizeof,
  191.                       "filterLZWDecode(table)");
  192.     if ( table == 0 ) return e_VMerror;
  193.     s_LZWD_init(s, table, enhanced);
  194.     return 0;
  195. }
  196. int
  197. zLZWD(os_ptr op)
  198. {    return zLZWD_open(op, 0);
  199. }
  200. int
  201. zALZWD(os_ptr op)
  202. {    return zLZWD_open(op, 1);
  203. }
  204.  
  205. /* ------ RunLength filters ------ */
  206.  
  207. /* .filterRunLengthEncode */
  208. extern const stream_procs s_RLE_procs;
  209. extern void s_RLE_init(P2(stream *, uint));
  210. int
  211. zRLE(register os_ptr op)
  212. {    stream *s;
  213.     int code;
  214.     check_type(*op, t_integer);
  215.     if ( (ulong)(op->value.intval) > max_uint )
  216.         return e_rangecheck;
  217.     code = filter_write(op - 1, &s_RLE_procs, &s, 0);
  218.     if ( code < 0 ) return code;
  219.     s_RLE_init(s, (uint)(op->value.intval));
  220.     pop(1);
  221.     return 0;
  222. }
  223.  
  224. /* .filterRunLengthDecode */
  225. extern const stream_procs s_RLD_procs;
  226. extern void s_RLD_init(P1(stream *));
  227. int
  228. zRLD(os_ptr op)
  229. {    stream *s;
  230.     int code = filter_read(op, &s_RLD_procs, &s, 0);
  231.     if ( code < 0 ) return code;
  232.     s_RLD_init(s);
  233.     return 0;
  234. }
  235.  
  236. /* ------ Initialization procedure ------ */
  237.  
  238. op_def zfilter2_op_defs[] = {
  239.     {"1.filterASCII85Encode", zA85E},
  240.     {"1.filterASCII85Decode", zA85D},
  241.     {"2.filterCCITTFaxEncode", zCFE},
  242.     {"2.filterCCITTFaxDecode", zCFD},
  243.     {"1.filterLZWDecode", zLZWD},
  244.     {"1.filterLZWEncode", zLZWE},
  245.     {"2.filterRunLengthEncode", zRLE},
  246.     {"1.filterRunLengthDecode", zRLD},
  247.     op_def_end(zfilter2_init)
  248. };
  249.