View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.tiff;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.imaging.ImagingException;
24  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
25  import org.apache.commons.imaging.internal.Debug;
26  
27  public class TiffContents {
28  
29      public final TiffHeader header;
30      public final List<TiffDirectory> directories;
31      public final List<TiffField> tiffFields;
32  
33      public TiffContents(final TiffHeader tiffHeader, final List<TiffDirectory> directories, final List<TiffField> tiffFields) {
34          this.header = tiffHeader;
35          this.directories = Collections.unmodifiableList(directories);
36          this.tiffFields = Collections.unmodifiableList(tiffFields);
37      }
38  
39      public void dissect() throws ImagingException {
40          final List<AbstractTiffElement> elements = getElements();
41  
42          elements.sort(AbstractTiffElement.COMPARATOR);
43  
44          long lastEnd = 0;
45          for (final AbstractTiffElement element : elements) {
46              if (element.offset > lastEnd) {
47                  Debug.debug("\t" + "gap: " + (element.offset - lastEnd));
48              }
49              if (element.offset < lastEnd) {
50                  Debug.debug("\t" + "overlap");
51              }
52  
53              Debug.debug("element, start: " + element.offset + ", length: " + element.length + ", end: " + (element.offset + element.length) + ": "
54                      + element.getElementDescription());
55              final String verbosity = element.getElementDescription();
56              if (null != verbosity) {
57                  Debug.debug(verbosity);
58              }
59  
60              lastEnd = element.offset + element.length;
61          }
62          Debug.debug("end: " + lastEnd);
63          Debug.debug();
64      }
65  
66      public TiffField findField(final TagInfo tag) throws ImagingException {
67          for (final TiffDirectory directory : directories) {
68              final TiffField field = directory.findField(tag);
69              if (null != field) {
70                  return field;
71              }
72          }
73  
74          return null;
75      }
76  
77      public List<AbstractTiffElement> getElements() throws ImagingException {
78          final List<AbstractTiffElement> result = new ArrayList<>();
79  
80          result.add(header);
81  
82          for (final TiffDirectory directory : directories) {
83              result.add(directory);
84  
85              for (final TiffField field : directory) {
86                  final AbstractTiffElement oversizeValue = field.getOversizeValueElement();
87                  if (null != oversizeValue) {
88                      result.add(oversizeValue);
89                  }
90              }
91  
92              if (directory.hasTiffImageData()) {
93                  result.addAll(directory.getTiffRawImageDataElements());
94              }
95              if (directory.hasJpegImageData()) {
96                  result.add(directory.getJpegRawImageDataElement());
97              }
98          }
99  
100         return result;
101     }
102 
103 }