use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class ImagicReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "hed")) {
id = id.substring(0, id.lastIndexOf(".")) + ".hed";
}
super.initFile(id);
in = new RandomAccessInputStream(id);
pixels = id.substring(0, id.lastIndexOf(".")) + ".img";
pixelsFile = new RandomAccessInputStream(pixels);
CoreMetadata m = core.get(0);
m.littleEndian = true;
in.order(isLittleEndian());
pixelsFile.order(isLittleEndian());
int nImages = (int) (in.length() / 1024);
String imageName = null;
double physicalXSize = 0d;
double physicalYSize = 0d;
double physicalZSize = 0d;
for (int i = 0; i < nImages; i++) {
in.seek(i * 1024);
in.skipBytes(16);
int month = in.readInt();
int day = in.readInt();
int year = in.readInt();
int hour = in.readInt();
int minute = in.readInt();
int seconds = in.readInt();
in.skipBytes(8);
m.sizeY = in.readInt();
m.sizeX = in.readInt();
String type = in.readString(4);
if (type.equals("REAL")) {
m.pixelType = FormatTools.FLOAT;
} else if (type.equals("INTG")) {
m.pixelType = FormatTools.UINT16;
} else if (type.equals("PACK")) {
m.pixelType = FormatTools.UINT8;
} else if (type.equals("COMP")) {
throw new FormatException("Unsupported pixel type 'COMP'");
} else if (type.equals("RECO")) {
throw new FormatException("Unsupported pixel type 'RECO'");
}
int ixold = in.readInt();
int iyold = in.readInt();
float averageDensity = in.readFloat();
float sigma = in.readFloat();
in.skipBytes(8);
float maxDensity = in.readFloat();
float minDensity = in.readFloat();
in.skipBytes(4);
float defocus1 = in.readFloat();
float defocus2 = in.readFloat();
float defocusAngle = in.readFloat();
float startAngle = in.readFloat();
float endAngle = in.readFloat();
imageName = in.readString(80);
float ccc3d = in.readFloat();
int ref3d = in.readInt();
int micrographID = in.readInt();
int zShift = in.readInt();
float alpha = in.readFloat();
float beta = in.readFloat();
float gamma = in.readFloat();
in.skipBytes(8);
int nAliSum = in.readInt();
int pointGroup = in.readInt();
in.skipBytes(28);
int version = in.readInt();
int stamp = in.readInt();
in.skipBytes(120);
float angle = in.readFloat();
float voltage = in.readFloat();
float sphericalAberration = in.readFloat();
float partialCoherence = in.readFloat();
float ccc = in.readFloat();
float errar = in.readFloat();
float err3d = in.readFloat();
int ref = in.readInt();
float classNumber = in.readFloat();
in.skipBytes(4);
float representationQuality = in.readFloat();
float eqZShift = in.readFloat();
float xShift = in.readFloat();
float yShift = in.readFloat();
float numcls = in.readFloat();
float overallQuality = in.readFloat();
float equivalentAngle = in.readFloat();
float eqXShift = in.readFloat();
float eqYShift = in.readFloat();
float cmToVar = in.readFloat();
float informat = in.readFloat();
int nEigenvalues = in.readInt();
int nActiveImages = in.readInt();
physicalXSize = in.readFloat();
physicalYSize = in.readFloat();
physicalZSize = in.readFloat();
addGlobalMeta("IXOLD", ixold);
addGlobalMeta("IYOLD", iyold);
addGlobalMeta("Average density (AVDENS)", averageDensity);
addGlobalMeta("SIGMA", sigma);
addGlobalMeta("Maximum density (DENSMAX)", maxDensity);
addGlobalMeta("Minimum density (DENSMIN)", minDensity);
addGlobalMeta("DEFOCUS1", defocus1);
addGlobalMeta("DEFOCUS2", defocus2);
addGlobalMeta("Defocus angle (DEFANGLE)", defocusAngle);
addGlobalMeta("SINOSTRT", startAngle);
addGlobalMeta("SINOEND", endAngle);
addGlobalMeta("Image name", imageName);
addGlobalMeta("CCC3D", ccc3d);
addGlobalMeta("REF3D", ref3d);
addGlobalMeta("MIDENT", micrographID);
addGlobalMeta("EZSHIFT", zShift);
addGlobalMeta("EALPHA", alpha);
addGlobalMeta("EBETA", beta);
addGlobalMeta("EGAMMA", gamma);
addGlobalMeta("NALISUM", nAliSum);
addGlobalMeta("PGROUP", pointGroup);
addGlobalMeta("IMAGIC Version (IMAVERS)", version);
addGlobalMeta("REALTYPE", stamp);
addGlobalMeta("ANGLE", angle);
addGlobalMeta("VOLTAGE (in kV)", voltage);
addGlobalMeta("SPABERR (in mm)", sphericalAberration);
addGlobalMeta("PCOHER", partialCoherence);
addGlobalMeta("CCC", ccc);
addGlobalMeta("ERRAR", errar);
addGlobalMeta("ERR3D", err3d);
addGlobalMeta("REF", ref);
addGlobalMeta("CLASSNO", classNumber);
addGlobalMeta("REPQUAL", representationQuality);
addGlobalMeta("ZSHIFT", eqZShift);
addGlobalMeta("XSHIFT", xShift);
addGlobalMeta("YSHIFT", yShift);
addGlobalMeta("NUMCLS", numcls);
addGlobalMeta("OVQUAL", overallQuality);
addGlobalMeta("EANGLE", equivalentAngle);
addGlobalMeta("EXSHIFT", eqXShift);
addGlobalMeta("EYSHIFT", eqYShift);
addGlobalMeta("CMTOTVAR", cmToVar);
addGlobalMeta("INFORMAT", informat);
addGlobalMeta("NUMEIGEN", nEigenvalues);
addGlobalMeta("NIACTIVE", nActiveImages);
addGlobalMeta("RESOLX", physicalXSize);
addGlobalMeta("RESOLY", physicalYSize);
addGlobalMeta("RESOLZ", physicalZSize);
}
m.sizeZ = nImages;
m.sizeC = 1;
m.sizeT = 1;
m.imageCount = nImages;
m.dimensionOrder = "XYZCT";
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setImageName(imageName.trim(), 0);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
Length sizeX = FormatTools.getPhysicalSizeX(physicalXSize * 0.0001);
Length sizeY = FormatTools.getPhysicalSizeY(physicalYSize * 0.0001);
Length sizeZ = FormatTools.getPhysicalSizeZ(physicalZSize * 0.0001);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}
}
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class InveonReader 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);
long planeSize = (long) FormatTools.getPlaneSize(this);
int index = getCoreIndex();
RandomAccessInputStream dat = new RandomAccessInputStream(datFile);
try {
dat.order(isLittleEndian());
dat.seek(dataPointers.get(index) + no * planeSize);
readPlane(dat, x, y, w, h, buf);
} finally {
dat.close();
}
return buf;
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class IvisionReader 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);
LOGGER.info("Populating metadata");
String version = in.readString(4);
addGlobalMeta("Version", version);
int fileFormat = in.read();
int dataType = in.read();
CoreMetadata m = core.get(0);
m.sizeC = 1;
switch(dataType) {
case 0:
m.pixelType = FormatTools.UINT8;
break;
case 1:
m.pixelType = FormatTools.INT16;
break;
case 2:
m.pixelType = FormatTools.INT32;
break;
case 3:
m.pixelType = FormatTools.FLOAT;
break;
case 4:
m.pixelType = FormatTools.UINT8;
m.sizeC = 3;
color16 = true;
break;
case 5:
m.pixelType = FormatTools.UINT8;
m.sizeC = 3;
hasPaddingByte = true;
break;
case 6:
m.pixelType = FormatTools.UINT16;
break;
case 7:
m.pixelType = FormatTools.FLOAT;
squareRoot = true;
break;
case 8:
m.pixelType = FormatTools.UINT16;
m.sizeC = 3;
break;
}
m.sizeX = in.readInt();
m.sizeY = in.readInt();
in.skipBytes(6);
m.sizeZ = in.readShort();
in.skipBytes(50);
m.sizeT = 1;
if (getSizeX() > 1 && getSizeY() > 1) {
lut = new byte[2048];
in.read(lut);
}
imageOffset = in.getFilePointer();
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
in.skipBytes(getSizeZ() * getSizeC() * getSizeT() * getSizeX() * getSizeY() * FormatTools.getBytesPerPixel(getPixelType()));
// look for block of XML data
LOGGER.info("Looking for XML metadata");
in.findString(false, "<?xml");
if (in.getFilePointer() < in.length()) {
in.seek(in.getFilePointer() - 5);
String xml = in.readString((int) (in.length() - in.getFilePointer()));
xml = xml.substring(xml.indexOf('<'), xml.lastIndexOf("plist>") + 6);
IvisionHandler handler = new IvisionHandler();
try {
XMLTools.parseXML(xml, handler);
} catch (IOException e) {
LOGGER.debug("", e);
}
} else
LOGGER.debug("XML metadata not found");
}
LOGGER.info("Populating core metadata");
m.rgb = getSizeC() > 1;
m.dimensionOrder = "XYCZT";
m.littleEndian = false;
m.interleaved = true;
m.indexed = false;
m.imageCount = getSizeZ() * getSizeT();
LOGGER.info("Populating MetadataStore");
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
if (creationDate != null) {
String date = DateTools.formatDate(creationDate, DATE_FORMAT);
if (date != null) {
store.setImageAcquisitionDate(new Timestamp(date), 0);
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
store.setImageInstrumentRef(instrumentID, 0);
if (deltaT != null) {
Double increment = 0d;
try {
increment = new Double(deltaT);
} catch (NumberFormatException e) {
LOGGER.debug("Failed to parse time increment", e);
}
if (increment != null) {
store.setPixelsTimeIncrement(new Time(increment, UNITS.SECOND), 0);
}
}
String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objectiveID, 0, 0);
store.setObjectiveSettingsID(objectiveID, 0);
store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
if (lensNA != null)
store.setObjectiveLensNA(lensNA, 0, 0);
if (magnification != null) {
store.setObjectiveNominalMagnification(magnification, 0, 0);
}
if (refractiveIndex != null) {
store.setObjectiveSettingsRefractiveIndex(refractiveIndex, 0);
}
String detectorID = MetadataTools.createLSID("Detector", 0, 0);
store.setDetectorID(detectorID, 0, 0);
store.setDetectorSettingsID(detectorID, 0, 0);
store.setDetectorType(getDetectorType("Other"), 0, 0);
store.setDetectorSettingsBinning(getBinning(binX + "x" + binY), 0, 0);
if (gain != null) {
try {
store.setDetectorSettingsGain(new Double(gain), 0, 0);
} catch (NumberFormatException e) {
LOGGER.debug("Failed to parse detector gain", e);
}
}
}
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class KhorosReader 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);
in.skipBytes(4);
in.order(true);
int dependency = in.readInt();
addGlobalMeta("Comment", in.readString(512));
in.order(dependency == 4 || dependency == 8);
CoreMetadata m = core.get(0);
m.sizeX = in.readInt();
m.sizeY = in.readInt();
in.skipBytes(28);
m.imageCount = in.readInt();
if (getImageCount() == 0)
m.imageCount = 1;
m.sizeC = in.readInt();
int type = in.readInt();
switch(type) {
case 0:
m.pixelType = FormatTools.INT8;
break;
case 1:
m.pixelType = FormatTools.UINT8;
break;
case 2:
m.pixelType = FormatTools.UINT16;
break;
case 4:
m.pixelType = FormatTools.INT32;
break;
case 5:
m.pixelType = FormatTools.FLOAT;
break;
case 9:
m.pixelType = FormatTools.DOUBLE;
break;
default:
throw new FormatException("Unsupported pixel type : " + type);
}
// read lookup table
in.skipBytes(12);
int c = in.readInt();
if (c > 1) {
m.sizeC = c;
int n = in.readInt();
lut = new byte[c][n];
in.skipBytes(436);
for (int i = 0; i < lut.length; i++) {
for (int j = 0; j < lut[0].length; j++) {
lut[i][j] = in.readByte();
}
}
} else
in.skipBytes(440);
offset = in.getFilePointer();
m.sizeZ = getImageCount();
m.sizeT = 1;
m.rgb = getSizeC() > 1;
m.interleaved = false;
m.littleEndian = dependency == 4 || dependency == 8;
m.dimensionOrder = "XYCZT";
m.indexed = lut != null;
m.falseColor = false;
m.metadataComplete = true;
if (isIndexed()) {
m.sizeC = 1;
m.rgb = false;
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
}
use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.
the class SBIGReader 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);
in.seek(HEADER_SIZE);
int width = getSizeX() * 2;
if (compressed) {
byte[] b = new byte[FormatTools.getPlaneSize(this)];
for (int row = 0; row < getSizeY(); row++) {
int rowLen = in.readShort();
if (rowLen == width) {
in.read(b, row * width, rowLen);
} else {
int bufferPointer = row * width;
in.read(b, bufferPointer, 2);
while (bufferPointer - row * width < width - 2) {
short prevPixel = DataTools.bytesToShort(b, bufferPointer, isLittleEndian());
bufferPointer += 2;
byte check = in.readByte();
if (check == -128) {
in.read(b, bufferPointer, 2);
} else {
prevPixel += check;
DataTools.unpackBytes(prevPixel, b, bufferPointer, 2, isLittleEndian());
}
}
}
}
RandomAccessInputStream stream = new RandomAccessInputStream(b);
readPlane(stream, x, y, w, h, buf);
stream.close();
} else
readPlane(in, x, y, w, h, buf);
return buf;
}
Aggregations