Advertisement
abdulfatirs

Grayscaler.java | Gray-scaling images in Java

May 2nd, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.io.File;
  2. import javax.imageio.ImageIO;
  3. import java.awt.image.BufferedImage;
  4. import java.io.IOException;
  5. import java.awt.Color;
  6. import static java.lang.System.out;
  7. public class Grayscaler
  8. {
  9.     public static String ImageName="raw.png";
  10.    
  11.     public static void main(String args[])throws IOException
  12.     {
  13.         // Image objects to store all the types of gray-scaled images
  14.         BufferedImage image,averaged,luma,desaturated,decoMax,decoMin,channelR,channelG,channelB;
  15.        
  16.         image = ImageIO.read(new File(ImageName));
  17.         int WIDTH = image.getWidth();
  18.         int HEIGHT = image.getHeight();
  19.        
  20.         averaged = new BufferedImage(WIDTH,HEIGHT,image.getType());
  21.         luma = new BufferedImage(WIDTH,HEIGHT,image.getType());
  22.         decoMax = new BufferedImage(WIDTH,HEIGHT,image.getType());
  23.         decoMin = new BufferedImage(WIDTH,HEIGHT,image.getType());
  24.         desaturated = new BufferedImage(WIDTH,HEIGHT,image.getType());
  25.         channelR = new BufferedImage(WIDTH,HEIGHT,image.getType());
  26.         channelG = new BufferedImage(WIDTH,HEIGHT,image.getType());
  27.         channelB = new BufferedImage(WIDTH,HEIGHT,image.getType());
  28.        
  29.         // Looping through each pixel
  30.         for(int y=0;y<HEIGHT;y++)
  31.         {
  32.             for(int x=0;x<WIDTH;x++)
  33.             {
  34.                 int RGB = image.getRGB(x,y);
  35.                 int R = (RGB >> 16) & 0xff; // Red Value
  36.                 int G = (RGB >> 8) & 0xff;  // Green Value
  37.                 int B = (RGB) & 0xff;       // Blue Value
  38.                
  39.                 // Getting the max of RGB
  40.                 int MAX = Math.max(R,G);
  41.                 MAX = Math.max(MAX,B);
  42.                
  43.                 // Getting the min of RGB
  44.                 int MIN = Math.min(R,G);
  45.                 MIN = Math.min(MIN,B);
  46.                
  47.                 // Averaging
  48.                 int GRAY = (R+G+B)/3;
  49.                 Color color = new Color(GRAY,GRAY,GRAY);
  50.                 averaged.setRGB(x,y,color.getRGB());
  51.                
  52.                 // Human Eye Correction
  53.                 GRAY = (int)(R*0.3f + G*0.59f + B*0.11f);
  54.                 color = new Color(GRAY,GRAY,GRAY);
  55.                 luma.setRGB(x,y,color.getRGB());
  56.                
  57.                 // Desaturation
  58.                 GRAY = (MAX + MIN)/2;
  59.                 color = new Color(GRAY,GRAY,GRAY);
  60.                 desaturated.setRGB(x,y,color.getRGB());
  61.                
  62.                 // Decomposition - MAX
  63.                 color = new Color(MAX,MAX,MAX);
  64.                 decoMax.setRGB(x,y,color.getRGB());
  65.                
  66.                 // Decomposition - MIN
  67.                 color = new Color(MIN,MIN,MIN);
  68.                 decoMin.setRGB(x,y,color.getRGB());
  69.                
  70.                 // Single Channel Gray-Scaling - Red
  71.                 color = new Color(R,R,R);
  72.                 channelR.setRGB(x,y,color.getRGB());
  73.                
  74.                 // Single Channel Gray-Scaling - Green
  75.                 color = new Color(G,G,G);
  76.                 channelG.setRGB(x,y,color.getRGB());
  77.                
  78.                 // Single Channel Gray-Scaling - Blue
  79.                 color = new Color(B,B,B);
  80.                 channelB.setRGB(x,y,color.getRGB());
  81.             }
  82.         }
  83.        
  84.         // Saving the Images
  85.         ImageIO.write(averaged,"PNG",new File("averaged.png"));
  86.         ImageIO.write(luma,"PNG",new File("luma.png"));
  87.         ImageIO.write(desaturated,"PNG",new File("desaturated.png"));
  88.         ImageIO.write(decoMax,"PNG",new File("decoMax.png"));
  89.         ImageIO.write(decoMin,"PNG",new File("decoMin.png"));
  90.         ImageIO.write(channelB,"PNG",new File("ChannelB.png"));
  91.         ImageIO.write(channelG,"PNG",new File("ChannelG.png"));
  92.         ImageIO.write(channelR,"PNG",new File("ChannelR.png"));
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement