Skip to content
Snippets Groups Projects
Commit f6c7c853 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Correction version finale

parent 0e0c2c45
No related branches found
No related tags found
No related merge requests found
Pipeline #1262 failed
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public class ByteGrayColor implements GrayColor { public class ByteGrayColor implements GrayColor {
private static final int MINIMUM_GRAY_LEVEL = 0; private static final int MINIMUM_GRAY_LEVEL = 0;
private static final int MAXIMUM_GRAY_LEVEL = 255; private static final int MAXIMUM_GRAY_LEVEL = 255;
private static final int OPACITY = 1; private static final int OPACITY = 1;
private final int grayLevel; public static final ByteGrayColor WHITE = new ByteGrayColor(MAXIMUM_GRAY_LEVEL);
public static final ByteGrayColor BLACK = new ByteGrayColor(MINIMUM_GRAY_LEVEL);
private static final double MAXIMUM_LUMINOSITY = 1.;
private final int grayLevel;
public ByteGrayColor(){ public ByteGrayColor(){
this.grayLevel = MINIMUM_GRAY_LEVEL; this.grayLevel = MINIMUM_GRAY_LEVEL;
} }
public ByteGrayColor(int grayLevel) { public ByteGrayColor(int grayLevel) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance. this.grayLevel = grayLevel;
this.grayLevel = 0;
} }
public ByteGrayColor(double luminosity) { public ByteGrayColor(double luminosity) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance. this.grayLevel = (int) (luminosity * MAXIMUM_GRAY_LEVEL);
this.grayLevel = 0;
} }
@Override @Override
public double getLuminosity() { public double getLuminosity() {
// TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc) return grayLevel/(double) MAXIMUM_GRAY_LEVEL;
return 0;
} }
@Override @Override
public Color getColor(){ public Color getColor(){
double component = grayLevel / (double) MAXIMUM_GRAY_LEVEL; double component = getLuminosity();
return new Color(component, component, component, OPACITY); return new Color(component, component, component, OPACITY);
} }
@Override @Override
public int compareTo(GrayColor o) { public int compareTo(GrayColor o) {
// TODO : Retourner la différence de niveau de gris. return Double.compare(this.getLuminosity(), o.getLuminosity());
return 0;
} }
@Override @Override
...@@ -54,4 +49,12 @@ public class ByteGrayColor implements GrayColor { ...@@ -54,4 +49,12 @@ public class ByteGrayColor implements GrayColor {
return this.compareTo(color) == 0; return this.compareTo(color) == 0;
} }
@Override
public GrayColor invert() {
return new ByteGrayColor(invertedLuminosity());
}
private double invertedLuminosity() {
return MAXIMUM_LUMINOSITY - getLuminosity();
}
} }
\ No newline at end of file
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class CompositeTransform implements Transform {
private final Transform[] transforms;
public CompositeTransform(Transform[] transforms) {
this.transforms = transforms;
}
@Override
public void applyTo(GrayImage image) {
for(Transform transform : transforms){
transform.applyTo(image);
}
}
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class CrissCross implements Transform{
private final int size;
public CrissCross(int size){
this.size = size;
}
@Override
public void applyTo(GrayImage image) {
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
modifyPixel(image, x, y);
}
}
}
private void modifyPixel(GrayImage image, int x, int y) {
if(x % size == 0 || y % size ==0){
image.setPixel(ByteGrayColor.BLACK, x, y);
}
}
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class DecreaseGrayLevels implements Transform {
private final int nbGrayLevels;
public DecreaseGrayLevels(int nbGrayLevels) {
this.nbGrayLevels = nbGrayLevels;
}
@Override
public void applyTo(GrayImage image) {
for(int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
modifyPixel(image, x, y);
}
}
}
private void modifyPixel(GrayImage image, int x, int y) {
int numberOfIntervals = nbGrayLevels - 1;
double sizeOfIntervals = 1. / (double) numberOfIntervals;
double luminosity = image.getPixelGrayColor(x, y).getLuminosity();
double newLuminosity = Math.floor(luminosity * numberOfIntervals) * sizeOfIntervals;
image.setPixel(new ByteGrayColor(newLuminosity), x, y);
}
}
...@@ -21,8 +21,10 @@ public class Display implements Initializable { ...@@ -21,8 +21,10 @@ public class Display implements Initializable {
this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm"); this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm");
// TODO : Ajouter les transformations d'image. Transform transform = new CompositeTransform(new Transform[] {new DecreaseGrayLevels(8), new Outline(0.05), new Invert()});
transform.applyTo(image);
image.writeIntoPGMFormat("/Users/arnaudlabourel/luminy.pgm");
render(); render();
} }
...@@ -49,3 +51,4 @@ public class Display implements Initializable { ...@@ -49,3 +51,4 @@ public class Display implements Initializable {
} }
} }
...@@ -4,7 +4,9 @@ import javafx.scene.paint.Color; ...@@ -4,7 +4,9 @@ import javafx.scene.paint.Color;
* Created by Arnaud Labourel on 04/10/2018. * Created by Arnaud Labourel on 04/10/2018.
* Interface correspondant à une couleur de gris. * Interface correspondant à une couleur de gris.
*/ */
public interface GrayColor extends Comparable<GrayColor> { public interface GrayColor extends Comparable<GrayColor> {
double getLuminosity(); double getLuminosity();
Color getColor(); Color getColor();
GrayColor invert();
} }
/** /**
* Created by Arnaud Labourel on 04/10/2018. * Interface pour une image avec des nuances de gris.
*/ */
public interface GrayImage extends Image { public interface GrayImage extends Image {
......
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
/** /**
* Created by Arnaud Labourel on 02/10/2018. * Interface pour une image.
*/ */
public interface Image { public interface Image {
Color getPixelColor(int x, int y); Color getPixelColor(int x, int y);
......
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class Invert implements Transform{
@Override
public void applyTo(GrayImage image) {
for(int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
modifyPixel(image, x, y);
}
}
}
private void modifyPixel(GrayImage image, int x, int y) {
image.setPixel(image.getPixelGrayColor(x, y).invert(), x, y);
}
}
...@@ -6,7 +6,6 @@ import javafx.stage.Stage; ...@@ -6,7 +6,6 @@ import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
public class Main extends Application public class Main extends Application
{ {
...@@ -18,8 +17,10 @@ public class Main extends Application ...@@ -18,8 +17,10 @@ public class Main extends Application
public void start(Stage primaryStage) throws IOException { public void start(Stage primaryStage) throws IOException {
Parent root =FXMLLoader.load(getClass().getResource("fxml/Display.fxml")); Parent root =FXMLLoader.load(getClass().getResource("fxml/Display.fxml"));
primaryStage.setTitle("Image display"); primaryStage.setTitle("Image display");
primaryStage.setScene(new Scene(root)); primaryStage.setScene(new Scene(root));
primaryStage.show(); primaryStage.show();
} }
} }
...@@ -14,45 +14,45 @@ public class MatrixGrayImage implements GrayImage { ...@@ -14,45 +14,45 @@ public class MatrixGrayImage implements GrayImage {
private final int width; private final int width;
private final int height; private final int height;
@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;
} }
@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];
return new ByteGrayColor();
} }
@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 getPixelGrayColor(x, y).getColor();
return Color.WHITE;
} }
@Override @Override
public int getWidth() { public int getWidth() {
// TODO : Changer les instructions pour retourner la largeur de l'image. return width;
return 600;
} }
@Override @Override
public int getHeight() { public int getHeight() {
// TODO : Changer les instructions pour retourner la hauteur de l'image. return height;
return 400;
} }
public MatrixGrayImage(int width, int height){ public MatrixGrayImage(int width, int height){
/* TODO : Modifier les instructions pour initialiser correctement this.width = width;
les propriétés de l'instance. this.height = height;
*/ this.pixels = new GrayColor[width][height];
this.width = 0; setAllPixelsColorTo(ByteGrayColor.WHITE);
this.height = 0;
this.pixels = null;
} }
private void setAllPixelsColorTo(GrayColor gray) {
for(int x = 0; x < this.width; x++){
for (int y = 0; y < this.height; y++){
pixels[x][y] = gray;
}
}
}
public static MatrixGrayImage createImageFromPGMFile(String fileName) { public static MatrixGrayImage createImageFromPGMFile(String fileName) {
// NE PAS MODIFIER ! // NE PAS MODIFIER !
...@@ -87,7 +87,7 @@ public class MatrixGrayImage implements GrayImage { ...@@ -87,7 +87,7 @@ public class MatrixGrayImage implements GrayImage {
printWriter.println("# CREATOR: TP3 Version 1.0"); printWriter.println("# CREATOR: TP3 Version 1.0");
printWriter.printf("%d %d\n",this.width, this.height); printWriter.printf("%d %d\n",this.width, this.height);
printWriter.println(pgmCodeOfGrayColor(pixels[0][0])); printWriter.println(PGM_MAXIMUM_CODE);
for(int y = 0; y < height; y++){ for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
......
import java.util.ArrayList;
import java.util.List;
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class Outline implements Transform {
private final double threshold;
public Outline(double threshold) {
this.threshold = threshold;
}
private List<GrayColor> southEastNeighborPixels(GrayImage image, int x, int y){
List<GrayColor> neighborPixels = new ArrayList<>();
if (x != image.getWidth() - 1) {
neighborPixels.add(image.getPixelGrayColor(x + 1, y));
}
if (y != image.getHeight() - 1) {
neighborPixels.add(image.getPixelGrayColor(x, y + 1));
}
return neighborPixels;
}
@Override
public void applyTo(GrayImage image) {
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
modifyPixel(image, x, y);
}
}
}
private void modifyPixel(GrayImage image, int x, int y) {
List<GrayColor> neighborPixels = southEastNeighborPixels(image, x, y);
ByteGrayColor newColor = ByteGrayColor.WHITE;
for(GrayColor neighborPixel : neighborPixels){
if(Math.abs(neighborPixel.getLuminosity() - image.getPixelGrayColor(x,y).getLuminosity()) > threshold) {
newColor = ByteGrayColor.BLACK;
}
}
image.setPixel(newColor, x, y);
}
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public class Pixelate implements Transform{
private class PixelSquare {
private final int xTopLeft;
private final int yTopLeft;
public PixelSquare(int xTopLeft, int yTopLeft) {
this.xTopLeft = xTopLeft;
this.yTopLeft = yTopLeft;
}
void setAllPixelsColor(GrayColor grayColor, GrayImage image){
for(int x = xTopLeft; x < Math.min(xTopLeft + newPixelSize, image.getWidth()); x++) {
for (int y = yTopLeft; y < Math.min(yTopLeft + newPixelSize, image.getHeight()); y++) {
image.setPixel(grayColor, x, y);
}
}
}
ByteGrayColor averageColorOfPixels(GrayImage image){
double sumOfLuminosities = 0;
int nbPixels = 0;
for(int x = xTopLeft; x < Math.min(xTopLeft + newPixelSize, image.getWidth()); x++) {
for (int y = yTopLeft; y < Math.min(yTopLeft + newPixelSize, image.getHeight()); y++) {
sumOfLuminosities += image.getPixelGrayColor(x, y).getLuminosity();
nbPixels++;
}
}
return new ByteGrayColor(sumOfLuminosities/nbPixels);
}
}
private final int newPixelSize;
public Pixelate(int newPixelSize) {
this.newPixelSize = newPixelSize;
}
@Override
public void applyTo(GrayImage image){
int rowCount = (int) Math.ceil((double)image.getWidth()/newPixelSize);
int columnCount = (int) Math.ceil((double)image.getHeight()/newPixelSize);
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < columnCount; j++){
PixelSquare pixelSquare = new PixelSquare(i*newPixelSize, j*newPixelSize);
pixelSquare.setAllPixelsColor(pixelSquare.averageColorOfPixels(image), image);
}
}
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public interface Transform {
void applyTo(GrayImage image);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment