roentgen b75cab
/* $Id: tiffsplit.c,v 1.22 2011-10-22 17:03:01 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1992-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1992-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tif_config.h"
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#ifndef HAVE_GETOPT
roentgen b75cab
extern int getopt(int, char**, char*);
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#define	CopyField(tag, v) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
roentgen b75cab
#define	CopyField2(tag, v1, v2) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
roentgen b75cab
#define	CopyField3(tag, v1, v2, v3) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
roentgen b75cab
roentgen b75cab
#define PATH_LENGTH 8192
roentgen b75cab
roentgen b75cab
static const char TIFF_SUFFIX[] = ".tif";
roentgen b75cab
roentgen b75cab
static	char fname[PATH_LENGTH];
roentgen b75cab
roentgen b75cab
static	int tiffcp(TIFF*, TIFF*);
roentgen b75cab
static	void newfilename(void);
roentgen b75cab
static	int cpStrips(TIFF*, TIFF*);
roentgen b75cab
static	int cpTiles(TIFF*, TIFF*);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	TIFF *in, *out;
roentgen b75cab
roentgen b75cab
	if (argc < 2) {
roentgen b75cab
                fprintf(stderr, "%s\n\n", TIFFGetVersion());
roentgen b75cab
		fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n");
roentgen b75cab
		return (-3);
roentgen b75cab
	}
roentgen b75cab
	if (argc > 2) {
roentgen b75cab
		strncpy(fname, argv[2], sizeof(fname));
roentgen b75cab
		fname[sizeof(fname) - 1] = '\0';
roentgen b75cab
	}
roentgen b75cab
	in = TIFFOpen(argv[1], "r");
roentgen b75cab
	if (in != NULL) {
roentgen b75cab
		do {
roentgen b75cab
			size_t path_len;
roentgen b75cab
			char *path;
roentgen b75cab
			
roentgen b75cab
			newfilename();
roentgen b75cab
roentgen b75cab
			path_len = strlen(fname) + sizeof(TIFF_SUFFIX);
roentgen b75cab
			path = (char *) _TIFFmalloc(path_len);
roentgen b75cab
			strncpy(path, fname, path_len);
roentgen b75cab
			path[path_len - 1] = '\0';
roentgen b75cab
			strncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1);
roentgen b75cab
			out = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl");
roentgen b75cab
			_TIFFfree(path);
roentgen b75cab
roentgen b75cab
			if (out == NULL)
roentgen b75cab
				return (-2);
roentgen b75cab
			if (!tiffcp(in, out))
roentgen b75cab
				return (-1);
roentgen b75cab
			TIFFClose(out);
roentgen b75cab
		} while (TIFFReadDirectory(in));
roentgen b75cab
		(void) TIFFClose(in);
roentgen b75cab
	}
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
newfilename(void)
roentgen b75cab
{
roentgen b75cab
	static int first = 1;
roentgen b75cab
	static long lastTurn;
roentgen b75cab
	static long fnum;
roentgen b75cab
	static short defname;
roentgen b75cab
	static char *fpnt;
roentgen b75cab
roentgen b75cab
	if (first) {
roentgen b75cab
		if (fname[0]) {
roentgen b75cab
			fpnt = fname + strlen(fname);
roentgen b75cab
			defname = 0;
roentgen b75cab
		} else {
roentgen b75cab
			fname[0] = 'x';
roentgen b75cab
			fpnt = fname + 1;
roentgen b75cab
			defname = 1;
roentgen b75cab
		}
roentgen b75cab
		first = 0;
roentgen b75cab
	}
roentgen b75cab
#define	MAXFILES	17576
roentgen b75cab
	if (fnum == MAXFILES) {
roentgen b75cab
		if (!defname || fname[0] == 'z') {
roentgen b75cab
			fprintf(stderr, "tiffsplit: too many files.\n");
roentgen b75cab
			exit(1);
roentgen b75cab
		}
roentgen b75cab
		fname[0]++;
roentgen b75cab
		fnum = 0;
roentgen b75cab
	}
roentgen b75cab
	if (fnum % 676 == 0) {
roentgen b75cab
		if (fnum != 0) {
roentgen b75cab
			/*
roentgen b75cab
                         * advance to next letter every 676 pages
roentgen b75cab
			 * condition for 'z'++ will be covered above
roentgen b75cab
                         */
roentgen b75cab
			fpnt[0]++;
roentgen b75cab
		} else {
roentgen b75cab
			/*
roentgen b75cab
                         * set to 'a' if we are on the very first file
roentgen b75cab
                         */
roentgen b75cab
			fpnt[0] = 'a';
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
                 * set the value of the last turning point
roentgen b75cab
                 */
roentgen b75cab
		lastTurn = fnum;
roentgen b75cab
	}
roentgen b75cab
	/* 
roentgen b75cab
         * start from 0 every 676 times (provided by lastTurn)
roentgen b75cab
         * this keeps us within a-z boundaries
roentgen b75cab
         */
roentgen b75cab
	fpnt[1] = (char)((fnum - lastTurn) / 26) + 'a';
roentgen b75cab
	/* 
roentgen b75cab
         * cycle last letter every file, from a-z, then repeat
roentgen b75cab
         */
roentgen b75cab
	fpnt[2] = (char)(fnum % 26) + 'a';
roentgen b75cab
	fnum++;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
tiffcp(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	uint16 bitspersample, samplesperpixel, compression, shortv, *shortav;
roentgen b75cab
	uint32 w, l;
roentgen b75cab
	float floatv;
roentgen b75cab
	char *stringv;
roentgen b75cab
	uint32 longv;
roentgen b75cab
roentgen b75cab
	CopyField(TIFFTAG_SUBFILETYPE, longv);
roentgen b75cab
	CopyField(TIFFTAG_TILEWIDTH, w);
roentgen b75cab
	CopyField(TIFFTAG_TILELENGTH, l);
roentgen b75cab
	CopyField(TIFFTAG_IMAGEWIDTH, w);
roentgen b75cab
	CopyField(TIFFTAG_IMAGELENGTH, l);
roentgen b75cab
	CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);
roentgen b75cab
	CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
roentgen b75cab
	CopyField(TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	if (compression == COMPRESSION_JPEG) {
roentgen b75cab
		uint32 count = 0;
roentgen b75cab
		void *table = NULL;
roentgen b75cab
		if (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table)
roentgen b75cab
		    && count > 0 && table) {
roentgen b75cab
		    TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
        CopyField(TIFFTAG_PHOTOMETRIC, shortv);
roentgen b75cab
	CopyField(TIFFTAG_PREDICTOR, shortv);
roentgen b75cab
	CopyField(TIFFTAG_THRESHHOLDING, shortv);
roentgen b75cab
	CopyField(TIFFTAG_FILLORDER, shortv);
roentgen b75cab
	CopyField(TIFFTAG_ORIENTATION, shortv);
roentgen b75cab
	CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
roentgen b75cab
	CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
roentgen b75cab
	CopyField(TIFFTAG_XRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_GROUP3OPTIONS, longv);
roentgen b75cab
	CopyField(TIFFTAG_GROUP4OPTIONS, longv);
roentgen b75cab
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
roentgen b75cab
	CopyField(TIFFTAG_PLANARCONFIG, shortv);
roentgen b75cab
	CopyField(TIFFTAG_ROWSPERSTRIP, longv);
roentgen b75cab
	CopyField(TIFFTAG_XPOSITION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YPOSITION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_IMAGEDEPTH, longv);
roentgen b75cab
	CopyField(TIFFTAG_TILEDEPTH, longv);
roentgen b75cab
	CopyField(TIFFTAG_SAMPLEFORMAT, shortv);
roentgen b75cab
	CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);
roentgen b75cab
	{ uint16 *red, *green, *blue;
roentgen b75cab
	  CopyField3(TIFFTAG_COLORMAP, red, green, blue);
roentgen b75cab
	}
roentgen b75cab
	{ uint16 shortv2;
roentgen b75cab
	  CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);
roentgen b75cab
	}
roentgen b75cab
	CopyField(TIFFTAG_ARTIST, stringv);
roentgen b75cab
	CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
roentgen b75cab
	CopyField(TIFFTAG_MAKE, stringv);
roentgen b75cab
	CopyField(TIFFTAG_MODEL, stringv);
roentgen b75cab
	CopyField(TIFFTAG_SOFTWARE, stringv);
roentgen b75cab
	CopyField(TIFFTAG_DATETIME, stringv);
roentgen b75cab
	CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
roentgen b75cab
	CopyField(TIFFTAG_PAGENAME, stringv);
roentgen b75cab
	CopyField(TIFFTAG_DOCUMENTNAME, stringv);
roentgen b75cab
	CopyField(TIFFTAG_BADFAXLINES, longv);
roentgen b75cab
	CopyField(TIFFTAG_CLEANFAXDATA, longv);
roentgen b75cab
	CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv);
roentgen b75cab
	CopyField(TIFFTAG_FAXRECVPARAMS, longv);
roentgen b75cab
	CopyField(TIFFTAG_FAXRECVTIME, longv);
roentgen b75cab
	CopyField(TIFFTAG_FAXSUBADDRESS, stringv);
roentgen b75cab
	CopyField(TIFFTAG_FAXDCS, stringv);
roentgen b75cab
	if (TIFFIsTiled(in))
roentgen b75cab
		return (cpTiles(in, out));
roentgen b75cab
	else
roentgen b75cab
		return (cpStrips(in, out));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cpStrips(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	tmsize_t bufsize  = TIFFStripSize(in);
roentgen b75cab
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);
roentgen b75cab
roentgen b75cab
	if (buf) {
roentgen b75cab
		tstrip_t s, ns = TIFFNumberOfStrips(in);
roentgen b75cab
		uint64 *bytecounts;
roentgen b75cab
roentgen b75cab
		if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) {
roentgen b75cab
			fprintf(stderr, "tiffsplit: strip byte counts are missing\n");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		for (s = 0; s < ns; s++) {
roentgen b75cab
			if (bytecounts[s] > (uint64)bufsize) {
roentgen b75cab
				buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]);
roentgen b75cab
				if (!buf)
roentgen b75cab
					return (0);
roentgen b75cab
				bufsize = (tmsize_t)bytecounts[s];
roentgen b75cab
			}
roentgen b75cab
			if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 ||
roentgen b75cab
			    TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) {
roentgen b75cab
				_TIFFfree(buf);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		_TIFFfree(buf);
roentgen b75cab
		return (1);
roentgen b75cab
	}
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cpTiles(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	tmsize_t bufsize = TIFFTileSize(in);
roentgen b75cab
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);
roentgen b75cab
roentgen b75cab
	if (buf) {
roentgen b75cab
		ttile_t t, nt = TIFFNumberOfTiles(in);
roentgen b75cab
		uint64 *bytecounts;
roentgen b75cab
roentgen b75cab
		if (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {
roentgen b75cab
			fprintf(stderr, "tiffsplit: tile byte counts are missing\n");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		for (t = 0; t < nt; t++) {
roentgen b75cab
			if (bytecounts[t] > (uint64) bufsize) {
roentgen b75cab
				buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);
roentgen b75cab
				if (!buf)
roentgen b75cab
					return (0);
roentgen b75cab
				bufsize = (tmsize_t)bytecounts[t];
roentgen b75cab
			}
roentgen b75cab
			if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||
roentgen b75cab
			    TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {
roentgen b75cab
				_TIFFfree(buf);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		_TIFFfree(buf);
roentgen b75cab
		return (1);
roentgen b75cab
	}
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */