roentgen b75cab
/* $Id: tiffcmp.c,v 1.16 2010-03-10 18:56:50 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-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
#include <math.h></math.h>
roentgen b75cab
roentgen b75cab
#ifdef HAVE_UNISTD_H
roentgen b75cab
# include <unistd.h></unistd.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef NEED_LIBPORT
roentgen b75cab
# include "libport.h"
roentgen b75cab
#endif
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
static	int stopondiff = 1;
roentgen b75cab
static	int stoponfirsttag = 1;
roentgen b75cab
static	uint16 bitspersample = 1;
roentgen b75cab
static	uint16 samplesperpixel = 1;
roentgen b75cab
static	uint16 sampleformat = SAMPLEFORMAT_UINT;
roentgen b75cab
static	uint32 imagewidth;
roentgen b75cab
static	uint32 imagelength;
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
static	int tiffcmp(TIFF*, TIFF*);
roentgen b75cab
static	int cmptags(TIFF*, TIFF*);
roentgen b75cab
static	int ContigCompare(int, uint32, unsigned char*, unsigned char*, tsize_t);
roentgen b75cab
static	int SeparateCompare(int, int, uint32, unsigned char*, unsigned char*);
roentgen b75cab
static	void PrintIntDiff(uint32, int, uint32, uint32, uint32);
roentgen b75cab
static	void PrintFloatDiff(uint32, int, uint32, double, double);
roentgen b75cab
roentgen b75cab
static	void leof(const char*, uint32, int);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	TIFF *tif1, *tif2;
roentgen b75cab
	int c, dirnum;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char* optarg;
roentgen b75cab
roentgen b75cab
	while ((c = getopt(argc, argv, "ltz:")) != -1)
roentgen b75cab
		switch (c) {
roentgen b75cab
		case 'l':
roentgen b75cab
			stopondiff = 0;
roentgen b75cab
			break;
roentgen b75cab
		case 'z':
roentgen b75cab
			stopondiff = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 't':
roentgen b75cab
			stoponfirsttag = 0;
roentgen b75cab
			break;
roentgen b75cab
		case '?':
roentgen b75cab
			usage();
roentgen b75cab
			/*NOTREACHED*/
roentgen b75cab
		}
roentgen b75cab
	if (argc - optind < 2)
roentgen b75cab
		usage();
roentgen b75cab
	tif1 = TIFFOpen(argv[optind], "r");
roentgen b75cab
	if (tif1 == NULL)
roentgen b75cab
		return (-1);
roentgen b75cab
	tif2 = TIFFOpen(argv[optind+1], "r");
roentgen b75cab
	if (tif2 == NULL)
roentgen b75cab
		return (-2);
roentgen b75cab
	dirnum = 0;
roentgen b75cab
	while (tiffcmp(tif1, tif2)) {
roentgen b75cab
		if (!TIFFReadDirectory(tif1)) {
roentgen b75cab
			if (!TIFFReadDirectory(tif2))
roentgen b75cab
				break;
roentgen b75cab
			printf("No more directories for %s\n",
roentgen b75cab
			    TIFFFileName(tif1));
roentgen b75cab
			return (1);
roentgen b75cab
		} else if (!TIFFReadDirectory(tif2)) {
roentgen b75cab
			printf("No more directories for %s\n",
roentgen b75cab
			    TIFFFileName(tif2));
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
		printf("Directory %d:\n", ++dirnum);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFClose(tif1);
roentgen b75cab
	TIFFClose(tif2);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
char* stuff[] = {
roentgen b75cab
"usage: tiffcmp [options] file1 file2",
roentgen b75cab
"where options are:",
roentgen b75cab
" -l		list each byte of image data that differs between the files",
roentgen b75cab
" -z #		list specified number of bytes that differs between the files",
roentgen b75cab
" -t		ignore any differences in directory tags",
roentgen b75cab
NULL
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
usage(void)
roentgen b75cab
{
roentgen b75cab
	char buf[BUFSIZ];
roentgen b75cab
	int i;
roentgen b75cab
roentgen b75cab
	setbuf(stderr, buf);
roentgen b75cab
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
roentgen b75cab
	for (i = 0; stuff[i] != NULL; i++)
roentgen b75cab
		fprintf(stderr, "%s\n", stuff[i]);
roentgen b75cab
	exit(-1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	checkEOF(tif, row, sample) { \
roentgen b75cab
	leof(TIFFFileName(tif), row, sample); \
roentgen b75cab
	goto bad; \
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static	int CheckShortTag(TIFF*, TIFF*, int, char*);
roentgen b75cab
static	int CheckShort2Tag(TIFF*, TIFF*, int, char*);
roentgen b75cab
static	int CheckShortArrayTag(TIFF*, TIFF*, int, char*);
roentgen b75cab
static	int CheckLongTag(TIFF*, TIFF*, int, char*);
roentgen b75cab
static	int CheckFloatTag(TIFF*, TIFF*, int, char*);
roentgen b75cab
static	int CheckStringTag(TIFF*, TIFF*, int, char*);
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
tiffcmp(TIFF* tif1, TIFF* tif2)
roentgen b75cab
{
roentgen b75cab
	uint16 config1, config2;
roentgen b75cab
	tsize_t size1;
roentgen b75cab
	uint32 row;
roentgen b75cab
	tsample_t s;
roentgen b75cab
	unsigned char *buf1, *buf2;
roentgen b75cab
roentgen b75cab
	if (!CheckShortTag(tif1, tif2, TIFFTAG_BITSPERSAMPLE, "BitsPerSample"))
roentgen b75cab
		return (0);
roentgen b75cab
	if (!CheckShortTag(tif1, tif2, TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel"))
roentgen b75cab
		return (0);
roentgen b75cab
	if (!CheckLongTag(tif1, tif2, TIFFTAG_IMAGEWIDTH, "ImageWidth"))
roentgen b75cab
		return (0);
roentgen b75cab
	if (!cmptags(tif1, tif2))
roentgen b75cab
		return (1);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_BITSPERSAMPLE, &bitspersample);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_SAMPLEFORMAT, &sampleformat);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_IMAGEWIDTH, &imagewidth);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_IMAGELENGTH, &imagelength);
roentgen b75cab
	(void) TIFFGetField(tif1, TIFFTAG_PLANARCONFIG, &config1);
roentgen b75cab
	(void) TIFFGetField(tif2, TIFFTAG_PLANARCONFIG, &config2);
roentgen b75cab
	buf1 = (unsigned char *)_TIFFmalloc(size1 = TIFFScanlineSize(tif1));
roentgen b75cab
	buf2 = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif2));
roentgen b75cab
	if (buf1 == NULL || buf2 == NULL) {
roentgen b75cab
		fprintf(stderr, "No space for scanline buffers\n");
roentgen b75cab
		exit(-1);
roentgen b75cab
	}
roentgen b75cab
	if (config1 != config2 && bitspersample != 8 && samplesperpixel > 1) {
roentgen b75cab
		fprintf(stderr,
roentgen b75cab
"Can't handle different planar configuration w/ different bits/sample\n");
roentgen b75cab
		goto bad;
roentgen b75cab
	}
roentgen b75cab
#define	pack(a,b)	((a)<<8)|(b)
roentgen b75cab
	switch (pack(config1, config2)) {
roentgen b75cab
	case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG):
roentgen b75cab
		for (row = 0; row < imagelength; row++) {
roentgen b75cab
			if (TIFFReadScanline(tif2, buf2, row, 0) < 0)
roentgen b75cab
				checkEOF(tif2, row, -1)
roentgen b75cab
			for (s = 0; s < samplesperpixel; s++) {
roentgen b75cab
				if (TIFFReadScanline(tif1, buf1, row, s) < 0)
roentgen b75cab
					checkEOF(tif1, row, s)
roentgen b75cab
				if (SeparateCompare(1, s, row, buf2, buf1) < 0)
roentgen b75cab
					goto bad1;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE):
roentgen b75cab
		for (row = 0; row < imagelength; row++) {
roentgen b75cab
			if (TIFFReadScanline(tif1, buf1, row, 0) < 0)
roentgen b75cab
				checkEOF(tif1, row, -1)
roentgen b75cab
			for (s = 0; s < samplesperpixel; s++) {
roentgen b75cab
				if (TIFFReadScanline(tif2, buf2, row, s) < 0)
roentgen b75cab
					checkEOF(tif2, row, s)
roentgen b75cab
				if (SeparateCompare(0, s, row, buf1, buf2) < 0)
roentgen b75cab
					goto bad1;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE):
roentgen b75cab
		for (s = 0; s < samplesperpixel; s++)
roentgen b75cab
			for (row = 0; row < imagelength; row++) {
roentgen b75cab
				if (TIFFReadScanline(tif1, buf1, row, s) < 0)
roentgen b75cab
					checkEOF(tif1, row, s)
roentgen b75cab
				if (TIFFReadScanline(tif2, buf2, row, s) < 0)
roentgen b75cab
					checkEOF(tif2, row, s)
roentgen b75cab
				if (ContigCompare(s, row, buf1, buf2, size1) < 0)
roentgen b75cab
					goto bad1;
roentgen b75cab
			}
roentgen b75cab
		break;
roentgen b75cab
	case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG):
roentgen b75cab
		for (row = 0; row < imagelength; row++) {
roentgen b75cab
			if (TIFFReadScanline(tif1, buf1, row, 0) < 0)
roentgen b75cab
				checkEOF(tif1, row, -1)
roentgen b75cab
			if (TIFFReadScanline(tif2, buf2, row, 0) < 0)
roentgen b75cab
				checkEOF(tif2, row, -1)
roentgen b75cab
			if (ContigCompare(-1, row, buf1, buf2, size1) < 0)
roentgen b75cab
				goto bad1;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	if (buf1) _TIFFfree(buf1);
roentgen b75cab
	if (buf2) _TIFFfree(buf2);
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	if (stopondiff)
roentgen b75cab
		exit(1);
roentgen b75cab
bad1:
roentgen b75cab
	if (buf1) _TIFFfree(buf1);
roentgen b75cab
	if (buf2) _TIFFfree(buf2);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	CmpShortField(tag, name) \
roentgen b75cab
	if (!CheckShortTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
#define	CmpShortField2(tag, name) \
roentgen b75cab
	if (!CheckShort2Tag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
#define	CmpLongField(tag, name) \
roentgen b75cab
	if (!CheckLongTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
#define	CmpFloatField(tag, name) \
roentgen b75cab
	if (!CheckFloatTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
#define	CmpStringField(tag, name) \
roentgen b75cab
	if (!CheckStringTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
#define	CmpShortArrayField(tag, name) \
roentgen b75cab
	if (!CheckShortArrayTag(tif1, tif2, tag, name) && stoponfirsttag) return (0)
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cmptags(TIFF* tif1, TIFF* tif2)
roentgen b75cab
{
roentgen b75cab
	CmpLongField(TIFFTAG_SUBFILETYPE,	"SubFileType");
roentgen b75cab
	CmpLongField(TIFFTAG_IMAGEWIDTH,	"ImageWidth");
roentgen b75cab
	CmpLongField(TIFFTAG_IMAGELENGTH,	"ImageLength");
roentgen b75cab
	CmpShortField(TIFFTAG_BITSPERSAMPLE,	"BitsPerSample");
roentgen b75cab
	CmpShortField(TIFFTAG_COMPRESSION,	"Compression");
roentgen b75cab
	CmpShortField(TIFFTAG_PREDICTOR,	"Predictor");
roentgen b75cab
	CmpShortField(TIFFTAG_PHOTOMETRIC,	"PhotometricInterpretation");
roentgen b75cab
	CmpShortField(TIFFTAG_THRESHHOLDING,	"Thresholding");
roentgen b75cab
	CmpShortField(TIFFTAG_FILLORDER,	"FillOrder");
roentgen b75cab
	CmpShortField(TIFFTAG_ORIENTATION,	"Orientation");
roentgen b75cab
	CmpShortField(TIFFTAG_SAMPLESPERPIXEL,	"SamplesPerPixel");
roentgen b75cab
	CmpShortField(TIFFTAG_MINSAMPLEVALUE,	"MinSampleValue");
roentgen b75cab
	CmpShortField(TIFFTAG_MAXSAMPLEVALUE,	"MaxSampleValue");
roentgen b75cab
	CmpShortField(TIFFTAG_SAMPLEFORMAT,	"SampleFormat");
roentgen b75cab
	CmpFloatField(TIFFTAG_XRESOLUTION,	"XResolution");
roentgen b75cab
	CmpFloatField(TIFFTAG_YRESOLUTION,	"YResolution");
roentgen b75cab
	CmpLongField(TIFFTAG_GROUP3OPTIONS,	"Group3Options");
roentgen b75cab
	CmpLongField(TIFFTAG_GROUP4OPTIONS,	"Group4Options");
roentgen b75cab
	CmpShortField(TIFFTAG_RESOLUTIONUNIT,	"ResolutionUnit");
roentgen b75cab
	CmpShortField(TIFFTAG_PLANARCONFIG,	"PlanarConfiguration");
roentgen b75cab
	CmpLongField(TIFFTAG_ROWSPERSTRIP,	"RowsPerStrip");
roentgen b75cab
	CmpFloatField(TIFFTAG_XPOSITION,	"XPosition");
roentgen b75cab
	CmpFloatField(TIFFTAG_YPOSITION,	"YPosition");
roentgen b75cab
	CmpShortField(TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit");
roentgen b75cab
	CmpShortField(TIFFTAG_COLORRESPONSEUNIT, "ColorResponseUnit");
roentgen b75cab
#ifdef notdef
roentgen b75cab
	{ uint16 *graycurve;
roentgen b75cab
	  CmpField(TIFFTAG_GRAYRESPONSECURVE, graycurve);
roentgen b75cab
	}
roentgen b75cab
	{ uint16 *red, *green, *blue;
roentgen b75cab
	  CmpField3(TIFFTAG_COLORRESPONSECURVE, red, green, blue);
roentgen b75cab
	}
roentgen b75cab
	{ uint16 *red, *green, *blue;
roentgen b75cab
	  CmpField3(TIFFTAG_COLORMAP, red, green, blue);
roentgen b75cab
	}
roentgen b75cab
#endif
roentgen b75cab
	CmpShortField2(TIFFTAG_PAGENUMBER,	"PageNumber");
roentgen b75cab
	CmpStringField(TIFFTAG_ARTIST,		"Artist");
roentgen b75cab
	CmpStringField(TIFFTAG_IMAGEDESCRIPTION,"ImageDescription");
roentgen b75cab
	CmpStringField(TIFFTAG_MAKE,		"Make");
roentgen b75cab
	CmpStringField(TIFFTAG_MODEL,		"Model");
roentgen b75cab
	CmpStringField(TIFFTAG_SOFTWARE,	"Software");
roentgen b75cab
	CmpStringField(TIFFTAG_DATETIME,	"DateTime");
roentgen b75cab
	CmpStringField(TIFFTAG_HOSTCOMPUTER,	"HostComputer");
roentgen b75cab
	CmpStringField(TIFFTAG_PAGENAME,	"PageName");
roentgen b75cab
	CmpStringField(TIFFTAG_DOCUMENTNAME,	"DocumentName");
roentgen b75cab
	CmpShortField(TIFFTAG_MATTEING,		"Matteing");
roentgen b75cab
	CmpShortArrayField(TIFFTAG_EXTRASAMPLES,"ExtraSamples");
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
ContigCompare(int sample, uint32 row,
roentgen b75cab
	      unsigned char* p1, unsigned char* p2, tsize_t size)
roentgen b75cab
{
roentgen b75cab
    uint32 pix;
roentgen b75cab
    int ppb = 8 / bitspersample;
roentgen b75cab
    int	 samples_to_test;
roentgen b75cab
roentgen b75cab
    if (memcmp(p1, p2, size) == 0)
roentgen b75cab
        return 0;
roentgen b75cab
roentgen b75cab
    samples_to_test = (sample == -1) ? samplesperpixel : 1;
roentgen b75cab
roentgen b75cab
    switch (bitspersample) {
roentgen b75cab
      case 1: case 2: case 4: case 8: 
roentgen b75cab
      {
roentgen b75cab
          unsigned char *pix1 = p1, *pix2 = p2;
roentgen b75cab
roentgen b75cab
          for (pix = 0; pix < imagewidth; pix += ppb) {
roentgen b75cab
              int		s;
roentgen b75cab
roentgen b75cab
              for(s = 0; s < samples_to_test; s++) {
roentgen b75cab
                  if (*pix1 != *pix2) {
roentgen b75cab
                      if( sample == -1 )
roentgen b75cab
                          PrintIntDiff(row, s, pix, *pix1, *pix2);
roentgen b75cab
                      else
roentgen b75cab
                          PrintIntDiff(row, sample, pix, *pix1, *pix2);
roentgen b75cab
                  }
roentgen b75cab
roentgen b75cab
                  pix1++;
roentgen b75cab
                  pix2++;
roentgen b75cab
              }
roentgen b75cab
          }
roentgen b75cab
          break;
roentgen b75cab
      }
roentgen b75cab
      case 16: 
roentgen b75cab
      {
roentgen b75cab
          uint16 *pix1 = (uint16 *)p1, *pix2 = (uint16 *)p2;
roentgen b75cab
roentgen b75cab
          for (pix = 0; pix < imagewidth; pix++) {
roentgen b75cab
              int	s;
roentgen b75cab
roentgen b75cab
              for(s = 0; s < samples_to_test; s++) {
roentgen b75cab
                  if (*pix1 != *pix2)
roentgen b75cab
                      PrintIntDiff(row, sample, pix, *pix1, *pix2);
roentgen b75cab
                        
roentgen b75cab
                  pix1++;
roentgen b75cab
                  pix2++;
roentgen b75cab
              }
roentgen b75cab
          }
roentgen b75cab
          break;
roentgen b75cab
      }
roentgen b75cab
      case 32: 
roentgen b75cab
	if (sampleformat == SAMPLEFORMAT_UINT
roentgen b75cab
	    || sampleformat == SAMPLEFORMAT_INT) {
roentgen b75cab
		uint32 *pix1 = (uint32 *)p1, *pix2 = (uint32 *)p2;
roentgen b75cab
roentgen b75cab
		for (pix = 0; pix < imagewidth; pix++) {
roentgen b75cab
			int	s;
roentgen b75cab
roentgen b75cab
			for(s = 0; s < samples_to_test; s++) {
roentgen b75cab
				if (*pix1 != *pix2) {
roentgen b75cab
					PrintIntDiff(row, sample, pix,
roentgen b75cab
						     *pix1, *pix2);
roentgen b75cab
				}
roentgen b75cab
                        
roentgen b75cab
				pix1++;
roentgen b75cab
				pix2++;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	} else if (sampleformat == SAMPLEFORMAT_IEEEFP) {
roentgen b75cab
		float *pix1 = (float *)p1, *pix2 = (float *)p2;
roentgen b75cab
roentgen b75cab
		for (pix = 0; pix < imagewidth; pix++) {
roentgen b75cab
			int	s;
roentgen b75cab
roentgen b75cab
			for(s = 0; s < samples_to_test; s++) {
roentgen b75cab
				if (fabs(*pix1 - *pix2) < 0.000000000001) {
roentgen b75cab
					PrintFloatDiff(row, sample, pix,
roentgen b75cab
						       *pix1, *pix2);
roentgen b75cab
				}
roentgen b75cab
                        
roentgen b75cab
				pix1++;
roentgen b75cab
				pix2++;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		  fprintf(stderr, "Sample format %d is not supported.\n",
roentgen b75cab
			  sampleformat);
roentgen b75cab
		  return -1;
roentgen b75cab
	}
roentgen b75cab
        break;
roentgen b75cab
      default:
roentgen b75cab
	fprintf(stderr, "Bit depth %d is not supported.\n", bitspersample);
roentgen b75cab
	return -1;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
PrintIntDiff(uint32 row, int sample, uint32 pix, uint32 w1, uint32 w2)
roentgen b75cab
{
roentgen b75cab
	if (sample < 0)
roentgen b75cab
		sample = 0;
roentgen b75cab
	switch (bitspersample) {
roentgen b75cab
	case 1:
roentgen b75cab
	case 2:
roentgen b75cab
	case 4:
roentgen b75cab
	    {
roentgen b75cab
		int32 mask1, mask2, s;
roentgen b75cab
roentgen b75cab
		mask1 =  ~((-1) << bitspersample);
roentgen b75cab
		s = (8 - bitspersample);
roentgen b75cab
		mask2 = mask1 << s;
roentgen b75cab
		for (; mask2 && pix < imagewidth;
roentgen b75cab
		     mask2 >>= bitspersample, s -= bitspersample, pix++) {
roentgen b75cab
			if ((w1 & mask2) ^ (w2 & mask2)) {
roentgen b75cab
				printf(
roentgen b75cab
			"Scanline %lu, pixel %lu, sample %d: %01x %01x\n",
roentgen b75cab
	    				(unsigned long) row,
roentgen b75cab
					(unsigned long) pix,
roentgen b75cab
					sample,
roentgen b75cab
					(unsigned int)((w1 >> s) & mask1),
roentgen b75cab
					(unsigned int)((w2 >> s) & mask1));
roentgen b75cab
				if (--stopondiff == 0)
roentgen b75cab
					exit(1);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	    }
roentgen b75cab
	case 8: 
roentgen b75cab
		printf("Scanline %lu, pixel %lu, sample %d: %02x %02x\n",
roentgen b75cab
		       (unsigned long) row, (unsigned long) pix, sample,
roentgen b75cab
		       (unsigned int) w1, (unsigned int) w2);
roentgen b75cab
		if (--stopondiff == 0)
roentgen b75cab
			exit(1);
roentgen b75cab
		break;
roentgen b75cab
	case 16:
roentgen b75cab
		printf("Scanline %lu, pixel %lu, sample %d: %04x %04x\n",
roentgen b75cab
		    (unsigned long) row, (unsigned long) pix, sample,
roentgen b75cab
		    (unsigned int) w1, (unsigned int) w2);
roentgen b75cab
		if (--stopondiff == 0)
roentgen b75cab
			exit(1);
roentgen b75cab
		break;
roentgen b75cab
	case 32:
roentgen b75cab
		printf("Scanline %lu, pixel %lu, sample %d: %08x %08x\n",
roentgen b75cab
		    (unsigned long) row, (unsigned long) pix, sample,
roentgen b75cab
		    (unsigned int) w1, (unsigned int) w2);
roentgen b75cab
		if (--stopondiff == 0)
roentgen b75cab
			exit(1);
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
PrintFloatDiff(uint32 row, int sample, uint32 pix, double w1, double w2)
roentgen b75cab
{
roentgen b75cab
	if (sample < 0)
roentgen b75cab
		sample = 0;
roentgen b75cab
	switch (bitspersample) {
roentgen b75cab
	case 32: 
roentgen b75cab
		printf("Scanline %lu, pixel %lu, sample %d: %g %g\n",
roentgen b75cab
		    (long) row, (long) pix, sample, w1, w2);
roentgen b75cab
		if (--stopondiff == 0)
roentgen b75cab
			exit(1);
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
SeparateCompare(int reversed, int sample, uint32 row,
roentgen b75cab
		unsigned char* cp1, unsigned char* p2)
roentgen b75cab
{
roentgen b75cab
	uint32 npixels = imagewidth;
roentgen b75cab
	int pixel;
roentgen b75cab
roentgen b75cab
	cp1 += sample;
roentgen b75cab
	for (pixel = 0; npixels-- > 0; pixel++, cp1 += samplesperpixel, p2++) {
roentgen b75cab
		if (*cp1 != *p2) {
roentgen b75cab
			printf("Scanline %lu, pixel %lu, sample %ld: ",
roentgen b75cab
			    (long) row, (long) pixel, (long) sample);
roentgen b75cab
			if (reversed)
roentgen b75cab
				printf("%02x %02x\n", *p2, *cp1);
roentgen b75cab
			else
roentgen b75cab
				printf("%02x %02x\n", *cp1, *p2);
roentgen b75cab
			if (--stopondiff == 0)
roentgen b75cab
				exit(1);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
checkTag(TIFF* tif1, TIFF* tif2, int tag, char* name, void* p1, void* p2)
roentgen b75cab
{
roentgen b75cab
roentgen b75cab
	if (TIFFGetField(tif1, tag, p1)) {
roentgen b75cab
		if (!TIFFGetField(tif2, tag, p2)) {
roentgen b75cab
			printf("%s tag appears only in %s\n",
roentgen b75cab
			    name, TIFFFileName(tif1));
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		return (1);
roentgen b75cab
	} else if (TIFFGetField(tif2, tag, p2)) {
roentgen b75cab
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (-1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	CHECK(cmp, fmt) {				\
roentgen b75cab
	switch (checkTag(tif1,tif2,tag,name,&v1,&v2)) {	\
roentgen b75cab
	case 1:	if (cmp)				\
roentgen b75cab
	case -1:	return (1);			\
roentgen b75cab
		printf(fmt, name, v1, v2);		\
roentgen b75cab
	}						\
roentgen b75cab
	return (0);					\
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckShortTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	uint16 v1, v2;
roentgen b75cab
	CHECK(v1 == v2, "%s: %u %u\n");
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckShort2Tag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	uint16 v11, v12, v21, v22;
roentgen b75cab
roentgen b75cab
	if (TIFFGetField(tif1, tag, &v11, &v12)) {
roentgen b75cab
		if (!TIFFGetField(tif2, tag, &v21, &v22)) {
roentgen b75cab
			printf("%s tag appears only in %s\n",
roentgen b75cab
			    name, TIFFFileName(tif1));
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		if (v11 == v21 && v12 == v22)
roentgen b75cab
			return (1);
roentgen b75cab
		printf("%s: <%u,%u> <%u,%u>\n", name, v11, v12, v21, v22);
roentgen b75cab
	} else if (TIFFGetField(tif2, tag, &v21, &v22))
roentgen b75cab
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
roentgen b75cab
	else
roentgen b75cab
		return (1);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckShortArrayTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	uint16 n1, *a1;
roentgen b75cab
	uint16 n2, *a2;
roentgen b75cab
roentgen b75cab
	if (TIFFGetField(tif1, tag, &n1, &a1)) {
roentgen b75cab
		if (!TIFFGetField(tif2, tag, &n2, &a2)) {
roentgen b75cab
			printf("%s tag appears only in %s\n",
roentgen b75cab
			    name, TIFFFileName(tif1));
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		if (n1 == n2) {
roentgen b75cab
			char* sep;
roentgen b75cab
			uint16 i;
roentgen b75cab
roentgen b75cab
			if (memcmp(a1, a2, n1 * sizeof(uint16)) == 0)
roentgen b75cab
				return (1);
roentgen b75cab
			printf("%s: value mismatch, <%u:", name, n1);
roentgen b75cab
			sep = "";
roentgen b75cab
			for (i = 0; i < n1; i++)
roentgen b75cab
				printf("%s%u", sep, a1[i]), sep = ",";
roentgen b75cab
			printf("> and <%u: ", n2);
roentgen b75cab
			sep = "";
roentgen b75cab
			for (i = 0; i < n2; i++)
roentgen b75cab
				printf("%s%u", sep, a2[i]), sep = ",";
roentgen b75cab
			printf(">\n");
roentgen b75cab
		} else
roentgen b75cab
			printf("%s: %u items in %s, %u items in %s", name,
roentgen b75cab
			    n1, TIFFFileName(tif1),
roentgen b75cab
			    n2, TIFFFileName(tif2)
roentgen b75cab
			);
roentgen b75cab
	} else if (TIFFGetField(tif2, tag, &n2, &a2))
roentgen b75cab
		printf("%s tag appears only in %s\n", name, TIFFFileName(tif2));
roentgen b75cab
	else
roentgen b75cab
		return (1);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckLongTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	uint32 v1, v2;
roentgen b75cab
	CHECK(v1 == v2, "%s: %u %u\n");
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckFloatTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	float v1, v2;
roentgen b75cab
	CHECK(v1 == v2, "%s: %g %g\n");
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
CheckStringTag(TIFF* tif1, TIFF* tif2, int tag, char* name)
roentgen b75cab
{
roentgen b75cab
	char *v1, *v2;
roentgen b75cab
	CHECK(strcmp(v1, v2) == 0, "%s: \"%s\" \"%s\"\n");
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
leof(const char* name, uint32 row, int s)
roentgen b75cab
{
roentgen b75cab
roentgen b75cab
	printf("%s: EOF at scanline %lu", name, (unsigned long)row);
roentgen b75cab
	if (s >= 0)
roentgen b75cab
		printf(", sample %d", s);
roentgen b75cab
	printf("\n");
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
 */