ftp://ftp.metalab.unc.edu/pub/Linux/apps/serialcomm/fax/efax-0.9.tar.gz
[efax.git] / efaxlib.h
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
52 typedef struct fontstruct {
53   int h, w ;
54   uchar buf [ MAXFONTBUF ] ;
55   short offset [ 256 ] ;
56 } faxfont ;
57
58 extern uchar stdfont [ ] ; /* compressed bit map for built-in font */
59
60 int readfont ( char *fname, faxfont *font ) ;
61
62                     /* T.4 Encoding/Decoding */
63
64 typedef struct t4tabstruct { 
65   short code, bits, rlen ;                      /* code, bits, run length */
66 } t4tab ;
67
68 extern t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] ;    /* white runs */
69 extern t4tab btab [ ( 64 + 27 + 13 ) + 1 ] ;    /* black runs */
70
71 typedef 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 )
79 extern short short256 ;
80
81 /* input, output and page file formats */
82
83 #define NIFORMATS 9
84 #define NOFORMATS 14
85 #define NPFORMATS 5
86
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 } ;
89
90 #define IFORMATS { "AUTO", "PBM", "FAX", "TEXT", "TIFF", \
91                 "DFAX", "PCX", "RAW", "DCX" } ;
92
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 } ;
96
97 #define OFORMATS { "AUTO", "PBM", "FAX", "PCL", "PS", \
98                 "PGM", "TEXT", "TIFF", "TIFF", "DFAX", \
99                   "TIFF", "PCX", "PCX", "DCX" } 
100
101 enum 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
106 extern char *iformatname [ NIFORMATS ] ;
107 extern char *oformatname [ NOFORMATS ] ;
108 extern char *pformatname [ NPFORMATS ] ;
109
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 */
115 } DECODER ;
116
117 void newDECODER ( DECODER *d ) ;
118
119 #define IFILEBUFSIZE 512
120
121 #define MAXPAGE 360             /* number of A4 pages in a 100m roll */
122
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 */
130 } PAGE ;
131
132 typedef struct ifilestruct {    /* input image file  */
133
134   /* data for each pages */
135
136   PAGE *page, *lastpage ;       /* pointers to current and last page */
137   PAGE pages [ MAXPAGE ] ;      /* page data */
138
139   long next ;                   /* offset to next page (while scanning only) */
140
141   /* data for current input page */
142
143   FILE *f ;                     /* current file pointer */
144   int lines ;                   /* scan lines remaining in page */
145
146   uchar bigend ;                /* TIFF: big-endian byte order */
147
148   DECODER d ;                   /* FAX: T.4 decoder state */
149
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 */
155
156 } IFILE ;
157
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 ) ;
163
164                             /* Image Output */
165
166 typedef struct encoderstruct {
167   long x ;                               /* unused bits */
168   short shift ;                          /* number of unused bits - 8 */
169 } ENCODER ;
170
171 void newENCODER ( ENCODER *e ) ;
172
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 */
184 } OFILE ;
185
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 ) ;
190
191                         /*  Scan Line Processing */
192
193 uchar   *putcode ( ENCODER *e, short code , short bits , uchar *buf ) ;
194 uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *buf ) ;
195
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 ) ;
200
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 ) ;
204
205 int runor ( short *a, int na, short *b, int nb, short *c, int *pels ) ;
206
207 /* Bit reversal lookup tables (note that the `normalbits' array
208    is the one actually used for the bit reversal.  */
209
210 uchar reversebits [ 256 ], normalbits [ 256 ] ;
211
212 void initbittab(void) ;
213
214 /* Other Stuff */
215
216 int ckfmt ( char *p, int n ) ;
217
218 #endif