Search in sources :

Example 76 with ByteProcessor

use of ij.process.ByteProcessor in project TrakEM2 by trakem2.

the class NonLinearTransform method transform.

public ImageProcessor[] transform(final ImageProcessor ip) {
    if (!precalculated)
        this.precalculateTransfom();
    final ImageProcessor newIp = ip.createProcessor(ip.getWidth(), ip.getHeight());
    if (ip instanceof ColorProcessor)
        ip.max(0);
    final ImageProcessor maskIp = new ByteProcessor(ip.getWidth(), ip.getHeight());
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            if (transField[x][y][0] == -1) {
                continue;
            }
            newIp.set(x, y, (int) ip.getInterpolatedPixel((int) transField[x][y][0], (int) transField[x][y][1]));
            maskIp.set(x, y, 255);
        }
    }
    return new ImageProcessor[] { newIp, maskIp };
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor)

Example 77 with ByteProcessor

use of ij.process.ByteProcessor in project TrakEM2 by trakem2.

the class ExportARGB method makeFlatImageARGBFromMipMaps.

/**
 * Returns nonsense or throws an Exception if mipmaps are not available.
 * Limited to 2GB arrays for the final image.
 *
 * @param patches
 * @param roi
 * @param backgroundValue
 * @param scale
 * @return
 */
public static final Pair<ColorProcessor, ByteProcessor> makeFlatImageARGBFromMipMaps(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale) {
    final int width = (int) (roi.width * scale);
    final int height = (int) (roi.height * scale);
    // Process the three channels separately in order to use proper alpha composition
    final ColorProcessor target = new ColorProcessor(width, height);
    target.setInterpolationMethod(ImageProcessor.BILINEAR);
    final ByteProcessor targetMask = new ByteProcessor(width, height);
    targetMask.setInterpolationMethod(ImageProcessor.BILINEAR);
    final Loader loader = patches.get(0).getProject().getLoader();
    for (final Patch patch : patches) {
        // MipMap image, already including any coordinate transforms and the alpha mask (if any), by definition.
        final MipMapImage mipMap = loader.fetchImage(patch, scale);
        // / DEBUG: is there an alpha channel at all?
        // new ij.ImagePlus("alpha of " + patch.getTitle(), new ByteProcessor( mipMap.image.getWidth(null), mipMap.image.getHeight(null), new ColorProcessor( mipMap.image ).getChannel( 4 ))).show();
        // Yes, there is, even though the mipmap images have the alpha pre-multiplied
        // Work-around strange bug that makes mipmap-loaded images paint with 7-bit depth instead of 8-bit depth
        final BufferedImage bi = new BufferedImage(mipMap.image.getWidth(null), mipMap.image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(mipMap.image, 0, 0, null);
        g2d.dispose();
        final int[] pix = extractARGBIntArray(bi);
        bi.flush();
        // DEBUG: does the BufferedImage have the alpha channel?
        // {
        // final byte[] aa = new byte[pix.length];
        // for (int i=0; i<aa.length; ++i) aa[i] = (byte)((pix[i] & 0xff000000) >> 24);
        // new ij.ImagePlus("alpha of BI of " + patch.getTitle(), new ByteProcessor(bi.getWidth(), bi.getHeight(), aa)).show();
        // }
        // YES: the alpha, containing the outside too. All fine.
        final ByteProcessor alpha;
        final ColorProcessor rgb = new ColorProcessor(bi.getWidth(), bi.getHeight(), pix);
        if (patch.hasAlphaChannel()) {
            // The mipMap has the alpha channel in it, even if the alpha is pre-multiplied as well onto the images.
            final byte[] a = new byte[pix.length];
            for (int i = 0; i < a.length; ++i) {
                a[i] = (byte) ((pix[i] & 0xff000000) >> 24);
            }
            alpha = new ByteProcessor(bi.getWidth(), bi.getHeight(), a);
        } else {
            alpha = new ByteProcessor(bi.getWidth(), bi.getHeight());
            Arrays.fill((byte[]) alpha.getPixels(), (byte) 255);
        }
        // The affine to apply to the MipMap.image
        final AffineTransform atc = new AffineTransform();
        atc.scale(scale, scale);
        atc.translate(-roi.x, -roi.y);
        final AffineTransform at = new AffineTransform();
        at.preConcatenate(atc);
        at.concatenate(patch.getAffineTransform());
        at.scale(mipMap.scaleX, mipMap.scaleY);
        final AffineModel2D aff = new AffineModel2D();
        aff.set(at);
        final CoordinateTransformMesh mesh = new CoordinateTransformMesh(aff, patch.getMeshResolution(), bi.getWidth(), bi.getHeight());
        final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
        // no interpolation
        alpha.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
        rgb.setInterpolationMethod(ImageProcessor.BILINEAR);
        mapping.map(rgb, alpha, target, targetMask);
    }
    return new Pair<ColorProcessor, ByteProcessor>(target, targetMask);
}
Also used : ByteProcessor(ij.process.ByteProcessor) Loader(ini.trakem2.persistence.Loader) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ColorProcessor(ij.process.ColorProcessor) MipMapImage(ini.trakem2.display.MipMapImage) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch) Pair(mpicbg.trakem2.util.Pair)

Example 78 with ByteProcessor

use of ij.process.ByteProcessor in project TrakEM2 by trakem2.

the class ExportBestFlatImage method makeFlatGrayImageAndAlpha.

/**
 * @return Return null when dimensions make the array larger than 2GB.
 */
public Pair<ByteProcessor, ByteProcessor> makeFlatGrayImageAndAlpha() {
    printInfo();
    if (canUseAWTImage()) {
        // In color to preserve the alpha channel present in mipmaps
        final Image img = createAWTImage(ImagePlus.COLOR_RGB);
        final int width = img.getWidth(null);
        final int height = img.getHeight(null);
        final int[] pixels = new int[width * height];
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }
        ;
        final byte[] grey = new byte[pixels.length];
        final byte[] alpha = new byte[pixels.length];
        for (int i = 0; i < pixels.length; ++i) {
            final int p = pixels[i];
            alpha[i] = (byte) ((p & 0xff000000) >> 24);
            grey[i] = (byte) ((((p & 0x00ff0000) >> 16) + ((p & 0x0000ff00) >> 8) + (p & 0x000000ff)) / 3f);
        }
        return new Pair<ByteProcessor, ByteProcessor>(new ByteProcessor(width, height, grey, null), new ByteProcessor(width, height, alpha, null));
    }
    if (!isSmallerThan2GB()) {
        Utils.log("Cannot create an image larger than 2 GB.");
        return null;
    }
    if (loader.isMipMapsRegenerationEnabled()) {
        // Use mipmaps directly: they are already Gaussian-downsampled
        return ExportUnsignedByte.makeFlatImageFromMipMaps(patches, finalBox, 0, scale);
    }
    // Else: no mipmaps
    return ExportUnsignedByte.makeFlatImageFromOriginals(patches, finalBox, 0, scale);
}
Also used : ByteProcessor(ij.process.ByteProcessor) PixelGrabber(java.awt.image.PixelGrabber) Image(java.awt.Image) Pair(mpicbg.trakem2.util.Pair)

Example 79 with ByteProcessor

use of ij.process.ByteProcessor in project TrakEM2 by trakem2.

the class ExportUnsignedByte method makeFlatImage.

public static final Pair<ByteProcessor, ByteProcessor> makeFlatImage(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale, final ImageSource fetcher) {
    final ByteProcessor target = new ByteProcessor((int) (roi.width * scale), (int) (roi.height * scale));
    target.setInterpolationMethod(ImageProcessor.BILINEAR);
    final ByteProcessor targetMask = new ByteProcessor(target.getWidth(), target.getHeight());
    targetMask.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
    for (final Patch patch : patches) {
        final ImageData imgd = fetcher.fetch(patch, scale);
        // The affine to apply to the MipMap.image
        final AffineTransform atc = new AffineTransform();
        atc.scale(scale, scale);
        atc.translate(-roi.x, -roi.y);
        final AffineTransform at = new AffineTransform();
        at.preConcatenate(atc);
        at.concatenate(patch.getAffineTransform());
        at.scale(imgd.scaleX, imgd.scaleY);
        final AffineModel2D aff = new AffineModel2D();
        aff.set(at);
        final CoordinateTransformMesh mesh = new CoordinateTransformMesh(aff, patch.getMeshResolution(), imgd.bp.getWidth(), imgd.bp.getHeight());
        final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
        imgd.bp.setInterpolationMethod(ImageProcessor.BILINEAR);
        // no interpolation
        imgd.alpha.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
        mapping.map(imgd.bp, imgd.alpha, target, targetMask);
    }
    return new Pair<ByteProcessor, ByteProcessor>(target, targetMask);
}
Also used : ByteProcessor(ij.process.ByteProcessor) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch) Pair(mpicbg.trakem2.util.Pair)

Example 80 with ByteProcessor

use of ij.process.ByteProcessor in project TrakEM2 by trakem2.

the class ExportUnsignedShort method map.

protected static final void map(final PatchTransform pt, final double x, final double y, final double scale, final ShortProcessor mappedIntensities, final ShortProcessor target, final ByteProcessor alphaTarget) {
    final TranslationModel2D t = new TranslationModel2D();
    t.set(-x, -y);
    final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
    ctl.add(pt.ct);
    if (!Double.isNaN(scale)) {
        final AffineModel2D s = new AffineModel2D();
        s.set(scale, 0, 0, scale, 0, 0);
        ctl.add(s);
    }
    ctl.add(t);
    final CoordinateTransformMesh mesh = new CoordinateTransformMesh(ctl, pt.pir.patch.getMeshResolution(), pt.pir.patch.getOWidth(), pt.pir.patch.getOHeight());
    final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
    ByteProcessor alpha = null;
    mappedIntensities.setInterpolationMethod(ImageProcessor.BILINEAR);
    if (pt.pir.patch.hasAlphaMask()) {
        alpha = pt.pir.patch.getAlphaMask();
        alpha.setInterpolationMethod(ImageProcessor.BILINEAR);
        mapping.map(mappedIntensities, alpha, target);
    } else {
        mapping.mapInterpolated(mappedIntensities, target);
    }
    // If alphaTarget is present, repeat the mapping but just for the alpha channel
    if (null != alphaTarget) {
        if (null == alpha) {
            // Simulate full alpha: no transparency
            alpha = new ByteProcessor(pt.pir.patch.getOWidth(), pt.pir.patch.getOHeight());
            alpha.setInterpolationMethod(ImageProcessor.BILINEAR);
            final byte[] as = (byte[]) alpha.getPixels();
            for (int i = 0; i < as.length; ++i) {
                as[i] = (byte) 255;
            }
        }
        mapping.mapInterpolated(alpha, alphaTarget);
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) CoordinateTransformList(mpicbg.models.CoordinateTransformList) TranslationModel2D(mpicbg.models.TranslationModel2D) CoordinateTransform(mpicbg.models.CoordinateTransform)

Aggregations

ByteProcessor (ij.process.ByteProcessor)103 ImagePlus (ij.ImagePlus)36 ImageProcessor (ij.process.ImageProcessor)31 FloatProcessor (ij.process.FloatProcessor)23 ShortProcessor (ij.process.ShortProcessor)21 ColorProcessor (ij.process.ColorProcessor)20 ArrayList (java.util.ArrayList)13 Point (java.awt.Point)12 Rectangle (java.awt.Rectangle)11 Roi (ij.gui.Roi)10 AffineTransform (java.awt.geom.AffineTransform)10 ImageStack (ij.ImageStack)9 Patch (ini.trakem2.display.Patch)9 Calibration (ij.measure.Calibration)8 Pair (mpicbg.trakem2.util.Pair)7 Color (java.awt.Color)6 LUT (ij.process.LUT)5 BufferedImage (java.awt.image.BufferedImage)5 IOException (java.io.IOException)5 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)5