4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 #include "tif_config.h"
47 static TIFFErrorHandler old_error_handler
= 0;
48 static int status
= 0; /* exit status */
49 static int showdata
= 0; /* show data */
50 static int rawdata
= 0; /* show raw/decoded data */
51 static int showwords
= 0; /* show data as bytes/words */
52 static int readdata
= 0; /* read data in file */
53 static int stoponerr
= 1; /* stop on first read error */
55 static void usage(void);
56 static void tiffinfo(TIFF
*, uint16
, long, int);
59 PrivateErrorHandler(const char* module, const char* fmt
, va_list ap
)
61 if (old_error_handler
)
62 (*old_error_handler
)(module,fmt
,ap
);
67 main(int argc
, char* argv
[])
69 int dirnum
= -1, multiplefiles
, c
;
76 int chopstrips
= 0; /* disable strip chopping */
78 while ((c
= getopt(argc
, argv
, "f:o:cdDSjilmrsvwz0123456789")) != -1)
80 case '0': case '1': case '2': case '3':
81 case '4': case '5': case '6': case '7':
83 dirnum
= atoi(&argv
[optind
-1][1]);
92 flags
|= TIFFPRINT_COLORMAP
| TIFFPRINT_CURVES
;
94 case 'f': /* fill order */
95 if (streq(optarg
, "lsb2msb"))
96 order
= FILLORDER_LSB2MSB
;
97 else if (streq(optarg
, "msb2lsb"))
98 order
= FILLORDER_MSB2LSB
;
106 diroff
= strtoul(optarg
, NULL
, 0);
109 flags
|= TIFFPRINT_JPEGQTABLES
|
110 TIFFPRINT_JPEGACTABLES
|
111 TIFFPRINT_JPEGDCTABLES
;
117 flags
|= TIFFPRINT_STRIPS
;
132 old_error_handler
= TIFFSetErrorHandler(PrivateErrorHandler
);
134 multiplefiles
= (argc
- optind
> 1);
135 for (; optind
< argc
; optind
++) {
137 printf("%s:\n", argv
[optind
]);
138 tif
= TIFFOpen(argv
[optind
], chopstrips
? "rC" : "rc");
141 if (TIFFSetDirectory(tif
, (tdir_t
) dirnum
))
142 tiffinfo(tif
, order
, flags
, 1);
143 } else if (diroff
!= 0) {
144 if (TIFFSetSubDirectory(tif
, diroff
))
145 tiffinfo(tif
, order
, flags
, 1);
150 tiffinfo(tif
, order
, flags
, 1);
151 if (TIFFGetField(tif
, TIFFTAG_EXIFIFD
,
153 if (TIFFReadEXIFDirectory(tif
, offset
)) {
154 tiffinfo(tif
, order
, flags
, 0);
157 } while (TIFFReadDirectory(tif
));
166 "usage: tiffinfo [options] input...",
167 "where options are:",
169 " -i ignore read errors",
170 " -c display data for grey/color response curve or colormap",
171 " -d display raw/decoded image data",
172 " -f lsb2msb force lsb-to-msb FillOrder for input",
173 " -f msb2lsb force msb-to-lsb FillOrder for input",
174 " -j show JPEG tables",
175 " -o offset set initial directory offset",
176 " -r read/display raw image data instead of decoded data",
177 " -s display strip offsets and byte counts",
178 " -w display raw data in words rather than bytes",
179 " -z enable strip chopping",
180 " -# set initial directory (first directory is # 0)",
191 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
192 for (i
= 0; stuff
[i
] != NULL
; i
++)
193 fprintf(stderr
, "%s\n", stuff
[i
]);
198 ShowStrip(tstrip_t strip
, unsigned char* pp
, uint32 nrow
, tsize_t scanline
)
202 printf("Strip %lu:\n", (unsigned long) strip
);
204 for (cc
= 0; cc
< scanline
; cc
++) {
205 printf(" %02x", *pp
++);
206 if (((cc
+1) % 24) == 0)
214 TIFFReadContigStripData(TIFF
* tif
)
217 tsize_t scanline
= TIFFScanlineSize(tif
);
219 buf
= (unsigned char *)_TIFFmalloc(TIFFStripSize(tif
));
222 uint32 rowsperstrip
= (uint32
)-1;
224 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
225 TIFFGetField(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
226 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
227 uint32 nrow
= (row
+rowsperstrip
> h
?
228 h
-row
: rowsperstrip
);
229 tstrip_t strip
= TIFFComputeStrip(tif
, row
, 0);
230 if (TIFFReadEncodedStrip(tif
, strip
, buf
, nrow
*scanline
) < 0) {
234 ShowStrip(strip
, buf
, nrow
, scanline
);
241 TIFFReadSeparateStripData(TIFF
* tif
)
244 tsize_t scanline
= TIFFScanlineSize(tif
);
246 buf
= (unsigned char *)_TIFFmalloc(TIFFStripSize(tif
));
249 uint32 rowsperstrip
= (uint32
)-1;
250 tsample_t s
, samplesperpixel
;
252 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
253 TIFFGetField(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
254 TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
255 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
256 for (s
= 0; s
< samplesperpixel
; s
++) {
257 uint32 nrow
= (row
+rowsperstrip
> h
?
258 h
-row
: rowsperstrip
);
259 tstrip_t strip
= TIFFComputeStrip(tif
, row
, s
);
260 if (TIFFReadEncodedStrip(tif
, strip
, buf
, nrow
*scanline
) < 0) {
264 ShowStrip(strip
, buf
, nrow
, scanline
);
272 ShowTile(uint32 row
, uint32 col
, tsample_t sample
,
273 unsigned char* pp
, uint32 nrow
, tsize_t rowsize
)
277 printf("Tile (%lu,%lu", (unsigned long) row
, (unsigned long) col
);
278 if (sample
!= (tsample_t
) -1)
279 printf(",%u", sample
);
282 for (cc
= 0; cc
< (uint32
) rowsize
; cc
++) {
283 printf(" %02x", *pp
++);
284 if (((cc
+1) % 24) == 0)
292 TIFFReadContigTileData(TIFF
* tif
)
295 tsize_t rowsize
= TIFFTileRowSize(tif
);
297 buf
= (unsigned char *)_TIFFmalloc(TIFFTileSize(tif
));
302 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &w
);
303 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
304 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
305 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
306 for (row
= 0; row
< h
; row
+= th
) {
307 for (col
= 0; col
< w
; col
+= tw
) {
308 if (TIFFReadTile(tif
, buf
, col
, row
, 0, 0) < 0) {
312 ShowTile(row
, col
, (tsample_t
) -1, buf
, th
, rowsize
);
320 TIFFReadSeparateTileData(TIFF
* tif
)
323 tsize_t rowsize
= TIFFTileRowSize(tif
);
325 buf
= (unsigned char *)_TIFFmalloc(TIFFTileSize(tif
));
329 tsample_t s
, samplesperpixel
;
331 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &w
);
332 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &h
);
333 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
334 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
335 TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
336 for (row
= 0; row
< h
; row
+= th
) {
337 for (col
= 0; col
< w
; col
+= tw
) {
338 for (s
= 0; s
< samplesperpixel
; s
++) {
339 if (TIFFReadTile(tif
, buf
, col
, row
, 0, s
) < 0) {
343 ShowTile(row
, col
, s
, buf
, th
, rowsize
);
352 TIFFReadData(TIFF
* tif
)
354 uint16 config
= PLANARCONFIG_CONTIG
;
356 TIFFGetField(tif
, TIFFTAG_PLANARCONFIG
, &config
);
357 if (TIFFIsTiled(tif
)) {
358 if (config
== PLANARCONFIG_CONTIG
)
359 TIFFReadContigTileData(tif
);
361 TIFFReadSeparateTileData(tif
);
363 if (config
== PLANARCONFIG_CONTIG
)
364 TIFFReadContigStripData(tif
);
366 TIFFReadSeparateStripData(tif
);
371 ShowRawBytes(unsigned char* pp
, uint32 n
)
375 for (i
= 0; i
< n
; i
++) {
376 printf(" %02x", *pp
++);
377 if (((i
+1) % 24) == 0)
384 ShowRawWords(uint16
* pp
, uint32 n
)
388 for (i
= 0; i
< n
; i
++) {
389 printf(" %04x", *pp
++);
390 if (((i
+1) % 15) == 0)
397 TIFFReadRawData(TIFF
* tif
, int bitrev
)
399 tstrip_t nstrips
= TIFFNumberOfStrips(tif
);
400 const char* what
= TIFFIsTiled(tif
) ? "Tile" : "Strip";
403 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &stripbc
);
405 uint32 bufsize
= (uint32
) stripbc
[0];
406 tdata_t buf
= _TIFFmalloc(bufsize
);
409 for (s
= 0; s
< nstrips
; s
++) {
410 if (stripbc
[s
] > bufsize
) {
411 buf
= _TIFFrealloc(buf
, (tmsize_t
)stripbc
[s
]);
412 bufsize
= (uint32
) stripbc
[s
];
416 "Cannot allocate buffer to read strip %lu\n",
420 if (TIFFReadRawStrip(tif
, s
, buf
, (tmsize_t
) stripbc
[s
]) < 0) {
421 fprintf(stderr
, "Error reading strip %lu\n",
425 } else if (showdata
) {
427 TIFFReverseBits(buf
, (tmsize_t
)stripbc
[s
]);
428 printf("%s %lu: (bit reversed)\n ",
429 what
, (unsigned long) s
);
431 printf("%s %lu:\n ", what
,
434 ShowRawWords((uint16
*) buf
, (uint32
) stripbc
[s
]>>1);
436 ShowRawBytes((unsigned char*) buf
, (uint32
) stripbc
[s
]);
445 tiffinfo(TIFF
* tif
, uint16 order
, long flags
, int is_image
)
447 TIFFPrintDirectory(tif
, stdout
, flags
);
448 if (!readdata
|| !is_image
)
453 TIFFGetFieldDefaulted(tif
,
454 TIFFTAG_FILLORDER
, &o
);
455 TIFFReadRawData(tif
, o
!= order
);
457 TIFFReadRawData(tif
, 0);
460 TIFFSetField(tif
, TIFFTAG_FILLORDER
, order
);
465 /* vim: set ts=8 sts=8 sw=8 noet: */