| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "libport.h" |
| 26 |
#include "tif_config.h" |
| 27 |
|
| 28 |
#include <math.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#ifdef HAVE_UNISTD_H |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "tiffio.h" |
| 38 |
|
| 39 |
#ifndef EXIT_SUCCESS |
| 40 |
#define EXIT_SUCCESS 0 |
| 41 |
#endif |
| 42 |
#ifndef EXIT_FAILURE |
| 43 |
#define EXIT_FAILURE 1 |
| 44 |
#endif |
| 45 |
|
| 46 |
static int stopondiff = 1; |
| 47 |
static int stoponfirsttag = 1; |
| 48 |
static uint16_t bitspersample = 1; |
| 49 |
static uint16_t samplesperpixel = 1; |
| 50 |
static uint16_t sampleformat = SAMPLEFORMAT_UINT; |
| 51 |
static uint32_t imagewidth; |
| 52 |
static uint32_t imagelength; |
| 53 |
|
| 54 |
static void usage(int code); |
| 55 |
static int tiffcmp(TIFF *, TIFF *); |
| 56 |
static int cmptags(TIFF *, TIFF *); |
| 57 |
static int ContigCompare(int, uint32_t, unsigned char *, unsigned char *, |
| 58 |
tsize_t); |
| 59 |
static int SeparateCompare(int, int, uint32_t, unsigned char *, |
| 60 |
unsigned char *); |
| 61 |
static void PrintIntDiff(uint32_t, int, uint32_t, uint32_t, uint32_t); |
| 62 |
static void PrintFloatDiff(uint32_t, int, uint32_t, double, double); |
| 63 |
|
| 64 |
static void leof(const char *, uint32_t, int); |
| 65 |
|
| 66 |
/* |
| 67 |
* exit with status : |
| 68 |
* 0 No differences were found. |
| 69 |
* 1 Differences were found. |
| 70 |
* >1 An error occurred. |
| 71 |
*/ |
| 72 |
int main(int argc, char *argv[]) |
| 73 |
{ |
| 74 |
TIFF *tif1, *tif2; |
| 75 |
int c, dirnum; |
| 76 |
#if !HAVE_DECL_OPTARG |
| 77 |
extern int optind; |
| 78 |
extern char *optarg; |
| 79 |
#endif |
| 80 |
|
| 81 |
while ((c = getopt(argc, argv, "ltz:h")) != -1) |
| 82 |
switch (c) |
| 83 |
{ |
| 84 |
case 'l': |
| 85 |
stopondiff = 0; |
| 86 |
break; |
| 87 |
case 'z': |
| 88 |
stopondiff = atoi(optarg); |
| 89 |
break; |
| 90 |
case 't': |
| 91 |
stoponfirsttag = 0; |
| 92 |
break; |
| 93 |
case 'h': |
| 94 |
usage(EXIT_SUCCESS); |
| 95 |
break; |
| 96 |
case '?': |
| 97 |
usage(2); |
| 98 |
/*NOTREACHED*/ |
| 99 |
break; |
| 100 |
} |
| 101 |
if (argc - optind < 2) |
| 102 |
usage(2); |
| 103 |
tif1 = TIFFOpen(argv[optind], "r"); |
| 104 |
if (tif1 == NULL) |
| 105 |
return (2); |
| 106 |
tif2 = TIFFOpen(argv[optind + 1], "r"); |
| 107 |
if (tif2 == NULL) |
| 108 |
return (2); |
| 109 |
dirnum = 0; |
| 110 |
while (tiffcmp(tif1, tif2)) |
| 111 |
{ |
| 112 |
if (!TIFFReadDirectory(tif1)) |
| 113 |
{ |
| 114 |
if (!TIFFReadDirectory(tif2)) |
| 115 |
break; |
| 116 |
printf("No more directories for %s\n", TIFFFileName(tif1)); |
| 117 |
return (1); |
| 118 |
} |
| 119 |
else if (!TIFFReadDirectory(tif2)) |
| 120 |
{ |
| 121 |
printf("No more directories for %s\n", TIFFFileName(tif2)); |
| 122 |
return (1); |
| 123 |
} |
| 124 |
printf("Directory %d:\n", ++dirnum); |
| 125 |
} |
| 126 |
|
| 127 |
TIFFClose(tif1); |
| 128 |
TIFFClose(tif2); |
| 129 |
return (0); |
| 130 |
} |
| 131 |
|
| 132 |
static const char usage_info[] = |
| 133 |
"Compare the tags and data in two TIFF files\n\n" |
| 134 |
"usage: tiffcmp [options] file1 file2\n" |
| 135 |
"where options are:\n" |
| 136 |
" -l list each byte of image data that differs between the " |
| 137 |
"files\n" |
| 138 |
" -z # list specified number of bytes that differs between " |
| 139 |
"the files\n" |
| 140 |
" -t ignore any differences in directory tags\n"; |
| 141 |
|
| 142 |
static void usage(int code) |
| 143 |
{ |
| 144 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 145 |
|
| 146 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 147 |
fprintf(out, "%s", usage_info); |
| 148 |
exit(code); |
| 149 |
} |
| 150 |
|
| 151 |
#define checkEOF(tif, row, sample) \ |
| 152 |
{ \ |
| 153 |
leof(TIFFFileName(tif), row, sample); \ |
| 154 |
goto bad; \ |
| 155 |
} |
| 156 |
|
| 157 |
static int CheckShortTag(TIFF *, TIFF *, int, char *); |
| 158 |
static int CheckShort2Tag(TIFF *, TIFF *, int, char *); |
| 159 |
static int CheckShortArrayTag(TIFF *, TIFF *, int, char *); |
| 160 |
static int CheckLongTag(TIFF *, TIFF *, int, char *); |
| 161 |
static int CheckFloatTag(TIFF *, TIFF *, int, char *); |
| 162 |
static int CheckStringTag(TIFF *, TIFF *, int, char *); |
| 163 |
|
| 164 |
static int tiffcmp(TIFF *tif1, TIFF *tif2) |
| 165 |
{ |
| 166 |
uint16_t config1, config2; |
| 167 |
tsize_t size1; |
| 168 |
uint32_t row; |
| 169 |
tsample_t s; |
| 170 |
unsigned char *buf1, *buf2; |
| 171 |
|
| 172 |
if (!CheckShortTag(tif1, tif2, TIFFTAG_BITSPERSAMPLE, "BitsPerSample")) |
| 173 |
return (0); |
| 174 |
if (!CheckShortTag(tif1, tif2, TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel")) |
| 175 |
return (0); |
| 176 |
if (!CheckLongTag(tif1, tif2, TIFFTAG_IMAGEWIDTH, "ImageWidth")) |
| 177 |
return (0); |
| 178 |
if (!cmptags(tif1, tif2)) |
| 179 |
return (1); |
| 180 |
(void)TIFFGetField(tif1, TIFFTAG_BITSPERSAMPLE, &bitspersample); |
| 181 |
(void)TIFFGetField(tif1, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); |
| 182 |
(void)TIFFGetField(tif1, TIFFTAG_SAMPLEFORMAT, &sampleformat); |
| 183 |
(void)TIFFGetField(tif1, TIFFTAG_IMAGEWIDTH, &imagewidth); |
| 184 |
(void)TIFFGetField(tif1, TIFFTAG_IMAGELENGTH, &imagelength); |
| 185 |
(void)TIFFGetField(tif1, TIFFTAG_PLANARCONFIG, &config1); |
| 186 |
(void)TIFFGetField(tif2, TIFFTAG_PLANARCONFIG, &config2); |
| 187 |
buf1 = (unsigned char *)_TIFFmalloc(size1 = TIFFScanlineSize(tif1)); |
| 188 |
buf2 = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif2)); |
| 189 |
if (buf1 == NULL || buf2 == NULL) |
| 190 |
{ |
| 191 |
fprintf(stderr, "No space for scanline buffers\n"); |
| 192 |
exit(2); |
| 193 |
} |
| 194 |
if (config1 != config2 && bitspersample != 8 && samplesperpixel > 1) |
| 195 |
{ |
| 196 |
fprintf(stderr, "Can't handle different planar configuration w/ " |
| 197 |
"different bits/sample\n"); |
| 198 |
goto bad; |
| 199 |
} |
| 200 |
#define pack(a, b) ((a) << 8) | (b) |
| 201 |
switch (pack(config1, config2)) |
| 202 |
{ |
| 203 |
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_CONTIG): |
| 204 |
for (row = 0; row < imagelength; row++) |
| 205 |
{ |
| 206 |
if (TIFFReadScanline(tif2, buf2, row, 0) < 0) |
| 207 |
checkEOF(tif2, row, -1) for (s = 0; s < samplesperpixel; |
| 208 |
s++) |
| 209 |
{ |
| 210 |
if (TIFFReadScanline(tif1, buf1, row, s) < 0) |
| 211 |
checkEOF(tif1, row, s) if (SeparateCompare( |
| 212 |
1, s, row, buf2, |
| 213 |
buf1) < 0) goto bad1; |
| 214 |
} |
| 215 |
} |
| 216 |
break; |
| 217 |
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_SEPARATE): |
| 218 |
for (row = 0; row < imagelength; row++) |
| 219 |
{ |
| 220 |
if (TIFFReadScanline(tif1, buf1, row, 0) < 0) |
| 221 |
checkEOF(tif1, row, -1) for (s = 0; s < samplesperpixel; |
| 222 |
s++) |
| 223 |
{ |
| 224 |
if (TIFFReadScanline(tif2, buf2, row, s) < 0) |
| 225 |
checkEOF(tif2, row, s) if (SeparateCompare( |
| 226 |
0, s, row, buf1, |
| 227 |
buf2) < 0) goto bad1; |
| 228 |
} |
| 229 |
} |
| 230 |
break; |
| 231 |
case pack(PLANARCONFIG_SEPARATE, PLANARCONFIG_SEPARATE): |
| 232 |
for (s = 0; s < samplesperpixel; s++) |
| 233 |
for (row = 0; row < imagelength; row++) |
| 234 |
{ |
| 235 |
if (TIFFReadScanline(tif1, buf1, row, s) < 0) |
| 236 |
checkEOF(tif1, row, s) if (TIFFReadScanline(tif2, buf2, |
| 237 |
row, s) < 0) |
| 238 |
checkEOF(tif2, row, |
| 239 |
s) if (ContigCompare(s, row, buf1, buf2, |
| 240 |
size1) < 0) goto bad1; |
| 241 |
} |
| 242 |
break; |
| 243 |
case pack(PLANARCONFIG_CONTIG, PLANARCONFIG_CONTIG): |
| 244 |
for (row = 0; row < imagelength; row++) |
| 245 |
{ |
| 246 |
if (TIFFReadScanline(tif1, buf1, row, 0) < 0) |
| 247 |
checkEOF(tif1, row, |
| 248 |
-1) if (TIFFReadScanline(tif2, buf2, row, 0) < 0) |
| 249 |
checkEOF(tif2, row, |
| 250 |
-1) if (ContigCompare(-1, row, buf1, buf2, |
| 251 |
size1) < 0) goto bad1; |
| 252 |
} |
| 253 |
break; |
| 254 |
} |
| 255 |
if (buf1) |
| 256 |
_TIFFfree(buf1); |
| 257 |
if (buf2) |
| 258 |
_TIFFfree(buf2); |
| 259 |
return (1); |
| 260 |
bad: |
| 261 |
if (stopondiff) |
| 262 |
exit(1); |
| 263 |
bad1: |
| 264 |
if (buf1) |
| 265 |
_TIFFfree(buf1); |
| 266 |
if (buf2) |
| 267 |
_TIFFfree(buf2); |
| 268 |
return (0); |
| 269 |
} |
| 270 |
|
| 271 |
#define CmpShortField(tag, name) \ |
| 272 |
if (!CheckShortTag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 273 |
return (0) |
| 274 |
#define CmpShortField2(tag, name) \ |
| 275 |
if (!CheckShort2Tag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 276 |
return (0) |
| 277 |
#define CmpLongField(tag, name) \ |
| 278 |
if (!CheckLongTag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 279 |
return (0) |
| 280 |
#define CmpFloatField(tag, name) \ |
| 281 |
if (!CheckFloatTag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 282 |
return (0) |
| 283 |
#define CmpStringField(tag, name) \ |
| 284 |
if (!CheckStringTag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 285 |
return (0) |
| 286 |
#define CmpShortArrayField(tag, name) \ |
| 287 |
if (!CheckShortArrayTag(tif1, tif2, tag, name) && stoponfirsttag) \ |
| 288 |
return (0) |
| 289 |
|
| 290 |
static int cmptags(TIFF *tif1, TIFF *tif2) |
| 291 |
{ |
| 292 |
uint16_t compression1, compression2; |
| 293 |
CmpLongField(TIFFTAG_SUBFILETYPE, "SubFileType"); |
| 294 |
CmpLongField(TIFFTAG_IMAGEWIDTH, "ImageWidth"); |
| 295 |
CmpLongField(TIFFTAG_IMAGELENGTH, "ImageLength"); |
| 296 |
CmpShortField(TIFFTAG_BITSPERSAMPLE, "BitsPerSample"); |
| 297 |
CmpShortField(TIFFTAG_COMPRESSION, "Compression"); |
| 298 |
CmpShortField(TIFFTAG_PREDICTOR, "Predictor"); |
| 299 |
CmpShortField(TIFFTAG_PHOTOMETRIC, "PhotometricInterpretation"); |
| 300 |
CmpShortField(TIFFTAG_THRESHHOLDING, "Thresholding"); |
| 301 |
CmpShortField(TIFFTAG_FILLORDER, "FillOrder"); |
| 302 |
CmpShortField(TIFFTAG_ORIENTATION, "Orientation"); |
| 303 |
CmpShortField(TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel"); |
| 304 |
CmpShortField(TIFFTAG_MINSAMPLEVALUE, "MinSampleValue"); |
| 305 |
CmpShortField(TIFFTAG_MAXSAMPLEVALUE, "MaxSampleValue"); |
| 306 |
CmpShortField(TIFFTAG_SAMPLEFORMAT, "SampleFormat"); |
| 307 |
CmpFloatField(TIFFTAG_XRESOLUTION, "XResolution"); |
| 308 |
CmpFloatField(TIFFTAG_YRESOLUTION, "YResolution"); |
| 309 |
if (TIFFGetField(tif1, TIFFTAG_COMPRESSION, &compression1) && |
| 310 |
compression1 == COMPRESSION_CCITTFAX3 && |
| 311 |
TIFFGetField(tif2, TIFFTAG_COMPRESSION, &compression2) && |
| 312 |
compression2 == COMPRESSION_CCITTFAX3) |
| 313 |
{ |
| 314 |
CmpLongField(TIFFTAG_GROUP3OPTIONS, "Group3Options"); |
| 315 |
} |
| 316 |
if (TIFFGetField(tif1, TIFFTAG_COMPRESSION, &compression1) && |
| 317 |
compression1 == COMPRESSION_CCITTFAX4 && |
| 318 |
TIFFGetField(tif2, TIFFTAG_COMPRESSION, &compression2) && |
| 319 |
compression2 == COMPRESSION_CCITTFAX4) |
| 320 |
{ |
| 321 |
CmpLongField(TIFFTAG_GROUP4OPTIONS, "Group4Options"); |
| 322 |
} |
| 323 |
CmpShortField(TIFFTAG_RESOLUTIONUNIT, "ResolutionUnit"); |
| 324 |
CmpShortField(TIFFTAG_PLANARCONFIG, "PlanarConfiguration"); |
| 325 |
CmpLongField(TIFFTAG_ROWSPERSTRIP, "RowsPerStrip"); |
| 326 |
CmpFloatField(TIFFTAG_XPOSITION, "XPosition"); |
| 327 |
CmpFloatField(TIFFTAG_YPOSITION, "YPosition"); |
| 328 |
CmpShortField(TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit"); |
| 329 |
CmpShortField(TIFFTAG_COLORRESPONSEUNIT, "ColorResponseUnit"); |
| 330 |
#ifdef notdef |
| 331 |
{ |
| 332 |
uint16_t *graycurve; |
| 333 |
CmpField(TIFFTAG_GRAYRESPONSECURVE, graycurve); |
| 334 |
} |
| 335 |
{ |
| 336 |
uint16_t *red, *green, *blue; |
| 337 |
CmpField3(TIFFTAG_COLORRESPONSECURVE, red, green, blue); |
| 338 |
} |
| 339 |
{ |
| 340 |
uint16_t *red, *green, *blue; |
| 341 |
CmpField3(TIFFTAG_COLORMAP, red, green, blue); |
| 342 |
} |
| 343 |
#endif |
| 344 |
CmpShortField2(TIFFTAG_PAGENUMBER, "PageNumber"); |
| 345 |
CmpStringField(TIFFTAG_ARTIST, "Artist"); |
| 346 |
CmpStringField(TIFFTAG_IMAGEDESCRIPTION, "ImageDescription"); |
| 347 |
CmpStringField(TIFFTAG_MAKE, "Make"); |
| 348 |
CmpStringField(TIFFTAG_MODEL, "Model"); |
| 349 |
CmpStringField(TIFFTAG_SOFTWARE, "Software"); |
| 350 |
CmpStringField(TIFFTAG_DATETIME, "DateTime"); |
| 351 |
CmpStringField(TIFFTAG_HOSTCOMPUTER, "HostComputer"); |
| 352 |
CmpStringField(TIFFTAG_PAGENAME, "PageName"); |
| 353 |
CmpStringField(TIFFTAG_DOCUMENTNAME, "DocumentName"); |
| 354 |
CmpShortField(TIFFTAG_MATTEING, "Matteing"); |
| 355 |
CmpShortArrayField(TIFFTAG_EXTRASAMPLES, "ExtraSamples"); |
| 356 |
return (1); |
| 357 |
} |
| 358 |
|
| 359 |
static int ContigCompare(int sample, uint32_t row, unsigned char *p1, |
| 360 |
unsigned char *p2, tsize_t size) |
| 361 |
{ |
| 362 |
uint32_t pix; |
| 363 |
int samples_to_test; |
| 364 |
|
| 365 |
if (memcmp(p1, p2, size) == 0) |
| 366 |
return 0; |
| 367 |
|
| 368 |
samples_to_test = (sample == -1) ? samplesperpixel : 1; |
| 369 |
|
| 370 |
switch (bitspersample) |
| 371 |
{ |
| 372 |
case 1: |
| 373 |
case 2: |
| 374 |
case 4: |
| 375 |
case 8: |
| 376 |
{ |
| 377 |
unsigned char *pix1 = p1, *pix2 = p2; |
| 378 |
unsigned bits = 0; |
| 379 |
|
| 380 |
for (pix = 0; pix < imagewidth; pix++) |
| 381 |
{ |
| 382 |
int s; |
| 383 |
|
| 384 |
for (s = 0; s < samples_to_test; s++) |
| 385 |
{ |
| 386 |
if (*pix1 != *pix2) |
| 387 |
{ |
| 388 |
if (sample == -1) |
| 389 |
PrintIntDiff(row, s, pix, *pix1, *pix2); |
| 390 |
else |
| 391 |
PrintIntDiff(row, sample, pix, *pix1, *pix2); |
| 392 |
} |
| 393 |
|
| 394 |
bits += bitspersample; |
| 395 |
pix1 += (bits / 8); |
| 396 |
pix2 += (bits / 8); |
| 397 |
bits &= 7; |
| 398 |
} |
| 399 |
} |
| 400 |
break; |
| 401 |
} |
| 402 |
case 16: |
| 403 |
{ |
| 404 |
uint16_t *pix1 = (uint16_t *)p1, *pix2 = (uint16_t *)p2; |
| 405 |
|
| 406 |
for (pix = 0; pix < imagewidth; pix++) |
| 407 |
{ |
| 408 |
int s; |
| 409 |
|
| 410 |
for (s = 0; s < samples_to_test; s++) |
| 411 |
{ |
| 412 |
if (*pix1 != *pix2) |
| 413 |
PrintIntDiff(row, sample, pix, *pix1, *pix2); |
| 414 |
|
| 415 |
pix1++; |
| 416 |
pix2++; |
| 417 |
} |
| 418 |
} |
| 419 |
break; |
| 420 |
} |
| 421 |
case 32: |
| 422 |
if (sampleformat == SAMPLEFORMAT_UINT || |
| 423 |
sampleformat == SAMPLEFORMAT_INT) |
| 424 |
{ |
| 425 |
uint32_t *pix1 = (uint32_t *)p1, *pix2 = (uint32_t *)p2; |
| 426 |
|
| 427 |
for (pix = 0; pix < imagewidth; pix++) |
| 428 |
{ |
| 429 |
int s; |
| 430 |
|
| 431 |
for (s = 0; s < samples_to_test; s++) |
| 432 |
{ |
| 433 |
if (*pix1 != *pix2) |
| 434 |
{ |
| 435 |
PrintIntDiff(row, sample, pix, *pix1, *pix2); |
| 436 |
} |
| 437 |
|
| 438 |
pix1++; |
| 439 |
pix2++; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
else if (sampleformat == SAMPLEFORMAT_IEEEFP) |
| 444 |
{ |
| 445 |
float *pix1 = (float *)p1, *pix2 = (float *)p2; |
| 446 |
|
| 447 |
for (pix = 0; pix < imagewidth; pix++) |
| 448 |
{ |
| 449 |
int s; |
| 450 |
|
| 451 |
for (s = 0; s < samples_to_test; s++) |
| 452 |
{ |
| 453 |
if (fabs(*pix1 - *pix2) < 0.000000000001) |
| 454 |
{ |
| 455 |
PrintFloatDiff(row, sample, pix, *pix1, *pix2); |
| 456 |
} |
| 457 |
|
| 458 |
pix1++; |
| 459 |
pix2++; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
fprintf(stderr, "Sample format %" PRIu16 " is not supported.\n", |
| 466 |
sampleformat); |
| 467 |
return -1; |
| 468 |
} |
| 469 |
break; |
| 470 |
default: |
| 471 |
fprintf(stderr, "Bit depth %" PRIu16 " is not supported.\n", |
| 472 |
bitspersample); |
| 473 |
return -1; |
| 474 |
} |
| 475 |
|
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
static void PrintIntDiff(uint32_t row, int sample, uint32_t pix, uint32_t w1, |
| 480 |
uint32_t w2) |
| 481 |
{ |
| 482 |
if (sample < 0) |
| 483 |
sample = 0; |
| 484 |
switch (bitspersample) |
| 485 |
{ |
| 486 |
case 1: |
| 487 |
case 2: |
| 488 |
case 4: |
| 489 |
{ |
| 490 |
int32_t mask1, mask2, s; |
| 491 |
|
| 492 |
/* mask1 should have the n lowest bits set, where n == bitspersample |
| 493 |
*/ |
| 494 |
mask1 = ((int32_t)1 << bitspersample) - 1; |
| 495 |
s = (8 - bitspersample); |
| 496 |
mask2 = mask1 << s; |
| 497 |
for (; mask2 && pix < imagewidth; |
| 498 |
mask2 >>= bitspersample, s -= bitspersample, pix++) |
| 499 |
{ |
| 500 |
if ((w1 & mask2) ^ (w2 & mask2)) |
| 501 |
{ |
| 502 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 |
| 503 |
", sample %d: %01" PRIx32 " %01" PRIx32 "\n", |
| 504 |
row, pix, sample, (w1 >> s) & mask1, |
| 505 |
(w2 >> s) & mask1); |
| 506 |
if (--stopondiff == 0) |
| 507 |
exit(1); |
| 508 |
} |
| 509 |
} |
| 510 |
break; |
| 511 |
} |
| 512 |
case 8: |
| 513 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 |
| 514 |
", sample %d: %02" PRIx32 " %02" PRIx32 "\n", |
| 515 |
row, pix, sample, w1, w2); |
| 516 |
if (--stopondiff == 0) |
| 517 |
exit(1); |
| 518 |
break; |
| 519 |
case 16: |
| 520 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 |
| 521 |
", sample %d: %04" PRIx32 " %04" PRIx32 "\n", |
| 522 |
row, pix, sample, w1, w2); |
| 523 |
if (--stopondiff == 0) |
| 524 |
exit(1); |
| 525 |
break; |
| 526 |
case 32: |
| 527 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 |
| 528 |
", sample %d: %08" PRIx32 " %08" PRIx32 "\n", |
| 529 |
row, pix, sample, w1, w2); |
| 530 |
if (--stopondiff == 0) |
| 531 |
exit(1); |
| 532 |
break; |
| 533 |
default: |
| 534 |
break; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
static void PrintFloatDiff(uint32_t row, int sample, uint32_t pix, double w1, |
| 539 |
double w2) |
| 540 |
{ |
| 541 |
if (sample < 0) |
| 542 |
sample = 0; |
| 543 |
switch (bitspersample) |
| 544 |
{ |
| 545 |
case 32: |
| 546 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 |
| 547 |
", sample %d: %g %g\n", |
| 548 |
row, pix, sample, w1, w2); |
| 549 |
if (--stopondiff == 0) |
| 550 |
exit(1); |
| 551 |
break; |
| 552 |
default: |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
static int SeparateCompare(int reversed, int sample, uint32_t row, |
| 558 |
unsigned char *cp1, unsigned char *p2) |
| 559 |
{ |
| 560 |
uint32_t npixels = imagewidth; |
| 561 |
int pixel; |
| 562 |
|
| 563 |
cp1 += sample; |
| 564 |
for (pixel = 0; npixels-- > 0; pixel++, cp1 += samplesperpixel, p2++) |
| 565 |
{ |
| 566 |
if (*cp1 != *p2) |
| 567 |
{ |
| 568 |
printf("Scanline %" PRIu32 ", pixel %" PRIu32 ", sample %d: ", row, |
| 569 |
pixel, sample); |
| 570 |
if (reversed) |
| 571 |
printf("%02x %02x\n", *p2, *cp1); |
| 572 |
else |
| 573 |
printf("%02x %02x\n", *cp1, *p2); |
| 574 |
if (--stopondiff == 0) |
| 575 |
exit(1); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
return 0; |
| 580 |
} |
| 581 |
|
| 582 |
static int checkTag(TIFF *tif1, TIFF *tif2, int tag, char *name, void *p1, |
| 583 |
void *p2) |
| 584 |
{ |
| 585 |
|
| 586 |
if (TIFFGetField(tif1, tag, p1)) |
| 587 |
{ |
| 588 |
if (!TIFFGetField(tif2, tag, p2)) |
| 589 |
{ |
| 590 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif1)); |
| 591 |
return (0); |
| 592 |
} |
| 593 |
return (1); |
| 594 |
} |
| 595 |
else if (TIFFGetField(tif2, tag, p2)) |
| 596 |
{ |
| 597 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif2)); |
| 598 |
return (0); |
| 599 |
} |
| 600 |
return (-1); |
| 601 |
} |
| 602 |
|
| 603 |
#define CHECK(cmp, fmt) \ |
| 604 |
{ \ |
| 605 |
switch (checkTag(tif1, tif2, tag, name, &v1, &v2)) \ |
| 606 |
{ \ |
| 607 |
case 1: \ |
| 608 |
if (cmp) \ |
| 609 |
case -1: \ |
| 610 |
return (1); \ |
| 611 |
printf(fmt, name, v1, v2); \ |
| 612 |
} \ |
| 613 |
return (0); \ |
| 614 |
} |
| 615 |
|
| 616 |
static int CheckShortTag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 617 |
{ |
| 618 |
uint16_t v1, v2; |
| 619 |
CHECK(v1 == v2, "%s: %" PRIu16 " %" PRIu16 "\n"); |
| 620 |
} |
| 621 |
|
| 622 |
static int CheckShort2Tag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 623 |
{ |
| 624 |
uint16_t v11, v12, v21, v22; |
| 625 |
|
| 626 |
if (TIFFGetField(tif1, tag, &v11, &v12)) |
| 627 |
{ |
| 628 |
if (!TIFFGetField(tif2, tag, &v21, &v22)) |
| 629 |
{ |
| 630 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif1)); |
| 631 |
return (0); |
| 632 |
} |
| 633 |
if (v11 == v21 && v12 == v22) |
| 634 |
return (1); |
| 635 |
printf("%s: <%" PRIu16 ",%" PRIu16 "> <%" PRIu16 ",%" PRIu16 ">\n", |
| 636 |
name, v11, v12, v21, v22); |
| 637 |
} |
| 638 |
else if (TIFFGetField(tif2, tag, &v21, &v22)) |
| 639 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif2)); |
| 640 |
else |
| 641 |
return (1); |
| 642 |
return (0); |
| 643 |
} |
| 644 |
|
| 645 |
static int CheckShortArrayTag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 646 |
{ |
| 647 |
uint16_t n1, *a1; |
| 648 |
uint16_t n2, *a2; |
| 649 |
|
| 650 |
if (TIFFGetField(tif1, tag, &n1, &a1)) |
| 651 |
{ |
| 652 |
if (!TIFFGetField(tif2, tag, &n2, &a2)) |
| 653 |
{ |
| 654 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif1)); |
| 655 |
return (0); |
| 656 |
} |
| 657 |
if (n1 == n2) |
| 658 |
{ |
| 659 |
char *sep; |
| 660 |
uint16_t i; |
| 661 |
|
| 662 |
if (memcmp(a1, a2, n1 * sizeof(uint16_t)) == 0) |
| 663 |
return (1); |
| 664 |
printf("%s: value mismatch, <%" PRIu16 ":", name, n1); |
| 665 |
sep = ""; |
| 666 |
for (i = 0; i < n1; i++) |
| 667 |
printf("%s%" PRIu16, sep, a1[i]), sep = ","; |
| 668 |
printf("> and <%" PRIu16 ": ", n2); |
| 669 |
sep = ""; |
| 670 |
for (i = 0; i < n2; i++) |
| 671 |
printf("%s%" PRIu16, sep, a2[i]), sep = ","; |
| 672 |
printf(">\n"); |
| 673 |
} |
| 674 |
else |
| 675 |
printf("%s: %" PRIu16 " items in %s, %" PRIu16 " items in %s", name, |
| 676 |
n1, TIFFFileName(tif1), n2, TIFFFileName(tif2)); |
| 677 |
} |
| 678 |
else if (TIFFGetField(tif2, tag, &n2, &a2)) |
| 679 |
printf("%s tag appears only in %s\n", name, TIFFFileName(tif2)); |
| 680 |
else |
| 681 |
return (1); |
| 682 |
return (0); |
| 683 |
} |
| 684 |
|
| 685 |
static int CheckLongTag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 686 |
{ |
| 687 |
uint32_t v1, v2; |
| 688 |
CHECK(v1 == v2, "%s: %" PRIu32 " %" PRIu32 "\n"); |
| 689 |
} |
| 690 |
|
| 691 |
static int CheckFloatTag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 692 |
{ |
| 693 |
float v1, v2; |
| 694 |
CHECK(v1 == v2, "%s: %g %g\n"); |
| 695 |
} |
| 696 |
|
| 697 |
static int CheckStringTag(TIFF *tif1, TIFF *tif2, int tag, char *name) |
| 698 |
{ |
| 699 |
char *v1, *v2; |
| 700 |
CHECK(strcmp(v1, v2) == 0, "%s: \"%s\" \"%s\"\n"); |
| 701 |
} |
| 702 |
|
| 703 |
static void leof(const char *name, uint32_t row, int s) |
| 704 |
{ |
| 705 |
|
| 706 |
printf("%s: EOF at scanline %" PRIu32, name, row); |
| 707 |
if (s >= 0) |
| 708 |
printf(", sample %d", s); |
| 709 |
printf("\n"); |
| 710 |
} |