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.ImageReadException;
25  import org.apache.commons.imaging.ImageWriteException;
26  import org.apache.commons.imaging.formats.tiff.TiffField;
27  
28  /**
29   * TIFF field types.
30   */
31  public abstract class FieldType {
32      public static final FieldTypeByteng/formats/tiff/fieldtypes/FieldTypeByte.html#FieldTypeByte">FieldTypeByte BYTE = new FieldTypeByte(1, "Byte");
33      public static final FieldTypeAscii/formats/tiff/fieldtypes/FieldTypeAscii.html#FieldTypeAscii">FieldTypeAscii ASCII = new FieldTypeAscii(2, "ASCII");
34      public static final FieldTypeShort/formats/tiff/fieldtypes/FieldTypeShort.html#FieldTypeShort">FieldTypeShort SHORT = new FieldTypeShort(3, "Short");
35      public static final FieldTypeLongng/formats/tiff/fieldtypes/FieldTypeLong.html#FieldTypeLong">FieldTypeLong LONG = new FieldTypeLong(4, "Long");
36      public static final FieldTypeRationalts/tiff/fieldtypes/FieldTypeRational.html#FieldTypeRational">FieldTypeRational RATIONAL = new FieldTypeRational(5, "Rational");
37      public static final FieldTypeByteg/formats/tiff/fieldtypes/FieldTypeByte.html#FieldTypeByte">FieldTypeByte SBYTE = new FieldTypeByte(6, "SByte");
38      public static final FieldTypeBytermats/tiff/fieldtypes/FieldTypeByte.html#FieldTypeByte">FieldTypeByte UNDEFINED = new FieldTypeByte(7, "Undefined");
39      public static final FieldTypeShortformats/tiff/fieldtypes/FieldTypeShort.html#FieldTypeShort">FieldTypeShort SSHORT = new FieldTypeShort(8, "SShort");
40      public static final FieldTypeLongg/formats/tiff/fieldtypes/FieldTypeLong.html#FieldTypeLong">FieldTypeLong SLONG = new FieldTypeLong(9, "SLong");
41      public static final FieldTypeRationals/tiff/fieldtypes/FieldTypeRational.html#FieldTypeRational">FieldTypeRational SRATIONAL = new FieldTypeRational(10, "SRational");
42      public static final FieldTypeFloat/formats/tiff/fieldtypes/FieldTypeFloat.html#FieldTypeFloat">FieldTypeFloat FLOAT = new FieldTypeFloat(11, "Float");
43      public static final FieldTypeDoubleormats/tiff/fieldtypes/FieldTypeDouble.html#FieldTypeDouble">FieldTypeDouble DOUBLE = new FieldTypeDouble(12, "Double");
44      public static final FieldTypeLonging/formats/tiff/fieldtypes/FieldTypeLong.html#FieldTypeLong">FieldTypeLong IFD = new FieldTypeLong(13, "IFD");
45  
46      private final int type;
47      private final String name;
48      private final int elementSize;
49  
50      public static final List<FieldType> ANY =
51              Collections.unmodifiableList(Arrays.asList(
52                      BYTE, ASCII, SHORT,
53                      LONG, RATIONAL, SBYTE,
54                      UNDEFINED, SSHORT, SLONG,
55                      SRATIONAL, FLOAT, DOUBLE,
56                      IFD));
57  
58      public static final List<FieldType> SHORT_OR_LONG =
59              Collections.unmodifiableList(Arrays.asList(
60                      SHORT, LONG));
61  
62      public static final List<FieldType> SHORT_OR_RATIONAL =
63              Collections.unmodifiableList(Arrays.asList(
64                      SHORT, RATIONAL));
65  
66      public static final List<FieldType> SHORT_OR_LONG_OR_RATIONAL =
67              Collections.unmodifiableList(Arrays.asList(
68                      SHORT, LONG, RATIONAL));
69  
70      public static final List<FieldType> LONG_OR_SHORT =
71              Collections.unmodifiableList(Arrays.asList(
72                      SHORT, LONG));
73  
74      public static final List<FieldType> BYTE_OR_SHORT =
75              Collections.unmodifiableList(Arrays.asList(
76                      SHORT, BYTE));
77  
78      public static final List<FieldType> LONG_OR_IFD =
79              Collections.unmodifiableList(Arrays.asList(
80                      (FieldType) LONG, IFD));
81  
82      public static final List<FieldType> ASCII_OR_RATIONAL =
83              Collections.unmodifiableList(Arrays.asList(
84                      ASCII, RATIONAL));
85  
86      public static final List<FieldType> ASCII_OR_BYTE =
87              Collections.unmodifiableList(Arrays.asList(
88                      ASCII, BYTE));
89  
90      protected FieldType(final int type, final String name, final int elementSize) {
91          this.type = type;
92          this.name = name;
93          this.elementSize = elementSize;
94      }
95  
96  
97      public int getType() {
98          return type;
99      }
100 
101     public String getName() {
102         return name;
103     }
104 
105     public int getSize() {
106         return elementSize;
107     }
108 
109     public static FieldType getFieldType(final int type) throws ImageReadException {
110         for (final FieldType fieldType : ANY) {
111             if (fieldType.getType() == type) {
112                 return fieldType;
113             }
114         }
115         throw new ImageReadException("Field type " + type + " is unsupported");
116     }
117 
118     public abstract Object getValue(TiffField entry);
119     public abstract byte[] writeData(Object o, ByteOrder byteOrder) throws ImageWriteException;
120 }