Search in sources :

Example 96 with FormatException

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

the class FileExportSPW method export.

/**
 * Save a single uint16 plane of data.
 */
public void export() {
    int width = 4, height = 4;
    int pixelType = FormatTools.UINT16;
    Exception exception = null;
    IMetadata omexml = initializeMetadata(width, height, pixelType);
    int series = 0;
    int nSeries = rows * cols * fovPerWell;
    // only save data if the file writer was initialized successfully
    boolean initializationSuccess = initializeWriter(omexml);
    if (initializationSuccess) {
        while (series < nSeries) {
            for (int p = 0; p < sizeT; p++) {
                savePlane(width, height, pixelType, p, series);
            }
            series++;
            if (series < nSeries) {
                try {
                    writer.setSeries(series);
                } catch (FormatException e) {
                    System.err.println("Fatal error unable to select correct image in series! " + e.getMessage());
                    break;
                }
            }
        }
    // endwhile
    }
    // endif
    cleanup();
}
Also used : IMetadata(loci.formats.meta.IMetadata) FormatException(loci.formats.FormatException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException)

Example 97 with FormatException

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

the class FileExportSPW method initializeWriter.

/**
 * Set up the file writer.
 *
 * @param omexml
 *          the IMetadata object that is to be associated with the writer
 * @return true if the file writer was successfully initialized; false if an
 *         error occurred
 */
private boolean initializeWriter(IMetadata omexml) {
    // create the file writer and associate the OME-XML metadata with it
    writer = new ImageWriter();
    writer.setMetadataRetrieve(omexml);
    Exception exception = null;
    try {
        writer.setId(outputFile);
    } catch (FormatException e) {
        exception = e;
    } catch (IOException e) {
        exception = e;
    }
    if (exception != null) {
        System.err.println("Failed to initialize file writer.");
        exception.printStackTrace();
    }
    return exception == null;
}
Also used : ImageWriter(loci.formats.ImageWriter) IOException(java.io.IOException) FormatException(loci.formats.FormatException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException)

Example 98 with FormatException

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

the class FitsReader 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);
    String line = in.readString(LINE_LENGTH);
    if (!line.startsWith("SIMPLE")) {
        throw new FormatException("Unsupported FITS file.");
    }
    String key = "", value = "";
    while (true) {
        line = in.readString(LINE_LENGTH);
        // parse key/value pair
        int ndx = line.indexOf('=');
        int comment = line.indexOf("/", ndx);
        if (comment < 0)
            comment = line.length();
        if (ndx >= 0) {
            key = line.substring(0, ndx).trim();
            value = line.substring(ndx + 1, comment).trim();
        } else
            key = line.trim();
        // image dimensions are only populated by the standard header
        if (key.equals("END") && getSizeX() > 0)
            break;
        if (key.equals("BITPIX")) {
            int bits = Integer.parseInt(value);
            boolean fp = bits < 0;
            boolean signed = bits != 8;
            bits = Math.abs(bits) / 8;
            m.pixelType = FormatTools.pixelTypeFromBytes(bits, signed, fp);
        } else if (key.equals("NAXIS1"))
            m.sizeX = Integer.parseInt(value);
        else if (key.equals("NAXIS2"))
            m.sizeY = Integer.parseInt(value);
        else if (key.equals("NAXIS3"))
            m.sizeZ = Integer.parseInt(value);
        addGlobalMeta(key, value);
    }
    while (in.read() == 0x20) ;
    pixelOffset = in.getFilePointer() - 1;
    m.sizeC = 1;
    m.sizeT = 1;
    if (getSizeZ() == 0)
        m.sizeZ = 1;
    // correct for truncated files
    int planeSize = getSizeX() * getSizeY() * FormatTools.getBytesPerPixel(getPixelType());
    if (DataTools.safeMultiply64(planeSize, getSizeZ()) > (in.length() - pixelOffset)) {
        m.sizeZ = (int) ((in.length() - pixelOffset) / planeSize);
    }
    m.imageCount = m.sizeZ;
    m.rgb = false;
    m.littleEndian = false;
    m.interleaved = false;
    m.dimensionOrder = "XYZCT";
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 99 with FormatException

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

the class GIFReader method readImageBlock.

private void readImageBlock() throws FormatException, IOException {
    ix = in.readShort();
    iy = in.readShort();
    iw = in.readShort();
    ih = in.readShort();
    int packed = in.read();
    boolean lctFlag = (packed & 0x80) != 0;
    interlace = (packed & 0x40) != 0;
    int lctSize = 2 << (packed & 7);
    act = lctFlag ? readLut(lctSize) : gct;
    int save = 0;
    if (transparency) {
        save = act[transIndex];
        act[transIndex] = 0;
    }
    if (act == null)
        throw new FormatException("Color table not found.");
    decodeImageData();
    skipBlocks();
    core.get(0).imageCount++;
    if (transparency)
        act[transIndex] = save;
    lastDispose = dispose;
}
Also used : FormatException(loci.formats.FormatException)

Example 100 with FormatException

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

the class IM3Reader method initFile.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#initFile(java.lang.String)
   */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    core.clear();
    IRandomAccess is = Location.getHandle(id, false);
    try {
        is.setOrder(ByteOrder.LITTLE_ENDIAN);
        final int cookie = is.readInt();
        if (cookie != COOKIE) {
            throw new FormatException(String.format("Expected file cookie of %d, but got %d.", COOKIE, cookie));
        }
        long fileLength = is.length();
        while (is.getFilePointer() < fileLength) {
            final IM3Record rec = parseRecord(is);
            if (rec == null) {
                if (is.getFilePointer() > fileLength - 16)
                    break;
                /*
           * # of bytes in chunk.
           */
                @SuppressWarnings("unused") final int chunkLength = is.readInt();
                /*
           * Is always zero? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown = is.readInt();
                /*
           * Is always one? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown1 = is.readInt();
                /*
           * # of records to follow
           */
                @SuppressWarnings("unused") final int nRecords = is.readInt();
            } else {
                if (rec instanceof ContainerRecord) {
                    final ContainerRecord bRec = (ContainerRecord) rec;
                    for (IM3Record subDS : bRec.parseChunks(is)) {
                        if ((subDS instanceof ContainerRecord) && (subDS.name.equals(FIELD_DATA_SET))) {
                            final ContainerRecord bSubDS = (ContainerRecord) subDS;
                            for (IM3Record subSubDS : bSubDS.parseChunks(is)) {
                                if (subSubDS instanceof ContainerRecord) {
                                    final ContainerRecord bDataSet = (ContainerRecord) subSubDS;
                                    dataSets.add(bDataSet);
                                    List<IM3Record> subRecs = bDataSet.parseChunks(is);
                                    final CoreMetadata cm = new CoreMetadata();
                                    cm.dimensionOrder = DimensionOrder.XYCZT.getValue();
                                    cm.littleEndian = true;
                                    // TODO: Detect pixel type
                                    cm.pixelType = FormatTools.UINT16;
                                    for (IM3Record subRec : subRecs) {
                                        if (subRec.name.equals(FIELD_SHAPE) && (subRec instanceof IntIM3Record)) {
                                            final IntIM3Record iRec = (IntIM3Record) subRec;
                                            cm.sizeX = iRec.getEntry(is, 0);
                                            cm.sizeY = iRec.getEntry(is, 1);
                                            cm.sizeC = iRec.getEntry(is, 2);
                                            cm.sizeZ = 1;
                                            cm.sizeT = 1;
                                            cm.imageCount = cm.sizeC;
                                            cm.metadataComplete = true;
                                        }
                                    }
                                    core.add(cm);
                                }
                            }
                        } else if ((subDS instanceof ContainerRecord) && subDS.name.equals(FIELD_SPECTRAL_LIBRARY)) {
                            /*
                 * SpectralLibrary
                 *   (unnamed container record)
                 *       Spectra
                 *           Keys (integers)
                 *           Values
                 *               (unnamed container record for spectrum #1)
                 *               (unnamed container record for spectrum #2)...
                 *
                 */
                            for (IM3Record slContainer : ((ContainerRecord) subDS).parseChunks(is)) {
                                /* unnamed container */
                                if (slContainer instanceof ContainerRecord) {
                                    for (IM3Record slSpectra : ((ContainerRecord) slContainer).parseChunks(is)) {
                                        if ((slSpectra instanceof ContainerRecord) && (slSpectra.name.equals(FIELD_SPECTRA))) {
                                            for (IM3Record slRec : ((ContainerRecord) slSpectra).parseChunks(is)) {
                                                if (slRec.name.equals(FIELD_VALUES) && (slRec instanceof ContainerRecord)) {
                                                    for (IM3Record spectrumRec : ((ContainerRecord) slRec).parseChunks(is)) {
                                                        if (spectrumRec instanceof ContainerRecord) {
                                                            spectra.add(new Spectrum(is, (ContainerRecord) spectrumRec));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                records.add(rec);
            }
        }
    } finally {
        is.close();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IRandomAccess(loci.common.IRandomAccess) CoreMetadata(loci.formats.CoreMetadata) 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