From 3c9328a789f1f55c6aeb63b023032880576880b4 Mon Sep 17 00:00:00 2001 From: Albert Cardona Date: Thu, 9 Apr 2009 11:11:48 +0200 Subject: [PATCH] Blending respects existing alpha masks if asked to. Disabled blending when one of the selected Patch is coordinate-transformed: is buggy/slow/something is wrong. --- ini/trakem2/display/Display.java | 10 ++++++++- ini/trakem2/imaging/Blending.java | 47 ++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/ini/trakem2/display/Display.java b/ini/trakem2/display/Display.java index 0d1bdbb5..34d6861d 100644 --- a/ini/trakem2/display/Display.java +++ b/ini/trakem2/display/Display.java @@ -3141,7 +3141,15 @@ public final class Display extends DBObject implements ActionListener, ImageList for (final Displayable d : selection.getSelected()) { if (d.getClass() == Patch.class) patches.add((Patch)d); } - if (patches.size() > 1) Blending.blend(patches); + if (patches.size() > 1) { + GenericDialog gd = new GenericDialog("Blending"); + gd.addCheckbox("Respect current alpha mask", true); + gd.showDialog(); + if (gd.wasCanceled()) return; + Blending.blend(patches, gd.getNextBoolean()); + } else { + IJ.log("Please select more than one overlapping image."); + } } else if (command.equals("Montage")) { if (!(active instanceof Patch)) { Utils.showMessage("Please select only images."); diff --git a/ini/trakem2/imaging/Blending.java b/ini/trakem2/imaging/Blending.java index fa18128b..0441d2b6 100644 --- a/ini/trakem2/imaging/Blending.java +++ b/ini/trakem2/imaging/Blending.java @@ -39,7 +39,7 @@ public final class Blending { * the other images) or not (full alpha). * An image that doesn't overlap at all gets no alpha set at all. */ - static public final Bureaucrat blend(final Set patches) { + static public final Bureaucrat blend(final Set patches, final boolean respect_current_mask) { if (null == patches || patches.size() < 2) return null; return Bureaucrat.createAndStart(new Worker("Blending images") { @@ -47,6 +47,13 @@ public final class Blending { try { startedWorking(); + for (final Patch p : patches) { + if (null != p.getCoordinateTransform()) { + Utils.log("CANNOT blend: at least one image has a coordinate transform.\nBlending of coordinate-transformed images will be enabled in the near future."); + return; + } + } + final HashMap meshes = new HashMap(); for (final Patch p : patches) { meshes.put(p, null == p.getCoordinateTransform() ? null @@ -64,7 +71,7 @@ public final class Blending { for ( Patch op : patches ) if ( p.getLayer().indexOf( op ) < pLayerIndex ) overlapping.add( op ); - if (setBlendingMask(p, overlapping, meshes)) { + if (setBlendingMask(p, overlapping, meshes, respect_current_mask)) { p.updateMipmaps(); } }}, null); @@ -93,7 +100,7 @@ public final class Blending { } /** Returns true if a new mask has been set to Patch p. */ - static private boolean setBlendingMask(final Patch p, Set overlapping, final Map meshes) { + static private boolean setBlendingMask(final Patch p, Set overlapping, final Map meshes, final boolean respect_current_mask) { Utils.log2("Blending " + p); @@ -105,7 +112,16 @@ public final class Blending { final AffineTransform at = p.getAffineTransform(); final TransformMesh mesh = meshes.get(p); - final ByteProcessor mask = new ByteProcessor(p.getOWidth(), p.getOHeight()); + ByteProcessor mask = null; + if (respect_current_mask) { + mask = p.getProject().getLoader().fetchImageMask(p); + } + if (null == mask) { + mask = new ByteProcessor(p.getOWidth(), p.getOHeight()); + mask.setValue(255); + mask.fill(); + } + final byte[] pix = (byte[]) mask.getPixels(); final Point2D.Double po = new Point2D.Double(); @@ -152,15 +168,26 @@ public final class Blending { float weight = intersects(fo, other, meshes.get(other)); if (weight > 0) weights[next++] = weight; } - if (next > 0) { + + final int i = y * p_o_height + x; + + if (respect_current_mask) { + // Don't compute if no overlap or if current mask value is zero + if (next > 0 && pix[i] != 0) { + weights[next++] = computeWeight(x, y, p_o_width, p_o_height); // the weight of Patch p, added last + float sum = 0; + for (int f=0; f 0) { + // Overwritting current mask weights[next++] = computeWeight(x, y, p_o_width, p_o_height); // the weight of Patch p, added last float sum = 0; for (int f=0; f 0) { p.setAlphaMask(mask); - new ij.ImagePlus("mask for " + p.getId(), mask).show(); + //new ij.ImagePlus("mask for " + p.getId(), mask).show(); return true; } -- 2.11.4.GIT