001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009
010package imagingbook.common.image.interpolation;
011import imagingbook.common.image.access.ScalarAccessor;
012
013
014/**
015 * <p>
016 * A {@link PixelInterpolator} implementing nearest-neighbor interpolation in 2D. See Sec. 22.5.1 of [1] for additional
017 * details.
018 * </p>
019 * <p>
020 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing &ndash; An Algorithmic Introduction</em>, 3rd ed, Springer
021 * (2022).
022 * </p>
023 *
024 * @author WB
025 */
026public class NearestNeighborInterpolator implements PixelInterpolator {
027        
028        /**
029         * Constructor.
030         */
031        public NearestNeighborInterpolator() {
032        }
033        
034        @Override
035        public float getInterpolatedValue(ScalarAccessor ia, double x, double y) {
036                final int u = (int) Math.rint(x);
037                final int v = (int) Math.rint(y);
038                return ia.getVal(u, v);
039        }
040
041        /**
042         * Corresponds to function w_nn(x), see Eqn. 22.10 in [1].
043         * TODO: test, not used currently.
044         */
045        @Override
046        public double getWeight(double x) {
047                if (-0.5 <= x && x < 0.5)
048                        return 1;
049                else
050                        return 0;
051        }
052        
053}