📄 tiff2ps.c
字号:
#undef CVT } for (row = 0; row < h; row++) { if (TIFFReadScanline(tif, tf_buf, row, 0) < 0) break; for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) { DOBREAK(breaklen, nc, fd); switch (bitspersample) { case 8: c = *cp++; PUTRGBHEX(c, fd); break; case 4: c = *cp++; PUTRGBHEX(c&0xf, fd); c >>= 4; PUTRGBHEX(c, fd); break; case 2: c = *cp++; PUTRGBHEX(c&0x3, fd); c >>= 2; PUTRGBHEX(c&0x3, fd); c >>= 2; PUTRGBHEX(c&0x3, fd); c >>= 2; PUTRGBHEX(c, fd); break; case 1: c = *cp++; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c&0x1, fd); c >>= 1; PUTRGBHEX(c, fd); break; } } } _TIFFfree((char *) tf_buf);}voidPSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h){ int breaklen = MAXLINE; unsigned char* tf_buf; unsigned char* cp; tsize_t stripsize = TIFFStripSize(tif); tstrip_t s;#if defined( EXP_ASCII85ENCODER ) int ascii85_l; /* Length, in bytes, of ascii85_p[] data */ uint8 *ascii85_p = 0; /* Holds ASCII85 encoded data */#endif (void) w; (void) h; tf_buf = (unsigned char *) _TIFFmalloc(stripsize); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; }#if defined( EXP_ASCII85ENCODER ) if ( ascii85 ) { /* * Allocate a buffer to hold the ASCII85 encoded data. Note * that it is allocated with sufficient room to hold the * encoded data (5*stripsize/4) plus the EOD marker (+8) * and formatting line breaks. The line breaks are more * than taken care of by using 6*stripsize/4 rather than * 5*stripsize/4. */ ascii85_p = _TIFFmalloc( (stripsize+(stripsize/2)) + 8 ); if ( !ascii85_p ) { _TIFFfree( tf_buf ); TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); return; } }#endif if (ascii85) Ascii85Init(); for (s = 0; s < TIFFNumberOfStrips(tif); s++) { int cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize); if (cc < 0) { TIFFError(filename, "Can't read strip"); break; } cp = tf_buf; if (photometric == PHOTOMETRIC_MINISWHITE) { for (cp += cc; --cp >= tf_buf;) *cp = ~*cp; cp++; } if (ascii85) {#if defined( EXP_ASCII85ENCODER ) if (alpha) { int adjust, i; for (i = 0; i < cc; i+=2) { adjust = 255 - cp[i + 1]; cp[i / 2] = cp[i] + adjust; } cc /= 2; } ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, cp, cc ); if ( ascii85_l > 0 ) fwrite( ascii85_p, ascii85_l, 1, fd );#else while (cc-- > 0) Ascii85Put(*cp++, fd);#endif /* EXP_ASCII85_ENCODER */ } else { unsigned char c; if (alpha) { int adjust; while (cc-- > 0) { DOBREAK(breaklen, 1, fd); /* * For images with alpha, matte against * a white background; i.e. * Cback * (1 - Aimage) * where Cback = 1. */ adjust = 255 - cp[1]; c = *cp++ + adjust; PUTHEX(c,fd); cp++, cc--; } } else { while (cc-- > 0) { c = *cp++; DOBREAK(breaklen, 1, fd); PUTHEX(c, fd); } } } } if ( !ascii85 ) { if ( level2 || level3) fputs(">\n", fd); }#if !defined( EXP_ASCII85ENCODER ) else Ascii85Flush(fd);#else if ( ascii85_p ) _TIFFfree( ascii85_p );#endif _TIFFfree(tf_buf);}voidPSRawDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h){ uint32 *bc; uint32 bufsize; int breaklen = MAXLINE, cc; uint16 fillorder; unsigned char *tf_buf; unsigned char *cp, c; tstrip_t s;#if defined( EXP_ASCII85ENCODER ) int ascii85_l; /* Length, in bytes, of ascii85_p[] data */ uint8 * ascii85_p = 0; /* Holds ASCII85 encoded data */#endif (void) w; (void) h; TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc); /* * Find largest strip: */ bufsize = bc[0]; for ( s = 0; ++s < tf_numberstrips; ) { if ( bc[s] > bufsize ) bufsize = bc[s]; } tf_buf = (unsigned char*) _TIFFmalloc(bufsize); if (tf_buf == NULL) { TIFFError(filename, "No space for strip buffer"); return; }#if defined( EXP_ASCII85ENCODER ) if ( ascii85 ) { /* * Allocate a buffer to hold the ASCII85 encoded data. Note * that it is allocated with sufficient room to hold the * encoded data (5*bufsize/4) plus the EOD marker (+8) * and formatting line breaks. The line breaks are more * than taken care of by using 6*bufsize/4 rather than * 5*bufsize/4. */ ascii85_p = _TIFFmalloc( (bufsize+(bufsize/2)) + 8 ); if ( !ascii85_p ) { _TIFFfree( tf_buf ); TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); return; } }#endif for (s = 0; s < tf_numberstrips; s++) { cc = TIFFReadRawStrip(tif, s, tf_buf, bc[s]); if (cc < 0) { TIFFError(filename, "Can't read strip"); break; } if (fillorder == FILLORDER_LSB2MSB) TIFFReverseBits(tf_buf, cc); if (!ascii85) { for (cp = tf_buf; cc > 0; cc--) { DOBREAK(breaklen, 1, fd); c = *cp++; PUTHEX(c, fd); } fputs(">\n", fd); breaklen = MAXLINE; } else { Ascii85Init();#if defined( EXP_ASCII85ENCODER ) ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, tf_buf, cc ); if ( ascii85_l > 0 ) fwrite( ascii85_p, ascii85_l, 1, fd );#else for (cp = tf_buf; cc > 0; cc--) Ascii85Put(*cp++, fd); Ascii85Flush(fd);#endif /* EXP_ASCII85ENCODER */ } } _TIFFfree((char *) tf_buf);#if defined( EXP_ASCII85ENCODER ) if ( ascii85_p ) _TIFFfree( ascii85_p );#endif}voidAscii85Init(void){ ascii85breaklen = 2*MAXLINE; ascii85count = 0;}static char*Ascii85Encode(unsigned char* raw){ static char encoded[6]; uint32 word; word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3]; if (word != 0L) { uint32 q; uint16 w1; q = word / (85L*85*85*85); /* actually only a byte */ encoded[0] = q + '!'; word -= q * (85L*85*85*85); q = word / (85L*85*85); encoded[1] = q + '!'; word -= q * (85L*85*85); q = word / (85*85); encoded[2] = q + '!'; w1 = (uint16) (word - q*(85L*85)); encoded[3] = (w1 / 85) + '!'; encoded[4] = (w1 % 85) + '!'; encoded[5] = '\0'; } else encoded[0] = 'z', encoded[1] = '\0'; return (encoded);}voidAscii85Put(unsigned char code, FILE* fd){ ascii85buf[ascii85count++] = code; if (ascii85count >= 4) { unsigned char* p; int n; for (n = ascii85count, p = ascii85buf; n >= 4; n -= 4, p += 4) { char* cp; for (cp = Ascii85Encode(p); *cp; cp++) { putc(*cp, fd); if (--ascii85breaklen == 0) { putc('\n', fd); ascii85breaklen = 2*MAXLINE; } } } _TIFFmemcpy(ascii85buf, p, n); ascii85count = n; }}voidAscii85Flush(FILE* fd){ if (ascii85count > 0) { char* res; _TIFFmemset(&ascii85buf[ascii85count], 0, 3); res = Ascii85Encode(ascii85buf); fwrite(res[0] == 'z' ? "!!!!" : res, ascii85count + 1, 1, fd); } fputs("~>\n", fd);}#if defined( EXP_ASCII85ENCODER)#define A85BREAKCNTR ascii85breaklen#define A85BREAKLEN (2*MAXLINE)/******************************************************************************* Name: Ascii85EncodeBlock( ascii85_p, f_eod, raw_p, raw_l )** Description: This routine will encode the raw data in the buffer described* by raw_p and raw_l into ASCII85 format and store the encoding* in the buffer given by ascii85_p.** Parameters: ascii85_p - A buffer supplied by the caller which will* contain the encoded ASCII85 data.* f_eod - Flag: Nz means to end the encoded buffer with* an End-Of-Data marker.* raw_p - Pointer to the buffer of data to be encoded* raw_l - Number of bytes in raw_p[] to be encoded** Returns: (int) < 0 Error, see errno* >= 0 Number of bytes written to ascii85_p[].** Notes: An external variable given by A85BREAKCNTR is used to* determine when to insert newline characters into the* encoded data. As each byte is placed into ascii85_p this* external is decremented. If the variable is decrement to* or past zero then a newline is inserted into ascii85_p* and the A85BREAKCNTR is then reset to A85BREAKLEN.* Note: for efficiency reasons the A85BREAKCNTR variable* is not actually checked on *every* character* placed into ascii85_p but often only for every* 5 characters.** THE CALLER IS RESPONSIBLE FOR ENSURING THAT ASCII85_P[] IS* SUFFICIENTLY LARGE TO THE ENCODED DATA!* You will need at least 5 * (raw_l/4) bytes plus space for* newline characters and space for an EOD marker (if* requested). A safe calculation is to use 6*(raw_l/4) + 8* to size ascii85_p.******************************************************************************/int Ascii85EncodeBlock( uint8 * ascii85_p, unsigned f_eod, const uint8 * raw_p, int raw_l ){ char ascii85[5]; /* Encoded 5 tuple */ int ascii85_l; /* Number of bytes written to ascii85_p[] */ int rc; /* Return code */ uint32 val32; /* Unencoded 4 tuple */ ascii85_l = 0; /* Nothing written yet */ if ( raw_p ) { --raw_p; /* Prepare for pre-increment fetches */ for ( ; raw_l > 3; raw_l -= 4 ) { val32 = *(++raw_p) << 24; val32 += *(++raw_p) << 16; val32 += *(++raw_p) << 8; val32 += *(++raw_p); if ( val32 == 0 ) /* Special case */ { ascii85_p[ascii85_l] = 'z'; rc = 1; } else { ascii85[4] = (val32 % 85) + 33; val32 /= 85; ascii85[3] = (val32 % 85) + 33; val32 /= 85; ascii85[2] = (val32 % 85) + 33; val32 /= 85; ascii85[1] = (val32 % 85) + 33; ascii85[0] = (val32 / 85) + 33; _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, sizeof(ascii85) ); rc = sizeof(ascii85); } ascii85_l += rc; if ( (A85BREAKCNTR -= rc) <= 0 ) { ascii85_p[ascii85_l] = '\n'; ++ascii85_l; A85BREAKCNTR = A85BREAKLEN; } } /* * Output any straggler bytes: */ if ( raw_l > 0 ) { int len; /* Output this many bytes */ len = raw_l + 1; val32 = *++raw_p << 24; /* Prime the pump */ if ( --raw_l > 0 ) val32 += *(++raw_p) << 16; if ( --raw_l > 0 ) val32 += *(++raw_p) << 8; val32 /= 85; ascii85[3] = (val32 % 85) + 33;; val32 /= 85; ascii85[2] = (val32 % 85) + 33;; val32 /= 85; ascii85[1] = (val32 % 85) + 33;; ascii85[0] = (val32 / 85) + 33;; _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, len ); ascii85_l += len; } } /* * If requested add an ASCII85 End Of Data marker: */ if ( f_eod ) { ascii85_p[ascii85_l++] = '~'; ascii85_p[ascii85_l++] = '>'; ascii85_p[ascii85_l++] = '\n'; } return ( ascii85_l );} /* Ascii85EncodeBlock() */#endif /* EXP_ASCII85ENCODER */char* stuff[] = {"usage: tiff2ps [options] input.tif ...","where options are:"," -1 generate PostScript Level 1 (default)"," -2 generate PostScript Level 2"," -3 generate PostScript Level 3"," -8 disable use of ASCII85 encoding with PostScript Level 2/3"," -a convert all directories in file (default is first)"," -b # set the bottom margin to # inches"," -c center image (-b and -l still add to this)"," -d # convert directory number #"," -D enable duplex printing (two pages per sheet of paper)"," -e generate Encapsulated PostScript (EPS) (implies -z)"," -h # assume printed page height is # inches (default 11)"," -w # assume printed page width is # inches (default 8.5)"," -H # split image if height is more than # inches"," -L # overLap split images by # inches"," -i # enable/disable (Nz/0) pixel interpolation (default: enable)"," -l # set the left margin to # inches"," -m use \"imagemask\" operator instead of \"image\""," -o # convert directory at file offset #"," -O file write PostScript to file instead of standard output"," -p generate regular PostScript"," -r rotate by 180 degrees"," -s generate PostScript for a single image"," -T print pages for top edge binding"," -x override resolution units as centimeters"," -y override resolution units as inches"," -z enable printing in the deadzone (only for PostScript Level 2/3)",NULL};static voidusage(int code){ char buf[BUFSIZ]; int i; setbuf(stderr, buf); fprintf(stderr, "%s\n\n", TIFFGetVersion()); for (i = 0; stuff[i] != NULL; i++) fprintf(stderr, "%s\n", stuff[i]); exit(code);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -