So lets say I have a block that is 10x10, my goal is the break this into quadrants and then once I have done that, go back to the first quadrant and break and into four quadrants, go to the second quadrants and break that into four quadrants and so on until they are all broken and then go back to the first and repeat it for the new set of blocks.
So essentially I want to break a block into 4 pieces and break each new block into four pieces and keep going with this pattern.
This is the code I have so far:
private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg) {
int red = 0;
int green = 0;
int blue = 0;
int rgb = 0;
if(endHeight <= 1 || endWidth <= 1) {
return;
}
// get average
for(int i = startHeight; i < endHeight; i++) {
for(int j = startWidth; j < endWidth; j++) {
rgb = img.getRGB(j, i);
red += (rgb >> 16) & 0xFF;
green += (rgb >> 8) & 0xFF;
blue += (rgb) & 0xFF;
}
}
// get average color
red = red /((startWidth - endWidth) * (startHeight - endHeight));
green = green/((startWidth - endWidth) * (startHeight - endHeight));
blue = blue/((startWidth - endWidth) * (startHeight - endHeight));
Color color = new Color(red,green,blue);
// apply
for(int i = startHeight; i < endHeight; i++) {
for(int j = startWidth; j < endWidth; j++) {
blockImg.setRGB(j, i, color.getRGB());
}
}
getBlockAverage(startHeight, endHeight/2, startWidth, endWidth/2, img, blockImg);
getBlockAverage(endHeight/2+1, endHeight, endWidth, endWidth/2, img, blockImg);
getBlockAverage(startHeight, endHeight/2, endWidth/2+1, endWidth, img, blockImg);
getBlockAverage(endHeight/2+1, endHeight, endWidth/2+1, endWidth, img, blockImg);
}
So what I am doing is trying to recursively call this function which will continue to break each block into quadrants but I continue to get a stack overflow.
What my code does is takes an image, gets the average colour of that block and displays it. It is a relatively simple concept that I am going to tweek a bit to get some cool images but that is for later, right now I am trying to fix this issue.
Edit here are the results of System.out.println(startHeight + " " + endHeight + " "+ startWidth + " " + endWidth);
0 72 0 108
0 36 0 54
0 18 0 27
0 9 0 13
0 4 0 6
0 2 0 3
0 1 0 1
0 1 2 3
3 4 0 3
3 4 0 1
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
and then 3 4 2 4 repeats until I get the stack overflow.