    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.ImageReadException;
    025import org.apache.commons.imaging.ImageWriteException;
    026import org.apache.commons.imaging.formats.tiff.TiffField;
    027
    028/**
    029 * TIFF field types.
    030 */
    031public abstract class FieldType {
    032    public static final FieldTypeByte BYTE = new FieldTypeByte(1, "Byte");
    033    public static final FieldTypeAscii ASCII = new FieldTypeAscii(2, "ASCII");
    034    public static final FieldTypeShort SHORT = new FieldTypeShort(3, "Short");
    035    public static final FieldTypeLong LONG = new FieldTypeLong(4, "Long");
    036    public static final FieldTypeRational RATIONAL = new FieldTypeRational(5, "Rational");
    037    public static final FieldTypeByte SBYTE = new FieldTypeByte(6, "SByte");
    038    public static final FieldTypeByte UNDEFINED = new FieldTypeByte(7, "Undefined");
    039    public static final FieldTypeShort SSHORT = new FieldTypeShort(8, "SShort");
    040    public static final FieldTypeLong SLONG = new FieldTypeLong(9, "SLong");
    041    public static final FieldTypeRational SRATIONAL = new FieldTypeRational(10, "SRational");
    042    public static final FieldTypeFloat FLOAT = new FieldTypeFloat(11, "Float");
    043    public static final FieldTypeDouble DOUBLE = new FieldTypeDouble(12, "Double");
    044    public static final FieldTypeLong IFD = new FieldTypeLong(13, "IFD");
    045
    046    private final int type;
    047    private final String name;
    048    private final int elementSize;
    049
    050    public static final List<FieldType> ANY =
    051            Collections.unmodifiableList(Arrays.asList(
    052                    BYTE, ASCII, SHORT,
    053                    LONG, RATIONAL, SBYTE,
    054                    UNDEFINED, SSHORT, SLONG,
    055                    SRATIONAL, FLOAT, DOUBLE,
    056                    IFD));
    057
    058    public static final List<FieldType> SHORT_OR_LONG =
    059            Collections.unmodifiableList(Arrays.asList(
    060                    SHORT, LONG));
    061
    062    public static final List<FieldType> SHORT_OR_RATIONAL =
    063            Collections.unmodifiableList(Arrays.asList(
    064                    SHORT, RATIONAL));
    065
    066    public static final List<FieldType> SHORT_OR_LONG_OR_RATIONAL =
    067            Collections.unmodifiableList(Arrays.asList(
    068                    SHORT, LONG, RATIONAL));
    069
    070    public static final List<FieldType> LONG_OR_SHORT =
    071            Collections.unmodifiableList(Arrays.asList(
    072                    SHORT, LONG));
    073
    074    public static final List<FieldType> BYTE_OR_SHORT =
    075            Collections.unmodifiableList(Arrays.asList(
    076                    SHORT, BYTE));
    077
    078    public static final List<FieldType> LONG_OR_IFD =
    079            Collections.unmodifiableList(Arrays.asList(
    080                    (FieldType) LONG, IFD));
    081
    082    public static final List<FieldType> ASCII_OR_RATIONAL =
    083            Collections.unmodifiableList(Arrays.asList(
    084                    ASCII, RATIONAL));
    085
    086    public static final List<FieldType> ASCII_OR_BYTE =
    087            Collections.unmodifiableList(Arrays.asList(
    088                    ASCII, BYTE));
    089
    090    protected FieldType(final int type, final String name, final int elementSize) {
    091        this.type = type;
    092        this.name = name;
    093        this.elementSize = elementSize;
    094    }
    095
    096
    097    public int getType() {
    098        return type;
    099    }
    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}