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