case 8:
break;
default:
throw new IllegalArgumentException("Bits per sample " + bitsPerSample + " is not supported.");
}
Image img = null;
int h = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
int w = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
int dpiX = 0;
int dpiY = 0;
int resolutionUnit = TIFFConstants.RESUNIT_INCH;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT))
resolutionUnit = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT);
dpiX = getDpi(dir.getField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
dpiY = getDpi(dir.getField(TIFFConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
int fillOrder = 1;
boolean reverse = false;
TIFFField fillOrderField = dir.getField(TIFFConstants.TIFFTAG_FILLORDER);
if (fillOrderField != null)
fillOrder = fillOrderField.getAsInt(0);
reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
int rowsStrip = h;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP)) //another hack for broken tiffs
rowsStrip = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP);
if (rowsStrip <= 0 || rowsStrip > h)
rowsStrip = h;
long offset[] = getArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPOFFSETS);
long size[] = getArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPBYTECOUNTS);
if ((size == null || (size.length == 1 && (size[0] == 0 || size[0] + offset[0] > s.length()))) && h == rowsStrip) { // some TIFF producers are really lousy, so...
size = new long[]{s.length() - (int)offset[0]};
}
if (compression == TIFFConstants.COMPRESSION_LZW) {
TIFFField predictorField = dir.getField(TIFFConstants.TIFFTAG_PREDICTOR);
if (predictorField != null) {
predictor = predictorField.getAsInt(0);
if (predictor != 1 && predictor != 2) {
throw new RuntimeException("Illegal value for Predictor in TIFF file.");
}
if (predictor == 2 && bitsPerSample != 8) {
throw new RuntimeException(bitsPerSample + "-bit samples are not supported for Horizontal differencing Predictor.");
}
}
lzwDecoder = new TIFFLZWDecoder(w, predictor,
samplePerPixel);
}
int rowsLeft = h;
ByteArrayOutputStream stream = null;
DeflaterOutputStream zip = null;
CCITTG4Encoder g4 = null;
if (bitsPerSample == 1 && samplePerPixel == 1) {
g4 = new CCITTG4Encoder(w);
}
else {
stream = new ByteArrayOutputStream();
if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG)
zip = new DeflaterOutputStream(stream);
}
if (compression == TIFFConstants.COMPRESSION_OJPEG) {
// Assume that the TIFFTAG_JPEGIFBYTECOUNT tag is optional, since it's obsolete and
// is often missing
if ((!dir.isTagPresent(TIFFConstants.TIFFTAG_JPEGIFOFFSET))) {
throw new IOException("Missing tag(s) for OJPEG compression.");
}
int jpegOffset = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFOFFSET);
int jpegLength = s.length() - jpegOffset;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT)) {
jpegLength = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT) +
(int)size[0];
}
byte[] jpeg = new byte[Math.min(jpegLength, s.length() - jpegOffset)];
int posFilePointer = s.getFilePointer();
posFilePointer += jpegOffset;
s.seek(posFilePointer);
s.readFully(jpeg);
img = new Jpeg(jpeg);
}
else if (compression == TIFFConstants.COMPRESSION_JPEG) {
if (size.length > 1)
throw new IOException("Compression JPEG is only supported with a single strip. This image has " + size.length + " strips.");
byte[] jpeg = new byte[(int)size[0]];
s.seek(offset[0]);
s.readFully(jpeg);
img = new Jpeg(jpeg);
}
else {
for (int k = 0; k < offset.length; ++k) {
byte im[] = new byte[(int)size[k]];
s.seek(offset[k]);
s.readFully(im);
int height = Math.min(rowsStrip, rowsLeft);
byte outBuf[] = null;
if (compression != TIFFConstants.COMPRESSION_NONE)
outBuf = new byte[(w * bitsPerSample * samplePerPixel + 7) / 8 * height];
if (reverse)
TIFFFaxDecoder.reverseBits(im);
switch (compression) {
case TIFFConstants.COMPRESSION_DEFLATE:
case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
inflate(im, outBuf);
break;
case TIFFConstants.COMPRESSION_NONE:
outBuf = im;
break;
case TIFFConstants.COMPRESSION_PACKBITS:
decodePackbits(im, outBuf);
break;
case TIFFConstants.COMPRESSION_LZW:
lzwDecoder.decode(im, outBuf, height);
break;
}
if (bitsPerSample == 1 && samplePerPixel == 1) {
g4.fax4Encode(outBuf, height);
}
else {
zip.write(outBuf);
}
rowsLeft -= rowsStrip;
}
if (bitsPerSample == 1 && samplePerPixel == 1) {
img = Image.getInstance(w, h, false, Image.CCITTG4,
photometric == TIFFConstants.PHOTOMETRIC_MINISBLACK ? Image.CCITT_BLACKIS1 : 0, g4.close());
}
else {
zip.close();
img = Image.getInstance(w, h, samplePerPixel, bitsPerSample, stream.toByteArray());
img.setDeflated(true);
}
}
img.setDpi(dpiX, dpiY);
if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG) {
if (dir.isTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
try {
TIFFField fd = dir.getField(TIFFConstants.TIFFTAG_ICCPROFILE);
ICC_Profile icc_prof = ICC_Profile.getInstance(fd.getAsBytes());
if (samplePerPixel == icc_prof.getNumComponents())
img.tagICC(icc_prof);
}
catch (RuntimeException e) {
//empty
}
}
if (dir.isTagPresent(TIFFConstants.TIFFTAG_COLORMAP)) {
TIFFField fd = dir.getField(TIFFConstants.TIFFTAG_COLORMAP);
char rgb[] = fd.getAsChars();
byte palette[] = new byte[rgb.length];
int gColor = rgb.length / 3;
int bColor = gColor * 2;
for (int k = 0; k < gColor; ++k) {
palette[k * 3] = (byte)(rgb[k] >>> 8);
palette[k * 3 + 1] = (byte)(rgb[k + gColor] >>> 8);
palette[k * 3 + 2] = (byte)(rgb[k + bColor] >>> 8);
}
PdfArray indexed = new PdfArray();
indexed.add(PdfName.INDEXED);
indexed.add(PdfName.DEVICERGB);
indexed.add(new PdfNumber(gColor - 1));
indexed.add(new PdfString(palette));
PdfDictionary additional = new PdfDictionary();
additional.put(PdfName.COLORSPACE, indexed);
img.setAdditional(additional);
}
img.setOriginalType(Image.ORIGINAL_TIFF);
}
if (photometric == TIFFConstants.PHOTOMETRIC_MINISWHITE)
img.setInverted(true);
if (rotation != 0)
img.setInitialRotation(rotation);
return img;
}
catch (Exception e) {
throw new ExceptionConverter(e);
}