    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 */
    017
    018package org.apache.commons.imaging.formats.tiff.taginfos;
    019
    020import java.nio.ByteOrder;
    021import java.util.ArrayList;
    022import java.util.Arrays;
    023import java.util.Collections;
    024import java.util.List;
    025
    026import org.apache.commons.imaging.ImageReadException;
    027import org.apache.commons.imaging.ImageWriteException;
    028import org.apache.commons.imaging.formats.tiff.TiffField;
    029import org.apache.commons.imaging.formats.tiff.constants.TiffDirectoryType;
    030import org.apache.commons.imaging.formats.tiff.fieldtypes.FieldType;
    031
    032public class TagInfo {
    033    public static final int LENGTH_UNKNOWN = -1;
    034    public final String name;
    035    public final int tag;
    036    public final List<FieldType> dataTypes;
    037    public final int length;
    038    public final TiffDirectoryType directoryType;
    039    private final boolean isOffset;
    040
    041    public TagInfo(final String name, final int tag, final FieldType dataType, final int length,
    042            final TiffDirectoryType exifDirectory) {
    043        this(name, tag, Arrays.asList(dataType), length, exifDirectory);
    044    }
    045
    046    public TagInfo(final String name, final int tag, final FieldType dataType, final int length,
    047            final TiffDirectoryType exifDirectory, final boolean isOffset) {
    048        this(name, tag, Arrays.asList(dataType), length, exifDirectory,
    049                isOffset);
    050    }
    051
    052    public TagInfo(final String name, final int tag, final FieldType dataType, final int length) {
    053        this(name, tag, Arrays.asList(dataType), length,
    054                TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
    055    }
    056
    057    public TagInfo(final String name, final int tag, final FieldType dataType) {
    058        this(name, tag, dataType, LENGTH_UNKNOWN,
    059                TiffDirectoryType.EXIF_DIRECTORY_UNKNOWN);
    060    }
    061
    062    public TagInfo(final String name, final int tag, final List<FieldType> dataTypes, final int length,
    063            final TiffDirectoryType exifDirectory) {
    064        this(name, tag, dataTypes, length, exifDirectory, false);
    065    }
    066
    067    public TagInfo(final String name, final int tag, final List<FieldType> dataTypes, final int length,
    068            final TiffDirectoryType exifDirectory, final boolean isOffset) {
    069        this.name = name;
    070        this.tag = tag;
    071        this.dataTypes = Collections.unmodifiableList(new ArrayList<>(
    072                dataTypes));
    073        this.length = length;
    074        this.directoryType = exifDirectory;
    075        this.isOffset = isOffset;
    076    }
    077
    078    /**
    079     *
    080     * @param entry the TIFF field whose value to return
    081     * @return the value of the TIFF field
    082     * @throws ImageReadException thrown by subclasses
    083     */
    084    public Object getValue(final TiffField entry) throws ImageReadException {
    085        return entry.getFieldType().getValue(entry);
    086    }
    087
    088    public byte[] encodeValue(final FieldType fieldType, final Object value, final ByteOrder byteOrder)
    089            throws ImageWriteException {
    090        return fieldType.writeData(value, byteOrder);
    091    }
    092
    093    public String getDescription() {
    094        return tag + " (0x" + Integer.toHexString(tag) + ": " + name + "): ";
    095    }
    096
    097    @Override
    098    public String toString() {
    099        return "[TagInfo. tag: " + tag + " (0x" + Integer.toHexString(tag)
    100                + ", name: " + name + "]";
    101    }
    102
    103    public boolean isOffset() {
    104        return isOffset;
    105    }
    106
    107    public boolean isText() {
    108        return false;
    109    }
    110}