Search in sources :

Example 11 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class FormatReaderTest method testIsThisType.

@Test(groups = { "all", "fast", "automated" })
public void testIsThisType() {
    String testName = "testIsThisType";
    if (!initFile())
        result(testName, false, "initFile");
    boolean success = true;
    String msg = null;
    try {
        IFormatReader r = reader;
        // unwrap reader
        while (true) {
            if (r instanceof ReaderWrapper) {
                r = ((ReaderWrapper) r).getReader();
            } else if (r instanceof FileStitcher) {
                r = ((FileStitcher) r).getReader();
            } else
                break;
        }
        if (r instanceof ImageReader) {
            ImageReader ir = (ImageReader) r;
            r = ir.getReader();
            IFormatReader[] readers = ir.getReaders();
            String[] used = reader.getUsedFiles();
            for (int i = 0; i < used.length && success; i++) {
                // and only one reader, identifies the dataset as its own
                for (int j = 0; j < readers.length; j++) {
                    boolean result = readers[j].isThisType(used[i]);
                    // TIFF reader is allowed to redundantly green-light files
                    if (result && readers[j] instanceof TiffDelegateReader)
                        continue;
                    // green-light PIC files from NRRD datasets
                    if (result && r instanceof NRRDReader && readers[j] instanceof BioRadReader) {
                        String low = used[i].toLowerCase();
                        boolean isPic = low.endsWith(".pic") || low.endsWith(".pic.gz");
                        if (isPic)
                            continue;
                    }
                    // Analyze reader is allowed to redundantly accept NIfTI files
                    if (result && r instanceof NiftiReader && readers[j] instanceof AnalyzeReader) {
                        continue;
                    }
                    if (result && r instanceof MetamorphReader && readers[j] instanceof MetamorphTiffReader) {
                        continue;
                    }
                    if (result && (readers[j] instanceof L2DReader) || ((r instanceof L2DReader) && (readers[j] instanceof GelReader) || readers[j] instanceof L2DReader)) {
                        continue;
                    }
                    // ND2Reader is allowed to accept JPEG-2000 files
                    if (result && r instanceof JPEG2000Reader && readers[j] instanceof ND2Reader) {
                        continue;
                    }
                    if ((result && r instanceof APLReader && readers[j] instanceof SISReader) || (!result && r instanceof APLReader && readers[j] instanceof APLReader)) {
                        continue;
                    }
                    // reader to pick up TIFFs from a Prairie dataset
                    if (result && r instanceof PrairieReader && readers[j] instanceof OMETiffReader) {
                        continue;
                    }
                    // extra metadata files
                    if (result && r instanceof ColumbusReader && readers[j] instanceof OMETiffReader) {
                        continue;
                    }
                    // with an extra metadata file
                    if (result && r instanceof MicromanagerReader && readers[j] instanceof OMETiffReader) {
                        continue;
                    }
                    if (!result && r instanceof MicromanagerReader && readers[j] instanceof MicromanagerReader && (used[i].toLowerCase().endsWith(".ome.tif") || used[i].toLowerCase().endsWith(".ome.tiff"))) {
                        continue;
                    }
                    if (result && r instanceof TrestleReader && (readers[j] instanceof JPEGReader || readers[j] instanceof PGMReader || readers[j] instanceof TiffDelegateReader)) {
                        continue;
                    }
                    if (result && ((r instanceof HitachiReader) || (readers[j] instanceof HitachiReader && (r instanceof TiffDelegateReader || r instanceof JPEGReader || r instanceof BMPReader)))) {
                        continue;
                    }
                    if (result && r instanceof BDReader && readers[j] instanceof BMPReader) {
                        continue;
                    }
                    if (!result && readers[j] instanceof BDReader && (used[i].endsWith(".bmp") || used[i].endsWith(".adf") || used[i].endsWith(".txt") || used[i].endsWith(".roi"))) {
                        continue;
                    }
                    if (!result && r instanceof VolocityReader && readers[j] instanceof VolocityReader) {
                        continue;
                    }
                    if (!result && r instanceof InCellReader && readers[j] instanceof InCellReader && !used[i].toLowerCase().endsWith(".xdce")) {
                        continue;
                    }
                    if (!result && r instanceof BrukerReader && readers[j] instanceof BrukerReader && !used[i].toLowerCase().equals("acqp") && !used[i].toLowerCase().equals("fid")) {
                        continue;
                    }
                    // Volocity reader is allowed to accept files of other formats
                    if (result && r instanceof VolocityReader) {
                        continue;
                    }
                    if (result && r instanceof NikonReader && readers[j] instanceof DNGReader) {
                        continue;
                    }
                    // DICOM reader is not expected to pick up companion files
                    if (!result && r instanceof DicomReader && readers[j] instanceof DicomReader) {
                        continue;
                    }
                    // AFI reader is not expected to pick up .svs files
                    if (r instanceof AFIReader && (readers[j] instanceof AFIReader || readers[j] instanceof SVSReader)) {
                        continue;
                    }
                    if (!result && readers[j] instanceof MIASReader) {
                        continue;
                    }
                    if ((readers[j] instanceof NDPISReader || r instanceof NDPISReader) && used[i].toLowerCase().endsWith(".ndpi")) {
                        continue;
                    }
                    // Hamamatsu VMS dataset
                    if (readers[j] instanceof JPEGReader && r instanceof HamamatsuVMSReader && used[i].toLowerCase().endsWith(".jpg")) {
                        continue;
                    }
                    // the Hamamatsu VMS reader only picks up its .vms file
                    if (!result && !used[i].toLowerCase().endsWith(".vms") && r instanceof HamamatsuVMSReader) {
                        continue;
                    }
                    // QuickTime reader doesn't pick up resource forks
                    if (!result && i > 0 && r instanceof QTReader) {
                        continue;
                    }
                    if (r instanceof CellVoyagerReader && (!result || readers[j] instanceof OMEXMLReader) && used[i].toLowerCase().endsWith(".ome.xml")) {
                        continue;
                    }
                    // the pattern reader only picks up pattern files
                    if (!used[i].toLowerCase().endsWith(".pattern") && r instanceof FilePatternReader) {
                        continue;
                    }
                    // ignore companion files for Leica LIF
                    if (!used[i].toLowerCase().endsWith(".lif") && r instanceof LIFReader) {
                        continue;
                    }
                    boolean expected = r == readers[j];
                    if (result != expected) {
                        success = false;
                        if (result) {
                            msg = TestTools.shortClassName(readers[j]) + " flagged \"" + used[i] + "\" but so did " + TestTools.shortClassName(r);
                        } else {
                            msg = TestTools.shortClassName(readers[j]) + " skipped \"" + used[i] + "\"";
                        }
                        break;
                    }
                }
            }
        } else {
            success = false;
            msg = "Reader " + r.getClass().getName() + " is not an ImageReader";
        }
    } catch (Throwable t) {
        LOGGER.info("", t);
        success = false;
    }
    result(testName, success, msg);
}
Also used : FileStitcher(loci.formats.FileStitcher) IFormatReader(loci.formats.IFormatReader) ReaderWrapper(loci.formats.ReaderWrapper) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) Test(org.testng.annotations.Test)

Example 12 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class NRRDReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    // make sure we actually have the .nrrd/.nhdr file
    if (!checkSuffix(id, "nhdr") && !checkSuffix(id, "nrrd")) {
        id += ".nhdr";
        if (!new Location(id).exists()) {
            id = id.substring(0, id.lastIndexOf("."));
            id = id.substring(0, id.lastIndexOf("."));
            id += ".nhdr";
        }
        id = new Location(id).getAbsolutePath();
    }
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    ClassList<IFormatReader> classes = ImageReader.getDefaultReaderClasses();
    Class<? extends IFormatReader>[] classArray = classes.getClasses();
    ClassList<IFormatReader> newClasses = new ClassList<IFormatReader>(IFormatReader.class);
    for (Class<? extends IFormatReader> c : classArray) {
        if (!c.equals(NRRDReader.class)) {
            newClasses.addClass(c);
        }
    }
    helper = new ImageReader(newClasses);
    helper.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.MINIMUM));
    String key, v;
    String[] pixelSizeUnits = null;
    int numDimensions = 0;
    CoreMetadata m = core.get(0);
    m.sizeX = 1;
    m.sizeY = 1;
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.dimensionOrder = "XYCZT";
    String line = in.readLine();
    while (line != null && line.length() > 0) {
        if (!line.startsWith("#") && !line.startsWith("NRRD")) {
            // parse key/value pair
            key = line.substring(0, line.indexOf(':')).trim();
            v = line.substring(line.indexOf(':') + 1).trim();
            addGlobalMeta(key, v);
            if (key.equals("type")) {
                if (v.indexOf("char") != -1 || v.indexOf('8') != -1) {
                    m.pixelType = FormatTools.UINT8;
                } else if (v.indexOf("short") != -1 || v.indexOf("16") != -1) {
                    m.pixelType = FormatTools.UINT16;
                } else if (v.equals("int") || v.equals("signed int") || v.equals("int32") || v.equals("int32_t") || v.equals("uint") || v.equals("unsigned int") || v.equals("uint32") || v.equals("uint32_t")) {
                    m.pixelType = FormatTools.UINT32;
                } else if (v.equals("float"))
                    m.pixelType = FormatTools.FLOAT;
                else if (v.equals("double"))
                    m.pixelType = FormatTools.DOUBLE;
                else
                    throw new FormatException("Unsupported data type: " + v);
            } else if (key.equals("dimension")) {
                numDimensions = Integer.parseInt(v);
            } else if (key.equals("sizes")) {
                String[] tokens = v.split(" ");
                for (int i = 0; i < numDimensions; i++) {
                    int size = Integer.parseInt(tokens[i]);
                    if (numDimensions >= 3 && i == 0 && size > 1 && size <= 16) {
                        m.sizeC = size;
                    } else if (i == 0 || (getSizeC() > 1 && i == 1)) {
                        m.sizeX = size;
                    } else if (i == 1 || (getSizeC() > 1 && i == 2)) {
                        m.sizeY = size;
                    } else if (i == 2 || (getSizeC() > 1 && i == 3)) {
                        m.sizeZ = size;
                    } else if (i == 3 || (getSizeC() > 1 && i == 4)) {
                        m.sizeT = size;
                    }
                }
            } else if (key.equals("data file") || key.equals("datafile")) {
                dataFile = v;
            } else if (key.equals("encoding"))
                encoding = v;
            else if (key.equals("endian")) {
                m.littleEndian = v.equals("little");
            } else if (key.equals("spacings") || key.equals("space directions")) {
                pixelSizes = v.split(" ");
            } else if (key.equals("space units")) {
                pixelSizeUnits = v.split(" ");
            } else if (key.equals("byte skip")) {
                offset = Long.parseLong(v);
            }
        }
        line = in.readLine();
        if (line != null)
            line = line.trim();
    }
    if (dataFile == null)
        offset = in.getFilePointer();
    else {
        Location f = new Location(currentId).getAbsoluteFile();
        Location parent = f.getParentFile();
        if (f.exists() && parent != null) {
            dataFile = dataFile.substring(dataFile.indexOf(File.separator) + 1);
            dataFile = new Location(parent, dataFile).getAbsolutePath();
        }
        initializeHelper = !encoding.equals("raw");
    }
    m.rgb = getSizeC() > 1;
    m.interleaved = true;
    m.imageCount = getSizeZ() * getSizeT();
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        if (pixelSizes != null) {
            for (int i = 0; i < pixelSizes.length; i++) {
                if (pixelSizes[i] == null)
                    continue;
                try {
                    Double d = parsePixelSize(i);
                    String unit = pixelSizeUnits == null || i >= pixelSizeUnits.length ? null : pixelSizeUnits[i].replaceAll("\"", "");
                    if (i == 0) {
                        Length x = FormatTools.getPhysicalSizeX(d, unit);
                        if (x != null) {
                            store.setPixelsPhysicalSizeX(x, 0);
                        }
                    } else if (i == 1) {
                        Length y = FormatTools.getPhysicalSizeY(d, unit);
                        if (y != null) {
                            store.setPixelsPhysicalSizeY(y, 0);
                        }
                    } else if (i == 2) {
                        Length z = FormatTools.getPhysicalSizeZ(d, unit);
                        if (z != null) {
                            store.setPixelsPhysicalSizeZ(z, 0);
                        }
                    }
                } catch (NumberFormatException e) {
                }
            }
        }
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) ClassList(loci.formats.ClassList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream) ImageReader(loci.formats.ImageReader) Location(loci.common.Location)

Example 13 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class BioFormatsExtensionPrinter method main.

public static void main(String[] args) throws IOException {
    System.out.println("Generating list of Bio-Formats supported suffixes...");
    try (IFormatReader reader = new ImageReader()) {
        String[] suffixes = reader.getSuffixes();
        PrintWriter fo = null;
        fo = new PrintWriter(new File("BioFormatsSuffixes.txt"), Constants.ENCODING);
        for (String s : suffixes) fo.println("*." + s);
        fo.close();
        System.out.println(suffixes.length + " suffixes discovered.");
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) ImageReader(loci.formats.ImageReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 14 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class PrintFormatTable method printSupportedFormats.

public static void printSupportedFormats(String[] args) {
    PrintStyles printStyle = PrintStyles.TXT;
    boolean usage = false;
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-help"))
                usage = true;
            else if (args[i].equals("-xml"))
                printStyle = PrintStyles.XML;
            else if (args[i].equals("-html"))
                printStyle = PrintStyles.HTML;
            else if (args[i].equals("-txt"))
                printStyle = PrintStyles.TXT;
            else {
                LOGGER.warn("unknown flag: {}; try -help for options", args[i]);
            }
        }
    }
    if (usage) {
        LOGGER.info("Usage: formatlist [-html] [-txt] [-xml]");
        LOGGER.info("  -html: show formats in an HTML table");
        LOGGER.info("   -txt: show formats in plaintext (default)");
        LOGGER.info("   -xml: show formats as XML data");
        return;
    }
    LOGGER.info(getHeader(printStyle));
    // retrieve all of the file format readers and writers
    ImageReader baseReader = new ImageReader();
    IFormatReader[] readers = baseReader.getReaders();
    ImageWriter baseWriter = new ImageWriter();
    IFormatWriter[] writers = baseWriter.getWriters();
    for (int i = 0; i < readers.length; i++) {
        String readerFormatName = readers[i].getFormat();
        boolean read = true;
        boolean write = false;
        boolean wmp = false;
        // check if there is a corresponding writer
        IFormatWriter writer = null;
        for (int j = 0; j < writers.length; j++) {
            if (writers[j].getFormat().equals(readerFormatName)) {
                writer = writers[j];
            }
        }
        if (writer != null) {
            write = true;
            // written to a single file
            if (writer.canDoStacks())
                wmp = true;
        }
        String[] extensions = readers[i].getSuffixes();
        StringBuilder ext = new StringBuilder();
        for (int j = 0; j < extensions.length; j++) {
            ext.append(extensions[j]);
            if (j < extensions.length - 1) {
                ext.append(", ");
            }
        }
        // display information about the format
        LOGGER.info(getFormatLine(printStyle, readerFormatName, read, write, wmp, ext.toString()));
    }
    LOGGER.info(getFooter(printStyle));
}
Also used : IFormatWriter(loci.formats.IFormatWriter) IFormatReader(loci.formats.IFormatReader) ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader)

Example 15 with IFormatReader

use of loci.formats.IFormatReader in project bioformats by openmicroscopy.

the class ImageConverterTest method checkImage.

public void checkImage() throws FormatException, IOException {
    IFormatReader r = new ImageReader();
    r.setId(outFile.getAbsolutePath());
    assertEquals(r.getSizeX(), 512);
    r.close();
}
Also used : IFormatReader(loci.formats.IFormatReader) ImageReader(loci.formats.ImageReader)

Aggregations

IFormatReader (loci.formats.IFormatReader)38 ImageReader (loci.formats.ImageReader)20 FormatException (loci.formats.FormatException)15 IOException (java.io.IOException)10 FileStitcher (loci.formats.FileStitcher)10 Test (org.testng.annotations.Test)8 Location (loci.common.Location)6 ChannelSeparator (loci.formats.ChannelSeparator)6 ServiceException (loci.common.services.ServiceException)5 ServiceFactory (loci.common.services.ServiceFactory)5 ReaderWrapper (loci.formats.ReaderWrapper)5 IMetadata (loci.formats.meta.IMetadata)5 RandomAccessInputStream (loci.common.RandomAccessInputStream)4 ChannelFiller (loci.formats.ChannelFiller)4 CoreMetadata (loci.formats.CoreMetadata)4 DimensionSwapper (loci.formats.DimensionSwapper)4 MinMaxCalculator (loci.formats.MinMaxCalculator)4 BufferedImageReader (loci.formats.gui.BufferedImageReader)4 OMEXMLService (loci.formats.services.OMEXMLService)4 DependencyException (loci.common.services.DependencyException)3