📄 efaxlib.c
字号:
u_char c ; if ( n > 0 ) { hexputc ( f, n ) ; while ( n-- ) { c = *p++ ^ 0xff ; hexputc ( f, c ) ; } }}/* Encode into postscript. If not a repeated line, test (using index j) from current position (i) for possible encodings as: copy of > 2 bytes, runs of > 4 or data >=127. Otherwise the byte is skipped. Uncoded bytes are output from the last uncoded byte (l) before output of runs/copies. */void pswrite ( OFILE *f, unsigned char *buf, int n ){ int i, j, l ; static unsigned char last [ MAXBITS ] ; l=i=0 ; for ( j=0 ; j<n && buf[j]==last[j] && ! f->firstpsline ; j++ ) ; if ( j == n ) { /* repeat line */ hexputc ( f->f, 0 ) ; l=i=n ; } while ( i<n ) { for ( j=i ; j<n && buf[j]==last[j] && j-i<127 && ! f->firstpsline ; j++ ) ; if ( j-i > 2 ) { /* skip */ hexputs ( f->f, buf+l , i-l ) ; hexputc ( f->f, j-i + 127 ) ; l=i=j ; } else { for ( j=i ; j<n && buf[j]==buf[i] && j-i<255 ; j++ ) ; if ( j-i > 4 ) { /* run */ hexputs ( f->f, buf+l , i-l ) ; hexputc ( f->f, 255 ) ; hexputc ( f->f, j-i ) ; hexputc ( f->f, buf[i] ^ 0xff ) ; l=i=j ; } else { if ( i-l >= 127 ) { /* maximum data length */ hexputs ( f->f, buf+l, i-l ) ; l=i ; } else { /* data */ i++ ; } } } } hexputs ( f->f, buf+l, i-l ) ; if ( n >= 0 ) memcpy ( last, buf, n ) ; f->firstpsline = 0 ;}/* Convert array 'runs' of 'nr' run lengths into a bit map 'buf'. Returns the number of bytes filled. */int runtobit ( short *runs, int nr, u_char *buf ){ static u_char zerofill [ 9 ] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 } ; static u_char onefill [ 9 ] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff } ; u_char col=0, *buf0 = buf ; register short len, b=8, bytes ; while ( nr-- > 0 ) { len = *runs++ ; if ( col ) *buf |= onefill [ b ] ; /* right bits of cur. byte */ else *buf &= zerofill [ b ] ; if ( b > len ) { /* done fill */ b -= len ; } else { /* continue to next byte */ len -= b ; buf++ ; b = 8 ; if ( ( bytes = len>>3 ) > 0 ) { /* fill >1 byte */ memset ( buf, col, bytes ) ; len -= bytes*8; buf += bytes ; } *buf = col ; /* flood the rest */ b -= len ; } col ^= 0xff ; } return buf - buf0 + ( b < 8 ) ;}/* Begin/end output pages. If not starting first page (0), terminate previous page. If output filename pattern is defined, [re-]opens that file. If not terminating last page (page==-1), writes file header. Returns 0 or 2 on errors. */int nextopage ( OFILE *f, int page ){ int err = 0 ; int i ; u_char *p, codes [ ( RTCEOL * EOLBITS ) / 8 + 3 ] ; if ( page ) /* terminate previous page */ switch ( f->format ) { case PBM: break ; case PGM: break ; case FAX: for ( p = codes, i=0 ; i<RTCEOL ; i++ ) p = putcode ( &f->e, EOLCODE, EOLBITS, p ) ; fwrite ( codes, 1, putcode ( &f->e, 0, 0, p ) - codes, f->f ) ; break ; case PCL: fprintf ( f->f, PCLEND ) ; break ; case PS: fprintf ( f->f, PSPAGEEND ) ; if ( f->fname || page<0 ) fprintf ( f->f, PSEND, f->lastpageno ) ; break ; } if ( ferror ( f->f ) ) err = msg ("ES2output error:" ) ; if ( ! err && f->fname && page >= 0 ) { /* start new file */ sprintf ( f->cfname, f->fname, page+1, page+1, page+1 ) ; if ( ! freopen ( f->cfname, ( f->format == PS ) ? "w" : "wb", f->f ) ) err = msg ("ES2unable to open output file\n%s:", f->cfname ) ; } if ( ! err && page >= 0 ) { /* start new page */ switch ( f->format ) { case PBM: fprintf ( f->f, "P4 %d %d\n", f->w , f->h ) ; break ; case PGM: fprintf ( f->f, "P5 %d %d %d\n", f->w/4, f->h/4, 255 ) ; break ; case FAX: p = putcode ( &f->e, EOLCODE, EOLBITS, codes ) ; fwrite ( codes, 1, p - codes, f->f ) ; break ; case PCL: fprintf ( f->f, PCLBEGIN, (int) f->xres ) ; break ; case PS: psinit ( f, ( f->fname || page==0 ), page+1, f->w, f->h, f->w/8 ) ; break ; } } if ( ! err && ferror ( f->f ) ) err = msg ("ES2output error:" ) ; return err ;}/* Output scan line of nr runs no times to output file f. */void writeline ( OFILE *f, short *runs, int nr, int no ){ int nb = 0 ; u_char *p, buf [ MAXCODES ] ; /* if line to be output, convert to right format */ if ( no > 0 ) switch ( f->format ) { case PBM: case PGM: case PCL: case PS: nb = runtobit ( runs, nr, buf ) ; break ; case FAX: break ; } /* output `no' times. */ while ( no-- > 0 ) switch ( f->format ) { case PBM: fwrite ( buf, 1, nb, f->f ) ; break ; case PGM: pgmwrite ( f, buf, nb ) ; break ; case FAX: p = runtocode ( &f->e, runs, nr, buf ) ; p = putcode ( &f->e, EOLCODE, EOLBITS, p ) ; fwrite ( buf, 1, p - buf, f->f ) ; break ; case PCL: pclwrite ( f, buf, nb ) ; break ; case PS: pswrite ( f, buf, nb ) ; break ; }}/* Initialize new output file. If fname is NULL, stdout will be used for all images. */void newOFILE ( OFILE *f, int format, char *fname, float xres, float yres, int w, int h ){ f->f = stdout ; f->format = format ; f->fname = fname ; f->xres = xres ; f->yres = yres ; f->w = w ; f->h = h ; newENCODER ( &(f->e) ) ;} /* T.4 Encoding/Decoding *//* Table-lookup decoder for variable-bit-length codewords. The table index is the N most recently undecoded bits with the first (oldest) undecoded bit as the MS bit. If the N bits uniquely identify a codeword then the indexed 'code' member identifies the code, otherwise it is zero. The 'bits' member gives the number of bits to be considered decoded (to be removed from the bit stream) and the 'next' element is a pointer to the table to use for decoding the next part of the bit sequence. For T.4 decoding the longest T.4 codeword is 13 bits. The implementation below uses two tables of 512 elements (N=9 bits) for each colour. Codewords longer than 9 bits require a second lookup. Since all codewords longer than than 9 bits have a 4-bit zero prefix it is possible to use only one secondary 9-bit lookup table by dropping only the first 4 bits after the first lookup. The code indentifier is the run length + 1. A separate table is used for decoding the variable-length FILL patterns. For undefined codewords, one bit is skipped and decoding continues at the white code table. *//* the lookup tables for each colour and the fill lookup table */dtab tw1 [ 512 ], tw2 [ 512 ], tb1 [ 512 ], tb2 [ 512 ], fill [ 512 ] ;char tinit=0 ;/* Add code cword shifted left by shift to decoding table tab. */void addcode ( dtab *tab, int cword, int shift, short code, short bits, dtab *next ){ int i, n = 1 << shift ; for ( i = cword << shift ; n-- > 0 ; i++ ) { tab[i].code = code ; tab[i].bits = bits ; tab[i].next = next ; }}/* Initialize the decoding table for one colour using the codes in the T.4 table p0. t1 and t2 are the two decoding tables and ot is the first table of the other colour. */void init1dtab ( t4tab *p0, dtab *t1, dtab *t2, dtab *ot ){ t4tab *p ; for ( p = p0 ; p->code ; p++ ) if ( p->bits <= 9 ) { addcode ( t1, p->code, 9 - p->bits, p->rlen + 1, p->bits, ( p - p0 ) > 63 ? t1 : ot ) ; } else { addcode ( t1, p->code >> ( p->bits - 9 ), 0, 0, 4, t2 ) ; addcode ( t2, p->code, 13 - p->bits, p->rlen + 1, p->bits - 4, ( p - p0 ) > 63 ? t1 : ot ) ; }}/* Initialize a T.4 decoder. First sets all */void newDECODER ( DECODER *d ){ int i ; if ( ! tinit ) { /* undefined codes */ addcode ( tw1, 0, 9, 0, 1, tw1 ) ; addcode ( tw2, 0, 9, 0, 1, tw1 ) ; addcode ( tb1, 0, 9, 0, 1, tw1 ) ; addcode ( tb2, 0, 9, 0, 1, tw1 ) ; addcode ( fill, 0, 9, 0, 1, tw1 ) ; /* fill and EOL */ addcode ( tw1, 0, 0, 0, 4, tw2 ) ; addcode ( tw2, 0, 2, 0, 7, fill ) ; addcode ( tb1, 0, 0, 0, 4, tb2 ) ; addcode ( tb2, 0, 2, 0, 7, fill ) ; addcode ( fill, 0, 0, 0, 9, fill ) ; for ( i=0 ; i<=8 ; i++ ) addcode ( fill, 1, i, -1, 9-i, tw1 ) ; /* white and black runs */ init1dtab ( wtab, tw1, tw2, tb1 ) ; init1dtab ( btab, tb1, tb2, tw1 ) ; tinit=1 ; } /* initialize decoder to starting state */ d->x = 0 ; d->shift = -9 ; d->tab = tw1 ; d->eolcnt = 0 ;} /* T.4 coding table and default font for efax/efix *//* T.4 1-D run-length coding tables. codes must be in run length order for runtocode(). */t4tab wtab [ ( 64 + 27 + 13 ) + 1 ] = { /* runs of white *//* Terminating White Codes */{53,8,0}, {7,6,1}, {7,4,2}, {8,4,3}, {11,4,4}, {12,4,5},{14,4,6}, {15,4,7}, {19,5,8}, {20,5,9}, {7,5,10}, {8,5,11},{8,6,12}, {3,6,13}, {52,6,14}, {53,6,15}, {42,6,16}, {43,6,17},{39,7,18}, {12,7,19}, {8,7,20}, {23,7,21}, {3,7,22}, {4,7,23},{40,7,24}, {43,7,25}, {19,7,26}, {36,7,27}, {24,7,28}, {2,8,29},{3,8,30}, {26,8,31}, {27,8,32}, {18,8,33}, {19,8,34}, {20,8,35},{21,8,36}, {22,8,37}, {23,8,38}, {40,8,39}, {41,8,40}, {42,8,41},{43,8,42}, {44,8,43}, {45,8,44}, {4,8,45}, {5,8,46}, {10,8,47},{11,8,48}, {82,8,49}, {83,8,50}, {84,8,51}, {85,8,52}, {36,8,53},{37,8,54}, {88,8,55}, {89,8,56}, {90,8,57}, {91,8,58}, {74,8,59},{75,8,60}, {50,8,61}, {51,8,62}, {52,8,63}, /* Make Up White Codes */{27,5,64}, {18,5,128}, {23,6,192}, {55,7,256}, {54,8,320}, {55,8,384},{100,8,448}, {101,8,512}, {104,8,576}, {103,8,640}, {204,9,704}, {205,9,768},{210,9,832}, {211,9,896}, {212,9,960}, {213,9,1024},{214,9,1088},{215,9,1152},{216,9,1216},{217,9,1280},{218,9,1344},{219,9,1408},{152,9,1472},{153,9,1536},{154,9,1600},{24,6,1664}, {155,9,1728},/* Extended Make Up Codes (Black and White) */{8,11,1792}, {12,11,1856},{13,11,1920},{18,12,1984},{19,12,2048},{20,12,2112},{21,12,2176},{22,12,2240},{23,12,2304},{28,12,2368},{29,12,2432},{30,12,2496},{31,12,2560},{0,0,0} } ;t4tab btab [ ( 64 + 27 + 13 ) + 1 ] = { /* runs of black *//* Terminating Black Codes */{55,10,0}, {2,3,1}, {3,2,2}, {2,2,3}, {3,3,4}, {3,4,5},{2,4,6}, {3,5,7}, {5,6,8}, {4,6,9}, {4,7,10}, {5,7,11},{7,7,12}, {4,8,13}, {7,8,14}, {24,9,15}, {23,10,16}, {24,10,17},{8,10,18}, {103,11,19}, {104,11,20}, {108,11,21}, {55,11,22}, {40,11,23},{23,11,24}, {24,11,25}, {202,12,26}, {203,12,27}, {204,12,28}, {205,12,29},{104,12,30}, {105,12,31}, {106,12,32}, {107,12,33}, {210,12,34}, {211,12,35},{212,12,36}, {213,12,37}, {214,12,38}, {215,12,39}, {108,12,40}, {109,12,41},{218,12,42}, {219,12,43}, {84,12,44}, {85,12,45}, {86,12,46}, {87,12,47},{100,12,48}, {101,12,49}, {82,12,50}, {83,12,51}, {36,12,52}, {55,12,53},{56,12,54}, {39,12,55}, {40,12,56}, {88,12,57}, {89,12,58}, {43,12,59},{44,12,60}, {90,12,61}, {102,12,62}, {103,12,63}, /* Make Up Black Codes */{15,10,64}, {200,12,128},{201,12,192},{91,12,256}, {51,12,320}, {52,12,384},{53,12,448}, {108,13,512},{109,13,576},{74,13,640}, {75,13,704}, {76,13,768},{77,13,832}, {114,13,896},{115,13,960},{116,13,1024},{117,13,1088},{118,13,1152},{119,13,1216},{82,13,1280},{83,13,1344},{84,13,1408},{85,13,1472},{90,13,1536},{91,13,1600},{100,13,1664},{101,13,1728},/* Extended Make Up Codes (Black and White) */{8,11,1792}, {12,11,1856},{13,11,1920},{18,12,1984},{19,12,2048},{20,12,2112},{21,12,2176},{22,12,2240},{23,12,2304},{28,12,2368},{29,12,2432},{30,12,2496},{31,12,2560},{0,0,0} } ;/* The built-in 8x16 font. Runs of zeroes are coded as 0 followed by the repetition count. */u_char stdfont [ 1980 ] = {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,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,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,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,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,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,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,12,60,14,56,62,30,14,34,62,62,33,16,33,34,12,60,12,60,30,127,34,33,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,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,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,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,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,24,34,34,12,32,34,2,34,34,0,2,2,0,1,16,2,34,12,34,18,36,32,16,18,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,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,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,18,0,1,12,12,12,12,12,12,24,18,62,62,62,62,62,62,62,62,36,34,12,12,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,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,34,34,24,24,4,0,1,8,2,78,18,34,32,34,32,16,32,34,8,8,36,16,51,50,33,34,33,34,32,8,34,33,73,20,34,4,8,16,8,20,0,2,28,44,14,30,28,62,30,44,56,60,34,8,82,44,28,44,30,22,30,62,34,34,65,34,34,62,8,8,8,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,0,2,8,34,0,1,40,40,100,4,12,12,12,12,12,12,40,32,32,32,32,32,8,8,8,8,34,50,18,18,18,18,18,34,35,34,34,34,34,34,60,40,28,28,28,28,28,28,54,14,28,28,28,28,56,56,56,56,2,44,28,28,28,28,28,8,29,34,34,34,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,36,60,32,4,34,34,24,24,8,127,4,2,82,18,34,32,34,32,16,32,34,8,8,40,16,45,42,33,34,33,34,48,8,34,33,73,20,20,4,8,8,8,20,0,2,34,50,16,34,34,16,34,50,8,4,36,8,109,50,34,50,34,24,32,16,34,34,73,34,34,2,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,127,12,2,0,1,34,58,28,0,1,8,34,36,40,40,24,4,18,18,18,18,18,18,40,32,32,32,32,32,8,8,8,8,34,50,33,33,33,33,33,20,37,34,34,34,34,20,34,40,34,34,34,34,34,34,9,16,34,34,34,34,8,8,8,8,30,50,34,34,34,34,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,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,60,32,34,60,30,32,62,8,8,56,16,45,42,33,34,33,60,28,8,34,18,85,8,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,16,32,16,34,34,73,20,34,4,24,8,12,78,0,35,36,60,34,62,0,1,36,0,1,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,1,18,18,18,18,18,18,47,32,60,60,60,60,8,8,8,8,122,42,33,33,33,33,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,34,34,34,34,34,34,34,127,38,34,34,34,34,34,34,34,0,33,8,0,1,63,10,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,86,63,34,32,34,32,16,34,34,8,8,36,16,45,38,33,60,33,36,6,8,34,18,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,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,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,63,63,63,63,63,63,120,32,32,32,32,32,8,8,8,8,34,42,33,33,33,33,33,20,41,34,34,34,34,8,34,34,30,30,30,30,30,30,63,32,62,62,62,62,8,8,8,8,34,34,34,34,34,34,34,0,1,42,34,34,34,34,20,34,20,0,35,18,10,41,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,73,33,34,32,34,32,16,34,34,8,8,34,16,33,38,33,32,33,34,2,8,34,18,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,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,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,32,32,32,32,32,8,8,8,8,34,38,33,33,33,33,33,34,49,34,34,34,34,8,60,34,34,34,34,34,34,34,72,32,32,32,32,32,8,8,8,8,34,34,34,34,34,34,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,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,34,16,36,32,16,18,34,8,8,33,16,33,34,18,32,18,34,2,8,34,12,34,34,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,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,5,127,0,3,54,10,0,4,36,79,68,79,32,33,33,33,33,33,33,72,16,32,32,32,32,8,8,8,8,36,38,18,18,18,18,18,0,1,18,34,34,34,34,8,32,34,34,34,34,34,34,34,72,16,34,34,34,34,8,8,8,8,34,34,34,34,34,34,34,8,34,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,28,8,62,28,4,28,28,16,28,28,24,24,0,3,16,28,33,60,14,56,62,16,14,34,62,112,33,30,33,34,12,32,12,34,60,8,28,12,34,34,8,62,8,2,8,0,3,29,60,14,26,28,16,2,34,8,4,33,8,73,34,28,60,26,16,60,14,26,8,34,34,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,32,33,33,33,33,33,33,79,14,62,62,62,62,62,62,62,62,56,34,12,12,12,12,12,0,1,44,28,28,28,28,8,32,36,29,29,29,29,29,29,55,14,28,28,28,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,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,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,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,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,0,255,0,1} ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -