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.io.IOException;
20  import java.nio.ByteOrder;
21  
22  import org.apache.commons.imaging.ImagingException;
23  import org.apache.commons.imaging.formats.tiff.constants.TiffPlanarConfiguration;
24  import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
25  import org.apache.commons.imaging.formats.tiff.datareaders.DataReaderStrips;
26  import org.apache.commons.imaging.formats.tiff.datareaders.DataReaderTiled;
27  import org.apache.commons.imaging.formats.tiff.datareaders.ImageDataReader;
28  import org.apache.commons.imaging.formats.tiff.photometricinterpreters.PhotometricInterpreter;
29  
30  public abstract class AbstractTiffImageData {
31  
32      public static class Data extends AbstractTiffElement.DataElement {
33  
34          public Data(final long offset, final int length, final byte[] data) {
35              super(offset, length, data);
36          }
37  
38          @Override
39          public String getElementDescription() {
40              return "TIFF image data: " + getDataLength() + " bytes";
41          }
42  
43      }
44  
45      public static class Strips extends AbstractTiffImageData {
46  
47          private final AbstractTiffElement.DataElement[] strips;
48          // public final byte strips[][];
49          public final int rowsPerStrip;
50  
51          public Strips(final AbstractTiffElement.DataElement[] strips, final int rowsPerStrip) {
52              this.strips = strips;
53              this.rowsPerStrip = rowsPerStrip;
54          }
55  
56          @Override
57          public ImageDataReader getDataReader(final TiffDirectory directory, final PhotometricInterpreter photometricInterpreter, final int bitsPerPixel,
58                  final int[] bitsPerSample, final int predictor, final int samplesPerPixel, final int width, final int height, final int compression,
59                  final TiffPlanarConfiguration planarConfiguration, final ByteOrder byteorder) throws IOException, ImagingException {
60              final int sampleFormat = extractSampleFormat(directory);
61              return new DataReaderStrips(directory, photometricInterpreter, bitsPerPixel, bitsPerSample, predictor, samplesPerPixel, sampleFormat, width, height,
62                      compression, planarConfiguration, byteorder, rowsPerStrip, this);
63          }
64  
65          @Override
66          public AbstractTiffElement.DataElement[] getImageData() {
67              return strips;
68          }
69  
70          public AbstractTiffElement.DataElement getImageData(final int offset) {
71              return strips[offset];
72          }
73  
74          public int getImageDataLength() {
75              return strips.length;
76          }
77  
78          @Override
79          public boolean stripsNotTiles() {
80              return true;
81          }
82  
83      }
84  
85      public static class Tiles extends AbstractTiffImageData {
86  
87          public final AbstractTiffElement.DataElement[] tiles;
88  
89          // public final byte tiles[][];
90          private final int tileWidth;
91          private final int tileLength;
92  
93          public Tiles(final AbstractTiffElement.DataElement[] tiles, final int tileWidth, final int tileLength) {
94              this.tiles = tiles;
95              this.tileWidth = tileWidth;
96              this.tileLength = tileLength;
97          }
98  
99          @Override
100         public ImageDataReader getDataReader(final TiffDirectory directory, final PhotometricInterpreter photometricInterpreter, final int bitsPerPixel,
101                 final int[] bitsPerSample, final int predictor, final int samplesPerPixel, final int width, final int height, final int compression,
102                 final TiffPlanarConfiguration planarConfiguration, final ByteOrder byteOrder) throws IOException, ImagingException {
103             final int sampleFormat = extractSampleFormat(directory);
104             return new DataReaderTiled(directory, photometricInterpreter, tileWidth, tileLength, bitsPerPixel, bitsPerSample, predictor, samplesPerPixel,
105                     sampleFormat, width, height, compression, planarConfiguration, byteOrder, this);
106         }
107 
108         @Override
109         public AbstractTiffElement.DataElement[] getImageData() {
110             return tiles;
111         }
112 
113         /**
114          * Gets the height of individual tiles. Note that if the overall image height is not a multiple of the tile height, then the last row of tiles may
115          * extend beyond the image height.
116          *
117          * @return an integer value greater than zero
118          */
119         public int getTileHeight() {
120             return tileLength;
121         }
122 
123         /**
124          * Gets the width of individual tiles. Note that if the overall image width is not a multiple of the tile width, then the last column of tiles may
125          * extend beyond the image width.
126          *
127          * @return an integer value greater than zero
128          */
129         public int getTileWidth() {
130             return tileWidth;
131         }
132 
133         @Override
134         public boolean stripsNotTiles() {
135             return false;
136         }
137 
138         // public TiffElement[] getElements()
139         // {
140         // return tiles;
141         // }
142     }
143 
144     private static int extractSampleFormat(final TiffDirectory directory) throws ImagingException {
145         final short[] sSampleFmt = directory.getFieldValue(TiffTagConstants.TIFF_TAG_SAMPLE_FORMAT, false);
146         if (sSampleFmt != null && sSampleFmt.length > 0) {
147             return sSampleFmt[0];
148         }
149         return 0; // unspecified format
150     }
151 
152     public abstract ImageDataReader getDataReader(TiffDirectory directory, PhotometricInterpreter photometricInterpreter, int bitsPerPixel, int[] bitsPerSample,
153             int predictor, int samplesPerPixel, int width, int height, int compression, TiffPlanarConfiguration planarConfiguration, ByteOrder byteOrder)
154             throws IOException, ImagingException;
155 
156     public abstract AbstractTiffElement.DataElement[] getImageData();
157 
158     public abstract boolean stripsNotTiles();
159 }