|
| 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.ops2.transform; |
| 31 | + |
| 32 | +import org.scijava.function.Functions; |
| 33 | +import org.scijava.ops.spi.Optional; |
| 34 | + |
| 35 | +import net.imglib2.FinalInterval; |
| 36 | +import net.imglib2.Interval; |
| 37 | +import net.imglib2.RandomAccessible; |
| 38 | +import net.imglib2.RandomAccessibleInterval; |
| 39 | +import net.imglib2.interpolation.InterpolatorFactory; |
| 40 | +import net.imglib2.interpolation.randomaccess.LanczosInterpolatorFactory; |
| 41 | +import net.imglib2.realtransform.InvertibleRealTransform; |
| 42 | +import net.imglib2.realtransform.RealViews; |
| 43 | +import net.imglib2.type.numeric.NumericType; |
| 44 | +import net.imglib2.type.numeric.RealType; |
| 45 | +import net.imglib2.view.Views; |
| 46 | + |
| 47 | +/** |
| 48 | + * Applies an Affine transform to a {@link RandomAccessibleInterval} |
| 49 | + * |
| 50 | + * @author Brian Northan (True North Intelligent Algorithms) |
| 51 | + * @author Martin Horn (University of Konstanz) |
| 52 | + * @author Stefan Helfrich (University of Konstanz) |
| 53 | + * @implNote op names="transform.realTransform", priority="101.0" |
| 54 | + */ |
| 55 | +public class DefaultTransformView<T extends NumericType<T> & RealType<T>> |
| 56 | + implements |
| 57 | + Functions.Arity4<RandomAccessibleInterval<T>, InvertibleRealTransform, Interval, InterpolatorFactory<T, RandomAccessible<T>>, RandomAccessibleInterval<T>> |
| 58 | +{ |
| 59 | + |
| 60 | + /** |
| 61 | + * TODO: declare {@code outputInterval}, {@code interpolator} as optional once |
| 62 | + * <a href=https://github.com/scijava/incubator/pull/32>this issue</a> has |
| 63 | + * been resolved. Until then, this op <b>must</b> be called as a |
| 64 | + * {@link Functions.Arity4} |
| 65 | + * |
| 66 | + * @param input the input |
| 67 | + * @param transform the transform to apply |
| 68 | + * @param outputInterval the output interval |
| 69 | + * @param interpolator the {@link InterpolatorFactory} delegated to for |
| 70 | + * interpolation |
| 71 | + * @return the output |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public RandomAccessibleInterval<T> apply( // |
| 75 | + RandomAccessibleInterval<T> input, // |
| 76 | + InvertibleRealTransform transform, // |
| 77 | + @Optional Interval outputInterval, // |
| 78 | + @Optional InterpolatorFactory<T, RandomAccessible<T>> interpolator // |
| 79 | + ) { |
| 80 | + if (outputInterval == null) { |
| 81 | + outputInterval = new FinalInterval(input); |
| 82 | + } |
| 83 | + |
| 84 | + if (interpolator == null) { |
| 85 | + interpolator = new LanczosInterpolatorFactory<>(); |
| 86 | + } |
| 87 | + |
| 88 | + var extended = Views.extendZero(input); |
| 89 | + var interpolated = Views.interpolate(extended, interpolator); |
| 90 | + var transformed = RealViews.transformReal(interpolated, transform); |
| 91 | + var rasterized = Views.raster(transformed); |
| 92 | + return Views.interval(rasterized, outputInterval); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments