Skip to content
Snippets Groups Projects
Commit 974251fd authored by SAEZ Theo's avatar SAEZ Theo
Browse files

Ajout de la classe Pixelate pour appliquer un effet de pixelisation sur les...

Ajout de la classe Pixelate pour appliquer un effet de pixelisation sur les images et mise à jour de la classe Display pour l'utiliser. Ajout de tests unitaires pour vérifier le comportement de la classe Pixelate. (Ne gère pas une partie de l'image car la taille de l'image n'est pas un multiple de this.newPixelSize)
parent f90d9865
No related branches found
No related tags found
No related merge requests found
Pipeline #48840 passed
Showing
with 68 additions and 1 deletion
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
File added
No preview for this file type
File deleted
File added
No preview for this file type
File added
No preview for this file type
...@@ -21,7 +21,7 @@ public class Display implements Initializable { ...@@ -21,7 +21,7 @@ public class Display implements Initializable {
this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm"); this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm");
transform transformation = new Outline(0.025); transform transformation = new Pixelate(10);
transformation.applyTo(this.image); transformation.applyTo(this.image);
......
import java.util.ArrayList;
import java.util.List;
public class Pixelate implements transform {
private int newPixelSize;
public Pixelate(int newPixelSize) {
this.newPixelSize = newPixelSize;
}
public void applyTo(GrayImage image) {
GrayColor newColor = new ByteGrayColor();
for (int i = 0; i < image.getWidth() - this.newPixelSize; i += this.newPixelSize) {
for (int j = 0; j < image.getHeight() - this.newPixelSize; j += this.newPixelSize) {
newColor = averageColor(image, i, j);
for (int square1 = 0; square1 < this.newPixelSize; square1++)
for (int square2 = 0; square2 < this.newPixelSize; square2++)
image.setPixel(newColor, i+square1, j+square2);
}
}
}
public GrayColor averageColor(GrayImage image, int x, int y) {
double average = 0;
for (int i = 0; i < this.newPixelSize; i++) {
for (int j = 0; j < this.newPixelSize; j++) {
average += image.getPixelGrayColor(x + i, y + j).getLuminosity();
}
}
average /= Math.pow(this.newPixelSize, 2);
return new ByteGrayColor(average);
}
}
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class PixelateTest {
@Test
public void testAverage(){
GrayImage image = new MatrixGrayImage(2, 2);
GrayColor color1 = new ByteGrayColor(1.);
GrayColor color2 = new ByteGrayColor(1.);
GrayColor color3 = new ByteGrayColor(0.);
GrayColor color4 = new ByteGrayColor(0.);
image.setPixel(color1, 0, 0);
image.setPixel(color2, 1, 0);
image.setPixel(color3, 0, 1);
image.setPixel(color4, 1, 1);
Pixelate pixelate = new Pixelate(2);
assertThat(pixelate.averageColor(image, 0, 0).getLuminosity()).isEqualTo(new ByteGrayColor(0.5).getLuminosity());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment