import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ /** * * @author alum-01 */ public class ExempleBase { //Aquesta funció desa l'imatge. public static void escriureImatge(String ruta, BufferedImage image) { try { File output_file = new File(ruta); ImageIO.write(image, "jpg", output_file); } catch (IOException e) { System.out.println("Error: " + e); } } //Aquesta funció agafa un array de 4 valors i retorna un int amb un color. public static int setPixelARGB(int[] pARGB) { int pixel = 0; pixel = (pARGB[0] << 24) | (pARGB[1] << 16) | (pARGB[2] << 8) | (pARGB[3]); return pixel; } //Aquesta funció agafa un int i retorna un array de 4 valors (ARGB) public static int[] getPixelARGB(int pixel) { int[] pARGB = new int[4]; pARGB[0] = (pixel >> 24) & 0xff; pARGB[1] = (pixel >> 16) & 0xff; pARGB[2] = (pixel >> 8) & 0xff; pARGB[3] = (pixel) & 0xff; return pARGB; } //Aquesta funció retorna el color del pixel processat pel filtre de blur public static int processaPixel(int matriu, int x, int y, BufferedImage image, int width, int height) { //Preparem el num pel qual dividirem tots els valors. int num_caselles = matriu * matriu; //Preparem les variables que tindran les mitjanes finals. int mitjana_alpha = 0; int mitjana_red = 0; int mitjana_green = 0; int mitjana_blue = 0; //Doble bucle for per recorrer les posicions de la matriu. for (int i = x - matriu / 2; i <= x + matriu / 2; i++) { for (int j = y - matriu / 2; j <= y + matriu / 2; j++) { int pos_x = i; int pos_y = j; //Aquests ifs són pel reflex. if (i < 0) { pos_x *= -1; } if (j < 0) { pos_y *= -1; } if (i >= width) { pos_x -= matriu; } if (j >= height) { pos_y -= matriu; } //Agafem els valors del pixel de la posició de la matriu int[] valors = getPixelARGB(image.getRGB(pos_x, pos_y)); //Afegim els valors del pixel dividit pel nombre de caselles als valors finals. mitjana_alpha += valors[0] / num_caselles; mitjana_red += valors[1] / num_caselles; mitjana_green += valors[2] / num_caselles; mitjana_blue += valors[3] / num_caselles; } } //Fem una variable per enviar-la a la funció setPixelARGB int[] pixel_resultant = new int[4]; pixel_resultant[0] = mitjana_alpha; pixel_resultant[1] = mitjana_red; pixel_resultant[2] = mitjana_green; pixel_resultant[3] = mitjana_blue; return setPixelARGB(pixel_resultant); } /* image és la imatge sobre la qual aplicarem el blur. matriu és un nombre enter que especifica la mida de la matriu. Cal tenir en compte que aquest codi no aplica un filtre com a tal, però la operació és equivalent Això és per estalviar espai de memòria, per no haver de declarar múltiples matrius de 20x20 amb el mateix valor a tots els seus espais. Si vulguessim aplicar un filtre de debó, caldria fer-ho amb una matriu. */ public static BufferedImage filtreBlur(BufferedImage image, int matriu) { //Doble bucle senzill per aplicar el filtre blur a tots els pixels de l'imatge. for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { image.setRGB(i, j, processaPixel(matriu, i, j, image, image.getWidth(), image.getHeight())); } } return image; } }