View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.formats.tiff.taginfos;
19  
20  import java.nio.ByteOrder;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.imaging.ImagingException;
27  import org.apache.commons.imaging.formats.tiff.TiffField;
28  import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType;
29  import org.apache.commons.imaging.formats.tiff.fieldtypes.AbstractFieldType;
30  
31  public class TagInfo {
32      public static final int LENGTH_UNKNOWN = -1;
33      public final String name;
34      public final int tag;
35      public final List<AbstractFieldType> dataTypes;
36      public final int length;
37      public final TiffDirectoryType directoryType;
38      private final boolean isOffset;
39  
40      public TagInfo(final String name, final int tag, final AbstractFieldType dataType) {
41          this(name, tag, dataType, LENGTH_UNKNOWN, TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
42      }
43  
44      public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length) {
45          this(name, tag, Arrays.asList(dataType), length, TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
46      }
47  
48      public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length, final TiffDirectoryType exifDirectory) {
49          this(name, tag, Arrays.asList(dataType), length, exifDirectory);
50      }
51  
52      public TagInfo(final String name, final int tag, final AbstractFieldType dataType, final int length, final TiffDirectoryType exifDirectory,
53              final boolean isOffset) {
54          this(name, tag, Arrays.asList(dataType), length, exifDirectory, isOffset);
55      }
56  
57      public TagInfo(final String name, final int tag, final List<AbstractFieldType> dataTypes, final int length, final TiffDirectoryType exifDirectory) {
58          this(name, tag, dataTypes, length, exifDirectory, false);
59      }
60  
61      public TagInfo(final String name, final int tag, final List<AbstractFieldType> dataTypes, final int length, final TiffDirectoryType exifDirectory,
62              final boolean isOffset) {
63          this.name = name;
64          this.tag = tag;
65          this.dataTypes = Collections.unmodifiableList(new ArrayList<>(dataTypes));
66          this.length = length;
67          this.directoryType = exifDirectory;
68          this.isOffset = isOffset;
69      }
70  
71      public byte[] encodeValue(final AbstractFieldType abstractFieldType, final Object value, final ByteOrder byteOrder) throws ImagingException {
72          return abstractFieldType.writeData(value, byteOrder);
73      }
74  
75      public String getDescription() {
76          return tag + " (0x" + Integer.toHexString(tag) + ": " + name + "): ";
77      }
78  
79      /**
80       *
81       * @param entry the TIFF field whose value to return
82       * @return the value of the TIFF field
83       * @throws ImagingException thrown by subclasses
84       */
85      public Object getValue(final TiffField entry) throws ImagingException {
86          return entry.getFieldType().getValue(entry);
87      }
88  
89      public boolean isOffset() {
90          return isOffset;
91      }
92  
93      public boolean isText() {
94          return false;
95      }
96  
97      @Override
98      public String toString() {
99          return "[TagInfo. tag: " + tag + " (0x" + Integer.toHexString(tag) + ", name: " + name + "]";
100     }
101 }