Advertisement
abdulfatirs

SepiaNegativeToner.java | Sepia-Negative Effect in Java

May 6th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 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 SepiaNegativeToner
  8. {
  9.     public static String ImageName="raw.png";
  10.    
  11.     public static void main(String args[])throws IOException
  12.     {
  13.         BufferedImage image,sepia,negative;
  14.        
  15.         image = ImageIO.read(new File(ImageName));
  16.         int WIDTH = image.getWidth();
  17.         int HEIGHT = image.getHeight();
  18.        
  19.         sepia = new BufferedImage(WIDTH,HEIGHT,image.getType());
  20.         negative = new BufferedImage(WIDTH,HEIGHT,image.getType());
  21.        
  22.         // Looping through each pixel
  23.         for(int y=0;y<HEIGHT;y++)
  24.         {
  25.             for(int x=0;x<WIDTH;x++)
  26.             {
  27.                 int RGB = image.getRGB(x,y);
  28.                 int R = (RGB >> 16) & 0xff; // Red Value
  29.                 int G = (RGB >> 8) & 0xff;  // Green Value
  30.                 int B = (RGB) & 0xff;       // Blue Value
  31.                
  32.                 // Output RGB values for Sepia
  33.                 int outputRed = (int)((R * .393) + (G *.769) + (B * .189));
  34.                 int outputGreen = (int)((R * .349) + (G *.686) + (B * .168));
  35.                 int outputBlue = (int)((R * .272) + (G *.534) + (B * .131));
  36.                
  37.                 outputRed = Math.min(outputRed,255);
  38.                 outputGreen = Math.min(outputGreen,255);
  39.                 outputBlue = Math.min(outputBlue,255);
  40.                 sepia.setRGB(x,y, new Color(outputRed,outputGreen,outputBlue).getRGB());
  41.                
  42.                 // Making the negative pixel
  43.                 Color complementary = new Color(255-R,255-G,255-B);
  44.                 negative.setRGB(x,y, complementary.getRGB());
  45.             }
  46.         }
  47.        
  48.         // Saving the Images
  49.         ImageIO.write(sepia,"PNG",new File("sepia.png"));
  50.         ImageIO.write(negative,"PNG",new File("negative.png"));
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement