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
|
package jogamp.opengl.util.pngj.chunks;
import java.util.ArrayList;
import java.util.List;
import jogamp.opengl.util.pngj.PngHelperInternal;
import jogamp.opengl.util.pngj.PngjException;
/**
* We consider "image metadata" every info inside the image except for the most
* basic image info (IHDR chunk - ImageInfo class) and the pixels values.
* <p>
* This includes the palette (if present) and all the ancillary chunks
* <p>
* This class provides a wrapper over the collection of chunks of a image (read
* or to write) and provides some high level methods to access them
*/
public class PngMetadata {
private final ChunksList chunkList;
private final boolean readonly;
public PngMetadata(ChunksList chunks) {
this.chunkList = chunks;
if (chunks instanceof ChunksListForWrite) {
this.readonly = false;
} else {
this.readonly = true;
}
}
/**
* Queues the chunk at the writer
* <p>
* lazyOverwrite: if true, checks if there is a queued "equivalent" chunk
* and if so, overwrites it. However if that not check for already written
* chunks.
*/
public void queueChunk(final PngChunk c, boolean lazyOverwrite) {
ChunksListForWrite cl = getChunkListW();
if (readonly)
throw new PngjException("cannot set chunk : readonly metadata");
if (lazyOverwrite) {
ChunkHelper.trimList(cl.getQueuedChunks(), new ChunkPredicate() {
public boolean match(PngChunk c2) {
return ChunkHelper.equivalent(c, c2);
}
});
}
cl.queue(c);
}
public void queueChunk(final PngChunk c) {
queueChunk(c, true);
}
private ChunksListForWrite getChunkListW() {
return (ChunksListForWrite) chunkList;
}
// ///// high level utility methods follow ////////////
// //////////// DPI
/**
* returns -1 if not found or dimension unknown
*/
public double[] getDpi() {
PngChunk c = chunkList.getById1(ChunkHelper.pHYs, true);
if (c == null)
return new double[] { -1, -1 };
else
return ((PngChunkPHYS) c).getAsDpi2();
}
public void setDpi(double x) {
setDpi(x, x);
}
public void setDpi(double x, double y) {
PngChunkPHYS c = new PngChunkPHYS(chunkList.imageInfo);
c.setAsDpi2(x, y);
queueChunk(c);
}
// //////////// TIME
/**
* Creates a time chunk with current time, less secsAgo seconds
* <p>
*
* @return Returns the created-queued chunk, just in case you want to
* examine or modify it
*/
public PngChunkTIME setTimeNow(int secsAgo) {
PngChunkTIME c = new PngChunkTIME(chunkList.imageInfo);
c.setNow(secsAgo);
queueChunk(c);
return c;
}
public PngChunkTIME setTimeNow() {
return setTimeNow(0);
}
/**
* Creates a time chunk with diven date-time
* <p>
*
* @return Returns the created-queued chunk, just in case you want to
* examine or modify it
*/
public PngChunkTIME setTimeYMDHMS(int yearx, int monx, int dayx, int hourx, int minx, int secx) {
PngChunkTIME c = new PngChunkTIME(chunkList.imageInfo);
c.setYMDHMS(yearx, monx, dayx, hourx, minx, secx);
queueChunk(c, true);
return c;
}
/**
* null if not found
*/
public PngChunkTIME getTime() {
return (PngChunkTIME) chunkList.getById1(ChunkHelper.tIME);
}
public String getTimeAsString() {
PngChunkTIME c = getTime();
return c == null ? "" : c.getAsString();
}
// //////////// TEXT
/**
* Creates a text chunk and queue it.
* <p>
*
* @param k
* : key (latin1)
* @param val
* (arbitrary, should be latin1 if useLatin1)
* @param useLatin1
* @param compress
* @return Returns the created-queued chunks, just in case you want to
* examine, touch it
*/
public PngChunkTextVar setText(String k, String val, boolean useLatin1, boolean compress) {
if (compress && !useLatin1)
throw new PngjException("cannot compress non latin text");
PngChunkTextVar c;
if (useLatin1) {
if (compress) {
c = new PngChunkZTXT(chunkList.imageInfo);
} else {
c = new PngChunkTEXT(chunkList.imageInfo);
}
} else {
c = new PngChunkITXT(chunkList.imageInfo);
((PngChunkITXT) c).setLangtag(k); // we use the same orig tag (this is not quite right)
}
c.setKeyVal(k, val);
queueChunk(c, true);
return c;
}
public PngChunkTextVar setText(String k, String val) {
return setText(k, val, false, false);
}
/**
* gets all text chunks with a given key
* <p>
* returns null if not found
* <p>
* Warning: this does not check the "lang" key of iTxt
*/
@SuppressWarnings("unchecked")
public List<? extends PngChunkTextVar> getTxtsForKey(String k) {
@SuppressWarnings("rawtypes")
List c = new ArrayList();
c.addAll(chunkList.getById(ChunkHelper.tEXt, k));
c.addAll(chunkList.getById(ChunkHelper.zTXt, k));
c.addAll(chunkList.getById(ChunkHelper.iTXt, k));
return c;
}
/**
* Returns empty if not found, concatenated (with newlines) if multiple! -
* and trimmed
* <p>
* Use getTxtsForKey() if you don't want this behaviour
*/
public String getTxtForKey(String k) {
List<? extends PngChunkTextVar> li = getTxtsForKey(k);
if (li.isEmpty())
return "";
StringBuilder t = new StringBuilder();
for (PngChunkTextVar c : li)
t.append(c.getVal()).append("\n");
return t.toString().trim();
}
/**
* Returns the palette chunk, if present
*
* @return null if not present
*/
public PngChunkPLTE getPLTE() {
return (PngChunkPLTE) chunkList.getById1(PngChunkPLTE.ID);
}
/**
* Creates a new empty palette chunk, queues it for write and return it to
* the caller, who should fill its entries
*/
public PngChunkPLTE createPLTEChunk() {
PngChunkPLTE plte = new PngChunkPLTE(chunkList.imageInfo);
queueChunk(plte);
return plte;
}
/**
* Returns the TRNS chunk, if present
*
* @return null if not present
*/
public PngChunkTRNS getTRNS() {
return (PngChunkTRNS) chunkList.getById1(PngChunkTRNS.ID);
}
/**
* Creates a new empty TRNS chunk, queues it for write and return it to the
* caller, who should fill its entries
*/
public PngChunkTRNS createTRNSChunk() {
PngChunkTRNS trns = new PngChunkTRNS(chunkList.imageInfo);
queueChunk(trns);
return trns;
}
}
|