]>
Commit | Line | Data |
---|---|---|
8414a40c VZ |
1 | /* $Id$ */ |
2 | ||
3 | /* | |
4 | * Copyright (c) 1988-1997 Sam Leffler | |
5 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. | |
6 | * | |
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. | |
14 | * | |
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. | |
18 | * | |
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 | |
24 | * OF THIS SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include "tif_config.h" | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
33 | #ifdef HAVE_STRINGS_H | |
34 | # include <strings.h> | |
35 | #endif | |
36 | ||
37 | #ifdef HAVE_UNISTD_H | |
38 | # include <unistd.h> | |
39 | #endif | |
40 | ||
80ed523f VZ |
41 | #ifdef NEED_LIBPORT |
42 | # include "libport.h" | |
43 | #endif | |
8414a40c | 44 | |
80ed523f | 45 | #include "tiffiop.h" |
8414a40c | 46 | |
80ed523f VZ |
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 */ | |
8414a40c VZ |
54 | |
55 | static void usage(void); | |
80ed523f VZ |
56 | static void tiffinfo(TIFF*, uint16, long, int); |
57 | ||
58 | static void | |
59 | PrivateErrorHandler(const char* module, const char* fmt, va_list ap) | |
60 | { | |
61 | if (old_error_handler) | |
62 | (*old_error_handler)(module,fmt,ap); | |
63 | status = 1; | |
64 | } | |
8414a40c VZ |
65 | |
66 | int | |
67 | main(int argc, char* argv[]) | |
68 | { | |
69 | int dirnum = -1, multiplefiles, c; | |
70 | uint16 order = 0; | |
71 | TIFF* tif; | |
72 | extern int optind; | |
73 | extern char* optarg; | |
74 | long flags = 0; | |
80ed523f | 75 | uint64 diroff = 0; |
8414a40c VZ |
76 | int chopstrips = 0; /* disable strip chopping */ |
77 | ||
78 | while ((c = getopt(argc, argv, "f:o:cdDSjilmrsvwz0123456789")) != -1) | |
79 | switch (c) { | |
80 | case '0': case '1': case '2': case '3': | |
81 | case '4': case '5': case '6': case '7': | |
82 | case '8': case '9': | |
83 | dirnum = atoi(&argv[optind-1][1]); | |
84 | break; | |
85 | case 'd': | |
86 | showdata++; | |
87 | /* fall thru... */ | |
88 | case 'D': | |
89 | readdata++; | |
90 | break; | |
91 | case 'c': | |
92 | flags |= TIFFPRINT_COLORMAP | TIFFPRINT_CURVES; | |
93 | break; | |
94 | case 'f': /* fill order */ | |
95 | if (streq(optarg, "lsb2msb")) | |
96 | order = FILLORDER_LSB2MSB; | |
97 | else if (streq(optarg, "msb2lsb")) | |
98 | order = FILLORDER_MSB2LSB; | |
99 | else | |
100 | usage(); | |
101 | break; | |
102 | case 'i': | |
103 | stoponerr = 0; | |
104 | break; | |
105 | case 'o': | |
106 | diroff = strtoul(optarg, NULL, 0); | |
107 | break; | |
108 | case 'j': | |
109 | flags |= TIFFPRINT_JPEGQTABLES | | |
110 | TIFFPRINT_JPEGACTABLES | | |
111 | TIFFPRINT_JPEGDCTABLES; | |
112 | break; | |
113 | case 'r': | |
114 | rawdata = 1; | |
115 | break; | |
116 | case 's': | |
117 | flags |= TIFFPRINT_STRIPS; | |
118 | break; | |
119 | case 'w': | |
120 | showwords = 1; | |
121 | break; | |
122 | case 'z': | |
123 | chopstrips = 1; | |
124 | break; | |
125 | case '?': | |
126 | usage(); | |
127 | /*NOTREACHED*/ | |
128 | } | |
129 | if (optind >= argc) | |
130 | usage(); | |
80ed523f VZ |
131 | |
132 | old_error_handler = TIFFSetErrorHandler(PrivateErrorHandler); | |
133 | ||
8414a40c VZ |
134 | multiplefiles = (argc - optind > 1); |
135 | for (; optind < argc; optind++) { | |
136 | if (multiplefiles) | |
137 | printf("%s:\n", argv[optind]); | |
138 | tif = TIFFOpen(argv[optind], chopstrips ? "rC" : "rc"); | |
139 | if (tif != NULL) { | |
140 | if (dirnum != -1) { | |
141 | if (TIFFSetDirectory(tif, (tdir_t) dirnum)) | |
80ed523f | 142 | tiffinfo(tif, order, flags, 1); |
8414a40c VZ |
143 | } else if (diroff != 0) { |
144 | if (TIFFSetSubDirectory(tif, diroff)) | |
80ed523f | 145 | tiffinfo(tif, order, flags, 1); |
8414a40c VZ |
146 | } else { |
147 | do { | |
80ed523f | 148 | toff_t offset; |
8414a40c | 149 | |
80ed523f | 150 | tiffinfo(tif, order, flags, 1); |
8414a40c VZ |
151 | if (TIFFGetField(tif, TIFFTAG_EXIFIFD, |
152 | &offset)) { | |
80ed523f VZ |
153 | if (TIFFReadEXIFDirectory(tif, offset)) { |
154 | tiffinfo(tif, order, flags, 0); | |
155 | } | |
8414a40c VZ |
156 | } |
157 | } while (TIFFReadDirectory(tif)); | |
158 | } | |
159 | TIFFClose(tif); | |
160 | } | |
161 | } | |
80ed523f | 162 | return (status); |
8414a40c VZ |
163 | } |
164 | ||
165 | char* stuff[] = { | |
166 | "usage: tiffinfo [options] input...", | |
167 | "where options are:", | |
168 | " -D read data", | |
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)", | |
181 | NULL | |
182 | }; | |
183 | ||
184 | static void | |
185 | usage(void) | |
186 | { | |
187 | char buf[BUFSIZ]; | |
188 | int i; | |
189 | ||
190 | setbuf(stderr, buf); | |
191 | fprintf(stderr, "%s\n\n", TIFFGetVersion()); | |
192 | for (i = 0; stuff[i] != NULL; i++) | |
193 | fprintf(stderr, "%s\n", stuff[i]); | |
194 | exit(-1); | |
195 | } | |
196 | ||
197 | static void | |
198 | ShowStrip(tstrip_t strip, unsigned char* pp, uint32 nrow, tsize_t scanline) | |
199 | { | |
200 | register tsize_t cc; | |
201 | ||
202 | printf("Strip %lu:\n", (unsigned long) strip); | |
203 | while (nrow-- > 0) { | |
204 | for (cc = 0; cc < scanline; cc++) { | |
205 | printf(" %02x", *pp++); | |
206 | if (((cc+1) % 24) == 0) | |
207 | putchar('\n'); | |
208 | } | |
209 | putchar('\n'); | |
210 | } | |
211 | } | |
212 | ||
213 | void | |
214 | TIFFReadContigStripData(TIFF* tif) | |
215 | { | |
216 | unsigned char *buf; | |
217 | tsize_t scanline = TIFFScanlineSize(tif); | |
218 | ||
219 | buf = (unsigned char *)_TIFFmalloc(TIFFStripSize(tif)); | |
220 | if (buf) { | |
80ed523f | 221 | uint32 row, h=0; |
8414a40c VZ |
222 | uint32 rowsperstrip = (uint32)-1; |
223 | ||
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) { | |
231 | if (stoponerr) | |
232 | break; | |
233 | } else if (showdata) | |
234 | ShowStrip(strip, buf, nrow, scanline); | |
235 | } | |
236 | _TIFFfree(buf); | |
237 | } | |
238 | } | |
239 | ||
240 | void | |
241 | TIFFReadSeparateStripData(TIFF* tif) | |
242 | { | |
243 | unsigned char *buf; | |
244 | tsize_t scanline = TIFFScanlineSize(tif); | |
245 | ||
246 | buf = (unsigned char *)_TIFFmalloc(TIFFStripSize(tif)); | |
247 | if (buf) { | |
248 | uint32 row, h; | |
249 | uint32 rowsperstrip = (uint32)-1; | |
250 | tsample_t s, samplesperpixel; | |
251 | ||
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) { | |
261 | if (stoponerr) | |
262 | break; | |
263 | } else if (showdata) | |
264 | ShowStrip(strip, buf, nrow, scanline); | |
265 | } | |
266 | } | |
267 | _TIFFfree(buf); | |
268 | } | |
269 | } | |
270 | ||
271 | static void | |
272 | ShowTile(uint32 row, uint32 col, tsample_t sample, | |
80ed523f | 273 | unsigned char* pp, uint32 nrow, tsize_t rowsize) |
8414a40c VZ |
274 | { |
275 | uint32 cc; | |
276 | ||
277 | printf("Tile (%lu,%lu", (unsigned long) row, (unsigned long) col); | |
278 | if (sample != (tsample_t) -1) | |
279 | printf(",%u", sample); | |
280 | printf("):\n"); | |
281 | while (nrow-- > 0) { | |
80ed523f | 282 | for (cc = 0; cc < (uint32) rowsize; cc++) { |
8414a40c VZ |
283 | printf(" %02x", *pp++); |
284 | if (((cc+1) % 24) == 0) | |
285 | putchar('\n'); | |
286 | } | |
287 | putchar('\n'); | |
288 | } | |
289 | } | |
290 | ||
291 | void | |
292 | TIFFReadContigTileData(TIFF* tif) | |
293 | { | |
294 | unsigned char *buf; | |
295 | tsize_t rowsize = TIFFTileRowSize(tif); | |
296 | ||
297 | buf = (unsigned char *)_TIFFmalloc(TIFFTileSize(tif)); | |
298 | if (buf) { | |
299 | uint32 tw, th, w, h; | |
300 | uint32 row, col; | |
301 | ||
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) { | |
309 | if (stoponerr) | |
310 | break; | |
311 | } else if (showdata) | |
312 | ShowTile(row, col, (tsample_t) -1, buf, th, rowsize); | |
313 | } | |
314 | } | |
315 | _TIFFfree(buf); | |
316 | } | |
317 | } | |
318 | ||
319 | void | |
320 | TIFFReadSeparateTileData(TIFF* tif) | |
321 | { | |
322 | unsigned char *buf; | |
323 | tsize_t rowsize = TIFFTileRowSize(tif); | |
324 | ||
325 | buf = (unsigned char *)_TIFFmalloc(TIFFTileSize(tif)); | |
326 | if (buf) { | |
327 | uint32 tw, th, w, h; | |
328 | uint32 row, col; | |
329 | tsample_t s, samplesperpixel; | |
330 | ||
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) { | |
340 | if (stoponerr) | |
341 | break; | |
342 | } else if (showdata) | |
343 | ShowTile(row, col, s, buf, th, rowsize); | |
344 | } | |
345 | } | |
346 | } | |
347 | _TIFFfree(buf); | |
348 | } | |
349 | } | |
350 | ||
351 | void | |
352 | TIFFReadData(TIFF* tif) | |
353 | { | |
80ed523f | 354 | uint16 config = PLANARCONFIG_CONTIG; |
8414a40c VZ |
355 | |
356 | TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config); | |
357 | if (TIFFIsTiled(tif)) { | |
358 | if (config == PLANARCONFIG_CONTIG) | |
359 | TIFFReadContigTileData(tif); | |
360 | else | |
361 | TIFFReadSeparateTileData(tif); | |
362 | } else { | |
363 | if (config == PLANARCONFIG_CONTIG) | |
364 | TIFFReadContigStripData(tif); | |
365 | else | |
366 | TIFFReadSeparateStripData(tif); | |
367 | } | |
368 | } | |
369 | ||
370 | static void | |
371 | ShowRawBytes(unsigned char* pp, uint32 n) | |
372 | { | |
373 | uint32 i; | |
374 | ||
375 | for (i = 0; i < n; i++) { | |
376 | printf(" %02x", *pp++); | |
377 | if (((i+1) % 24) == 0) | |
378 | printf("\n "); | |
379 | } | |
380 | putchar('\n'); | |
381 | } | |
382 | ||
383 | static void | |
384 | ShowRawWords(uint16* pp, uint32 n) | |
385 | { | |
386 | uint32 i; | |
387 | ||
388 | for (i = 0; i < n; i++) { | |
389 | printf(" %04x", *pp++); | |
390 | if (((i+1) % 15) == 0) | |
391 | printf("\n "); | |
392 | } | |
393 | putchar('\n'); | |
394 | } | |
395 | ||
396 | void | |
397 | TIFFReadRawData(TIFF* tif, int bitrev) | |
398 | { | |
399 | tstrip_t nstrips = TIFFNumberOfStrips(tif); | |
400 | const char* what = TIFFIsTiled(tif) ? "Tile" : "Strip"; | |
80ed523f | 401 | uint64* stripbc; |
8414a40c VZ |
402 | |
403 | TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &stripbc); | |
404 | if (nstrips > 0) { | |
80ed523f | 405 | uint32 bufsize = (uint32) stripbc[0]; |
8414a40c VZ |
406 | tdata_t buf = _TIFFmalloc(bufsize); |
407 | tstrip_t s; | |
408 | ||
409 | for (s = 0; s < nstrips; s++) { | |
410 | if (stripbc[s] > bufsize) { | |
80ed523f VZ |
411 | buf = _TIFFrealloc(buf, (tmsize_t)stripbc[s]); |
412 | bufsize = (uint32) stripbc[s]; | |
8414a40c VZ |
413 | } |
414 | if (buf == NULL) { | |
415 | fprintf(stderr, | |
416 | "Cannot allocate buffer to read strip %lu\n", | |
417 | (unsigned long) s); | |
418 | break; | |
419 | } | |
80ed523f | 420 | if (TIFFReadRawStrip(tif, s, buf, (tmsize_t) stripbc[s]) < 0) { |
8414a40c VZ |
421 | fprintf(stderr, "Error reading strip %lu\n", |
422 | (unsigned long) s); | |
423 | if (stoponerr) | |
424 | break; | |
425 | } else if (showdata) { | |
426 | if (bitrev) { | |
80ed523f | 427 | TIFFReverseBits(buf, (tmsize_t)stripbc[s]); |
8414a40c VZ |
428 | printf("%s %lu: (bit reversed)\n ", |
429 | what, (unsigned long) s); | |
430 | } else | |
431 | printf("%s %lu:\n ", what, | |
432 | (unsigned long) s); | |
433 | if (showwords) | |
80ed523f | 434 | ShowRawWords((uint16*) buf, (uint32) stripbc[s]>>1); |
8414a40c | 435 | else |
80ed523f | 436 | ShowRawBytes((unsigned char*) buf, (uint32) stripbc[s]); |
8414a40c VZ |
437 | } |
438 | } | |
439 | if (buf != NULL) | |
440 | _TIFFfree(buf); | |
441 | } | |
442 | } | |
443 | ||
444 | static void | |
80ed523f | 445 | tiffinfo(TIFF* tif, uint16 order, long flags, int is_image) |
8414a40c VZ |
446 | { |
447 | TIFFPrintDirectory(tif, stdout, flags); | |
80ed523f | 448 | if (!readdata || !is_image) |
8414a40c VZ |
449 | return; |
450 | if (rawdata) { | |
451 | if (order) { | |
452 | uint16 o; | |
453 | TIFFGetFieldDefaulted(tif, | |
454 | TIFFTAG_FILLORDER, &o); | |
455 | TIFFReadRawData(tif, o != order); | |
456 | } else | |
457 | TIFFReadRawData(tif, 0); | |
458 | } else { | |
459 | if (order) | |
460 | TIFFSetField(tif, TIFFTAG_FILLORDER, order); | |
461 | TIFFReadData(tif); | |
462 | } | |
463 | } | |
464 | ||
465 | /* vim: set ts=8 sts=8 sw=8 noet: */ | |
80ed523f VZ |
466 | /* |
467 | * Local Variables: | |
468 | * mode: c | |
469 | * c-basic-offset: 8 | |
470 | * fill-column: 78 | |
471 | * End: | |
472 | */ |