Search in sources :

Example 16 with Region

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;
}
Also used : Region(loci.common.Region)

Example 17 with Region

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);
    }
}
Also used : IFD(loci.formats.tiff.IFD) Region(loci.common.Region) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

Region (loci.common.Region)17 FormatException (loci.formats.FormatException)5 ImagePlus (ij.ImagePlus)3 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 IOException (java.io.IOException)2 IFormatReader (loci.formats.IFormatReader)2 GenericDialog (ij.gui.GenericDialog)1 ImageProcessor (ij.process.ImageProcessor)1 ArrayList (java.util.ArrayList)1 ServiceException (loci.common.services.ServiceException)1 IMetadata (loci.formats.meta.IMetadata)1 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)1 JPEGTurboServiceImpl (loci.formats.services.JPEGTurboServiceImpl)1 IFD (loci.formats.tiff.IFD)1 IFDList (loci.formats.tiff.IFDList)1 TiffParser (loci.formats.tiff.TiffParser)1 Calibrator (loci.plugins.in.Calibrator)1 ImagePlusReader (loci.plugins.in.ImagePlusReader)1 ImportProcess (loci.plugins.in.ImportProcess)1 ImporterOptions (loci.plugins.in.ImporterOptions)1