/*
 * Function:  floodFillRecursion()
 * --------------------
 * Helper function that does the actual recursion for floodFill
 *
 * Preconditions: None
 *
 *  @param: A buffered image object that tells us where and what was clicked
 *          x,y position clicked
 *          color selected to change to
 *  @return: The number of pixels actually changed.
 */
int floodFillRecursion(GBufferedImage& image, int x, int y, int colorClicked, int color,int& numChangedPixels) {
    if(image.getRGB(x,y)==colorClicked){
        image.setRGB(x,y,color);
        numChangedPixels++;
    } else {
        return numChangedPixels;
    }
    if(image.inBounds(x-1,y)){
        floodFillRecursion(image,x-1,y,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x+1,y)){
        floodFillRecursion(image,x+1,y,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x,y-1)){
        floodFillRecursion(image,x,y-1,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x,y+1)){
        floodFillRecursion(image,x,y+1,colorClicked,color,numChangedPixels);
    }
    return numChangedPixels;
}
/*
 * Function:  floodFill();
 * --------------------
 * Function that fills clicked color with selected color
 *
 * Preconditions: None
 *
 *  @param: A buffered image object that tells us where and what was clicked
 *          x,y position clicked
 *          color selected to change to
 *  @return: returns the numebr of pixels changed
 */
int floodFill(GBufferedImage& image, int x, int y, int color) {
    int colorClicked = image.getRGB(x,y);
    int numChangedPixels = 0;
    // First check to make sure that we don't do anything if the color picked is also
    // The color selected. If it is same return 0
    if(colorClicked==color){
        return 0;
    }
    // Doubly make sure that the place clicked is inBounds
    if(image.inBounds(x,y)){
        return floodFillRecursion(image,x,y,colorClicked,color,numChangedPixels);
    } else {
        try
          {
            throw 0003;
          }
          catch (int e)
          {
            cout << "An exception occurred (x||y out of bounds). Exception Nr. " << e << '\n';
          }
    }
    return 0;
}