216 * {@code null}.
217 */
218 public TIFFDirectory(TIFFTagSet[] tagSets, TIFFTag parentTag) {
219 if(tagSets == null) {
220 throw new NullPointerException("tagSets == null!");
221 }
222 this.tagSets = new ArrayList<TIFFTagSet>(tagSets.length);
223 int numTagSets = tagSets.length;
224 for(int i = 0; i < numTagSets; i++) {
225 this.tagSets.add(tagSets[i]);
226 }
227 this.parentTag = parentTag;
228 }
229
230 /**
231 * Returns the {@link TIFFTagSet}s of which this directory is aware.
232 *
233 * @return The {@code TIFFTagSet}s associated with this
234 * {@code TIFFDirectory}.
235 */
236 public TIFFTagSet[] getTagSets() {
237 return tagSets.toArray(new TIFFTagSet[tagSets.size()]);
238 }
239
240 /**
241 * Adds an element to the group of {@link TIFFTagSet}s of which this
242 * directory is aware.
243 *
244 * @param tagSet The {@code TIFFTagSet} to add.
245 * @throws NullPointerException if {@code tagSet} is
246 * {@code null}.
247 */
248 public void addTagSet(TIFFTagSet tagSet) {
249 if(tagSet == null) {
250 throw new NullPointerException("tagSet == null");
251 }
252
253 if(!tagSets.contains(tagSet)) {
254 tagSets.add(tagSet);
255 }
256 }
263 * @throws NullPointerException if {@code tagSet} is
264 * {@code null}.
265 */
266 public void removeTagSet(TIFFTagSet tagSet) {
267 if(tagSet == null) {
268 throw new NullPointerException("tagSet == null");
269 }
270
271 if(tagSets.contains(tagSet)) {
272 tagSets.remove(tagSet);
273 }
274 }
275
276 /**
277 * Returns the parent {@link TIFFTag} of this directory if one
278 * has been defined or {@code null} otherwise.
279 *
280 * @return The parent {@code TIFFTag} of this
281 * {@code TIFFDiectory} or {@code null}.
282 */
283 public TIFFTag getParentTag() {
284 return parentTag;
285 }
286
287 /**
288 * Returns the {@link TIFFTag} which has tag number equal to
289 * {@code tagNumber} or {@code null} if no such tag
290 * exists in the {@link TIFFTagSet}s associated with this
291 * directory.
292 *
293 * @param tagNumber The tag number of interest.
294 * @return The corresponding {@code TIFFTag} or {@code null}.
295 */
296 public TIFFTag getTag(int tagNumber) {
297 return TIFFIFD.getTag(tagNumber, tagSets);
298 }
299
300 /**
301 * Returns the number of {@link TIFFField}s in this directory.
302 *
303 * @return The number of {@code TIFFField}s in this
304 * {@code TIFFDirectory}.
305 */
306 public int getNumTIFFFields() {
307 return numLowFields + highFields.size();
308 }
309
310 /**
311 * Determines whether a TIFF field with the given tag number is
312 * contained in this directory.
313 *
314 * @param tagNumber The tag number.
315 * @return Whether a {@link TIFFTag} with tag number equal to
316 * {@code tagNumber} is present in this {@code TIFFDirectory}.
317 */
318 public boolean containsTIFFField(int tagNumber) {
319 return (tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM &&
320 lowFields[tagNumber] != null) ||
321 highFields.containsKey(Integer.valueOf(tagNumber));
322 }
323
324 /**
325 * Adds a TIFF field to the directory.
326 *
327 * @param f The field to add.
328 * @throws NullPointerException if {@code f} is {@code null}.
329 */
330 public void addTIFFField(TIFFField f) {
331 if(f == null) {
332 throw new NullPointerException("f == null");
333 }
334 int tagNumber = f.getTagNumber();
335 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
336 if(lowFields[tagNumber] == null) {
337 numLowFields++;
338 }
339 lowFields[tagNumber] = f;
340 } else {
341 highFields.put(Integer.valueOf(tagNumber), f);
342 }
343 }
344
345 /**
346 * Retrieves a TIFF field from the directory.
347 *
348 * @param tagNumber The tag number of the tag associated with the field.
349 * @return A {@code TIFFField} with the requested tag number of
350 * {@code null} if no such field is present.
351 */
352 public TIFFField getTIFFField(int tagNumber) {
353 TIFFField f;
354 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
355 f = lowFields[tagNumber];
356 } else {
357 f = highFields.get(Integer.valueOf(tagNumber));
358 }
359 return f;
360 }
361
362 /**
363 * Removes a TIFF field from the directory.
364 *
365 * @param tagNumber The tag number of the tag associated with the field.
366 */
367 public void removeTIFFField(int tagNumber) {
368 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
369 if(lowFields[tagNumber] != null) {
370 numLowFields--;
371 lowFields[tagNumber] = null;
372 }
373 } else {
374 highFields.remove(Integer.valueOf(tagNumber));
375 }
376 }
377
378 /**
379 * Retrieves all TIFF fields from the directory.
380 *
381 * @return An array of all TIFF fields in order of numerically increasing
382 * tag number.
383 */
384 public TIFFField[] getTIFFFields() {
385 // Allocate return value.
386 TIFFField[] fields = new TIFFField[numLowFields + highFields.size()];
387
388 // Copy any low-index fields.
389 int nextIndex = 0;
390 for(int i = 0; i <= MAX_LOW_FIELD_TAG_NUM; i++) {
391 if(lowFields[i] != null) {
392 fields[nextIndex++] = lowFields[i];
393 if(nextIndex == numLowFields) break;
394 }
395 }
396
397 // Copy any high-index fields.
398 if(!highFields.isEmpty()) {
399 Iterator<Integer> keys = highFields.keySet().iterator();
400 while(keys.hasNext()) {
401 fields[nextIndex++] = highFields.get(keys.next());
402 }
403 }
404
405 return fields;
406 }
407
408 /**
409 * Removes all TIFF fields from the directory.
410 */
411 public void removeTIFFFields() {
412 Arrays.fill(lowFields, (Object)null);
413 numLowFields = 0;
414 highFields.clear();
415 }
416
417 /**
418 * Converts the directory to a metadata object.
419 *
420 * @return A metadata instance initialized from the contents of this
421 * {@code TIFFDirectory}.
422 */
423 public IIOMetadata getAsMetadata() {
424 return new TIFFImageMetadata(TIFFIFD.getDirectoryAsIFD(this));
425 }
426
427 /**
428 * Clones the directory and all the fields contained therein.
429 *
430 * @return A clone of this {@code TIFFDirectory}.
431 * @throws CloneNotSupportedException if the instance cannot be cloned.
432 */
433 @Override
434 public TIFFDirectory clone() throws CloneNotSupportedException {
435 TIFFDirectory dir = (TIFFDirectory) super.clone();
436 dir.tagSets = new ArrayList<TIFFTagSet>(tagSets);
437 dir.parentTag = getParentTag();
438 TIFFField[] fields = getTIFFFields();
439 for(TIFFField field : fields) {
440 dir.addTIFFField(field.clone());
441 }
442
443 return dir;
|
216 * {@code null}.
217 */
218 public TIFFDirectory(TIFFTagSet[] tagSets, TIFFTag parentTag) {
219 if(tagSets == null) {
220 throw new NullPointerException("tagSets == null!");
221 }
222 this.tagSets = new ArrayList<TIFFTagSet>(tagSets.length);
223 int numTagSets = tagSets.length;
224 for(int i = 0; i < numTagSets; i++) {
225 this.tagSets.add(tagSets[i]);
226 }
227 this.parentTag = parentTag;
228 }
229
230 /**
231 * Returns the {@link TIFFTagSet}s of which this directory is aware.
232 *
233 * @return The {@code TIFFTagSet}s associated with this
234 * {@code TIFFDirectory}.
235 */
236 public final TIFFTagSet[] getTagSets() {
237 return tagSets.toArray(new TIFFTagSet[tagSets.size()]);
238 }
239
240 /**
241 * Adds an element to the group of {@link TIFFTagSet}s of which this
242 * directory is aware.
243 *
244 * @param tagSet The {@code TIFFTagSet} to add.
245 * @throws NullPointerException if {@code tagSet} is
246 * {@code null}.
247 */
248 public void addTagSet(TIFFTagSet tagSet) {
249 if(tagSet == null) {
250 throw new NullPointerException("tagSet == null");
251 }
252
253 if(!tagSets.contains(tagSet)) {
254 tagSets.add(tagSet);
255 }
256 }
263 * @throws NullPointerException if {@code tagSet} is
264 * {@code null}.
265 */
266 public void removeTagSet(TIFFTagSet tagSet) {
267 if(tagSet == null) {
268 throw new NullPointerException("tagSet == null");
269 }
270
271 if(tagSets.contains(tagSet)) {
272 tagSets.remove(tagSet);
273 }
274 }
275
276 /**
277 * Returns the parent {@link TIFFTag} of this directory if one
278 * has been defined or {@code null} otherwise.
279 *
280 * @return The parent {@code TIFFTag} of this
281 * {@code TIFFDiectory} or {@code null}.
282 */
283 public final TIFFTag getParentTag() {
284 return parentTag;
285 }
286
287 /**
288 * Returns the {@link TIFFTag} which has tag number equal to
289 * {@code tagNumber} or {@code null} if no such tag
290 * exists in the {@link TIFFTagSet}s associated with this
291 * directory.
292 *
293 * @param tagNumber The tag number of interest.
294 * @return The corresponding {@code TIFFTag} or {@code null}.
295 */
296 public final TIFFTag getTag(int tagNumber) {
297 return TIFFIFD.getTag(tagNumber, tagSets);
298 }
299
300 /**
301 * Returns the number of {@link TIFFField}s in this directory.
302 *
303 * @return The number of {@code TIFFField}s in this
304 * {@code TIFFDirectory}.
305 */
306 public final int getNumTIFFFields() {
307 return numLowFields + highFields.size();
308 }
309
310 /**
311 * Determines whether a TIFF field with the given tag number is
312 * contained in this directory.
313 *
314 * @param tagNumber The tag number.
315 * @return Whether a {@link TIFFTag} with tag number equal to
316 * {@code tagNumber} is present in this {@code TIFFDirectory}.
317 */
318 public final boolean containsTIFFField(int tagNumber) {
319 return (tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM &&
320 lowFields[tagNumber] != null) ||
321 highFields.containsKey(Integer.valueOf(tagNumber));
322 }
323
324 /**
325 * Adds a TIFF field to the directory.
326 *
327 * @param f The field to add.
328 * @throws NullPointerException if {@code f} is {@code null}.
329 */
330 public void addTIFFField(TIFFField f) {
331 if(f == null) {
332 throw new NullPointerException("f == null");
333 }
334 int tagNumber = f.getTagNumber();
335 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
336 if(lowFields[tagNumber] == null) {
337 numLowFields++;
338 }
339 lowFields[tagNumber] = f;
340 } else {
341 highFields.put(Integer.valueOf(tagNumber), f);
342 }
343 }
344
345 /**
346 * Retrieves a TIFF field from the directory.
347 *
348 * @param tagNumber The tag number of the tag associated with the field.
349 * @return A {@code TIFFField} with the requested tag number of
350 * {@code null} if no such field is present.
351 */
352 public final TIFFField getTIFFField(int tagNumber) {
353 TIFFField f;
354 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
355 f = lowFields[tagNumber];
356 } else {
357 f = highFields.get(Integer.valueOf(tagNumber));
358 }
359 return f;
360 }
361
362 /**
363 * Removes a TIFF field from the directory.
364 *
365 * @param tagNumber The tag number of the tag associated with the field.
366 */
367 public void removeTIFFField(int tagNumber) {
368 if(tagNumber >= 0 && tagNumber <= MAX_LOW_FIELD_TAG_NUM) {
369 if(lowFields[tagNumber] != null) {
370 numLowFields--;
371 lowFields[tagNumber] = null;
372 }
373 } else {
374 highFields.remove(Integer.valueOf(tagNumber));
375 }
376 }
377
378 /**
379 * Retrieves all TIFF fields from the directory.
380 *
381 * @return An array of all TIFF fields in order of numerically increasing
382 * tag number.
383 */
384 public final TIFFField[] getTIFFFields() {
385 // Allocate return value.
386 TIFFField[] fields = new TIFFField[numLowFields + highFields.size()];
387
388 // Copy any low-index fields.
389 int nextIndex = 0;
390 for(int i = 0; i <= MAX_LOW_FIELD_TAG_NUM; i++) {
391 if(lowFields[i] != null) {
392 fields[nextIndex++] = lowFields[i];
393 if(nextIndex == numLowFields) break;
394 }
395 }
396
397 // Copy any high-index fields.
398 if(!highFields.isEmpty()) {
399 Iterator<Integer> keys = highFields.keySet().iterator();
400 while(keys.hasNext()) {
401 fields[nextIndex++] = highFields.get(keys.next());
402 }
403 }
404
405 return fields;
406 }
407
408 /**
409 * Removes all TIFF fields from the directory.
410 */
411 public void removeTIFFFields() {
412 Arrays.fill(lowFields, (Object)null);
413 numLowFields = 0;
414 highFields.clear();
415 }
416
417 /**
418 * Converts the directory to a metadata object.
419 *
420 * @return A metadata instance initialized from the contents of this
421 * {@code TIFFDirectory}.
422 */
423 public final IIOMetadata getAsMetadata() {
424 return new TIFFImageMetadata(TIFFIFD.getDirectoryAsIFD(this));
425 }
426
427 /**
428 * Clones the directory and all the fields contained therein.
429 *
430 * @return A clone of this {@code TIFFDirectory}.
431 * @throws CloneNotSupportedException if the instance cannot be cloned.
432 */
433 @Override
434 public TIFFDirectory clone() throws CloneNotSupportedException {
435 TIFFDirectory dir = (TIFFDirectory) super.clone();
436 dir.tagSets = new ArrayList<TIFFTagSet>(tagSets);
437 dir.parentTag = getParentTag();
438 TIFFField[] fields = getTIFFFields();
439 for(TIFFField field : fields) {
440 dir.addTIFFField(field.clone());
441 }
442
443 return dir;
|