Search in sources :

Example 71 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class APLReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    LOGGER.debug("Initializing {}", id);
    // find the corresponding .mtb file
    if (!checkSuffix(id, "mtb")) {
        if (checkSuffix(id, METADATA_SUFFIXES)) {
            int separator = id.lastIndexOf(File.separator);
            if (separator < 0)
                separator = 0;
            int underscore = id.lastIndexOf("_");
            if (underscore < separator || checkSuffix(id, "apl")) {
                underscore = id.lastIndexOf(".");
            }
            String mtbFile = id.substring(0, underscore) + "_d.mtb";
            if (!new Location(mtbFile).exists()) {
                throw new FormatException(".mtb file not found");
            }
            currentId = new Location(mtbFile).getAbsolutePath();
        } else {
            Location parent = new Location(id).getAbsoluteFile().getParentFile();
            parent = parent.getParentFile();
            String[] list = parent.list(true);
            for (String f : list) {
                if (checkSuffix(f, "mtb")) {
                    currentId = new Location(parent, f).getAbsolutePath();
                    break;
                }
            }
            if (!checkSuffix(currentId, "mtb")) {
                throw new FormatException(".mtb file not found");
            }
        }
    }
    String mtb = new Location(currentId).getAbsolutePath();
    LOGGER.debug("Reading .mtb file '{}'", mtb);
    MDBService mdb = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        mdb = factory.getInstance(MDBService.class);
    } catch (DependencyException de) {
        throw new FormatException("MDB Tools Java library not found", de);
    }
    String[] columnNames = null;
    List<String[]> rows = null;
    try {
        mdb.initialize(mtb);
        rows = mdb.parseDatabase().get(0);
        columnNames = rows.get(0);
        String[] tmpNames = columnNames;
        columnNames = new String[tmpNames.length - 1];
        System.arraycopy(tmpNames, 1, columnNames, 0, columnNames.length);
    } finally {
        mdb.close();
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 1; i < rows.size(); i++) {
            String[] row = rows.get(i);
            for (int q = 0; q < row.length; q++) {
                addGlobalMetaList(columnNames[q], row[q]);
            }
        }
    }
    used = new ArrayList<String>();
    used.add(mtb);
    String tnb = mtb.substring(0, mtb.lastIndexOf("."));
    if (tnb.lastIndexOf("_") > tnb.lastIndexOf(File.separator)) {
        tnb = tnb.substring(0, tnb.lastIndexOf("_"));
    }
    used.add(tnb + "_1.tnb");
    used.add(tnb + ".apl");
    String idPath = new Location(id).getAbsolutePath();
    if (!used.contains(idPath) && checkSuffix(idPath, METADATA_SUFFIXES)) {
        used.add(idPath);
    }
    // calculate indexes to relevant metadata
    int calibrationUnit = DataTools.indexOf(columnNames, "Calibration Unit");
    int colorChannels = DataTools.indexOf(columnNames, "Color Channels");
    int frames = DataTools.indexOf(columnNames, "Frames");
    int calibratedHeight = DataTools.indexOf(columnNames, "Height");
    int calibratedWidth = DataTools.indexOf(columnNames, "Width");
    int path = DataTools.indexOf(columnNames, "Image Path");
    if (path == -1) {
        path = DataTools.indexOf(columnNames, "Path");
    }
    int filename = DataTools.indexOf(columnNames, "File Name");
    int magnification = DataTools.indexOf(columnNames, "Magnification");
    int width = DataTools.indexOf(columnNames, "X-Resolution");
    int height = DataTools.indexOf(columnNames, "Y-Resolution");
    int imageType = DataTools.indexOf(columnNames, "Image Type");
    int imageName = DataTools.indexOf(columnNames, "Image Name");
    int zLayers = DataTools.indexOf(columnNames, "Z-Layers");
    String parentDirectory = mtb.substring(0, mtb.lastIndexOf(File.separator));
    // look for the directory that contains TIFF and XML files
    LOGGER.debug("Searching {} for a directory with TIFFs", parentDirectory);
    Location dir = new Location(parentDirectory);
    String topDirectory = null;
    int index = 2;
    String pathName = rows.get(index++)[path].trim();
    while (pathName.equals("") && index < rows.size()) {
        pathName = rows.get(index++)[path].trim();
    }
    pathName = pathName.replace('\\', File.separatorChar);
    pathName = pathName.replaceAll("/", File.separator);
    String[] dirs = pathName.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
    for (int i = dirs.length - 1; i >= 0; i--) {
        if (dirs[i].indexOf("_DocumentFiles") > 0) {
            Location file = new Location(dir, dirs[i]);
            if (file.exists()) {
                topDirectory = file.getAbsolutePath();
                break;
            }
        }
    }
    if (topDirectory == null) {
        String[] list = dir.list();
        for (String f : list) {
            LOGGER.debug("  '{}'", f);
            Location file = new Location(dir, f);
            if (file.isDirectory() && f.indexOf("_DocumentFiles") > 0) {
                topDirectory = file.getAbsolutePath();
                LOGGER.debug("Found {}", topDirectory);
                break;
            }
        }
    }
    if (topDirectory == null) {
        throw new FormatException("Could not find a directory with TIFF files.");
    }
    final List<Integer> seriesIndexes = new ArrayList<Integer>();
    for (int i = 1; i < rows.size(); i++) {
        String file = parseFilename(rows.get(i), filename, path);
        if (file.equals(""))
            continue;
        file = topDirectory + File.separator + file;
        if (new Location(file).exists() && checkSuffix(file, "tif")) {
            seriesIndexes.add(i);
        }
    }
    int seriesCount = seriesIndexes.size();
    core.clear();
    tiffFiles = new String[seriesCount];
    xmlFiles = new String[seriesCount];
    parser = new TiffParser[seriesCount];
    ifds = new IFDList[seriesCount];
    for (int i = 0; i < seriesCount; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        int secondRow = seriesIndexes.get(i);
        int firstRow = secondRow - 1;
        String[] row2 = rows.get(firstRow);
        String[] row3 = rows.get(secondRow);
        if (frames > -1) {
            ms.sizeT = parseDimension(row3[frames]);
        }
        if (zLayers > -1) {
            ms.sizeZ = parseDimension(row3[zLayers]);
        }
        if (colorChannels > -1) {
            ms.sizeC = parseDimension(row3[colorChannels]);
        } else if (imageType > -1) {
            if (row3[imageType] != null && row3[imageType].equals("RGB")) {
                ms.sizeC = 3;
            }
        }
        ms.dimensionOrder = "XYCZT";
        if (ms.sizeZ == 0)
            ms.sizeZ = 1;
        if (ms.sizeC == 0)
            ms.sizeC = 1;
        if (ms.sizeT == 0)
            ms.sizeT = 1;
        xmlFiles[i] = topDirectory + File.separator + parseFilename(row2, filename, path);
        tiffFiles[i] = topDirectory + File.separator + parseFilename(row3, filename, path);
        parser[i] = new TiffParser(tiffFiles[i]);
        parser[i].setDoCaching(false);
        ifds[i] = parser[i].getIFDs();
        for (IFD ifd : ifds[i]) {
            parser[i].fillInIFD(ifd);
        }
        // get core metadata from TIFF file
        IFD ifd = ifds[i].get(0);
        PhotoInterp photo = ifd.getPhotometricInterpretation();
        int samples = ifd.getSamplesPerPixel();
        ms.sizeX = (int) ifd.getImageWidth();
        ms.sizeY = (int) ifd.getImageLength();
        ms.rgb = samples > 1 || photo == PhotoInterp.RGB;
        ms.pixelType = ifd.getPixelType();
        ms.littleEndian = ifd.isLittleEndian();
        ms.indexed = photo == PhotoInterp.RGB_PALETTE && ifd.containsKey(IFD.COLOR_MAP);
        ms.imageCount = ifds[i].size();
        if (ms.sizeZ * ms.sizeT * (ms.rgb ? 1 : ms.sizeC) != ms.imageCount) {
            ms.sizeT = ms.imageCount / (ms.rgb ? 1 : ms.sizeC);
            ms.sizeZ = 1;
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    for (int i = 0; i < seriesCount; i++) {
        String[] row = rows.get(seriesIndexes.get(i));
        // populate Image data
        MetadataTools.setDefaultCreationDate(store, mtb, i);
        store.setImageName(row[imageName].trim(), i);
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            // populate Dimensions data
            // calculate physical X and Y sizes
            double realWidth = Double.parseDouble(row[calibratedWidth]);
            double realHeight = Double.parseDouble(row[calibratedHeight]);
            String units = row[calibrationUnit];
            CoreMetadata ms = core.get(i);
            double px = realWidth / ms.sizeX;
            double py = realHeight / ms.sizeY;
            Length physicalSizeX = FormatTools.getPhysicalSizeX(px, units);
            Length physicalSizeY = FormatTools.getPhysicalSizeY(py, units);
            if (physicalSizeX != null) {
                store.setPixelsPhysicalSizeX(physicalSizeX, i);
            }
            if (physicalSizeY != null) {
                store.setPixelsPhysicalSizeY(physicalSizeY, i);
            }
        }
    }
    setSeries(0);
}
Also used : MDBService(loci.formats.services.MDBService) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) PhotoInterp(loci.formats.tiff.PhotoInterp) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) TiffParser(loci.formats.tiff.TiffParser) Location(loci.common.Location)

Example 72 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class BDReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser p = new TiffParser(stream);
    IFD ifd = p.getFirstIFD();
    if (ifd == null)
        return false;
    String software = ifd.getIFDTextValue(IFD.SOFTWARE);
    if (software == null)
        return false;
    return software.trim().startsWith("MATROX Imaging Library");
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 73 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class TiffParserTest method testBitsPerSampleMismatch.

@Test
public void testBitsPerSampleMismatch() throws IOException, FormatException {
    mock = new BitsPerSampleSamplesPerPixelMismatchMock();
    tiffParser = mock.getTiffParser();
    assertTrue(tiffParser.checkHeader());
    IFD ifd = tiffParser.getFirstIFD();
    int[] bitsPerSample = ifd.getBitsPerSample();
    int[] mockBitsPerSample = mock.getBitsPerSample();
    assertEquals(bitsPerSample.length, ifd.getSamplesPerPixel());
    for (int i = 0; i < mockBitsPerSample.length; i++) {
        assertEquals(bitsPerSample[i], mockBitsPerSample[i]);
    }
    mock.close();
}
Also used : IFD(loci.formats.tiff.IFD) Test(org.testng.annotations.Test)

Example 74 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class TiffSaverTest method setUp.

@BeforeMethod
public void setUp() throws IOException {
    ByteArrayHandle handle = new ByteArrayHandle(INITIAL_CAPACITY);
    out = new RandomAccessOutputStream(handle);
    in = new RandomAccessInputStream(handle);
    tiffSaver = new TiffSaver(out, handle);
    tiffParser = new TiffParser(in);
    ifd = new IFD();
    ifd.putIFDValue(IFD.IMAGE_WIDTH, 512);
    ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "comment");
}
Also used : IFD(loci.formats.tiff.IFD) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 75 with IFD

use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.

the class BaseTiffReaderTest method setUp.

@BeforeClass
public void setUp() throws Exception {
    ifd = new IFD();
    reader = new BaseTiffReaderMock();
}
Also used : IFD(loci.formats.tiff.IFD) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

IFD (loci.formats.tiff.IFD)121 TiffParser (loci.formats.tiff.TiffParser)74 RandomAccessInputStream (loci.common.RandomAccessInputStream)51 CoreMetadata (loci.formats.CoreMetadata)33 FormatException (loci.formats.FormatException)32 MetadataStore (loci.formats.meta.MetadataStore)21 IFDList (loci.formats.tiff.IFDList)21 PhotoInterp (loci.formats.tiff.PhotoInterp)18 IOException (java.io.IOException)17 Location (loci.common.Location)15 ArrayList (java.util.ArrayList)14 Timestamp (ome.xml.model.primitives.Timestamp)11 Length (ome.units.quantity.Length)10 Time (ome.units.quantity.Time)8 TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)7 TiffRational (loci.formats.tiff.TiffRational)6 File (java.io.File)5 TiffReader (loci.formats.in.TiffReader)5 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5