6 #define EFAX_PATH_MAX 1024
8 /* T.4 fax encoding/decoding */
11 #define uchar unsigned char
14 #define DEFPGLINES 66 /* default lines per page */
18 /* The maximum scan line width, MAXRUNS, is conservatively
19 set at 8k pels. This is enough for the longest standard T.4 coding line
20 width (2432 pels), the longest encodeable run (2623 pels) and with
21 32-pel-wide characters allows up to 256 characters per line. Converted
22 to T.4 codes, each pair of runs takes up to 25 bits to code. MAXCODES
23 must also be at least the maximum minimum line length (1200 cps*40 ms ~=
27 #define MAXBITS (MAXRUNS/8+1)
28 #define MAXCODES (MAXRUNS*25/8/2+1)
30 /* Line/font size limits */
32 #define MAXLINELEN 256 /* maximum length of string */
33 #define MAXFONTW 32 /* maximum char width */
34 #define MAXFONTH 48 /* maximum char height */
35 #define MAXFONTBUF (MAXFONTW*MAXFONTH/8*256) /* PBM font buffer size */
37 /* Longest run encodeable by the T.4 encoding tables used. */
39 #define MAXRUNLEN (2560+63)
41 /* Codes for EOL and number of EOLs required for RTC */
48 #define STDFONTW 8 /* the built-in font width, height & size */
50 #define STDFONTBUF 4096
52 typedef struct fontstruct {
54 uchar buf [ MAXFONTBUF ] ;
55 short offset [ 256 ] ;
58 extern uchar stdfont [ ] ; /* compressed bit map for built-in font */
60 int readfont ( char *fname, faxfont *font ) ;
62 /* T.4 Encoding/Decoding */
64 typedef struct t4tabstruct {
65 short code, bits, rlen ; /* code, bits, run length */
68 extern t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] ; /* white runs */
69 extern t4tab btab [ ( 64 + 27 + 13 ) + 1 ] ; /* black runs */
71 typedef struct dtabstruct { /* decoder table entry */
72 struct dtabstruct *next ;
78 #define bigendian ( * (uchar*) &short256 )
79 extern short short256 ;
81 /* input, output and page file formats */
87 enum iformats { I_AUTO=0, I_PBM=1, I_FAX=2, I_TEXT=3, I_TIFF=4,
88 I_DFAX=5, I_PCX=6, I_RAW=7, I_DCX=8 } ;
90 #define IFORMATS { "AUTO", "PBM", "FAX", "TEXT", "TIFF", \
91 "DFAX", "PCX", "RAW", "DCX" } ;
93 enum oformats { O_AUTO=0, O_PBM=1, O_FAX=2, O_PCL=3, O_PS=4,
94 O_PGM=5, O_TEXT=6, O_TIFF_FAX=7, O_TIFF_RAW=8, O_DFAX=9,
95 O_TIFF=10, O_PCX=11, O_PCX_RAW=12, O_DCX=13 } ;
97 #define OFORMATS { "AUTO", "PBM", "FAX", "PCL", "PS", \
98 "PGM", "TEXT", "TIFF", "TIFF", "DFAX", \
99 "TIFF", "PCX", "PCX", "DCX" }
101 enum pformats { P_RAW=0, P_FAX=1, P_PBM=2, P_TEXT=3, P_PCX=4 } ;
103 #define PFORMATS { "RAW", "FAX", "PBM", "TEXT", "PCX" }
106 extern char *iformatname [ NIFORMATS ] ;
107 extern char *oformatname [ NOFORMATS ] ;
108 extern char *pformatname [ NPFORMATS ] ;
110 typedef struct decoderstruct {
111 long x ; /* undecoded bits */
112 short shift ; /* number of unused bits - 9 */
113 dtab *tab ; /* current decoding table */
114 int eolcnt ; /* EOL count for detecting RTC */
117 void newDECODER ( DECODER *d ) ;
119 #define IFILEBUFSIZE 512
121 #define MAXPAGE 360 /* number of A4 pages in a 100m roll */
123 typedef struct PAGEstruct { /* page data */
124 char *fname ; /* file name */
125 long offset ; /* location of data within file */
126 int w, h ; /* pel and line counts */
127 float xres, yres ; /* x and y resolution, dpi */
128 uchar format ; /* image coding */
129 uchar revbits ; /* fill order is LS to MS bit */
132 typedef struct ifilestruct { /* input image file */
134 /* data for each pages */
136 PAGE *page, *lastpage ; /* pointers to current and last page */
137 PAGE pages [ MAXPAGE ] ; /* page data */
139 long next ; /* offset to next page (while scanning only) */
141 /* data for current input page */
143 FILE *f ; /* current file pointer */
144 int lines ; /* scan lines remaining in page */
146 uchar bigend ; /* TIFF: big-endian byte order */
148 DECODER d ; /* FAX: T.4 decoder state */
150 faxfont *font ; /* TEXT: font to use */
151 int pglines ; /* TEXT: text lines per page */
152 char text [ MAXLINELEN ] ; /* TEXT: current string */
153 int txtlines ; /* TEXT: scan lines left in text l. */
154 int charw, charh, lmargin ; /* TEXT: desired char w, h & margin */
158 int newIFILE ( IFILE *f, char **fname ) ;
159 void logifnames ( IFILE *f, char *s ) ;
160 int nextipage ( IFILE *f, int dp ) ;
161 int lastpage ( IFILE *f ) ;
162 int readline ( IFILE *f, short *runs, int *pels ) ;
166 typedef struct encoderstruct {
167 long x ; /* unused bits */
168 short shift ; /* number of unused bits - 8 */
171 void newENCODER ( ENCODER *e ) ;
173 typedef struct ofilestruct { /* input image file state */
174 FILE *f ; /* file pointer */
175 int format ; /* file format */
176 char *fname ; /* file name pattern */
177 float xres, yres ; /* x and y resolution, dpi */
178 int w, h ; /* width & height, pixels */
179 int lastpageno ; /* PS: last page number this file */
180 int pslines ; /* PS: scan lines written to file */
181 int bytes ; /* TIFF: data bytes written */
182 ENCODER e ; /* T.4 encoder state */
183 char cfname [ EFAX_PATH_MAX + 1 ] ; /* current file name */
186 void newOFILE ( OFILE *f, int format, char *fname,
187 float xres, float yres, int w, int h ) ;
188 int nextopage ( OFILE *f, int page ) ;
189 void writeline ( OFILE *f, short *runs, int nr, int no ) ;
191 /* Scan Line Processing */
193 uchar *putcode ( ENCODER *e, short code , short bits , uchar *buf ) ;
194 uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *buf ) ;
196 /* int bittorun ( uchar *buf, int n, short *runs ) ; */
197 int texttorun ( uchar *txt, faxfont *font, short line,
198 int w, int h, int lmargin,
199 short *runs, int *pels ) ;
201 int xpad ( short *runs, int nr, int pad ) ;
202 int xscale ( short *runs, int nr, int xs ) ;
203 int xshift ( short *runs, int nr, int s ) ;
205 int runor ( short *a, int na, short *b, int nb, short *c, int *pels ) ;
207 /* Bit reversal lookup tables (note that the `normalbits' array
208 is the one actually used for the bit reversal. */
210 uchar reversebits [ 256 ], normalbits [ 256 ] ;
212 void initbittab(void) ;
216 int ckfmt ( char *p, int n ) ;