Negativ.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. public class Negativ {
  7. public static void main(String [] args) {
  8. String outFormat = "png";
  9. File inputPath = new File("img/Taj_Mahal4.png");
  10. File outputPath = new File("img/Taj_Mahal4_grau.png");
  11. BufferedImage original_image = null;
  12. try {
  13. original_image = ImageIO.read(inputPath);
  14. System.out.println("Reading " + inputPath + " complete !"); }
  15. catch (IOException e) {
  16. System.out.println("Error: " + e);
  17. }
  18. int width = original_image.getWidth();
  19. int height = original_image.getHeight();
  20. System.out.println("\n" + "width = " + width + " height = " +
  21. height + "\n");
  22. BufferedImage newImage = new BufferedImage(width, height,
  23. BufferedImage.TYPE_INT_ARGB);
  24. newImage.createGraphics().drawImage(original_image, 0, 0, null);
  25. int p, new_p, a, r, g, b;
  26. for (int y = 0; y < height; y++) {
  27. for (int x = 0; x < width; x++) {
  28. p = original_image.getRGB(x, y);
  29. a = (p >> 24) & 0xFF; r = (p >> 16) & 0xFF;
  30. g = (p >> 8) & 0xFF; b = p & 0xFF;
  31. new_p = (a << 24) | (255 - r << 16) | (255 - g << 8) | 255 - b;
  32. newImage.setRGB(x, y, new_p);
  33. }
  34. }
  35. try {
  36. ImageIO.write(newImage, outFormat, outputPath);
  37. System.out.println("Writing " + outputPath + " complete !"); }
  38. catch(IOException e) {
  39. System.out.println("Error: " + e);
  40. System.exit(1);
  41. }
  42. System.exit(0);
  43. }
  44. }