import net.sourceforge.jiu.data.RGBIntegerImage;

/** This represents a square neighborhood in a complete image
 *  and provides a way to iterate over the image's neighborhoods.
 *  This neighborhood keeps itself completely within the image.
 */
public class InputNHood extends NHood {

    /** This calls NHood's constructor and verifies that the center
     *  places the neighborhood completely within the image.
     */
    public InputNHood(RGBIntegerImage image, int cx, int cy, int size) {
	super(image, cx, cy, size);
	if( xoffset < 0 || yoffset < 0 || xoffset+size > image.getWidth()
                  || yoffset+size > image.getHeight()) {
	    throw new IllegalArgumentException( "Invalid center location");
	}
    }

    /** 
     *  This will set the neighborhood as close to the upper left corner
     *  of the image as possible.
     *
     *  @param image This is the image that the neighborhood is in.
     *  @param size This is the width and height (in pixels) of the nhood.
     */
    public InputNHood(RGBIntegerImage image, int size) {
        this(image, size/2, size/2, size);
    }

    /** This shifts the neighborhood's center to the next pixel.
     *  The center shifts to the right until it runs out of room.
     *  It then wraps around to the beginning of the next row.
     *  This operation keeps the entire neighborhood within
     *  the image boundaries.
     *  
     *  If such a move is not possible from
     *  the current location (ie stuck in the bottom right corner)
     *  then this returns false. This otherwise returns true.
     *
     *  @return This returns false if the center can not be moved
     *          any farther and this returns true otherwise.
     */
    public boolean nextPixel() {

	// if can move right
	if( xoffset + size < image.getWidth()-1 ) {

	    centerx++;
	    setCorner(centerx-viewcenter, centery-viewcenter);
	    return true;
	}

	// else if can move down
	if( yoffset + size < image.getHeight()-1 ) {

	    // wrap around to the next row
	    centery++;
	    centerx = 0 + viewcenter;
	    setCorner(0, centery-viewcenter);
	    return true;
	}

	return false;
    }

}
