unstrip-downed Class 2 error messages table
[efax.git] / efaxlib.c
1 /* 
2               efaxlib.c - utility routines for efax
3                      Copyright 1995 Ed Casas
4 */
5
6 #include "config.h"
7
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "efaxmsg.h"
13 #include "efaxlib.h"
14
15 #ifndef SEEK_SET
16 #define SEEK_SET 0
17 #endif
18
19 #define DEFXRES 204.145         /* fax x and y resolution in dpi */
20 #define DEFYRES 195.58
21
22 #define DEFWIDTH  1728          /* 215x297 mm image at fax resolution */
23 #define DEFHEIGHT 2287
24
25 extern t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] ; /* T.4 coding tables */
26 extern t4tab btab [ ( 64 + 27 + 13 ) + 1 ] ;
27
28 #ifndef UCLINUX
29 short short256 = 256 ;          /* for endian-ness detection */
30
31 /* Make sure printf strings have only %d escapes and n or fewer
32    of them.  Returns 0 if OK, 1 on error. */
33
34 int ckfmt ( char *p, int n )
35 {
36   for ( ; *p ; p++ ) {
37     if ( p[0] == '%' ) {
38       if ( p[1] == 'd' ) n-- ;
39       else if ( p[1] == '%' ) p++ ;
40       else n=-1 ;
41     }
42   }
43   
44   return n < 0 ;
45 }
46 #endif /* UCLINUX */
47
48
49 /* Initialize state of variable-length code word encoder. */
50
51 void newENCODER ( ENCODER *e )
52 {
53   e->x = 0 ;
54   e->shift = -8 ;
55 }
56
57
58 /* Store a code word `code' of length `bits' (<=24) in buffer pointed to by
59    `buf'.  Bits that don't fit in complete bytes are saved between calls.
60    To flush the remaining bits call the function with code=0 and bits=0.
61    Returns pointer to next free element in output buffer.  Calling function
62    must ensure at least bits/8 bytes are available in buffer.  */
63
64 uchar *putcode ( ENCODER *e, short code, short bits, uchar *buf )
65 {
66   e->x = ( e->x << bits ) | code ;
67   e->shift += bits ? bits : -e->shift ;
68
69   while ( e->shift >= 0 ) {
70     *buf++ = e->x >> e->shift ;
71     e->shift -= 8 ;
72   }
73
74   return buf ;
75 }
76
77
78 /* Convert run lengths to 1-D T.4-codes.  First run is white.  Silently
79    truncates run lengths that are too long. After using this function EOLs
80    may need to be added and/or the putcode() buffer flushed.  Returns
81    pointer to next free element in output buffer. */
82
83 uchar *runtocode ( ENCODER *e, short *runs, int nr, uchar *codes )
84 {
85   uchar col = 0, *maxcodes = codes + MAXCODES ;
86   t4tab *ctab = wtab, *p ;
87   short rlen ;
88   long x ;
89   short shift ;
90
91 #define PUTCODE(p) { x = ( x << p->bits ) | p->code ;  shift += p->bits ; \
92         while ( shift >= 0 ) { *codes++ = x >> shift ; shift -= 8 ; } }
93
94   x = e->x ; shift = e->shift ;
95
96   while ( nr-- > 0 ) {
97     rlen = *runs++ ;
98     if ( rlen > 63 ) {                          /* make-up code */
99       if ( rlen > MAXRUNLEN ) rlen = MAXRUNLEN ;
100       p = ctab + 63 + ( rlen >> 6 ) ;
101       if ( codes < maxcodes ) PUTCODE(p) ;
102     }
103     p = ctab + ( rlen & 0x3f ) ;                /* terminating code */
104     if ( codes < maxcodes ) PUTCODE(p) ;
105     ctab = ( col ^= 1 ) ? btab : wtab ;
106   }  
107   
108   e->x = x ; e->shift = shift ;
109
110   return codes ;
111 }
112
113
114 /* Pad/truncate run-length coded scan line 'runs' of 'nr' runs by 'pad'
115    pixels (truncate if negative).  Returns the new number of runs. */
116
117 int xpad ( short *runs, int nr, int pad )
118 {
119   if ( pad < 0 ) {                        /* truncate */
120     while ( pad < 0 ) pad += ( nr <= 0 ? -pad : runs [ --nr ] ) ;
121     runs [ nr++ ] = pad ;
122   } else {                                /* pad with white */
123     if ( nr & 1 ) runs [ nr - 1 ] += pad ;
124     else runs [ nr++ ] = pad ;
125   }
126   return nr ;
127 }
128
129
130 /* Shift a run-length coded scan line right by s pixels (left if negative).
131    If necessary, zero-length runs are created to avoid copying.  Returns
132    the pixel width change (+/-). */
133
134 #ifndef UCLINUX
135 int xshift ( short *runs, int nr, int s )
136 {
137   int i=0, n=0 ;
138   if ( s < 0 ) {
139     for ( i = 0 ; s < 0 && i < nr ; i++ ) { 
140       s += runs [ i ] ;
141       n -= runs [ i ] ;
142       runs [ i ] = 0 ; 
143     }
144     i-- ;
145   }
146   if ( i < nr ) {
147     runs [ i ] += s ;
148     n += s ;
149   }
150   return n ;
151 }
152
153
154 /* Scale nr run lengths in buffer pointed to by p to scale image
155    horizontally.  The scaling factor is xs/256. Returns new line width in
156    pixels. */
157
158 int xscale ( short *p, int nr, int xs )
159 {
160   int inlen=0, outlen=0 ;
161   for ( ; nr-- > 0 ; p++ ) {
162     inlen += *p ;
163     *p = ( ( inlen * xs + 128 ) >> 8 ) - outlen ;
164     outlen += *p ;
165   }
166   return outlen ;
167 }
168 #endif /* UCLINUX */
169
170
171 /* Zero-terminated lists of run lengths for each byte. */
172
173 static uchar byteruns [ 1408 + 1 ] = 
174    "8071061106205120511105210530413041210411110411204220421104310440"
175    "3140313103121103122031112031111103112103113032303221032111032120"
176    "3320331103410350215021410213110213202121202121110212210212302111"
177    "3021112102111111021111202112202112110211310211402240223102221102"
178    "2220221120221111022121022130233023210231110231202420241102510260"
179    "1160115101141101142011312011311101132101133011213011212101121111"
180    "0112112011222011221101123101124011114011113101111211011112201111"
181    "1120111111110111112101111130111230111221011121110111212011132011"
182    "1311011141011150125012410123110123201221201221110122210122301211"
183    "3012112101211111012111201212201212110121310121401340133101321101"
184    "3220131120131111013121013130143014210141110141201520151101610170"
185    "1701610151101520141201411101421014301313013121013111101311201322"
186    "0132110133101340121401213101212110121220121112012111110121121012"
187    "1130122301222101221110122120123201231101241012501115011141011131"
188    "1011132011121201112111011122101112301111130111112101111111101111"
189    "1120111122011112110111131011114011240112310112211011222011211201"
190    "1211110112121011213011330113210113111011312011420114110115101160"
191    "2602510241102420231202311102321023302213022121022111102211202222"
192    "0222110223102240211402113102112110211220211112021111110211121021"
193    "1130212302122102121110212120213202131102141021503503410331103320"
194    "3212032111032210323031130311210311111031112031220312110313103140"
195    "4404310421104220411204111104121041305305210511105120620611071080" ;
196
197 /* Convert byte-aligned bit-mapped n-byte scan line into array of run
198    lengths.  Run length array must have *more* than 8*n elements.  First
199    run is white.  Returns number of runs coded.  */
200
201 static int bittorun ( uchar *bits, int n, short *runs )
202 {
203   static uchar init=0, *rltab [ 256 ] ;
204   register uchar *p, c, lastc = 0x00 ;
205   short *runs0 = runs ;
206
207   if ( ! init ) {               /* initialize pointer and run tables */
208     int i = 0 ;
209     for ( rltab[ 0 ] = p = byteruns ; *p ; p++ )
210       if ( ! ( *p -= '0' ) && i < 255 ) 
211         rltab [ ++i ] = p+1 ;
212     init = 1 ;
213   }
214
215   *runs = 0 ;
216   for ( ; n > 0 ; n-- ) {
217     p = rltab [ c = *bits++ ] ;
218     if ( ( lastc & 0x01 ) ? ! ( c & 0x80 ) : ( c & 0x80 ) )
219       *(++runs) = *p++ ;                  /* new run */
220     else                          
221       *runs += *p++ ;                     /* continue run */
222     while ( *p ) 
223       *(++runs) = *p++ ;
224     lastc = c ;
225   }
226
227   return runs - runs0 + 1  ;
228 }
229
230
231 /* Bitwise-OR two run-length coded scan lines.  The run length
232    vectors a and b are OR-ed into c.  If c is null, the result is
233    placed in a.  The new image width is stored in pels if it is
234    not null.  Returns the number of runs in the result.  */
235
236 int runor ( short *a, int na, short *b, int nb, short *c, int *pels )
237 {
238   register short la, lb ;
239   int ia, ib, ic, np=0 ;
240   short tmp [ MAXRUNS ] ;
241
242   if ( ! c ) c = tmp ;
243
244   la = a [ ia = 0 ] ;
245   lb = b [ ib = 0 ] ;
246   c [ ic = 0 ] = 0 ;
247
248   while ( 1 ) {
249     if ( la <= lb ) {                     /* select shorter sub-run */
250       if ( ( ( ia | ib ) ^ ic ) & 1 )     /* OR of subruns same colour as c? */
251         c [ ++ic ] = la ;                 /* no, new output run */
252       else 
253         c [ ic ] += la ;                  /* yes, add it */
254       lb -= la ;                          /* align subruns */
255       if ( ++ia >= na ) break ;           /* done */
256       la = a [ ia ] ;                     /* get new subrun */
257     } else {                              /* same for line b ... */
258       if ( ( ( ia | ib ) ^ ic ) & 1 ) 
259         c [ ++ic ] = lb ;
260       else 
261         c [ ic ] += lb ;
262       la -= lb ;
263       if ( ++ib >= nb ) break ;
264       lb = b [ ib ] ;
265     }
266   }
267
268   if ( ia < na )
269     while ( 1 ) {
270       if ( ( ia ^ ic ) & 1 )      
271         c [ ++ic ] = la ;                 
272       else 
273         c [ ic ] += la ;                  
274       if ( ++ia >= na ) break ;           
275       la = a [ ia ] ;                     
276     } 
277   else
278     while ( 1 ) {
279       if ( ( ib ^ ic ) & 1 ) 
280         c [ ++ic ] = lb ;
281       else 
282         c [ ic ] += lb ;
283       if ( ++ib >= nb ) break ;
284       lb = b [ ib ] ;
285     }
286
287   if ( c == tmp ) for ( ia=0 ; ia <= ic ; ia++ ) np += a[ia] = c[ia] ;
288
289   if ( pels ) *pels = np ;
290
291   return ic + 1 ;
292 }  
293
294
295 /* Get a number from a PBM file header while skipping whitespace
296    and comments. Returns the number or 0 on EOF. Reads one more
297    byte than used by the number. */
298
299 static int pbmdim ( IFILE *f )
300 {
301   int c, n=0 ;
302   
303   /* scan for first digit and skip comments */
304   while ( ! isdigit ( c = fgetc ( f->f ) ) && c >= 0 ) 
305     if ( c == '#' )
306       while ( ( c = fgetc ( f->f ) ) != '\n' && c >= 0 ) ;
307
308   /* get the number */
309   if ( c >= 0 && isdigit( c ) ) {
310     n = c - '0' ;
311     while ( isdigit ( c = fgetc ( f->f ) ) && c >= 0 ) 
312       n = n * 10 + c - '0' ;
313   }
314
315   return n ;
316 }
317
318
319 /* Append nb bits from in[from] to bit-mapped scan line buffer
320    where `from' is a bit (not byte) index.  Bits in bytes are
321    ordered from MS to LS bit. Initialize before each scan line by
322    calling with nb=0 and in pointing to output buffer.  Flush
323    after each scan line by calling with nb=0 and in=NULL. */
324
325 #define putbits( c, b ) { x = ( x << (b) ) | (c) ; shift += (b) ; \
326           if ( shift >= 0 ) { *out++ = x >> shift ; shift -= 8 ; } }
327
328 #ifndef UCLINUX
329 void copybits ( uchar *in, int from, short nb )
330 {
331   uchar *f ;
332   short bits ;
333   static uchar *out ;
334   static short x, shift ;
335   static unsigned char right [ 9 ] = { 
336     0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff } ;
337   
338   if ( ! nb ) {                         /* reset for new scan line */
339     if ( in ) out = in ;                   
340     else putbits ( 0, -shift ) ;        /* or flush bit buffer */
341     x = 0 ;
342     shift = -8 ;
343   } else {
344     f = in + ( from >> 3 ) ;
345     bits = 8 - ( from & 7 ) ;
346
347     if ( nb >= bits ) {
348       putbits ( *f++ & right [ bits ], bits ) ;
349       nb -= bits ;
350     } else {
351       putbits ( ( *f >> ( bits - nb ) ) & right [ bits ], nb ) ;
352       nb = 0 ;
353     } 
354
355     while ( nb >= 8 ) { putbits ( *f++, 8 ) ; nb -= 8 ; }
356
357     if ( nb > 0 ) putbits ( *f >> ( 8 - nb ), nb );
358   }
359 }   
360 #endif /* UCLINUX */
361
362
363 /* Generate scan line 'line' of string 'txt' using font `font'
364    and store the runs in 'runs'.  The font is scaled so it
365    appears to have cells of width w and height h.  lmargin pixels
366    of white space are added at the left margin. Sets 'pels' to
367    line width if not null.  Returns number of runs coded. */
368
369 #ifndef UCLINUX
370 int texttorun ( uchar *txt, faxfont *font, short line, 
371                int w, int h, int lmargin,
372                short *runs, int *ppels )
373 {
374   uchar *in, out [ MAXLINELEN * MAXFONTW / 8 + 1 ] ;
375   int i, nc = 0, cw, nr, pels ;
376
377   line = ( line * font->h + h/2 ) / h ;
378
379   cw = font->w ;
380   if ( line >= font->h ) line = font->h - 1 ;
381   in = font->buf + 256/8 * cw * line ;
382
383   copybits ( out, 0, 0 ) ;
384   for ( i=0 ; txt[i] && i < MAXLINELEN ; i++ ) {
385     copybits ( in, font->offset [ txt[i] ], cw ) ;
386     nc++ ;
387     while ( ( txt[i] == HT ) && ( nc & 7 ) ) { /* tab */
388       copybits ( in, font->offset [ ' ' ], cw ) ;
389       nc++ ;
390     }
391   }
392   copybits ( 0, 0, 0 ) ;
393
394   nr = bittorun ( out, ( nc*cw + 7 )/8, runs ) ;
395   
396   if ( font->w == w )
397     pels = nc*cw ;
398   else    
399     pels = xscale ( runs, nr, ( w * 256 ) / font->w ) ;
400   
401   pels += xshift ( runs, nr, lmargin ) ;
402   
403   if ( ppels ) *ppels = pels ;
404
405   return nr ;
406 }
407 #endif /* UCLINUX */
408
409                 /* Image File Input Functions */
410
411
412 /* Names of file formats */
413
414 static char *iformatname [ NIFORMATS ] = IFORMATS ;
415 #ifndef UCLINUX
416 static char *oformatname [ NOFORMATS ] = OFORMATS ;
417 #endif /* UCLINUX */
418 static char *pformatname [ NPFORMATS ] = PFORMATS ;
419
420 /* Log the names of files still to be sent using the "msg()"
421    format string s. */
422
423 void logifnames ( IFILE *f, char *s )
424 {
425   PAGE *p ;
426   char *fn ;
427
428   if ( f && f->page )
429     for ( p = f->page ; p <= f->lastpage ; p++ ) {
430       fn = p->fname ;
431       if ( fn ) msg ( s, fn ) ;
432     }
433 }
434
435
436 /* Bit reversal lookup tables (note that the `normalbits' array
437    is the one actually used for the bit reversal.  */
438
439 uchar reversebits [ 256 ], normalbits [ 256 ] ;
440
441 /* Read run lengths for one scan line from T.4-coded IFILE f into buffer
442    runs.  If pointer pels is not null it is used to save pixel count.
443    Returns number of runs stored, EOF on RTC, or -2 on EOF or other
444    error. */
445
446 #ifndef UCLINUX
447 int readruns ( IFILE *f, short *runs, int *pels )
448 {
449   int err=0, c=EOF, n ;
450   register int x ;
451   dtab *tab, *t ;
452   short shift ;
453   short *p, *maxp, *q, len=0, npad=0 ;
454   DECODER *d ;
455   uchar reverse=f->page->revbits ;
456
457   maxp = ( p = runs ) + MAXRUNS ;
458   d = &f->d ;
459
460   x = d->x ; shift = d->shift ; tab = d->tab ; /* restore decoder state */
461
462   do {
463     do {
464       while ( shift < 0 ) { 
465         if ( ( c = fgetc ( f->f ) ) == EOF )  {
466           x = ( x << 15 ) | 1 ; shift += 15 ;  /* EOL pad at EOF */
467           npad++ ;
468         } else {
469           if ( reverse ) c = normalbits [ c & 0xff ] ;
470           x = ( x <<  8 ) | c ; shift +=  8 ; 
471         }
472       }
473       t = tab + ( ( x >> shift ) & 0x1ff ) ;
474       tab = t->next ;
475       shift -= t->bits ;
476     } while ( ! t->code ) ;
477     if ( p < maxp ) *p++ = t->code ;
478   } while ( t->code != -1 ) ;
479
480   d->x = x ; d->shift = shift ; d->tab = tab ; /* save state */
481
482   if ( npad > 1 ) msg ("W EOF before RTC" ) ;
483
484   if ( p >= maxp ) msg ( "W run length buffer overflow" ) ;
485
486   /* combine make-up and terminating codes and remove +1 offset
487      in run lengths */
488
489   n = p - runs - 1 ;
490   for ( p = q = runs ; n-- > 0 ; )
491     if ( *p > 64 && n-- > 0 ) {
492       len += *q++ = p[0] + p[1] - 2 ;
493       p+=2 ;
494     } else {
495       len += *q++ = *p++ - 1 ;
496     }
497   n = q - runs ;
498   
499   /* check for RTC and errors */
500
501   if ( len )
502     d->eolcnt = 0 ;
503   else
504     if ( ++(d->eolcnt) >= RTCEOL ) err = EOF ;
505
506   if ( c == EOF ) {
507     if ( ferror ( f->f ) ) {
508       err = -msg ("ES2error reading fax file:") ;
509     } else { 
510       err = -2 ;
511     }
512   }
513
514   if ( pels ) *pels = len ;
515   
516   return err ? err : n ;
517 }
518 #endif /* UCLINUX */
519
520
521 /* Read a PCX compressed bit-map */
522
523 #ifndef UCLINUX
524 int readpcx ( char *p, int len, IFILE *f )
525 {
526   int err=0, n, c ;
527
528   while ( !err && len > 0 ) {
529     if ( ( c = fgetc ( f->f ) ) < 0 ) {
530       err = msg ( "ES2 PCX read failed" ) ;
531     } else {
532       if ( ( c & 0xc0 ) == 0xc0 ) {     /* a run count */
533         n = c & 0x3f ;
534         c = fgetc ( f->f ) ;
535         if ( ( c = fgetc ( f->f ) ) < 0 ) {
536           err = msg ( "ES2 PCX read failed" ) ;
537         } else {
538           memset ( p, c, n <= len ? n : len ) ;
539           p += n ;
540           len -= n ;
541         }
542       } else {                  /* bits 0 to 7 are image data */
543         *p++ = c ;
544         len-- ;
545       }
546     }
547   }
548
549   if ( len != 0 )
550     msg ( "W PCX run-length coding error" ) ;
551
552   return err ;
553 }
554 #endif /* UCLINUX */
555
556 /* Read a scan line from the current page of IFILE f.  Stores
557    number of runs in runs and line width in pels if not null.
558    Pages ends at EOF. Text pages also end if a complete text line
559    would not fit or if the line contains a formfeed character.
560    PBM pages also end when all lines in the bitmap have been
561    read. Fax pages also end at RTC. Returns number of runs stored
562    or EOF at end of page. */
563
564 int readline ( IFILE *f, short *runs, int *pels )
565 {
566   int nr = 0 ;
567 #ifndef UCLINUX
568   int nb ;
569 #endif /* UCLINUX */
570   uchar bits [ MAXBITS ] ;
571
572   if ( f->lines != 0 ) {        /* -1 allowed as well */
573     switch ( f->page->format ) {
574
575 #ifndef UCLINUX
576     case P_TEXT :
577       if ( f->txtlines <= 0 ) { /* need another text line */
578         if ( fgets ( f->text, MAXLINELEN, f->f ) ) {
579           f->txtlines = f->charh ;
580           if ( strchr ( f->text, FF ) ) {
581             f->lines = 0 ;      /* no more lines in this page */
582             nr = EOF ;          /* don't return any */
583           } 
584         } else {
585           nr = EOF ;
586         }
587       }
588       if ( nr != EOF ) {
589         nr = texttorun ( (uchar*) f->text, f->font, f->charh - f->txtlines, 
590                         f->charw, f->charh, f->lmargin,
591                         runs, pels ) ;
592         f->txtlines-- ;
593       } 
594       break ;
595 #endif /* UCLINUX */
596
597     case P_RAW:
598     case P_PBM:
599       if ( fread ( bits, 1, f->page->w/8, f->f ) != f->page->w/8 ) {
600         nr = EOF ;
601       } else {
602         nr = bittorun ( bits, f->page->w/8, runs ) ;
603         if ( pels ) *pels = f->page->w ;
604       }
605       break ;
606
607 #ifndef UCLINUX
608     case P_FAX:
609       nr = readruns ( f, runs, pels ) ;
610       break ;
611
612     case P_PCX:
613       nb = ( ( f->page->w + 15 ) / 16 ) * 2 ;   /* round up */
614       if ( readpcx ( bits, nb, f ) != 0 ) {
615         nr = EOF ;
616       } else {
617         nr = bittorun ( bits, nb, runs ) ;
618         if ( pels ) *pels = f->page->w ;
619       }
620       break ;
621 #endif /* UCLINUX */
622
623     }
624   } else {
625     nr = EOF ;
626   }
627   
628   if ( nr >= 0 && f->lines > 0 ) f->lines-- ;
629   
630   return nr ;
631 }
632
633
634 /* Deduce the file type by scanning buffer p of n bytes. */
635    
636 static int getformat ( uchar *p, int n )
637 {
638   int format = 0 ;
639
640   /* figure out file type if not already set */
641
642 #ifndef UCLINUX
643   if ( ! format && n < 2 ) {
644     format = I_TEXT ;
645     msg ( "W only read %d byte(s) from input file, assuming text",  n ) ;
646   } 
647
648   if ( ! format && ! p[0] && ! ( p[1] & 0xe0 ) ) {
649     format = I_FAX ;
650   } 
651 #endif /* UCLINUX */
652
653   if ( ! format && ! strncmp ( p, "P4", 2 ) ) {
654     format = I_PBM ;
655   }
656
657 #ifndef UCLINUX
658   if ( ! format && n >= 128 && p[0] == 0x0a && 
659        strchr ("\02\03\05", p[1] ) && p[2] <= 1 ) {
660     if ( p[65] != 1 ) {
661       msg ( "E can't read colour PCX" ) ;
662     } else {
663       format = p[2] ? I_PCX : I_PCX ;
664     }
665   }
666
667   if ( ! format && ! strncmp ( p, "%!", 2 ) ) {
668     msg ( "W Postscript input file will be treated as text" ) ;
669   }
670
671   if ( ! format && ( p[0] == 'M' || p[1] == 'I' ) && ( p[1] == p[0] ) ) {
672     format = I_TIFF ;
673   }
674   
675   if ( ! format &&
676        ( p[0] == 0x3a && p[1] == 0xde && 
677          p[2] == 0x68 && p[3] == 0xb1) ) {
678     format = I_DCX ;
679   }
680   
681   if ( ! format &&  n ) { /* "90% printable" heuristic */
682     int i, nprint = 0 ;
683     for ( i=0 ; i<n ; i++ ) {
684       if ( isspace ( p[i] ) || isprint ( p[i] ) ) {
685         nprint++ ;
686       }
687     }
688     if ( ( nprint / (float) n ) > 0.90 ) {
689       format = I_TEXT ;
690     }
691   }
692 #endif /* UCLINUX */
693
694   return format ;
695 }
696
697
698 /* initialize page descriptor */
699
700 static void page_init ( PAGE *p, char *fn )
701 {
702   p->fname = fn ;
703   p->offset = 0 ;
704   p->w = DEFWIDTH ;
705   p->h = DEFHEIGHT ;
706   p->xres = DEFXRES ;
707   p->yres = DEFYRES ;
708   p->format = P_FAX ;
709   p->revbits = 0 ;
710 }
711
712
713 static void page_report ( PAGE *p, int fmt, int n )
714 {
715   msg ( "F page %d : %s + %ld : %dx%d @ %.fx%.f dpi %s/%s", 
716         n, 
717         p->fname, p->offset, p->w, p->h, 
718         p->xres, p->yres, 
719         iformatname [fmt], pformatname [p->format] ) ;
720 }
721         
722 /* File handling for undefined file types */
723
724 #define auto_first 0
725 #define auto_next 0
726
727 /* File handling for TIFF files */
728
729 #define tiff_reset 0 
730
731 /* Name of TIFF tag 'tag'. */
732
733 #ifndef UCLINUX
734 char *tagname ( int tag )
735 {
736   static struct tagnamestruct {  int code ;  char *name ; } 
737   tagnames [] = {
738   { 256, "width" },
739   { 257, "length" },
740   { 258, "bits/sample" },
741   { 259, "compresssion(g3=3)" },
742   { 262, "photometric(0-min=white)" },
743   { 266, "fill order(msb2lsb=1)" },
744   { 273, "strip offsets" },
745   { 274, "orientation(1=normal)" },
746   { 277, "samples/pixel" },
747   { 278, "rows/strip" },
748   { 279, "strip byte counts" },
749   { 282, "xresolution" },
750   { 283, "yresolution" },
751   { 284, "storage(1=single plane)" },
752   { 292, "g3options" },
753   { 296, "resolution units(2=in,3=cm)" },
754   { 297, "page number" },
755   { 327, "clean fax(0=clean/1=regen/2=errors)" },
756   {0,0} },
757   *p ;
758   
759   for ( p=tagnames ; p->code ; p++ )
760     if ( tag == p->code ) break ;
761   return p->code ? p->name :  "unknown tag" ;
762 }
763
764 /* Read 2- or 4-byte integers from a file and correct for file
765    endianness.  Returns 0 if OK, 1 on error. */
766
767 int fread2 ( unsigned short *p, IFILE *f )
768 {
769   int err=0 ;
770   uchar c[2] ;
771
772   err = fread ( c, 1, 2, f->f ) == 2 ? 0 : 1 ;
773   
774   *p = f->bigend ?
775     c[0] << 8 | c[1] << 0 :
776     c[1] << 8 | c[0] << 0 ;
777     
778   return err ;
779 }
780
781 int fread4 ( unsigned long *p, IFILE *f )
782 {
783   int err=0 ;
784   uchar c[4] ;
785
786   err = fread ( c, 1, 4, f->f ) == 4 ? 0 : 1 ;
787   
788   *p = f->bigend ?
789     c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3] << 0 :
790     c[3] << 24 | c[2] << 16 | c[1] << 8 | c[0] << 0 ;
791     
792   return err ;
793 }
794 #endif /* UCLINUX */
795
796
797 /* Read a TIFF directory at current file offset, save image
798    format information and seek to next directory if any.  Returns
799    0 if OK, 2 on errors. */
800
801 #ifndef UCLINUX
802 int tiff_next ( IFILE *f )
803 {
804   int err=0 ;
805   unsigned short ntag, tag, type ;
806   unsigned long count, tv ;
807   double ftv ;
808
809   msg ( "F+ TIFF directory at %ld", ftell ( f->f ) ) ;
810
811   if ( fread2 ( &ntag, f ) ) {
812     err = msg ( "E2can't read TIFF tag count" ) ;
813   } else {
814     msg ( "F+  with %d tags", (int) ntag ) ;
815   }
816
817   for ( ; ! err && ntag > 0 ; ntag-- ) {
818
819     err = err || fread2 ( &tag, f ) ;
820     err = err || fread2 ( &type, f ) ;
821     err = err || fread4 ( &count, f ) ;
822
823     if ( type == 3 ) {                /* left-aligned short */
824       unsigned short a, b ;
825       err = err || fread2 ( &a, f ) ;
826       err = err || fread2 ( &b, f ) ;
827       tv = a ;
828     } else {                          /* long or offset to data */
829       err = err || fread4 ( &tv, f ) ;
830     }
831
832     if ( type == 5 ) {                /* float as ratio in directory data */
833       long a, b, where=0 ;
834       err = err || ( ( where = ftell ( f->f ) ) < 0 ) ;
835       err = err || fseek ( f->f, tv, SEEK_SET ) ;
836       err = err || fread4 ( &a, f ) ;
837       err = err || fread4 ( &b, f ) ;
838       err = err || fseek ( f->f, where, SEEK_SET ) ;
839       ftv = (float) a / ( b ? b : 1 ) ;
840     } else { 
841       ftv = 0.0 ;
842     }
843
844     if ( err ) {
845       err = msg ( "ES2can't read TIFF tag" ) ;
846       continue ;
847     }
848     
849 #ifdef TIFF_DEBUG
850     {
851       char *tagtype[] = { "none", "byte", "ascii", "short", "long", "ratio" } ;
852       msg ( "F  %3d %-5s tag %s %5ld (%3d:%s)", 
853             count,
854             type <= 5 ? tagtype[type] : "other",
855             count > 1 ? "@" : "=",
856             type == 5 ? (long) ftv : tv, tag, tagname(tag) ) ; 
857     }
858 #endif
859
860     switch ( tag ) {
861     case 256 :                  /* width */
862       f->page->w = tv ;
863       break ;
864     case 257 :                  /* height */
865       f->page->h = tv ;
866       break ;
867     case 259 :                  /* compression: 1=none, 3=G3 */
868       if ( tv == 1 ) {
869         f->page->format = P_RAW ;
870       } else if ( tv == 3 ) {
871         f->page->format = P_FAX ;
872       } else {
873         err = msg ( "E2can only read TIFF/G3 or TIFF/uncompressed" ) ;
874       }
875       break ;
876     case 266 :                  /* fill order */
877       f->page->revbits = ( tv == 2 ? 1 : 0 ) ;
878       break ;
879     case 273 :                  /* data offset */
880       if ( count != 1 )
881         err = msg ( "E2can't read multi-strip TIFF files" ) ;
882       else
883         f->page->offset = tv ;
884       break ;
885     case 282 :                  /* x resolution */
886       f->page->xres = ftv ;
887       break ;
888     case 283 :                  /* y resolution */
889       f->page->yres = ftv ;
890       break ;
891     case 292 :                  /* T4 options: 1=2D, 2=uncompressed */
892       if ( tv & 0x1 )
893         err = msg ( "E2can't read 2D compressed TIFF-F file" ) ;
894       if ( tv & 0x2 )
895         err = msg ( "E2can't read uncompressed TIFF-F file" ) ;
896       break ;
897     case 296 :                  /* units: 2=in, 3=cm */
898       if ( tv == 3 ) {
899         f->page->xres *= 2.54 ;
900         f->page->yres *= 2.54 ;
901       }
902       break ;
903     }
904
905   } /* end of tag reading loop */
906
907
908   if ( f->page->format == I_AUTO ) {
909     msg ( "W missing TIFF compression format, set to raw" ) ;
910     f->page->format = P_RAW ;
911   }
912   
913   if ( ! err ) {
914
915     if ( fread4 ( &(f->next), f ) ) {
916       err = msg ( "E2can't read offset to next TIFF directory" ) ;
917     } else {
918       if ( f->next ) {
919         msg ( "F , next directory at %ld.", f->next ) ;
920         if ( fseek ( f->f, f->next, SEEK_SET ) )
921           err = msg ( "ES2 seek to next TIFF directory failed" ) ;
922       } else {
923         msg ( "F , last image." ) ;
924       }
925     }
926
927   }
928
929   if ( ! f->page->offset )
930     err = msg ( "E2 missing offset to TIFF data" ) ;
931
932   return err ;
933 }
934
935
936 int tiff_first ( IFILE *f )
937 {
938   short magic, version ;
939
940   fread ( (uchar*) &magic, 1, 2, f->f ) ;
941   f->bigend = ( *(uchar*) &magic == 'M' ) ? 1 : 0 ;
942   fread2 ( &version, f ) ;
943   fread4 ( &(f->next), f ) ;
944   
945   msg ( "F TIFF version %d.%d file (%s-endian)",
946        version/10, version%10, f->bigend ? "big" : "little" ) ;
947
948   fseek ( f->f, f->next, SEEK_SET ) ;
949
950   return tiff_next ( f ) ;
951 }
952
953
954 /* File handling for text files. */
955
956 int text_next ( IFILE *f )
957 {
958   int err = 0, i, nc ;
959   char buf [ MAXLINELEN ] ;
960
961   f->page->offset = ftell ( f->f ) ;
962   f->page->format = P_TEXT ;
963
964   nc = ( f->page->w - f->lmargin ) / f->charw ;
965
966   if ( nc > MAXLINELEN ) 
967     nc = MAXLINELEN ;
968
969   for ( i = 0 ; i < f->pglines && fgets ( buf, nc, f->f ) ; i++ ) ;
970
971   f->next = ! feof(f->f) ? ftell ( f->f ) : 0 ;
972
973   err = ferror(f->f) ? 2 : 0 ;
974
975   return err ;
976 }
977
978
979 int text_first ( IFILE *f )
980 {
981   static faxfont defaultfont ;
982
983   if ( ! f->font ) {
984     /* use default font scaled 2X, 1 inch margin, 66 lines/page */
985     f->font = &defaultfont ;
986     readfont ( 0, f->font ) ; 
987     if ( ! f->charw ) f->charw = 2 * f->font->w ;
988     if ( ! f->charh ) f->charh = 2 * f->font->h ;
989     if ( ! f->lmargin ) f->lmargin = 204 ;
990     if ( ! f->pglines ) f->pglines = DEFPGLINES ;
991   }
992   /* if not set, take default values from font  */
993   if ( ! f->charw ) f->charw = f->font->w ;
994   if ( ! f->charh ) f->charh = f->font->h ;
995   if ( ! f->lmargin ) f->lmargin = 0 ;
996   if ( ! f->pglines ) f->pglines = f->page->h / f->charh - 6 ;
997   
998   return text_next ( f ) ; 
999 }
1000 #endif /* UCLINUX */
1001
1002
1003 /* File handling for PBM files */
1004
1005 static int pbm_first ( IFILE *f )
1006 {
1007   int err=0 ;
1008
1009   fseek ( f->f, 2, SEEK_SET ) ;
1010
1011   if ( ! ( f->page->w = pbmdim ( f ) ) || ! ( f->page->h = pbmdim ( f ) ) ) {
1012     err = msg ( "E2 EOF or 0 dimension in PBM header" ) ;
1013   } else if ( f->page->w % 8 ) {
1014     err = msg ( "E2 PBM width must be multiple of 8" ) ;
1015   } else {
1016     msg ( "F read %dx%d PBM header", f->page->w, f->page->h ) ;
1017   }
1018
1019   f->page->offset = ftell ( f->f ) ;
1020   f->page->format = P_PBM ;
1021   f->next = 0 ;
1022
1023   return err ;
1024 }
1025
1026 #define pbm_next 0
1027
1028
1029 /* File handling for FAX files */
1030
1031 #define fax_first 0
1032
1033 #define fax_next 0
1034
1035 /* File handling for PCX files */
1036
1037 /* get a 16-bit word in Intel byte order. */
1038
1039 #ifndef UCLINUX
1040 int fgeti ( IFILE *f )
1041 {
1042   return fgetc ( f->f ) + fgetc ( f->f ) * 256 ;
1043 }
1044
1045 int pcx_first ( IFILE *f )
1046 {
1047   int err=0, xmin, xmax, ymin, ymax, nc, nb ;
1048   long start ;
1049
1050   start = ftell ( f->f ) ;
1051
1052   fseek ( f->f, start+4, SEEK_SET ) ;
1053   xmin = fgeti ( f ) ;
1054   ymin = fgeti ( f ) ;
1055   xmax = fgeti ( f ) ;
1056   ymax = fgeti ( f ) ;
1057
1058   f->page->w = xmax - xmin + 1 ;
1059   f->page->h = ymax - ymin + 1 ;
1060
1061   f->page->xres = fgeti ( f ) ;
1062   f->page->yres = fgeti ( f ) ;
1063
1064   fseek ( f->f, start+0x41, SEEK_SET ) ;
1065   nc = fgetc ( f->f ) ;
1066   nb = fgeti ( f ) ;
1067
1068   if ( nc != 1 ) 
1069     msg ( "W mono PCX file has %d colour planes", nc ) ;
1070
1071   if ( nb != ( f->page->w + 15 ) / 16 * 2 )
1072     msg ( "W PCX file has %d bytes per scan line for %d pels",
1073           nb, f->page->w ) ;
1074
1075   f->page->offset = start + 128 ;
1076   f->page->format = P_PCX ;
1077
1078   return err ;
1079 }
1080
1081 #define pcx_next 0
1082
1083 /* File handling for DCX files */
1084
1085 int dcx_next ( IFILE *f )
1086 {
1087   int err=0 ;
1088   long thisp, nextp ;
1089
1090   /* get this and next pages' offsets */
1091
1092   fseek ( f->f, f->next, SEEK_SET ) ; 
1093   fread4 ( &thisp, f ) ;
1094   fread4 ( &nextp, f ) ;
1095
1096   /* save address of next directory entry, if any */
1097
1098   f->next = nextp ? f->next + 4 : 0 ;
1099
1100   if ( ! thisp )
1101     err = msg ( "E2 can't happen (dcx_next)" ) ;
1102   else
1103     fseek ( f->f, thisp, SEEK_SET ) ;
1104
1105   return err ? err : pcx_first ( f ) ;
1106 }
1107
1108 int dcx_first ( IFILE *f )
1109 {
1110   f->bigend = 0 ;
1111   f->next = 4 ;
1112   return dcx_next ( f ) ;
1113 }
1114
1115
1116 #define raw_reset 0
1117 #define raw_first 0
1118 #define raw_next 0
1119
1120
1121 /* input file state reset for different compression methods */
1122
1123 #define pcx_reset 0
1124 #endif /* UCLINUX */
1125 #define pbm_reset 0
1126 #ifndef UCLINUX
1127
1128 int text_reset ( IFILE *f )
1129 {
1130   int err=0 ;
1131
1132   f->lines = ( ( f->pglines * f->charh ) / f->charh ) * f->charh ;
1133   f->txtlines = 0 ;
1134   f->page->yres = f->lines > 1078 ? 196 : 98 ; /* BOGUS */
1135
1136   return err ;
1137 }
1138
1139 int fax_reset ( IFILE *f )
1140 {
1141   int pels ;
1142   short runs [ MAXRUNS ] ;
1143   
1144   newDECODER ( &f->d ) ;
1145   if ( readruns ( f, runs, &pels ) < 0 || pels ) /* skip first EOL */
1146     msg ( "W first line has %d pixels: probably not fax data", pels ) ;
1147   f->lines = -1 ;
1148
1149   return 0 ;
1150 }
1151 #endif /* UCLINUX */
1152
1153
1154 /* Skip to start of same (dp=0) or next (dp=1) page image.
1155    Returns 0 if OK, 1 if no more pages, 2 on errors. */
1156
1157 int nextipage ( IFILE *f, int dp )
1158 {
1159   int err=0 ;
1160
1161   int ( *reset [NPFORMATS] ) ( IFILE * ) = {
1162 #ifndef UCLINUX
1163     raw_reset, fax_reset,
1164 #endif /* UCLINUX */
1165                 pbm_reset,
1166 #ifndef UCLINUX
1167                 text_reset, pcx_reset
1168 #endif /* UCLINUX */
1169   }, (*pf)(IFILE*) ;
1170
1171   /* close current file if any and set to NULL */
1172
1173   if ( f->f ) {
1174     fclose ( f->f ) ;
1175     f->f = 0 ;
1176   }
1177
1178   /*  if requested, point to next page and check if done */
1179
1180   if ( dp ) {
1181     f->page++ ;
1182   }
1183   
1184   if ( f->page > f->lastpage ) {
1185     err = 1 ;
1186   }
1187
1188   /* open the file and seek to start of image data */
1189
1190   if ( ! err ) {
1191     f->f = fopen ( f->page->fname, (f->page->format == P_TEXT) ? "r" : "rb" ) ;
1192     if ( ! f->f )
1193       err = msg ( "ES2can't open %s:", f->page->fname ) ;
1194   }
1195
1196   if ( ! err && fseek ( f->f, f->page->offset, SEEK_SET ) )
1197     err = msg ( "ES2 seek failed" ) ;
1198
1199   /* default initializations */
1200
1201   f->lines = f->page->h ;
1202
1203   /* coding-specific initializations for this page */
1204   
1205   if ( ! err ) {
1206     pf = reset[f->page->format] ;
1207     if ( pf )
1208       err = (*pf)(f) ;
1209   }
1210
1211   return err ;
1212 }
1213
1214
1215 /* Returns true if on last file. */
1216
1217 int lastpage ( IFILE *f )
1218 {
1219   return f->page >= f->lastpage ;
1220 }
1221
1222 #define dfax_first 0
1223 #define dfax_next 0
1224
1225 /* Initialize an input (IFILE) structure.  This structure
1226    collects the data about images to be processed to allow a
1227    simple interface for functions that need to read image files.
1228
1229    The IFILE is initialized by building an array of information
1230    for each page (image) in all of the files.  
1231
1232    The page pointer index is initialized so that the first call
1233    to nextipage with dp=1 actually opens the first file.
1234
1235 */
1236
1237
1238 int newIFILE ( IFILE *f, char **fnames )
1239 {
1240   int err=0, i, n = 0 /* GCC paranoia */, fformat=0 ;
1241   char **p ;
1242   uchar buf[128] ;
1243   int ( *fun ) ( IFILE * ) ;
1244   
1245   int ( *first [NIFORMATS] ) ( IFILE * ) = {
1246     auto_first, pbm_first,
1247 #ifndef UCLINUX
1248                 fax_first, text_first, tiff_first, 
1249     dfax_first, pcx_first, raw_first, dcx_first
1250 #endif /* UCLINUX */
1251   } ;
1252
1253   int ( *next [NIFORMATS] ) ( IFILE * ) = {
1254     auto_next, pbm_next,
1255 #ifndef UCLINUX
1256                 fax_next, text_next, tiff_next, 
1257     dfax_next, pcx_next, raw_next, dcx_next
1258 #endif /* UCLINUX */
1259   } ;
1260
1261   f->page = f->pages ;
1262
1263   /* get info for all pages in all files */
1264
1265   for ( p=fnames ; ! err && *p ; p++ ) {
1266
1267     if ( ! ( f->f = fopen ( *p, "rb" ) ) )
1268       err = msg ( "ES2 can't open %s:", *p ) ;
1269     
1270     if ( ! err ) {
1271       n = fread ( buf, 1, 128, f->f ) ;
1272       if ( ferror ( f->f ) )
1273         err = msg ( "ES2 can't open %s:", *p ) ;
1274     }
1275     
1276     if ( ! err ) {
1277       fformat = getformat ( buf, n ) ;
1278       if ( ! fformat )
1279         err = msg ( "E2 can't get format of %s", *p ) ;
1280     }
1281
1282     if ( ! err && fseek ( f->f, 0, SEEK_SET ) )
1283       err = msg ( "ES2 can't rewind %s:", *p ) ;
1284
1285     /* get format information for all pages in this file */
1286
1287     for ( i=0 ; ! err ; i++ ) {
1288
1289       page_init ( f->page, *p ) ;
1290
1291       if ( ( fun = i ? next[fformat] : first[fformat] ) )
1292         err = (*fun)(f) ;
1293
1294       if ( ! err ) {
1295
1296         page_report ( f->page, fformat, f->page - f->pages + 1 ) ;
1297
1298         f->page++ ;
1299         
1300         if ( f->page >= f->pages + MAXPAGE )
1301           err = msg ( "E2 too many pages (max is %d)", MAXPAGE ) ;
1302       }
1303
1304       if ( ! f->next ) break ;
1305     }
1306
1307     if ( f->f ) {
1308       fclose ( f->f ) ;
1309       f->f = 0 ;
1310     }
1311
1312   }
1313
1314   f->lastpage = f->page - 1 ;
1315
1316   f->page = f->pages ;
1317   
1318   if ( ! normalbits[1] ) initbittab() ; /* bit-reverse table initialization */
1319
1320   return err ;
1321 }
1322
1323                 /* Image File Output Functions */
1324
1325 /* Strings and function to write a bit map in HP-PCL format. The only
1326    compression is removal of trailing zeroes.  Margins and resolution are
1327    set before first write.  */
1328
1329 #ifndef UCLINUX
1330 char *PCLBEGIN =
1331          "\033E"                /* Printer reset. */
1332          "\033&l0E"             /* top  margin = 0 */
1333          "\033&a0L"             /* left margin = 0 */
1334          "\033*t%dR"            /* Set raster graphics resolution */
1335          "\033*r1A" ;           /* Start raster graphics, rel. adressing */
1336
1337 char *PCLEND = 
1338          "\033*rB"              /* end raster graphics */
1339          "\014"                 /* form feed */
1340          "\033E" ;              /* Printer reset. */
1341
1342 void pclwrite ( OFILE *f, unsigned char *buf, int n )
1343 {
1344   while ( n > 0 && buf [ n-1 ] == 0 ) n-- ; 
1345   fprintf( f->f, "\033*b%dW", n ) ;
1346   fwrite ( buf, n, 1, f->f ) ;
1347 }
1348
1349
1350 /* Write a bit map as (raw) Portable Gray Map (PGM) format after
1351    decimating by a factor of 4.  Sums bits in each 4x4-pel square
1352    to compute sample value.  This function reduces each dimension
1353    of a bit map by 4 (it writes n*8/4 pixels per scan line and
1354    one scan line for every 4 in).  The 17 possible sample values
1355    are spread linearly over the range 0-255. */
1356
1357 void pgmwrite ( OFILE *f, uchar *buf, int n )
1358 {
1359   static uchar gval [ MAXBITS * 8 / 4 ] ;
1360   static int init=0, lines=0 ;
1361   static uchar hbits [ 256 ], lbits [ 256 ] ;
1362   static int nybblecnt [ 16 ] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4 } ;
1363   static uchar corr [ 17 ] = { 255, 239, 223, 207, 191, 175, 159, 143, 127, 
1364                                 111,  95,  79,  63,  47,  31,  15,   0 } ;
1365   int m ;
1366   uchar *p, *q ; 
1367
1368   if ( ! init ) {           /*  build table of bit counts in each nybble */
1369     short i ;
1370     for ( i=0 ; i<256 ; i++ ) {
1371         hbits [ i ] = nybblecnt [ i >> 4 & 0x0f ] ;
1372         lbits [ i ] = nybblecnt [ i & 0x0f ] ;
1373       }
1374     init = 1 ;
1375   }
1376
1377   for ( m=n, p=gval, q=buf ; m-- > 0 ; q++ ) {
1378     *p++ += hbits [ *q ] ;
1379     *p++ += lbits [ *q ] ;
1380   }
1381   
1382   if ( ( lines++ & 0x03 ) == 0x03 ) {
1383     for ( p=gval, m=2*n ; m-- > 0 ; p++ ) *p = corr [ *p ] ;
1384     fwrite ( gval,  1, 2*n, f->f ) ;
1385     memset ( gval,  0, 2*n ) ;
1386   }
1387 }
1388
1389
1390 /* Postscript image data is differentially coded vertically and
1391    run-length coded horizontally.  A leading byte (n) defines the type
1392    of coding for subsequent data:
1393
1394    0        repeat previous line
1395    1-127    n data bytes follow
1396    128-254  copy n-127 bytes from previous line
1397    255 n    repeat the next character 'n' times 
1398
1399    The overhead for coding a copy is 2 bytes (copy count, data count),
1400    so copies > 2 bytes should be so coded.  The overhead for coding a
1401    run is 4 bytes (255, count, byte, data count), so runs > 4 bytes
1402    should be so coded.  Copies decode/execute faster and code more
1403    compactly so are preferred over runs.
1404
1405 */
1406
1407 const char PSBEGIN [] =         /* start of file */
1408   "%%!PS-Adobe-2.0 EPSF-2.0 \n"
1409   "%%%%Creator: efax (Copyright 1995 Ed Casas) \n"
1410   "%%%%Title: efix output\n"
1411   "%%%%Pages: (atend) \n"
1412   "%%%%BoundingBox: 0 0 %d %d \n"
1413   "%%%%BeginComments \n"
1414   "%%%%EndComments \n"
1415   "/val 1 string def \n"
1416   "/buf %d string def   \n"
1417   "/getval { \n"
1418   "  currentfile val readhexstring pop 0 get \n"
1419   "} bind def \n"
1420   "/readbuf { \n"
1421   "  0  %% => index \n"
1422   "  { \n"
1423   "    dup buf length ge { exit } if \n"
1424   "    getval   %% => index run_length \n"
1425   "    dup 127 le { \n"
1426   "      dup 0 eq { \n"
1427   "        pop buf length \n"
1428   "      } { \n"
1429   "        currentfile buf 3 index 3 index getinterval readhexstring pop pop\n"
1430   "      } ifelse \n"
1431   "    } { \n"
1432   "      dup 255 eq { \n"
1433   "        pop getval getval %% => index run_length value \n"
1434   "        2 index 1 3 index 2 index add 1 sub  %% => ... start 1 end \n"
1435   "          { buf exch 2 index put } for \n"
1436   "        pop \n"
1437   "      } { \n"
1438   "        127 sub \n"
1439   "      } ifelse \n"
1440   "    } ifelse \n"
1441   "    add %% => index \n"
1442   "  } loop \n"
1443   "  pop \n"
1444   "  buf \n"
1445   "} bind def \n"
1446   "%%%%EndProlog \n" ;
1447
1448 const char PSPAGE [] =          /* start of page */
1449   "%%%%Page: %d %d \n"
1450   "gsave \n"
1451   "%f %f translate \n"
1452   "%f %f scale \n"
1453   "%d %d %d [ %d %d %d %d %d %d ] { readbuf } image \n" ;
1454
1455 const char PSPAGEEND [] =       /* end of page */
1456   "\n"
1457   "grestore \n"
1458   "showpage \n" ;
1459
1460 const char PSEND [] =           /* end of file */
1461   "%%Trailer \n"
1462   "%%%%Pages: %d \n" ;
1463
1464
1465 void psinit ( OFILE *f, int newfile, int page, int w, int h, int n )
1466 {
1467   float ptw, pth ;
1468
1469   if ( ! f ) {
1470     msg ( "E2 can't happen (psinit)" ) ;
1471     return ;
1472   }
1473
1474   ptw = w/f->xres * 72.0 ;                 /* convert to points */
1475   pth = h/f->yres * 72.0 ;
1476
1477   if ( newfile )
1478     fprintf ( f->f, PSBEGIN, 
1479             (int) ptw, (int) pth,                /* Bounding Box */
1480             n ) ;                                /* buffer string length */
1481
1482   fprintf ( f->f, PSPAGE, 
1483           page, page,                            /* page number */
1484           0.0, 0.0,                              /* shift */
1485           ptw, pth,                              /* scaling */
1486           w, h, 1,                               /* image size */
1487           w, 0, 0, -h, 0, h ) ;                  /* CTM */
1488   f->pslines = 0 ;
1489   f->lastpageno = page ;
1490 }
1491
1492
1493 char nhexout = 0, hexchars [ 16 ] = "0123456789abcdef" ;
1494
1495 #define hexputc( f, c ) ( \
1496         putc ( hexchars [ (c) >>   4 ], f ), \
1497         putc ( hexchars [ (c) & 0x0f ], f ), \
1498         ( ( ( nhexout++ & 31 ) == 31 ) ? putc ( '\n', f ) : 0 ) )
1499
1500 void hexputs ( FILE *f, uchar *p, int n )
1501 {
1502   uchar c ;
1503   if ( n > 0 ) {
1504     hexputc ( f, n ) ;
1505     while ( n-- ) { c = *p++ ^ 0xff ; hexputc ( f, c ) ; }
1506   }
1507 }
1508
1509 /* Encode into postscript.  If not a repeated line, test (using
1510    index j) from current position (i) for possible encodings as:
1511    copy of > 2 bytes, runs of > 4 or data >=127.  Otherwise the
1512    byte is skipped. Uncoded bytes are output from the last
1513    uncoded byte (l) before output of runs/copies.  */
1514
1515 void pswrite ( OFILE *f, unsigned char *buf, int n )
1516 {
1517   int i, j, l ;
1518   static unsigned char last [ MAXBITS ] ;
1519   
1520   l=i=0 ;
1521
1522   if ( ! f || ! buf || n<0 ) {
1523     msg ( "E2 can't happen (pswrite)" ) ;
1524     return ;
1525   }
1526
1527   for ( j=0 ; j<n && buf[j]==last[j] && f->pslines ; j++ ) ;
1528   if ( j == n ) {               /* repeat line */
1529     hexputc ( f->f, 0 ) ;
1530     l=i=n ;
1531   }
1532
1533   while ( i<n ) {
1534
1535     for ( j=i ; j<n && buf[j]==last[j] && j-i<127 && f->pslines ; j++ ) ;
1536     if ( j-i > 2 ) {            /* skip */
1537       hexputs ( f->f, buf+l, i-l ) ;
1538       hexputc ( f->f, j-i + 127 ) ; 
1539       l=i=j ;
1540     } else {
1541       for ( j=i ; j<n && buf[j]==buf[i] && j-i<255 ; j++ ) ;
1542       if ( j-i > 4 ) {          /* run */
1543         hexputs ( f->f, buf+l, i-l ) ;
1544         hexputc ( f->f, 255 ) ; 
1545         hexputc ( f->f, j-i ) ; 
1546         hexputc ( f->f, buf[i] ^ 0xff ) ;
1547         l=i=j ;
1548       } else {
1549         if ( i-l >= 127 ) {     /* maximum data length */
1550           hexputs ( f->f, buf+l, i-l ) ;
1551           l=i ;
1552         } else {                /* data */
1553           i++ ;
1554         }
1555       }
1556     }
1557
1558   }
1559   hexputs ( f->f, buf+l, i-l ) ;
1560
1561   if ( n >= 0 ) 
1562     memcpy ( last, buf, n ) ;
1563
1564   f->pslines++ ;
1565 }
1566
1567
1568 /* Write 2- and 4-byte integers to an image output file.  Return
1569    as for fwrite. */
1570
1571 int fwrite2 ( short s, OFILE *f )
1572 {
1573   uchar *p = (void*) &s ;
1574   return fwrite ( bigendian ? p + sizeof(short) - 2 : p, 2, 1, f->f ) ;
1575 }
1576
1577 int fwrite4 ( long l, OFILE *f )
1578 {
1579   uchar *p = (void*) &l ;
1580   return fwrite ( bigendian ? p + sizeof(long ) - 4 : p, 4, 1, f->f ) ;
1581 }
1582
1583
1584 /* Write a TIFF directory tag.  Returns 0 if OK, 1 on errors. */
1585
1586 int wtag ( OFILE *f, int lng, short tag, short type, long count, long offset )
1587 {
1588   int err=0 ;
1589
1590   err = err || ! fwrite2 ( tag,   f ) ;
1591   err = err || ! fwrite2 ( type,  f ) ;
1592   err = err || ! fwrite4 ( count, f ) ;
1593   if ( lng ) {
1594     err = err || ! fwrite4 ( offset, f ) ;
1595   } else {
1596     err = err || ! fwrite2 ( offset, f ) ;
1597     err = err || ! fwrite2 (      0, f ) ;
1598   }
1599
1600   if ( err ) msg ( "ES2 can't write TIFF tag" ) ;
1601   return err ;
1602 }
1603
1604
1605 /* Write TIFF header and directory.  File format based on Sam
1606    Leffler's tiff.h.  Can only be used for single-image TIFFs
1607    because always seeks to start of file to re-write the
1608    header. */
1609
1610 #define NTAGS 17                      /* number of tags in directory */
1611 #define NRATIO 2                      /* number of floats (as ratios) */
1612
1613 int tiffinit ( OFILE *f )
1614 {
1615   int err=0, compr=1 ;
1616   long tdoff, doff ;
1617
1618   fseek ( f->f, 0, SEEK_SET ) ;
1619
1620   /* 0 ==> (start of TIFF file) */
1621
1622   /* write magic, TIFF version and offset to directory */
1623
1624   fwrite2 ( bigendian ? 0x4d4d : 0x4949, f ) ;
1625   fwrite2 ( 42, f ) ;
1626   fwrite4 ( 8, f ) ;
1627
1628   /* 8 ==> directory */
1629
1630   fwrite2 ( NTAGS, f ) ;
1631
1632   /* figure out offsets within file and compression code */
1633
1634   tdoff = 8 + 2 + NTAGS*12 + 4 ;     /* offset to directory data */
1635   doff = tdoff + NRATIO*8 ;          /* offset to image data */
1636
1637   switch ( f->format ) {
1638   case O_TIFF_RAW: compr = 1 ; break ;
1639   case O_TIFF_FAX: compr = 3 ; break ;
1640   default: err = msg ( "E2can't happen(tiffinit)" ) ; break ;
1641   }
1642
1643   /* write directory tags, 12 bytes each */
1644
1645   wtag( f, 1, 256, 4, 1, f->w ) ;     /* width long */
1646   wtag( f, 1, 257, 4, 1, f->h ) ;     /* length long */
1647   wtag( f, 0, 258, 3, 1, 1 ) ;        /* bits/sample short */
1648
1649   wtag( f, 0, 259, 3, 1, compr ) ;    /* compresssion(g3=3) short */
1650   wtag( f, 0, 262, 3, 1, 0 ) ;        /* photometric(0-min=white) short */
1651   wtag( f, 0, 266, 3, 1, 1 ) ;        /* fill order(msb2lsb=1) short */
1652   wtag( f, 1, 273, 4, 1, doff ) ;     /* strip offsets long */
1653
1654   wtag( f, 0, 274, 3, 1, 1 ) ;        /* orientation(1=normal) short */
1655   wtag( f, 0, 277, 3, 1, 1 ) ;        /* samples/pixel short */
1656   wtag( f, 1, 278, 4, 1, f->h ) ;     /* rows/strip long */
1657   wtag( f, 1, 279, 4, 1, f->bytes ) ; /* strip byte counts long */
1658
1659   wtag( f, 1, 282, 5, 1, tdoff+0 ) ;  /* xresolution ratio */
1660   wtag( f, 1, 283, 5, 1, tdoff+8 ) ;  /* yresolution ratio */
1661   wtag( f, 0, 284, 3, 1, 1 ) ;        /* storage(1=single plane) short */
1662   wtag( f, 1, 292, 4, 1, 0 ) ;        /* g3options long */
1663
1664   wtag( f, 0, 296, 3, 1, 2 ) ;        /* resolution units(2=in,3=cm) short */
1665   wtag( f, 0, 327, 3, 1, 0 ) ;        /* clean fax(0=clean) short */
1666   
1667   fwrite4 ( 0, f ) ;                  /* offset to next dir (no more) */
1668
1669   /* ==> tdoff (tag data offset), write ratios for floats here */
1670
1671   fwrite4 ( f->xres+0.5, f ) ;
1672   fwrite4 ( 1, f ) ;
1673   fwrite4 ( f->yres+0.5, f ) ;
1674   fwrite4 ( 1, f ) ;
1675
1676   /* ==> doff (strip data offset), image data goes here */
1677
1678   return err ;
1679 }
1680
1681
1682 /* Convert array 'runs' of 'nr' run lengths into a bit map 'buf'. Returns
1683    the number of bytes filled. */
1684
1685 int runtobit ( short *runs, int nr, uchar *buf )
1686 {
1687   static uchar zerofill [ 9 ] = { 
1688     0xff,  0xfe, 0xfc, 0xf8, 0xf0,  0xe0, 0xc0, 0x80, 0x00 } ;
1689   static uchar onefill [ 9 ] = { 
1690     0x00,  0x01, 0x03, 0x07, 0x0f,  0x1f, 0x3f, 0x7f, 0xff } ; 
1691
1692   uchar col=0, *buf0 = buf ;
1693   register short len, b=8, bytes ;
1694   
1695   while ( nr-- > 0 ) {
1696     len = *runs++ ;
1697     if ( col ) *buf |= onefill  [ b ] ;          /* right bits of cur. byte */
1698     else       *buf &= zerofill [ b ] ;
1699     if ( b > len ) {                             /* done fill */
1700       b -= len ;
1701     } else {                                     /* continue to next byte */
1702       len -= b ; 
1703       buf++ ; 
1704       b = 8 ;
1705       if ( ( bytes = len>>3 ) > 0 ) {            /* fill >1 byte */
1706         memset ( buf, col, bytes ) ;
1707         len -= bytes*8; 
1708         buf += bytes ;
1709       } 
1710       *buf = col ;                               /* flood the rest */
1711       b -= len ;
1712     }
1713     col ^= 0xff ;
1714   }
1715
1716   return buf - buf0 + ( b < 8 ) ;
1717 }
1718
1719
1720 /* Write a PCX file header. */
1721
1722 int fputi ( int i, OFILE *f )
1723 {
1724   putc ( i & 0xff, f->f ) ;
1725   putc ( ( i >> 8 ) & 0xff, f->f ) ;
1726   return 0 ;
1727 }
1728
1729 void pcxinit ( OFILE *f )
1730 {
1731   uchar buf [ 60 ] = { 0x0a, 3, 1, 1 } ; /* magic, version, compr, BPP */
1732   
1733   fwrite ( buf, 1, 4, f->f ) ;  /* 4 */
1734   fputi ( 0, f ) ;              /* 8 xmin, ymin, xmax, ymax */
1735   fputi ( 0, f ) ;
1736   fputi ( f->w-1, f ) ;
1737   fputi ( f->h-1, f ) ;
1738   fputi ( f->xres, f ) ;        /* 4 x and y dpi */
1739   fputi ( f->yres, f ) ;
1740   memset ( buf, 0, 48 ) ;       /* 48 palette */
1741   fwrite ( buf, 1, 48, f->f ) ;
1742   putc ( 0, f->f ) ;            /* 1 reserved */
1743   putc ( 1, f->f ) ;            /* 1 planes per pixel  */
1744   fputi ( (f->w+15)/16*2, f ) ; /* 2 bytes per line */
1745   memset ( buf, 0, 60 ) ;       /* 60 zero */
1746   fwrite ( buf, 1, 60, f->f ) ;
1747 }
1748
1749 /* Write a PCX-compressed scan line. */
1750
1751 void  pcxwrite ( OFILE *of, uchar *p, int nb )
1752 {
1753   int c, n, runc ;
1754   FILE *f = of->f ;
1755
1756   runc = *p++ ;
1757   n = 1 ;
1758
1759   for ( nb-- ; nb > 0 ; nb-- ) {
1760     c = *p++ ;
1761     if ( c == runc && n < 63 ) { /* continue run */
1762       n++ ;
1763     } else {            /* terminate run */
1764       if ( n > 1 || ( ( runc & 0xc0 ) == 0xc0 ) ) /* output as run */
1765         putc ( n | 0xc0, f ) ;
1766       putc ( runc, f ) ;
1767       runc = c ;        /* start new run */
1768       n = 1 ;
1769     }
1770   }
1771
1772   /* last run */
1773
1774   if ( n > 1 || ( ( runc & 0xc0 ) == 0xc0 ) ) /* output as run */
1775     putc ( n | 0xc0, f ) ;
1776   putc ( runc, f ) ;
1777
1778 }
1779
1780
1781 /* Begin/end output pages.  If not starting first page (0), terminate
1782    previous page.  If output filename pattern is defined, [re-]opens that
1783    file.  If not terminating last page (page==EOF), writes file header.
1784    Returns 0 or 2 on errors. */
1785
1786 int nextopage ( OFILE *f, int page )
1787 {
1788   int err = 0 ;
1789   int i, nb=0 ;
1790   uchar *p, codes [ ( RTCEOL * EOLBITS ) / 8 + 3 ] ;
1791   
1792   if ( f->f ) { /* terminate previous page */
1793
1794     switch ( f->format ) {
1795     case O_PBM:
1796       break ;
1797     case O_PGM:
1798       break ;
1799     case O_FAX:
1800     case O_TIFF_FAX:
1801       for ( p = codes, i=0 ; i<RTCEOL ; i++ ) 
1802         p = putcode ( &f->e, EOLCODE, EOLBITS, p ) ;
1803       nb = putcode ( &f->e, 0, 0, p ) - codes ;
1804       fwrite ( codes, 1, nb, f->f ) ;
1805       f->bytes += nb ;
1806       if ( f->format == O_TIFF_FAX ) tiffinit ( f ) ;
1807       break ;
1808     case O_TIFF_RAW:
1809       tiffinit(f) ;             /* rewind & update TIFF header */
1810       break ;
1811     case O_PCL:
1812       fprintf ( f->f, PCLEND ) ;
1813       break ;
1814     case O_PS:
1815       fprintf ( f->f, PSPAGEEND ) ;
1816       if ( f->fname || page<0 ) fprintf ( f->f, PSEND, f->lastpageno ) ;
1817       break ;
1818     case O_PCX:
1819     case O_PCX_RAW:
1820       fseek ( f->f, 0, SEEK_SET ) ;
1821       pcxinit ( f ) ;
1822       break ;
1823     }
1824
1825     if ( ferror ( f->f ) ) {
1826       err = msg ("ES2output error:" ) ;
1827     } else {
1828       msg ( "F+ wrote %s as %dx%d pixel %.fx%.f dpi %s page", 
1829            f->cfname, f->w, f->h, f->xres, f->yres, 
1830            oformatname [f->format] ) ;
1831       
1832       switch ( f->format ) {
1833       case O_PS: 
1834         msg ( "F  (%d lines)", f->pslines ) ;
1835         break ;
1836       case O_TIFF_RAW:
1837       case O_TIFF_FAX:
1838         msg ( "F  (%d bytes)", f->bytes ) ;
1839         break ;
1840       default:
1841         msg ( "F " ) ;
1842         break ;
1843       }
1844
1845     }
1846
1847   }
1848
1849   if ( ! err && page >= 0 ) {   /* open new file */
1850     if ( f->fname ) {
1851       sprintf ( f->cfname, f->fname, page+1, page+1, page+1 ) ;
1852
1853       if ( ! f->f )
1854         f->f = fopen ( f->cfname, ( f->format == O_PS ) ? "w" : "wb+" ) ;
1855       else
1856         f->f = freopen ( f->cfname, ( f->format == O_PS ) ? "w" : "wb+", f->f ) ;
1857
1858       if ( ! f->f ) {
1859         err = msg ("ES2can't open output file %s:", f->cfname ) ;
1860       }
1861     } else {
1862       f->f = stdout ;
1863       strcpy ( f->cfname, "standard output" ) ;
1864     }
1865   }
1866
1867   /* start new page */
1868
1869   if ( ! err && page >= 0 ) {
1870     switch ( f->format ) {
1871     case  O_PBM:
1872       fprintf ( f->f, "P4 %d %d\n", f->w, f->h ) ;
1873       break ;
1874     case  O_PGM:
1875       fprintf ( f->f, "P5 %d %d %d\n", f->w/4, f->h/4, 255 ) ;
1876       break ;
1877     case O_FAX:
1878     case O_TIFF_FAX:
1879       if ( f->format == O_TIFF_FAX ) tiffinit ( f ) ;
1880       p = putcode ( &f->e, EOLCODE, EOLBITS, codes ) ;
1881       nb = p - codes ;
1882       fwrite ( codes, 1, nb, f->f ) ;
1883       break ;
1884     case O_TIFF_RAW:
1885       tiffinit ( f ) ;
1886       break ;
1887     case O_PCL:
1888       fprintf ( f->f, PCLBEGIN, (int) f->xres ) ;
1889       break ;
1890     case O_PS:
1891       psinit ( f, ( f->fname || page==0 ), page+1, f->w, f->h, f->w/8 ) ;
1892       break ;
1893     case O_PCX:
1894     case O_PCX_RAW:
1895       fseek ( f->f, 0, SEEK_SET ) ;
1896       pcxinit ( f ) ;
1897       break ;
1898     }
1899
1900     if ( ferror ( f->f ) ) err = msg ("ES2output error:" ) ;
1901   }
1902
1903   /* only count lines/bytes for those formats that don't have
1904      headers or where we will update the headers on closing */
1905
1906   switch ( f->format ) {
1907   case O_FAX:
1908   case O_TIFF_FAX:
1909   case O_PCX:
1910   case O_PCX_RAW:
1911     f->h = 0 ;
1912     f->bytes = nb ;
1913     break ;
1914   }
1915
1916   return err ;
1917 }
1918
1919
1920 /* Output scan line of nr runs no times to output file f. */
1921
1922 void writeline ( OFILE *f, short *runs, int nr, int no )
1923 {
1924   int nb = 0 ;
1925   uchar *p, buf [ MAXCODES ] ;
1926
1927   /* if line to be output, convert to right format */
1928
1929   if ( no > 0 )
1930     switch ( f->format ) {
1931     case O_PBM:
1932     case O_PGM:
1933     case O_PCL:
1934     case O_PS:
1935     case O_TIFF_RAW:
1936     case O_PCX:
1937     case O_PCX_RAW:
1938       nb = runtobit ( runs, nr, buf ) ;
1939       break ;
1940     case O_FAX:
1941     case O_TIFF_FAX:
1942       break ;
1943     }
1944   
1945   /* output `no' times. */
1946     
1947   while ( no-- > 0 ) {
1948     switch ( f->format ) {
1949     case O_PCX_RAW:
1950     case O_TIFF_RAW:
1951     case O_PBM:
1952       fwrite ( buf, 1, nb, f->f ) ;
1953       break ;
1954     case O_PGM:
1955       pgmwrite ( f, buf, nb ) ;
1956       break ;
1957     case O_TIFF_FAX:
1958     case O_FAX:
1959       p = runtocode ( &f->e, runs, nr, buf ) ;
1960       p = putcode ( &f->e, EOLCODE, EOLBITS, p ) ;
1961       nb = p - buf ;
1962       fwrite ( buf, 1, nb, f->f ) ;
1963       break ;
1964     case O_PCL:
1965       pclwrite ( f, buf, nb ) ;
1966       break ;
1967     case O_PS:
1968       pswrite ( f, buf, nb ) ;
1969       break ;
1970     case O_PCX:
1971       pcxwrite ( f, buf, nb ) ;
1972       break ;
1973     }
1974
1975   /* only count lines/bytes for those formats that don't have
1976      headers or where we will update the headers on closing */
1977
1978     switch ( f->format ) {
1979     case O_FAX:
1980     case O_TIFF_FAX:
1981     case O_TIFF_RAW:
1982     case O_PCX:
1983     case O_PCX_RAW:
1984       f->h++ ;
1985       f->bytes += nb ;
1986       break ;
1987     }
1988     
1989   }
1990 }
1991
1992
1993 /* Initialize new output file. If fname is NULL, stdout will be used for
1994    all images. */
1995
1996 void newOFILE ( OFILE *f, int format, char *fname, 
1997                float xres, float yres, int w, int h )
1998 {
1999   f->f = 0 ;
2000   f->format = format ;
2001   f->fname = fname ;
2002   f->xres = xres ;
2003   f->yres = yres ;
2004   f->w = w ;
2005   f->h = h ;
2006   f->bytes = 0 ;
2007   newENCODER ( &f->e ) ;
2008 }
2009
2010 /* Read a bitmap to use as a font and fill in the font data.  If
2011    the file name is null, empty, or there are errors, the font is
2012    initialized to the built-in font. Returns 0 if OK, 2 on
2013    errors. */
2014
2015 int readfont ( char *fname, faxfont *font )
2016 {
2017   int err=0, i, j, n=0, nr, nb, fontok=0, pels ;
2018   char *fnames [2] = { 0, 0 } ;
2019   short runs [ MAXRUNS ] ;
2020   IFILE f;
2021
2022   if ( fname && *fname ) {
2023
2024     fnames[0] = fname ;
2025     
2026     newIFILE ( &f, fnames ) ;
2027     
2028     if ( nextipage ( &f, 0 ) ) {
2029       err = msg ( "E2 can't open font file %s", fnames[0] ) ;
2030     }
2031     
2032     nb = 0 ;
2033     while ( ! err && ( nr = readline ( &f, runs, &pels ) ) >= 0 ) {
2034       if ( nb+pels/8 < MAXFONTBUF ) {
2035         nb += runtobit ( runs, nr, font->buf+nb ) ;
2036       } else {
2037         err = msg ("E2font file %s too large (max %d bytes)", 
2038                    fnames[0], MAXFONTBUF ) ;
2039       }
2040     }
2041     
2042     if ( ! err && nb != f.page->w * f.page->h / 8 )
2043       err = msg ( "E2 read %d bytes of font data for %dx%d bitmap",
2044                  nb, f.page->w, f.page->h ) ;
2045     
2046     if ( ! err && ( f.page->w / 256 > MAXFONTW || f.page->h > MAXFONTH ) ) {
2047       err = msg ( "E2font size (%dx%d) too large", f.page->w, f.page->h ) ;
2048     }
2049     
2050     if ( err ) {
2051       font->w = font->h = 0 ;
2052     } else {
2053       font->w = f.page->w / 256 ;
2054       font->h = f.page->h ;
2055       for ( i=0 ; i<256 ; i++ ) font->offset[i] = i*font->w ;
2056       msg ("Iread %dx%d font %s (%d bytes)", font->w, font->h, fname, nb ) ;
2057       fontok = 1 ;
2058     }
2059
2060     if ( f.f ) {
2061       fclose ( f.f ) ;
2062       f.f = 0 ;
2063     }
2064   }    
2065
2066   if ( ! fontok ) {                /* use built-in font */
2067
2068     font->w = STDFONTW ;
2069     font->h = STDFONTH ;
2070
2071     for ( i=j=0 ; j<STDFONTBUF ; i++ )     /* expand bit map */
2072       if ( stdfont [ i ] == 0 )
2073         for ( n = stdfont [ ++i ] ; n > 0 ; n-- ) 
2074           font->buf [ j++ ] = 0 ;
2075       else
2076         font->buf [ j++ ] = stdfont [ i ] ;
2077
2078     if ( i != 1980 ) err = msg ( "E2can't happen(readfont)" ) ;
2079
2080     for ( i=0 ; i<256 ; i++ ) font->offset[i] = i*font->w ;
2081   }
2082
2083   return err ;
2084 }
2085 #endif /* UCLINUX */
2086
2087
2088 /* Initialize bit reversal lookup tables (note that the
2089    `normalbits' array is the one actually used for the bit
2090    reversal.  */
2091
2092 void initbittab ( void )
2093 {
2094   int i ;
2095   for ( i=0 ; i<256 ; i++ ) 
2096     normalbits [ reversebits [ i ] = i ] = 
2097       ( i& 1 ? 128:0 ) | ( i& 2 ? 64:0 ) | ( i& 4 ? 32:0 ) | ( i&  8 ? 16:0 ) |
2098       ( i&16 ?   8:0 ) | ( i&32 ?  4:0 ) | ( i&64 ?  2:0 ) | ( i&128 ?  1:0 ) ;
2099 }
2100
2101
2102                    /* T.4 Encoding/Decoding */
2103
2104 /* Table-lookup decoder for variable-bit-length codewords.  The table index
2105    is the N most recently undecoded bits with the first (oldest) undecoded
2106    bit as the MS bit.  If the N bits uniquely identify a codeword then the
2107    indexed 'code' member identifies the code, otherwise it is zero.  The
2108    'bits' member gives the number of bits to be considered decoded (to be
2109    removed from the bit stream) and the 'next' element is a pointer to the
2110    table to use for decoding the next part of the bit sequence.
2111
2112    For T.4 decoding the longest T.4 codeword is 13 bits. The implementation
2113    below uses two tables of 512 elements (N=9 bits) for each colour.
2114    Codewords longer than 9 bits require a second lookup.  Since all
2115    codewords longer than than 9 bits have a 4-bit zero prefix it is
2116    possible to use only one secondary 9-bit lookup table by dropping only
2117    the first 4 bits after the first lookup. The code indentifier is the run
2118    length + 1. A separate table is used for decoding the variable-length
2119    FILL patterns.
2120
2121    For undefined codewords, one bit is skipped and decoding continues at
2122    the white code table. */
2123
2124 /* the lookup tables for each colour and the fill lookup table */
2125
2126 #ifndef UCLINUX
2127 dtab tw1 [ 512 ], tw2 [ 512 ], tb1 [ 512 ], tb2 [ 512 ], fill [ 512 ] ;
2128 static char tabinit=0 ;
2129
2130 /* Add code cword shifted left by shift to decoding table tab. */
2131
2132 void addcode ( dtab *tab, int cword, int shift, 
2133               short code, short bits, dtab *next )
2134 {
2135   int i, n = 1 << shift ;
2136   
2137   for ( i = cword << shift ; n-- > 0 ; i++ ) {
2138     tab[i].code = code ;
2139     tab[i].bits = bits ;
2140     tab[i].next = next ;
2141   }
2142 }
2143
2144 /* Initialize the decoding table for one colour using the codes in the T.4
2145    table p0.  t1 and t2 are the two decoding tables and ot is the first
2146    table of the other colour. */
2147
2148 void init1dtab ( t4tab *p0, dtab *t1, dtab *t2, dtab *ot )
2149 {
2150   t4tab *p ;
2151   for ( p = p0 ; p->code ; p++ ) 
2152     if ( p->bits <= 9 ) {
2153       addcode ( t1, p->code, 9 - p->bits, p->rlen + 1, p->bits,   
2154                ( p - p0 ) > 63 ? t1 : ot ) ;
2155     } else {
2156       addcode ( t1, p->code >> ( p->bits - 9 ), 0, 0, 4, t2 ) ;
2157       addcode ( t2, p->code, 13 - p->bits, p->rlen + 1, p->bits - 4, 
2158                ( p - p0 ) > 63 ? t1 : ot ) ;
2159     }
2160 }
2161 #endif /* UCLINUX */
2162
2163
2164 /* Initialize a T.4 decoder.   */
2165
2166 #ifndef UCLINUX
2167 void newDECODER ( DECODER *d )
2168 {
2169   int i ;
2170
2171   if ( ! tabinit ) {
2172
2173     /* undefined codes */
2174
2175     addcode ( tw1,  0, 9, 0, 1, tw1 ) ;
2176     addcode ( tw2,  0, 9, 0, 1, tw1 ) ;
2177     addcode ( tb1,  0, 9, 0, 1, tw1 ) ;
2178     addcode ( tb2,  0, 9, 0, 1, tw1 ) ;
2179     addcode ( fill, 0, 9, 0, 1, tw1 ) ;
2180   
2181     /* fill and EOL */
2182
2183     addcode ( tw1, 0, 0, 0, 4, tw2 ) ;
2184     addcode ( tw2, 0, 2, 0, 7, fill ) ;
2185     addcode ( tb1, 0, 0, 0, 4, tb2 ) ;
2186     addcode ( tb2, 0, 2, 0, 7, fill ) ;
2187
2188     addcode ( fill, 0, 0, 0, 9, fill ) ;
2189     for ( i=0 ; i<=8 ; i++ )
2190       addcode ( fill, 1, i, -1, 9-i, tw1 ) ;
2191
2192     /* white and black runs */
2193     
2194     init1dtab ( wtab, tw1, tw2, tb1 ) ;
2195     init1dtab ( btab, tb1, tb2, tw1 ) ;
2196
2197     tabinit=1 ;
2198   }
2199
2200   /* initialize decoder to starting state */
2201
2202   d->x = 0 ;
2203   d->shift = -9 ;
2204   d->tab = tw1 ;
2205   d->eolcnt = 0 ;
2206 }
2207 #endif /* UCLINUX */
2208
2209       /* T.4 coding table and default font for efax/efix */
2210
2211 /* T.4 1-D run-length coding tables. codes must be in run length
2212    order for runtocode(). */
2213
2214 static t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] = {  /* runs of white */
2215
2216 /*  Terminating White Codes */
2217
2218 {53,8,0},    {7,6,1},     {7,4,2},     {8,4,3},     {11,4,4},    {12,4,5},
2219 {14,4,6},    {15,4,7},    {19,5,8},    {20,5,9},    {7,5,10},    {8,5,11},
2220 {8,6,12},    {3,6,13},    {52,6,14},   {53,6,15},   {42,6,16},   {43,6,17},
2221 {39,7,18},   {12,7,19},   {8,7,20},    {23,7,21},   {3,7,22},    {4,7,23},
2222 {40,7,24},   {43,7,25},   {19,7,26},   {36,7,27},   {24,7,28},   {2,8,29},
2223 {3,8,30},    {26,8,31},   {27,8,32},   {18,8,33},   {19,8,34},   {20,8,35},
2224 {21,8,36},   {22,8,37},   {23,8,38},   {40,8,39},   {41,8,40},   {42,8,41},
2225 {43,8,42},   {44,8,43},   {45,8,44},   {4,8,45},    {5,8,46},    {10,8,47},
2226 {11,8,48},   {82,8,49},   {83,8,50},   {84,8,51},   {85,8,52},   {36,8,53},
2227 {37,8,54},   {88,8,55},   {89,8,56},   {90,8,57},   {91,8,58},   {74,8,59},
2228 {75,8,60},   {50,8,61},   {51,8,62},   {52,8,63},   
2229
2230 /*  Make Up White Codes */
2231
2232 {27,5,64},   {18,5,128},  {23,6,192},  {55,7,256},  {54,8,320},  {55,8,384},
2233 {100,8,448}, {101,8,512}, {104,8,576}, {103,8,640}, {204,9,704}, {205,9,768},
2234 {210,9,832}, {211,9,896}, {212,9,960}, {213,9,1024},{214,9,1088},{215,9,1152},
2235 {216,9,1216},{217,9,1280},{218,9,1344},{219,9,1408},{152,9,1472},{153,9,1536},
2236 {154,9,1600},{24,6,1664}, {155,9,1728},
2237
2238 /*  Extended Make Up Codes (Black and White) */
2239
2240 {8,11,1792}, {12,11,1856},{13,11,1920},{18,12,1984},{19,12,2048},{20,12,2112},
2241 {21,12,2176},{22,12,2240},{23,12,2304},{28,12,2368},{29,12,2432},{30,12,2496},
2242 {31,12,2560},
2243
2244 {0,0,0}  } ;
2245
2246 static t4tab btab [ ( 64 + 27 + 13 ) + 1 ] = {  /* runs of black */
2247
2248 /*  Terminating Black Codes */
2249
2250 {55,10,0},   {2,3,1},     {3,2,2},     {2,2,3},     {3,3,4},     {3,4,5},
2251 {2,4,6},     {3,5,7},     {5,6,8},     {4,6,9},     {4,7,10},    {5,7,11},
2252 {7,7,12},    {4,8,13},    {7,8,14},    {24,9,15},   {23,10,16},  {24,10,17},
2253 {8,10,18},   {103,11,19}, {104,11,20}, {108,11,21}, {55,11,22},  {40,11,23},
2254 {23,11,24},  {24,11,25},  {202,12,26}, {203,12,27}, {204,12,28}, {205,12,29},
2255 {104,12,30}, {105,12,31}, {106,12,32}, {107,12,33}, {210,12,34}, {211,12,35},
2256 {212,12,36}, {213,12,37}, {214,12,38}, {215,12,39}, {108,12,40}, {109,12,41},
2257 {218,12,42}, {219,12,43}, {84,12,44},  {85,12,45},  {86,12,46},  {87,12,47},
2258 {100,12,48}, {101,12,49}, {82,12,50},  {83,12,51},  {36,12,52},  {55,12,53},
2259 {56,12,54},  {39,12,55},  {40,12,56},  {88,12,57},  {89,12,58},  {43,12,59},
2260 {44,12,60},  {90,12,61},  {102,12,62}, {103,12,63}, 
2261
2262 /*  Make Up Black Codes */
2263
2264 {15,10,64},  {200,12,128},{201,12,192},{91,12,256}, {51,12,320}, {52,12,384},
2265 {53,12,448}, {108,13,512},{109,13,576},{74,13,640}, {75,13,704}, {76,13,768},
2266 {77,13,832}, {114,13,896},{115,13,960},{116,13,1024},{117,13,1088},
2267 {118,13,1152},
2268 {119,13,1216},{82,13,1280},{83,13,1344},{84,13,1408},{85,13,1472},{90,13,1536},
2269 {91,13,1600},{100,13,1664},{101,13,1728},
2270
2271 /*  Extended Make Up Codes (Black and White) */
2272
2273 {8,11,1792}, {12,11,1856},{13,11,1920},{18,12,1984},{19,12,2048},{20,12,2112},
2274 {21,12,2176},{22,12,2240},{23,12,2304},{28,12,2368},{29,12,2432},{30,12,2496},
2275 {31,12,2560},
2276
2277 {0,0,0}  } ;
2278
2279
2280 /* The built-in 8x16 font.  Runs of zeroes are coded as 0
2281    followed by the repetition count. */
2282
2283 #ifndef UCLINUX
2284 uchar stdfont [ 1980 ] = {
2285 0,255,0,255,0,194,8,4,12,10,18,0,3,16,4,8,20,8,4,8,20,0,1,10,8,4,
2286 4,10,18,0,2,16,4,8,20,4,0,68,20,0,1,8,0,2,12,6,48,0,5,2,0,43,14,32,
2287 56,0,2,12,0,1,32,0,1,2,0,1,14,0,1,32,8,4,32,56,0,14,6,8,48,0,40,8,
2288 0,1,18,0,6,30,0,4,4,0,11,4,8,18,20,18,12,0,2,8,8,20,20,4,8,20,20,
2289 0,1,20,4,8,10,20,18,0,2,8,8,20,20,8,0,1,24,8,4,8,10,20,12,0,2,8,4,
2290 8,20,16,8,8,20,54,10,8,4,8,10,20,0,2,16,4,8,20,4,0,1,20,0,33,12,20,
2291 18,28,48,12,12,8,8,8,0,4,2,28,8,28,28,4,62,28,62,28,28,0,5,60,28,
2292 12,60,14,56,62,30,14,34,62,62,33,16,33,34,12,60,12,60,30,127,34,33,
2293 65,34,34,62,8,32,8,8,0,1,24,0,1,32,0,1,2,0,1,16,0,1,32,8,4,32,8,0,
2294 7,16,0,6,8,8,8,0,36,4,14,0,1,34,8,12,18,28,24,0,3,28,0,1,24,0,1,28,
2295 28,8,0,1,30,0,2,8,28,0,1,100,100,98,0,6,18,31,14,0,8,56,0,7,13,0,
2296 5,32,36,4,8,20,20,20,18,0,2,4,8,20,20,8,16,20,20,8,20,4,8,20,20,20,
2297 0,2,8,8,20,20,8,32,20,0,33,12,20,18,42,73,18,24,8,8,42,8,0,3,4,34,
2298 24,34,34,12,32,34,2,34,34,0,2,2,0,1,16,2,34,12,34,18,36,32,16,18,
2299 34,8,8,34,16,51,50,18,34,18,34,32,8,34,33,73,34,34,2,8,16,8,8,0,1,
2300 24,0,1,32,0,1,2,0,1,16,0,1,32,0,2,32,8,0,7,16,0,6,8,8,8,0,36,15,16,
2301 65,34,8,18,0,1,34,4,0,3,34,0,1,36,8,2,2,0,2,58,0,2,56,34,0,1,36,36,
2302 18,0,1,12,12,12,12,12,12,24,18,62,62,62,62,62,62,62,62,36,34,12,12,
2303 12,12,12,0,1,18,34,34,34,34,34,32,36,0,5,12,0,10,52,0,6,8,0,6,32,
2304 0,34,12,0,1,63,40,74,18,0,1,16,4,20,8,0,3,4,34,40,2,2,20,32,32,2,
2305 34,34,24,24,4,0,1,8,2,78,18,34,32,34,32,16,32,34,8,8,36,16,51,50,
2306 33,34,33,34,32,8,34,33,73,20,34,4,8,16,8,20,0,2,28,44,14,30,28,62,
2307 30,44,56,60,34,8,82,44,28,44,30,22,30,62,34,34,65,34,34,62,8,8,8,
2308 0,35,12,20,16,62,34,8,16,0,1,77,4,0,3,93,0,1,24,8,2,12,0,1,34,58,
2309 0,2,8,34,0,1,40,40,100,4,12,12,12,12,12,12,40,32,32,32,32,32,8,8,
2310 8,8,34,50,18,18,18,18,18,34,35,34,34,34,34,34,60,40,28,28,28,28,28,
2311 28,54,14,28,28,28,28,56,56,56,56,2,44,28,28,28,28,28,8,29,34,34,34,
2312 34,34,44,34,0,33,12,0,1,18,24,52,12,0,1,16,4,42,8,0,3,8,34,8,2,2,
2313 36,60,32,4,34,34,24,24,8,127,4,2,82,18,34,32,34,32,16,32,34,8,8,40,
2314 16,45,42,33,34,33,34,48,8,34,33,73,20,20,4,8,8,8,20,0,2,34,50,16,
2315 34,34,16,34,50,8,4,36,8,109,50,34,50,34,24,32,16,34,34,73,34,34,2,
2316 4,8,16,57,0,34,12,36,16,34,20,0,1,40,0,1,81,28,18,127,0,1,89,0,2,
2317 127,12,2,0,1,34,58,28,0,1,8,34,36,40,40,24,4,18,18,18,18,18,18,40,
2318 32,32,32,32,32,8,8,8,8,34,50,33,33,33,33,33,20,37,34,34,34,34,20,
2319 34,40,34,34,34,34,34,34,9,16,34,34,34,34,8,8,8,8,30,50,34,34,34,34,
2320 34,0,1,34,34,34,34,34,34,50,34,0,33,12,0,1,18,12,8,25,0,1,16,4,8,
2321 127,0,1,127,0,1,8,34,8,4,12,68,2,60,8,28,30,0,2,16,0,1,2,28,82,18,
2322 60,32,34,60,30,32,62,8,8,56,16,45,42,33,34,33,60,28,8,34,18,85,8,
2323 20,8,8,8,8,34,0,2,2,34,32,34,34,16,34,34,8,4,40,8,73,34,34,34,34,
2324 16,32,16,34,34,73,20,34,4,24,8,12,78,0,35,36,60,34,62,0,1,36,0,1,
2325 81,36,36,1,28,85,0,2,8,16,2,0,1,34,26,28,0,1,8,34,18,18,22,106,0,
2326 1,18,18,18,18,18,18,47,32,60,60,60,60,8,8,8,8,122,42,33,33,33,33,
2327 33,8,45,34,34,34,34,20,34,36,2,2,2,2,2,2,9,32,34,34,34,34,8,8,8,8,
2328 34,34,34,34,34,34,34,127,38,34,34,34,34,34,34,34,0,33,8,0,1,63,10,
2329 22,37,0,1,16,4,0,1,8,0,3,8,34,8,8,2,126,2,34,8,34,2,0,2,8,127,4,16,
2330 86,63,34,32,34,32,16,34,34,8,8,36,16,45,38,33,60,33,36,6,8,34,18,
2331 54,20,8,16,8,8,8,34,0,2,30,34,32,34,62,16,34,34,8,4,56,8,73,34,34,
2332 34,34,16,28,16,34,20,85,8,20,8,4,8,16,0,35,8,36,16,34,8,8,18,0,1,
2333 81,26,72,1,0,1,34,0,2,8,30,28,0,1,34,10,28,0,1,8,28,9,22,17,22,4,
2334 63,63,63,63,63,63,120,32,32,32,32,32,8,8,8,8,34,42,33,33,33,33,33,
2335 20,41,34,34,34,34,8,34,34,30,30,30,30,30,30,63,32,62,62,62,62,8,8,
2336 8,8,34,34,34,34,34,34,34,0,1,42,34,34,34,34,20,34,20,0,35,18,10,41,
2337 34,0,1,16,4,0,1,8,0,3,16,34,8,16,2,4,2,34,16,34,2,0,2,4,0,1,8,0,1,
2338 73,33,34,32,34,32,16,34,34,8,8,34,16,33,38,33,32,33,34,2,8,34,18,
2339 34,20,8,16,8,4,8,0,3,34,34,32,34,32,16,38,34,8,4,36,8,73,34,34,34,
2340 34,16,2,16,34,20,34,20,20,16,8,8,8,0,35,12,20,16,62,62,8,10,0,1,77,
2341 0,1,36,1,0,1,28,0,6,34,10,0,4,18,42,34,42,28,33,33,33,33,33,33,72,
2342 32,32,32,32,32,8,8,8,8,34,38,33,33,33,33,33,34,49,34,34,34,34,8,60,
2343 34,34,34,34,34,34,34,72,32,32,32,32,32,8,8,8,8,34,34,34,34,34,34,
2344 34,8,50,34,34,34,34,20,34,20,0,33,12,0,1,18,42,73,34,0,1,8,8,0,1,
2345 8,12,0,1,24,16,34,8,32,34,4,34,34,16,34,34,24,24,2,0,1,16,16,32,33,
2346 34,16,36,32,16,18,34,8,8,33,16,33,34,18,32,18,34,2,8,34,12,34,34,
2347 8,32,8,4,8,0,3,34,34,16,38,34,16,26,34,8,4,34,8,73,34,34,34,38,16,
2348 2,16,38,8,34,34,8,32,8,8,8,0,35,12,15,16,65,8,8,4,0,1,34,0,1,18,0,
2349 5,127,0,3,54,10,0,4,36,79,68,79,32,33,33,33,33,33,33,72,16,32,32,
2350 32,32,8,8,8,8,36,38,18,18,18,18,18,0,1,18,34,34,34,34,8,32,34,34,
2351 34,34,34,34,34,72,16,34,34,34,34,8,8,8,8,34,34,34,34,34,34,34,8,34,
2352 38,38,38,38,8,34,8,0,33,12,0,1,18,28,6,29,0,1,8,8,0,2,12,0,1,24,32,
2353 28,8,62,28,4,28,28,16,28,28,24,24,0,3,16,28,33,60,14,56,62,16,14,
2354 34,62,112,33,30,33,34,12,32,12,34,60,8,28,12,34,34,8,62,8,2,8,0,3,
2355 29,60,14,26,28,16,2,34,8,4,33,8,73,34,28,60,26,16,60,14,26,8,34,34,
2356 8,62,8,8,8,0,35,12,4,62,0,1,8,8,36,0,1,28,0,11,42,10,0,5,66,71,66,
2357 32,33,33,33,33,33,33,79,14,62,62,62,62,62,62,62,62,56,34,12,12,12,
2358 12,12,0,1,44,28,28,28,28,8,32,36,29,29,29,29,29,29,55,14,28,28,28,
2359 28,8,8,8,8,28,34,28,28,28,28,28,0,1,92,26,26,26,26,8,60,8,0,36,8,
2360 0,3,6,48,0,2,24,0,2,32,0,11,48,0,21,6,0,9,14,2,56,0,1,127,0,7,2,0,
2361 2,4,0,5,32,2,0,7,16,0,1,6,8,48,0,35,12,0,4,8,24,0,13,32,10,0,1,4,
2362 0,6,32,0,7,4,0,31,4,0,21,16,32,16,0,81,3,0,21,28,0,2,56,0,5,32,2,
2363 0,7,48,0,39,12,0,19,32,0,2,24,0,6,30,0,7,24,0,31,24,0,21,48,32,48,
2364 0,255,0,1
2365 } ;
2366 #endif /* UCLINUX */