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