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 ******************************************************************************/
009package imagingbook.common.image.access;
010
011import ij.process.FloatProcessor;
012import ij.process.ImageProcessor;
013import imagingbook.common.image.OutOfBoundsStrategy;
014import imagingbook.common.image.interpolation.InterpolationMethod;
015
016/**
017 * Image accessor for scalar images with 32-bit (float) values.
018 * 
019 * @author WB
020 * @version 2022/09/22
021 */
022public class FloatAccessor extends ScalarAccessor {
023        private final float[] pixels;
024
025        /**
026         * Constructor. See also the factory method
027         * {@link ScalarAccessor#create(ImageProcessor, OutOfBoundsStrategy, InterpolationMethod)}.
028         *
029         * @param ip an instance of {@link FloatProcessor}
030         * @param obs the out-of-bounds strategy to be used (use {@code null} for default settings)
031         * @param ipm the interpolation method to be used (use {@code null} for default settings)
032         */
033        public FloatAccessor(FloatProcessor ip, OutOfBoundsStrategy obs, InterpolationMethod ipm) {
034                super(ip, obs, ipm);
035                this.pixels = (float[]) ip.getPixels();
036        }
037        
038        public static FloatAccessor create(FloatProcessor ip, OutOfBoundsStrategy obs, InterpolationMethod ipm) {
039                return new FloatAccessor(ip, obs, ipm);
040        }
041
042        @Override
043        public float getVal(int u, int v) {
044                int i = indexer.getIndex(u, v);
045                if (i < 0)
046                        return this.defaultValue;
047                else
048                        return pixels[i];
049        }
050
051        @Override
052        public void setVal(int u, int v, float val) {
053                if (u >= 0 && u < width && v >= 0 && v < height) {
054                        pixels[width * v + u] = val;
055                }
056        }
057}