| 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 <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#ifdef HAVE_STRINGS_H |
| 33 |
#include <strings.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef HAVE_UNISTD_H |
| 37 |
#include <unistd.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "tiffiop.h" |
| 41 |
|
| 42 |
#ifndef EXIT_SUCCESS |
| 43 |
#define EXIT_SUCCESS 0 |
| 44 |
#endif |
| 45 |
#ifndef EXIT_FAILURE |
| 46 |
#define EXIT_FAILURE 1 |
| 47 |
#endif |
| 48 |
|
| 49 |
static TIFFErrorHandler old_error_handler = 0; |
| 50 |
static int status = EXIT_SUCCESS; /* exit status */ |
| 51 |
static int showdata = 0; /* show data */ |
| 52 |
static int rawdata = 0; /* show raw/decoded data */ |
| 53 |
static int showwords = 0; /* show data as bytes/words */ |
| 54 |
static int readdata = 0; /* read data in file */ |
| 55 |
static int stoponerr = 1; /* stop on first read error */ |
| 56 |
|
| 57 |
static void usage(int); |
| 58 |
static void tiffinfo(TIFF *, uint16_t, long, int); |
| 59 |
|
| 60 |
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024) |
| 61 |
/* malloc size limit (in bytes) |
| 62 |
* disabled when set to 0 */ |
| 63 |
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC; |
| 64 |
|
| 65 |
static void PrivateErrorHandler(const char *module, const char *fmt, va_list ap) |
| 66 |
{ |
| 67 |
if (old_error_handler) |
| 68 |
(*old_error_handler)(module, fmt, ap); |
| 69 |
status = EXIT_FAILURE; |
| 70 |
} |
| 71 |
|
| 72 |
int main(int argc, char *argv[]) |
| 73 |
{ |
| 74 |
int dirnum = -1, multiplefiles, c; |
| 75 |
uint16_t order = 0; |
| 76 |
TIFF *tif; |
| 77 |
#if !HAVE_DECL_OPTARG |
| 78 |
extern int optind; |
| 79 |
extern char *optarg; |
| 80 |
#endif |
| 81 |
long flags = 0; |
| 82 |
uint64_t diroff = 0; |
| 83 |
int chopstrips = 0; /* disable strip chopping */ |
| 84 |
|
| 85 |
while ((c = getopt(argc, argv, "f:o:M:cdDSjilmrsvwz0123456789h")) != -1) |
| 86 |
switch (c) |
| 87 |
{ |
| 88 |
case '0': |
| 89 |
case '1': |
| 90 |
case '2': |
| 91 |
case '3': |
| 92 |
case '4': |
| 93 |
case '5': |
| 94 |
case '6': |
| 95 |
case '7': |
| 96 |
case '8': |
| 97 |
case '9': |
| 98 |
dirnum = atoi(&argv[optind - 1][1]); |
| 99 |
break; |
| 100 |
case 'd': |
| 101 |
showdata++; |
| 102 |
/* fall through... */ |
| 103 |
case 'D': |
| 104 |
readdata++; |
| 105 |
break; |
| 106 |
case 'c': |
| 107 |
flags |= TIFFPRINT_COLORMAP | TIFFPRINT_CURVES; |
| 108 |
break; |
| 109 |
case 'f': /* fill order */ |
| 110 |
if (streq(optarg, "lsb2msb")) |
| 111 |
order = FILLORDER_LSB2MSB; |
| 112 |
else if (streq(optarg, "msb2lsb")) |
| 113 |
order = FILLORDER_MSB2LSB; |
| 114 |
else |
| 115 |
usage(EXIT_FAILURE); |
| 116 |
break; |
| 117 |
case 'i': |
| 118 |
stoponerr = 0; |
| 119 |
break; |
| 120 |
case 'M': |
| 121 |
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20; |
| 122 |
break; |
| 123 |
case 'o': |
| 124 |
diroff = strtoul(optarg, NULL, 0); |
| 125 |
break; |
| 126 |
case 'j': |
| 127 |
flags |= TIFFPRINT_JPEGQTABLES | TIFFPRINT_JPEGACTABLES | |
| 128 |
TIFFPRINT_JPEGDCTABLES; |
| 129 |
break; |
| 130 |
case 'r': |
| 131 |
rawdata = 1; |
| 132 |
break; |
| 133 |
case 's': |
| 134 |
flags |= TIFFPRINT_STRIPS; |
| 135 |
break; |
| 136 |
case 'w': |
| 137 |
showwords = 1; |
| 138 |
break; |
| 139 |
case 'z': |
| 140 |
chopstrips = 1; |
| 141 |
break; |
| 142 |
case 'h': |
| 143 |
usage(EXIT_SUCCESS); |
| 144 |
/*NOTREACHED*/ |
| 145 |
break; |
| 146 |
case '?': |
| 147 |
usage(EXIT_FAILURE); |
| 148 |
/*NOTREACHED*/ |
| 149 |
break; |
| 150 |
} |
| 151 |
if (optind >= argc) |
| 152 |
usage(EXIT_FAILURE); |
| 153 |
|
| 154 |
old_error_handler = TIFFSetErrorHandler(PrivateErrorHandler); |
| 155 |
|
| 156 |
multiplefiles = (argc - optind > 1); |
| 157 |
for (; optind < argc; optind++) |
| 158 |
{ |
| 159 |
if (multiplefiles) |
| 160 |
printf("File %s:\n", argv[optind]); |
| 161 |
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc(); |
| 162 |
if (opts == NULL) |
| 163 |
{ |
| 164 |
status = EXIT_FAILURE; |
| 165 |
break; |
| 166 |
} |
| 167 |
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc); |
| 168 |
tif = TIFFOpenExt(argv[optind], chopstrips ? "rC" : "rc", opts); |
| 169 |
TIFFOpenOptionsFree(opts); |
| 170 |
if (tif != NULL) |
| 171 |
{ |
| 172 |
if (dirnum != -1) |
| 173 |
{ |
| 174 |
if (TIFFSetDirectory(tif, (tdir_t)dirnum)) |
| 175 |
tiffinfo(tif, order, flags, 1); |
| 176 |
} |
| 177 |
else if (diroff != 0) |
| 178 |
{ |
| 179 |
if (TIFFSetSubDirectory(tif, diroff)) |
| 180 |
tiffinfo(tif, order, flags, 1); |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
do |
| 185 |
{ |
| 186 |
toff_t offset = 0; |
| 187 |
tdir_t curdir = TIFFCurrentDirectory(tif); |
| 188 |
printf("=== TIFF directory %u ===\n", curdir); |
| 189 |
tiffinfo(tif, order, flags, 1); |
| 190 |
if (TIFFGetField(tif, TIFFTAG_EXIFIFD, &offset)) |
| 191 |
{ |
| 192 |
printf("--- EXIF directory within directory %u \n", |
| 193 |
curdir); |
| 194 |
if (TIFFReadEXIFDirectory(tif, offset)) |
| 195 |
{ |
| 196 |
tiffinfo(tif, order, flags, 0); |
| 197 |
/*-- Go back to previous directory, (directory is |
| 198 |
* reloaded from file!) */ |
| 199 |
TIFFSetDirectory(tif, curdir); |
| 200 |
} |
| 201 |
} |
| 202 |
if (TIFFGetField(tif, TIFFTAG_GPSIFD, &offset)) |
| 203 |
{ |
| 204 |
printf("--- GPS directory within directory %u \n", |
| 205 |
curdir); |
| 206 |
if (TIFFReadGPSDirectory(tif, offset)) |
| 207 |
{ |
| 208 |
tiffinfo(tif, order, flags, 0); |
| 209 |
TIFFSetDirectory(tif, curdir); |
| 210 |
} |
| 211 |
} |
| 212 |
/*-- Check for SubIFDs --*/ |
| 213 |
uint16_t nCount; |
| 214 |
void *vPtr; |
| 215 |
uint64_t *subIFDoffsets = NULL; |
| 216 |
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &nCount, &vPtr)) |
| 217 |
{ |
| 218 |
if (nCount > 0) |
| 219 |
{ |
| 220 |
subIFDoffsets = malloc(nCount * sizeof(uint64_t)); |
| 221 |
if (subIFDoffsets != NULL) |
| 222 |
{ |
| 223 |
memcpy(subIFDoffsets, vPtr, |
| 224 |
nCount * sizeof(subIFDoffsets[0])); |
| 225 |
printf("--- SubIFD image descriptor tag within " |
| 226 |
"TIFF directory %u with array of %d " |
| 227 |
"SubIFD chains ---\n", |
| 228 |
curdir, nCount); |
| 229 |
for (int i = 0; i < nCount; i++) |
| 230 |
{ |
| 231 |
offset = subIFDoffsets[i]; |
| 232 |
int s = 0; |
| 233 |
if (TIFFSetSubDirectory(tif, offset)) |
| 234 |
{ |
| 235 |
/* print info and check for SubIFD chain |
| 236 |
*/ |
| 237 |
do |
| 238 |
{ |
| 239 |
printf("--- SubIFD %d of chain %d " |
| 240 |
"at offset 0x%" PRIx64 |
| 241 |
" (%" PRIu64 "):\n", |
| 242 |
s, i, offset, offset); |
| 243 |
tiffinfo(tif, order, flags, 0); |
| 244 |
s++; |
| 245 |
} while (TIFFReadDirectory(tif)); |
| 246 |
} |
| 247 |
} |
| 248 |
TIFFSetDirectory(tif, curdir); |
| 249 |
free(subIFDoffsets); |
| 250 |
subIFDoffsets = NULL; |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
fprintf(stderr, |
| 255 |
"Error: Could not allocate memory for " |
| 256 |
"SubIFDs list. SubIFDs not parsed.\n"); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
printf("\n"); |
| 261 |
} while (TIFFReadDirectory(tif)); |
| 262 |
} |
| 263 |
TIFFClose(tif); |
| 264 |
} |
| 265 |
} |
| 266 |
return (status); |
| 267 |
} |
| 268 |
|
| 269 |
static const char usage_info[] = |
| 270 |
"Display information about TIFF files\n\n" |
| 271 |
"usage: tiffinfo [options] input...\n" |
| 272 |
"where options are:\n" |
| 273 |
" -D read data\n" |
| 274 |
" -i ignore read errors\n" |
| 275 |
" -c display data for grey/color response curve or " |
| 276 |
"colormap\n" |
| 277 |
" -d display raw/decoded image data\n" |
| 278 |
" -f lsb2msb force lsb-to-msb FillOrder for input\n" |
| 279 |
" -f msb2lsb force msb-to-lsb FillOrder for input\n" |
| 280 |
" -j show JPEG tables\n" |
| 281 |
" -o offset set initial directory offset\n" |
| 282 |
" -r read/display raw image data instead of decoded data\n" |
| 283 |
" -s display strip offsets and byte counts\n" |
| 284 |
" -w display raw data in words rather than bytes\n" |
| 285 |
" -z enable strip chopping\n" |
| 286 |
" -M size set the memory allocation limit in MiB. 0 to disable limit\n" |
| 287 |
" -# set initial directory (first directory is # 0)\n"; |
| 288 |
|
| 289 |
static void usage(int code) |
| 290 |
{ |
| 291 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 292 |
|
| 293 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 294 |
fprintf(out, "%s", usage_info); |
| 295 |
exit(code); |
| 296 |
} |
| 297 |
|
| 298 |
static void ShowStrip(tstrip_t strip, unsigned char *pp, uint32_t nrow, |
| 299 |
tsize_t scanline) |
| 300 |
{ |
| 301 |
register tsize_t cc; |
| 302 |
|
| 303 |
printf("Strip %" PRIu32 ":\n", strip); |
| 304 |
while (nrow-- > 0) |
| 305 |
{ |
| 306 |
for (cc = 0; cc < scanline; cc++) |
| 307 |
{ |
| 308 |
printf(" %02x", *pp++); |
| 309 |
if (((cc + 1) % 24) == 0) |
| 310 |
putchar('\n'); |
| 311 |
} |
| 312 |
putchar('\n'); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
void TIFFReadContigStripData(TIFF *tif) |
| 317 |
{ |
| 318 |
unsigned char *buf; |
| 319 |
tsize_t scanline = TIFFScanlineSize(tif); |
| 320 |
tmsize_t stripsize = TIFFStripSize(tif); |
| 321 |
|
| 322 |
if (maxMalloc != 0 && stripsize > maxMalloc) |
| 323 |
{ |
| 324 |
fprintf(stderr, |
| 325 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 326 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 327 |
stripsize, maxMalloc); |
| 328 |
return; |
| 329 |
} |
| 330 |
buf = (unsigned char *)_TIFFmalloc(stripsize); |
| 331 |
if (buf) |
| 332 |
{ |
| 333 |
uint32_t row, h = 0; |
| 334 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 335 |
|
| 336 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 337 |
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
| 338 |
for (row = 0; row < h; row += rowsperstrip) |
| 339 |
{ |
| 340 |
uint32_t nrow = (row + rowsperstrip > h ? h - row : rowsperstrip); |
| 341 |
tstrip_t strip = TIFFComputeStrip(tif, row, 0); |
| 342 |
if (TIFFReadEncodedStrip(tif, strip, buf, nrow * scanline) < 0) |
| 343 |
{ |
| 344 |
if (stoponerr) |
| 345 |
break; |
| 346 |
} |
| 347 |
else if (showdata) |
| 348 |
ShowStrip(strip, buf, nrow, scanline); |
| 349 |
} |
| 350 |
_TIFFfree(buf); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
fprintf(stderr, "Cannot allocate %" TIFF_SSIZE_FORMAT " bytes.\n", |
| 355 |
stripsize); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
void TIFFReadSeparateStripData(TIFF *tif) |
| 360 |
{ |
| 361 |
unsigned char *buf; |
| 362 |
tsize_t scanline = TIFFScanlineSize(tif); |
| 363 |
tmsize_t stripsize = TIFFStripSize(tif); |
| 364 |
|
| 365 |
if (maxMalloc != 0 && stripsize > maxMalloc) |
| 366 |
{ |
| 367 |
fprintf(stderr, |
| 368 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 369 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 370 |
stripsize, maxMalloc); |
| 371 |
return; |
| 372 |
} |
| 373 |
buf = (unsigned char *)_TIFFmalloc(stripsize); |
| 374 |
if (buf) |
| 375 |
{ |
| 376 |
uint32_t row, h = 0; |
| 377 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 378 |
tsample_t s, samplesperpixel = 0; |
| 379 |
|
| 380 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 381 |
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); |
| 382 |
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); |
| 383 |
for (row = 0; row < h; row += rowsperstrip) |
| 384 |
{ |
| 385 |
for (s = 0; s < samplesperpixel; s++) |
| 386 |
{ |
| 387 |
uint32_t nrow = |
| 388 |
(row + rowsperstrip > h ? h - row : rowsperstrip); |
| 389 |
tstrip_t strip = TIFFComputeStrip(tif, row, s); |
| 390 |
if (TIFFReadEncodedStrip(tif, strip, buf, nrow * scanline) < 0) |
| 391 |
{ |
| 392 |
if (stoponerr) |
| 393 |
break; |
| 394 |
} |
| 395 |
else if (showdata) |
| 396 |
ShowStrip(strip, buf, nrow, scanline); |
| 397 |
} |
| 398 |
} |
| 399 |
_TIFFfree(buf); |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
fprintf(stderr, "Cannot allocate %" TIFF_SSIZE_FORMAT " bytes.\n", |
| 404 |
stripsize); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
static void ShowTile(uint32_t row, uint32_t col, tsample_t sample, |
| 409 |
unsigned char *pp, uint32_t nrow, tsize_t rowsize) |
| 410 |
{ |
| 411 |
uint32_t cc; |
| 412 |
|
| 413 |
printf("Tile (%" PRIu32 ",%" PRIu32 "", row, col); |
| 414 |
if (sample != (tsample_t)-1) |
| 415 |
printf(",%" PRIu16, sample); |
| 416 |
printf("):\n"); |
| 417 |
while (nrow-- > 0) |
| 418 |
{ |
| 419 |
for (cc = 0; cc < (uint32_t)rowsize; cc++) |
| 420 |
{ |
| 421 |
printf(" %02x", *pp++); |
| 422 |
if (((cc + 1) % 24) == 0) |
| 423 |
putchar('\n'); |
| 424 |
} |
| 425 |
putchar('\n'); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
void TIFFReadContigTileData(TIFF *tif) |
| 430 |
{ |
| 431 |
unsigned char *buf; |
| 432 |
tmsize_t rowsize = TIFFTileRowSize(tif); |
| 433 |
tmsize_t tilesize = TIFFTileSize(tif); |
| 434 |
|
| 435 |
if (maxMalloc != 0 && tilesize > maxMalloc) |
| 436 |
{ |
| 437 |
fprintf(stderr, |
| 438 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 439 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 440 |
tilesize, maxMalloc); |
| 441 |
return; |
| 442 |
} |
| 443 |
buf = (unsigned char *)_TIFFmalloc(tilesize); |
| 444 |
if (buf) |
| 445 |
{ |
| 446 |
uint32_t tw = 0, th = 0, w = 0, h = 0; |
| 447 |
uint32_t row, col; |
| 448 |
|
| 449 |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); |
| 450 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 451 |
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); |
| 452 |
TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); |
| 453 |
if (rowsize == 0 || th > (size_t)(tilesize / rowsize)) |
| 454 |
{ |
| 455 |
fprintf(stderr, "Cannot display data: th * rowsize > tilesize\n"); |
| 456 |
_TIFFfree(buf); |
| 457 |
return; |
| 458 |
} |
| 459 |
for (row = 0; row < h; row += th) |
| 460 |
{ |
| 461 |
for (col = 0; col < w; col += tw) |
| 462 |
{ |
| 463 |
if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) |
| 464 |
{ |
| 465 |
if (stoponerr) |
| 466 |
break; |
| 467 |
} |
| 468 |
else if (showdata) |
| 469 |
ShowTile(row, col, (tsample_t)-1, buf, th, rowsize); |
| 470 |
} |
| 471 |
} |
| 472 |
_TIFFfree(buf); |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
fprintf(stderr, "Cannot allocate %" TIFF_SSIZE_FORMAT " bytes.\n", |
| 477 |
tilesize); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
void TIFFReadSeparateTileData(TIFF *tif) |
| 482 |
{ |
| 483 |
unsigned char *buf; |
| 484 |
tmsize_t rowsize = TIFFTileRowSize(tif); |
| 485 |
tmsize_t tilesize = TIFFTileSize(tif); |
| 486 |
|
| 487 |
if (maxMalloc != 0 && tilesize > maxMalloc) |
| 488 |
{ |
| 489 |
fprintf(stderr, |
| 490 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 491 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 492 |
tilesize, maxMalloc); |
| 493 |
return; |
| 494 |
} |
| 495 |
buf = (unsigned char *)_TIFFmalloc(tilesize); |
| 496 |
if (buf) |
| 497 |
{ |
| 498 |
uint32_t tw = 0, th = 0, w = 0, h = 0; |
| 499 |
uint32_t row, col; |
| 500 |
tsample_t s, samplesperpixel = 0; |
| 501 |
|
| 502 |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); |
| 503 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); |
| 504 |
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw); |
| 505 |
TIFFGetField(tif, TIFFTAG_TILELENGTH, &th); |
| 506 |
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); |
| 507 |
if (rowsize == 0 || th > (size_t)(tilesize / rowsize)) |
| 508 |
{ |
| 509 |
fprintf(stderr, "Cannot display data: th * rowsize > tilesize\n"); |
| 510 |
_TIFFfree(buf); |
| 511 |
return; |
| 512 |
} |
| 513 |
for (row = 0; row < h; row += th) |
| 514 |
{ |
| 515 |
for (col = 0; col < w; col += tw) |
| 516 |
{ |
| 517 |
for (s = 0; s < samplesperpixel; s++) |
| 518 |
{ |
| 519 |
if (TIFFReadTile(tif, buf, col, row, 0, s) < 0) |
| 520 |
{ |
| 521 |
if (stoponerr) |
| 522 |
break; |
| 523 |
} |
| 524 |
else if (showdata) |
| 525 |
ShowTile(row, col, s, buf, th, rowsize); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
_TIFFfree(buf); |
| 530 |
} |
| 531 |
else |
| 532 |
{ |
| 533 |
fprintf(stderr, "Cannot allocate %" TIFF_SSIZE_FORMAT " bytes.\n", |
| 534 |
tilesize); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
void TIFFReadData(TIFF *tif) |
| 539 |
{ |
| 540 |
uint16_t config = PLANARCONFIG_CONTIG; |
| 541 |
|
| 542 |
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config); |
| 543 |
if (TIFFIsTiled(tif)) |
| 544 |
{ |
| 545 |
if (config == PLANARCONFIG_CONTIG) |
| 546 |
TIFFReadContigTileData(tif); |
| 547 |
else |
| 548 |
TIFFReadSeparateTileData(tif); |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
if (config == PLANARCONFIG_CONTIG) |
| 553 |
TIFFReadContigStripData(tif); |
| 554 |
else |
| 555 |
TIFFReadSeparateStripData(tif); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static void ShowRawBytes(unsigned char *pp, uint32_t n) |
| 560 |
{ |
| 561 |
uint32_t i; |
| 562 |
|
| 563 |
for (i = 0; i < n; i++) |
| 564 |
{ |
| 565 |
printf(" %02x", *pp++); |
| 566 |
if (((i + 1) % 24) == 0) |
| 567 |
printf("\n "); |
| 568 |
} |
| 569 |
putchar('\n'); |
| 570 |
} |
| 571 |
|
| 572 |
static void ShowRawWords(uint16_t *pp, uint32_t n) |
| 573 |
{ |
| 574 |
uint32_t i; |
| 575 |
|
| 576 |
for (i = 0; i < n; i++) |
| 577 |
{ |
| 578 |
printf(" %04" PRIx16, *pp++); |
| 579 |
if (((i + 1) % 15) == 0) |
| 580 |
printf("\n "); |
| 581 |
} |
| 582 |
putchar('\n'); |
| 583 |
} |
| 584 |
|
| 585 |
static void TIFFReadRawDataStriped(TIFF *tif, int bitrev) |
| 586 |
{ |
| 587 |
tstrip_t nstrips = TIFFNumberOfStrips(tif); |
| 588 |
const char *what = "Strip"; |
| 589 |
uint64_t *stripbc = NULL; |
| 590 |
|
| 591 |
TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &stripbc); |
| 592 |
if (stripbc != NULL && nstrips > 0) |
| 593 |
{ |
| 594 |
uint32_t bufsize = 0; |
| 595 |
tdata_t buf = NULL; |
| 596 |
tstrip_t s; |
| 597 |
|
| 598 |
for (s = 0; s < nstrips; s++) |
| 599 |
{ |
| 600 |
if (stripbc[s] > bufsize || buf == NULL) |
| 601 |
{ |
| 602 |
tdata_t newbuf; |
| 603 |
if (maxMalloc != 0 && stripbc[s] > (uint64_t)maxMalloc) |
| 604 |
{ |
| 605 |
fprintf(stderr, |
| 606 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 607 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 608 |
(tmsize_t)stripbc[s], maxMalloc); |
| 609 |
break; |
| 610 |
} |
| 611 |
newbuf = _TIFFrealloc(buf, (tmsize_t)stripbc[s]); |
| 612 |
if (newbuf == NULL) |
| 613 |
{ |
| 614 |
fprintf(stderr, |
| 615 |
"Cannot allocate buffer to read strip %" PRIu32 |
| 616 |
"\n", |
| 617 |
s); |
| 618 |
break; |
| 619 |
} |
| 620 |
bufsize = (uint32_t)stripbc[s]; |
| 621 |
buf = newbuf; |
| 622 |
} |
| 623 |
if (TIFFReadRawStrip(tif, s, buf, (tmsize_t)stripbc[s]) < 0) |
| 624 |
{ |
| 625 |
fprintf(stderr, "Error reading strip %" PRIu32 "\n", s); |
| 626 |
if (stoponerr) |
| 627 |
break; |
| 628 |
} |
| 629 |
else if (showdata) |
| 630 |
{ |
| 631 |
if (bitrev) |
| 632 |
{ |
| 633 |
TIFFReverseBits(buf, (tmsize_t)stripbc[s]); |
| 634 |
printf("%s %" PRIu32 ": (bit reversed)\n ", what, s); |
| 635 |
} |
| 636 |
else |
| 637 |
printf("%s %" PRIu32 ":\n ", what, s); |
| 638 |
if (showwords) |
| 639 |
ShowRawWords((uint16_t *)buf, (uint32_t)stripbc[s] >> 1); |
| 640 |
else |
| 641 |
ShowRawBytes((unsigned char *)buf, (uint32_t)stripbc[s]); |
| 642 |
} |
| 643 |
} |
| 644 |
if (buf != NULL) |
| 645 |
_TIFFfree(buf); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
static void TIFFReadRawDataTiled(TIFF *tif, int bitrev) |
| 650 |
{ |
| 651 |
const char *what = "Tile"; |
| 652 |
uint32_t ntiles = TIFFNumberOfTiles(tif); |
| 653 |
uint64_t *tilebc = NULL; |
| 654 |
|
| 655 |
TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &tilebc); |
| 656 |
if (tilebc != NULL && ntiles > 0) |
| 657 |
{ |
| 658 |
uint64_t bufsize = 0; |
| 659 |
tdata_t buf = NULL; |
| 660 |
uint32_t t; |
| 661 |
|
| 662 |
for (t = 0; t < ntiles; t++) |
| 663 |
{ |
| 664 |
if (tilebc[t] > bufsize || buf == NULL) |
| 665 |
{ |
| 666 |
tdata_t newbuf; |
| 667 |
if (maxMalloc != 0 && tilebc[t] > (uint64_t)maxMalloc) |
| 668 |
{ |
| 669 |
fprintf(stderr, |
| 670 |
"Memory allocation attempt %" TIFF_SSIZE_FORMAT |
| 671 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")\n", |
| 672 |
(tmsize_t)tilebc[t], maxMalloc); |
| 673 |
break; |
| 674 |
} |
| 675 |
newbuf = _TIFFrealloc(buf, (tmsize_t)tilebc[t]); |
| 676 |
if (newbuf == NULL) |
| 677 |
{ |
| 678 |
fprintf(stderr, |
| 679 |
"Cannot allocate buffer to read tile %" PRIu32 "\n", |
| 680 |
t); |
| 681 |
break; |
| 682 |
} |
| 683 |
bufsize = (uint32_t)tilebc[t]; |
| 684 |
buf = newbuf; |
| 685 |
} |
| 686 |
if (TIFFReadRawTile(tif, t, buf, (tmsize_t)tilebc[t]) < 0) |
| 687 |
{ |
| 688 |
fprintf(stderr, "Error reading tile %" PRIu32 "\n", t); |
| 689 |
if (stoponerr) |
| 690 |
break; |
| 691 |
} |
| 692 |
else if (showdata) |
| 693 |
{ |
| 694 |
if (bitrev) |
| 695 |
{ |
| 696 |
TIFFReverseBits(buf, (tmsize_t)tilebc[t]); |
| 697 |
printf("%s %" PRIu32 ": (bit reversed)\n ", what, t); |
| 698 |
} |
| 699 |
else |
| 700 |
{ |
| 701 |
printf("%s %" PRIu32 ":\n ", what, t); |
| 702 |
} |
| 703 |
if (showwords) |
| 704 |
{ |
| 705 |
ShowRawWords((uint16_t *)buf, (uint32_t)(tilebc[t] >> 1)); |
| 706 |
} |
| 707 |
else |
| 708 |
{ |
| 709 |
ShowRawBytes((unsigned char *)buf, (uint32_t)tilebc[t]); |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
if (buf != NULL) |
| 714 |
_TIFFfree(buf); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
void TIFFReadRawData(TIFF *tif, int bitrev) |
| 719 |
{ |
| 720 |
if (TIFFIsTiled(tif)) |
| 721 |
{ |
| 722 |
TIFFReadRawDataTiled(tif, bitrev); |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
TIFFReadRawDataStriped(tif, bitrev); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
static void tiffinfo(TIFF *tif, uint16_t order, long flags, int is_image) |
| 731 |
{ |
| 732 |
TIFFPrintDirectory(tif, stdout, flags); |
| 733 |
if (!readdata || !is_image) |
| 734 |
return; |
| 735 |
if (rawdata) |
| 736 |
{ |
| 737 |
if (order) |
| 738 |
{ |
| 739 |
uint16_t o; |
| 740 |
TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &o); |
| 741 |
TIFFReadRawData(tif, o != order); |
| 742 |
} |
| 743 |
else |
| 744 |
TIFFReadRawData(tif, 0); |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
if (order) |
| 749 |
TIFFSetField(tif, TIFFTAG_FILLORDER, order); |
| 750 |
TIFFReadData(tif); |
| 751 |
} |
| 752 |
} |