Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import static java.lang.Math.pow;
  7.  
  8. /**
  9. * Created by Emix on 16/03/2017.
  10. */
  11. public class Atv {
  12.  
  13. public int pallete(int pos){
  14. int[] pallete64 = {
  15. 0x000000, 0x00AA00, 0x0000AA, 0x00AAAA, 0xAA0000, 0xAA00AA, 0xAAAA00, 0xAAAAAA,
  16. 0x000055, 0x0000FF, 0x00AA55, 0x00AAFF, 0xAA0055, 0xAA00FF, 0xAAAA55, 0xAAAAFF,
  17. 0x005500, 0x0055AA, 0x00FF00, 0x00FFAA, 0xAA5500, 0xAA55AA, 0xAAFF00, 0xAAFFAA,
  18. 0x005555, 0x0055FF, 0x00FF55, 0x00FFFF, 0xAA5555, 0xAA55FF, 0xAAFF55, 0xAAFFFF,
  19. 0x550000, 0x5500AA, 0x55AA00, 0x55AAAA, 0xFF0000, 0xFF00AA, 0xFFAA00, 0xFFAAAA,
  20. 0x550055, 0x5500FF, 0x55AA55, 0x55AAFF, 0xFF0055, 0xFF00FF, 0xFFAA55, 0xFFAAFF,
  21. 0x555500, 0x5555AA, 0x55FF00, 0x55FFAA, 0xFF5500, 0xFF55AA, 0xFFFF00, 0xFFFFAA,
  22. 0x555555, 0x5555FF, 0x55FF55, 0x55FFFF, 0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF
  23. };
  24. return pallete64[pos];
  25. }
  26.  
  27. public Color getPalleteRGB(int pos){
  28. int pallete64 = pallete(pos);
  29. return new Color (pallete64);
  30. }
  31.  
  32. public double distance (Color pixel0, Color pixel1){
  33. double dist = Math.sqrt(
  34. pow((pixel0.getRed()-pixel1.getRed()),2)+
  35. pow((pixel0.getGreen()-pixel1.getGreen()),2)+
  36. pow((pixel0.getBlue()-pixel1.getBlue()),2));
  37. return dist;
  38. }
  39.  
  40. public int saturate (int value)
  41. {
  42. if (value > 255)
  43. return 255;
  44. else if (value < 0)
  45. return 0;
  46. else
  47. return value;
  48. }
  49.  
  50. public Color diferencePixels(int x, int y, BufferedImage newImage, int red, int gre, int blu, int value)
  51. {
  52. Color old = new Color(newImage.getRGB(x,y));
  53. int r = saturate(old.getRed() + red * value / 16);
  54. int g = saturate(old.getGreen() + gre * value / 16);
  55. int b = saturate(old.getBlue() + blu * value / 16);
  56. Color out = new Color(r, g, b);
  57. return out;
  58. }
  59.  
  60. public BufferedImage compare (BufferedImage img){
  61. BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
  62. for (int y = 0; y < out.getHeight(); y++)
  63. {
  64. for (int x =0; x < out.getWidth(); x++)
  65. {
  66. Color pixel = new Color(img.getRGB(x,y));
  67. double dist = 255;
  68. int a = 0;
  69. for(int i = 0; i < 64; i++) {
  70. if (distance(pixel, getPalleteRGB(i)) < dist )
  71. {
  72. a = i;
  73. }
  74. }
  75. Color newPixel = getPalleteRGB(a);
  76.  
  77. int r = pixel.getRed() - newPixel.getRed();
  78. int g = pixel.getGreen() - newPixel.getGreen();
  79. int b = pixel.getBlue() - newPixel.getBlue();
  80.  
  81. if (x+1 >= 0 && x+1 < img.getWidth()) {
  82. img.setRGB(x + 1, y, diferencePixels(x + 1, y, img, r,g,b, 7).getRGB());
  83. }
  84. if (x-1 >= 0 && x+1 < img.getWidth() && y+1 >= 0 && y+1 < img.getHeight()) {
  85. img.setRGB(x - 1, y + 1, diferencePixels(x - 1, y + 1, img, r,g,b, 3).getRGB());
  86. }
  87. if (y+1 < img.getHeight()) {
  88. img.setRGB(x, y + 1, diferencePixels(x, y + 1, img, r,g,b, 5).getRGB());
  89. }
  90. if(x+1 < img.getWidth() && y+1 < img.getHeight()) {
  91. img.setRGB(x + 1, y + 1, diferencePixels(x + 1, y + 1, img, r,g,b, 1).getRGB());
  92. }
  93. out.setRGB(x,y, newPixel.getRGB());
  94. }
  95. }
  96. return out;
  97. }
  98.  
  99. public void run () throws IOException {
  100. File PATH = new File ("C:\\Users\\Emix\\Desktop\\Faculdade\\2ºano\\Programação de Jogos 3D\\img\\cor");
  101. BufferedImage puppy = ImageIO.read(new File(PATH, "puppy.png"));
  102. BufferedImage EGAPuppy = compare(puppy);
  103. ImageIO.write(EGAPuppy, "png", new File(PATH, "EGAPuppys.png"));
  104. }
  105.  
  106. public static void main (String[] args) throws IOException {
  107. Atv atividade = new Atv();
  108. atividade.run();
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement