📄 efaxlib.c.orig
字号:
" pop buf length \n" " } { \n" " currentfile buf 3 index 3 index getinterval readhexstring pop pop\n" " } ifelse \n" " } { \n" " dup 255 eq { \n" " pop getval getval %% => index run_length value \n" " 2 index 1 3 index 2 index add 1 sub %% => ... start 1 end \n" " { buf exch 2 index put } for \n" " pop \n" " } { \n" " 127 sub \n" " } ifelse \n" " } ifelse \n" " add %% => index \n" " } loop \n" " pop \n" " buf \n" "} bind def \n" "%%%%EndProlog \n" ;const char PSPAGE [] = /* start of page */ "%%%%Page: %d %d \n" "gsave \n" "%f %f translate \n" "%f %f scale \n" "%d %d %d [ %d %d %d %d %d %d ] { readbuf } image \n" ;const char PSPAGEEND [] = /* end of page */ "\n" "grestore \n" "showpage \n" ;const char PSEND [] = /* end of file */ "%%Trailer \n" "%%%%Pages: %d \n" ;void psinit ( OFILE *f, int newfile, int page, int w, int h, int n ){ float ptw, pth ; if ( ! f ) { msg ( "E2 can't happen (psinit)" ) ; return ; } ptw = w/f->xres * 72.0 ; /* convert to points */ pth = h/f->yres * 72.0 ; if ( newfile ) fprintf ( f->f, PSBEGIN, (int) ptw, (int) pth, /* Bounding Box */ n ) ; /* buffer string length */ fprintf ( f->f, PSPAGE, page, page, /* page number */ 0.0, 0.0, /* shift */ ptw, pth, /* scaling */ w, h, 1, /* image size */ w, 0, 0, -h, 0, h ) ; /* CTM */ f->pslines = 0 ; f->lastpageno = page ;}char nhexout = 0, hexchars [ 16 ] = "0123456789abcdef" ;#define hexputc( f, c ) ( \ putc ( hexchars [ (c) >> 4 ], f ), \ putc ( hexchars [ (c) & 0x0f ], f ), \ ( ( ( nhexout++ & 31 ) == 31 ) ? putc ( '\n', f ) : 0 ) )void hexputs ( FILE *f, uchar *p, int n ){ uchar 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 ; if ( ! f || ! buf || n<0 ) { msg ( "E2 can't happen (pswrite)" ) ; return ; } for ( j=0 ; j<n && buf[j]==last[j] && f->pslines ; 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->pslines ; 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->pslines++ ;}/* Write 2- and 4-byte integers to an image output file. Return as for fwrite. */int fwrite2 ( short s, OFILE *f ){ uchar *p = (void*) &s ; return fwrite ( bigendian ? p + sizeof(short) - 2 : p, 2, 1, f->f ) ;}int fwrite4 ( long l, OFILE *f ){ uchar *p = (void*) &l ; return fwrite ( bigendian ? p + sizeof(long ) - 4 : p, 4, 1, f->f ) ;}/* Write a TIFF directory tag. Returns 0 if OK, 1 on errors. */int wtag ( OFILE *f, int lng, short tag, short type, long count, long offset ){ int err=0 ; err = err || ! fwrite2 ( tag, f ) ; err = err || ! fwrite2 ( type, f ) ; err = err || ! fwrite4 ( count, f ) ; if ( lng ) { err = err || ! fwrite4 ( offset, f ) ; } else { err = err || ! fwrite2 ( offset, f ) ; err = err || ! fwrite2 ( 0, f ) ; } if ( err ) msg ( "ES2 can't write TIFF tag" ) ; return err ;}/* Write TIFF header and directory. File format based on Sam Leffler's tiff.h. Can only be used for single-image TIFFs because always seeks to start of file to re-write the header. */#define NTAGS 17 /* number of tags in directory */#define NRATIO 2 /* number of floats (as ratios) */int tiffinit ( OFILE *f ){ int err=0, compr=1 ; long tdoff, doff ; fseek ( f->f, 0, SEEK_SET ) ; /* 0 ==> (start of TIFF file) */ /* write magic, TIFF version and offset to directory */ fwrite2 ( bigendian ? 0x4d4d : 0x4949, f ) ; fwrite2 ( 42, f ) ; fwrite4 ( 8, f ) ; /* 8 ==> directory */ fwrite2 ( NTAGS, f ) ; /* figure out offsets within file and compression code */ tdoff = 8 + 2 + NTAGS*12 + 4 ; /* offset to directory data */ doff = tdoff + NRATIO*8 ; /* offset to image data */ switch ( f->format ) { case O_TIFF_RAW: compr = 1 ; break ; case O_TIFF_FAX: compr = 3 ; break ; default: err = msg ( "E2can't happen(tiffinit)" ) ; break ; } /* write directory tags, 12 bytes each */ wtag( f, 1, 256, 4, 1, f->w ) ; /* width long */ wtag( f, 1, 257, 4, 1, f->h ) ; /* length long */ wtag( f, 0, 258, 3, 1, 1 ) ; /* bits/sample short */ wtag( f, 0, 259, 3, 1, compr ) ; /* compresssion(g3=3) short */ wtag( f, 0, 262, 3, 1, 0 ) ; /* photometric(0-min=white) short */ wtag( f, 0, 266, 3, 1, 1 ) ; /* fill order(msb2lsb=1) short */ wtag( f, 1, 273, 4, 1, doff ) ; /* strip offsets long */ wtag( f, 0, 274, 3, 1, 1 ) ; /* orientation(1=normal) short */ wtag( f, 0, 277, 3, 1, 1 ) ; /* samples/pixel short */ wtag( f, 1, 278, 4, 1, f->h ) ; /* rows/strip long */ wtag( f, 1, 279, 4, 1, f->bytes ) ; /* strip byte counts long */ wtag( f, 1, 282, 5, 1, tdoff+0 ) ; /* xresolution ratio */ wtag( f, 1, 283, 5, 1, tdoff+8 ) ; /* yresolution ratio */ wtag( f, 0, 284, 3, 1, 1 ) ; /* storage(1=single plane) short */ wtag( f, 1, 292, 4, 1, 0 ) ; /* g3options long */ wtag( f, 0, 296, 3, 1, 2 ) ; /* resolution units(2=in,3=cm) short */ wtag( f, 0, 327, 3, 1, 0 ) ; /* clean fax(0=clean) short */ fwrite4 ( 0, f ) ; /* offset to next dir (no more) */ /* ==> tdoff (tag data offset), write ratios for floats here */ fwrite4 ( f->xres+0.5, f ) ; fwrite4 ( 1, f ) ; fwrite4 ( f->yres+0.5, f ) ; fwrite4 ( 1, f ) ; /* ==> doff (strip data offset), image data goes here */ return err ;}/* Convert array 'runs' of 'nr' run lengths into a bit map 'buf'. Returns the number of bytes filled. */int runtobit ( short *runs, int nr, uchar *buf ){ static uchar zerofill [ 9 ] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 } ; static uchar onefill [ 9 ] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff } ; uchar 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 ) ;}/* Write a PCX file header. */int fputi ( int i, OFILE *f ){ putc ( i & 0xff, f->f ) ; putc ( ( i >> 8 ) & 0xff, f->f ) ; return 0 ;}void pcxinit ( OFILE *f ){ uchar buf [ 60 ] = { 0x0a, 3, 1, 1 } ; /* magic, version, compr, BPP */ fwrite ( buf, 1, 4, f->f ) ; /* 4 */ fputi ( 0, f ) ; /* 8 xmin, ymin, xmax, ymax */ fputi ( 0, f ) ; fputi ( f->w-1, f ) ; fputi ( f->h-1, f ) ; fputi ( f->xres, f ) ; /* 4 x and y dpi */ fputi ( f->yres, f ) ; memset ( buf, 0, 48 ) ; /* 48 palette */ fwrite ( buf, 1, 48, f->f ) ; putc ( 0, f->f ) ; /* 1 reserved */ putc ( 1, f->f ) ; /* 1 planes per pixel */ fputi ( (f->w+15)/16*2, f ) ; /* 2 bytes per line */ memset ( buf, 0, 60 ) ; /* 60 zero */ fwrite ( buf, 1, 60, f->f ) ;}/* Write a PCX-compressed scan line. */void pcxwrite ( OFILE *of, uchar *p, int nb ){ int c, n, runc ; FILE *f = of->f ; runc = *p++ ; n = 1 ; for ( nb-- ; nb > 0 ; nb-- ) { c = *p++ ; if ( c == runc && n < 63 ) { /* continue run */ n++ ; } else { /* terminate run */ if ( n > 1 || ( ( runc & 0xc0 ) == 0xc0 ) ) /* output as run */ putc ( n | 0xc0, f ) ; putc ( runc, f ) ; runc = c ; /* start new run */ n = 1 ; } } /* last run */ if ( n > 1 || ( ( runc & 0xc0 ) == 0xc0 ) ) /* output as run */ putc ( n | 0xc0, f ) ; putc ( runc, f ) ;}/* 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==EOF), writes file header. Returns 0 or 2 on errors. */int nextopage ( OFILE *f, int page ){ int err = 0 ; int i, nb=0 ; uchar *p, codes [ ( RTCEOL * EOLBITS ) / 8 + 3 ] ; if ( f->f ) { /* terminate previous page */ switch ( f->format ) { case O_PBM: break ; case O_PGM: break ; case O_FAX: case O_TIFF_FAX: for ( p = codes, i=0 ; i<RTCEOL ; i++ ) p = putcode ( &f->e, EOLCODE, EOLBITS, p ) ; nb = putcode ( &f->e, 0, 0, p ) - codes ; fwrite ( codes, 1, nb, f->f ) ; f->bytes += nb ; if ( f->format == O_TIFF_FAX ) tiffinit ( f ) ; break ; case O_TIFF_RAW: tiffinit(f) ; /* rewind & update TIFF header */ break ; case O_PCL: fprintf ( f->f, PCLEND ) ; break ; case O_PS: fprintf ( f->f, PSPAGEEND ) ; if ( f->fname || page<0 ) fprintf ( f->f, PSEND, f->lastpageno ) ; break ; case O_PCX: case O_PCX_RAW: fseek ( f->f, 0, SEEK_SET ) ; pcxinit ( f ) ; break ; } if ( ferror ( f->f ) ) { err = msg ("ES2output error:" ) ; } else { msg ( "F+ wrote %s as %dx%d pixel %.fx%.f dpi %s page", f->cfname, f->w, f->h, f->xres, f->yres, oformatname [f->format] ) ; switch ( f->format ) { case O_PS: msg ( "F (%d lines)", f->pslines ) ; break ; case O_TIFF_RAW: case O_TIFF_FAX: msg ( "F (%d bytes)", f->bytes ) ; break ; default: msg ( "F " ) ; break ; } } } if ( ! err && page >= 0 ) { /* open new file */ if ( f->fname ) { sprintf ( f->cfname, f->fname, page+1, page+1, page+1 ) ; if ( ! f->f ) f->f = fopen ( f->cfname, ( f->format == O_PS ) ? "w" : "wb+" ) ; else f->f = freopen ( f->cfname, ( f->format == O_PS ) ? "w" : "wb+", f->f ) ; if ( ! f->f ) { err = msg ("ES2can't open output file %s:", f->cfname ) ; } } else { f->f = stdout ; strcpy ( f->cfname, "standard output" ) ; } } /* start new page */ if ( ! err && page >= 0 ) { switch ( f->format ) { case O_PBM: fprintf ( f->f, "P4 %d %d\n", f->w, f->h ) ; break ; case O_PGM: fprintf ( f->f, "P5 %d %d %d\n", f->w/4, f->h/4, 255 ) ; break ; case O_FAX: case O_TIFF_FAX: if ( f->format == O_TIFF_FAX ) tiffinit ( f ) ; p = putcode ( &f->e, EOLCODE, EOLBITS, codes ) ; nb = p - codes ; fwrite ( codes, 1, nb, f->f ) ; break ; case O_TIFF_RAW: tiffinit ( f ) ; break ; case O_PCL: fprintf ( f->f, PCLBEGIN, (int) f->xres ) ; break ; case O_PS: psinit ( f, ( f->fname || page==0 ), page+1, f->w, f->h, f->w/8 ) ; break ; case O_PCX:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -