📄 tiffdecoder.java
字号:
/**
*
*/
package com.intohotel.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Vector;
/**
* @author majh
*
*/
public class TiffDecoder {
// tags
public static final int NEW_SUBFILE_TYPE = 254;
public static final int IMAGE_WIDTH = 256;
public static final int IMAGE_LENGTH = 257;
public static final int BITS_PER_SAMPLE = 258;
public static final int COMPRESSION = 259;
public static final int PHOTO_INTERP = 262;
public static final int IMAGE_DESCRIPTION = 270;
public static final int STRIP_OFFSETS = 273;
public static final int SAMPLES_PER_PIXEL = 277;
public static final int ROWS_PER_STRIP = 278;
public static final int STRIP_BYTE_COUNT = 279;
public static final int X_RESOLUTION = 282;
public static final int Y_RESOLUTION = 283;
public static final int PLANAR_CONFIGURATION = 284;
public static final int RESOLUTION_UNIT = 296;
public static final int SOFTWARE = 305;
public static final int DATE_TIME = 306;
public static final int PREDICTOR = 317;
public static final int COLOR_MAP = 320;
public static final int SAMPLE_FORMAT = 339;
public static final int METAMORPH1 = 33628;
public static final int METAMORPH2 = 33629;
public static final int IPLAB = 34122;
public static final int NIH_IMAGE_HDR = 43314;
public static final int META_DATA_BYTE_COUNTS = 50838; // private tag registered with Adobe
public static final int META_DATA = 50839; // private tag registered with Adobe
//constants
static final int UNSIGNED = 1;
static final int SIGNED = 2;
static final int FLOATING_POINT = 3;
//field types
static final int SHORT = 3;
static final int LONG = 4;
private String directory;
private String name;
private String url;
protected RandomAccessStream in;
protected boolean debugMode;
private boolean littleEndian;
private String dInfo;
private int ifdCount;
private int[] metaDataCounts;
public TiffDecoder(String directory, String name) {
this.directory = directory;
this.name = name;
}
public TiffDecoder(InputStream in, String name) {
directory = "";
this.name = name;
url = "";
this.in = new RandomAccessStream(in);
}
final int getInt() throws IOException {
int b1 = in.read();
int b2 = in.read();
int b3 = in.read();
int b4 = in.read();
if (littleEndian)
return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0));
else
return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
}
int getShort() throws IOException {
int b1 = in.read();
int b2 = in.read();
if (littleEndian)
return ((b2 << 8) + b1);
else
return ((b1 << 8) + b2);
}
int OpenImageFileHeader() throws IOException {
// Open 8-byte Image File Header at start of file.
// Returns the offset in bytes to the first IFD or -1
// if this is not a valid tiff file.
int byteOrder = in.readShort();
if (byteOrder==0x4949) // "II"
littleEndian = true;
else if (byteOrder==0x4d4d) // "MM"
littleEndian = false;
else {
in.close();
return -1;
}
int magicNumber = getShort(); // 42
int offset = getInt();
return offset;
}
int getValue(int fieldType, int count) throws IOException {
int value = 0;
int unused;
if (fieldType==SHORT && count==1) {
value = getShort();
unused = getShort();
}
else
value = getInt();
return value;
}
void getColorMap(int offset, FileInfo fi) throws IOException {
byte[] colorTable16 = new byte[768*2];
int saveLoc = in.getFilePointer();
in.seek(offset);
in.readFully(colorTable16);
in.seek(saveLoc);
fi.lutSize = 256;
fi.reds = new byte[256];
fi.greens = new byte[256];
fi.blues = new byte[256];
int j = 0;
if (littleEndian)
j++;
for (int i=0; i<256; i++) {
fi.reds[i] = colorTable16[j];
fi.greens[i] = colorTable16[512+j];
fi.blues[i] = colorTable16[1024+j];
j += 2;
}
fi.fileType = FileInfo.COLOR8;
}
byte[] getString(int count, int offset) throws IOException {
count--; // skip null byte at end of string
if (count<=0)
return null;
byte[] bytes = new byte[count];
int saveLoc = in.getFilePointer();
in.seek(offset);
in.readFully(bytes);
in.seek(saveLoc);
return bytes;
}
/** Save the image description in the specified FileInfo. ImageJ
saves spatial and density calibration data in this string. For
stacks, it also saves the number of images to avoid having to
decode an IFD for each image. */
public void saveImageDescription(byte[] description, FileInfo fi) {
if (description.length<7)
return;
//if (ij.IJ.debugMode)
// ij.IJ.log("Image Description: " + new String(description).replace('\n',' '));
String id = new String(description);
fi.description = id;
int index1 = id.indexOf("images=");
if (index1>0) {
int index2 = id.indexOf("\n", index1);
if (index2>0) {
String images = id.substring(index1+7,index2);
int n = (int)Tools.parseDouble(images, 0.0);
if (n>1) fi.nImages = n;
}
}
}
void decodeNIHImageHeader(int offset, FileInfo fi) throws IOException {
int saveLoc = in.getFilePointer();
in.seek(offset+12);
int version = in.readShort();
in.seek(offset+160);
double scale = in.readDouble();
if (version>106 && scale!=0.0) {
fi.pixelWidth = 1.0/scale;
fi.pixelHeight = fi.pixelWidth;
}
// spatial calibration
in.seek(offset+172);
int units = in.readShort();
if (version<=153) units += 5;
switch (units) {
case 5: fi.unit = "nanometer"; break;
case 6: fi.unit = "micrometer"; break;
case 7: fi.unit = "mm"; break;
case 8: fi.unit = "cm"; break;
case 9: fi.unit = "meter"; break;
case 10: fi.unit = "km"; break;
case 11: fi.unit = "inch"; break;
case 12: fi.unit = "ft"; break;
case 13: fi.unit = "mi"; break;
}
// density calibration
in.seek(offset+182);
int fitType = in.read();
int unused = in.read();
int nCoefficients = in.readShort();
if (fitType==11) {
fi.calibrationFunction = 21; //Calibration.UNCALIBRATED_OD
fi.valueUnit = "U. OD";
} else if (fitType>=0 && fitType<=8 && nCoefficients>=1 && nCoefficients<=5) {
switch (fitType) {
case 0: fi.calibrationFunction = 0; break; //Calibration.STRAIGHT_LINE
case 1: fi.calibrationFunction = 1; break; //Calibration.POLY2
case 2: fi.calibrationFunction = 2; break; //Calibration.POLY3
case 3: fi.calibrationFunction = 3; break; //Calibration.POLY4
case 5: fi.calibrationFunction = 4; break; //Calibration.EXPONENTIAL
case 6: fi.calibrationFunction = 5; break; //Calibration.POWER
case 7: fi.calibrationFunction = 6; break; //Calibration.LOG
case 8: fi.calibrationFunction = 10; break; //Calibration.RODBARD2 (NIH Image)
}
fi.coefficients = new double[nCoefficients];
for (int i=0; i<nCoefficients; i++) {
fi.coefficients[i] = in.readDouble();
}
in.seek(offset+234);
int size = in.read();
StringBuffer sb = new StringBuffer();
if (size>=1 && size<=16) {
for (int i=0; i<size; i++)
sb.append((char)(in.read()));
fi.valueUnit = new String(sb);
} else
fi.valueUnit = " ";
}
in.seek(offset+260);
int nImages = in.readShort();
if(nImages>=2 && (fi.fileType==FileInfo.GRAY8||fi.fileType==FileInfo.COLOR8)) {
fi.nImages = nImages;
fi.pixelDepth = in.readFloat(); //SliceSpacing
int skip = in.readShort(); //CurrentSlice
fi.frameInterval = in.readFloat();
//ij.IJ.write("fi.pixelDepth: "+fi.pixelDepth);
}
in.seek(offset+272);
float aspectRatio = in.readFloat();
if (version>140 && aspectRatio!=0.0)
fi.pixelHeight = fi.pixelWidth/aspectRatio;
in.seek(saveLoc);
}
void dumpTag(int tag, int count, int value, FileInfo fi) {
String name;
switch (tag) {
case NEW_SUBFILE_TYPE: name="NewSubfileType"; break;
case IMAGE_WIDTH: name="ImageWidth"; break;
case IMAGE_LENGTH: name="ImageLength"; break;
case STRIP_OFFSETS: name="StripOffsets"; break;
case PHOTO_INTERP: name="PhotoInterp"; break;
case IMAGE_DESCRIPTION: name="ImageDescription"; break;
case BITS_PER_SAMPLE: name="BitsPerSample"; break;
case SAMPLES_PER_PIXEL: name="SamplesPerPixel"; break;
case ROWS_PER_STRIP: name="RowsPerStrip"; break;
case STRIP_BYTE_COUNT: name="StripByteCount"; break;
case X_RESOLUTION: name="XResolution"; break;
case Y_RESOLUTION: name="YResolution"; break;
case RESOLUTION_UNIT: name="ResolutionUnit"; break;
case SOFTWARE: name="Software"; break;
case DATE_TIME: name="DateTime"; break;
case PLANAR_CONFIGURATION: name="PlanarConfiguration"; break;
case COMPRESSION: name="Compression"; break;
case PREDICTOR: name="Predictor"; break;
case COLOR_MAP: name="ColorMap"; break;
case SAMPLE_FORMAT: name="SampleFormat"; break;
case NIH_IMAGE_HDR: name="NIHImageHeader"; break;
case META_DATA_BYTE_COUNTS: name="MetaDataByteCounts"; break;
case META_DATA: name="MetaData"; break;
default: name="???"; break;
}
String cs = (count==1)?"":", count=" + count;
dInfo += " " + tag + ", \"" + name + "\", value=" + value + cs + "\n";
//ij.IJ.log(tag + ", \"" + name + "\", value=" + value + cs + "\n");
}
double getRational(int loc) throws IOException {
int saveLoc = in.getFilePointer();
in.seek(loc);
int numerator = getInt();
int denominator = getInt();
in.seek(saveLoc);
//System.out.println("numerator: "+numerator);
//System.out.println("denominator: "+denominator);
if (denominator!=0)
return (double)numerator/denominator;
else
return 0.0;
}
FileInfo OpenIFD() throws IOException {
// Get Image File Directory data
int tag, fieldType, count, value;
int nEntries = getShort();
if (nEntries<1 || nEntries>1000)
return null;
ifdCount++;
FileInfo fi = new FileInfo();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -