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