/** * Fills the whole image with the specified pixel value * * @param img An image. * @param value The value that the image is being filled with. */ public static void fill(InterleavedI8 img, byte... value) { if (value.length != img.numBands) throw new IllegalArgumentException("Unexpected number of bands"); final int h = img.getHeight(); final int w = img.getWidth(); byte[] data = img.data; for (int y = 0; y < h; y++) { int index = img.getStartIndex() + y * img.getStride(); for (int x = 0; x < w; x++) { for (int k = 0; k < img.numBands; k++) data[index++] = value[k]; } } }
/** * Sets the pixel's value for all the bands using an array. * * @param x pixel coordinate. * @param y pixel coordinate. * @param value The pixel's new value for each band. */ public void set(int x, int y, byte... value) { if (!isInBounds(x, y)) throw new ImageAccessException("Requested pixel is out of bounds"); int index = getIndex(x, y, 0); for (int i = 0; i < numBands; i++, index++) { data[index] = value[i]; } }
/** * Fills one band in the image with the specified value * * @param input An image. * @param band Which band is to be filled with the specified value * @param value The value that the image is being filled with. */ public static void fillBand(InterleavedI8 input, int band , int value) { final int numBands = input.numBands; for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride() + band; int end = index + input.width*numBands - band; for (; index < end; index += numBands ) { input.data[index] = (byte)value; } } }
public static void convert( InterleavedU8 from, InterleavedI8 to ) { if (from.isSubimage() || to.isSubimage()) { final int N = from.width * from.getNumBands(); for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < N; x++) { to.data[indexTo++] = ( byte )( from.data[indexFrom++] & 0xFF); } } } else { final int N = from.width * from.height * from.getNumBands(); System.arraycopy(from.data, 0, to.data, 0, N); } }
final int offsetR = kernelWidth-offsetL-1; final int imgWidth = output.getWidth(); final int imgHeight = output.getHeight(); final int numBands = output.getNumBands(); final int total[] = new int[ numBands ];
public static void vertical( Kernel1D_S32 kernel, InterleavedU16 src, InterleavedI8 dst , int divisor ) { final short[] dataSrc = src.data; final byte[] dataDst = dst.data; final int[] dataKer = kernel.data; final int offset = kernel.getOffset(); final int kernelWidth = kernel.getWidth(); final int numBands = src.getNumBands(); final int halfDivisor = divisor/2; final int imgWidth = dst.getWidth(); final int imgHeight = dst.getHeight(); final int yEnd = imgHeight-(kernelWidth-offset-1); for( int y = offset; y < yEnd; y++ ) { int indexDst = dst.startIndex+y*dst.stride; int indexSrcStart = src.startIndex+(y-offset)*src.stride; for (int x = 0; x < imgWidth; x++) { for (int band = 0; band < numBands; band++) { int indexSrc = indexSrcStart + band; int total = 0; for (int k = 0; k < kernelWidth; k++) { total += (dataSrc[indexSrc] & 0xFFFF)* dataKer[k]; indexSrc += src.stride; } dataDst[indexDst++] = (byte)((total+halfDivisor)/divisor); } indexSrcStart += numBands; } } }
total[band] = (total[band]+weight/2)/weight; output.set(x,y, total );
@Override public void unsafe_set(int x, int y, int... value) { int index = getIndex(x, y, 0); for (int i = 0; i < numBands; i++, index++) { data[index] = (byte)value[i]; } }
final int numBands = dst.getNumBands(); final int total[] = new int[ numBands ];
public static void convert( InterleavedS8 from, InterleavedI8 to ) { if (from.isSubimage() || to.isSubimage()) { final int N = from.width * from.getNumBands(); for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < N; x++) { to.data[indexTo++] = ( from.data[indexFrom++] ); } } } else { final int N = from.width * from.height * from.getNumBands(); System.arraycopy(from.data, 0, to.data, 0, N); } }
/** * Fills the whole image with the specified value * * @param input An image. * @param value The value that the image is being filled with. */ public static void fill(InterleavedI8 input, int value) { for (int y = 0; y < input.height; y++) { int index = input.getStartIndex() + y * input.getStride(); int end = index + input.width*input.numBands; Arrays.fill(input.data,index,end, (byte)value); } }
final int offsetX1 = kernelWidthX-offsetX-1; final int imgWidth = dst.getWidth(); final int imgHeight = dst.getHeight(); final int numBands = dst.getNumBands(); final int total[] = new int[ numBands ];
public static void vertical( Kernel1D_S32 kernel, InterleavedU8 src, InterleavedI8 dst , int divisor ) { final byte[] dataSrc = src.data; final byte[] dataDst = dst.data; final int[] dataKer = kernel.data; final int offset = kernel.getOffset(); final int kernelWidth = kernel.getWidth(); final int numBands = src.getNumBands(); final int halfDivisor = divisor/2; final int imgWidth = dst.getWidth(); final int imgHeight = dst.getHeight(); final int yEnd = imgHeight-(kernelWidth-offset-1); for( int y = offset; y < yEnd; y++ ) { int indexDst = dst.startIndex+y*dst.stride; int indexSrcStart = src.startIndex+(y-offset)*src.stride; for (int x = 0; x < imgWidth; x++) { for (int band = 0; band < numBands; band++) { int indexSrc = indexSrcStart + band; int total = 0; for (int k = 0; k < kernelWidth; k++) { total += (dataSrc[indexSrc] & 0xFF)* dataKer[k]; indexSrc += src.stride; } dataDst[indexDst++] = (byte)((total+halfDivisor)/divisor); } indexSrcStart += numBands; } } }
public static void vertical(Kernel1D_S32 kernel, InterleavedU8 input, InterleavedI8 output ) { final int offset = kernel.getOffset(); final int width = input.getWidth(); final int height = input.getHeight(); final int numBands = input.getNumBands(); final int[] pixel = new int[ numBands ]; final int[] total = new int[ numBands ]; for (int y = 0; y < height; y++) { for( int x = 0; x < width; x++ ) { Arrays.fill(total,0); int weight = 0; int startY = y - offset; int endY = startY + kernel.getWidth(); if( startY < 0 ) startY = 0; if( endY > height ) endY = height; for( int i = startY; i < endY; i++ ) { int v = kernel.get(i-y+offset); input.get(x,i, pixel); for (int band = 0; band < numBands; band++) { total[band] += pixel[band]*v; } weight += v; } for (int band = 0; band < numBands; band++) { total[band] = (total[band]+weight/2)/weight; } output.set(x,y, total); } } }
/** * Rotates the image 90 degrees in the clockwise direction. */ public static void rotateCW( InterleavedI8 input , InterleavedI8 output ) { if( input.width != output.height || input.height != output.width || input.numBands != output.numBands ) throw new IllegalArgumentException("Incompatible shapes"); int h = input.height-1; for( int y = 0; y < input.height; y++ ) { int indexSrc = input.startIndex + y*input.stride; for (int x = 0; x < input.width; x++) { int indexDst = output.getIndex(h-y,x); int end = indexSrc + input.numBands; while( indexSrc != end ) { output.data[indexDst++] = input.data[indexSrc++]; } } } }
public static void convert( InterleavedF64 from, InterleavedI8 to ) { if (from.isSubimage() || to.isSubimage()) { final int N = from.width * from.getNumBands(); for (int y = 0; y < from.height; y++) { int indexFrom = from.getIndex(0, y); int indexTo = to.getIndex(0, y); for (int x = 0; x < N; x++) { to.data[indexTo++] = ( byte )( from.data[indexFrom++] ); } } } else { final int N = from.width * from.height * from.getNumBands(); for (int i = 0; i < N; i++) { to.data[i] = ( byte )( from.data[i] ); } } }
/** * Fills the whole image with random values * * @param img An image. * @param rand The value that the image is being filled with. */ public static void randomize(InterleavedI8 img, Random rand) { final int h = img.getHeight(); final int w = img.getWidth(); byte[] data = img.data; int range = Byte.MAX_VALUE - Byte.MIN_VALUE; for (int y = 0; y < h; y++) { int index = img.getStartIndex() + y * img.getStride(); for (int x = 0; x < w; x++) { for (int k = 0; k < img.numBands; k++) data[index++] = (byte) (rand.nextInt(range) + Byte.MIN_VALUE); } } } }
/** * Fills each band in the image with the specified values * * @param input An image. * @param values Array which contains the values each band is to be filled with. */ public static void fill(InterleavedI8 input, int[] values) { final int numBands = input.numBands; for (int y = 0; y < input.height; y++) { for( int band = 0; band < numBands; band++ ) { int index = input.getStartIndex() + y * input.getStride() + band; int end = index + input.width*numBands - band; int value = values[band]; for (; index < end; index += numBands ) { input.data[index] = (byte)value; } } } }
/** * Returns the pixel's value for all the bands as an array. * * @param x pixel coordinate. * @param y pixel coordinate. * @param storage If not null then the pixel's value is written here. If null a new array is created. * @return The pixel's value. */ public byte[] get(int x, int y, byte[] storage) { if (!isInBounds(x, y)) throw new ImageAccessException("Requested pixel is out of bounds"); if (storage == null) { storage = new byte[numBands]; } int index = getIndex(x, y, 0); for (int i = 0; i < numBands; i++, index++) { storage[i] = data[index]; } return storage; }
output.set(x,y, total);