kusano 7d535a
/* $Id: tiffcmp.c,v 1.16 2010-03-10 18:56:50 bfriesen Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1988-1997 Sam Leffler
kusano 7d535a
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
kusano 7d535a
 *
kusano 7d535a
 * Permission to use, copy, modify, distribute, and sell this software and 
kusano 7d535a
 * its documentation for any purpose is hereby granted without fee, provided
kusano 7d535a
 * that (i) the above copyright notices and this permission notice appear in
kusano 7d535a
 * all copies of the software and related documentation, and (ii) the names of
kusano 7d535a
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
kusano 7d535a
 * publicity relating to the software without the specific, prior written
kusano 7d535a
 * permission of Sam Leffler and Silicon Graphics.
kusano 7d535a
 * 
kusano 7d535a
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
kusano 7d535a
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
kusano 7d535a
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
kusano 7d535a
 * 
kusano 7d535a
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
kusano 7d535a
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
kusano 7d535a
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
kusano 7d535a
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
kusano 7d535a
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
kusano 7d535a
 * OF THIS SOFTWARE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "tif_config.h"
kusano 7d535a
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <stdlib.h></stdlib.h>
kusano 7d535a
#include <string.h></string.h>
kusano 7d535a
#include <math.h></math.h>
kusano 7d535a
kusano 7d535a
#ifdef HAVE_UNISTD_H
kusano 7d535a
# include <unistd.h></unistd.h>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef NEED_LIBPORT
kusano 7d535a
# include "libport.h"
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#include "tiffio.h"
kusano 7d535a
kusano 7d535a
#ifndef HAVE_GETOPT
kusano 7d535a
extern int getopt(int, char**, char*);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
static	int stopondiff = 1;
kusano 7d535a
static	int stoponfirsttag = 1;
kusano 7d535a
static	uint16 bitspersample = 1;
kusano 7d535a
static	uint16 samplesperpixel = 1;
kusano 7d535a
static	uint16 sampleformat = SAMPLEFORMAT_UINT;
kusano 7d535a
static	uint32 imagewidth;
kusano 7d535a
static	uint32 imagelength;
kusano 7d535a
kusano 7d535a
static	void usage(void);
kusano 7d535a
static	int tiffcmp(TIFF*, TIFF*);
kusano 7d535a
static	int cmptags(TIFF*, TIFF*);
kusano 7d535a
static	int ContigCompare(int, uint32, unsigned char*, unsigned char*, tsize_t);
kusano 7d535a
static	int SeparateCompare(int, int, uint32, unsigned char*, unsigned char*);
kusano 7d535a
static	void PrintIntDiff(uint32, int, uint32, uint32, uint32);
kusano 7d535a
static	void PrintFloatDiff(uint32, int, uint32, double, double);
kusano 7d535a
kusano 7d535a
static	void leof(const char*, uint32, int);
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
main(int argc, char* argv[])
kusano 7d535a
{
kusano 7d535a
	TIFF *tif1, *tif2;
kusano 7d535a
	int c, dirnum;
kusano 7d535a
	extern int optind;
kusano 7d535a
	extern char* optarg;
kusano 7d535a
kusano 7d535a
	while ((c = getopt(argc, argv, "ltz:")) != -1)
kusano 7d535a
		switch (c) {
kusano 7d535a
		case 'l':
kusano 7d535a
			stopondiff = 0;
kusano 7d535a
			break;
kusano 7d535a
		case 'z':
kusano 7d535a
			stopondiff = atoi(optarg);
kusano 7d535a
			break;
kusano 7d535a
		case 't':
kusano 7d535a
			stoponfirsttag = 0;
kusano 7d535a
			break;
kusano 7d535a
		case '?':
kusano 7d535a
			usage();
kusano 7d535a
			/*NOTREACHED*/
kusano 7d535a
		}
kusano 7d535a
	if (argc - optind < 2)
kusano 7d535a
		usage();
kusano 7d535a
	tif1 = TIFFOpen(argv[optind], "r");
kusano 7d535a
	if (tif1 == NULL)
kusano 7d535a
		return (-1);
kusano 7d535a
	tif2 = TIFFOpen(argv[optind+1], "r");
kusano 7d535a
	if (tif2 == NULL)
kusano 7d535a
		return (-2);
kusano 7d535a
	dirnum = 0;
kusano 7d535a
	while (tiffcmp(tif1, tif2)) {
kusano 7d535a
		if (!TIFFReadDirectory(tif1)) {
kusano 7d535a
			if (!TIFFReadDirectory(tif2))
kusano 7d535a
				break;
kusano 7d535a
			printf("No more directories for %s\n",
kusano 7d535a
			    TIFFFileName(tif1));
kusano 7d535a
			return (1);
kusano 7d535a
		} else if (!TIFFReadDirectory(tif2)) {
kusano 7d535a
			printf("No more directories for %s\n",
kusano 7d535a
			    TIFFFileName(tif2));
kusano 7d535a
			return (1);
kusano 7d535a
		}
kusano 7d535a
		printf("Directory %d:\n", ++dirnum);
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	TIFFClose(tif1);
kusano 7d535a
	TIFFClose(tif2);
kusano 7d535a
	return (0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
char* stuff[] = {
kusano 7d535a
"usage: tiffcmp [options] file1 file2",
kusano 7d535a
"where options are:",
kusano 7d535a
" -l		list each byte of image data that differs between the files",
kusano 7d535a
" -z #		list specified number of bytes that differs between the files",
kusano 7d535a
" -t		ignore any differences in directory tags",
kusano 7d535a
NULL
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
usage(void)
kusano 7d535a
{
kusano 7d535a
	char buf[BUFSIZ];
kusano 7d535a
	int i;
kusano 7d535a
kusano 7d535a
	setbuf(stderr, buf);
kusano 7d535a
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
kusano 7d535a
	for (i = 0; stuff[i] != NULL; i++)
kusano 7d535a
		fprintf(stderr, "%s\n", stuff[i]);
kusano 7d535a
	exit(-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#define	checkEOF(tif, row, sample) { \
kusano 7d535a
	leof(TIFFFileName(tif), row, sample); \
kusano 7d535a
	goto bad; \
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static	int CheckShortTag(TIFF*, TIFF*, int, char*);
kusano 7d535a
static	int CheckShort2Tag(TIFF*, TIFF*, int, char*);
kusano 7d535a
static	int CheckShortArrayTag(TIFF*, TIFF*, int, char*);
kusano 7d535a
static	int CheckLongTag(TIFF*, TIFF*, int, char*);
kusano 7d535a
static	int CheckFloatTag(TIFF*, TIFF*, int, char*);
kusano 7d535a
static	int CheckStringTag(TIFF*, TIFF*, int, char*);
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
tiffcmp(TIFF* tif1, TIFF* tif2)
kusano 7d535a
{
kusano 7d535a
	uint16 config1, config2;
kusano 7d535a
	tsize_t size1;
kusano 7d535a
	uint32 row;
kusano 7d535a
	tsample_t s;
kusano 7d535a
	unsigned char *buf1, *buf2;
kusano 7d535a
kusano 7d535a
	if (!CheckShortTag(tif1, tif2, TIFFTAG_BITSPERSAMPLE, "BitsPerSample"))
kusano 7d535a
		return (0);
kusano 7d535a
	if (!CheckShortTag(tif1, tif2, TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel"))
kusano 7d535a
		return (0);
kusano 7d535a
	if (!CheckLongTag(tif1, tif2, TIFFTAG_IMAGEWIDTH, "ImageWidth"))
kusano 7d535a
		return (0);
kusano 7d535a
	if (!cmptags(tif1, tif2))
kusano 7d535a
		return (1);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_BITSPERSAMPLE, &bitspersample);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_SAMPLEFORMAT, &sampleformat);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_IMAGEWIDTH, &imagewidth);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_IMAGELENGTH, &imagelength);
kusano 7d535a
	(void) TIFFGetField(tif1, TIFFTAG_PLANARCONFIG, &config1);
kusano 7d535a
	(void) TIFFGetField(tif2, TIFFTAG_PLANARCONFIG, &config2);
kusano 7d535a
	buf1 = (unsigned char *)_TIFFmalloc(size1 = TIFFScanlineSize(tif1));
kusano 7d535a
	buf2 = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif2));
kusano 7d535a
	if (buf1 == NULL || buf2 == NULL) {
kusano 7d535a
		fprintf(stderr, "No space for scanline buffers\n");
kusano 7d535a
		exit(-1);
kusano 7d535a
	}
kusano 7d535a
	if (config1 != config2 && bitspersample != 8 && samplesperpixel > 1) {
kusano 7d535a
		fprintf(stderr,
kusano 7d535a
"Can't handle different planar configuration w/ different bits/sample\n");
kusano 7d535a
		goto bad;
kusano 7d535a
	}
kusano 7d535a
#define	pack(a,b)	((a)<<8)|(b)
kusano 7d535a
	switch (pack(config1, config2)) {
kusano 7d535a
	case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG):
kusano 7d535a
		for (row = 0; row < imagelength; row++) {
kusano 7d535a
			if (TIFFReadScanline(tif2, buf2, row, 0) < 0)
kusano 7d535a
				checkEOF(tif2, row, -1)
kusano 7d535a
			for (s = 0; s < samplesperpixel; s++) {
kusano 7d535a
				if (TIFFReadScanline(tif1, buf1, row, s) < 0)
kusano 7d535a
					checkEOF(tif1, row, s)
kusano 7d535a
				if (SeparateCompare(1, s, row, buf2, buf1) < 0)
kusano 7d535a
					goto bad1;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
		break;
kusano 7d535a
	case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE):
kusano 7d535a
		for (row = 0; row < imagelength; row++) {
kusano 7d535a
			if (TIFFReadScanline(tif1, buf1, row, 0) < 0)
kusano 7d535a
				checkEOF(tif1, row, -1)
kusano 7d535a
			for (s = 0; s < samplesperpixel; s++) {
kusano 7d535a
				if (TIFFReadScanline(tif2, buf2, row, s) < 0)
kusano 7d535a
					checkEOF(tif2, row, s)
kusano 7d535a
				if (SeparateCompare(0, s, row, buf1, buf2) < 0)
kusano 7d535a
					goto bad1;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
		break;
kusano 7d535a
	case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE):
kusano 7d535a
		for (s = 0; s < samplesperpixel; s++)
kusano 7d535a
			for (row = 0; row < imagelength; row++) {
kusano 7d535a
				if (TIFFReadScanline(tif1, buf1, row, s) < 0)
kusano 7d535a
					checkEOF(tif1, row, s)
kusano 7d535a
				if (TIFFReadScanline(tif2, buf2, row, s) < 0)
kusano 7d535a
					checkEOF(tif2, row, s)
kusano 7d535a
				if (ContigCompare(s, row, buf1, buf2, size1) < 0)
kusano 7d535a
					goto bad1;
kusano 7d535a
			}
kusano 7d535a
		break;
kusano 7d535a
	case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG):
kusano 7d535a
		for (row = 0; row < imagelength; row++) {
kusano 7d535a
			if (TIFFReadScanline(tif1, buf1, row, 0) < 0)
kusano 7d535a
				checkEOF(tif1, row, -1)
kusano 7d535a
			if (TIFFReadScanline(tif2, buf2, row, 0) < 0)
kusano 7d535a
				checkEOF(tif2, row, -1)
kusano 7d535a
			if (ContigCompare(-1, row, buf1, buf2, size1) < 0)
kusano 7d535a
				goto bad1;
kusano 7d535a
		}
kusano 7d535a
		break;
kusano 7d535a
	}
kusano 7d535a
	if (buf1) _TIFFfree(buf1);
kusano 7d535a
	if (buf2) _TIFFfree(buf2);
kusano 7d535a
	return (1);
kusano 7d535a
bad:
kusano 7d535a
	if (stopondiff)
kusano 7d535a
		exit(1);
kusano 7d535a
bad1:
kusano 7d535a
	if (buf1) _TIFFfree(buf1);
kusano 7d535a
	if (buf2) _TIFFfree(buf2);
kusano 7d535a
	return (0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#define	CmpShortField(tag, name) \
kusano 7d535a
	if (!CheckShortTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
#define	CmpShortField2(tag, name) \
kusano 7d535a
	if (!CheckShort2Tag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
#define	CmpLongField(tag, name) \
kusano 7d535a
	if (!CheckLongTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
#define	CmpFloatField(tag, name) \
kusano 7d535a
	if (!CheckFloatTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
#define	CmpStringField(tag, name) \
kusano 7d535a
	if (!CheckStringTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
#define	CmpShortArrayField(tag, name) \
kusano 7d535a
	if (!CheckShortArrayTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
cmptags(TIFF* tif1, TIFF* tif2)
kusano 7d535a
{
kusano 7d535a
	CmpLongField(TIFFTAG_SUBFILETYPE,	"SubFileType");
kusano 7d535a
	CmpLongField(TIFFTAG_IMAGEWIDTH,	"ImageWidth");
kusano 7d535a
	CmpLongField(TIFFTAG_IMAGELENGTH,	"ImageLength");
kusano 7d535a
	CmpShortField(TIFFTAG_BITSPERSAMPLE,	"BitsPerSample");
kusano 7d535a
	CmpShortField(TIFFTAG_COMPRESSION,	"Compression");
kusano 7d535a
	CmpShortField(TIFFTAG_PREDICTOR,	"Predictor");
kusano 7d535a
	CmpShortField(TIFFTAG_PHOTOMETRIC,	"PhotometricInterpretation");
kusano 7d535a
	CmpShortField(TIFFTAG_THRESHHOLDING,	"Thresholding");
kusano 7d535a
	CmpShortField(TIFFTAG_FILLORDER,	"FillOrder");
kusano 7d535a
	CmpShortField(TIFFTAG_ORIENTATION,	"Orientation");
kusano 7d535a
	CmpShortField(TIFFTAG_SAMPLESPERPIXEL,	"SamplesPerPixel");
kusano 7d535a
	CmpShortField(TIFFTAG_MINSAMPLEVALUE,	"MinSampleValue");
kusano 7d535a
	CmpShortField(TIFFTAG_MAXSAMPLEVALUE,	"MaxSampleValue");
kusano 7d535a
	CmpShortField(TIFFTAG_SAMPLEFORMAT,	"SampleFormat");
kusano 7d535a
	CmpFloatField(TIFFTAG_XRESOLUTION,	"XResolution");
kusano 7d535a
	CmpFloatField(TIFFTAG_YRESOLUTION,	"YResolution");
kusano 7d535a
	CmpLongField(TIFFTAG_GROUP3OPTIONS,	"Group3Options");
kusano 7d535a
	CmpLongField(TIFFTAG_GROUP4OPTIONS,	"Group4Options");
kusano 7d535a
	CmpShortField(TIFFTAG_RESOLUTIONUNIT,	"ResolutionUnit");
kusano 7d535a
	CmpShortField(TIFFTAG_PLANARCONFIG,	"PlanarConfiguration");
kusano 7d535a
	CmpLongField(TIFFTAG_ROWSPERSTRIP,	"RowsPerStrip");
kusano 7d535a
	CmpFloatField(TIFFTAG_XPOSITION,	"XPosition");
kusano 7d535a
	CmpFloatField(TIFFTAG_YPOSITION,	"YPosition");
kusano 7d535a
	CmpShortField(TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit");
kusano 7d535a
	CmpShortField(TIFFTAG_COLORRESPONSEUNIT, "ColorResponseUnit");
kusano 7d535a
#ifdef notdef
kusano 7d535a
	{ uint16 *graycurve;
kusano 7d535a
	  CmpField(TIFFTAG_GRAYRESPONSECURVE, graycurve);
kusano 7d535a
	}
kusano 7d535a
	{ uint16 *red, *green, *blue;
kusano 7d535a
	  CmpField3(TIFFTAG_COLORRESPONSECURVE, red, green, blue);
kusano 7d535a
	}
kusano 7d535a
	{ uint16 *red, *green, *blue;
kusano 7d535a
	  CmpField3(TIFFTAG_COLORMAP, red, green, blue);
kusano 7d535a
	}
kusano 7d535a
#endif
kusano 7d535a
	CmpShortField2(TIFFTAG_PAGENUMBER,	"PageNumber");
kusano 7d535a
	CmpStringField(TIFFTAG_ARTIST,		"Artist");
kusano 7d535a
	CmpStringField(TIFFTAG_IMAGEDESCRIPTION,"ImageDescription");
kusano 7d535a
	CmpStringField(TIFFTAG_MAKE,		"Make");
kusano 7d535a
	CmpStringField(TIFFTAG_MODEL,		"Model");
kusano 7d535a
	CmpStringField(TIFFTAG_SOFTWARE,	"Software");
kusano 7d535a
	CmpStringField(TIFFTAG_DATETIME,	"DateTime");
kusano 7d535a
	CmpStringField(TIFFTAG_HOSTCOMPUTER,	"HostComputer");
kusano 7d535a
	CmpStringField(TIFFTAG_PAGENAME,	"PageName");
kusano 7d535a
	CmpStringField(TIFFTAG_DOCUMENTNAME,	"DocumentName");
kusano 7d535a
	CmpShortField(TIFFTAG_MATTEING,		"Matteing");
kusano 7d535a
	CmpShortArrayField(TIFFTAG_EXTRASAMPLES,"ExtraSamples");
kusano 7d535a
	return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
ContigCompare(int sample, uint32 row,
kusano 7d535a
	      unsigned char* p1, unsigned char* p2, tsize_t size)
kusano 7d535a
{
kusano 7d535a
    uint32 pix;
kusano 7d535a
    int ppb = 8 / bitspersample;
kusano 7d535a
    int	 samples_to_test;
kusano 7d535a
kusano 7d535a
    if (memcmp(p1, p2, size) == 0)
kusano 7d535a
        return 0;
kusano 7d535a
kusano 7d535a
    samples_to_test = (sample == -1) ? samplesperpixel : 1;
kusano 7d535a
kusano 7d535a
    switch (bitspersample) {
kusano 7d535a
      case 1: case 2: case 4: case 8: 
kusano 7d535a
      {
kusano 7d535a
          unsigned char *pix1 = p1, *pix2 = p2;
kusano 7d535a
kusano 7d535a
          for (pix = 0; pix < imagewidth; pix += ppb) {
kusano 7d535a
              int		s;
kusano 7d535a
kusano 7d535a
              for(s = 0; s < samples_to_test; s++) {
kusano 7d535a
                  if (*pix1 != *pix2) {
kusano 7d535a
                      if( sample == -1 )
kusano 7d535a
                          PrintIntDiff(row, s, pix, *pix1, *pix2);
kusano 7d535a
                      else
kusano 7d535a
                          PrintIntDiff(row, sample, pix, *pix1, *pix2);
kusano 7d535a
                  }
kusano 7d535a
kusano 7d535a
                  pix1++;
kusano 7d535a
                  pix2++;
kusano 7d535a
              }
kusano 7d535a
          }
kusano 7d535a
          break;
kusano 7d535a
      }
kusano 7d535a
      case 16: 
kusano 7d535a
      {
kusano 7d535a
          uint16 *pix1 = (uint16 *)p1, *pix2 = (uint16 *)p2;
kusano 7d535a
kusano 7d535a
          for (pix = 0; pix < imagewidth; pix++) {
kusano 7d535a
              int	s;
kusano 7d535a
kusano 7d535a
              for(s = 0; s < samples_to_test; s++) {
kusano 7d535a
                  if (*pix1 != *pix2)
kusano 7d535a
                      PrintIntDiff(row, sample, pix, *pix1, *pix2);
kusano 7d535a
                        
kusano 7d535a
                  pix1++;
kusano 7d535a
                  pix2++;
kusano 7d535a
              }
kusano 7d535a
          }
kusano 7d535a
          break;
kusano 7d535a
      }
kusano 7d535a
      case 32: 
kusano 7d535a
	if (sampleformat == SAMPLEFORMAT_UINT
kusano 7d535a
	    || sampleformat == SAMPLEFORMAT_INT) {
kusano 7d535a
		uint32 *pix1 = (uint32 *)p1, *pix2 = (uint32 *)p2;
kusano 7d535a
kusano 7d535a
		for (pix = 0; pix < imagewidth; pix++) {
kusano 7d535a
			int	s;
kusano 7d535a
kusano 7d535a
			for(s = 0; s < samples_to_test; s++) {
kusano 7d535a
				if (*pix1 != *pix2) {
kusano 7d535a
					PrintIntDiff(row, sample, pix,
kusano 7d535a
						     *pix1, *pix2);
kusano 7d535a
				}
kusano 7d535a
                        
kusano 7d535a
				pix1++;
kusano 7d535a
				pix2++;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
	} else if (sampleformat == SAMPLEFORMAT_IEEEFP) {
kusano 7d535a
		float *pix1 = (float *)p1, *pix2 = (float *)p2;
kusano 7d535a
kusano 7d535a
		for (pix = 0; pix < imagewidth; pix++) {
kusano 7d535a
			int	s;
kusano 7d535a
kusano 7d535a
			for(s = 0; s < samples_to_test; s++) {
kusano 7d535a
				if (fabs(*pix1 - *pix2) < 0.000000000001) {
kusano 7d535a
					PrintFloatDiff(row, sample, pix,
kusano 7d535a
						       *pix1, *pix2);
kusano 7d535a
				}
kusano 7d535a
                        
kusano 7d535a
				pix1++;
kusano 7d535a
				pix2++;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
	} else {
kusano 7d535a
		  fprintf(stderr, "Sample format %d is not supported.\n",
kusano 7d535a
			  sampleformat);
kusano 7d535a
		  return -1;
kusano 7d535a
	}
kusano 7d535a
        break;
kusano 7d535a
      default:
kusano 7d535a
	fprintf(stderr, "Bit depth %d is not supported.\n", bitspersample);
kusano 7d535a
	return -1;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
PrintIntDiff(uint32 row, int sample, uint32 pix, uint32 w1, uint32 w2)
kusano 7d535a
{
kusano 7d535a
	if (sample < 0)
kusano 7d535a
		sample = 0;
kusano 7d535a
	switch (bitspersample) {
kusano 7d535a
	case 1:
kusano 7d535a
	case 2:
kusano 7d535a
	case 4:
kusano 7d535a
	    {
kusano 7d535a
		int32 mask1, mask2, s;
kusano 7d535a
kusano 7d535a
		mask1 =  ~((-1) << bitspersample);
kusano 7d535a
		s = (8 - bitspersample);
kusano 7d535a
		mask2 = mask1 << s;
kusano 7d535a
		for (; mask2 && pix < imagewidth;
kusano 7d535a
		     mask2 >>= bitspersample, s -= bitspersample, pix++) {
kusano 7d535a
			if ((w1 & mask2) ^ (w2 & mask2)) {
kusano 7d535a
				printf(
kusano 7d535a
			"Scanline %lu, pixel %lu, sample %d: %01x %01x\n",
kusano 7d535a
	    				(unsigned long) row,
kusano 7d535a
					(unsigned long) pix,
kusano 7d535a
					sample,
kusano 7d535a
					(unsigned int)((w1 >> s) & mask1),
kusano 7d535a
					(unsigned int)((w2 >> s) & mask1));
kusano 7d535a
				if (--stopondiff == 0)
kusano 7d535a
					exit(1);
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
		break;
kusano 7d535a
	    }
kusano 7d535a
	case 8: 
kusano 7d535a
		printf("Scanline %lu, pixel %lu, sample %d: %02x %02x\n",
kusano 7d535a
		       (unsigned long) row, (unsigned long) pix, sample,
kusano 7d535a
		       (unsigned int) w1, (unsigned int) w2);
kusano 7d535a
		if (--stopondiff == 0)
kusano 7d535a
			exit(1);
kusano 7d535a
		break;
kusano 7d535a
	case 16:
kusano 7d535a
		printf("Scanline %lu, pixel %lu, sample %d: %04x %04x\n",
kusano 7d535a
		    (unsigned long) row, (unsigned long) pix, sample,
kusano 7d535a
		    (unsigned int) w1, (unsigned int) w2);
kusano 7d535a
		if (--stopondiff == 0)
kusano 7d535a
			exit(1);
kusano 7d535a
		break;
kusano 7d535a
	case 32:
kusano 7d535a
		printf("Scanline %lu, pixel %lu, sample %d: %08x %08x\n",
kusano 7d535a
		    (unsigned long) row, (unsigned long) pix, sample,
kusano 7d535a
		    (unsigned int) w1, (unsigned int) w2);
kusano 7d535a
		if (--stopondiff == 0)
kusano 7d535a
			exit(1);
kusano 7d535a
		break;
kusano 7d535a
	default:
kusano 7d535a
		break;
kusano 7d535a
	}
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
PrintFloatDiff(uint32 row, int sample, uint32 pix, double w1, double w2)
kusano 7d535a
{
kusano 7d535a
	if (sample < 0)
kusano 7d535a
		sample = 0;
kusano 7d535a
	switch (bitspersample) {
kusano 7d535a
	case 32: 
kusano 7d535a
		printf("Scanline %lu, pixel %lu, sample %d: %g %g\n",
kusano 7d535a
		    (long) row, (long) pix, sample, w1, w2);
kusano 7d535a
		if (--stopondiff == 0)
kusano 7d535a
			exit(1);
kusano 7d535a
		break;
kusano 7d535a
	default:
kusano 7d535a
		break;
kusano 7d535a
	}
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
SeparateCompare(int reversed, int sample, uint32 row,
kusano 7d535a
		unsigned char* cp1, unsigned char* p2)
kusano 7d535a
{
kusano 7d535a
	uint32 npixels = imagewidth;
kusano 7d535a
	int pixel;
kusano 7d535a
kusano 7d535a
	cp1 += sample;
kusano 7d535a
	for (pixel = 0; npixels-- > 0; pixel++, cp1 += samplesperpixel, p2++) {
kusano 7d535a
		if (*cp1 != *p2) {
kusano 7d535a
			printf("Scanline %lu, pixel %lu, sample %ld: ",
kusano 7d535a
			    (long) row, (long) pixel, (long) sample);
kusano 7d535a
			if (reversed)
kusano 7d535a
				printf("%02x %02x\n", *p2, *cp1);
kusano 7d535a
			else
kusano 7d535a
				printf("%02x %02x\n", *cp1, *p2);
kusano 7d535a
			if (--stopondiff == 0)
kusano 7d535a
				exit(1);
kusano 7d535a
		}
kusano 7d535a
	}
kusano 7d535a
kusano 7d535a
	return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
checkTag(TIFF* tif1, TIFF* tif2, int tag, char* name, void* p1, void* p2)
kusano 7d535a
{
kusano 7d535a
kusano 7d535a
	if (TIFFGetField(tif1, tag, p1)) {
kusano 7d535a
		if (!TIFFGetField(tif2, tag, p2)) {
kusano 7d535a
			printf("%s tag appears only in %s\n",
kusano 7d535a
			    name, TIFFFileName(tif1));
kusano 7d535a
			return (0);
kusano 7d535a
		}
kusano 7d535a
		return (1);
kusano 7d535a
	} else if (TIFFGetField(tif2, tag, p2)) {
kusano 7d535a
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
kusano 7d535a
		return (0);
kusano 7d535a
	}
kusano 7d535a
	return (-1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#define	CHECK(cmp, fmt) {				\
kusano 7d535a
	switch (checkTag(tif1,tif2,tag,name,&v1,&v2)) {	\
kusano 7d535a
	case 1:	if (cmp)				\
kusano 7d535a
	case -1:	return (1);			\
kusano 7d535a
		printf(fmt, name, v1, v2);		\
kusano 7d535a
	}						\
kusano 7d535a
	return (0);					\
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckShortTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	uint16 v1, v2;
kusano 7d535a
	CHECK(v1 == v2, "%s: %u %u\n");
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckShort2Tag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	uint16 v11, v12, v21, v22;
kusano 7d535a
kusano 7d535a
	if (TIFFGetField(tif1, tag, &v11, &v12)) {
kusano 7d535a
		if (!TIFFGetField(tif2, tag, &v21, &v22)) {
kusano 7d535a
			printf("%s tag appears only in %s\n",
kusano 7d535a
			    name, TIFFFileName(tif1));
kusano 7d535a
			return (0);
kusano 7d535a
		}
kusano 7d535a
		if (v11 == v21 && v12 == v22)
kusano 7d535a
			return (1);
kusano 7d535a
		printf("%s: <%u,%u> <%u,%u>\n", name, v11, v12, v21, v22);
kusano 7d535a
	} else if (TIFFGetField(tif2, tag, &v21, &v22))
kusano 7d535a
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
kusano 7d535a
	else
kusano 7d535a
		return (1);
kusano 7d535a
	return (0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckShortArrayTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	uint16 n1, *a1;
kusano 7d535a
	uint16 n2, *a2;
kusano 7d535a
kusano 7d535a
	if (TIFFGetField(tif1, tag, &n1, &a1)) {
kusano 7d535a
		if (!TIFFGetField(tif2, tag, &n2, &a2)) {
kusano 7d535a
			printf("%s tag appears only in %s\n",
kusano 7d535a
			    name, TIFFFileName(tif1));
kusano 7d535a
			return (0);
kusano 7d535a
		}
kusano 7d535a
		if (n1 == n2) {
kusano 7d535a
			char* sep;
kusano 7d535a
			uint16 i;
kusano 7d535a
kusano 7d535a
			if (memcmp(a1, a2, n1 * sizeof(uint16)) == 0)
kusano 7d535a
				return (1);
kusano 7d535a
			printf("%s: value mismatch, <%u:", name, n1);
kusano 7d535a
			sep = "";
kusano 7d535a
			for (i = 0; i < n1; i++)
kusano 7d535a
				printf("%s%u", sep, a1[i]), sep = ",";
kusano 7d535a
			printf("> and <%u: ", n2);
kusano 7d535a
			sep = "";
kusano 7d535a
			for (i = 0; i < n2; i++)
kusano 7d535a
				printf("%s%u", sep, a2[i]), sep = ",";
kusano 7d535a
			printf(">\n");
kusano 7d535a
		} else
kusano 7d535a
			printf("%s: %u items in %s, %u items in %s", name,
kusano 7d535a
			    n1, TIFFFileName(tif1),
kusano 7d535a
			    n2, TIFFFileName(tif2)
kusano 7d535a
			);
kusano 7d535a
	} else if (TIFFGetField(tif2, tag, &n2, &a2))
kusano 7d535a
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
kusano 7d535a
	else
kusano 7d535a
		return (1);
kusano 7d535a
	return (0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckLongTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	uint32 v1, v2;
kusano 7d535a
	CHECK(v1 == v2, "%s: %u %u\n");
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckFloatTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	float v1, v2;
kusano 7d535a
	CHECK(v1 == v2, "%s: %g %g\n");
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
CheckStringTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
kusano 7d535a
{
kusano 7d535a
	char *v1, *v2;
kusano 7d535a
	CHECK(strcmp(v1, v2) == 0, "%s: \"%s\" \"%s\"\n");
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
leof(const char* name, uint32 row, int s)
kusano 7d535a
{
kusano 7d535a
kusano 7d535a
	printf("%s: EOF at scanline %lu", name, (unsigned long)row);
kusano 7d535a
	if (s >= 0)
kusano 7d535a
		printf(", sample %d", s);
kusano 7d535a
	printf("\n");
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* vim: set ts=8 sts=8 sw=8 noet: */
kusano 7d535a
/*
kusano 7d535a
 * Local Variables:
kusano 7d535a
 * mode: c
kusano 7d535a
 * c-basic-offset: 8
kusano 7d535a
 * fill-column: 78
kusano 7d535a
 * End:
kusano 7d535a
 */