Search in sources :

Example 1 with BitWriter

use of loci.formats.codec.BitWriter in project bioformats by openmicroscopy.

the class MIASReader method populateMaskPixels.

// -- MIASReader API methods --
/**
 * Populate the MaskPixels.BinData attribute for the Mask identified by the
 * given Image index, ROI index, and Shape index.
 * @return true if the mask was populated successfully.
 */
public boolean populateMaskPixels(int imageIndex, int roiIndex, int shapeIndex, MetadataStore store) throws FormatException, IOException {
    FormatTools.assertId(currentId, true, 1);
    String id = MetadataTools.createLSID("Mask", imageIndex, roiIndex, shapeIndex);
    String maskFile = overlayFiles.get(id);
    if (maskFile == null) {
        LOGGER.warn("Could not find an overlay file matching {}", id);
        return false;
    }
    MinimalTiffReader r = new MinimalTiffReader();
    r.setId(maskFile);
    int index = overlayPlanes.get(id).intValue();
    byte[] plane = r.openBytes(0);
    byte[][] planes = null;
    if (r.isIndexed()) {
        planes = ImageTools.indexedToRGB(r.get8BitLookupTable(), plane);
    } else {
        int bpp = FormatTools.getBytesPerPixel(r.getPixelType());
        planes = new byte[r.getRGBChannelCount()][];
        for (int c = 0; c < planes.length; c++) {
            planes[c] = ImageTools.splitChannels(plane, c, r.getRGBChannelCount(), bpp, false, r.isInterleaved());
        }
    }
    r.close();
    for (int i = 0; i < planes[0].length; i++) {
        boolean channelsEqual = true;
        for (int c = 1; c < planes.length; c++) {
            if (planes[c][i] != planes[0][i]) {
                channelsEqual = false;
                break;
            }
        }
        if (channelsEqual) {
            for (int c = 0; c < planes.length; c++) {
                planes[c][i] = 0;
            }
        }
    }
    // threshold and binary encode the pixel data
    boolean validMask = false;
    BitWriter bits = null;
    if (planes.length > index) {
        bits = new BitWriter(planes[index].length / 8);
        for (int p = 0; p < planes[index].length; p++) {
            bits.write(planes[index][p] == 0 ? 0 : 1, 1);
            if (planes[index][p] != 0) {
                validMask = true;
            }
        }
    }
    if (validMask) {
        store.setMaskBinData(bits.toByteArray(), roiIndex, shapeIndex);
    } else
        LOGGER.debug("Did not populate MaskPixels.BinData for {}", id);
    return validMask;
}
Also used : BitWriter(loci.formats.codec.BitWriter)

Example 2 with BitWriter

use of loci.formats.codec.BitWriter in project bioformats by openmicroscopy.

the class TrestleReader method parseROIs.

// -- Helper methods --
private void parseROIs(MetadataStore store) throws FormatException, IOException {
    String roiID = MetadataTools.createLSID("ROI", 0, 0);
    String maskID = MetadataTools.createLSID("Shape", 0, 0);
    store.setROIID(roiID, 0);
    store.setMaskID(maskID, 0, 0);
    String positionData = DataTools.readFile(roiDrawFile);
    String[] coordinates = positionData.split("[ ,]");
    double x1 = Double.parseDouble(coordinates[1]);
    double y1 = Double.parseDouble(coordinates[2]);
    double x2 = Double.parseDouble(coordinates[3]);
    double y2 = Double.parseDouble(coordinates[5]);
    store.setMaskX(x1, 0, 0);
    store.setMaskY(y1, 0, 0);
    store.setMaskWidth(x2 - x1, 0, 0);
    store.setMaskHeight(y2 - y1, 0, 0);
    store.setImageROIRef(roiID, 0, 0);
    ImageReader roiReader = new ImageReader();
    roiReader.setId(roiFile);
    byte[] roiPixels = roiReader.openBytes(0);
    roiReader.close();
    BitWriter bits = new BitWriter(roiPixels.length / 8);
    for (int i = 0; i < roiPixels.length; i++) {
        bits.write(roiPixels[i] == 0 ? 0 : 1, 1);
    }
    store.setMaskBinData(bits.toByteArray(), 0, 0);
}
Also used : BitWriter(loci.formats.codec.BitWriter) ImageReader(loci.formats.ImageReader)

Aggregations

BitWriter (loci.formats.codec.BitWriter)2 ImageReader (loci.formats.ImageReader)1