|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2018 ImageJ developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package net.imagej.ops.filter.addUniformNoise; |
| 31 | + |
| 32 | +import net.imagej.ops.Ops; |
| 33 | +import net.imagej.ops.special.computer.AbstractUnaryComputerOp; |
| 34 | +import net.imglib2.type.numeric.IntegerType; |
| 35 | + |
| 36 | +import org.scijava.Priority; |
| 37 | +import org.scijava.plugin.Parameter; |
| 38 | +import org.scijava.plugin.Plugin; |
| 39 | +import org.scijava.util.MersenneTwisterFast; |
| 40 | + |
| 41 | +/** |
| 42 | + * Adds a pseudorandomly generated value {@code x} to a {@link IntegerType} |
| 43 | + * {@code I}, such that {@code x} is (inclusively) bounded by {@code rangeMin} |
| 44 | + * and {@code rangeMax} parameters, i.e. {@code rangeMin <= x <= rangeMax}. |
| 45 | + * |
| 46 | + * @author Gabe Selzer |
| 47 | + */ |
| 48 | +@Plugin(type = Ops.Filter.AddUniformNoise.class, priority = Priority.HIGH) |
| 49 | +public class AddUniformNoiseIntegerType<I extends IntegerType<I>> |
| 50 | + extends AbstractUnaryComputerOp<I, I> implements Ops.Filter.AddUniformNoise |
| 51 | +{ |
| 52 | + |
| 53 | + /** |
| 54 | + * The greatest that an input value can be decreased. |
| 55 | + */ |
| 56 | + @Parameter |
| 57 | + private long rangeMin; |
| 58 | + |
| 59 | + /** |
| 60 | + * The greatest that an input value can be <b> increased </b> |
| 61 | + */ |
| 62 | + @Parameter |
| 63 | + private long rangeMax; |
| 64 | + |
| 65 | + /** |
| 66 | + * If false, the Op will wrap outputs that are outside of the type bounds, |
| 67 | + * instead of clamping them |
| 68 | + */ |
| 69 | + @Parameter(required = false) |
| 70 | + private boolean clampOutput = true; |
| 71 | + |
| 72 | + @Parameter(required = false) |
| 73 | + private long seed = 0xabcdef1234567890L; |
| 74 | + |
| 75 | + private MersenneTwisterFast rng; |
| 76 | + |
| 77 | + @Override |
| 78 | + public void initialize() { |
| 79 | + if (rng == null) rng = new MersenneTwisterFast(seed); |
| 80 | + if (rangeMax < rangeMin) { |
| 81 | + long temp = rangeMax; |
| 82 | + rangeMax = rangeMin; |
| 83 | + rangeMin = temp; |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void compute(I input, I output) { |
| 90 | + final long newVal = rng.nextLong(rangeMax - rangeMin + 1) + |
| 91 | + rangeMin + input.getIntegerLong(); |
| 92 | + |
| 93 | + // clamp output |
| 94 | + if (clampOutput) { |
| 95 | + output.setReal(Math.max(input.getMinValue(), Math.min(input.getMaxValue(), |
| 96 | + newVal))); |
| 97 | + } |
| 98 | + |
| 99 | + // wrap output |
| 100 | + else { |
| 101 | + double outVal = newVal; |
| 102 | + // when output larger than max value, add difference of output and max |
| 103 | + // value to the min value |
| 104 | + while (outVal > input.getMaxValue()) { |
| 105 | + outVal = input.getMinValue() + (outVal - input.getMaxValue() - 1); |
| 106 | + } |
| 107 | + // when output smaller than min value, subtract difference of output and |
| 108 | + // min value from the max value |
| 109 | + while (outVal < input.getMinValue()) { |
| 110 | + outVal = input.getMaxValue() - (outVal - input.getMinValue() + 1); |
| 111 | + } |
| 112 | + |
| 113 | + output.setReal(outVal); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments