Search in sources :

Example 1 with MDShortArray

use of ch.systemsx.cisd.base.mdarray.MDShortArray in project bioformats by openmicroscopy.

the class CellH5Writer method saveBytes.

/**
 * Saves the given image to the specified (possibly already open) file.
 */
@Override
public void saveBytes(int no, byte[] buf) throws IOException, FormatException {
    LOGGER.info("CellH5Writer: Save image to HDF5 path: " + outputPath);
    MetadataRetrieve r = getMetadataRetrieve();
    int sizeX = r.getPixelsSizeX(series).getValue();
    int sizeY = r.getPixelsSizeY(series).getValue();
    int sizeC = r.getPixelsSizeC(series).getValue();
    int sizeT = r.getPixelsSizeT(series).getValue();
    int sizeZ = r.getPixelsSizeZ(series).getValue();
    DimensionOrder dimo = r.getPixelsDimensionOrder(0);
    int c, z, t;
    if (dimo.equals(DimensionOrder.XYCZT)) {
        c = no % sizeC;
        z = ((no - c) / sizeC) % sizeZ;
        t = (((no - c) / sizeC)) / sizeZ;
    } else if (dimo.equals(DimensionOrder.XYCTZ)) {
        c = no % sizeC;
        t = ((no - c) / sizeC) % sizeT;
        z = (((no - c) / sizeC)) / sizeT;
    } else if (dimo.equals(DimensionOrder.XYZTC)) {
        z = no % sizeZ;
        t = ((no - z) / sizeZ) % sizeT;
        c = (((no - z) / sizeZ)) / sizeT;
    } else {
        throw new FormatException("CellH5Writer: Dimension order not understood: " + dimo.getValue());
    }
    LOGGER.info("CellH5Writer.saveBytes(): Current c, t, z == {} {} {}", c, t, z);
    LOGGER.info("CellH5Writer.saveBytes(): bpp {} byte buffer len {}", bpp, buf.length);
    if (bpp == 1) {
        MDByteArray image = new MDByteArray(new int[] { 1, 1, 1, sizeY, sizeX });
        for (int x_i = 0; x_i < sizeX; x_i++) {
            for (int y_i = 0; y_i < sizeY; y_i++) {
                byte value = (byte) buf[y_i * sizeX + x_i];
                image.set(value, 0, 0, 0, y_i, x_i);
            }
        }
        jhdf.writeArraySlice(outputPath, image, new long[] { c, t, z, 0, 0 });
    } else if (bpp == 2) {
        ByteBuffer bb = ByteBuffer.wrap(buf);
        ShortBuffer sb = bb.asShortBuffer();
        MDShortArray image = new MDShortArray(new int[] { 1, 1, 1, sizeY, sizeX });
        for (int x_i = 0; x_i < sizeX; x_i++) {
            for (int y_i = 0; y_i < sizeY; y_i++) {
                short value = sb.get(y_i * sizeX + x_i);
                image.set(value, 0, 0, 0, y_i, x_i);
            }
        }
        jhdf.writeArraySlice(outputPath, image, new long[] { c, t, z, 0, 0 });
    } else if (bpp == 4) {
        ByteBuffer bb = ByteBuffer.wrap(buf);
        IntBuffer ib = bb.asIntBuffer();
        MDIntArray image = new MDIntArray(new int[] { 1, 1, 1, sizeY, sizeX });
        for (int x_i = 0; x_i < sizeX; x_i++) {
            for (int y_i = 0; y_i < sizeY; y_i++) {
                int value = (int) ib.get(y_i * sizeX + x_i);
                image.set(value, 0, 0, 0, y_i, x_i);
            }
        }
        jhdf.writeArraySlice(outputPath, image, new long[] { c, t, z, 0, 0 });
    } else {
        throw new FormatException("CellH5Writer: Pixel type not supported");
    }
}
Also used : MDIntArray(ch.systemsx.cisd.base.mdarray.MDIntArray) IntBuffer(java.nio.IntBuffer) MDShortArray(ch.systemsx.cisd.base.mdarray.MDShortArray) DimensionOrder(ome.xml.model.enums.DimensionOrder) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer) FormatException(loci.formats.FormatException) MDByteArray(ch.systemsx.cisd.base.mdarray.MDByteArray)

Aggregations

MDByteArray (ch.systemsx.cisd.base.mdarray.MDByteArray)1 MDIntArray (ch.systemsx.cisd.base.mdarray.MDIntArray)1 MDShortArray (ch.systemsx.cisd.base.mdarray.MDShortArray)1 ByteBuffer (java.nio.ByteBuffer)1 IntBuffer (java.nio.IntBuffer)1 ShortBuffer (java.nio.ShortBuffer)1 FormatException (loci.formats.FormatException)1 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)1 DimensionOrder (ome.xml.model.enums.DimensionOrder)1