use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class EPSReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id);
CoreMetadata m = core.get(0);
LOGGER.info("Verifying EPS format");
String line = readLine();
if (!line.trim().startsWith("%!PS")) {
// read the TIFF preview
isTiff = true;
in.order(true);
in.seek(20);
int offset = in.readInt();
int len = in.readInt();
byte[] b = new byte[len];
in.seek(offset);
in.read(b);
in = new RandomAccessInputStream(b);
TiffParser tp = new TiffParser(in);
ifds = tp.getIFDs();
IFD firstIFD = ifds.get(0);
map = tp.getColorMap(firstIFD);
m.sizeX = (int) firstIFD.getImageWidth();
m.sizeY = (int) firstIFD.getImageLength();
m.sizeZ = 1;
m.sizeT = 1;
m.sizeC = firstIFD.getSamplesPerPixel();
if (map != null && getSizeC() == 1) {
m.sizeC = 3;
}
if (getSizeC() == 2)
m.sizeC = 4;
m.littleEndian = firstIFD.isLittleEndian();
m.interleaved = true;
m.rgb = getSizeC() > 1;
m.pixelType = firstIFD.getPixelType();
m.imageCount = 1;
m.dimensionOrder = "XYCZT";
m.metadataComplete = true;
m.indexed = false;
m.falseColor = false;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
return;
}
LOGGER.info("Finding image data");
binary = false;
String image = "image";
int lineNum = 1;
line = readLine().trim();
while (line != null && !line.equals("%%EOF")) {
if (line.endsWith(image)) {
if (!line.startsWith(image)) {
if (line.indexOf("colorimage") != -1)
m.sizeC = 3;
String[] t = line.split(" ");
try {
int newX = Integer.parseInt(t[0]);
int newY = Integer.parseInt(t[1]);
if (t.length > 2 && Integer.parseInt(t[2]) >= 8) {
m.sizeX = newX;
m.sizeY = newY;
start = lineNum;
}
} catch (NumberFormatException exc) {
LOGGER.debug("Could not parse image dimensions", exc);
if (t.length > 3) {
m.sizeC = Integer.parseInt(t[3]);
}
}
}
break;
} else if (line.startsWith("%%")) {
if (line.startsWith("%%BoundingBox:")) {
line = line.substring(14).trim();
String[] t = line.split(" ");
try {
int originX = Integer.parseInt(t[0].trim());
int originY = Integer.parseInt(t[1].trim());
m.sizeX = Integer.parseInt(t[2].trim()) - originX;
m.sizeY = Integer.parseInt(t[3].trim()) - originY;
addGlobalMeta("X-coordinate of origin", originX);
addGlobalMeta("Y-coordinate of origin", originY);
} catch (NumberFormatException e) {
throw new FormatException("Files without image data are not supported.");
}
} else if (line.startsWith("%%BeginBinary")) {
binary = true;
} else {
// parse key/value pairs
int ndx = line.indexOf(':');
if (ndx != -1) {
String key = line.substring(0, ndx);
String value = line.substring(ndx + 1);
addGlobalMeta(key, value);
}
}
} else if (line.startsWith("%ImageData:")) {
line = line.substring(11);
String[] t = line.split(" ");
m.sizeX = Integer.parseInt(t[0]);
m.sizeY = Integer.parseInt(t[1]);
m.sizeC = Integer.parseInt(t[3]);
for (int i = 4; i < t.length; i++) {
image = t[i].trim();
if (image.length() > 1) {
image = image.substring(1, image.length() - 1);
}
}
}
lineNum++;
line = readLine().trim();
}
LOGGER.info("Populating metadata");
if (getSizeC() == 0)
m.sizeC = 1;
m.sizeZ = 1;
m.sizeT = 1;
m.dimensionOrder = "XYCZT";
m.pixelType = FormatTools.UINT8;
m.rgb = getSizeC() == 3;
m.interleaved = true;
m.littleEndian = true;
m.imageCount = 1;
// Populate metadata store
// The metadata store we're working with.
MetadataStore store = getMetadataStore();
MetadataTools.populatePixels(store, this);
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class ZeissLSMReader method isThisType.
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
final int blockLen = 4096;
if (!FormatTools.validStream(stream, blockLen, false))
return false;
TiffParser parser = new TiffParser(stream);
if (parser.isValidHeader()) {
return parser.getIFDOffsets().length > 1;
}
stream.seek(4);
if (stream.readShort() == 0x5374) {
String check = stream.readString((int) (blockLen - stream.getFilePointer()));
return check.indexOf("ID") > 0;
}
return false;
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class ZeissLSMReader method openBytes.
/**
* @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
*/
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
if (getSeriesCount() > 1) {
in.close();
in = new RandomAccessInputStream(getLSMFileFromSeries(getSeries()));
in.order(!isLittleEndian());
tiffParser = new TiffParser(in);
} else if (tiffParser == null) {
tiffParser = new TiffParser(in);
}
IFDList ifds = ifdsList.get(getSeries());
if (splitPlanes && getSizeC() > 1 && ifds.size() == getSizeZ() * getSizeT()) {
int bpp = FormatTools.getBytesPerPixel(getPixelType());
int plane = no / getSizeC();
int c = no % getSizeC();
Region region = new Region(x, y, w, h);
if (prevPlane != plane || prevBuf == null || prevBuf.length < w * h * bpp * getSizeC() || !region.equals(prevRegion)) {
prevBuf = new byte[w * h * bpp * getSizeC()];
tiffParser.getSamples(ifds.get(plane), prevBuf, x, y, w, h);
prevPlane = plane;
prevRegion = region;
}
ImageTools.splitChannels(prevBuf, buf, c, getSizeC(), bpp, false, false, w * h * bpp);
prevChannel = c;
} else {
tiffParser.getSamples(ifds.get(no), buf, x, y, w, h);
prevChannel = getZCTCoords(no)[1];
}
if (getSeriesCount() > 1)
in.close();
return buf;
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class ZeissLSMReader method getExtraSeries.
private int getExtraSeries(String file) throws FormatException, IOException {
if (in != null)
in.close();
in = new RandomAccessInputStream(file, 16);
boolean littleEndian = in.read() == TiffConstants.LITTLE;
in.order(littleEndian);
tiffParser = new TiffParser(in);
IFD ifd = tiffParser.getFirstIFD();
RandomAccessInputStream ras = getCZTag(ifd);
if (ras == null)
return 1;
ras.order(littleEndian);
ras.seek(264);
dimensionP = ras.readInt();
dimensionM = ras.readInt();
ras.close();
int nSeries = dimensionM * dimensionP;
return nSeries <= 0 ? 1 : nSeries;
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class TCSReader method isThisType.
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
// check for Leica TCS IFD directory entries
TiffParser tp = new TiffParser(stream);
IFD ifd = tp.getFirstIFD();
if (ifd == null) {
stream.seek(0);
return stream.readString(6).equals("<Data>");
}
String document = ifd.getIFDTextValue(IFD.DOCUMENT_NAME);
if (document == null)
document = "";
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
if (software == null)
software = "";
software = software.trim();
return document.startsWith("CHANNEL") || software.startsWith("TCS");
}
Aggregations