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    public static final FieldTypeByte BYTE = new FieldTypeByte(1, "Byte");
032    public static final FieldTypeAscii ASCII = new FieldTypeAscii(2, "ASCII");
033    public static final FieldTypeShort SHORT = new FieldTypeShort(3, "Short");
034    public static final FieldTypeLong LONG = new FieldTypeLong(4, "Long");
035    public static final FieldTypeRational RATIONAL = new FieldTypeRational(5, "Rational");
036    public static final FieldTypeByte SBYTE = new FieldTypeByte(6, "SByte");
037    public static final FieldTypeByte UNDEFINED = new FieldTypeByte(7, "Undefined");
038    public static final FieldTypeShort SSHORT = new FieldTypeShort(8, "SShort");
039    public static final FieldTypeLong SLONG = new FieldTypeLong(9, "SLong");
040    public static final FieldTypeRational SRATIONAL = new FieldTypeRational(10, "SRational");
041    public static final FieldTypeFloat FLOAT = new FieldTypeFloat(11, "Float");
042    public static final FieldTypeDouble DOUBLE = new FieldTypeDouble(12, "Double");
043    public static final FieldTypeLong IFD = new FieldTypeLong(13, "IFD");
044    public static final FieldTypeLong8 LONG8 = new FieldTypeLong8(16, "Long8");
045    public static final FieldTypeLong8 SLONG8 = new FieldTypeLong8(17, "Long8");
046    public static final FieldTypeLong8 IFD8 = new FieldTypeLong8(18, "Long8");
047
048    public static final List<AbstractFieldType> ANY = Collections.unmodifiableList(
049            Arrays.asList(BYTE, ASCII, SHORT, LONG, RATIONAL, SBYTE, UNDEFINED, SSHORT, SLONG, SRATIONAL, FLOAT, DOUBLE, IFD, LONG8, SLONG8, IFD8));
050    public static final List<AbstractFieldType> SHORT_OR_LONG = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
051    public static final List<AbstractFieldType> SHORT_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, RATIONAL));
052
053    public static final List<AbstractFieldType> SHORT_OR_LONG_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(SHORT, LONG, RATIONAL));
054
055    public static final List<AbstractFieldType> LONG_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, LONG));
056
057    public static final List<AbstractFieldType> BYTE_OR_SHORT = Collections.unmodifiableList(Arrays.asList(SHORT, BYTE));
058
059    public static final List<AbstractFieldType> LONG_OR_IFD = Collections.unmodifiableList(Arrays.asList((AbstractFieldType) LONG, IFD));
060
061    public static final List<AbstractFieldType> ASCII_OR_RATIONAL = Collections.unmodifiableList(Arrays.asList(ASCII, RATIONAL));
062
063    public static final List<AbstractFieldType> ASCII_OR_BYTE = Collections.unmodifiableList(Arrays.asList(ASCII, BYTE));
064
065    public static AbstractFieldType getFieldType(final int type) throws ImagingException {
066        for (final AbstractFieldType abstractFieldType : ANY) {
067            if (abstractFieldType.getType() == type) {
068                return abstractFieldType;
069            }
070        }
071        throw new ImagingException("Field type " + type + " is unsupported");
072    }
073
074    private final int type;
075
076    private final String name;
077
078    private final int elementSize;
079
080    protected AbstractFieldType(final int type, final String name, final int elementSize) {
081        this.type = type;
082        this.name = name;
083        this.elementSize = elementSize;
084    }
085
086    public String getName() {
087        return name;
088    }
089
090    public int getSize() {
091        return elementSize;
092    }
093
094    public int getType() {
095        return type;
096    }
097
098    public abstract Object getValue(TiffField entry);
099
100    public abstract byte[] writeData(Object o, ByteOrder byteOrder) throws ImagingException;
101}