isefire

RedGreenBlueFromGrayscale

Jul 21st, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. /* RedGreenBlue
  2.  * <Description>
  3.  * Converts image to greyscale and red,green,blue variants
  4.  * <Usage>
  5.  * % javac RedGreenBlue.java Luminance.java Picture.java
  6.  *
  7.  * % java RedGreenBlue mandrill.jpg
  8.  * //YIELDS 4 different picture objects
  9.  *
  10.  * <References>
  11.  * http://introcs.cs.princeton.edu/java/31datatype/Luminance.java
  12.  * http://introcs.cs.princeton.edu/java/31datatype/Picture.java
  13.  * http://programmersheaven.com/discussion/360618/how-to-make-java-wait
  14.  * @author
  15.  * Dan Latham
  16.  *
  17.  * @version 0.0.1
  18.  *
  19.  */
  20. public class RedGreenBlue
  21. {
  22.     public static void wait (int n)
  23.     {
  24.         long t0,t1;
  25.         t0 = System.currentTimeMillis();
  26.         do
  27.         {
  28.             t1=System.currentTimeMillis();
  29.         }
  30.         while (t1-t0<1000);
  31.     }
  32.     public static Picture redscale(Picture picture)
  33.     {
  34.         int y0 = picture.height();
  35.         int x0 = picture.width();
  36.         for (int x = 0; x < x0; x+=1)
  37.         {
  38.             for (int y = 0; y < y0; y+=1)
  39.             {
  40.                 java.awt.Color initcolor = picture.get(x,y);
  41.                 int r = 255;
  42.                 int b = initcolor.getBlue();
  43.                 int g = initcolor.getGreen();
  44.                
  45.                 java.awt.Color newcolor = new java.awt.Color(r,g,b);
  46.                 picture.set(x, y, newcolor);
  47.             }
  48.         }
  49.         return picture;
  50.     }
  51.     public static Picture greenscale(Picture picture)
  52.     {
  53.         int y0 = picture.height();
  54.         int x0 = picture.width();
  55.         for (int x = 0; x < x0; x+=1)
  56.         {
  57.             for (int y = 0; y < y0; y+=1)
  58.             {
  59.                 java.awt.Color initcolor = picture.get(x,y);
  60.                 int r = initcolor.getRed();
  61.                 int b = initcolor.getBlue();
  62.                 int g = 255;
  63.                
  64.                 java.awt.Color newcolor = new java.awt.Color(r,g,b);
  65.                 picture.set(x, y, newcolor);
  66.             }
  67.         }
  68.         return picture;
  69.     }
  70.     public static Picture bluescale(Picture picture)
  71.     {
  72.         int y0 = picture.height();
  73.         int x0 = picture.width();
  74.         for (int x = 0; x < x0; x+=1)
  75.         {
  76.             for (int y = 0; y < y0; y+=1)
  77.             {
  78.                 java.awt.Color initcolor = picture.get(x,y);
  79.                 int r = initcolor.getRed();
  80.                 int b = 255;
  81.                 int g = initcolor.getGreen();
  82.                
  83.                 java.awt.Color newcolor = new java.awt.Color(r,g,b);
  84.                 picture.set(x, y, newcolor);
  85.             }
  86.         }
  87.         return picture;
  88.     }
  89.     public static Picture grayscale(Picture picture)
  90.     {
  91.         int y0 = picture.height();
  92.         int x0 = picture.width();
  93.         //get color of each pixel, convert to grayscale, set color to gray
  94.         for (int x = 0; x < x0; x+=1)
  95.         {
  96.             for (int y = 0; y < y0; y+=1)
  97.             {
  98.                 java.awt.Color initcolor = picture.get(x,y);
  99.                 java.awt.Color graycolor = Luminance.toGray(initcolor);
  100.                 int r = graycolor.getRed();
  101.                 int b = graycolor.getBlue();
  102.                 int g = graycolor.getGreen();
  103.                
  104.                 java.awt.Color newcolor = new java.awt.Color(r,g,b);
  105.                 picture.set(x, y, newcolor);
  106.             }
  107.         }
  108.         return picture;
  109.     }
  110.    
  111.    
  112.     public static void main(String[] args)
  113.     {
  114.         String pic = args[0]; //Path to PICTURE
  115.         Picture picture = new Picture(pic); //Create new Picture object
  116.         //picture.show();
  117.         picture = grayscale(picture); //iterates over all pixels in file, shows resulting image
  118.         picture.show();
  119.         wait(0);
  120.         Picture redpicture = new Picture(picture);
  121.         Picture greenpicture = new Picture(picture);
  122.         Picture bluepicture = new Picture(picture);
  123.         redpicture = redscale(redpicture);
  124.         redpicture.show();
  125.         wait(0);
  126.        
  127.         greenpicture = greenscale(greenpicture);
  128.         greenpicture.show();
  129.         wait(0);
  130.        
  131.         bluepicture = bluescale(bluepicture);
  132.         bluepicture.show();
  133.         wait(0);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment