-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDumpDiffIFDs.java
More file actions
199 lines (179 loc) · 7.94 KB
/
DumpDiffIFDs.java
File metadata and controls
199 lines (179 loc) · 7.94 KB
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
import mil.nga.tiff.*;
import java.io.File;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* DumpDiffIFDs
* - Lists all IFDs (including subIFDs) in a TIFF/GeoTIFF.
* - Prints dimensions, quick georef presence, and every tag id with a brief value preview.
*
* Usage:
* javac -cp "lib/*" DumpDiffIFDs.java
* java -cp ".:lib/*" DumpDiffIFDs your.tif
*/
public class DumpDiffIFDs {
// Relevant GeoTIFF tag IDs
static final int TAG_ModelPixelScale = 33550;
static final int TAG_ModelTiepoint = 33922;
static final int TAG_ModelTransformation = 34264;
static final int TAG_GeoKeyDirectory = 34735;
static final int TAG_GDAL_NODATA = 42113;
static final int TAG_GDAL_METADATA = 42112; // optional XML with <GeoTransform>...
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: java -cp .:lib/* DumpDiffIFDs <file.tif>");
System.exit(1);
}
var img = TiffReader.readTiff(new File(args[0]));
List<FileDirectory> dirs = collectAllDirectories(img);
int idx = 0;
for (FileDirectory d : dirs) {
int w = toInt(d.getImageWidth());
int h = toInt(invokeNumberGetterFallback(d, "getImageHeight", "getImageLength"));
System.out.printf("IFD #%d: %dx%d%n", idx++, w, h);
// quick georef presence
boolean hasScaleTie = (findEntryByTag(d, TAG_ModelPixelScale) != null) &&
(findEntryByTag(d, TAG_ModelTiepoint) != null);
boolean hasMT = findEntryByTag(d, TAG_ModelTransformation) != null;
boolean hasGKD = findEntryByTag(d, TAG_GeoKeyDirectory) != null;
boolean hasGMD = findEntryByTag(d, TAG_GDAL_METADATA) != null;
System.out.printf(" Georef: %s%s%s%s%n",
hasScaleTie ? "Scale+Tiepoint " : "",
hasMT ? "ModelTransformation " : "",
hasGKD ? "GeoKeyDirectory " : "",
hasGMD ? "GDAL_METADATA " : (hasScaleTie||hasMT||hasGKD ? "" : "none"));
// list all entries (id + short preview)
Set<FileDirectoryEntry> entries = d.getEntries();
if (entries == null || entries.isEmpty()) {
System.out.println(" <no entries exposed by this library>");
} else {
for (FileDirectoryEntry e : entries) {
Integer tagId = getTagId(e);
Object v = e.getValues();
String preview = previewValue(v);
System.out.printf(" tag %s -> %s%n", tagId == null ? "?" : tagId.toString(), preview);
// small bonus: if this is GDAL_METADATA or ImageDescription, try to show a <GeoTransform> line if present
if (tagId != null && (tagId == TAG_GDAL_METADATA || tagId == 270 /*ImageDescription*/)) {
String s = toAscii(v);
if (s != null) {
String gt = extractGeoTransformLine(s);
if (gt != null) System.out.println(" GeoTransform: " + gt);
}
}
}
}
System.out.println();
}
}
/* ----------------- helpers ----------------- */
static List<FileDirectory> collectAllDirectories(Object tiffImage) {
List<FileDirectory> out = new ArrayList<>();
try {
Method mRoot = tiffImage.getClass().getMethod("getFileDirectory");
Object root = mRoot.invoke(tiffImage);
if (root instanceof FileDirectory) out.add((FileDirectory) root);
} catch (Exception ignore) {}
try {
Method mList = tiffImage.getClass().getMethod("getFileDirectories");
Object v = mList.invoke(tiffImage);
if (v instanceof Collection<?>)
for (Object o : (Collection<?>) v)
if (o instanceof FileDirectory) out.add((FileDirectory) o);
} catch (Exception ignore) {}
// recurse into subIFDs if API exposes them
Set<FileDirectory> seen = new HashSet<>(out);
Deque<FileDirectory> stack = new ArrayDeque<>(out);
while (!stack.isEmpty()) {
FileDirectory d = stack.pop();
for (FileDirectory sub : readSubIFDs(d)) {
if (seen.add(sub)) { out.add(sub); stack.push(sub); }
}
}
return out;
}
static List<FileDirectory> readSubIFDs(FileDirectory d) {
List<FileDirectory> out = new ArrayList<>();
for (String name : new String[]{"getSubIFDs","getSubFileDirectories","getSubDirectories"}) {
try {
Method m = d.getClass().getMethod(name);
Object v = m.invoke(d);
if (v instanceof Collection<?>)
for (Object o : (Collection<?>) v)
if (o instanceof FileDirectory) out.add((FileDirectory) o);
} catch (Exception ignore) {}
}
return out;
}
static Integer getTagId(FileDirectoryEntry e) {
try {
// int getTag()
try {
var m = e.getClass().getMethod("getTag");
Object v = m.invoke(e);
if (v instanceof Number) return ((Number) v).intValue();
if (v != null) {
try { var mm=v.getClass().getMethod("getId"); Object id=mm.invoke(v); if (id instanceof Number) return ((Number) id).intValue(); } catch (NoSuchMethodException ignore) {}
try { var mm=v.getClass().getMethod("getValue"); Object id=mm.invoke(v); if (id instanceof Number) return ((Number) id).intValue(); } catch (NoSuchMethodException ignore) {}
try { var mm=v.getClass().getMethod("ordinal"); Object id=mm.invoke(v); if (id instanceof Number) return ((Number) id).intValue(); } catch (NoSuchMethodException ignore) {}
}
} catch (NoSuchMethodException ignore) {}
// int getTagId()
try {
var m = e.getClass().getMethod("getTagId");
Object v = m.invoke(e);
if (v instanceof Number) return ((Number) v).intValue();
} catch (NoSuchMethodException ignore) {}
} catch (Exception ignore) {}
return null;
}
static FileDirectoryEntry findEntryByTag(FileDirectory d, int tag) {
Set<FileDirectoryEntry> entries = d.getEntries();
if (entries == null) return null;
for (FileDirectoryEntry e : entries) {
Integer id = getTagId(e);
if (id != null && id == tag) return e;
}
return null;
}
static int toInt(Object n) {
if (n instanceof Number) return ((Number) n).intValue();
throw new IllegalStateException("Expected Number, got " + (n == null ? "null" : n.getClass()));
}
static Number invokeNumberGetterFallback(Object target, String primary, String secondary) throws Exception {
try { return (Number) target.getClass().getMethod(primary).invoke(target); }
catch (NoSuchMethodException e) { return (Number) target.getClass().getMethod(secondary).invoke(target); }
}
static String toAscii(Object v) {
if (v == null) return null;
if (v instanceof byte[]) return new String((byte[]) v, StandardCharsets.UTF_8);
if (v instanceof char[]) return new String((char[]) v);
String s = v.toString();
return s;
}
static String previewValue(Object v) {
if (v == null) return "null";
if (v instanceof byte[]) return "byte[" + ((byte[]) v).length + "]";
if (v instanceof short[]) return "short[" + ((short[]) v).length + "]";
if (v instanceof char[]) return "char[" + ((char[]) v).length + "]";
if (v instanceof int[]) return "int[" + ((int[]) v).length + "]";
if (v instanceof long[]) return "long[" + ((long[]) v).length + "]";
if (v instanceof float[]) return "float[" + ((float[]) v).length + "]";
if (v instanceof double[])return "double[" + ((double[]) v).length + "]";
if (v.getClass().isArray()) return v.getClass().getSimpleName();
String s = v.toString();
return s.length() > 80 ? s.substring(0,77) + "..." : s;
}
/** Pulls out a single-line GeoTransform payload if present in a metadata string (XML/WKT/JSON blobs). */
static String extractGeoTransformLine(String s) {
if (s == null) return null;
int i0 = s.indexOf("<GeoTransform>");
int i1 = s.indexOf("</GeoTransform>");
if (i0 >= 0 && i1 > i0) {
String body = s.substring(i0 + "<GeoTransform>".length(), i1).trim();
body = body.replaceAll("\\s+", " ");
return body;
}
return null;
}
}