Code with Finding: |
class TiffImage {
/** Reads a page from a TIFF image.
* @param s the file source
* @param page the page to get. The first page is 1
* @param direct for single strip, CCITT images, generate the image
* by direct byte copying. It's faster but may not work
* every time
* @return the <CODE>Image</CODE>
*/
public static Image getTiffImage(RandomAccessFileOrArray s, int page, boolean direct) {
if (page < 1)
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.page.number.must.be.gt.eq.1"));
try {
TIFFDirectory dir = new TIFFDirectory(s, page - 1);
if (dir.isTagPresent(TIFFConstants.TIFFTAG_TILEWIDTH))
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("tiles.are.not.supported"));
int compression = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_COMPRESSION);
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
case TIFFConstants.COMPRESSION_CCITTFAX3:
case TIFFConstants.COMPRESSION_CCITTFAX4:
break;
default:
return getTiffImageColor(dir, s);
}
float rotation = 0;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_ORIENTATION)) {
int rot = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_ORIENTATION);
if (rot == TIFFConstants.ORIENTATION_BOTRIGHT || rot == TIFFConstants.ORIENTATION_BOTLEFT)
rotation = (float)Math.PI;
else if (rot == TIFFConstants.ORIENTATION_LEFTTOP || rot == TIFFConstants.ORIENTATION_LEFTBOT)
rotation = (float)(Math.PI / 2.0);
else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIENTATION_RIGHTBOT)
rotation = -(float)(Math.PI / 2.0);
}
Image img = null;
long tiffT4Options = 0;
long tiffT6Options = 0;
int fillOrder = 1;
int h = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
int w = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
int dpiX = 0;
int dpiY = 0;
float XYRatio = 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);
if (resolutionUnit == TIFFConstants.RESUNIT_NONE) {
if (dpiY != 0)
XYRatio = (float)dpiX / (float)dpiY;
dpiX = 0;
dpiY = 0;
}
int rowsStrip = h;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP))
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]};
}
boolean reverse = false;
TIFFField fillOrderField = dir.getField(TIFFConstants.TIFFTAG_FILLORDER);
if (fillOrderField != null)
fillOrder = fillOrderField.getAsInt(0);
reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
int params = 0;
if (dir.isTagPresent(TIFFConstants.TIFFTAG_PHOTOMETRIC)) {
long photo = dir.getFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC);
if (photo == TIFFConstants.PHOTOMETRIC_MINISBLACK)
params |= Image.CCITT_BLACKIS1;
}
int imagecomp = 0;
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
imagecomp = Image.CCITTG3_1D;
params |= Image.CCITT_ENCODEDBYTEALIGN | Image.CCITT_ENDOFBLOCK;
break;
case TIFFConstants.COMPRESSION_CCITTFAX3:
imagecomp = Image.CCITTG3_1D;
params |= Image.CCITT_ENDOFLINE | Image.CCITT_ENDOFBLOCK;
TIFFField t4OptionsField = dir.getField(TIFFConstants.TIFFTAG_GROUP3OPTIONS);
if (t4OptionsField != null) {
tiffT4Options = t4OptionsField.getAsLong(0);
if ((tiffT4Options & TIFFConstants.GROUP3OPT_2DENCODING) != 0)
imagecomp = Image.CCITTG3_2D;
if ((tiffT4Options & TIFFConstants.GROUP3OPT_FILLBITS) != 0)
params |= Image.CCITT_ENCODEDBYTEALIGN;
}
break;
case TIFFConstants.COMPRESSION_CCITTFAX4:
imagecomp = Image.CCITTG4;
TIFFField t6OptionsField = dir.getField(TIFFConstants.TIFFTAG_GROUP4OPTIONS);
if (t6OptionsField != null)
tiffT6Options = t6OptionsField.getAsLong(0);
break;
}
if (direct && rowsStrip == h) { //single strip, direct
byte im[] = new byte[(int)size[0]];
s.seek(offset[0]);
s.readFully(im);
img = Image.getInstance(w, h, false, imagecomp, params, im);
img.setInverted(true);
}
else {
int rowsLeft = h;
CCITTG4Encoder g4 = new CCITTG4Encoder(w);
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);
TIFFFaxDecoder decoder = new TIFFFaxDecoder(fillOrder, w, height);
byte outBuf[] = new byte[(w + 7) / 8 * height];
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
decoder.decode1D(outBuf, im, 0, height);
g4.fax4Encode(outBuf,height);
break;
case TIFFConstants.COMPRESSION_CCITTFAX3:
try {
decoder.decode2D(outBuf, im, 0, height, tiffT4Options);
}
catch (RuntimeException e) {
// let's flip the fill bits and try again...
tiffT4Options ^= TIFFConstants.GROUP3OPT_FILLBITS;
try {
decoder.decode2D(outBuf, im, 0, height, tiffT4Options);
}
catch (RuntimeException e2) {
throw e;
}
}
g4.fax4Encode(outBuf, height);
break;
case TIFFConstants.COMPRESSION_CCITTFAX4:
decoder.decodeT6(outBuf, im, 0, height, tiffT6Options);
g4.fax4Encode(outBuf, height);
break;
}
rowsLeft -= rowsStrip;
}
byte g4pic[] = g4.close();
img = Image.getInstance(w, h, false, Image.CCITTG4, params & Image.CCITT_BLACKIS1, g4pic);
}
img.setDpi(dpiX, dpiY);
img.setXYRatio(XYRatio);
if (dir.isTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
try {
TIFFField fd = dir.getField(TIFFConstants.TIFFTAG_ICCPROFILE);
ICC_Profile icc_prof = ICC_Profile.getInstance(fd.getAsBytes());
if (icc_prof.getNumComponents() == 1)
img.tagICC(icc_prof);
}
catch (RuntimeException e) {
//empty
}
}
img.setOriginalType(Image.ORIGINAL_TIFF);
if (rotation != 0)
img.setInitialRotation(rotation);
return img;
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
|