Search in sources :

Example 16 with ByteArrayHandle

use of loci.common.ByteArrayHandle in project bioformats by openmicroscopy.

the class CellSensReader method decodeTile.

private byte[] decodeTile(int no, int row, int col) throws FormatException, IOException {
    if (tileMap.get(getCoreIndex()) == null) {
        return new byte[getTileSize()];
    }
    int[] zct = getZCTCoords(no);
    TileCoordinate t = new TileCoordinate(nDimensions.get(getCoreIndex()));
    t.coordinate[0] = col;
    t.coordinate[1] = row;
    int resIndex = getResolution();
    int pyramidIndex = getSeries();
    if (hasFlattenedResolutions()) {
        int index = 0;
        pyramidIndex = 0;
        for (int i = 0; i < core.size(); ) {
            if (index + core.get(i).resolutionCount <= getSeries()) {
                index += core.get(i).resolutionCount;
                i += core.get(i).resolutionCount;
                pyramidIndex++;
            } else {
                resIndex = getSeries() - index;
                break;
            }
        }
    }
    Pyramid pyramid = pyramids.get(pyramidIndex);
    for (String dim : pyramid.dimensionOrdering.keySet()) {
        int index = pyramid.dimensionOrdering.get(dim) + 2;
        if (dim.equals("Z")) {
            t.coordinate[index] = zct[0];
        } else if (dim.equals("C")) {
            t.coordinate[index] = zct[1];
        } else if (dim.equals("T")) {
            t.coordinate[index] = zct[2];
        }
    }
    if (resIndex > 0) {
        t.coordinate[t.coordinate.length - 1] = resIndex;
    }
    ArrayList<TileCoordinate> map = tileMap.get(getCoreIndex());
    Integer index = map.indexOf(t);
    if (index == null || index < 0) {
        // fill in the tile with the stored background color
        // usually this is either black or white
        byte[] tile = new byte[getTileSize()];
        byte[] color = backgroundColor.get(getCoreIndex());
        if (color != null) {
            for (int q = 0; q < getTileSize(); q += color.length) {
                for (int i = 0; i < color.length; i++) {
                    tile[q + i] = color[i];
                }
            }
        }
        return tile;
    }
    Long offset = tileOffsets.get(getCoreIndex())[index];
    RandomAccessInputStream ets = new RandomAccessInputStream(fileMap.get(getCoreIndex()));
    ets.seek(offset);
    CodecOptions options = new CodecOptions();
    options.interleaved = isInterleaved();
    options.littleEndian = isLittleEndian();
    int tileSize = getTileSize();
    if (tileSize == 0) {
        tileSize = tileX.get(getCoreIndex()) * tileY.get(getCoreIndex()) * 10;
    }
    options.maxBytes = (int) (offset + tileSize);
    byte[] buf = null;
    long end = index < tileOffsets.get(getCoreIndex()).length - 1 ? tileOffsets.get(getCoreIndex())[index + 1] : ets.length();
    IFormatReader reader = null;
    String file = null;
    switch(compressionType.get(getCoreIndex())) {
        case RAW:
            buf = new byte[tileSize];
            ets.read(buf);
            break;
        case JPEG:
            Codec codec = new JPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_2000:
            codec = new JPEG2000Codec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_LOSSLESS:
            codec = new LosslessJPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case PNG:
            file = "tile.png";
            reader = new APNGReader();
        case BMP:
            if (reader == null) {
                file = "tile.bmp";
                reader = new BMPReader();
            }
            byte[] b = new byte[(int) (end - offset)];
            ets.read(b);
            Location.mapFile(file, new ByteArrayHandle(b));
            reader.setId(file);
            buf = reader.openBytes(0);
            Location.mapFile(file, null);
            break;
    }
    if (reader != null) {
        reader.close();
    }
    ets.close();
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) Codec(loci.formats.codec.Codec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 17 with ByteArrayHandle

use of loci.common.ByteArrayHandle in project bioformats by openmicroscopy.

the class SixteenBitLosslessJPEG2000Test method testLosslessPixels.

@Test
public void testLosslessPixels() throws Exception {
    int failureCount = 0;
    for (int v = Short.MIN_VALUE; v < Short.MAX_VALUE; v += increment) {
        int index = v + Short.MAX_VALUE + 1;
        byte[] pixels = DataTools.shortToBytes((short) v, false);
        String file = index + ".jp2";
        ByteArrayHandle tmpFile = new ByteArrayHandle(1);
        Location.mapFile(file, tmpFile);
        IMetadata metadata16;
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service = factory.getInstance(OMEXMLService.class);
            metadata16 = service.createOMEXMLMetadata();
        } catch (DependencyException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        } catch (ServiceException exc) {
            throw new FormatException("Could not create OME-XML store.", exc);
        }
        MetadataTools.populateMetadata(metadata16, 0, "foo", false, "XYCZT", "uint16", 1, 1, 1, 1, 1, 1);
        IFormatWriter writer16 = new JPEG2000Writer();
        writer16.setMetadataRetrieve(metadata16);
        writer16.setId(file);
        writer16.saveBytes(0, pixels);
        writer16.close();
        byte[] buf = tmpFile.getBytes();
        byte[] realData = new byte[(int) tmpFile.length()];
        System.arraycopy(buf, 0, realData, 0, realData.length);
        tmpFile.close();
        tmpFile = new ByteArrayHandle(realData);
        Location.mapFile(file, tmpFile);
        ImageReader reader = new ImageReader();
        reader.setId(file);
        byte[] plane = reader.openBytes(0);
        for (int q = 0; q < plane.length; q++) {
            if (plane[q] != pixels[q]) {
                LOGGER.debug("FAILED on {}", DataTools.bytesToShort(pixels, false));
                failureCount++;
                break;
            }
        }
        reader.close();
        tmpFile.close();
        Location.mapFile(file, null);
    }
    assertEquals(failureCount, 0);
}
Also used : JPEG2000Writer(loci.formats.out.JPEG2000Writer) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) IFormatWriter(loci.formats.IFormatWriter) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ByteArrayHandle(loci.common.ByteArrayHandle) ImageReader(loci.formats.ImageReader) Test(org.testng.annotations.Test)

Example 18 with ByteArrayHandle

use of loci.common.ByteArrayHandle in project bioformats by openmicroscopy.

the class TiffSaverTest method setUp.

@BeforeMethod
public void setUp() throws IOException {
    ByteArrayHandle handle = new ByteArrayHandle(INITIAL_CAPACITY);
    out = new RandomAccessOutputStream(handle);
    in = new RandomAccessInputStream(handle);
    tiffSaver = new TiffSaver(out, handle);
    tiffParser = new TiffParser(in);
    ifd = new IFD();
    ifd.putIFDValue(IFD.IMAGE_WIDTH, 512);
    ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "comment");
}
Also used : IFD(loci.formats.tiff.IFD) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 19 with ByteArrayHandle

use of loci.common.ByteArrayHandle in project bioformats by openmicroscopy.

the class ZeissCZIReader method readAttachments.

private void readAttachments() throws FormatException, IOException {
    if (!canReadAttachments()) {
        return;
    }
    boolean foundLabel = false;
    boolean foundPreview = false;
    for (Segment segment : segments) {
        if (segment instanceof Attachment) {
            AttachmentEntry entry = ((Attachment) segment).attachment;
            String name = entry.name.trim();
            if ((name.equals("Label") && !foundLabel) || (name.equals("SlidePreview") && !foundPreview)) {
                if (!foundLabel) {
                    foundLabel = name.equals("Label");
                }
                if (!foundPreview) {
                    foundPreview = name.equals("SlidePreview");
                }
                segment.fillInData();
                // label and preview are CZI files embedded as attachments
                ZeissCZIReader thumbReader = new ZeissCZIReader();
                thumbReader.setMetadataOptions(getMetadataOptions());
                ByteArrayHandle stream = new ByteArrayHandle(((Attachment) segment).attachmentData);
                Location.mapFile("image.czi", stream);
                thumbReader.setId("image.czi");
                CoreMetadata c = thumbReader.getCoreMetadataList().get(0);
                if (c.sizeZ > 1 || c.sizeT > 1) {
                    continue;
                }
                core.add(new CoreMetadata(c));
                core.get(core.size() - 1).thumbnail = true;
                ((Attachment) segment).attachmentData = thumbReader.openBytes(0);
                thumbReader.close();
                stream.close();
                Location.mapFile("image.czi", null);
                extraImages.add((Attachment) segment);
            }
        }
        segment.close();
    }
}
Also used : ByteArrayHandle(loci.common.ByteArrayHandle) CoreMetadata(loci.formats.CoreMetadata)

Example 20 with ByteArrayHandle

use of loci.common.ByteArrayHandle in project bioformats by openmicroscopy.

the class PGMReader 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);
    in.seek(offset);
    if (rawBits) {
        readPlane(in, x, y, w, h, buf);
    } else {
        ByteArrayHandle handle = new ByteArrayHandle();
        RandomAccessOutputStream out = new RandomAccessOutputStream(handle);
        out.order(isLittleEndian());
        while (in.getFilePointer() < in.length()) {
            String line = in.readLine().trim();
            line = line.replaceAll("[^0-9]", " ");
            StringTokenizer t = new StringTokenizer(line, " ");
            while (t.hasMoreTokens()) {
                int q = Integer.parseInt(t.nextToken().trim());
                if (getPixelType() == FormatTools.UINT16) {
                    out.writeShort(q);
                } else
                    out.writeByte(q);
            }
        }
        out.close();
        RandomAccessInputStream s = new RandomAccessInputStream(handle);
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
        s.close();
    }
    return buf;
}
Also used : StringTokenizer(java.util.StringTokenizer) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Aggregations

ByteArrayHandle (loci.common.ByteArrayHandle)26 RandomAccessInputStream (loci.common.RandomAccessInputStream)18 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)7 IOException (java.io.IOException)6 FormatException (loci.formats.FormatException)6 CodecOptions (loci.formats.codec.CodecOptions)5 CoreMetadata (loci.formats.CoreMetadata)4 TiffSaver (loci.formats.tiff.TiffSaver)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Location (loci.common.Location)3 DependencyException (loci.common.services.DependencyException)3 ServiceFactory (loci.common.services.ServiceFactory)3 IFD (loci.formats.tiff.IFD)3 TiffParser (loci.formats.tiff.TiffParser)3 Length (ome.units.quantity.Length)3 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 IFormatReader (loci.formats.IFormatReader)2 Codec (loci.formats.codec.Codec)2 JPEGCodec (loci.formats.codec.JPEGCodec)2