⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiff2ps.c

📁 tiff文件开发库
💻 C
📖 第 1 页 / 共 4 页
字号:
	if (cnt)	    left_offset += xtran / 2;	fprintf(fp, "%f %f translate\n", left_offset, bottom_offset);	fprintf(fp, "%f %f scale\n", xscale, yscale);	if (rotate)	    fputs ("1 1 translate 180 rotate\n", fp);	return splitpage;}/* returns the sequence number of the page processed */intTIFF2PS(FILE* fd, TIFF* tif, float pw, float ph, double lm, double bm, int cnt){	uint32 w, h;	float ox, oy, prw, prh;	float scale = 1.0;	float left_offset = lm * PS_UNIT_SIZE;	float bottom_offset = bm * PS_UNIT_SIZE;	uint32 subfiletype;	uint16* sampleinfo;	static int npages = 0;	int split;	if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))		ox = 0;	if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))		oy = 0;	setupPageState(tif, &w, &h, &prw, &prh);	do {		tf_numberstrips = TIFFNumberOfStrips(tif);		TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP,		    &tf_rowsperstrip);		setupPageState(tif, &w, &h, &prw, &prh);		if (!npages)			PSHead(fd, tif, w, h, prw, prh, ox, oy);		TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,		    &bitspersample);		TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL,		    &samplesperpixel);		TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG,		    &planarconfiguration);		TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);		TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,		    &extrasamples, &sampleinfo);		alpha = (extrasamples == 1 &&			 sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);		if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {			switch (samplesperpixel - extrasamples) {			case 1:				if (isCCITTCompression(tif))					photometric = PHOTOMETRIC_MINISWHITE;				else					photometric = PHOTOMETRIC_MINISBLACK;				break;			case 3:				photometric = PHOTOMETRIC_RGB;				break;			case 4:				photometric = PHOTOMETRIC_SEPARATED;				break;			}		}		if (checkImage(tif)) {			tf_bytesperrow = TIFFScanlineSize(tif);			npages++;			fprintf(fd, "%%%%Page: %d %d\n", npages, npages);			if (!generateEPSF && ( level2 || level3 )) {				float psw,psh;				if (pw!=0 && ph!=0) {					psw=pw;					psh=ph;			    				}				else {					psw=rotate ? prh:prw;				    psh=rotate ? prw:prh;		    				}				fprintf(fd,	"1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",				        psw, psh);				fputs(	"<<\n  /Policies <<\n    /PageSize 3\n  >>\n>> setpagedevice\n",				      fd);			}			fprintf(fd, "gsave\n");			fprintf(fd, "100 dict begin\n");			if (pw != 0 && ph != 0) {				if (maxPageHeight) { /* used -H option */					split = PlaceImage(fd,pw,ph,prw,prh,0,lm,bm,cnt);					while( split ) {					    PSpage(fd, tif, w, h);					    fprintf(fd, "end\n");					    fprintf(fd, "grestore\n");					    fprintf(fd, "showpage\n");					    npages++;					    fprintf(fd, "%%%%Page: %d %d\n", npages, npages);					    fprintf(fd, "gsave\n");					    fprintf(fd, "100 dict begin\n");					    split = PlaceImage(fd,pw,ph,prw,prh,split,lm,bm,cnt);					}				} else {					/* NB: maintain image aspect ratio */					scale = (pw*PS_UNIT_SIZE/prw) < (ph*PS_UNIT_SIZE/prh) ?					    (pw*PS_UNIT_SIZE/prw) :					    (ph*PS_UNIT_SIZE/prh);					if (scale > 1.0)						scale = 1.0;					bottom_offset +=						(ph * PS_UNIT_SIZE - prh * scale) / (cnt?2:1);					if (cnt)						left_offset += (pw * PS_UNIT_SIZE - prw * scale) / 2;					fprintf(fd, "%f %f translate\n",						left_offset, bottom_offset);					fprintf(fd, "%f %f scale\n", prw * scale, prh * scale);					if (rotate)						fputs ("1 1 translate 180 rotate\n", fd);				}			} else {				fprintf(fd, "%f %f scale\n", prw, prh);				if (rotate)					fputs ("1 1 translate 180 rotate\n", fd);			}			PSpage(fd, tif, w, h);			fprintf(fd, "end\n");			fprintf(fd, "grestore\n");			fprintf(fd, "showpage\n");		}		if (generateEPSF)			break;		TIFFGetFieldDefaulted(tif, TIFFTAG_SUBFILETYPE, &subfiletype);	} while (((subfiletype & FILETYPE_PAGE) || printAll) &&	    TIFFReadDirectory(tif));	return(npages);}static char DuplexPreamble[] = "\%%BeginFeature: *Duplex True\n\systemdict begin\n\  /languagelevel where { pop languagelevel } { 1 } ifelse\n\  2 ge { 1 dict dup /Duplex true put setpagedevice }\n\  { statusdict /setduplex known { statusdict begin setduplex true end } if\n\  } ifelse\n\end\n\%%EndFeature\n\";static char TumblePreamble[] = "\%%BeginFeature: *Tumble True\n\systemdict begin\n\  /languagelevel where { pop languagelevel } { 1 } ifelse\n\  2 ge { 1 dict dup /Tumble true put setpagedevice }\n\  { statusdict /settumble known { statusdict begin true settumble end } if\n\  } ifelse\n\end\n\%%EndFeature\n\";static char AvoidDeadZonePreamble[] = "\gsave newpath clippath pathbbox grestore\n\  4 2 roll 2 copy translate\n\  exch 3 1 roll sub 3 1 roll sub exch\n\  currentpagedevice /PageSize get aload pop\n\  exch 3 1 roll div 3 1 roll div abs exch abs\n\  2 copy gt { exch } if pop\n\  dup 1 lt { dup scale } { pop } ifelse\n\";voidPSHead(FILE *fd, TIFF *tif, uint32 w, uint32 h, float pw, float ph,	float ox, float oy){	time_t t;	(void) tif; (void) w; (void) h;	t = time(0);	fprintf(fd, "%%!PS-Adobe-3.0%s\n", generateEPSF ? " EPSF-3.0" : "");	fprintf(fd, "%%%%Creator: tiff2ps\n");	fprintf(fd, "%%%%Title: %s\n", filename);	fprintf(fd, "%%%%CreationDate: %s", ctime(&t));	fprintf(fd, "%%%%DocumentData: Clean7Bit\n");	fprintf(fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy);	/* NB: should use PageBoundingBox */	fprintf(fd, "%%%%BoundingBox: 0 0 %ld %ld\n",	    (long) ceil(pw), (long) ceil(ph));	fprintf(fd, "%%%%LanguageLevel: %d\n", (level3 ? 3 : (level2 ? 2 : 1)));	fprintf(fd, "%%%%Pages: (atend)\n");	fprintf(fd, "%%%%EndComments\n");	fprintf(fd, "%%%%BeginSetup\n");	if (PSduplex)		fprintf(fd, "%s", DuplexPreamble);	if (PStumble)		fprintf(fd, "%s", TumblePreamble);	if (PSavoiddeadzone && (level2 || level3))		fprintf(fd, "%s", AvoidDeadZonePreamble);	fprintf(fd, "%%%%EndSetup\n");}voidPSTail(FILE *fd, int npages){	fprintf(fd, "%%%%Trailer\n");	fprintf(fd, "%%%%Pages: %d\n", npages);	fprintf(fd, "%%%%EOF\n");}static intcheckcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b){	(void) tif;	while (n-- > 0)		if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)			return (16);	TIFFWarning(filename, "Assuming 8-bit colormap");	return (8);}static voidPS_Lvl2colorspace(FILE* fd, TIFF* tif){	uint16 *rmap, *gmap, *bmap;	int i, num_colors;	const char * colorspace_p;	switch ( photometric )	{	case PHOTOMETRIC_SEPARATED:		colorspace_p = "CMYK";		break;	case PHOTOMETRIC_RGB:		colorspace_p = "RGB";		break;	default:		colorspace_p = "Gray";	}	/*	 * Set up PostScript Level 2 colorspace according to	 * section 4.8 in the PostScript refenence manual.	 */	fputs("% PostScript Level 2 only.\n", fd);	if (photometric != PHOTOMETRIC_PALETTE) {		if (photometric == PHOTOMETRIC_YCBCR) {		    /* MORE CODE HERE */		}		fprintf(fd, "/Device%s setcolorspace\n", colorspace_p );		return;	}	/*	 * Set up an indexed/palette colorspace	 */	num_colors = (1 << bitspersample);	if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {		TIFFError(filename,			"Palette image w/o \"Colormap\" tag");		return;	}	if (checkcmap(tif, num_colors, rmap, gmap, bmap) == 16) {		/*		 * Convert colormap to 8-bits values.		 */#define	CVT(x)		(((x) * 255) / ((1L<<16)-1))		for (i = 0; i < num_colors; i++) {			rmap[i] = CVT(rmap[i]);			gmap[i] = CVT(gmap[i]);			bmap[i] = CVT(bmap[i]);		}#undef CVT	}	fprintf(fd, "[ /Indexed /DeviceRGB %d", num_colors - 1);	if (ascii85) {		Ascii85Init();		fputs("\n<~", fd);		ascii85breaklen -= 2;	} else		fputs(" <", fd);	for (i = 0; i < num_colors; i++) {		if (ascii85) {			Ascii85Put(rmap[i], fd);			Ascii85Put(gmap[i], fd);			Ascii85Put(bmap[i], fd);		} else {			fputs((i % 8) ? " " : "\n  ", fd);			fprintf(fd, "%02x%02x%02x",			    rmap[i], gmap[i], bmap[i]);		}	}	if (ascii85)		Ascii85Flush(fd);	else		fputs(">\n", fd);	fputs("] setcolorspace\n", fd);}static intPS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h){	int use_rawdata;	uint32 tile_width, tile_height;	uint16 predictor, minsamplevalue, maxsamplevalue;	int repeat_count;	char im_h[64], im_x[64], im_y[64];	char * imageOp = "image";	if ( useImagemask && (bitspersample == 1) )		imageOp = "imagemask";	(void)strcpy(im_x, "0");	(void)sprintf(im_y, "%lu", (long) h);	(void)sprintf(im_h, "%lu", (long) h);	tile_width = w;	tile_height = h;	if (TIFFIsTiled(tif)) {		repeat_count = TIFFNumberOfTiles(tif);		TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);		TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);		if (tile_width > w || tile_height > h ||		    (w % tile_width) != 0 || (h % tile_height != 0)) {			/*			 * The tiles does not fit image width and height.			 * Set up a clip rectangle for the image unit square.			 */			fputs("0 0 1 1 rectclip\n", fd);		}		if (tile_width < w) {			fputs("/im_x 0 def\n", fd);			(void)strcpy(im_x, "im_x neg");		}		if (tile_height < h) {			fputs("/im_y 0 def\n", fd);			(void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);		}	} else {		repeat_count = tf_numberstrips;		tile_height = tf_rowsperstrip;		if (tile_height > h)			tile_height = h;		if (repeat_count > 1) {			fputs("/im_y 0 def\n", fd);			fprintf(fd, "/im_h %lu def\n",			    (unsigned long) tile_height);			(void)strcpy(im_h, "im_h");			(void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);		}	}	/*	 * Output start of exec block	 */	fputs("{ % exec\n", fd);	if (repeat_count > 1)		fprintf(fd, "%d { %% repeat\n", repeat_count);	/*	 * Output filter options and image dictionary.	 */	if (ascii85)		fputs(" /im_stream currentfile /ASCII85Decode filter def\n",		    fd);	fputs(" <<\n", fd);	fputs("  /ImageType 1\n", fd);	fprintf(fd, "  /Width %lu\n", (unsigned long) tile_width);	/*	 * Workaround for some software that may crash when last strip	 * of image contains fewer number of scanlines than specified	 * by the `/Height' variable. So for stripped images with multiple	 * strips we will set `/Height' as `im_h', because one is 	 * recalculated for each strip - including the (smaller) final strip.	 * For tiled images and images with only one strip `/Height' will	 * contain number of scanlines in tile (or image height in case of	 * one-stripped image).	 */	if (TIFFIsTiled(tif) || tf_numberstrips == 1)		fprintf(fd, "  /Height %lu\n", (unsigned long) tile_height);	else		fprintf(fd, "  /Height im_h\n");		if (planarconfiguration == PLANARCONFIG_SEPARATE && samplesperpixel > 1)		fputs("  /MultipleDataSources true\n", fd);	fprintf(fd, "  /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",	    (unsigned long) w, - (long)h, im_x, im_y);	fprintf(fd, "  /BitsPerComponent %d\n", bitspersample);	fprintf(fd, "  /Interpolate %s\n", interpolate ? "true" : "false");	switch (samplesperpixel - extrasamples) {	case 1:		switch (photometric) {		case PHOTOMETRIC_MINISBLACK:			fputs("  /Decode [0 1]\n", fd);			break;		case PHOTOMETRIC_MINISWHITE:			switch (compression) {			case COMPRESSION_CCITTRLE:			case COMPRESSION_CCITTRLEW:			case COMPRESSION_CCITTFAX3:			case COMPRESSION_CCITTFAX4:				/*				 * Manage inverting with /Blackis1 flag				 * since there migth be uncompressed parts				 */				fputs("  /Decode [0 1]\n", fd);				break;			default:				/*				 * ERROR...				 */				fputs("  /Decode [1 0]\n", fd);				break;			}			break;		case PHOTOMETRIC_PALETTE:			TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE,			    &minsamplevalue);			TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE,			    &maxsamplevalue);			fprintf(fd, "  /Decode [%u %u]\n",				    minsamplevalue, maxsamplevalue);			break;		default:			/*			 * ERROR ?			 */			fputs("  /Decode [0 1]\n", fd);			break;		}		break;	case 3:		switch (photometric) {		case PHOTOMETRIC_RGB:			fputs("  /Decode [0 1 0 1 0 1]\n", fd);			break;		case PHOTOMETRIC_MINISWHITE:		case PHOTOMETRIC_MINISBLACK:		default:			/*			 * ERROR??			 */			fputs("  /Decode [0 1 0 1 0 1]\n", fd);			break;		}		break;	case 4:		/*		 * ERROR??		 */		fputs("  /Decode [0 1 0 1 0 1 0 1]\n", fd);		break;	}	fputs("  /DataSource", fd);	if (planarconfiguration == PLANARCONFIG_SEPARATE &&	    samplesperpixel > 1)		fputs(" [", fd);	if (ascii85)		fputs(" im_stream", fd);	else		fputs(" currentfile /ASCIIHexDecode filter", fd);	use_rawdata = TRUE;	switch (compression) {	case COMPRESSION_NONE:		/* 1: uncompressed */		break;	case COMPRESSION_CCITTRLE:	/* 2: CCITT modified Huffman RLE */	case COMPRESSION_CCITTRLEW:	/* 32771: #1 w/ word alignment */	case COMPRESSION_CCITTFAX3:	/* 3: CCITT Group 3 fax encoding */	case COMPRESSION_CCITTFAX4:	/* 4: CCITT Group 4 fax encoding */		fputs("\n\t<<\n", fd);		if (compression == COMPRESSION_CCITTFAX3) {			uint32 g3_options;			fputs("\t /EndOfLine true\n", fd);			fputs("\t /EndOfBlock false\n", fd);			if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS,					    &g3_options))				g3_options = 0;			if (g3_options & GROUP3OPT_2DENCODING)				fprintf(fd, "\t /K %s\n", im_h);			if (g3_options & GROUP3OPT_UNCOMPRESSED)				fputs("\t /Uncompressed true\n", fd);			if (g3_options & GROUP3OPT_FILLBITS)				fputs("\t /EncodedByteAlign true\n", fd);		}		if (compression == COMPRESSION_CCITTFAX4) {			uint32 g4_options;			fputs("\t /K -1\n", fd);			TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS,					       &g4_options);			if (g4_options & GROUP4OPT_UNCOMPRESSED)				fputs("\t /Uncompressed true\n", fd);		}		if (!(tile_width == w && w == 1728U))			fprintf(fd, "\t /Columns %lu\n",			    (unsigned long) tile_width);		fprintf(fd, "\t /Rows %s\n", im_h);		if (compression == COMPRESSION_CCITTRLE ||		    compression == COMPRESSION_CCITTRLEW) {			fputs("\t /EncodedByteAlign true\n", fd);			fputs("\t /EndOfBlock false\n", fd);		}		if (photometric == PHOTOMETRIC_MINISBLACK)			fputs("\t /BlackIs1 true\n", fd);		fprintf(fd, "\t>> /CCITTFaxDecode filter");		break;	case COMPRESSION_LZW:	/* 5: Lempel-Ziv & Welch */		TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);		if (predictor == 2) {			fputs("\n\t<<\n", fd);			fprintf(fd, "\t /Predictor %u\n", predictor);			fprintf(fd, "\t /Columns %lu\n",			    (unsigned long) tile_width);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -