home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / misc / elcheapofax / rcs / tofax.c,v < prev    next >
Text File  |  1993-12-21  |  21KB  |  656 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     OCT93:1.2;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    93.06.11.16.33.37;    author Rhialto;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    93.06.11.14.53.53;    author Rhialto;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @Convert bitmaps to g3 data
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @First real RCS checkin
  28. @
  29. text
  30. @/* Derived from: */
  31. /*
  32.  *    Copyright 1992 DigiBoard, Inc. All rights reserved
  33.  *
  34.  * Permission to use, copy, modify, and distribute this software and its
  35.  * documentation for any purpose and without fee is hereby granted.
  36.  * This software is provided "as is" without express or implied warranty.
  37.  */
  38. /* $Id$
  39.  * $Log$
  40.  */
  41.  
  42. /* gdevdfax.c */
  43. /* DigiBoard, Inc. DigiFAX driver for Ghostscript. */
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "g3.h"
  49. #include "faxfile.h"
  50.  
  51. /**********************************************************************/
  52. /*
  53.  *    Generic fax output library
  54.  */
  55. /**********************************************************************/
  56.  
  57. typedef struct faxout
  58. {
  59.     FILE           *fp;
  60.     int        fax_byte;
  61.     int        fax_weight;
  62.     int        pages;
  63.     int        raw;    /* 1: no header, 2: no EndOfPage */
  64.     char           *stdiobuf;
  65. } FAXOUT;
  66.  
  67. struct faxout  *faxout_open (char *, int);
  68. struct faxout  *faxout_open_fp (FILE *, int);
  69. int    faxout_begin_page (FAXOUT *, int);
  70. int    faxout_eolcode (FAXOUT *);
  71. int    faxout_end_page (FAXOUT *);
  72. int    faxout_close (FAXOUT *);
  73. unsigned short fax_ushort (unsigned short);
  74. void   tofax(struct faxout *faxp, unsigned char *p, int linebits);
  75.  
  76. void putwhitespan(register FAXOUT *faxp, int c);
  77. void putblackspan(register FAXOUT *faxp, int c);
  78. void putcode(register FAXOUT *faxp, tableentry *te);
  79. void puteol(register FAXOUT *faxp);
  80. void putbit(register FAXOUT *faxp, int d);
  81. void flushbits(register FAXOUT *faxp);
  82.  
  83. /*************************************************************************
  84.     Internal routines
  85.  *************************************************************************/
  86.  
  87. /************************Coding FAX Routines*************************/
  88. /*
  89.  *    faxp = faxout_open(filename, raw);
  90.  *    faxp = faxout_open_fp(fp, raw);
  91.  *    faxout_begin_page(faxp, resolution);
  92.  *    for(;;) tofax(faxp, linebuf, linebits);
  93.  *    faxout_end_page(faxp);
  94.  *    faxout_close(faxp);
  95.  */
  96.  
  97. FAXOUT    *
  98. faxout_open_fp(FILE *fp, int raw)
  99. {
  100.     register FAXOUT *faxp;
  101.  
  102.     faxp = (FAXOUT *) malloc(sizeof(*faxp));
  103.  
  104.     faxp->fp = fp;
  105.     faxp->fax_byte = 0;
  106.     faxp->fax_weight = 0x80;
  107.     faxp->pages = 0;
  108.     faxp->raw = raw;
  109. #ifdef _DCC
  110.     setvbuf(fp, NULL, _IOFBF, 16384);
  111. #else
  112.     if (faxp->stdiobuf = malloc(16384))
  113.         setvbuf(fp, faxp->stdiobuf, _IOFBF, 16384);
  114. #endif
  115.     return (faxp);
  116. }
  117.  
  118. FAXOUT    *
  119. faxout_open(char *filename, int raw)
  120. {
  121.     register FILE    *fp;
  122.  
  123.     if (filename)
  124.         fp = fopen(filename, "wb");
  125.     else
  126.         fp = stdout;
  127.     if (!fp) return (NULL);
  128.  
  129.     return(faxout_open_fp(fp, raw));
  130. }
  131.  
  132. int
  133. faxout_begin_page(FAXOUT *faxp, int resolution)
  134. {
  135.     if (faxp->raw == 0) {
  136.         FAXHDR    hdr;
  137.  
  138.         memset(&hdr, 0, sizeof(hdr));
  139.         memcpy(hdr.id.magic, FAXMAGIC, sizeof(hdr.id.magic));
  140.         hdr.info.pagenum = fax_ushort(++faxp->pages);
  141.         hdr.info.msbfirst = 1;
  142.         hdr.info.hires = resolution;
  143.         hdr.jtinfo.jthires = resolution ? 0x40 : 0;
  144.         fwrite((char*)&hdr, sizeof(hdr), 1, faxp->fp);
  145.     }
  146.  
  147.     puteol(faxp);
  148.  
  149.     return (0);
  150. }
  151.  
  152. int
  153. faxout_end_page(FAXOUT *faxp)
  154. {
  155.     flushbits(faxp);
  156.     if (faxp->raw < 2) {
  157.         puteol(faxp);
  158.         puteol(faxp);
  159.         puteol(faxp);
  160.         puteol(faxp);
  161.         puteol(faxp);
  162.         flushbits(faxp);
  163.     }
  164.     return (0);
  165. }
  166.  
  167. int
  168. faxout_close(FAXOUT *faxp)
  169. {
  170.     unsigned short p = fax_ushort(faxp->pages);
  171.  
  172.     fflush(faxp->fp);
  173.     if (faxp->raw == 0 && fseek(faxp->fp, (long) OFFSET_PAGES, 0) == 0)
  174.         fwrite((char*)&p, sizeof(p), 1, faxp->fp);
  175.     fclose(faxp->fp);
  176. #ifndef _DCC
  177.     if (faxp->stdiobuf)
  178.         free(faxp->stdiobuf);
  179. #endif
  180.     free(faxp);
  181.  
  182.     return (0);
  183. }
  184.  
  185. unsigned short
  186. fax_ushort(unsigned short v)
  187. {
  188.     static unsigned short    x = 0x1122;
  189.     static unsigned short    *xp = &x;
  190.  
  191.     if ( *((unsigned char *)xp) == 0x22)
  192.         return (v);
  193.     else
  194.         return ( ((v>>8)&255) + (v<<8) );
  195. }
  196.  
  197. const unsigned char   b_run_tbl[8][256] =
  198. {
  199.   {    /* START BIT 0 */
  200.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  201.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  202.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  203.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  204.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  205.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  206.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  207.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  208.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  209.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  210.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  211.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  212.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  213.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  214.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  215.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  216.   },
  217.   {    /* START BIT 1 */
  218.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  219.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  220.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  221.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  222.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  223.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  224.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  225.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  226.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  227.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  228.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  229.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  230.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  231.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  232.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  233.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2,
  234.   },
  235.   {    /* START BIT 2 */
  236.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  237.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  238.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  239.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  240.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  241.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  242.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  243.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  244.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  245.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  246.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  247.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  248.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  249.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  250.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  251.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3,
  252.   },
  253.   {    /* START BIT 3 */
  254.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  255.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  256.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  257.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  258.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  259.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  260.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  261.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  262.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  263.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  264.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  265.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  266.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  267.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  268.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  269.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4,
  270.   },
  271.   {    /* START BIT 4 */
  272.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  273.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  274.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  275.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  276.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  277.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  278.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  279.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  280.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  281.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  282.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  283.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  284.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  285.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
  286.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,