package filter.basic;

import filter.Filter;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by Alexandre on 24/04/2016.
 */
public class GrayScale implements Filter
{
    @Override
    public BufferedImage perform(BufferedImage img)
    {
        BufferedImage res = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
        int w = img.getWidth();
        int h = img.getHeight();
        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                Color c = new Color(img.getRGB(i, j));
                Double scale = 0.21 * c.getRed() + 0.72 * c.getGreen() + 0.07 * c.getBlue();
                int scaleInt = scale.intValue();
                res.setRGB(i, j, new Color(scaleInt, scaleInt, scaleInt).getRGB());
            }
        }
        return res;
    }

    @Override
    public String getName()
    {
        return "Gray Scale";
    }
}
