This site requires JavaScript, please enable it in your browser!
Greenfoot back
ironphoenix20
ironphoenix20 wrote ...

2017/5/8

Emboss Image new

1
2
3
ironphoenix20 ironphoenix20

2017/5/8

#
Hi. I am trying to emboss an image and for some reason, this method is removing the image rather than doing the desired effect on it. Can you see what could be wrong and how I could fix it? Emboss method:
public static void emboss(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();

        BufferedImage dst;
        dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++) {
                int upperLeft = 0;
                int lowerRight = 0;

                if (i > 0 && j > 0)
                {
                    upperLeft = bi.getRGB(j - 1, i - 1);
                }

                if (i < height - 1 && j < width - 1)
                {
                    lowerRight = bi.getRGB(j + 1, i + 1);
                }

                int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);

                int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);

                int blueDiff = (lowerRight & 255) - (upperLeft & 255);

                int diff = redDiff;
                if (Math.abs(greenDiff) > Math.abs(diff))
                {
                    diff = greenDiff;
                }

                if (Math.abs(blueDiff) > Math.abs(diff))
                {
                    diff = blueDiff;
                }

                int grayColor = 128 + diff;

                if (grayColor > 255)
                {
                    grayColor = 255;
                }
                else if (grayColor < 0)
                {
                    grayColor = 0;
                }

                int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;

                bi.setRGB(j, i, newColor);
            }
        }
    }
danpost danpost

2017/5/8

#
I do not know why you would want to work at the BufferedImage level when you can work at the GreenfootImage level. The following works on a GreenfootImage object:
public static void emboss(GreenfootImage image)
{
    int width = image.getWidth();
    int height = image.getHeight();
    GreenfootImage copy = new GreenfootImage(image);
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {
            int upperLeft = 0;
            int lowerRight = 0;
            if (i > 0 && j > 0) upperLeft = copy.getColorAt(j-1, i-1).getRGB();
            if (i < height - 1 && j < width - 1) lowerRight = copy.getColorAt(j+1, i+1).getRGB();
            int redDiff = ((lowerRight >> 16) & 255)-((upperLeft >> 16) & 255);
            int greenDiff = ((lowerRight >> 8) & 255)-((upperLeft >> 8) & 255);
            int blueDiff = (lowerRight & 255) - (upperLeft & 255);
            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff)) diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff)) diff = blueDiff;
            int grayColor = 128+diff;
            if (grayColor > 255) grayColor = 255; else if (grayColor < 0) grayColor = 0;
            int newColor = (grayColor << 16)+(grayColor << 8)+grayColor;
            image.setColorAt(j, i, new Color(newColor));
        }
    }
}
ironphoenix20 ironphoenix20

2017/5/8

#
Thank you very much. Ill try this out and get back to you. By the way, did you create this method or did you get it from somewhere else? I need to give credit to anyone who's methods I'm using, that's why.
ironphoenix20 ironphoenix20

2017/5/8

#
Oh, wait. You just manipulated the method I gave, right?
danpost danpost

2017/5/8

#
ironphoenix20 wrote...
Oh, wait. You just manipulated the method I gave, right?
I just made some modifications to it. Lines 1, 5, 12, 13 and 23 were modified lines.
ironphoenix20 ironphoenix20

2017/5/8

#
The getRGB() method on lines 12 and 13 do not seem to work. Do I need to write that method? Or import something?
danpost danpost

2017/5/8

#
ironphoenix20 wrote...
The getRGB() method on lines 12 and 13 do not seem to work. Do I need to write that method? Or import something?
Are you importing 'java.awt.Color' or does a Color class come with your version of greenfoot?
ironphoenix20 ironphoenix20

2017/5/8

#
A color class comes with my version (which is the latest version).
danpost danpost

2017/5/8

#
ironphoenix20 wrote...
A color class comes with my version (which is the latest version).
Okay, then some adjustments are needed. The greenfoot.Color class does not have the full capabilities of the java.awt.Color class. Give me a few to come up with something that will work for you.
ironphoenix20 ironphoenix20

2017/5/8

#
I can get the red, green, and blue values separately then put them into one integer. I have a method for that (that my teacher provided).
ironphoenix20 ironphoenix20

2017/5/8

#
but if you can, post your method as well since im not sure if my method is going to work.
danpost danpost

2017/5/8

#
Okay, try this:
public static void emboss(GreenfootImage image)
{
    int width = image.getWidth();
    int height = image.getHeight();
    GreenfootImage copy = new GreenfootImage(image);
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {
            Color upperLeft = Color.gray;
            Color lowerRight = Color.gray;
            if (i > 0 && j > 0) upperLeft = copy.getColorAt(j-1, i-1);
            if (i < height - 1 && j < width - 1) lowerRight = copy.getColorAt(j+1, i+1);
            int redDiff = lowerRight.getRed()-upperLeft.getRed();
            int greenDiff = lowerRight.getGreen()-upperLeft.getGreen();
            int blueDiff = lowerRight.getBlue()-upperLeft.getBlue();
            int diff = redDiff;
            if (Math.abs(greenDiff) > Math.abs(diff)) diff = greenDiff;
            if (Math.abs(blueDiff) > Math.abs(diff)) diff = blueDiff;
            int grayColor = 128 + diff;
            if (grayColor > 255) grayColor = 255; else if (grayColor < 0) grayColor = 0;
            int newColor = (grayColor << 16)+(grayColor << 8)+grayColor;
            image.setColorAt(j, i, new Color(newColor));
        }
    }
}
Modified lines 10 through 16 to avoid RGB values altogether.
ironphoenix20 ironphoenix20

2017/5/8

#
for some reason, it says a method called Color(int, int, int) does not exist even though it clearly states that it does in the API.
ironphoenix20 ironphoenix20

2017/5/8

#
Line 23 does not work. It says int cannot be converted to java.awt.Color
danpost danpost

2017/5/8

#
ironphoenix20 wrote...
for some reason, it says a method called Color(int, int, int) does not exist even though it clearly states that it does in the API.
I am not sure where you are coming from with this.
ironphoenix20 wrote...
Line 23 does not work. It says int cannot be converted to java.awt.Color
Remove line 22 and change line 23 to this:
image.setColor(j, i, new Color(grayColor, grayColor, grayColor));
There are more replies on the next page.
1
2
3