use of loci.common.Region in project bioformats by openmicroscopy.
the class JPEGTurboServiceImpl method getTile.
@Override
public byte[] getTile(byte[] buf, int xCoordinate, int yCoordinate, int width, int height) throws IOException {
Region image = new Region(xCoordinate, yCoordinate, width, height);
int bufX = 0;
int bufY = 0;
int outputRowLen = width * 3;
Region intersection = null;
Region tileBoundary = new Region(0, 0, 0, 0);
byte[] tile = null;
for (int row = 0; row < yTiles; row++) {
tileBoundary.height = row < yTiles - 1 ? tileDim : imageHeight - (tileDim * row);
tileBoundary.y = row * tileDim;
for (int col = 0; col < xTiles; col++) {
tileBoundary.x = col * tileDim;
tileBoundary.width = col < xTiles - 1 ? tileDim : imageWidth - (tileDim * col);
if (tileBoundary.intersects(image)) {
intersection = image.intersection(tileBoundary);
tile = getTile(col, row);
int rowLen = 3 * (int) Math.min(tileBoundary.width, intersection.width);
int outputOffset = bufY * outputRowLen + bufX;
int intersectionX = 0;
if (tileBoundary.x < image.x) {
intersectionX = image.x - tileBoundary.x;
}
for (int trow = 0; trow < intersection.height; trow++) {
int realRow = trow + intersection.y - tileBoundary.y;
int inputOffset = 3 * (realRow * tileDim + intersectionX);
System.arraycopy(tile, inputOffset, buf, outputOffset, rowLen);
outputOffset += outputRowLen;
}
bufX += rowLen;
}
}
if (intersection != null) {
bufX = 0;
bufY += intersection.height;
}
if (bufY >= height) {
break;
}
}
return buf;
}
use of loci.common.Region in project bioformats by openmicroscopy.
the class TiffWriter method saveBytes.
/**
* Saves the given image to the specified series in the current file.
* The IFD hashtable allows specification of TIFF parameters such as bit
* depth, compression and units.
*/
public void saveBytes(int no, byte[] buf, IFD ifd, int x, int y, int w, int h) throws IOException, FormatException {
if (checkParams)
checkParams(no, buf, x, y, w, h);
if (ifd == null)
ifd = new IFD();
MetadataRetrieve retrieve = getMetadataRetrieve();
int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(series).toString());
int index = no;
int imageWidth = retrieve.getPixelsSizeX(series).getValue().intValue();
int imageHeight = retrieve.getPixelsSizeY(series).getValue().intValue();
int currentTileSizeX = getTileSizeX();
int currentTileSizeY = getTileSizeY();
if (currentTileSizeX != imageWidth || currentTileSizeY != imageHeight) {
ifd.put(new Integer(IFD.TILE_WIDTH), new Long(currentTileSizeX));
ifd.put(new Integer(IFD.TILE_LENGTH), new Long(currentTileSizeY));
}
if (currentTileSizeX < w || currentTileSizeY < h) {
int numTilesX = (w + (x % currentTileSizeX) + currentTileSizeX - 1) / currentTileSizeX;
int numTilesY = (h + (y % currentTileSizeY) + currentTileSizeY - 1) / currentTileSizeY;
for (int yTileIndex = 0; yTileIndex < numTilesY; yTileIndex++) {
for (int xTileIndex = 0; xTileIndex < numTilesX; xTileIndex++) {
Region tileParams = new Region();
tileParams.width = xTileIndex < numTilesX - 1 ? currentTileSizeX - (x % currentTileSizeX) : w - (currentTileSizeX * xTileIndex);
tileParams.height = yTileIndex < numTilesY - 1 ? currentTileSizeY - (y % currentTileSizeY) : h - (currentTileSizeY * yTileIndex);
tileParams.x = x + (xTileIndex * currentTileSizeX) - (xTileIndex > 0 ? (x % currentTileSizeX) : 0);
tileParams.y = y + (yTileIndex * currentTileSizeY) - (yTileIndex > 0 ? (y % currentTileSizeY) : 0);
byte[] tileBuf = getTile(buf, tileParams, new Region(x, y, w, h));
// This operation is synchronized
synchronized (this) {
// This operation is synchronized against the TIFF saver.
synchronized (tiffSaver) {
index = prepareToWriteImage(no, tileBuf, ifd, tileParams.x, tileParams.y, tileParams.width, tileParams.height);
if (index == -1) {
return;
}
}
}
tiffSaver.writeImage(tileBuf, ifd, index, type, tileParams.x, tileParams.y, tileParams.width, tileParams.height, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
}
}
} else {
// This operation is synchronized
synchronized (this) {
// This operation is synchronized against the TIFF saver.
synchronized (tiffSaver) {
index = prepareToWriteImage(no, buf, ifd, x, y, w, h);
if (index == -1) {
return;
}
}
}
tiffSaver.writeImage(buf, ifd, index, type, x, y, w, h, no == getPlaneCount() - 1 && getSeries() == retrieve.getImageCount() - 1);
}
}
Aggregations