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

Correction du fait qu'on ne prenait pas l'image en entièreté

parent 974251fd
No related branches found
No related tags found
No related merge requests found
Pipeline #48841 failed
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
No preview for this file type
......@@ -2,39 +2,51 @@ import java.util.ArrayList;
import java.util.List;
public class Pixelate implements transform {
private int newPixelSize;
private final int newPixelSize;
public Pixelate(int newPixelSize) {
this.newPixelSize = newPixelSize;
}
@Override
public void applyTo(GrayImage image) {
int width = image.getWidth();
int height = image.getHeight();
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);
// On parcourt l’image par blocs de taille newPixelSize
for (int x = 0; x < width; x += newPixelSize) {
for (int y = 0; y < height; y += newPixelSize) {
// Calcul de la couleur moyenne pour le bloc courant
GrayColor average = averageColor(image, x, y, width, height);
// Application de la couleur moyenne à tous les pixels du bloc
for (int i = x; i < Math.min(x + newPixelSize, width); i++) {
for (int j = y; j < Math.min(y + newPixelSize, height); j++) {
image.setPixel(average, i, j);
}
}
}
}
}
public GrayColor averageColor(GrayImage image, int x, int y) {
double average = 0;
/**
* Calcule la couleur moyenne d’un bloc de taille newPixelSize à partir de la position (startX, startY).
* On s’assure de ne pas dépasser les bords de l’image.
*/
private GrayColor averageColor(GrayImage image, int startX, int startY, int width, int height) {
double sum = 0.0;
int count = 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();
for (int i = startX; i < Math.min(startX + newPixelSize, width); i++) {
for (int j = startY; j < Math.min(startY + newPixelSize, height); j++) {
sum += image.getPixelGrayColor(i, j).getLuminosity();
count++;
}
}
average /= Math.pow(this.newPixelSize, 2);
double average = (count == 0) ? 0.0 : sum / count; // average = sun / count sauf si count = 0, dans ce cas average = 0 (enlève erreur DivisionByZero)
return new ByteGrayColor(average);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment