forked from libertiff/libertiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
171 lines (163 loc) · 5.62 KB
/
demo.cpp
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
// Define LIBERTIFF_C_FILE_READER to be able to use libertiff::CFileReader
#define LIBERTIFF_C_FILE_READER
#include "libertiff.hpp"
#include <cinttypes>
static void dumpIFD(const libertiff::Image *tiff)
{
printf("Width: %u\n", tiff->width());
printf("Height: %u\n", tiff->height());
printf("SamplesPerPixel: %u\n", tiff->samplesPerPixel());
printf("PlanarConfiguration: %u\n",
static_cast<uint32_t>(tiff->planarConfiguration()));
printf("Compression: %u\n", static_cast<uint32_t>(tiff->compression()));
printf("PhotometricInterpretation: %u\n",
static_cast<uint32_t>(tiff->photometricInterpretation()));
printf("Predictor: %u\n", tiff->predictor());
printf("SampleFormat: %u\n", static_cast<uint32_t>(tiff->sampleFormat()));
if (!tiff->isTiled())
{
printf("Organization: strip\n");
printf("Number of strips: %" PRIu64 "\n", tiff->strileCount());
printf("RowsPerStrip: %u\n", tiff->rowsPerStrip());
printf("StripOffsets: ");
for (uint64_t i = 0; i < tiff->strileCount(); ++i)
{
bool ok = true;
if (i > 0)
printf(",");
printf("%" PRIu64, tiff->strileOffset(i, ok));
}
printf("\n");
printf("StripByteCounts: ");
for (uint64_t i = 0; i < tiff->strileCount(); ++i)
{
bool ok = true;
if (i > 0)
printf(",");
printf("%" PRIu64, tiff->strileByteCount(i, ok));
}
printf("\n");
}
else
{
printf("Organization: tile\n");
printf("Number of tiles: %" PRIu64 "\n", tiff->strileCount());
printf("TileWidth: %u\n", tiff->tileWidth());
printf("TileHeight: %u\n", tiff->tileHeight());
printf("TilesPerRow: %u\n", tiff->tilesPerRow());
printf("TilesPerCol: %u\n", tiff->tilesPerCol());
printf("TileOffsets: ");
for (uint64_t i = 0; i < tiff->strileCount(); ++i)
{
bool ok = true;
if (i > 0)
printf(",");
printf("%" PRIu64, tiff->strileOffset(i, ok));
}
printf("\n");
printf("TileByteCounts: ");
for (uint64_t i = 0; i < tiff->strileCount(); ++i)
{
bool ok = true;
if (i > 0)
printf(",");
printf("%" PRIu64, tiff->strileByteCount(i, ok));
}
printf("\n");
}
const auto &tags = tiff->tags();
printf("All tags (%u):\n", static_cast<uint32_t>(tags.size()));
for (const auto &tag : tags)
{
const char *typeName = "";
if (tag.type == static_cast<uint16_t>(libertiff::Type::ASCII))
typeName = "ASCII";
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Short))
typeName = "SHORT";
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Long))
typeName = "LONG";
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Long8))
typeName = "LONG8";
printf(" Code=%d(0x%X), Type=%s(%d), Count=%" PRIu64, tag.tag, tag.tag,
typeName, tag.type, tag.count);
if (!tag.invalid_value_offset)
{
printf(", Offset of value=%" PRIu64, tag.value_offset);
}
if (tag.count < 1000)
{
if (tag.type == static_cast<uint16_t>(libertiff::Type::ASCII))
{
bool ok = true;
printf(", Value=\"%s\"",
tiff->readTagAsString(tag, ok).c_str());
}
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Short))
{
printf(", Value%s=", tag.count > 1 ? "s" : "");
bool ok = true;
const auto values = tiff->readTagAsVector<uint16_t>(tag, ok);
for (size_t i = 0; i < values.size(); ++i)
{
if (i > 0)
printf(",");
printf("%u", values[i]);
}
}
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Long))
{
printf(", Value%s=", tag.count > 1 ? "s" : "");
bool ok = true;
const auto values = tiff->readTagAsVector<uint32_t>(tag, ok);
for (size_t i = 0; i < values.size(); ++i)
{
if (i > 0)
printf(",");
printf("%u", values[i]);
}
}
else if (tag.type == static_cast<uint16_t>(libertiff::Type::Long8))
{
printf(", Value%s=", tag.count > 1 ? "s" : "");
bool ok = true;
const auto values = tiff->readTagAsVector<uint64_t>(tag, ok);
for (size_t i = 0; i < values.size(); ++i)
{
if (i > 0)
printf(",");
printf("%" PRIu64, values[i]);
}
}
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: demo my.tiff\n");
exit(1);
}
FILE *f = fopen(argv[1], "rb");
if (!f)
{
fprintf(stderr, "Cannot open %s\n", argv[1]);
exit(1);
}
auto tiff = libertiff::open(std::make_shared<libertiff::CFileReader>(f));
if (!tiff)
{
fprintf(stderr, "Not a valid TIFF file\n");
exit(1);
}
printf("File: %s\n", argv[1]);
uint32_t ifd = 0;
do
{
printf("IFD %u:\n", ifd);
dumpIFD(tiff.get());
tiff = tiff->next();
} while (tiff);
return 0;
}