Saturday, 23 July 2016

Grayscaling An Image One At A Time -- Java Programming: Solving Problems with Software


/**
 * Write a description of GrayScaleConverter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import edu.duke.*;
public class GrayScaleConverter {
    //started with the image i wanted(inImage)
    public ImageResource makeGray(ImageResource inImage){
    //I Made a blank image of the same size
        ImageResource outImage = new ImageResource(inImage.getWidth(),inImage.getHeight());
    //for each pixelin outImage
        for(Pixel pixel : outImage.pixels()){
    //look at the corresponding pixel in inImage
            Pixel inPixel = inImage.getPixel(pixel.getX(),pixel.getY());
    //compute inPixel's red + inPixel's blue + inpixel's green
    //divide that sum by 3(call it average)
    int average = (inPixel.getRed()+inPixel.getGreen()+inPixel.getBlue())/3;
    //set pixel's red to average
    pixel.setRed(average);
    //set pixel's green to average
    pixel.setGreen(average);
    //set pixel's blur to average
    pixel.setBlue(average);
    }
     return outImage;
    }   
    public void testgray(){
        ImageResource ir = new ImageResource();
        ImageResource gray = makeGray(ir);
        gray.draw();                        
    }  
}

No comments:

Post a Comment