use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class NiftiReader method populatePixelType.
// -- Helper methods --
private void populatePixelType(int dataType) throws FormatException {
CoreMetadata m = core.get(0);
switch(dataType) {
case 1:
case 2:
m.pixelType = FormatTools.UINT8;
break;
case 4:
m.pixelType = FormatTools.INT16;
break;
case 8:
m.pixelType = FormatTools.INT32;
break;
case 16:
m.pixelType = FormatTools.FLOAT;
break;
case 64:
m.pixelType = FormatTools.DOUBLE;
break;
case 128:
m.pixelType = FormatTools.UINT8;
m.sizeC = 3;
case 256:
m.pixelType = FormatTools.INT8;
break;
case 512:
m.pixelType = FormatTools.UINT16;
break;
case 768:
m.pixelType = FormatTools.UINT32;
break;
case 2304:
m.pixelType = FormatTools.UINT8;
m.sizeC = 4;
default:
throw new FormatException("Unsupported data type: " + dataType);
}
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class NikonReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
// reset image dimensions
// the actual image data is stored in IFDs referenced by the SubIFD tag
// in the 'real' IFD
CoreMetadata m = core.get(0);
m.imageCount = ifds.size();
IFD firstIFD = ifds.get(0);
PhotoInterp photo = firstIFD.getPhotometricInterpretation();
int samples = firstIFD.getSamplesPerPixel();
m.rgb = samples > 1 || photo == PhotoInterp.RGB || photo == PhotoInterp.CFA_ARRAY;
if (photo == PhotoInterp.CFA_ARRAY)
samples = 3;
m.sizeX = (int) firstIFD.getImageWidth();
m.sizeY = (int) firstIFD.getImageLength();
m.sizeZ = 1;
m.sizeC = isRGB() ? samples : 1;
m.sizeT = ifds.size();
m.pixelType = firstIFD.getPixelType();
m.indexed = false;
// now look for the EXIF IFD pointer
IFDList exifIFDs = tiffParser.getExifIFDs();
if (exifIFDs.size() > 0) {
IFD exifIFD = exifIFDs.get(0);
tiffParser.fillInIFD(exifIFD);
for (Integer key : exifIFD.keySet()) {
int tag = key.intValue();
String name = IFD.getIFDTagName(tag);
if (tag == IFD.CFA_PATTERN) {
byte[] cfa = (byte[]) exifIFD.get(key);
int[] colorMap = new int[cfa.length];
for (int i = 0; i < cfa.length; i++) colorMap[i] = (int) cfa[i];
addGlobalMeta(name, colorMap);
cfaPattern = colorMap;
} else {
addGlobalMeta(name, exifIFD.get(key));
if (name.equals("MAKER_NOTE")) {
byte[] b = (byte[]) exifIFD.get(key);
int extra = new String(b, 0, 10, Constants.ENCODING).startsWith("Nikon") ? 10 : 0;
byte[] buf = new byte[b.length];
System.arraycopy(b, extra, buf, 0, buf.length - extra);
RandomAccessInputStream makerNote = new RandomAccessInputStream(buf);
TiffParser tp = new TiffParser(makerNote);
IFD note = null;
try {
note = tp.getFirstIFD();
} catch (Exception e) {
LOGGER.debug("Failed to parse first IFD", e);
}
if (note != null) {
for (Integer nextKey : note.keySet()) {
int nextTag = nextKey.intValue();
addGlobalMeta(name, note.get(nextKey));
if (nextTag == 150) {
b = (byte[]) note.get(nextKey);
RandomAccessInputStream s = new RandomAccessInputStream(b);
byte check1 = s.readByte();
byte check2 = s.readByte();
lossyCompression = check1 != 0x46;
vPredictor = new int[4];
for (int q = 0; q < vPredictor.length; q++) {
vPredictor[q] = s.readShort();
}
curve = new int[16385];
int bps = ifds.get(0).getBitsPerSample()[0];
int max = 1 << bps & 0x7fff;
int step = 0;
int csize = s.readShort();
if (csize > 1) {
step = max / (csize - 1);
}
if (check1 == 0x44 && check2 == 0x20 && step > 0) {
for (int i = 0; i < csize; i++) {
curve[i * step] = s.readShort();
}
for (int i = 0; i < max; i++) {
int n = i % step;
curve[i] = (curve[i - n] * (step - n) + curve[i - n + step] * n) / step;
}
s.seek(562);
split = s.readShort();
} else {
int maxValue = (int) Math.pow(2, bps) - 1;
Arrays.fill(curve, maxValue);
int nElements = (int) (s.length() - s.getFilePointer()) / 2;
if (nElements < 100) {
for (int i = 0; i < curve.length; i++) {
curve[i] = (short) i;
}
} else {
for (int q = 0; q < nElements; q++) {
curve[q] = s.readShort();
}
}
}
s.close();
} else if (nextTag == WHITE_BALANCE_RGB_COEFFS) {
whiteBalance = (TiffRational[]) note.get(nextKey);
}
}
}
makerNote.close();
}
}
}
}
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class OpenlabRawReader 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);
// read the 12 byte file header
LOGGER.info("Verifying Openlab RAW format");
if (!in.readString(4).equals("OLRW")) {
throw new FormatException("Openlab RAW magic string not found.");
}
LOGGER.info("Populating metadata");
CoreMetadata m = core.get(0);
int version = in.readInt();
m.imageCount = in.readInt();
offsets = new int[getImageCount()];
offsets[0] = 12;
in.skipBytes(8);
m.sizeX = in.readInt();
m.sizeY = in.readInt();
in.skipBytes(1);
m.sizeC = in.read();
bytesPerPixel = in.read();
in.skipBytes(1);
long stampMs = in.readLong();
if (stampMs > 0) {
stampMs /= 1000000;
stampMs -= (67 * 365.25 * 24 * 60 * 60);
} else
stampMs = System.currentTimeMillis();
String stamp = DateTools.convertDate(stampMs, DateTools.UNIX);
in.skipBytes(4);
int len = in.read() & 0xff;
String imageName = in.readString(len - 1).trim();
if (getSizeC() <= 1)
m.sizeC = 1;
else
m.sizeC = 3;
int plane = getSizeX() * getSizeY() * bytesPerPixel;
for (int i = 1; i < getImageCount(); i++) {
offsets[i] = offsets[i - 1] + HEADER_SIZE + plane;
}
m.sizeZ = getImageCount();
m.sizeT = 1;
m.rgb = getSizeC() > 1;
m.dimensionOrder = isRGB() ? "XYCZT" : "XYZTC";
m.interleaved = false;
m.littleEndian = false;
m.metadataComplete = true;
m.indexed = false;
m.falseColor = false;
switch(bytesPerPixel) {
case 1:
case 3:
m.pixelType = FormatTools.UINT8;
break;
case 2:
m.pixelType = FormatTools.UINT16;
break;
default:
m.pixelType = FormatTools.FLOAT;
}
addGlobalMeta("Width", getSizeX());
addGlobalMeta("Height", getSizeY());
addGlobalMeta("Bytes per pixel", bytesPerPixel);
addGlobalMeta("Image name", imageName);
addGlobalMeta("Timestamp", stamp);
addGlobalMeta("Version", version);
// The metadata store we're working with.
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
if (stamp != null) {
store.setImageAcquisitionDate(new Timestamp(stamp), 0);
}
store.setImageName(imageName, 0);
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class PCIReader method initPOIService.
// -- Helper methods --
private void initPOIService() throws FormatException, IOException {
try {
ServiceFactory factory = new ServiceFactory();
poi = factory.getInstance(POIService.class);
} catch (DependencyException de) {
throw new FormatException("POI library not found", de);
}
poi.initialize(Location.getMappedId(getCurrentFile()));
}
use of loci.formats.FormatException in project bioformats by openmicroscopy.
the class PCIReader method getOptimalTileHeight.
/* @see loci.formats.IFormatReader#getOptimalTileHeight() */
@Override
public int getOptimalTileHeight() {
FormatTools.assertId(currentId, true, 1);
String file = imageFiles.get(0);
try {
if (poi == null) {
initPOIService();
}
RandomAccessInputStream s = poi.getDocumentStream(file);
TiffParser tp = new TiffParser(s);
if (tp.isValidHeader()) {
IFD ifd = tp.getFirstIFD();
s.close();
return (int) ifd.getTileLength();
}
s.close();
} catch (FormatException e) {
LOGGER.debug("Could not retrieve tile height", e);
} catch (IOException e) {
LOGGER.debug("Could not retrieve tile height", e);
}
return super.getOptimalTileHeight();
}
Aggregations