diff --git a/tp4/.gitignore b/tp4/.gitignore index c9480640b854a9661e53f6f6576db1b1aa18ad1f..8a8da1dd576ddd5b66f19aa85129a4298078d5c5 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 ee68aa3f99f55e42dc091bffeb02824bfa0867e6..8f1ff25683ef7b1c10ae0ab3d3e41e4ae8527952 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 0000000000000000000000000000000000000000..007ead46db0f7eefed84f043508272f40445dcf0 --- /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 c790c1273a322510f1f4db1f41b0877cf14da6ae..54e5f10ca2f610c126d3c8108242dd3b50d223fd 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 0f1483d193b6c6a35ab403cf7a07d30e0e01e648..fa3bce925f8ebc7bbc403af9ff4030b496808a5f 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 d303cb401a0a7bb00ef51bb86f8766809f30e684..57abdcbda5952bbab15cdc8c63bb58c9f8f4a71a 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 fcb770fbea180c0bc871b960f8a2d56483bf127c..1097647f3ca282cd7183856d236b6082f56df58a 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 8f92861c29535a99c40dc12ed42dd6f6a71643cb..0274ac00389608613b646a3c43eeb062125af1f7 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 0000000000000000000000000000000000000000..d15fae37a971d0ee871d9902cfe0fb4b1328d599 --- /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); + } + } + + } + } + } +}