Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import static java.lang.Math.pow;
- /**
- * Created by Emix on 16/03/2017.
- */
- public class Atv {
- public int pallete(int pos){
- int[] pallete64 = {
- 0x000000, 0x00AA00, 0x0000AA, 0x00AAAA, 0xAA0000, 0xAA00AA, 0xAAAA00, 0xAAAAAA,
- 0x000055, 0x0000FF, 0x00AA55, 0x00AAFF, 0xAA0055, 0xAA00FF, 0xAAAA55, 0xAAAAFF,
- 0x005500, 0x0055AA, 0x00FF00, 0x00FFAA, 0xAA5500, 0xAA55AA, 0xAAFF00, 0xAAFFAA,
- 0x005555, 0x0055FF, 0x00FF55, 0x00FFFF, 0xAA5555, 0xAA55FF, 0xAAFF55, 0xAAFFFF,
- 0x550000, 0x5500AA, 0x55AA00, 0x55AAAA, 0xFF0000, 0xFF00AA, 0xFFAA00, 0xFFAAAA,
- 0x550055, 0x5500FF, 0x55AA55, 0x55AAFF, 0xFF0055, 0xFF00FF, 0xFFAA55, 0xFFAAFF,
- 0x555500, 0x5555AA, 0x55FF00, 0x55FFAA, 0xFF5500, 0xFF55AA, 0xFFFF00, 0xFFFFAA,
- 0x555555, 0x5555FF, 0x55FF55, 0x55FFFF, 0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF
- };
- return pallete64[pos];
- }
- public Color getPalleteRGB(int pos){
- int pallete64 = pallete(pos);
- return new Color (pallete64);
- }
- public double distance (Color pixel0, Color pixel1){
- double dist = Math.sqrt(
- pow((pixel0.getRed()-pixel1.getRed()),2)+
- pow((pixel0.getGreen()-pixel1.getGreen()),2)+
- pow((pixel0.getBlue()-pixel1.getBlue()),2));
- return dist;
- }
- public int saturate (int value)
- {
- if (value > 255)
- return 255;
- else if (value < 0)
- return 0;
- else
- return value;
- }
- public Color diferencePixels(int x, int y, BufferedImage newImage, int red, int gre, int blu, int value)
- {
- Color old = new Color(newImage.getRGB(x,y));
- int r = saturate(old.getRed() + red * value / 16);
- int g = saturate(old.getGreen() + gre * value / 16);
- int b = saturate(old.getBlue() + blu * value / 16);
- Color out = new Color(r, g, b);
- return out;
- }
- public BufferedImage compare (BufferedImage img){
- BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
- for (int y = 0; y < out.getHeight(); y++)
- {
- for (int x =0; x < out.getWidth(); x++)
- {
- Color pixel = new Color(img.getRGB(x,y));
- double dist = 255;
- int a = 0;
- for(int i = 0; i < 64; i++) {
- if (distance(pixel, getPalleteRGB(i)) < dist )
- {
- a = i;
- }
- }
- Color newPixel = getPalleteRGB(a);
- int r = pixel.getRed() - newPixel.getRed();
- int g = pixel.getGreen() - newPixel.getGreen();
- int b = pixel.getBlue() - newPixel.getBlue();
- if (x+1 >= 0 && x+1 < img.getWidth()) {
- img.setRGB(x + 1, y, diferencePixels(x + 1, y, img, r,g,b, 7).getRGB());
- }
- if (x-1 >= 0 && x+1 < img.getWidth() && y+1 >= 0 && y+1 < img.getHeight()) {
- img.setRGB(x - 1, y + 1, diferencePixels(x - 1, y + 1, img, r,g,b, 3).getRGB());
- }
- if (y+1 < img.getHeight()) {
- img.setRGB(x, y + 1, diferencePixels(x, y + 1, img, r,g,b, 5).getRGB());
- }
- if(x+1 < img.getWidth() && y+1 < img.getHeight()) {
- img.setRGB(x + 1, y + 1, diferencePixels(x + 1, y + 1, img, r,g,b, 1).getRGB());
- }
- out.setRGB(x,y, newPixel.getRGB());
- }
- }
- return out;
- }
- public void run () throws IOException {
- File PATH = new File ("C:\\Users\\Emix\\Desktop\\Faculdade\\2ºano\\Programação de Jogos 3D\\img\\cor");
- BufferedImage puppy = ImageIO.read(new File(PATH, "puppy.png"));
- BufferedImage EGAPuppy = compare(puppy);
- ImageIO.write(EGAPuppy, "png", new File(PATH, "EGAPuppys.png"));
- }
- public static void main (String[] args) throws IOException {
- Atv atividade = new Atv();
- atividade.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement