Skip to content
Snippets Groups Projects
Commit b226473d authored by Teo Blaise Kaplanski's avatar Teo Blaise Kaplanski
Browse files

fin

parent 2f81946c
No related branches found
No related tags found
No related merge requests found
...@@ -7,3 +7,6 @@ ...@@ -7,3 +7,6 @@
/MatrixGrayImage.class /MatrixGrayImage.class
/Transform.class /Transform.class
/Invert.class /Invert.class
/DecreaseGrayLevels.class
/pixelate.class
/Pixelate.class
...@@ -38,7 +38,7 @@ public class ByteGrayColor implements GrayColor { ...@@ -38,7 +38,7 @@ public class ByteGrayColor implements GrayColor {
@Override @Override
public double getLuminosity() { public double getLuminosity() {
// TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc) // TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc)
return 0; return (double)this.grayLevel/MAXIMUM_GRAY_LEVEL;
} }
@Override @Override
...@@ -64,8 +64,13 @@ public class ByteGrayColor implements GrayColor { ...@@ -64,8 +64,13 @@ public class ByteGrayColor implements GrayColor {
@Override @Override
public GrayColor invert() { public GrayColor invert() {
// TODO Auto-generated method stub return new ByteGrayColor(MAXIMUM_GRAY_LEVEL - grayLevel);
return null; }
public GrayColor decreaseGrayLevel(int nbGrayLevels){
return new ByteGrayColor((Math.floor(grayLevel/255.0 * (nbGrayLevels-1)) * MAXIMUM_GRAY_LEVEL / (nbGrayLevels-1)));
} }
} }
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);
}
}
}
}
...@@ -8,11 +8,8 @@ public interface GrayColor extends Comparable<GrayColor> { ...@@ -8,11 +8,8 @@ public interface GrayColor extends Comparable<GrayColor> {
int getGrayLevel(); int getGrayLevel();
double getLuminosity(); double getLuminosity();
Color getColor(); Color getColor();
GrayColor invert();
GrayColor decreaseGrayLevel(int nbGrayLevels);
// Add methode Invert()
public GrayColor invert() {
} }
}
...@@ -5,4 +5,12 @@ ...@@ -5,4 +5,12 @@
public interface GrayImage extends Image { public interface GrayImage extends Image {
void setPixel(GrayColor gray, int x, int y); void setPixel(GrayColor gray, int x, int y);
GrayColor getPixelGrayColor(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));
}
} }
public class Invert implements Transform { public class Invert implements Transform {
public void invert() { @Override
}
public void applyTo(GrayImage image) { 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
...@@ -15,6 +15,14 @@ public class Main ...@@ -15,6 +15,14 @@ public class Main
MatrixGrayImage.createImageFromPGMFile("luminy.pgm"); MatrixGrayImage.createImageFromPGMFile("luminy.pgm");
// Add code here; // 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); display(image);
} }
......
...@@ -17,49 +17,47 @@ public class MatrixGrayImage implements GrayImage { ...@@ -17,49 +17,47 @@ public class MatrixGrayImage implements GrayImage {
@Override @Override
public void setPixel(GrayColor gray, int x, int y) { 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 @Override
public GrayColor getPixelGrayColor(int x, int y) { public GrayColor getPixelGrayColor(int x, int y) {
// TODO : Changer les instructions pour retourner le bon pixel. return pixels[x][y];
int Gcolor = pixels[x][y].getGrayLevel();
return new ByteGrayColor(Gcolor);
} }
@Override @Override
public Color getPixelColor(int x, int y) { public Color getPixelColor(int x, int y) {
// TODO : Changer les instructions pour retourner la couleur du pixel.
return pixels[x][y].getColor(); return pixels[x][y].getColor();
} }
@Override @Override
public int getWidth() { public int getWidth() {
// TODO : Changer les instructions pour retourner la largeur de l'image.
return this.width; return this.width;
} }
@Override @Override
public int getHeight() { public int getHeight() {
// TODO : Changer les instructions pour retourner la hauteur de l'image.
return this.height; return this.height;
} }
public MatrixGrayImage(int width, int 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.width = width;
this.height = height; this.height = height;
this.pixels = null; this.pixels = new GrayColor[width][height];
} }
public int getPixelByteColor(int x, int y) { public int getPixelByteColor(int x, int y) {
return (int)(getPixelGrayColor(x, y).getGrayLevel()*PGM_MAXIMUM_CODE); 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) { public static MatrixGrayImage createImageFromPGMFile(String fileName) {
// NE PAS MODIFIER ! // NE PAS MODIFIER !
InputStream file = ClassLoader.getSystemResourceAsStream(fileName); InputStream file = ClassLoader.getSystemResourceAsStream(fileName);
......
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);
}
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment