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());
}
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);
}
}
}
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);
}
}
}
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;
}
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++;
}
}
}
Aggregations