unstrip-downed Class 2 error messages table
[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,
88 #ifndef UCLINUX
89                 I_FAX=2, I_TEXT=3, I_TIFF=4,
90                 I_DFAX=5, I_PCX=6, I_RAW=7, I_DCX=8
91 #endif /* UCLINUX */
92                 } ;
93
94 #define IFORMATS { "AUTO", "PBM", "FAX", "TEXT", "TIFF", \
95                 "DFAX", "PCX", "RAW", "DCX" } ;
96
97 enum oformats { O_AUTO=0, O_PBM=1,
98 #ifndef UCLINUX
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
102 #endif /* UCLINUX */
103                 } ;
104
105 #define OFORMATS { "AUTO", "PBM", "FAX", "PCL", "PS", \
106                 "PGM", "TEXT", "TIFF", "TIFF", "DFAX", \
107                   "TIFF", "PCX", "PCX", "DCX" } 
108
109 enum pformats { P_RAW=0, P_FAX=1, P_PBM=2, P_TEXT=3, P_PCX=4 } ;
110
111 #define PFORMATS { "RAW", "FAX", "PBM", "TEXT", "PCX" }
112
113
114 extern char *iformatname [ NIFORMATS ] ;
115 extern char *oformatname [ NOFORMATS ] ;
116 extern char *pformatname [ NPFORMATS ] ;
117
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 */
123 } DECODER ;
124
125 void newDECODER ( DECODER *d ) ;
126
127 #define IFILEBUFSIZE 512
128
129 #define MAXPAGE 360             /* number of A4 pages in a 100m roll */
130
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 */
138 } PAGE ;
139
140 typedef struct ifilestruct {    /* input image file  */
141
142   /* data for each pages */
143
144   PAGE *page, *lastpage ;       /* pointers to current and last page */
145   PAGE pages [ MAXPAGE ] ;      /* page data */
146
147   long next ;                   /* offset to next page (while scanning only) */
148
149   /* data for current input page */
150
151   FILE *f ;                     /* current file pointer */
152   int lines ;                   /* scan lines remaining in page */
153
154   uchar bigend ;                /* TIFF: big-endian byte order */
155
156   DECODER d ;                   /* FAX: T.4 decoder state */
157
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 */
163
164 } IFILE ;
165
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 ) ;
171
172                             /* Image Output */
173
174 typedef struct encoderstruct {
175   long x ;                               /* unused bits */
176   short shift ;                          /* number of unused bits - 8 */
177 } ENCODER ;
178
179 void newENCODER ( ENCODER *e ) ;
180
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 */
192 } OFILE ;
193
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 ) ;
198
199                         /*  Scan Line Processing */
200
201 uchar   *putcode ( ENCODER *e, short code , short bits , uchar *buf ) ;
202 uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *buf ) ;
203
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 ) ;
208
209 int   xpad ( short *runs, int nr, int pad ) ;
210
211 int runor ( short *a, int na, short *b, int nb, short *c, int *pels ) ;
212
213 /* Bit reversal lookup tables (note that the `normalbits' array
214    is the one actually used for the bit reversal.  */
215
216 extern uchar reversebits [ 256 ], normalbits [ 256 ] ;
217
218 void initbittab(void) ;
219
220 /* Other Stuff */
221
222 int ckfmt ( char *p, int n ) ;
223
224 #endif