/* This function takes a picture and make from it filled grid. Each element of * grid is image`s pixel. Grid is filled with boolean values. If it is a black * point, it will be marked as true, other - false. */ void imageParsing(string fileName) { string fileAdress = "images/"; fileAdress = fileAdress + fileName; GBufferedImage image; image.load(fileAdress); pixels.resize(image.getWidth(), image.getHeight()); for (int column = 0; column < image.getWidth(); column++) { for (int row = 0; row < image.getHeight(); row++) { if (image.getRGB(column, row) <= GREY) { pixels.set(column, row, true); } else { pixels.set(column, row, false); } } } for (int column = 0; column < pixels.nCols; column++) { for (int row = 0; row < pixels.nRows; row++) { if (pixels.inBounds(column, row) && pixels.get(column, row) == true) { pixels.set(column, row, false); Points blackPoint = makePoint(column, row); checkAllNeighbours.push(blackPoint); detectingSilouettes(pixels); } } } }
/* * 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; }