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;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.commons.imaging.ImageReadException;
024import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
025import org.apache.commons.imaging.internal.Debug;
026
027public class TiffContents {
028    public final TiffHeader header;
029    public final List<TiffDirectory> directories;
030    public final List<TiffField> tiffFields;
031
032    public TiffContents(final TiffHeader tiffHeader, final List<TiffDirectory> directories, final List<TiffField> tiffFields) {
033        this.header = tiffHeader;
034        this.directories = Collections.unmodifiableList(directories);
035        this.tiffFields = Collections.unmodifiableList(tiffFields);
036    }
037
038    public List<TiffElement> getElements() throws ImageReadException {
039        final List<TiffElement> result = new ArrayList<>();
040
041        result.add(header);
042
043        for (final TiffDirectory directory : directories) {
044            result.add(directory);
045
046            final List<TiffField> fields = directory.entries;
047            for (final TiffField field : fields) {
048                final TiffElement oversizeValue = field.getOversizeValueElement();
049                if (null != oversizeValue) {
050                    result.add(oversizeValue);
051                }
052            }
053
054            if (directory.hasTiffImageData()) {
055                result.addAll(directory.getTiffRawImageDataElements());
056            }
057            if (directory.hasJpegImageData()) {
058                result.add(directory.getJpegRawImageDataElement());
059            }
060        }
061
062        return result;
063    }
064
065    public TiffField findField(final TagInfo tag) throws ImageReadException {
066        for (final TiffDirectory directory : directories) {
067            final TiffField field = directory.findField(tag);
068            if (null != field) {
069                return field;
070            }
071        }
072
073        return null;
074    }
075
076    public void dissect() throws ImageReadException {
077        final List<TiffElement> elements = getElements();
078
079        Collections.sort(elements, TiffElement.COMPARATOR);
080
081        long lastEnd = 0;
082        for (final TiffElement element : elements) {
083            if (element.offset > lastEnd) {
084                Debug.debug("\t" + "gap: " + (element.offset - lastEnd));
085            }
086            if (element.offset < lastEnd) {
087                Debug.debug("\t" + "overlap");
088            }
089
090            Debug.debug("element, start: " + element.offset + ", length: "
091                    + element.length + ", end: "
092                    + (element.offset + element.length) + ": "
093                    + element.getElementDescription());
094            final String verbosity = element.getElementDescription();
095            if (null != verbosity) {
096                Debug.debug(verbosity);
097            }
098
099            lastEnd = element.offset + element.length;
100        }
101        Debug.debug("end: " + lastEnd);
102        Debug.debug();
103    }
104
105}