use of loci.common.Region in project bioformats by openmicroscopy.
the class ZeissCZIReader 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);
if (isThumbnailSeries()) {
// thumbnail, label, or preview image stored as an attachment
int index = getCoreIndex() - (core.size() - extraImages.size());
byte[] fullPlane = extraImages.get(index).attachmentData;
RandomAccessInputStream s = new RandomAccessInputStream(fullPlane);
try {
readPlane(s, x, y, w, h, buf);
} finally {
s.close();
}
return buf;
}
previousChannel = getZCTCoords(no)[1];
int currentIndex = getCoreIndex();
Region image = new Region(x, y, w, h);
int bpp = FormatTools.getBytesPerPixel(getPixelType());
int pixel = getRGBChannelCount() * bpp;
int outputRowLen = w * pixel;
int outputRow = 0, outputCol = 0;
boolean validScanDim = scanDim == (getImageCount() / (getSizeC() * phases)) && scanDim > 1;
if (planes.size() == getImageCount()) {
validScanDim = false;
}
Arrays.fill(buf, (byte) 0);
boolean emptyTile = true;
try {
int minTileX = Integer.MAX_VALUE, minTileY = Integer.MAX_VALUE;
int baseResolution = currentIndex;
while (baseResolution > 0 && core.get(baseResolution - 1).sizeX > core.get(baseResolution).sizeX) {
baseResolution--;
}
for (SubBlock plane : planes) {
if ((plane.planeIndex == no && ((maxResolution == 0 && plane.coreIndex == currentIndex) || (maxResolution > 0 && plane.coreIndex == baseResolution))) || (plane.planeIndex == previousChannel && validScanDim)) {
if (plane.row < minTileY) {
minTileY = plane.row;
}
if (plane.col < minTileX) {
minTileX = plane.col;
}
}
}
for (SubBlock plane : planes) {
if ((plane.coreIndex == currentIndex && plane.planeIndex == no) || (plane.planeIndex == previousChannel && validScanDim)) {
int res = (int) Math.pow(scaleFactor, plane.resolutionIndex);
int realX = plane.x / res;
int realY = plane.y / res;
if ((prestitched != null && prestitched) || validScanDim) {
Region tile = new Region(plane.col, plane.row, realX, realY);
if (validScanDim) {
tile.y += (no / getSizeC());
image.height = scanDim;
}
if (prestitched != null && prestitched && realX == getSizeX() && realY == getSizeY()) {
tile.x = 0;
tile.y = 0;
} else if (prestitched != null && prestitched) {
// normalize the coordinates such that minimum row/col values are 0
tile.x -= minTileX;
tile.y -= minTileY;
}
tile.x /= res;
tile.y /= res;
if (tile.intersects(image)) {
emptyTile = false;
byte[] rawData = new SubBlock(plane).readPixelData();
Region intersection = tile.intersection(image);
int intersectionX = 0;
if (tile.x < image.x) {
intersectionX = image.x - tile.x;
}
outputCol = (intersection.x - x) * pixel;
outputRow = intersection.y - y;
if (validScanDim) {
outputRow -= tile.y;
}
if (rawData.length < realX * realY * pixel) {
realX = rawData.length / (realY * pixel);
} else if (rawData.length == (realX + 1) * (realY + 1) * pixel) {
realX++;
realY++;
}
int rowLen = pixel * (int) Math.min(intersection.width, realX);
int outputOffset = outputRow * outputRowLen + outputCol;
for (int trow = 0; trow < intersection.height; trow++) {
int realRow = trow + intersection.y - tile.y;
if (validScanDim) {
realRow += tile.y;
}
int inputOffset = pixel * (realRow * realX + intersectionX);
System.arraycopy(rawData, inputOffset, buf, outputOffset, rowLen);
outputOffset += outputRowLen;
}
}
} else {
byte[] rawData = null;
// this saves a little time over opening a new stream for every tile/plane
if (pixels.size() == 0) {
rawData = new SubBlock(plane).readPixelData(in, new Region(x, y, w, h), buf);
} else {
rawData = new SubBlock(plane).readPixelData();
}
if (rawData.length > buf.length || pixels.size() > 0) {
RandomAccessInputStream s = new RandomAccessInputStream(rawData);
try {
readPlane(s, x, y, w, h, realX - getSizeX(), buf);
emptyTile = false;
} finally {
s.close();
}
} else {
emptyTile = false;
}
break;
}
}
}
} finally {
}
if (isRGB() && !emptyTile) {
// channels are stored in BGR order; red and blue channels need switching
int redOffset = bpp * 2;
for (int i = 0; i < buf.length / pixel; i++) {
int index = i * pixel;
for (int b = 0; b < bpp; b++) {
int blueIndex = index + b;
int redIndex = index + redOffset + b;
byte red = buf[redIndex];
buf[redIndex] = buf[blueIndex];
buf[blueIndex] = red;
}
}
}
return buf;
}
use of loci.common.Region in project bioformats by openmicroscopy.
the class CropDialog method harvestResults.
@Override
protected boolean harvestResults(GenericDialog gd) {
final int seriesCount = process.getSeriesCount();
final IFormatReader r = process.getReader();
for (int s = 0; s < seriesCount; s++) {
if (!options.isSeriesOn(s))
continue;
r.setSeries(s);
Region region = new Region();
region.x = (int) gd.getNextNumber();
region.y = (int) gd.getNextNumber();
region.width = (int) gd.getNextNumber();
region.height = (int) gd.getNextNumber();
if (region.x < 0)
region.x = 0;
if (region.y < 0)
region.y = 0;
if (region.x >= r.getSizeX())
region.x = r.getSizeX() - region.width - 1;
if (region.y >= r.getSizeY())
region.y = r.getSizeY() - region.height - 1;
if (region.width < 1)
region.width = 1;
if (region.height < 1)
region.height = 1;
if (region.width + region.x > r.getSizeX()) {
region.width = r.getSizeX() - region.x;
}
if (region.height + region.y > r.getSizeY()) {
region.height = r.getSizeY() - region.y;
}
options.setCropRegion(s, region);
}
return true;
}
use of loci.common.Region in project bioformats by openmicroscopy.
the class ImportProcess method getMemoryUsage.
/**
* Gets a projection of required memory in bytes.
* Valid only after {@link ImportStep#SERIES}.
*/
public long getMemoryUsage() {
final int seriesCount = getSeriesCount();
long total = 0;
for (int s = 0; s < seriesCount; s++) {
if (!options.isSeriesOn(s))
continue;
reader.setSeries(s);
// determine size of one image plane
final Region cropRegion = getCropRegion(s);
final int bpp = FormatTools.getBytesPerPixel(reader.getPixelType());
final long planeSize = bpp * cropRegion.width * cropRegion.height;
// determine total number of image planes
final int cCount = getCCount(s);
final int zCount = getZCount(s);
final int tCount = getTCount(s);
final long planeCount = cCount * zCount * tCount;
// determine active number of image planes
final boolean isVirtual = options.isVirtual();
final long activeChannels = options.isColorModeComposite() ? cCount : 1;
final long activePlanes = isVirtual ? activeChannels : planeCount;
// compute total memory footprint for this series
final long seriesSize = planeSize * activePlanes;
total += seriesSize;
}
return total;
}
use of loci.common.Region in project bioformats by openmicroscopy.
the class ImportProcess method getCropRegion.
// crop options
/**
* Valid only after {@link ImportStep#STACK}.
*/
public Region getCropRegion(int s) {
assertStep(ImportStep.STACK);
Region region = options.doCrop() ? options.getCropRegion(s) : null;
reader.setSeries(s);
int sizeX = reader.getSizeX(), sizeY = reader.getSizeY();
if (region == null) {
// entire image plane is the default region
region = new Region(0, 0, sizeX, sizeY);
} else {
// bounds checking for cropped region
if (region.x < 0)
region.x = 0;
if (region.y < 0)
region.y = 0;
if (region.width <= 0 || region.x + region.width > sizeX) {
region.width = sizeX - region.x;
}
if (region.height <= 0 || region.y + region.height > sizeY) {
region.height = sizeX - region.y;
}
}
return region;
}
use of loci.common.Region in project bioformats by openmicroscopy.
the class LociFunctions method openSubImage.
public void openSubImage(String title, Double no, Double x, Double y, Double w, Double h) throws FormatException, IOException {
ImporterOptions options = new ImporterOptions();
options.setWindowless(true);
options.setId(r.getCurrentFile());
options.setCrop(true);
options.setSpecifyRanges(true);
options.setSeriesOn(r.getSeries(), true);
int[] zct = r.getZCTCoords(no.intValue());
options.setCBegin(r.getSeries(), zct[1]);
options.setZBegin(r.getSeries(), zct[0]);
options.setTBegin(r.getSeries(), zct[2]);
options.setCEnd(r.getSeries(), zct[1]);
options.setZEnd(r.getSeries(), zct[0]);
options.setTEnd(r.getSeries(), zct[2]);
Region region = new Region(x.intValue(), y.intValue(), w.intValue(), h.intValue());
options.setCropRegion(r.getSeries(), region);
ImportProcess process = new ImportProcess(options);
process.execute();
ImagePlusReader reader = new ImagePlusReader(process);
final ImagePlus imp = reader.openImagePlus()[0];
Calibrator calibrator = new Calibrator(process);
calibrator.applyCalibration(imp);
process.getReader().close();
imp.show();
}
Aggregations