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  package org.apache.commons.imaging.formats.tiff.fieldtypes;
18  
19  import java.nio.ByteOrder;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.commons.imaging.ImagingException;
25  import org.apache.commons.imaging.formats.tiff.TiffField;
26  
27  /**
28   * TIFF field types.
29   */
30  public abstract class AbstractFieldType {
31      public static final FieldTypeByte BYTE = new FieldTypeByte(1, "Byte");
32      public static final FieldTypeAscii ASCII = new FieldTypeAscii(2, "ASCII");
33      public static final FieldTypeShort SHORT = new FieldTypeShort(3, "Short");
34      public static final FieldTypeLong LONG = new FieldTypeLong(4, "Long");
35      public static final FieldTypeRational RATIONAL = new FieldTypeRational(5, "Rational");
36      public static final FieldTypeByte SBYTE = new FieldTypeByte(6, "SByte");
37      public static final FieldTypeByte UNDEFINED = new FieldTypeByte(7, "Undefined");
38      public static final FieldTypeShort SSHORT = new FieldTypeShort(8, "SShort");
39      public static final FieldTypeLong SLONG = new FieldTypeLong(9, "SLong");
40      public static final FieldTypeRational SRATIONAL = new FieldTypeRational(10, "SRational");
41      public static final FieldTypeFloat FLOAT = new FieldTypeFloat(11, "Float");
42      public static final FieldTypeDouble DOUBLE = new FieldTypeDouble(12, "Double");
43      public static final FieldTypeLong IFD = new FieldTypeLong(13, "IFD");
44      public static final FieldTypeLong8 LONG8 = new FieldTypeLong8(16, "Long8");
45      public static final FieldTypeLong8 SLONG8 = new FieldTypeLong8(17, "Long8");
46      public static final FieldTypeLong8 IFD8 = new FieldTypeLong8(18, "Long8");
47  
48      public static final List<AbstractFieldType> ANY = Collections.unmodifiableList(
49              Arrays.asList(BYTE, ASCII, SHORT, LONG, RATIONAL, SBYTE, UNDEFINED, SSHORT, SLONG, SRATIONAL, FLOAT, DOUBLE, IFD, LONG8, SLONG8, IFD8));
50      public static final List<AbstractFieldType> SHORT_OR_LONG = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
51      public static final List<AbstractFieldType> SHORT_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, RATIONAL));
52  
53      public static final List<AbstractFieldType> SHORT_OR_LONG_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, LONG, RATIONAL));
54  
55      public static final List<AbstractFieldType> LONG_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
56  
57      public static final List<AbstractFieldType> BYTE_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, BYTE));
58  
59      public static final List<AbstractFieldType> LONG_OR_IFD = Collections.unmodifiableList(Arrays.asList((AbstractFieldType) LONG, IFD));
60  
61      public static final List<AbstractFieldType> ASCII_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(ASCII, RATIONAL));
62  
63      public static final List<AbstractFieldType> ASCII_OR_BYTE = Collections.unmodifiableList(Arrays.asList(ASCII, BYTE));
64  
65      public static AbstractFieldType getFieldType(final int type) throws ImagingException {
66          for (final AbstractFieldType abstractFieldType : ANY) {
67              if (abstractFieldType.getType() == type) {
68                  return abstractFieldType;
69              }
70          }
71          throw new ImagingException("Field type " + type + " is unsupported");
72      }
73  
74      private final int type;
75  
76      private final String name;
77  
78      private final int elementSize;
79  
80      protected AbstractFieldType(final int type, final String name, final int elementSize) {
81          this.type = type;
82          this.name = name;
83          this.elementSize = elementSize;
84      }
85  
86      public String getName() {
87          return name;
88      }
89  
90      public int getSize() {
91          return elementSize;
92      }
93  
94      public int getType() {
95          return type;
96      }
97  
98      public abstract Object getValue(TiffField entry);
99  
100     public abstract byte[] writeData(Object o, ByteOrder byteOrder) throws ImagingException;
101 }