3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 #include "tif_config.h"
46 static TIFFErrorHandler old_error_handler
= 0;
47 static int status
= 0; /* exit status */
48 static int showdata
= 0; /* show data */
49 static int rawdata
= 0; /* show raw/decoded data */
50 static int showwords
= 0; /* show data as bytes/words */
51 static int readdata
= 0; /* read data in file */
52 static int stoponerr
= 1; /* stop on first read error */
54 static void usage(void);
55 static void tiffinfo(TIFF
*, uint16
, long, int);
58 PrivateErrorHandler(const char* module, const char* fmt
, va_list ap
)
60 if (old_error_handler
)
61 (*old_error_handler
)(module,fmt
,ap
);
66 main(int argc
, char* argv
[])
68 int dirnum
= -1, multiplefiles
, c
;
75 int chopstrips
= 0; /* disable strip chopping */
77 while ((c
= getopt(argc
, argv
, "f:o:cdDSjilmrsvwz0123456789")) != -1)
79 case '0': case '1': case '2': case '3':
80 case '4': case '5': case '6': case '7':
82 dirnum
= atoi(&argv
[optind
-1][1]);
91 flags
|= TIFFPRINT_COLORMAP
| TIFFPRINT_CURVES
;
93 case 'f': /* fill order */
94 if (streq(optarg
, "lsb2msb"))
95 order
= FILLORDER_LSB2MSB
;
96 else if (streq(optarg
, "msb2lsb"))
97 order
= FILLORDER_MSB2LSB
;
105 diroff
= strtoul(optarg
, NULL
, 0);
108 flags
|= TIFFPRINT_JPEGQTABLES
|
109 TIFFPRINT_JPEGACTABLES
|
110 TIFFPRINT_JPEGDCTABLES
;
116 flags
|= TIFFPRINT_STRIPS
;
131 old_error_handler
= TIFFSetErrorHandler(PrivateErrorHandler
);
133 multiplefiles
= (argc
- optind
> 1);
134 for (; optind
< argc
; optind
++) {
136 printf("%s:\n", argv
[optind
]);
137 tif
= TIFFOpen(argv
[optind
], chopstrips
? "rC" : "rc");
140 if (TIFFSetDirectory(tif
, (tdir_t
) dirnum
))
141 tiffinfo(tif
, order
, flags
, 1);
142 } else if (diroff
!= 0) {
143 if (TIFFSetSubDirectory(tif
, diroff
))
144 tiffinfo(tif
, order
, flags
, 1);
149 tiffinfo(tif
, order
, flags
, 1);
150 if (TIFFGetField(tif
, TIFFTAG_EXIFIFD
,
152 if (TIFFReadEXIFDirectory(tif
, offset
)) {
153 tiffinfo(tif
, order
, flags
, 0);
156 } while (TIFFReadDirectory(tif
));
165 "usage: tiffinfo [options] input...",
166 "where options are:",
168 " -i ignore read errors",
169 " -c display data for grey/color response curve or colormap",
170 " -d display raw/decoded image data",
171 " -f lsb2msb force lsb-to-msb FillOrder for input",
172 " -f msb2lsb force msb-to-lsb FillOrder for input",
173 " -j show JPEG tables",
174 " -o offset set initial directory offset",
175 " -r read/display raw image data instead of decoded data",
176 " -s display strip offsets and byte counts",
177 " -w display raw data in words rather than bytes",
178 " -z enable strip chopping",
179 " -# set initial directory (first directory is # 0)",
190 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
191 for (i
= 0; stuff
[i
] != NULL
; i
++)
192 fprintf(stderr
, "%s\n", stuff
[i
]);
197 ShowStrip(tstrip_t strip
, unsigned char* pp
, uint32 nrow
, tsize_t scanline
)
201 printf("Strip %lu:\n", (unsigned long) strip
);
203 for (cc
= 0; cc
< scanline
; cc
++) {
204 printf(" %02x", *pp
++);
205 if (((cc
+1) % 24) == 0)
213 TIFFReadContigStripData(TIFF
* tif
)
216 tsize_t scanline
= TIFFScanlineSize(tif
);
218 buf
= (unsigned char *)_TIFFmalloc(TIFFStripSize(tif
));
221 uint32 rowsperstrip
= (uint32
)-1;
223 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
224 TIFFGetField(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
225 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
226 uint32 nrow
= (row
+rowsperstrip
> h
?
227 h
-row
: rowsperstrip
);
228 tstrip_t strip
= TIFFComputeStrip(tif
, row
, 0);
229 if (TIFFReadEncodedStrip(tif
, strip
, buf
, nrow
*scanline
) < 0) {
233 ShowStrip(strip
, buf
, nrow
, scanline
);
240 TIFFReadSeparateStripData(TIFF
* tif
)
243 tsize_t scanline
= TIFFScanlineSize(tif
);
245 buf
= (unsigned char *)_TIFFmalloc(TIFFStripSize(tif
));
248 uint32 rowsperstrip
= (uint32
)-1;
249 tsample_t s
, samplesperpixel
;
251 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
252 TIFFGetField(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
253 TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
254 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
255 for (s
= 0; s
< samplesperpixel
; s
++) {
256 uint32 nrow
= (row
+rowsperstrip
> h
?
257 h
-row
: rowsperstrip
);
258 tstrip_t strip
= TIFFComputeStrip(tif
, row
, s
);
259 if (TIFFReadEncodedStrip(tif
, strip
, buf
, nrow
*scanline
) < 0) {
263 ShowStrip(strip
, buf
, nrow
, scanline
);
271 ShowTile(uint32 row
, uint32 col
, tsample_t sample
,
272 unsigned char* pp
, uint32 nrow
, tsize_t rowsize
)
276 printf("Tile (%lu,%lu", (unsigned long) row
, (unsigned long) col
);
277 if (sample
!= (tsample_t
) -1)
278 printf(",%u", sample
);
281 for (cc
= 0; cc
< (uint32
) rowsize
; cc
++) {
282 printf(" %02x", *pp
++);
283 if (((cc
+1) % 24) == 0)
291 TIFFReadContigTileData(TIFF
* tif
)
294 tsize_t rowsize
= TIFFTileRowSize(tif
);
296 buf
= (unsigned char *)_TIFFmalloc(TIFFTileSize(tif
));
301 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &w
);
302 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
303 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
304 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
305 for (row
= 0; row
< h
; row
+= th
) {
306 for (col
= 0; col
< w
; col
+= tw
) {
307 if (TIFFReadTile(tif
, buf
, col
, row
, 0, 0) < 0) {
311 ShowTile(row
, col
, (tsample_t
) -1, buf
, th
, rowsize
);
319 TIFFReadSeparateTileData(TIFF
* tif
)
322 tsize_t rowsize
= TIFFTileRowSize(tif
);
324 buf
= (unsigned char *)_TIFFmalloc(TIFFTileSize(tif
));
328 tsample_t s
, samplesperpixel
;
330 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &w
);
331 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
332 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
333 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
334 TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
335 for (row
= 0; row
< h
; row
+= th
) {
336 for (col
= 0; col
< w
; col
+= tw
) {
337 for (s
= 0; s
< samplesperpixel
; s
++) {
338 if (TIFFReadTile(tif
, buf
, col
, row
, 0, s
) < 0) {
342 ShowTile(row
, col
, s
, buf
, th
, rowsize
);
351 TIFFReadData(TIFF
* tif
)
353 uint16 config
= PLANARCONFIG_CONTIG
;
355 TIFFGetField(tif
, TIFFTAG_PLANARCONFIG
, &config
);
356 if (TIFFIsTiled(tif
)) {
357 if (config
== PLANARCONFIG_CONTIG
)
358 TIFFReadContigTileData(tif
);
360 TIFFReadSeparateTileData(tif
);
362 if (config
== PLANARCONFIG_CONTIG
)
363 TIFFReadContigStripData(tif
);
365 TIFFReadSeparateStripData(tif
);
370 ShowRawBytes(unsigned char* pp
, uint32 n
)
374 for (i
= 0; i
< n
; i
++) {
375 printf(" %02x", *pp
++);
376 if (((i
+1) % 24) == 0)
383 ShowRawWords(uint16
* pp
, uint32 n
)
387 for (i
= 0; i
< n
; i
++) {
388 printf(" %04x", *pp
++);
389 if (((i
+1) % 15) == 0)
396 TIFFReadRawData(TIFF
* tif
, int bitrev
)
398 tstrip_t nstrips
= TIFFNumberOfStrips(tif
);
399 const char* what
= TIFFIsTiled(tif
) ? "Tile" : "Strip";
402 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &stripbc
);
404 uint32 bufsize
= (uint32
) stripbc
[0];
405 tdata_t buf
= _TIFFmalloc(bufsize
);
408 for (s
= 0; s
< nstrips
; s
++) {
409 if (stripbc
[s
] > bufsize
) {
410 buf
= _TIFFrealloc(buf
, (tmsize_t
)stripbc
[s
]);
411 bufsize
= (uint32
) stripbc
[s
];
415 "Cannot allocate buffer to read strip %lu\n",
419 if (TIFFReadRawStrip(tif
, s
, buf
, (tmsize_t
) stripbc
[s
]) < 0) {
420 fprintf(stderr
, "Error reading strip %lu\n",
424 } else if (showdata
) {
426 TIFFReverseBits(buf
, (tmsize_t
)stripbc
[s
]);
427 printf("%s %lu: (bit reversed)\n ",
428 what
, (unsigned long) s
);
430 printf("%s %lu:\n ", what
,
433 ShowRawWords((uint16
*) buf
, (uint32
) stripbc
[s
]>>1);
435 ShowRawBytes((unsigned char*) buf
, (uint32
) stripbc
[s
]);
444 tiffinfo(TIFF
* tif
, uint16 order
, long flags
, int is_image
)
446 TIFFPrintDirectory(tif
, stdout
, flags
);
447 if (!readdata
|| !is_image
)
452 TIFFGetFieldDefaulted(tif
,
453 TIFFTAG_FILLORDER
, &o
);
454 TIFFReadRawData(tif
, o
!= order
);
456 TIFFReadRawData(tif
, 0);
459 TIFFSetField(tif
, TIFFTAG_FILLORDER
, order
);
464 /* vim: set ts=8 sts=8 sw=8 noet: */