Search in sources :

Example 16 with ByteProcessor

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

the class ExportBestFlatImage method makeFlatFloatGrayImageAndAlpha.

/**
 * While the data is in the 8-bit range, the format is as a FloatProcessor.
 */
public Pair<FloatProcessor, FloatProcessor> makeFlatFloatGrayImageAndAlpha() {
    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 float[] grey = new float[pixels.length];
        final float[] alpha = new float[pixels.length];
        for (int i = 0; i < pixels.length; ++i) {
            final int p = pixels[i];
            alpha[i] = ((p & 0xff000000) >> 24);
            grey[i] = (((p & 0x00ff0000) >> 16) + ((p & 0x0000ff00) >> 8) + (p & 0x000000ff)) / 3f;
        }
        return new Pair<FloatProcessor, FloatProcessor>(new FloatProcessor(width, height, grey, null), new FloatProcessor(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
        final Pair<ByteProcessor, ByteProcessor> pair = ExportUnsignedByte.makeFlatImageFromMipMaps(patches, finalBox, 0, scale);
        return new Pair<FloatProcessor, FloatProcessor>(pair.a.convertToFloatProcessor(), pair.b.convertToFloatProcessor());
    }
    // Else: no mipmaps
    loader.releaseAll();
    // Use originals and Gaussian-downsample them, then map them onto the target image
    final Pair<ByteProcessor, ByteProcessor> pair = ExportUnsignedByte.makeFlatImageFromOriginals(patches, finalBox, 0, scale);
    return new Pair<FloatProcessor, FloatProcessor>(pair.a.convertToFloatProcessor(), pair.b.convertToFloatProcessor());
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) PixelGrabber(java.awt.image.PixelGrabber) Image(java.awt.Image) Pair(mpicbg.trakem2.util.Pair)

Example 17 with ByteProcessor

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

the class TransformMeshMappingWithMasks method map.

public final void map(final ImageProcessorWithMasks source, final ImageProcessorWithMasks target, final int numThreads) {
    target.outside = new ByteProcessor(target.getWidth(), target.getHeight());
    final HashMap<AffineModel2D, ArrayList<PointMatch>> av = transform.getAV();
    if (numThreads > 1) {
        final ArrayList<AffineModel2D> triangles = new ArrayList<AffineModel2D>(av.keySet());
        final AtomicInteger i = new AtomicInteger(0);
        final ArrayList<Thread> threads = new ArrayList<Thread>(numThreads);
        for (int k = 0; k < numThreads; ++k) {
            final Thread mtt = new MapTriangleThread(i, triangles, transform, source, target);
            threads.add(mtt);
            mtt.start();
        }
        for (final Thread mtt : threads) {
            try {
                mtt.join();
            } catch (final InterruptedException e) {
            }
        }
    } else if (source.mask == null) {
        for (final AffineModel2D triangle : av.keySet()) {
            mapTriangle(transform, triangle, source.ip, target.ip, target.outside);
        }
    } else {
        for (final AffineModel2D triangle : av.keySet()) {
            mapTriangle(transform, triangle, source.ip, source.mask, target.ip, target.mask, target.outside);
        }
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList)

Example 18 with ByteProcessor

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

the class TransformMeshMappingWithMasks method mapInterpolated.

public final void mapInterpolated(final ImageProcessorWithMasks source, final ImageProcessorWithMasks target, final int numThreads) {
    target.outside = new ByteProcessor(target.getWidth(), target.getHeight());
    source.ip.setInterpolationMethod(ImageProcessor.BILINEAR);
    if (source.mask != null) {
        source.mask.setInterpolationMethod(ImageProcessor.BILINEAR);
    }
    final HashMap<AffineModel2D, ArrayList<PointMatch>> av = transform.getAV();
    if (numThreads > 1) {
        final ArrayList<AffineModel2D> triangles = new ArrayList<AffineModel2D>(av.keySet());
        final AtomicInteger i = new AtomicInteger(0);
        final ArrayList<Thread> threads = new ArrayList<Thread>(numThreads);
        for (int k = 0; k < numThreads; ++k) {
            final Thread mtt = new MapTriangleInterpolatedThread(i, triangles, transform, source, target);
            threads.add(mtt);
            mtt.start();
        }
        for (final Thread mtt : threads) {
            try {
                mtt.join();
            } catch (final InterruptedException e) {
            }
        }
    } else if (source.mask == null) {
        for (final AffineModel2D triangle : av.keySet()) {
            mapTriangleInterpolated(transform, triangle, source.ip, target.ip, target.outside);
        }
    } else {
        for (final AffineModel2D triangle : av.keySet()) {
            mapTriangleInterpolated(transform, triangle, source.ip, source.mask, target.ip, target.mask, target.outside);
        }
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffineModel2D(mpicbg.models.AffineModel2D) ArrayList(java.util.ArrayList)

Example 19 with ByteProcessor

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

the class Downsampler method downsampleOutside.

/**
 * Downsample and outside mask.  Those pixels not fully covered in the
 * outside mask are set to 0, all others to 255.
 *
 * @param aOutside
 * @return
 */
public static final ByteProcessor downsampleOutside(final ByteProcessor aOutside) {
    final int wa = aOutside.getWidth();
    final int ha = aOutside.getHeight();
    final int wa2 = wa + wa;
    final int wb = wa / 2;
    final int hb = ha / 2;
    final int nb = hb * wb;
    final ByteProcessor bOutside = new ByteProcessor(wb, hb);
    final byte[] aOutsidePixels = (byte[]) aOutside.getPixels();
    final byte[] bOutsidePixels = (byte[]) bOutside.getPixels();
    for (int ya = 0, yb = 0; yb < nb; ya += wa2, yb += wb) {
        final int ya1 = ya + wa;
        for (int xa = 0, xb = 0; xb < wb; xa += 2, ++xb) {
            final int xa1 = xa + 1;
            final int sOutside = andByte(ya + xa, ya + xa1, ya1 + xa, ya1 + xa1, aOutsidePixels);
            if (sOutside == 0xff)
                bOutsidePixels[yb + xb] = -1;
            else
                bOutsidePixels[yb + xb] = 0;
        }
    }
    return bOutside;
}
Also used : ByteProcessor(ij.process.ByteProcessor)

Example 20 with ByteProcessor

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

the class DisplayCanvas method paintWithFiltering.

private final void paintWithFiltering(final Graphics2D g, final ArrayList<Displayable> al_paint, final Collection<? extends Paintable> paintables, final int first_non_patch, final int g_width, final int g_height, final Displayable active, final int c_alphas, final Layer layer, final List<Layer> layers, final boolean paint_non_images) {
    // Determine the type of the image: if any Patch is of type COLOR_RGB or COLOR_256, use RGB
    int type = BufferedImage.TYPE_BYTE_GRAY;
    search: for (final Displayable d : al_paint) {
        if (d.getClass() == Patch.class) {
            switch(((Patch) d).getType()) {
                case ImagePlus.COLOR_256:
                case ImagePlus.COLOR_RGB:
                    type = BufferedImage.TYPE_INT_ARGB;
                    break search;
            }
        }
    }
    // Paint all patches to an image
    final BufferedImage bi = new BufferedImage(g_width, g_height, type);
    final Graphics2D gpre = bi.createGraphics();
    gpre.setTransform(atc);
    int i = 0;
    for (final Paintable p : paintables) {
        if (i == first_non_patch)
            break;
        p.paint(gpre, srcRect, magnification, p == active, c_alphas, layer, layers);
        i++;
    }
    gpre.dispose();
    final ImagePlus imp = new ImagePlus("filtered", type == BufferedImage.TYPE_BYTE_GRAY ? new ByteProcessor(bi) : new ColorProcessor(bi));
    bi.flush();
    display.applyFilters(imp);
    // Paint the filtered image
    final AffineTransform aff = g.getTransform();
    // reset
    g.setTransform(new AffineTransform());
    g.drawImage(imp.getProcessor().createImage(), 0, 0, null);
    // Paint the remaining elements if any
    if (paint_non_images && first_non_patch != paintables.size()) {
        // restore srcRect and magnification
        g.setTransform(aff);
        // to smooth edges of the images
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        i = 0;
        for (final Paintable p : paintables) {
            if (i < first_non_patch) {
                i++;
                continue;
            }
            p.paint(g, srcRect, magnification, p == active, c_alphas, layer, layers);
            i++;
        }
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) ColorProcessor(ij.process.ColorProcessor) AffineTransform(java.awt.geom.AffineTransform) ImagePlus(ij.ImagePlus) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

ByteProcessor (ij.process.ByteProcessor)86 ImagePlus (ij.ImagePlus)30 ImageProcessor (ij.process.ImageProcessor)23 FloatProcessor (ij.process.FloatProcessor)21 ShortProcessor (ij.process.ShortProcessor)19 ColorProcessor (ij.process.ColorProcessor)14 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