📄 tiff2ps.c
字号:
fprintf(fd, "\t /Colors %u\n", samplesperpixel); fprintf(fd, "\t /BitsPerComponent %u\n", bitspersample); fputs("\t>>", fd); } fputs(" /LZWDecode filter", fd); break; case COMPRESSION_DEFLATE: /* 5: ZIP */ case COMPRESSION_ADOBE_DEFLATE: if ( level3 ) { TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor); if (predictor > 1) { fprintf(fd, "\t %% PostScript Level 3 only."); fputs("\n\t<<\n", fd); fprintf(fd, "\t /Predictor %u\n", predictor); fprintf(fd, "\t /Columns %lu\n", (unsigned long) tile_width); fprintf(fd, "\t /Colors %u\n", samplesperpixel); fprintf(fd, "\t /BitsPerComponent %u\n", bitspersample); fputs("\t>>", fd); } fputs(" /FlateDecode filter", fd); } else { use_rawdata = FALSE ; } break; case COMPRESSION_PACKBITS: /* 32773: Macintosh RLE */ fputs(" /RunLengthDecode filter", fd); use_rawdata = TRUE; break; case COMPRESSION_OJPEG: /* 6: !6.0 JPEG */ case COMPRESSION_JPEG: /* 7: %JPEG DCT compression */#ifdef notdef /* * Code not tested yet */ fputs(" /DCTDecode filter", fd); use_rawdata = TRUE;#else use_rawdata = FALSE;#endif break; case COMPRESSION_NEXT: /* 32766: NeXT 2-bit RLE */ case COMPRESSION_THUNDERSCAN: /* 32809: ThunderScan RLE */ case COMPRESSION_PIXARFILM: /* 32908: Pixar companded 10bit LZW */ case COMPRESSION_JBIG: /* 34661: ISO JBIG */ use_rawdata = FALSE; break; case COMPRESSION_SGILOG: /* 34676: SGI LogL or LogLuv */ case COMPRESSION_SGILOG24: /* 34677: SGI 24-bit LogLuv */ use_rawdata = FALSE; break; default: /* * ERROR... */ use_rawdata = FALSE; break; } if (planarconfiguration == PLANARCONFIG_SEPARATE && samplesperpixel > 1) { uint16 i; /* * NOTE: This code does not work yet... */ for (i = 1; i < samplesperpixel; i++) fputs(" dup", fd); fputs(" ]", fd); } fprintf( fd, "\n >> %s\n", imageOp ); if (ascii85) fputs(" im_stream status { im_stream flushfile } if\n", fd); if (repeat_count > 1) { if (tile_width < w) { fprintf(fd, " /im_x im_x %lu add def\n", (unsigned long) tile_width); if (tile_height < h) { fprintf(fd, " im_x %lu ge {\n", (unsigned long) w); fputs(" /im_x 0 def\n", fd); fprintf(fd, " /im_y im_y %lu add def\n", (unsigned long) tile_height); fputs(" } if\n", fd); } } if (tile_height < h) { if (tile_width >= w) { fprintf(fd, " /im_y im_y %lu add def\n", (unsigned long) tile_height); if (!TIFFIsTiled(tif)) { fprintf(fd, " /im_h %lu im_y sub", (unsigned long) h); fprintf(fd, " dup %lu gt { pop", (unsigned long) tile_height); fprintf(fd, " %lu } if def\n", (unsigned long) tile_height); } } } fputs("} repeat\n", fd); } /* * End of exec function */ fputs("}\n", fd); return(use_rawdata);}intPS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h){ uint16 fillorder; int use_rawdata, tiled_image, breaklen; uint32 chunk_no, num_chunks, *bc; unsigned char *buf_data, *cp; tsize_t chunk_size, byte_count;#if defined( EXP_ASCII85ENCODER ) int ascii85_l; /* Length, in bytes, of ascii85_p[] data */ uint8 * ascii85_p = 0; /* Holds ASCII85 encoded data */#endif PS_Lvl2colorspace(fd, tif); use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h);/* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */#ifdef ENABLE_BROKEN_BEGINENDDATA fputs("%%BeginData:\n", fd);#endif fputs("exec\n", fd); tiled_image = TIFFIsTiled(tif); if (tiled_image) { num_chunks = TIFFNumberOfTiles(tif); TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc); } else { num_chunks = TIFFNumberOfStrips(tif); TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc); } if (use_rawdata) { chunk_size = bc[0]; for (chunk_no = 1; chunk_no < num_chunks; chunk_no++) if (bc[chunk_no] > chunk_size) chunk_size = bc[chunk_no]; } else { if (tiled_image) chunk_size = TIFFTileSize(tif); else chunk_size = TIFFStripSize(tif); } buf_data = (unsigned char *)_TIFFmalloc(chunk_size); if (!buf_data) { TIFFError(filename, "Can't alloc %u bytes for %s.", chunk_size, tiled_image ? "tiles" : "strips"); return(FALSE); }#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*chunk_size/4) plus the EOD marker (+8) * and formatting line breaks. The line breaks are more * than taken care of by using 6*chunk_size/4 rather than * 5*chunk_size/4. */ ascii85_p = _TIFFmalloc( (chunk_size+(chunk_size/2)) + 8 ); if ( !ascii85_p ) { _TIFFfree( buf_data ); TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); return ( FALSE ); } }#endif TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) { if (ascii85) Ascii85Init(); else breaklen = 36; if (use_rawdata) { if (tiled_image) byte_count = TIFFReadRawTile(tif, chunk_no, buf_data, chunk_size); else byte_count = TIFFReadRawStrip(tif, chunk_no, buf_data, chunk_size); if (fillorder == FILLORDER_LSB2MSB) TIFFReverseBits(buf_data, byte_count); } else { if (tiled_image) byte_count = TIFFReadEncodedTile(tif, chunk_no, buf_data, chunk_size); else byte_count = TIFFReadEncodedStrip(tif, chunk_no, buf_data, chunk_size); } if (byte_count < 0) { TIFFError(filename, "Can't read %s %d.", tiled_image ? "tile" : "strip", chunk_no); if (ascii85) Ascii85Put('\0', fd); } /* * For images with alpha, matte against a white background; * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the * lower part of the buffer with the modified values. * * XXX: needs better solution */ if (alpha) { int adjust, i, j = 0; int ncomps = samplesperpixel - extrasamples; for (i = 0; i < byte_count; i+=samplesperpixel) { adjust = 255 - buf_data[i + ncomps]; switch (ncomps) { case 1: buf_data[j++] = buf_data[i] + adjust; break; case 2: buf_data[j++] = buf_data[i] + adjust; buf_data[j++] = buf_data[i+1] + adjust; break; case 3: buf_data[j++] = buf_data[i] + adjust; buf_data[j++] = buf_data[i+1] + adjust; buf_data[j++] = buf_data[i+2] + adjust; break; } } byte_count -= j; } if (ascii85) {#if defined( EXP_ASCII85ENCODER ) ascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count ); if ( ascii85_l > 0 ) fwrite( ascii85_p, ascii85_l, 1, fd );#else for (cp = buf_data; byte_count > 0; byte_count--) Ascii85Put(*cp++, fd);#endif } else { for (cp = buf_data; byte_count > 0; byte_count--) { putc(hex[((*cp)>>4)&0xf], fd); putc(hex[(*cp)&0xf], fd); cp++; if (--breaklen <= 0) { putc('\n', fd); breaklen = 36; } } } if ( !ascii85 ) { if ( level2 || level3 ) putc( '>', fd ); putc('\n', fd); }#if !defined( EXP_ASCII85ENCODER ) else Ascii85Flush(fd);#endif }#if defined( EXP_ASCII85ENCODER ) if ( ascii85_p ) _TIFFfree( ascii85_p );#endif _TIFFfree(buf_data);#ifdef ENABLE_BROKEN_BEGINENDDATA fputs("%%EndData\n", fd);#endif return(TRUE);}voidPSpage(FILE* fd, TIFF* tif, uint32 w, uint32 h){ char * imageOp = "image"; if ( useImagemask && (bitspersample == 1) ) imageOp = "imagemask"; if ((level2 || level3) && PS_Lvl2page(fd, tif, w, h)) return; ps_bytesperrow = tf_bytesperrow - (extrasamples * bitspersample / 8)*w; switch (photometric) { case PHOTOMETRIC_RGB: if (planarconfiguration == PLANARCONFIG_CONTIG) { fprintf(fd, "%s", RGBcolorimage); PSColorContigPreamble(fd, w, h, 3); PSDataColorContig(fd, tif, w, h, 3); } else { PSColorSeparatePreamble(fd, w, h, 3); PSDataColorSeparate(fd, tif, w, h, 3); } break; case PHOTOMETRIC_SEPARATED: /* XXX should emit CMYKcolorimage */ if (planarconfiguration == PLANARCONFIG_CONTIG) { PSColorContigPreamble(fd, w, h, 4); PSDataColorContig(fd, tif, w, h, 4); } else { PSColorSeparatePreamble(fd, w, h, 4); PSDataColorSeparate(fd, tif, w, h, 4); } break; case PHOTOMETRIC_PALETTE: fprintf(fd, "%s", RGBcolorimage); PhotoshopBanner(fd, w, h, 1, 3, "false 3 colorimage"); fprintf(fd, "/scanLine %ld string def\n", (long) ps_bytesperrow * 3L); fprintf(fd, "%lu %lu 8\n", (unsigned long) w, (unsigned long) h); fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", (unsigned long) w, (unsigned long) h, (unsigned long) h); fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n"); fprintf(fd, "false 3 colorimage\n"); PSDataPalette(fd, tif, w, h); break; case PHOTOMETRIC_MINISBLACK: case PHOTOMETRIC_MINISWHITE: PhotoshopBanner(fd, w, h, 1, 1, imageOp); fprintf(fd, "/scanLine %ld string def\n", (long) ps_bytesperrow); fprintf(fd, "%lu %lu %d\n", (unsigned long) w, (unsigned long) h, bitspersample); fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", (unsigned long) w, (unsigned long) h, (unsigned long) h); fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n"); fprintf(fd, "%s\n", imageOp); PSDataBW(fd, tif, w, h); break; } putc('\n', fd);}voidPSColorContigPreamble(FILE* fd, uint32 w, uint32 h, int nc){ ps_bytesperrow = nc * (tf_bytesperrow / samplesperpixel); PhotoshopBanner(fd, w, h, 1, nc, "false %d colorimage"); fprintf(fd, "/line %ld string def\n", (long) ps_bytesperrow); fprintf(fd, "%lu %lu %d\n", (unsigned long) w, (unsigned long) h, bitspersample); fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", (unsigned long) w, (unsigned long) h, (unsigned long) h); fprintf(fd, "{currentfile line readhexstring pop} bind\n"); fprintf(fd, "false %d colorimage\n", nc);}voidPSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc){ int i; PhotoshopBanner(fd, w, h, ps_bytesperrow, nc, "true %d colorimage"); for (i = 0; i < nc; i++) fprintf(fd, "/line%d %ld string def\n", i, (long) ps_bytesperrow); fprintf(fd, "%lu %lu %d\n", (unsigned long) w, (unsigned long) h, bitspersample); fprintf(fd, "[%lu 0 0 -%lu 0 %lu] \n", (unsigned long) w, (unsigned long) h, (unsigned long) h); for (i = 0; i < nc; i++) fprintf(fd, "{currentfile line%d readhexstring pop}bind\n", i); fprintf(fd, "true %d colorimage\n", nc);}#define MAXLINE 36#define DOBREAK(len, howmany, fd) \ if (((len) -= (howmany)) <= 0) { \ putc('\n', fd); \ (len) = MAXLINE-(howmany); \ }#define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)voidPSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc){ uint32 row; int breaklen = MAXLINE, cc, es = samplesperpixel - nc; unsigned char *tf_buf; unsigned char *cp, c; (void) w; tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; } for (row = 0; row < h; row++) { if (TIFFReadScanline(tif, tf_buf, row, 0) < 0) break; cp = tf_buf; if (alpha) { int adjust; cc = 0; for (; cc < tf_bytesperrow; cc += samplesperpixel) { DOBREAK(breaklen, nc, fd); /* * For images with alpha, matte against * a white background; i.e. * Cback * (1 - Aimage) * where Cback = 1. */ adjust = 255 - cp[nc]; switch (nc) { case 4: c = *cp++ + adjust; PUTHEX(c,fd); case 3: c = *cp++ + adjust; PUTHEX(c,fd); case 2: c = *cp++ + adjust; PUTHEX(c,fd); case 1: c = *cp++ + adjust; PUTHEX(c,fd); } cp += es; } } else { cc = 0; for (; cc < tf_bytesperrow; cc += samplesperpixel) { DOBREAK(breaklen, nc, fd); switch (nc) { case 4: c = *cp++; PUTHEX(c,fd); case 3: c = *cp++; PUTHEX(c,fd); case 2: c = *cp++; PUTHEX(c,fd); case 1: c = *cp++; PUTHEX(c,fd); } cp += es; } } } _TIFFfree((char *) tf_buf);}voidPSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc){ uint32 row; int breaklen = MAXLINE, cc, s, maxs; unsigned char *tf_buf; unsigned char *cp, c; (void) w; tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; } maxs = (samplesperpixel > nc ? nc : samplesperpixel); for (row = 0; row < h; row++) { for (s = 0; s < maxs; s++) { if (TIFFReadScanline(tif, tf_buf, row, s) < 0) break; for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) { DOBREAK(breaklen, 1, fd); c = *cp++; PUTHEX(c,fd); } } } _TIFFfree((char *) tf_buf);}#define PUTRGBHEX(c,fd) \ PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)voidPSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h){ uint16 *rmap, *gmap, *bmap; uint32 row; int breaklen = MAXLINE, cc, nc; unsigned char *tf_buf; unsigned char *cp, c; (void) w; if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) { TIFFError(filename, "Palette image w/o \"Colormap\" tag"); return; } switch (bitspersample) { case 8: case 4: case 2: case 1: break; default: TIFFError(filename, "Depth %d not supported", bitspersample); return; } nc = 3 * (8 / bitspersample); tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); if (tf_buf == NULL) { TIFFError(filename, "No space for scanline buffer"); return; } if (checkcmap(tif, 1<<bitspersample, rmap, gmap, bmap) == 16) { int i;#define CVT(x) (((x) * 255) / ((1U<<16)-1)) for (i = (1<<bitspersample)-1; i >= 0; i--) { rmap[i] = CVT(rmap[i]); gmap[i] = CVT(gmap[i]); bmap[i] = CVT(bmap[i]); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -