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,
89 I_FAX=2, I_TEXT=3, I_TIFF=4,
90 I_DFAX=5, I_PCX=6, I_RAW=7, I_DCX=8
94 #define IFORMATS { "AUTO", "PBM", "FAX", "TEXT", "TIFF", \
95 "DFAX", "PCX", "RAW", "DCX" } ;
97 enum oformats { O_AUTO=0, O_PBM=1,
99 O_FAX=2, O_PCL=3, O_PS=4,
100 O_PGM=5, O_TEXT=6, O_TIFF_FAX=7, O_TIFF_RAW=8, O_DFAX=9,
101 O_TIFF=10, O_PCX=11, O_PCX_RAW=12, O_DCX=13
105 #define OFORMATS { "AUTO", "PBM", "FAX", "PCL", "PS", \
106 "PGM", "TEXT", "TIFF", "TIFF", "DFAX", \
107 "TIFF", "PCX", "PCX", "DCX" }
109 enum pformats { P_RAW=0, P_FAX=1, P_PBM=2, P_TEXT=3, P_PCX=4 } ;
111 #define PFORMATS { "RAW", "FAX", "PBM", "TEXT", "PCX" }
114 extern char *iformatname [ NIFORMATS ] ;
115 extern char *oformatname [ NOFORMATS ] ;
116 extern char *pformatname [ NPFORMATS ] ;
118 typedef struct decoderstruct {
119 long x ; /* undecoded bits */
120 short shift ; /* number of unused bits - 9 */
121 dtab *tab ; /* current decoding table */
122 int eolcnt ; /* EOL count for detecting RTC */
125 void newDECODER ( DECODER *d ) ;
127 #define IFILEBUFSIZE 512
129 #define MAXPAGE 360 /* number of A4 pages in a 100m roll */
131 typedef struct PAGEstruct { /* page data */
132 char *fname ; /* file name */
133 long offset ; /* location of data within file */
134 int w, h ; /* pel and line counts */
135 float xres, yres ; /* x and y resolution, dpi */
136 uchar format ; /* image coding */
137 uchar revbits ; /* fill order is LS to MS bit */
140 typedef struct ifilestruct { /* input image file */
142 /* data for each pages */
144 PAGE *page, *lastpage ; /* pointers to current and last page */
145 PAGE pages [ MAXPAGE ] ; /* page data */
147 long next ; /* offset to next page (while scanning only) */
149 /* data for current input page */
151 FILE *f ; /* current file pointer */
152 int lines ; /* scan lines remaining in page */
154 uchar bigend ; /* TIFF: big-endian byte order */
156 DECODER d ; /* FAX: T.4 decoder state */
158 faxfont *font ; /* TEXT: font to use */
159 int pglines ; /* TEXT: text lines per page */
160 char text [ MAXLINELEN ] ; /* TEXT: current string */
161 int txtlines ; /* TEXT: scan lines left in text l. */
162 int charw, charh, lmargin ; /* TEXT: desired char w, h & margin */
166 int newIFILE ( IFILE *f, char **fname ) ;
167 void logifnames ( IFILE *f, char *s ) ;
168 int nextipage ( IFILE *f, int dp ) ;
169 int lastpage ( IFILE *f ) ;
170 int readline ( IFILE *f, short *runs, int *pels ) ;
174 typedef struct encoderstruct {
175 long x ; /* unused bits */
176 short shift ; /* number of unused bits - 8 */
179 void newENCODER ( ENCODER *e ) ;
181 typedef struct ofilestruct { /* input image file state */
182 FILE *f ; /* file pointer */
183 int format ; /* file format */
184 char *fname ; /* file name pattern */
185 float xres, yres ; /* x and y resolution, dpi */
186 int w, h ; /* width & height, pixels */
187 int lastpageno ; /* PS: last page number this file */
188 int pslines ; /* PS: scan lines written to file */
189 int bytes ; /* TIFF: data bytes written */
190 ENCODER e ; /* T.4 encoder state */
191 char cfname [ EFAX_PATH_MAX + 1 ] ; /* current file name */
194 void newOFILE ( OFILE *f, int format, char *fname,
195 float xres, float yres, int w, int h ) ;
196 int nextopage ( OFILE *f, int page ) ;
197 void writeline ( OFILE *f, short *runs, int nr, int no ) ;
199 /* Scan Line Processing */
201 uchar *putcode ( ENCODER *e, short code , short bits , uchar *buf ) ;
202 uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *buf ) ;
204 /* int bittorun ( uchar *buf, int n, short *runs ) ; */
205 int texttorun ( uchar *txt, faxfont *font, short line,
206 int w, int h, int lmargin,
207 short *runs, int *pels ) ;
209 int xpad ( short *runs, int nr, int pad ) ;
211 int runor ( short *a, int na, short *b, int nb, short *c, int *pels ) ;
213 /* Bit reversal lookup tables (note that the `normalbits' array
214 is the one actually used for the bit reversal. */
216 extern uchar reversebits [ 256 ], normalbits [ 256 ] ;
218 void initbittab(void) ;
222 int ckfmt ( char *p, int n ) ;