1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
115
116
117
118
119 public int getTileHeight() {
120 return tileLength;
121 }
122
123
124
125
126
127
128
129 public int getTileWidth() {
130 return tileWidth;
131 }
132
133 @Override
134 public boolean stripsNotTiles() {
135 return false;
136 }
137
138
139
140
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;
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 }