Given an image I have to write a method that will "Sharpen" it. By sharpen, follow the formula newColorValue = (oldColorValue - min) * maxPossiblePixelValue / (max - min) where max and min are the maximum and minimum color values in the surrounding neighborhood. I wrote code that I think SHOULD work but it does random things. The first time I run it it doesn't do anything, and then when I try to sharpen again, I get values that are out of the color range which doesn't seem to make sense. There's a GUI already written and when you click "sharpen" this code executes.
public void execute (Pixmap target)
{
ourDialog.show();
Dimension neighborhood = ourDialog.getSize();
Dimension bounds = target.getSize();
int maxRed=0,maxGreen=0,maxBlue=0;
int minRed=255,minGreen=255,minBlue=255;
int newRed=0,newGreen=0,newBlue=0;
for(int x = neighborhood.width/2; x < bounds.width-neighborhood.width/2 ; x++)
{
for(int y = neighborhood.height/2; y < bounds.height-neighborhood.height/2; y++)
{
for(int xneighs = x-neighborhood.width/2; xneighs <= x+neighborhood.width/2 ; xneighs ++)
{
for(int yneighs = y-neighborhood.height/2; yneighs <= y+neighborhood.height/2 ; yneighs ++)
{
if(!(x==xneighs && y==yneighs))
{
maxRed=Math.max(target.getColor(xneighs,yneighs).getRed(), maxRed);
maxGreen=Math.max(target.getColor(xneighs,yneighs).getGreen(), maxGreen);
maxBlue=Math.max(target.getColor(xneighs,yneighs).getBlue(), maxBlue);
minRed=Math.min(target.getColor(xneighs,yneighs).getRed(),minRed);
minGreen=Math.min(target.getColor(xneighs,yneighs).getGreen(),minGreen);
minBlue=Math.min(target.getColor(xneighs,yneighs).getBlue(),minBlue);
}
}
}
if(!(maxRed==minRed || maxBlue==minBlue || maxGreen==minGreen))
{
newRed = (target.getColor(x,y).getRed()-minRed)*255/(maxRed-minRed);
newGreen = (target.getColor(x,y).getGreen()-minGreen)*255/(maxGreen-minGreen);
newBlue = (target.getColor(x,y).getBlue()-minBlue)*255/(maxBlue-minBlue);
}
else
{
newRed = target.getColor(x,y).getRed()/2;
newGreen = target.getColor(x,y).getGreen()/2;
newBlue = target.getColor(x,y).getBlue()/2;
}
target.setColor(x,y,new Color(newRed,newGreen,newBlue));
}
}