/** {@inheritDoc} */ public int read() throws IOException { synchronized (file) { int retval = -1; if (currentPosition < endPosition) { file.seek(currentPosition); currentPosition++; retval = file.read(); } return retval; } }
/** {@inheritDoc} */ public int read(byte[] b, int offset, int length) throws IOException { // only allow a read of the amount available. if (length > available()) { length = available(); } int amountRead = -1; // only read if there are bytes actually available, otherwise // return -1 if the EOF has been reached. if (available() > 0) { synchronized (file) { file.seek(currentPosition); amountRead = file.read(b, offset, length); } } // update the current cursor position. if (amountRead > 0) { currentPosition += amountRead; } return amountRead; }
// change the return type to Img<FloatType> and adjust the code accordingly public <T extends RealType<T>> Img<T> gradient(Img<T> img) { // create a new ImgLib2 image of same dimensions, but FloatType // to to that, create a new PlanarImgFactory and instantiate a new // Img<FloatType> /** * ImgFactory<T> imgFactory = img.factory(); Img<T> gradientImg = imgFactory.create( img, * img.firstElement() ); */ // create a localizing cursor on the GradientImg, it will iterate all pixels // and is able to efficiently return its position at each pixel, at each // pixel we will compute the gradient /** Cursor<T> cursor = gradientImg.localizingCursor(); */ // We extend the input image by a mirroring out of bounds strategy so // that we can access pixels outside of the image RandomAccessible<T> view = Views.extendMirrorSingle(img); // instantiate a RandomAccess on the extended view, it will be used to // compute the gradient locally at each pixel location RandomAccess<T> randomAccess = view.randomAccess(); // iterate over all pixels while (cursor.hasNext()) { // move the cursor to the next pixel cursor.fwd(); // compute gradient in each dimension double gradient = 0; for (int d = 0; d < img.numDimensions(); ++d) { // set the randomaccess to the location of the cursor randomAccess.setPosition(cursor); // move one pixel back in dimension d randomAccess.bck(d); // get the value double v1 = randomAccess.get().getRealDouble(); // move twice forward in dimension d, i.e. // one pixel above the location of the cursor randomAccess.fwd(d); randomAccess.fwd(d); // get the value double v2 = randomAccess.get().getRealDouble(); // add the square of the magnitude of the gradient gradient += ((v2 - v1) * (v2 - v1)) / 4; } // the square root of all quadratic sums yields // the magnitude of the gradient at this location, // set the pixel value of the gradient image // change the value to float, otherwise it will not be // compatible cursor.get().setReal(Math.sqrt(gradient)); } return gradientImg; }