1#ifndef _EFAXLIB_H
2#define _EFAXLIB_H
3
4#include <stdio.h>
5
6#define EFAX_PATH_MAX 1024
7
8		/*  T.4 fax encoding/decoding */
9
10#ifndef uchar
11#define uchar unsigned char
12#endif
13
14#define DEFPGLINES 66           /* default lines per page */
15
16			   /* Buffer sizes.  */
17
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 ~=
24   48 bytes).  */
25
26#define MAXRUNS 8192
27#define MAXBITS (MAXRUNS/8+1)
28#define MAXCODES (MAXRUNS*25/8/2+1)
29
30/* Line/font size limits */
31
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 */
36
37/* Longest run encodeable by the T.4 encoding tables used. */
38
39#define MAXRUNLEN (2560+63)
40
41     /* Codes for EOL and number of EOLs required for RTC */
42
43#define EOLCODE 1
44#define EOLBITS 12
45#define RTCEOL  5
46			   /* Fonts */
47
48#define STDFONTW    8		/* the built-in font width, height & size */
49#define STDFONTH    16
50#define STDFONTBUF  4096
51
52typedef struct fontstruct {
53  int h, w ;
54  uchar buf [ MAXFONTBUF ] ;
55  short offset [ 256 ] ;
56} faxfont ;
57
58extern uchar stdfont [ ] ; /* compressed bit map for built-in font */
59
60int readfont ( char *fname, faxfont *font ) ;
61
62		    /* T.4 Encoding/Decoding */
63
64typedef struct t4tabstruct {
65  short code, bits, rlen ;			/* code, bits, run length */
66} t4tab ;
67
68extern t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] ;	/* white runs */
69extern t4tab btab [ ( 64 + 27 + 13 ) + 1 ] ;	/* black runs */
70
71typedef struct dtabstruct {			/* decoder table entry */
72  struct dtabstruct *next ;
73  short bits, code ;
74} dtab ;
75
76			     /* Image Input */
77
78#define bigendian ( * (uchar*) &short256 )
79extern short short256 ;
80
81/* input, output and page file formats */
82
83#define NIFORMATS 9
84#define NOFORMATS 14
85#define NPFORMATS 5
86
87enum 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 } ;
89
90#define IFORMATS { "AUTO", "PBM", "FAX", "TEXT", "TIFF", \
91		"DFAX", "PCX", "RAW", "DCX" } ;
92
93enum 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 } ;
96
97#define OFORMATS { "AUTO", "PBM", "FAX", "PCL", "PS", \
98		"PGM", "TEXT", "TIFF", "TIFF", "DFAX", \
99		  "TIFF", "PCX", "PCX", "DCX" }
100
101enum pformats { P_RAW=0, P_FAX=1, P_PBM=2, P_TEXT=3, P_PCX=4 } ;
102
103#define PFORMATS { "RAW", "FAX", "PBM", "TEXT", "PCX" }
104
105
106extern char *iformatname [ NIFORMATS ] ;
107extern char *oformatname [ NOFORMATS ] ;
108extern char *pformatname [ NPFORMATS ] ;
109
110typedef 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 */
115} DECODER ;
116
117void newDECODER ( DECODER *d ) ;
118
119#define IFILEBUFSIZE 512
120
121#define MAXPAGE 360		/* number of A4 pages in a 100m roll */
122
123typedef 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 */
130  uchar black_is_zero ;		/* black is encoded as zero */
131} PAGE ;
132
133typedef struct ifilestruct {	/* input image file  */
134
135  enum iformats fileformat;
136
137  /* data for each pages */
138
139  PAGE *page, *lastpage ;	/* pointers to current and last page */
140  PAGE pages [ MAXPAGE ] ;	/* page data */
141
142  long next ;			/* offset to next page (while scanning only) */
143  int totalpages;		/* total number of pages */
144
145  /* data for current input page */
146
147  FILE *f ;			/* current file pointer */
148  int lines ;			/* scan lines remaining in page */
149
150  uchar bigend ;		/* TIFF: big-endian byte order */
151
152  DECODER d ;			/* FAX: T.4 decoder state */
153
154  faxfont *font ;		/* TEXT: font to use */
155  int pglines ;			/* TEXT: text lines per page */
156  char text [ MAXLINELEN ] ;	/* TEXT: current string */
157  int txtlines ;		/* TEXT: scan lines left in text l. */
158  int charw, charh, lmargin ;	/* TEXT: desired char w, h & margin */
159
160} IFILE ;
161
162int    newIFILE ( IFILE *f, char **fname ) ;
163void logifnames ( IFILE *f, char *s ) ;
164int nextipage ( IFILE *f, int dp ) ;
165int lastpage ( IFILE *f ) ;
166int     readline ( IFILE *f, short *runs, int *pels ) ;
167
168			    /* Image Output */
169
170typedef struct encoderstruct {
171  long x ;				 /* unused bits */
172  short shift ;				 /* number of unused bits - 8 */
173} ENCODER ;
174
175void newENCODER ( ENCODER *e ) ;
176
177typedef struct ofilestruct {		 /* input image file state  */
178  FILE *f ;				 /* file pointer */
179  int format ;				 /* file format */
180  char *fname ;			         /* file name pattern */
181  float xres, yres ;			 /* x and y resolution, dpi */
182  int w, h ;			         /* width & height, pixels */
183  int lastpageno ;			 /* PS: last page number this file */
184  int pslines ;			         /* PS: scan lines written to file */
185  int bytes ;			         /* TIFF: data bytes written */
186  ENCODER e ;				 /* T.4 encoder state */
187  char cfname [ EFAX_PATH_MAX + 1 ] ;	 /* current file name */
188} OFILE ;
189
190void  newOFILE ( OFILE *f, int format, char *fname,
191		float xres, float yres, int w, int h ) ;
192int  nextopage ( OFILE *f, int page ) ;
193void writeline ( OFILE *f, short *runs, int nr, int no ) ;
194
195			/*  Scan Line Processing */
196
197uchar   *putcode ( ENCODER *e, short code , short bits , uchar *buf ) ;
198uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *buf ) ;
199
200/* int bittorun ( uchar *buf, int n, short *runs ) ; */
201int texttorun ( uchar *txt, faxfont *font, short line,
202	       int w, int h, int lmargin,
203	       short *runs, int *pels ) ;
204
205int   xpad ( short *runs, int nr, int pad ) ;
206int xscale ( short *runs, int nr, int xs ) ;
207int xshift ( short *runs, int nr, int s ) ;
208
209int runor ( short *a, int na, short *b, int nb, short *c, int *pels ) ;
210
211/* Bit reversal lookup tables (note that the `normalbits' array
212   is the one actually used for the bit reversal.  */
213
214extern uchar reversebits [ 256 ], normalbits [ 256 ] ;
215
216void initbittab(void) ;
217
218/* Other Stuff */
219
220int ckfmt ( char *p, int n ) ;
221
222#endif
223