/* RedGreenBlue * * Converts image to greyscale and red,green,blue variants * * % javac RedGreenBlue.java Luminance.java Picture.java * * % java RedGreenBlue mandrill.jpg * //YIELDS 4 different picture objects * * * http://introcs.cs.princeton.edu/java/31datatype/Luminance.java * http://introcs.cs.princeton.edu/java/31datatype/Picture.java * http://programmersheaven.com/discussion/360618/how-to-make-java-wait * @author * Dan Latham * * * @version 0.0.1 * */ public class RedGreenBlue { public static void wait (int n) { long t0,t1; t0 = System.currentTimeMillis(); do { t1=System.currentTimeMillis(); } while (t1-t0<1000); } public static Picture redscale(Picture picture) { int y0 = picture.height(); int x0 = picture.width(); for (int x = 0; x < x0; x+=1) { for (int y = 0; y < y0; y+=1) { java.awt.Color initcolor = picture.get(x,y); int r = 255; int b = initcolor.getBlue(); int g = initcolor.getGreen(); java.awt.Color newcolor = new java.awt.Color(r,g,b); picture.set(x, y, newcolor); } } return picture; } public static Picture greenscale(Picture picture) { int y0 = picture.height(); int x0 = picture.width(); for (int x = 0; x < x0; x+=1) { for (int y = 0; y < y0; y+=1) { java.awt.Color initcolor = picture.get(x,y); int r = initcolor.getRed(); int b = initcolor.getBlue(); int g = 255; java.awt.Color newcolor = new java.awt.Color(r,g,b); picture.set(x, y, newcolor); } } return picture; } public static Picture bluescale(Picture picture) { int y0 = picture.height(); int x0 = picture.width(); for (int x = 0; x < x0; x+=1) { for (int y = 0; y < y0; y+=1) { java.awt.Color initcolor = picture.get(x,y); int r = initcolor.getRed(); int b = 255; int g = initcolor.getGreen(); java.awt.Color newcolor = new java.awt.Color(r,g,b); picture.set(x, y, newcolor); } } return picture; } public static Picture grayscale(Picture picture) { int y0 = picture.height(); int x0 = picture.width(); //get color of each pixel, convert to grayscale, set color to gray for (int x = 0; x < x0; x+=1) { for (int y = 0; y < y0; y+=1) { java.awt.Color initcolor = picture.get(x,y); java.awt.Color graycolor = Luminance.toGray(initcolor); int r = graycolor.getRed(); int b = graycolor.getBlue(); int g = graycolor.getGreen(); java.awt.Color newcolor = new java.awt.Color(r,g,b); picture.set(x, y, newcolor); } } return picture; } public static void main(String[] args) { String pic = args[0]; //Path to PICTURE Picture picture = new Picture(pic); //Create new Picture object //picture.show(); picture = grayscale(picture); //iterates over all pixels in file, shows resulting image picture.show(); wait(0); Picture redpicture = new Picture(picture); Picture greenpicture = new Picture(picture); Picture bluepicture = new Picture(picture); redpicture = redscale(redpicture); redpicture.show(); wait(0); greenpicture = greenscale(greenpicture); greenpicture.show(); wait(0); bluepicture = bluescale(bluepicture); bluepicture.show(); wait(0); } }