    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.imaging.formats.tiff.fieldtypes;
    018
    019import java.nio.ByteOrder;
    020import java.util.Arrays;
    021import java.util.Collections;
    022import java.util.List;
    023
    024import org.apache.commons.imaging.ImagingException;
    025import org.apache.commons.imaging.formats.tiff.TiffField;
    026
    027/**
    028 * TIFF field types.
    029 */
    030public abstract class AbstractFieldType {
    031
    032    /**
    033     * Byte field type.
    034     */
    035    public static final FieldTypeByte BYTE = new FieldTypeByte(1, "Byte");
    036
    037    /**
    038     * ASCII field type.
    039     */
    040    public static final FieldTypeAscii ASCII = new FieldTypeAscii(2, "ASCII");
    041
    042    /**
    043     * Short field type.
    044     */
    045    public static final FieldTypeShort SHORT = new FieldTypeShort(3, "Short");
    046
    047    /**
    048     * Long field type.
    049     */
    050    public static final FieldTypeLong LONG = new FieldTypeLong(4, "Long");
    051
    052    /**
    053     * Rational field type.
    054     */
    055    public static final FieldTypeRational RATIONAL = new FieldTypeRational(5, "Rational");
    056
    057    /**
    058     * SByte field type.
    059     */
    060    public static final FieldTypeByte SBYTE = new FieldTypeByte(6, "SByte");
    061
    062    /**
    063     * Undefined field type.
    064     */
    065    public static final FieldTypeByte UNDEFINED = new FieldTypeByte(7, "Undefined");
    066
    067    /**
    068     * SShort field type.
    069     */
    070    public static final FieldTypeShort SSHORT = new FieldTypeShort(8, "SShort");
    071
    072    /**
    073     * SLong field type.
    074     */
    075    public static final FieldTypeLong SLONG = new FieldTypeLong(9, "SLong");
    076
    077    /**
    078     * SRational field type.
    079     */
    080    public static final FieldTypeRational SRATIONAL = new FieldTypeRational(10, "SRational");
    081
    082    /**
    083     * Float field type.
    084     */
    085    public static final FieldTypeFloat FLOAT = new FieldTypeFloat(11, "Float");
    086
    087    /**
    088     * Double field type.
    089     */
    090    public static final FieldTypeDouble DOUBLE = new FieldTypeDouble(12, "Double");
    091
    092    /**
    093     * IFD field type.
    094     */
    095    public static final FieldTypeLong IFD = new FieldTypeLong(13, "IFD");
    096
    097    /**
    098     * Long8 field type.
    099     */
    100    public static final FieldTypeLong8 LONG8 = new FieldTypeLong8(16, "Long8");
    101
    102    /**
    103     * SLong8 field type.
    104     */
    105    public static final FieldTypeLong8 SLONG8 = new FieldTypeLong8(17, "Long8");
    106
    107    /**
    108     * IFD8 field type.
    109     */
    110    public static final FieldTypeLong8 IFD8 = new FieldTypeLong8(18, "Long8");
    111
    112    /**
    113     * Unmodifiable List of all field types.
    114     */
    115    public static final List<AbstractFieldType> ANY = Collections.unmodifiableList(
    116            Arrays.asList(BYTE, ASCII, SHORT, LONG, RATIONAL, SBYTE, UNDEFINED, SSHORT, SLONG, SRATIONAL, FLOAT, DOUBLE, IFD, LONG8, SLONG8, IFD8));
    117
    118    /**
    119     * Unmodifiable List of SHORT, LONG field types.
    120     */
    121    public static final List<AbstractFieldType> SHORT_OR_LONG = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
    122
    123    /**
    124     * Unmodifiable List of SHORT, RATIONAL field types.
    125     */
    126    public static final List<AbstractFieldType> SHORT_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, RATIONAL));
    127
    128
    129    /**
    130     * Unmodifiable List of SHORT, LONG, RATIONAL field types.
    131     */
    132    public static final List<AbstractFieldType> SHORT_OR_LONG_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, LONG, RATIONAL));
    133
    134
    135    /**
    136     * Unmodifiable List of SHORT, LONG field types.
    137     */
    138    public static final List<AbstractFieldType> LONG_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
    139
    140
    141    /**
    142     * Unmodifiable List of SHORT, BYTE field types.
    143     */
    144    public static final List<AbstractFieldType> BYTE_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, BYTE));
    145
    146
    147    /**
    148     * Unmodifiable List of LONG, IFD field types.
    149     */
    150    public static final List<AbstractFieldType> LONG_OR_IFD = Collections.unmodifiableList(Arrays.asList((AbstractFieldType) LONG, IFD));
    151
    152
    153    /**
    154     * Unmodifiable List of ASCII, RATIONAL field types.
    155     */
    156    public static final List<AbstractFieldType> ASCII_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(ASCII, RATIONAL));
    157
    158
    159    /**
    160     * Unmodifiable List of ASCII, BYTE field types.
    161     */
    162    public static final List<AbstractFieldType> ASCII_OR_BYTE = Collections.unmodifiableList(Arrays.asList(ASCII, BYTE));
    163
    164    /**
    165     * Gets a known field type.
    166     *
    167     * @param type the type to find.
    168     * @return the matching field type.
    169     * @throws ImagingException thrown when not found.
    170     */
    171    public static AbstractFieldType getFieldType(final int type) throws ImagingException {
    172        for (final AbstractFieldType abstractFieldType : ANY) {
    173            if (abstractFieldType.getType() == type) {
    174                return abstractFieldType;
    175            }
    176        }
    177        throw new ImagingException("Field type " + type + " is unsupported");
    178    }
    179
    180    private final int type;
    181
    182    private final String name;
    183
    184    private final int elementSize;
    185
    186    /**
    187     * Constructs a new instance.
    188     *
    189     * @param type the type.
    190     * @param name the name.
    191     * @param elementSize the element size.
    192     */
    193    protected AbstractFieldType(final int type, final String name, final int elementSize) {
    194        this.type = type;
    195        this.name = name;
    196        this.elementSize = elementSize;
    197    }
    198
    199    /**
    200     * Gets the name.
    201     *
    202     * @return the name.
    203     */
    204    public String getName() {
    205        return name;
    206    }
    207
    208    /**
    209     * Gets the size.
    210     *
    211     * @return the size.
    212     */
    213    public int getSize() {
    214        return elementSize;
    215    }
    216
    217    /**
    218     * Gets the type.
    219     *
    220     * @return the type.
    221     */
    222    public int getType() {
    223        return type;
    224    }
    225
    226    /**
    227     * Gets the value from a TIFF field.
    228     *
    229     * @param entry the TIFF field.
    230     * @return the value.
    231     */
    232    public abstract Object getValue(TiffField entry);
    233
    234    /**
    235     * Converts the given object to a byte array.
    236     *
    237     * @param obj input.
    238     * @param byteOrder output byte order; not used by all subclasses.
    239     * @return a byte array.
    240     * @throws ImagingException Thrown on a bad input.
    241     */
    242    public abstract byte[] writeData(Object obj, ByteOrder byteOrder) throws ImagingException;
    243}