gdevcfax.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright (C) 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: gdevcfax.c,v 1.5 2002/02/21 22:24:51 giles Exp $ */
  14. /* SFF format writer for CAPI fax devices */
  15. #include "gdevprn.h"
  16. #include "strimpl.h"
  17. #include "scfx.h"
  18. #include "gdevfax.h"
  19. /* The device descriptor */
  20. private dev_proc_print_page(cfax_print_page);
  21. private dev_proc_close_device(cfax_prn_close);
  22. /* Define procedures for cfax. For sff multipage documents */
  23. /* a special close procedure is required because sff needs */
  24. /* an additional "end of document" signature after the last */
  25. /* "end page" signature */
  26. private const gx_device_procs gdev_cfax_std_procs =
  27. prn_params_procs(gdev_prn_open, gdev_prn_output_page, cfax_prn_close,
  28. gdev_fax_get_params, gdev_fax_put_params);
  29. const gx_device_fax gs_cfax_device = {
  30. FAX_DEVICE_BODY(gx_device_fax, gdev_cfax_std_procs, "cfax", cfax_print_page)
  31. };
  32. /* ---------------- SFF output ----------------- */
  33. private void
  34. cfax_byte(uint c, FILE * file)
  35. {
  36. fputc(c & 0xff, file);
  37. }
  38. private void
  39. cfax_word(ushort c, FILE * file)
  40. {
  41. cfax_byte(c & 0xff, file);
  42. cfax_byte(c >> 8, file);
  43. }
  44. private void
  45. cfax_dword(ulong c, FILE * file)
  46. {
  47. cfax_byte(c & 0xff, file);
  48. cfax_byte(c >> 8, file);
  49. cfax_byte(c >> 16, file);
  50. cfax_byte(c >> 24, file);
  51. }
  52. private void
  53. cfax_doc_hdr(FILE * file)
  54. {
  55. cfax_byte('S', file);
  56. cfax_byte('f', file);
  57. cfax_byte('f', file);
  58. cfax_byte('f', file);
  59. cfax_byte(1, file);
  60. cfax_byte(0, file);
  61. cfax_word(0, file);
  62. cfax_word(0, file);
  63. cfax_word(20, file);
  64. cfax_dword(0, file);
  65. cfax_dword(0, file);
  66. }
  67. private void
  68. cfax_page_hdr(gx_device_printer * pdev, FILE * file)
  69. {
  70. cfax_byte(254, file);
  71. cfax_byte(16, file);
  72. cfax_byte((pdev->y_pixels_per_inch < 100 ? 0 : 1), file);
  73. cfax_byte(0, file);
  74. cfax_byte(0, file);
  75. cfax_byte(0, file);
  76. cfax_word(pdev->width, file);
  77. cfax_word(pdev->height, file);
  78. cfax_dword(0, file);
  79. cfax_dword(0, file);
  80. }
  81. private void
  82. cfax_doc_end(FILE * file)
  83. {
  84. cfax_byte(254, file);
  85. cfax_byte(0, file);
  86. }
  87. /* Send the page to the printer. */
  88. private int
  89. cfax_stream_print_page_width(gx_device_printer * pdev, FILE * prn_stream,
  90. const stream_template * temp, stream_state * ss,
  91. int width)
  92. {
  93. gs_memory_t *mem = pdev->memory;
  94. int code = 0;
  95. stream_cursor_read r;
  96. stream_cursor_write w;
  97. int in_size = gdev_prn_raster((gx_device *) pdev);
  98. /*
  99. * Because of the width adjustment for fax systems, width may
  100. * be different from (either greater than or less than) pdev->width.
  101. * Allocate a large enough buffer to account for this.
  102. */
  103. int col_size = (width * pdev->color_info.depth + 7) >> 3;
  104. int max_size = max(in_size, col_size);
  105. int lnum, nbytes, i;
  106. byte *in;
  107. byte *out;
  108. /* If the file is 'nul', don't even do the writes. */
  109. bool nul = !strcmp(pdev->fname, "nul");
  110. /* Initialize the common part of the encoder state. */
  111. ss->template = temp;
  112. ss->memory = mem;
  113. /* Allocate the buffers. */
  114. in = gs_alloc_bytes(mem, temp->min_in_size + max_size + 1,
  115. "cfax_stream_print_page(in)");
  116. #define OUT_SIZE 1000
  117. out = gs_alloc_bytes(mem, OUT_SIZE, "cfax_stream_print_page(out)");
  118. if (in == 0 || out == 0) {
  119. code = gs_note_error(gs_error_VMerror);
  120. goto done;
  121. }
  122. /* Process the image */
  123. for (lnum = 0; lnum < pdev->height; lnum++) {
  124. /* Initialize read and write pointer each time, because they're getting modified */
  125. r.ptr = in - 1;
  126. r.limit = in + col_size;
  127. w.ptr = out - 1;
  128. w.limit = w.ptr + OUT_SIZE;
  129. /* Decoder must encode line for line, so init it for each line */
  130. code = (*temp->init) (ss);
  131. if (code < 0)
  132. return_error(gs_error_limitcheck);
  133. /* Now, get the bits and encode them */
  134. gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  135. if (col_size > in_size) {
  136. memset(in + in_size , 0, col_size - in_size);
  137. }
  138. code = (*temp->process) (ss, &r, &w, 1 /* always last line */);
  139. nbytes = w.ptr - out + 1;
  140. if (!nul) {
  141. if (nbytes > 0) {
  142. if (nbytes < 217) {
  143. cfax_byte(nbytes, prn_stream);
  144. for (i = 0; i < nbytes; i++)
  145. cfax_byte(out[i], prn_stream);
  146. } else {
  147. cfax_byte(0, prn_stream);
  148. cfax_word(nbytes, prn_stream);
  149. for (i = 0; i < nbytes; i++)
  150. cfax_byte(out[i], prn_stream);
  151. }
  152. } else {
  153. cfax_byte(218, prn_stream);
  154. }
  155. }
  156. if (temp->release != 0)
  157. (*temp->release) (ss);
  158. }
  159. #undef OUT_SIZE
  160. done:
  161. gs_free_object(mem, out, "cfax_stream_print_page(out)");
  162. gs_free_object(mem, in, "cfax_stream_print_page(in)");
  163. return code;
  164. }
  165. /* Begin a capi fax page. */
  166. private int
  167. cfax_begin_page(gx_device_printer * pdev, FILE * fp, int width)
  168. {
  169. /* Patch the width to reflect fax page width adjustment. */
  170. int save_width = pdev->width;
  171. pdev->width = width;
  172. if (gdev_prn_file_is_new(pdev)) {
  173. cfax_doc_hdr(fp);
  174. }
  175. cfax_page_hdr(pdev, fp);
  176. pdev->width = save_width;
  177. return 0;
  178. }
  179. /* Print an capi fax (sff-encoded) page. */
  180. private int
  181. cfax_print_page(gx_device_printer * pdev, FILE * prn_stream)
  182. {
  183. stream_CFE_state state;
  184. int code;
  185. gdev_fax_init_fax_state(&state, (gx_device_fax *)pdev);
  186. state.EndOfLine = false;
  187. state.EndOfBlock = false;
  188. state.EncodedByteAlign = true;
  189. state.FirstBitLowOrder = true;
  190. state.K = 0;
  191. cfax_begin_page(pdev, prn_stream, state.Columns);
  192. code = cfax_stream_print_page_width(pdev, prn_stream,
  193. &s_CFE_template, (stream_state *) &state, state.Columns);
  194. return code;
  195. }
  196. /* Close an capi fax (sff-encoded) document. */
  197. private int
  198. cfax_prn_close(gx_device * pdev)
  199. {
  200. gx_device_printer * const ppdev = (gx_device_printer *)pdev;
  201. if (ppdev->file != NULL) {
  202. cfax_doc_end(ppdev->file);
  203. }
  204. return gdev_prn_close(pdev);
  205. }