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

📄 tiffcp.c

📁 tiff文件开发库
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Header: /cvsroot/osrs/libtiff/tools/tiffcp.c,v 1.15 2003/12/20 13:46:57 dron Exp $ *//* * Copyright (c) 1988-1997 Sam Leffler * Copyright (c) 1991-1997 Silicon Graphics, Inc. * *  Revised:  2/18/01 BAR -- added syntax for extracting single images from *                          multi-image TIFF files. * *    New syntax is:  sourceFileName,image# * * image# ranges from 0..<n-1> where n is the # of images in the file. * There may be no white space between the comma and the filename or * image number. * *    Example:   tiffcp source.tif,1 destination.tif * * Copies the 2nd image in source.tif to the destination. * ***** * Permission to use, copy, modify, distribute, and sell this software and  * its documentation for any purpose is hereby granted without fee, provided * that (i) the above copyright notices and this permission notice appear in * all copies of the software and related documentation, and (ii) the names of * Sam Leffler and Silicon Graphics may not be used in any advertising or * publicity relating to the software without the specific, prior written * permission of Sam Leffler and Silicon Graphics. *  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.   *  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  * OF THIS SOFTWARE. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <assert.h>#include "tiffio.h"#if defined(VMS)#define unlink delete#endif#define	streq(a,b)	(strcmp(a,b) == 0)#define	strneq(a,b,n)	(strncmp(a,b,n) == 0)#define	TRUE	1#define	FALSE	0static  int outtiled = -1;static  uint32 tilewidth;static  uint32 tilelength;static	uint16 config;static	uint16 compression;static	uint16 predictor;static	uint16 fillorder;static	uint16 orientation;static	uint32 rowsperstrip;static	uint32 g3opts;static	int ignore = FALSE;		/* if true, ignore read errors */static	uint32 defg3opts = (uint32) -1;static	int quality = 75;		/* JPEG quality */static	int jpegcolormode = JPEGCOLORMODE_RGB;static	uint16 defcompression = (uint16) -1;static	uint16 defpredictor = (uint16) -1;static	int tiffcp(TIFF*, TIFF*);static	int processCompressOptions(char*);static	void usage(void);static char comma = ',';  /* (default) comma separator character */static TIFF* bias = NULL;static int pageNum = 0;static int nextSrcImage (TIFF *tif, char **imageSpec)/*  seek to the next image specified in *imageSpec  returns 1 if success, 0 if no more images to process  *imageSpec=NULL if subsequent images should be processed in sequence*/{  if (**imageSpec == comma) {  /* if not @comma, we've done all images */    char *start = *imageSpec + 1;    unsigned long nextImage = strtol (start, imageSpec, 0);    if (start == *imageSpec) nextImage = TIFFCurrentDirectory (tif);    if (**imageSpec)    {      if (**imageSpec == comma) {          /* a trailing comma denotes remaining images in sequence */        if ((*imageSpec)[1] == '\0') *imageSpec = NULL;      }else{        fprintf (stderr,           "Expected a %c separated image # list after %s\n",          comma, TIFFFileName (tif));        exit (-4);   /* syntax error */      }    }    if (TIFFSetDirectory (tif, nextImage)) return 1;      fprintf (stderr, "%s%c%d not found!\n",              TIFFFileName(tif), comma, (int) nextImage);   }  return 0;}  static TIFF* openSrcImage (char **imageSpec)/*  imageSpec points to a pointer to a filename followed by optional ,image#'s  Open the TIFF file and assign *imageSpec to either NULL if there are  no images specified, or a pointer to the next image number text*/{    TIFF *tif;    char *fn = *imageSpec;    *imageSpec = strchr (fn, comma);    if (*imageSpec) {  /* there is at least one image number specifier */        **imageSpec = '\0';         tif = TIFFOpen (fn, "r");        /* but, ignore any single trailing comma */        if (!(*imageSpec)[1]) {*imageSpec = NULL; return tif;}        if (tif) {             **imageSpec = comma;  /* replace the comma */            if (!nextSrcImage(tif, imageSpec)) {              TIFFClose (tif);              tif = NULL;            }        }    }else        tif = TIFFOpen (fn, "r");    return tif;}intmain(int argc, char* argv[]){	uint16 defconfig = (uint16) -1;	uint16 deffillorder = 0;	uint32 deftilewidth = (uint32) -1;	uint32 deftilelength = (uint32) -1;	uint32 defrowsperstrip = (uint32) 0;	uint32 diroff = 0;	TIFF* in;	TIFF* out;	char mode[10];	char* mp = mode;	int c;	extern int optind;	extern char* optarg;	*mp++ = 'w';	*mp = '\0';	while ((c = getopt(argc, argv, ",:b:c:f:l:o:z:p:r:w:aistBLMC")) != -1)		switch (c) {                case ',':                        if (optarg[0] != '=') usage();                        comma = optarg[1];                        break;                case 'b':   /* this file is bias image subtracted from others */                        if (bias) {                          fputs ("Only 1 bias image may be specified\n", stderr);                          exit (-2);                        }                        {                          uint16    samples = (uint16) -1;                          char **biasFn = &optarg;                          bias = openSrcImage (biasFn);                          if (!bias) exit (-5);                          if (TIFFIsTiled (bias)) {                     fputs ("Bias image must be organized in strips\n", stderr);                            exit (-7);                          }		          TIFFGetField(bias, TIFFTAG_SAMPLESPERPIXEL, &samples);                          if (samples != 1) {                     fputs ("Bias image must be monochrome\n", stderr);                            exit (-7);                          }                        }                        break;		case 'a':		/* append to output */			mode[0] = 'a';			break;		case 'c':		/* compression scheme */			if (!processCompressOptions(optarg))				usage();			break;		case 'f':		/* fill order */			if (streq(optarg, "lsb2msb"))				deffillorder = FILLORDER_LSB2MSB;			else if (streq(optarg, "msb2lsb"))				deffillorder = FILLORDER_MSB2LSB;			else				usage();			break;		case 'i':		/* ignore errors */			ignore = TRUE;			break;		case 'l':		/* tile length */			outtiled = TRUE;			deftilelength = atoi(optarg);			break;		case 'o':		/* initial directory offset */			diroff = strtoul(optarg, NULL, 0);			break;		case 'p':		/* planar configuration */			if (streq(optarg, "separate"))				defconfig = PLANARCONFIG_SEPARATE;			else if (streq(optarg, "contig"))				defconfig = PLANARCONFIG_CONTIG;			else				usage();			break;		case 'r':		/* rows/strip */			defrowsperstrip = atol(optarg);			break;		case 's':		/* generate stripped output */			outtiled = FALSE;			break;		case 't':		/* generate tiled output */			outtiled = TRUE;			break;		case 'w':		/* tile width */			outtiled = TRUE;			deftilewidth = atoi(optarg);			break;		case 'B':			*mp++ = 'b'; *mp = '\0';			break;		case 'L':			*mp++ = 'l'; *mp = '\0';			break;		case 'M':			*mp++ = 'm'; *mp = '\0';			break;		case 'C':			*mp++ = 'c'; *mp = '\0';			break;		case '?':			usage();			/*NOTREACHED*/		}	if (argc - optind < 2)		usage();	out = TIFFOpen(argv[argc-1], mode);	if (out == NULL)		return (-2);	if ((argc - optind) == 2)	  pageNum = -1;	for (; optind < argc-1 ; optind++) {                char *imageCursor = argv[optind];		in = openSrcImage (&imageCursor);		if (in == NULL)			return (-3);		if (diroff != 0 && !TIFFSetSubDirectory(in, diroff)) {			TIFFError(TIFFFileName(in),			    "Error, setting subdirectory at %#x", diroff);			(void) TIFFClose(out);			return (1);		}                for (;;) {                   config = defconfig;                   compression = defcompression;                   predictor = defpredictor;                   fillorder = deffillorder;                   rowsperstrip = defrowsperstrip;                   tilewidth = deftilewidth;                   tilelength = deftilelength;                   g3opts = defg3opts;                   if (!tiffcp(in, out) || !TIFFWriteDirectory(out)) {                        TIFFClose(out);                        return (1);                   }                   if (imageCursor) { /* seek next image directory */                        if (!nextSrcImage(in, &imageCursor)) break;                   }else                        if (!TIFFReadDirectory(in)) break;		}		TIFFClose(in);	}        exit( 0 );}static voidprocessG3Options(char* cp){	if( (cp = strchr(cp, ':')) ) {		if (defg3opts == (uint32) -1)			defg3opts = 0;		do {			cp++;			if (strneq(cp, "1d", 2))				defg3opts &= ~GROUP3OPT_2DENCODING;			else if (strneq(cp, "2d", 2))				defg3opts |= GROUP3OPT_2DENCODING;			else if (strneq(cp, "fill", 4))				defg3opts |= GROUP3OPT_FILLBITS;			else				usage();		} while( (cp = strchr(cp, ':')) );	}}static intprocessCompressOptions(char* opt){	if (streq(opt, "none")) {		defcompression = COMPRESSION_NONE;	} else if (streq(opt, "packbits")) {		defcompression = COMPRESSION_PACKBITS;	} else if (strneq(opt, "jpeg", 4)) {		char* cp = strchr(opt, ':');		if (cp && isdigit(cp[1]))			quality = atoi(cp+1);		if (cp && strchr(cp, 'r'))			jpegcolormode = JPEGCOLORMODE_RAW;		defcompression = COMPRESSION_JPEG;	} else if (strneq(opt, "g3", 2)) {		processG3Options(opt);		defcompression = COMPRESSION_CCITTFAX3;	} else if (streq(opt, "g4")) {		defcompression = COMPRESSION_CCITTFAX4;	} else if (strneq(opt, "lzw", 3)) {		char* cp = strchr(opt, ':');		if (cp)			defpredictor = atoi(cp+1);		defcompression = COMPRESSION_LZW;	} else if (strneq(opt, "zip", 3)) {		char* cp = strchr(opt, ':');		if (cp)			defpredictor = atoi(cp+1);		defcompression = COMPRESSION_DEFLATE;	} else		return (0);	return (1);}char* stuff[] = {"usage: tiffcp [options] input... output","where options are:"," -a		append to output instead of overwriting"," -o offset	set initial directory offset"," -p contig	pack samples contiguously (e.g. RGBRGB...)"," -p separate	store samples separately (e.g. RRR...GGG...BBB...)"," -s		write output in strips"," -t		write output in tiles"," -i		ignore read errors"," -b file[,#]	bias (dark) monochrome image to be subtracted from all others"," -,=%	    	use % rather than , to separate image #'s (per Note below)",           ""," -r #		make each strip have no more than # rows"," -w #		set output tile width (pixels)"," -l #		set output tile length (pixels)",""," -f lsb2msb	force lsb-to-msb FillOrder for output"," -f msb2lsb	force msb-to-lsb FillOrder for output",""," -c lzw[:opts]	compress output with Lempel-Ziv & Welch encoding","               (no longer supported by default due to Unisys patent enforcement)", " -c zip[:opts]	compress output with deflate encoding"," -c jpeg[:opts]	compress output with JPEG encoding"," -c packbits	compress output with packbits encoding"," -c g3[:opts]	compress output with CCITT Group 3 encoding"," -c g4		compress output with CCITT Group 4 encoding"," -c none	use no compression algorithm on output","","Group 3 options:"," 1d		use default CCITT Group 3 1D-encoding"," 2d		use optional CCITT Group 3 2D-encoding"," fill		byte-align EOL codes","For example, -c g3:2d:fill to get G3-2D-encoded data with byte-aligned EOLs","","JPEG options:"," #		set compression quality level (0-100, default 75)"," r		output color image as RGB rather than YCbCr","For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality","","LZW and deflate options:"," #		set predictor value","For example, -c lzw:2 to get LZW-encoded data with horizontal differencing","","Note that input filenames may be of the form filename,x,y,z","where x, y, and z specify image numbers in the filename to copy.","example:  tiffcp -c none -b esp.tif,1 esp.tif,0 test.tif","  subtract 2nd image in esp.tif from 1st yielding uncompressed result test.tif",NULL};static voidusage(void){	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(-1);}#define	CopyField(tag, v) \    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)#define	CopyField2(tag, v1, v2) \    if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)#define	CopyField3(tag, v1, v2, v3) \    if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)#define	CopyField4(tag, v1, v2, v3, v4) \    if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) TIFFSetField(out, tag, v1, v2, v3, v4)static voidcpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type){	switch (type) {	case TIFF_SHORT:		if (count == 1) {			uint16 shortv;			CopyField(tag, shortv);		} else if (count == 2) {			uint16 shortv1, shortv2;			CopyField2(tag, shortv1, shortv2);		} else if (count == 4) {			uint16 *tr, *tg, *tb, *ta;			CopyField4(tag, tr, tg, tb, ta);		} else if (count == (uint16) -1) {			uint16 shortv1;			uint16* shortav;			CopyField2(tag, shortv1, shortav);		}		break;	case TIFF_LONG:		{ uint32 longv;		  CopyField(tag, longv);		}		break;	case TIFF_RATIONAL:		if (count == 1) {			float floatv;			CopyField(tag, floatv);		} else if (count == (uint16) -1) {			float* floatav;			CopyField(tag, floatav);		}		break;	case TIFF_ASCII:		{ char* stringv;		  CopyField(tag, stringv);		}		break;	case TIFF_DOUBLE:		if (count == 1) {			double doublev;			CopyField(tag, doublev);		} else if (count == (uint16) -1) {			double* doubleav;			CopyField(tag, doubleav);		}		break;          default:            assert( FALSE );	}}static struct cpTag {	uint16	tag;	uint16	count;	TIFFDataType type;} tags[] = {	{ TIFFTAG_SUBFILETYPE,		1, TIFF_LONG },	{ TIFFTAG_THRESHHOLDING,	1, TIFF_SHORT },	{ TIFFTAG_DOCUMENTNAME,		1, TIFF_ASCII },	{ TIFFTAG_IMAGEDESCRIPTION,	1, TIFF_ASCII },	{ TIFFTAG_MAKE,			1, TIFF_ASCII },	{ TIFFTAG_MODEL,		1, TIFF_ASCII },	{ TIFFTAG_MINSAMPLEVALUE,	1, TIFF_SHORT },	{ TIFFTAG_MAXSAMPLEVALUE,	1, TIFF_SHORT },	{ TIFFTAG_XRESOLUTION,		1, TIFF_RATIONAL },	{ TIFFTAG_YRESOLUTION,		1, TIFF_RATIONAL },	{ TIFFTAG_PAGENAME,		1, TIFF_ASCII },	{ TIFFTAG_XPOSITION,		1, TIFF_RATIONAL },	{ TIFFTAG_YPOSITION,		1, TIFF_RATIONAL },	{ TIFFTAG_RESOLUTIONUNIT,	1, TIFF_SHORT },	{ TIFFTAG_SOFTWARE,		1, TIFF_ASCII },	{ TIFFTAG_DATETIME,		1, TIFF_ASCII },	{ TIFFTAG_ARTIST,		1, TIFF_ASCII },	{ TIFFTAG_HOSTCOMPUTER,		1, TIFF_ASCII },	{ TIFFTAG_WHITEPOINT,		1, TIFF_RATIONAL },	{ TIFFTAG_PRIMARYCHROMATICITIES,(uint16) -1,TIFF_RATIONAL },	{ TIFFTAG_HALFTONEHINTS,	2, TIFF_SHORT },	{ TIFFTAG_INKSET,		1, TIFF_SHORT },	{ TIFFTAG_INKNAMES,		1, TIFF_ASCII },	{ TIFFTAG_NUMBEROFINKS,		1, TIFF_SHORT },	{ TIFFTAG_DOTRANGE,		2, TIFF_SHORT },	{ TIFFTAG_TARGETPRINTER,	1, TIFF_ASCII },	{ TIFFTAG_SAMPLEFORMAT,		1, TIFF_SHORT },	{ TIFFTAG_YCBCRCOEFFICIENTS,	(uint16) -1,TIFF_RATIONAL },	{ TIFFTAG_YCBCRSUBSAMPLING,	2, TIFF_SHORT },	{ TIFFTAG_YCBCRPOSITIONING,	1, TIFF_SHORT },	{ TIFFTAG_REFERENCEBLACKWHITE,	(uint16) -1,TIFF_RATIONAL },	{ TIFFTAG_EXTRASAMPLES,		(uint16) -1, TIFF_SHORT },	{ TIFFTAG_SMINSAMPLEVALUE,	1, TIFF_DOUBLE },	{ TIFFTAG_SMAXSAMPLEVALUE,	1, TIFF_DOUBLE },

⌨️ 快捷键说明

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