Search in sources :

Example 16 with ChannelSeparator

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

the class OpenlabReader 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);
    lastPlane = no;
    PlaneInfo planeInfo = null;
    if (specialPlateNames) {
        planeInfo = getPlane(getZCTCoords(no));
    } else if (no < planeOffsets[getSeries()].length) {
        int index = planeOffsets[getSeries()][no];
        planeInfo = planes[index];
    }
    if (planeInfo == null)
        return buf;
    long first = planeInfo.planeOffset;
    long last = first + FormatTools.getPlaneSize(this) * 2;
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (!planeInfo.pict) {
        if (version == 2) {
            in.seek(first);
            readPlane(in, x, y, w, h, buf);
        } else {
            in.seek(first + 16);
            int bytes = bpp * getRGBChannelCount();
            byte[] b = new byte[(int) (last - first)];
            in.read(b);
            CodecOptions options = new CodecOptions();
            options.width = getSizeX();
            options.height = getSizeY();
            options.bitsPerSample = bytes * 8;
            options.maxBytes = getSizeX() * getSizeY() * bytes;
            b = new LZOCodec().decompress(b, options);
            if (getSizeX() * getSizeY() * 4 <= b.length) {
                for (int yy = y; yy < h + y; yy++) {
                    for (int xx = x; xx < w + x; xx++) {
                        System.arraycopy(b, (yy * (getSizeX() + 4) + xx) * 4 + 1, buf, ((yy - y) * w + xx - x) * 3, 3);
                    }
                }
            } else {
                int src = b.length / getSizeY();
                if (src - (getSizeX() * bytes) != 16)
                    src = getSizeX() * bytes;
                int dest = w * bytes;
                for (int row = 0; row < h; row++) {
                    System.arraycopy(b, (row + y) * src + x * bytes, buf, row * dest, dest);
                }
            }
            b = null;
        }
        if (planeInfo.volumeType == MAC_256_GREYS || planeInfo.volumeType == MAC_256_COLORS) {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (~buf[i] & 0xff);
            }
        }
    } else {
        // PICT plane
        Exception exc = null;
        if (getPixelType() == FormatTools.UINT8) {
            in.seek(first);
            byte[] b = new byte[(int) (last - first) + 512];
            in.read(b, 512, b.length - 512);
            IFormatReader r = getRGBChannelCount() == 1 ? new ChannelSeparator(pict) : pict;
            try {
                Location.mapFile("OPENLAB_PICT", new ByteArrayHandle(b));
                r.setId("OPENLAB_PICT");
                if (getPixelType() != r.getPixelType()) {
                    throw new FormatException("Pixel type of inner PICT does not " + "match pixel type of Openlab file");
                }
                if (isIndexed()) {
                    int index = no;
                    if (getSeries() < planeOffsets.length) {
                        index = planeOffsets[getSeries()][lastPlane];
                    }
                    luts.set(index, pict.get8BitLookupTable());
                }
                r.openBytes(0, buf, x, y, w, h);
            } catch (FormatException e) {
                exc = e;
            } catch (IOException e) {
                exc = e;
            } finally {
                r.close();
                // remove file from map
                Location.mapFile("OPENLAB_PICT", null);
            }
            b = null;
        }
        if (exc != null || getPixelType() != FormatTools.UINT8) {
            LOGGER.debug("", exc);
            in.seek(planeInfo.planeOffset - 298);
            if (in.readByte() == 1)
                in.skipBytes(297);
            else
                in.skipBytes(169);
            int size = 0, expectedBlock = 0, totalBlocks = -1, pixPos = 0;
            byte[] plane = new byte[FormatTools.getPlaneSize(this)];
            while (expectedBlock != totalBlocks && pixPos < plane.length && in.getFilePointer() + 32 < last) {
                findNextBlock();
                if (in.getFilePointer() + 4 >= in.length())
                    break;
                int num = in.readInt();
                if (num != expectedBlock) {
                    throw new FormatException("Expected iPic block not found");
                }
                expectedBlock++;
                if (totalBlocks == -1) {
                    totalBlocks = in.readInt();
                    in.skipBytes(8);
                } else
                    in.skipBytes(12);
                size = in.readInt();
                in.skipBytes(4);
                if (size + pixPos > plane.length)
                    size = plane.length - pixPos;
                in.read(plane, pixPos, size);
                pixPos += size;
            }
            RandomAccessInputStream pix = new RandomAccessInputStream(plane);
            readPlane(pix, x, y, w, h, buf);
            pix.close();
            plane = null;
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) LZOCodec(loci.formats.codec.LZOCodec) IOException(java.io.IOException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 17 with ChannelSeparator

use of loci.formats.ChannelSeparator in project TrakEM2 by trakem2.

the class Loader method getDimensions.

/**
 * Read out the width,height of an image using LOCI BioFormats.
 */
public static Dimension getDimensions(final String path) {
    IFormatReader fr = null;
    try {
        fr = new ChannelSeparator();
        fr.setGroupFiles(false);
        fr.setId(path);
        return new Dimension(fr.getSizeX(), fr.getSizeY());
    } catch (final FormatException fe) {
        Utils.log("Error in reading image file at " + path + "\n" + fe);
    } catch (final Exception e) {
        IJError.print(e);
    } finally {
        if (null != fr)
            try {
                fr.close();
            } catch (final IOException ioe) {
                Utils.log2("Could not close IFormatReader: " + ioe);
            }
    }
    return null;
}
Also used : IFormatReader(loci.formats.IFormatReader) Dimension(java.awt.Dimension) IOException(java.io.IOException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Aggregations

ChannelSeparator (loci.formats.ChannelSeparator)17 ChannelFiller (loci.formats.ChannelFiller)11 MinMaxCalculator (loci.formats.MinMaxCalculator)11 ImageReader (loci.formats.ImageReader)10 FormatException (loci.formats.FormatException)8 IOException (java.io.IOException)6 IFormatReader (loci.formats.IFormatReader)6 ChannelMerger (loci.formats.ChannelMerger)5 DimensionSwapper (loci.formats.DimensionSwapper)5 Test (org.testng.annotations.Test)5 Location (loci.common.Location)4 FileStitcher (loci.formats.FileStitcher)4 OMEXMLMetadataImpl (loci.formats.ome.OMEXMLMetadataImpl)4 MetadataStore (loci.formats.meta.MetadataStore)3 DataProvider (org.testng.annotations.DataProvider)3 RandomAccessInputStream (loci.common.RandomAccessInputStream)2 DependencyException (loci.common.services.DependencyException)2 ServiceException (loci.common.services.ServiceException)2 ServiceFactory (loci.common.services.ServiceFactory)2 CoreMetadata (loci.formats.CoreMetadata)2