Search in sources :

Example 41 with FormatException

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

the class AWTImageTools method openImage.

/**
 * Creates an image from the given byte array, using the given
 * IFormatReader to retrieve additional information.
 * The floating point normalization setting is specified by 'normal',
 * which allows the reader's normalization setting to be overridden.
 */
public static BufferedImage openImage(byte[] buf, IFormatReader r, int w, int h, boolean normal) throws FormatException, IOException {
    int pixelType = r.getPixelType();
    boolean little = r.isLittleEndian();
    int rgbChanCount = r.getRGBChannelCount();
    boolean interleaved = r.isInterleaved();
    boolean indexed = r.isIndexed();
    if (pixelType == FormatTools.FLOAT) {
        float[] f = (float[]) DataTools.makeDataArray(buf, 4, true, little);
        if (normal)
            f = DataTools.normalizeFloats(f);
        return makeImage(f, w, h, rgbChanCount, interleaved);
    } else if (pixelType == FormatTools.DOUBLE) {
        double[] d = (double[]) DataTools.makeDataArray(buf, 8, true, little);
        if (normal)
            d = DataTools.normalizeDoubles(d);
        return makeImage(d, w, h, rgbChanCount, interleaved);
    }
    boolean signed = FormatTools.isSigned(pixelType);
    ColorModel model = null;
    if (signed) {
        if (pixelType == FormatTools.INT8) {
            model = new SignedColorModel(8, DataBuffer.TYPE_BYTE, rgbChanCount);
        } else if (pixelType == FormatTools.INT16) {
            model = new SignedColorModel(16, DataBuffer.TYPE_SHORT, rgbChanCount);
        } else if (pixelType == FormatTools.INT32) {
            model = new SignedColorModel(32, DataBuffer.TYPE_INT, rgbChanCount);
        }
    }
    int bpp = FormatTools.getBytesPerPixel(pixelType);
    BufferedImage b = makeImage(buf, w, h, rgbChanCount, interleaved, bpp, false, little, signed);
    if (b == null) {
        throw new FormatException("Could not construct BufferedImage");
    }
    if (indexed && rgbChanCount == 1) {
        if (pixelType == FormatTools.UINT8 || pixelType == FormatTools.INT8) {
            byte[][] table = r.get8BitLookupTable();
            if (table != null && table.length > 0 && table[0] != null) {
                int len = table[0].length;
                byte[] dummy = table.length < 3 ? new byte[len] : null;
                byte[] red = table.length >= 1 ? table[0] : dummy;
                byte[] green = table.length >= 2 ? table[1] : dummy;
                byte[] blue = table.length >= 3 ? table[2] : dummy;
                model = new IndexColorModel(8, len, red, green, blue);
            }
        } else if (pixelType == FormatTools.UINT16 || pixelType == FormatTools.INT16) {
            short[][] table = r.get16BitLookupTable();
            if (table != null && table.length > 0 && table[0] != null) {
                model = new Index16ColorModel(16, table[0].length, table, r.isLittleEndian());
            }
        }
    }
    if (model != null) {
        WritableRaster raster = Raster.createWritableRaster(b.getSampleModel(), b.getRaster().getDataBuffer(), null);
        b = new BufferedImage(model, raster, false, null);
    }
    return b;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage) FormatException(loci.formats.FormatException) IndexColorModel(java.awt.image.IndexColorModel)

Example 42 with FormatException

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

the class ImageViewer method open.

/**
 * Opens the given data source using the current format reader.
 */
public void open(String id) {
    wait(true);
    try {
        // Location f = new Location(id);
        // id = f.getAbsolutePath();
        IMetadata meta = null;
        if (omexmlService != null) {
            try {
                meta = omexmlService.createOMEXMLMetadata();
                myReader.setMetadataStore(meta);
            } catch (ServiceException exc) {
                LOGGER.debug("Could not create OME-XML metadata", exc);
            }
        }
        if (meta == null) {
            LOGGER.info("OME metadata unavailable");
        }
        myReader.setId(id);
        int num = myReader.getImageCount();
        ProgressMonitor progress = new ProgressMonitor(this, "Reading " + id, null, 0, num + 1);
        sizeZ = myReader.getSizeZ();
        sizeT = myReader.getSizeT();
        sizeC = myReader.getEffectiveSizeC();
        // if (myReader.isRGB(id)) sizeC = (sizeC + 2) / 3; // adjust for RGB
        progress.setProgress(1);
        BufferedImage[] img = new BufferedImage[num];
        for (int i = 0; i < num; i++) {
            if (progress.isCanceled())
                break;
            img[i] = myReader.openImage(i);
            if (i == 0)
                setImages(myReader, img);
            progress.setProgress(i + 2);
        }
        myReader.close(true);
    } catch (FormatException exc) {
        LOGGER.info("", exc);
        wait(false);
        return;
    } catch (IOException exc) {
        LOGGER.info("", exc);
        wait(false);
        return;
    }
    wait(false);
}
Also used : ProgressMonitor(javax.swing.ProgressMonitor) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) FormatException(loci.formats.FormatException)

Example 43 with FormatException

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

the class EPSReader 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);
    CoreMetadata m = core.get(0);
    LOGGER.info("Verifying EPS format");
    String line = readLine();
    if (!line.trim().startsWith("%!PS")) {
        // read the TIFF preview
        isTiff = true;
        in.order(true);
        in.seek(20);
        int offset = in.readInt();
        int len = in.readInt();
        byte[] b = new byte[len];
        in.seek(offset);
        in.read(b);
        in = new RandomAccessInputStream(b);
        TiffParser tp = new TiffParser(in);
        ifds = tp.getIFDs();
        IFD firstIFD = ifds.get(0);
        map = tp.getColorMap(firstIFD);
        m.sizeX = (int) firstIFD.getImageWidth();
        m.sizeY = (int) firstIFD.getImageLength();
        m.sizeZ = 1;
        m.sizeT = 1;
        m.sizeC = firstIFD.getSamplesPerPixel();
        if (map != null && getSizeC() == 1) {
            m.sizeC = 3;
        }
        if (getSizeC() == 2)
            m.sizeC = 4;
        m.littleEndian = firstIFD.isLittleEndian();
        m.interleaved = true;
        m.rgb = getSizeC() > 1;
        m.pixelType = firstIFD.getPixelType();
        m.imageCount = 1;
        m.dimensionOrder = "XYCZT";
        m.metadataComplete = true;
        m.indexed = false;
        m.falseColor = false;
        MetadataStore store = makeFilterMetadata();
        MetadataTools.populatePixels(store, this);
        return;
    }
    LOGGER.info("Finding image data");
    binary = false;
    String image = "image";
    int lineNum = 1;
    line = readLine().trim();
    while (line != null && !line.equals("%%EOF")) {
        if (line.endsWith(image)) {
            if (!line.startsWith(image)) {
                if (line.indexOf("colorimage") != -1)
                    m.sizeC = 3;
                String[] t = line.split(" ");
                try {
                    int newX = Integer.parseInt(t[0]);
                    int newY = Integer.parseInt(t[1]);
                    if (t.length > 2 && Integer.parseInt(t[2]) >= 8) {
                        m.sizeX = newX;
                        m.sizeY = newY;
                        start = lineNum;
                    }
                } catch (NumberFormatException exc) {
                    LOGGER.debug("Could not parse image dimensions", exc);
                    if (t.length > 3) {
                        m.sizeC = Integer.parseInt(t[3]);
                    }
                }
            }
            break;
        } else if (line.startsWith("%%")) {
            if (line.startsWith("%%BoundingBox:")) {
                line = line.substring(14).trim();
                String[] t = line.split(" ");
                try {
                    int originX = Integer.parseInt(t[0].trim());
                    int originY = Integer.parseInt(t[1].trim());
                    m.sizeX = Integer.parseInt(t[2].trim()) - originX;
                    m.sizeY = Integer.parseInt(t[3].trim()) - originY;
                    addGlobalMeta("X-coordinate of origin", originX);
                    addGlobalMeta("Y-coordinate of origin", originY);
                } catch (NumberFormatException e) {
                    throw new FormatException("Files without image data are not supported.");
                }
            } else if (line.startsWith("%%BeginBinary")) {
                binary = true;
            } else {
                // parse key/value pairs
                int ndx = line.indexOf(':');
                if (ndx != -1) {
                    String key = line.substring(0, ndx);
                    String value = line.substring(ndx + 1);
                    addGlobalMeta(key, value);
                }
            }
        } else if (line.startsWith("%ImageData:")) {
            line = line.substring(11);
            String[] t = line.split(" ");
            m.sizeX = Integer.parseInt(t[0]);
            m.sizeY = Integer.parseInt(t[1]);
            m.sizeC = Integer.parseInt(t[3]);
            for (int i = 4; i < t.length; i++) {
                image = t[i].trim();
                if (image.length() > 1) {
                    image = image.substring(1, image.length() - 1);
                }
            }
        }
        lineNum++;
        line = readLine().trim();
    }
    LOGGER.info("Populating metadata");
    if (getSizeC() == 0)
        m.sizeC = 1;
    m.sizeZ = 1;
    m.sizeT = 1;
    m.dimensionOrder = "XYCZT";
    m.pixelType = FormatTools.UINT8;
    m.rgb = getSizeC() == 3;
    m.interleaved = true;
    m.littleEndian = true;
    m.imageCount = 1;
    // Populate metadata store
    // The metadata store we're working with.
    MetadataStore store = getMetadataStore();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 44 with FormatException

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

the class WlzReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        wlz = factory.getInstance(WlzService.class);
    } catch (DependencyException e) {
        throw new FormatException(NO_WLZ_MSG, e);
    }
    if (wlz != null) {
        wlz.open(id, "r");
        CoreMetadata md = core.get(0);
        MetadataStore store = makeFilterMetadata();
        md.rgb = wlz.isRGB();
        md.interleaved = false;
        md.indexed = false;
        md.sizeX = wlz.getSizeX();
        md.sizeY = wlz.getSizeY();
        md.sizeZ = wlz.getSizeZ();
        md.sizeC = wlz.getSizeC();
        md.sizeT = wlz.getSizeT();
        md.dimensionOrder = "XYZCT";
        md.imageCount = wlz.getSizeZ();
        md.pixelType = wlz.getPixelType();
        PositiveFloat x = new PositiveFloat(Math.abs(wlz.getVoxSzX()));
        PositiveFloat y = new PositiveFloat(Math.abs(wlz.getVoxSzY()));
        PositiveFloat z = new PositiveFloat(Math.abs(wlz.getVoxSzZ()));
        store.setPixelsPhysicalSizeX(FormatTools.createLength(x, UNITS.MICROMETER), 0);
        store.setPixelsPhysicalSizeY(FormatTools.createLength(y, UNITS.MICROMETER), 0);
        store.setPixelsPhysicalSizeZ(FormatTools.createLength(z, UNITS.MICROMETER), 0);
        store.setStageLabelName(wlz.getWlzOrgLabelName(), 0);
        store.setStageLabelX(new Length(wlz.getOrgX(), UNITS.REFERENCEFRAME), 0);
        store.setStageLabelY(new Length(wlz.getOrgY(), UNITS.REFERENCEFRAME), 0);
        store.setStageLabelZ(new Length(wlz.getOrgZ(), UNITS.REFERENCEFRAME), 0);
        MetadataTools.populatePixels(store, this);
    }
}
Also used : WlzService(loci.formats.services.WlzService) MetadataStore(loci.formats.meta.MetadataStore) ServiceFactory(loci.common.services.ServiceFactory) Length(ome.units.quantity.Length) PositiveFloat(ome.xml.model.primitives.PositiveFloat) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 45 with FormatException

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

the class WlzReader 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);
    if (wlz != null) {
        buf = wlz.readBytes(no, buf, x, y, w, h);
    } else {
        try {
            ServiceFactory factory = new ServiceFactory();
            wlz = factory.getInstance(WlzService.class);
        } catch (DependencyException e) {
            throw new FormatException(NO_WLZ_MSG, e);
        }
        if (wlz != null) {
            wlz.open(currentId, "r");
            buf = wlz.readBytes(no, buf, x, y, w, h);
        }
    }
    return buf;
}
Also used : WlzService(loci.formats.services.WlzService) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException)

Aggregations

FormatException (loci.formats.FormatException)246 IOException (java.io.IOException)91 CoreMetadata (loci.formats.CoreMetadata)86 RandomAccessInputStream (loci.common.RandomAccessInputStream)73 MetadataStore (loci.formats.meta.MetadataStore)66 Location (loci.common.Location)48 DependencyException (loci.common.services.DependencyException)44 ServiceException (loci.common.services.ServiceException)43 Length (ome.units.quantity.Length)39 ServiceFactory (loci.common.services.ServiceFactory)35 ArrayList (java.util.ArrayList)33 IFD (loci.formats.tiff.IFD)32 TiffParser (loci.formats.tiff.TiffParser)25 OMEXMLService (loci.formats.services.OMEXMLService)23 Time (ome.units.quantity.Time)23 Timestamp (ome.xml.model.primitives.Timestamp)23 ImagePlus (ij.ImagePlus)21 ImageReader (loci.formats.ImageReader)20 IMetadata (loci.formats.meta.IMetadata)18 IFormatReader (loci.formats.IFormatReader)14