1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* $Header: /usr/people/sam/tiff/tools/RCS/tiffinfo.c,v 1.26 1996/01/10 19:35:39 sam Exp $ */
/*
* Copyright (c) 1988-1996 Sam Leffler
* Copyright (c) 1991-1996 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tiffio.h"
#define streq(a,b) (strcmp(a,b) == 0)
int showdata = 0; /* show data */
int rawdata = 0; /* show raw/decoded data */
int showwords = 0; /* show data as bytes/words */
int readdata = 0; /* read data in file */
int stoponerr = 1; /* stop on first read error */
static void usage(void);
static void tiffinfo(TIFF*, uint16, long);
int
main(int argc, char* argv[])
{
int dirnum = -1, multiplefiles, c;
uint16 order = 0;
TIFF* tif;
extern int optind;
extern char* optarg;
long flags = 0;
uint32 diroff = 0;
while ((c = getopt(argc, argv, "f:o:cdDSjlmrsvw0123456789")) != -1)
switch (c) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
dirnum = atoi(&argv[optind-1][1]);
break;
case 'd':
showdata++;
/* fall thru... */
case 'D':
readdata++;
break;
case 'c':
flags |= TIFFPRINT_COLORMAP | TIFFPRINT_CURVES;
break;
case 'f': /* fill order */
if (streq(optarg, "lsb2msb"))
order = FILLORDER_LSB2MSB;
else if (streq(optarg, "msb2lsb"))
order = FILLORDER_MSB2LSB;
else
usage();
break;
case 'i':
stoponerr = 0;
break;
case 'o':
diroff = strtoul(optarg, NULL, 0);
break;
case 'j':
flags |= TIFFPRINT_JPEGQTABLES |
TIFFPRINT_JPEGACTABLES |
TIFFPRINT_JPEGDCTABLES;
break;
case 'r':
rawdata = 1;
break;
case 's':
flags |= TIFFPRINT_STRIPS;
break;
case 'w':
showwords = 1;
break;
case '?':
usage();
/*NOTREACHED*/
}
if (optind >= argc)
usage();
multiplefiles = (argc - optind > 1);
for (; optind < argc; optind++) {
if (multiplefiles)
printf("%s:\n", argv[optind]);
tif = TIFFOpen(argv[optind], "r");
if (tif != NULL) {
if (dirnum != -1) {
if (TIFFSetDirectory(tif, dirnum))
tiffinfo(tif, order, flags);
} else if (diroff != 0) {
if (TIFFSetSubDirectory(tif, diroff))
tiffinfo(tif, order, flags);
} else {
do
tiffinfo(tif, order, flags);
while (TIFFReadDirectory(tif));
}
TIFFClose(tif);
}
}
return (0);
}
char* stuff[] = {
"usage: tiffinfo [options] input...",
"where options are:",
" -D read data",
" -i ignore read errors",
" -c display data for grey/color response curve or colormap",
" -d display raw/decoded image data",
" -f lsb2msb force lsb-to-msb FillOrder for input",
" -f msb2lsb force msb-to-lsb FillOrder for input",
" -j show JPEG tables",
" -o offset set initial directory offset",
" -r read/display raw image data instead of decoded data",
" -s display strip offsets and byte counts",
" -w display raw data in words rather than bytes",
" -# set initial directory (first directory is # 0)",
NULL
};
static void
usage(void)
{
char buf[BUFSIZ];
int i;
setbuf(stderr, buf);
for (i = 0; stuff[i] != NULL; i++)
fprintf(stderr, "%s\n", stuff[i]);
exit(-1);
}
static void
ShowStrip(tstrip_t strip, unsigned char* pp, uint32 nrow, tsize_t scanline)
{
register tsize_t cc;
printf("Strip %lu:\n", (unsigned long) strip);
while (nrow-- > 0) {
for (cc = 0; cc < scanline; cc++) {
printf(" %02x", *pp++);
if (((cc+1) % 24) == 0)
putchar('\n');
}
putchar('\n');
}
}
void
TIFFReadContigStripData(TIFF* tif)
{
unsigned char *buf;
tsize_t scanline = TIFFScanlineSize(tif);
buf = (unsigned char *)_TIFFmalloc(TIFFStripSize(tif));
if (buf) {
uint32 row, h;
uint32 rowsperstrip = (uint32)-1;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
for (row = 0; row < h; row += rowsperstrip) {
uint32 nrow = (row+rowsperstrip > h ?
h-row : rowsperstrip);
tstrip_t strip = TIFFComputeStrip(tif, row, 0);
if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
if (stoponerr)
break;
} else if (showdata)
ShowStrip(strip, buf, nrow, scanline);
}
_TIFFfree(buf);
}
}
void
TIFFReadSeparateStripData(TIFF* tif)
{
unsigned char *buf;
tsize_t scanline = TIFFScanlineSize(tif);
buf = (unsigned char *)_TIFFmalloc(TIFFStripSize(tif));
if (buf) {
uint32 row, h;
uint32 rowsperstrip = (uint32)-1;
tsample_t s, samplesperpixel;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
for (row = 0; row < h; row += rowsperstrip) {
for (s = 0; s < samplesperpixel; s++) {
uint32 nrow = (row+rowsperstrip > h ?
h-row : rowsperstrip);
tstrip_t strip = TIFFComputeStrip(tif, row, s);
if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
if (stoponerr)
break;
} else if (showdata)
ShowStrip(strip, buf, nrow, scanline);
}
}
_TIFFfree(buf);
}
}
static void
ShowTile(uint32 row, uint32 col, tsample_t sample,
unsigned char* pp, uint32 nrow, uint32 rowsize)
{
register tsize_t cc;
printf("Tile (%lu,%lu", (unsigned long) row, (unsigned long) col);
if (sample != (tsample_t) -1)
printf(",%u", sample);
printf("):\n");
while (nrow-- > 0) {
for (cc = 0; cc < rowsize; cc++) {
printf(" %02x", *pp++);
if (((cc+1) % 24) == 0)
putchar('\n');
}
putchar('\n');
}
}
void
TIFFReadContigTileData(TIFF* tif)
{
unsigned char *buf;
tsize_t rowsize = TIFFTileRowSize(tif);
buf = (unsigned char *)_TIFFmalloc(TIFFTileSize(tif));
if (buf) {
uint32 tw, th, w, h;
uint32 row, col;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
for (row = 0; row < h; row += th) {
for (col = 0; col < w; col += tw) {
if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) {
if (stoponerr)
break;
} else if (showdata)
ShowTile(row, col, (tsample_t) -1, buf, th, rowsize);
}
}
_TIFFfree(buf);
}
}
void
TIFFReadSeparateTileData(TIFF* tif)
{
unsigned char *buf;
tsize_t rowsize = TIFFTileRowSize(tif);
buf = (unsigned char *)_TIFFmalloc(TIFFTileSize(tif));
if (buf) {
uint32 tw, th, w, h;
uint32 row, col;
tsample_t s, samplesperpixel;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
for (row = 0; row < h; row += th) {
for (col = 0; col < w; col += tw) {
for (s = 0; s < samplesperpixel; s++) {
if (TIFFReadTile(tif, buf, col, row, 0, s) < 0) {
if (stoponerr)
break;
} else if (showdata)
ShowTile(row, col, s, buf, th, rowsize);
}
}
}
_TIFFfree(buf);
}
}
void
TIFFReadData(TIFF* tif)
{
uint16 config;
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
if (TIFFIsTiled(tif)) {
if (config == PLANARCONFIG_CONTIG)
TIFFReadContigTileData(tif);
else
TIFFReadSeparateTileData(tif);
} else {
if (config == PLANARCONFIG_CONTIG)
TIFFReadContigStripData(tif);
else
TIFFReadSeparateStripData(tif);
}
}
static void
ShowRawBytes(unsigned char* pp, uint32 n)
{
tsize_t i;
for (i = 0; i < n; i++) {
printf(" %02x", *pp++);
if (((i+1) % 24) == 0)
printf("\n ");
}
putchar('\n');
}
static void
ShowRawWords(uint16* pp, uint32 n)
{
tsize_t i;
for (i = 0; i < n; i++) {
printf(" %04x", *pp++);
if (((i+1) % 15) == 0)
printf("\n ");
}
putchar('\n');
}
void
TIFFReadRawData(TIFF* tif, int bitrev)
{
tstrip_t nstrips = TIFFNumberOfStrips(tif);
const char* what = TIFFIsTiled(tif) ? "Tile" : "Strip";
uint32* stripbc;
TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &stripbc);
if (nstrips > 0) {
tsize_t bufsize = stripbc[0];
tdata_t buf = _TIFFmalloc(bufsize);
tstrip_t s;
for (s = 0; s < nstrips; s++) {
if (stripbc[s] > bufsize) {
buf = _TIFFrealloc(buf, stripbc[s]);
bufsize = stripbc[s];
}
if (buf == NULL) {
fprintf(stderr,
"Cannot allocate buffer to read strip %lu\n",
(unsigned long) s);
break;
}
if (TIFFReadRawStrip(tif, s, buf, stripbc[s]) < 0) {
fprintf(stderr, "Error reading strip %lu\n",
(unsigned long) s);
if (stoponerr)
break;
} else if (showdata) {
if (bitrev) {
TIFFReverseBits(buf, stripbc[s]);
printf("%s %lu: (bit reversed)\n ",
what, (unsigned long) s);
} else
printf("%s %lu:\n ", what,
(unsigned long) s);
if (showwords)
ShowRawWords((uint16*) buf, stripbc[s]>>1);
else
ShowRawBytes((unsigned char*) buf, stripbc[s]);
}
}
if (buf != NULL)
_TIFFfree(buf);
}
}
static void
tiffinfo(TIFF* tif, uint16 order, long flags)
{
TIFFPrintDirectory(tif, stdout, flags);
if (!readdata)
return;
if (rawdata) {
if (order) {
uint16 o;
TIFFGetFieldDefaulted(tif,
TIFFTAG_FILLORDER, &o);
TIFFReadRawData(tif, o != order);
} else
TIFFReadRawData(tif, 0);
} else {
if (order)
TIFFSetField(tif, TIFFTAG_FILLORDER, order);
TIFFReadData(tif);
}
}
|