From b226473d932f85086c3dfa99e93573238e3c4db1 Mon Sep 17 00:00:00 2001 From: Teo Blaise Kaplanski <teobk@macbook-air-de-teo.home> Date: Sun, 11 Oct 2020 21:23:02 +0200 Subject: [PATCH] fin --- tp4/.gitignore | 3 +++ tp4/ByteGrayColor.java | 17 +++++++++++------ tp4/DecreaseGrayLevels.java | 18 ++++++++++++++++++ tp4/GrayColor.java | 9 +++------ tp4/GrayImage.java | 8 ++++++++ tp4/Invert.java | 19 +++++++++---------- tp4/Main.java | 8 ++++++++ tp4/MatrixGrayImage.java | 22 ++++++++++------------ tp4/Pixelate.java | 28 ++++++++++++++++++++++++++++ 9 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 tp4/DecreaseGrayLevels.java create mode 100644 tp4/Pixelate.java diff --git a/tp4/.gitignore b/tp4/.gitignore index c948064..8a8da1d 100644 --- a/tp4/.gitignore +++ b/tp4/.gitignore @@ -7,3 +7,6 @@ /MatrixGrayImage.class /Transform.class /Invert.class +/DecreaseGrayLevels.class +/pixelate.class +/Pixelate.class diff --git a/tp4/ByteGrayColor.java b/tp4/ByteGrayColor.java index ee68aa3..8f1ff25 100755 --- a/tp4/ByteGrayColor.java +++ b/tp4/ByteGrayColor.java @@ -38,7 +38,7 @@ public class ByteGrayColor implements GrayColor { @Override public double getLuminosity() { // TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc) - return 0; + return (double)this.grayLevel/MAXIMUM_GRAY_LEVEL; } @Override @@ -62,10 +62,15 @@ public class ByteGrayColor implements GrayColor { return this.compareTo(color) == 0; } - @Override - public GrayColor invert() { - // TODO Auto-generated method stub - return null; - } + @Override + public GrayColor invert() { + return new ByteGrayColor(MAXIMUM_GRAY_LEVEL - grayLevel); + } + + public GrayColor decreaseGrayLevel(int nbGrayLevels){ + return new ByteGrayColor((Math.floor(grayLevel/255.0 * (nbGrayLevels-1)) * MAXIMUM_GRAY_LEVEL / (nbGrayLevels-1))); + } + + } diff --git a/tp4/DecreaseGrayLevels.java b/tp4/DecreaseGrayLevels.java new file mode 100644 index 0000000..007ead4 --- /dev/null +++ b/tp4/DecreaseGrayLevels.java @@ -0,0 +1,18 @@ +public class DecreaseGrayLevels implements Transform { + int nbGrayLevel; + DecreaseGrayLevels(int nbGrayLevel){ + this.nbGrayLevel = nbGrayLevel; + + } + + @Override + public void applyTo(GrayImage image) { + for (int x = 0; x < image.getWidth(); ++x){ + for(int y = 0; y < image.getHeight(); ++y){ + image.setPixel(image.getPixelGrayColor(x, y).decreaseGrayLevel(nbGrayLevel), x, y); + + } + } + } + +} diff --git a/tp4/GrayColor.java b/tp4/GrayColor.java index c790c12..54e5f10 100755 --- a/tp4/GrayColor.java +++ b/tp4/GrayColor.java @@ -8,11 +8,8 @@ public interface GrayColor extends Comparable<GrayColor> { int getGrayLevel(); double getLuminosity(); Color getColor(); - - // Add methode Invert() -public GrayColor invert() { - - } -} + GrayColor invert(); + GrayColor decreaseGrayLevel(int nbGrayLevels); +} diff --git a/tp4/GrayImage.java b/tp4/GrayImage.java index 0f1483d..fa3bce9 100755 --- a/tp4/GrayImage.java +++ b/tp4/GrayImage.java @@ -5,4 +5,12 @@ public interface GrayImage extends Image { void setPixel(GrayColor gray, int x, int y); GrayColor getPixelGrayColor(int x, int y); + GrayColor getDominantGray(int squareX, int squareY, int squareSize); + default int dominantGray(int squareX, int squareY, int squareSize) { + int sumOfGrayLevels = 0; + for (int x = squareX; x < squareX+squareSize; ++x) + for (int y = squareY; y < squareY+squareSize; ++y) + sumOfGrayLevels += getPixelGrayColor(x, y).getGrayLevel(); + return (sumOfGrayLevels / (squareSize*squareSize)); + } } diff --git a/tp4/Invert.java b/tp4/Invert.java index d303cb4..57abdcb 100644 --- a/tp4/Invert.java +++ b/tp4/Invert.java @@ -1,11 +1,10 @@ - public class Invert implements Transform { - public void invert() { - - } - - public void applyTo(GrayImage image) { - - } - -} + @Override + public void applyTo(GrayImage image) { + for (int x = 0; x < image.getWidth(); ++x){ + for(int y = 0; y < image.getHeight(); ++y){ + image.setPixel(image.getPixelGrayColor(x, y).invert(), x, y); + } + } + } +} \ No newline at end of file diff --git a/tp4/Main.java b/tp4/Main.java index fcb770f..1097647 100755 --- a/tp4/Main.java +++ b/tp4/Main.java @@ -15,6 +15,14 @@ public class Main MatrixGrayImage.createImageFromPGMFile("luminy.pgm"); // Add code here; + Invert invert = new Invert(); + DecreaseGrayLevels decreaseGrayLevel = new DecreaseGrayLevels(5); + Pixelate pixelate = new Pixelate(10); + + invert.applyTo(image); + pixelate.applyTo(image); + decreaseGrayLevel.applyTo(image); + display(image); } diff --git a/tp4/MatrixGrayImage.java b/tp4/MatrixGrayImage.java index 8f92861..0274ac0 100755 --- a/tp4/MatrixGrayImage.java +++ b/tp4/MatrixGrayImage.java @@ -17,49 +17,47 @@ public class MatrixGrayImage implements GrayImage { @Override public void setPixel(GrayColor gray, int x, int y) { - // TODO : Compléter la méthode pour modifier le pixel. - pixels[x][y] = gray; + pixels[x][y] = gray; } @Override public GrayColor getPixelGrayColor(int x, int y) { - // TODO : Changer les instructions pour retourner le bon pixel. - int Gcolor = pixels[x][y].getGrayLevel(); - return new ByteGrayColor(Gcolor); + return pixels[x][y]; } @Override public Color getPixelColor(int x, int y) { - // TODO : Changer les instructions pour retourner la couleur du pixel. return pixels[x][y].getColor(); } @Override public int getWidth() { - // TODO : Changer les instructions pour retourner la largeur de l'image. return this.width; } @Override public int getHeight() { - // TODO : Changer les instructions pour retourner la hauteur de l'image. return this.height; } public MatrixGrayImage(int width, int height){ - /* TODO : Modifier les instructions pour initialiser correctement - les propriétés de l'instance. - */ this.width = width; this.height = height; - this.pixels = null; + this.pixels = new GrayColor[width][height]; } public int getPixelByteColor(int x, int y) { return (int)(getPixelGrayColor(x, y).getGrayLevel()*PGM_MAXIMUM_CODE); } + @Override + public GrayColor getDominantGray(int squareX, int squareY, int squareSize) { + return new ByteGrayColor(dominantGray(squareX, squareY, squareSize)); + } + + + public static MatrixGrayImage createImageFromPGMFile(String fileName) { // NE PAS MODIFIER ! InputStream file = ClassLoader.getSystemResourceAsStream(fileName); diff --git a/tp4/Pixelate.java b/tp4/Pixelate.java new file mode 100644 index 0000000..d15fae3 --- /dev/null +++ b/tp4/Pixelate.java @@ -0,0 +1,28 @@ +public class Pixelate implements Transform { + int newPixelSize; + int newSquareX; + int newSquareY; + GrayColor dominantColor; + Pixelate(int newPixelSize){ + this.newPixelSize = newPixelSize; + } + + @Override + public void applyTo(GrayImage image) { + for (int x = 0; x < (image.getWidth()) / newPixelSize; ++x) { + for (int y = 0; y < (image.getHeight()) / newPixelSize; ++y) { + newSquareX = x * newPixelSize; + newSquareY = y * newPixelSize; + dominantColor = image.getDominantGray(newSquareX, newSquareY, newPixelSize); + for (int squareX = 0; squareX < newPixelSize; ++squareX){ + for (int squareY = 0; squareY < newPixelSize; ++squareY ){ + int X = newSquareX + squareX; + int Y = newSquareY + squareY; + image.setPixel(dominantColor, X, Y); + } + } + + } + } + } +} -- GitLab