Search in sources :

Example 1 with ClassList

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

the class HitachiReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    if (!checkSuffix(id, "txt")) {
        String base = id;
        if (base.indexOf('.') >= 0) {
            base = base.substring(0, base.lastIndexOf("."));
        }
        id = base + ".txt";
        initFile(id);
        return;
    }
    super.initFile(id);
    String data = DataTools.readFile(id);
    IniParser parser = new IniParser();
    parser.setBackslashContinuesLine(false);
    IniList ini = parser.parseINI(new BufferedReader(new StringReader(data)));
    IniTable image = ini.getTable("SemImageFile");
    if (image == null) {
        throw new FormatException("Could not find 'SemImageFile' table.");
    }
    for (String key : image.keySet()) {
        addGlobalMeta(key, image.get(key));
    }
    String imageName = image.get("SampleName");
    String pixelsFile = image.get("ImageName");
    String date = image.get("Date");
    String time = image.get("Time");
    Location parent = new Location(id).getAbsoluteFile().getParentFile();
    pixelsFile = new Location(parent, pixelsFile).getAbsolutePath();
    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(HitachiReader.class)) {
            newClasses.addClass(c);
        }
    }
    helperReader = new ImageReader(newClasses);
    helperReader.setId(pixelsFile);
    core = new ArrayList<CoreMetadata>(helperReader.getCoreMetadataList());
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM);
    store.setImageName(imageName, 0);
    date = DateTools.formatDate(date + " " + time, DATE_FORMAT);
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    populateOMEMetadata(image, store);
}
Also used : IniParser(loci.common.IniParser) IFormatReader(loci.formats.IFormatReader) IniList(loci.common.IniList) ClassList(loci.formats.ClassList) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) IniTable(loci.common.IniTable) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ImageReader(loci.formats.ImageReader) Location(loci.common.Location)

Example 2 with ClassList

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

the class LociPrefs method makeImageReader.

// -- Utility methods --
/**
 * Creates an image reader according to the current configuration settings,
 * including which format readers are currently enabled, as well as
 * format-specific configuration settings.
 */
public static ImageReader makeImageReader() {
    ClassList<IFormatReader> defaultClasses = ImageReader.getDefaultReaderClasses();
    Class<? extends IFormatReader>[] c = defaultClasses.getClasses();
    // include only enabled classes
    ClassList<IFormatReader> enabledClasses = new ClassList<IFormatReader>(IFormatReader.class);
    for (int i = 0; i < c.length; i++) {
        boolean on = LociPrefs.isReaderEnabled(c[i]);
        if (on)
            enabledClasses.addClass(c[i]);
    }
    ImageReader reader = new ImageReader(enabledClasses);
    MetadataOptions options = reader.getMetadataOptions();
    if (options instanceof DynamicMetadataOptions) {
        ((DynamicMetadataOptions) options).setBoolean(ZeissCZIReader.ALLOW_AUTOSTITCHING_KEY, allowCZIAutostitch());
        ((DynamicMetadataOptions) options).setBoolean(ZeissCZIReader.INCLUDE_ATTACHMENTS_KEY, includeCZIAttachments());
        ((DynamicMetadataOptions) options).setBoolean(NativeND2Reader.USE_CHUNKMAP_KEY, useND2Chunkmap());
        ((DynamicMetadataOptions) options).setBoolean(LIFReader.OLD_PHYSICAL_SIZE_KEY, isLeicaLIFPhysicalSizeBackwardsCompatible());
        ((DynamicMetadataOptions) options).setBoolean(CellSensReader.FAIL_ON_MISSING_KEY, isCellsensFailOnMissing());
        reader.setMetadataOptions(options);
    }
    // toggle reader-specific options
    boolean nd2Nikon = LociPrefs.isND2Nikon();
    boolean pictQTJava = LociPrefs.isPictQTJava();
    boolean qtQTJava = LociPrefs.isQTQTJava();
    boolean sdtIntensity = LociPrefs.isSDTIntensity();
    boolean tiffImageIO = LociPrefs.isTiffImageIO();
    IFormatReader[] r = reader.getReaders();
    for (int i = 0; i < r.length; i++) {
        if (r[i] instanceof ND2Reader) {
            ND2Reader nd2 = (ND2Reader) r[i];
            nd2.setLegacy(nd2Nikon);
        } else if (r[i] instanceof PictReader) {
            PictReader pict = (PictReader) r[i];
            pict.setLegacy(pictQTJava);
        } else if (r[i] instanceof QTReader) {
            QTReader qt = (QTReader) r[i];
            qt.setLegacy(qtQTJava);
        } else if (r[i] instanceof SDTReader) {
            SDTReader sdt = (SDTReader) r[i];
            sdt.setIntensity(sdtIntensity);
        } else if (r[i] instanceof TiffDelegateReader) {
            TiffDelegateReader tiff = (TiffDelegateReader) r[i];
            tiff.setLegacy(tiffImageIO);
        }
    }
    return reader;
}
Also used : IFormatReader(loci.formats.IFormatReader) TiffDelegateReader(loci.formats.in.TiffDelegateReader) MetadataOptions(loci.formats.in.MetadataOptions) DynamicMetadataOptions(loci.formats.in.DynamicMetadataOptions) ClassList(loci.formats.ClassList) QTReader(loci.formats.in.QTReader) SDTReader(loci.formats.in.SDTReader) NativeND2Reader(loci.formats.in.NativeND2Reader) ND2Reader(loci.formats.in.ND2Reader) DynamicMetadataOptions(loci.formats.in.DynamicMetadataOptions) PictReader(loci.formats.in.PictReader) ImageReader(loci.formats.ImageReader)

Example 3 with ClassList

use of loci.formats.ClassList 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 4 with ClassList

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

the class ImportProcess method createBaseReader.

/**
 * Initializes an {@link loci.formats.IFormatReader}
 * according to the current configuration.
 */
private void createBaseReader() throws FormatException, IOException {
    if (options.isLocal() || options.isHTTP()) {
        BF.status(options.isQuiet(), "Identifying " + idName);
        imageReader = LociPrefs.makeImageReader();
        baseReader = imageReader.getReader(options.isUsingPatternIds() ? new FilePattern(options.getId()).getFiles()[0] : options.getId());
    } else if (options.isOMERO()) {
        BF.status(options.isQuiet(), "Establishing server connection");
        try {
            ReflectedUniverse r = new ReflectedUniverse();
            r.exec("import loci.ome.io.OmeroReader");
            r.exec("baseReader = new OmeroReader()");
            ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class);
            r.setVar("classes", classes);
            r.exec("class = baseReader.getClass()");
            r.exec("classes.addClass(class)");
            imageReader = new ImageReader(classes);
            baseReader = imageReader.getReader(options.getId());
        } catch (Exception exc) {
            WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem communicating with the server.");
            cancel();
            return;
        }
    } else {
        WindowTools.reportException(null, options.isQuiet(), "Sorry, there has been an internal error: unknown data source");
        cancel();
        return;
    }
    // attach OME-XML metadata store
    Exception exc = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        omeXML = null;
    } catch (DependencyException de) {
        exc = de;
    } catch (ServiceException se) {
        exc = se;
    }
    if (exc != null) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem constructing the OME-XML metadata store");
        throw new FormatException(exc);
    }
    baseReader.setMetadataStore(meta);
    BF.status(options.isQuiet(), "");
    DebugTools.enableIJLogging(IJ.debugMode);
}
Also used : IFormatReader(loci.formats.IFormatReader) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FilePattern(loci.formats.FilePattern) ReflectedUniverse(loci.common.ReflectedUniverse) ClassList(loci.formats.ClassList) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Aggregations

ClassList (loci.formats.ClassList)4 IFormatReader (loci.formats.IFormatReader)4 ImageReader (loci.formats.ImageReader)4 FormatException (loci.formats.FormatException)3 Location (loci.common.Location)2 CoreMetadata (loci.formats.CoreMetadata)2 MetadataStore (loci.formats.meta.MetadataStore)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 IniList (loci.common.IniList)1 IniParser (loci.common.IniParser)1 IniTable (loci.common.IniTable)1 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 ReflectedUniverse (loci.common.ReflectedUniverse)1 DependencyException (loci.common.services.DependencyException)1 ServiceException (loci.common.services.ServiceException)1 ServiceFactory (loci.common.services.ServiceFactory)1 FilePattern (loci.formats.FilePattern)1 DynamicMetadataOptions (loci.formats.in.DynamicMetadataOptions)1