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

Version finale

parent 908ab70c
No related branches found
No related tags found
No related merge requests found
Showing
with 246 additions and 1333203 deletions
# Image
# Représentation d'images en couleurs
## Description du projet
Ce projet est l'occasion de travailler sur la représentation et la manipulation d'images. Ces images seront constituées de pixels caractérisés par une couleur représentant un niveau de gris.
On va considérer quatre manières de représenter une image en couleur et donc quatre classes d'images :
- `BruteRasterImage`
- `PaletteRasterImage`
- `SparseRasterImage`
- `VectorImage`
## Membres du projet
......
......@@ -23,5 +23,5 @@ test {
}
application {
mainClassName = "Main"
mainClassName = "viewer.Main"
}
\ No newline at end of file
rootProject.name = 'image'
rootProject.name = 'color-image'
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public class ByteGrayColor implements GrayColor {
private static final int MINIMUM_GRAY_LEVEL = 0;
private static final int MAXIMUM_GRAY_LEVEL = 255;
private static final int OPACITY = 1;
private final int grayLevel;
public ByteGrayColor(){
this.grayLevel = MINIMUM_GRAY_LEVEL;
}
public ByteGrayColor(int grayLevel) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance.
this.grayLevel = 0;
}
public ByteGrayColor(double luminosity) {
// TODO : Corriger l'initialisation de la propriété grayLevel de l'instance.
this.grayLevel = 0;
}
@Override
public double getLuminosity() {
// TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc)
return 0;
}
@Override
public Color getColor(){
double component = grayLevel / (double) MAXIMUM_GRAY_LEVEL;
return new Color(component, component, component, OPACITY);
}
@Override
public int compareTo(GrayColor o) {
// TODO : Retourner la différence de niveau de gris.
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (this.getClass() != o.getClass()) return false;
ByteGrayColor color = (ByteGrayColor) o;
return this.compareTo(color) == 0;
}
}
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 04/10/2018.
* Interface correspondant à une couleur de gris.
*/
public interface GrayColor extends Comparable<GrayColor> {
double getLuminosity();
Color getColor();
}
/**
* Created by Arnaud Labourel on 04/10/2018.
*/
public interface GrayImage extends Image {
void setPixel(GrayColor gray, int x, int y);
GrayColor getPixelGrayColor(int x, int y);
}
import javafx.scene.paint.Color;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Created by Arnaud Labourel on 02/10/2018.
*/
public class MatrixGrayImage implements GrayImage {
private final GrayColor[][] pixels;
private final int width;
private final int height;
@Override
public void setPixel(GrayColor gray, int x, int y) {
// TODO : Compléter la méthode pour modifier le pixel.
}
@Override
public GrayColor getPixelGrayColor(int x, int y) {
// TODO : Changer les instructions pour retourner le bon pixel.
return new ByteGrayColor();
}
@Override
public Color getPixelColor(int x, int y) {
// TODO : Changer les instructions pour retourner la couleur du pixel.
return Color.WHITE;
}
@Override
public int getWidth() {
// TODO : Changer les instructions pour retourner la largeur de l'image.
return 600;
}
@Override
public int getHeight() {
// TODO : Changer les instructions pour retourner la hauteur de l'image.
return 400;
}
public MatrixGrayImage(int width, int height){
/* TODO : Modifier les instructions pour initialiser correctement
les propriétés de l'instance.
*/
this.width = 0;
this.height = 0;
this.pixels = null;
}
public static MatrixGrayImage createImageFromPGMFile(String fileName) {
// NE PAS MODIFIER !
InputStream file = ClassLoader.getSystemResourceAsStream(fileName);
Scanner scan = new Scanner(file);
scan.nextLine();
scan.nextLine();
int width = scan.nextInt();
int height = scan.nextInt();
MatrixGrayImage result = new MatrixGrayImage(width, height);
scan.nextInt();
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++) {
GrayColor color = new ByteGrayColor(scan.nextInt());
result.setPixel(color, x, y);
}
}
return result;
}
public void writeIntoPGMFormat(String fileName){
// NE PAS MODIFIER !
try {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println("P2");
printWriter.println("# CREATOR: TP3 Version 1.0");
printWriter.printf("%d %d\n",this.width, this.height);
printWriter.println(pgmCodeOfGrayColor(pixels[0][0]));
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++) {
printWriter.println(pgmCodeOfGrayColor(getPixelGrayColor(x,y)));
}
}
printWriter.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static final int PGM_MAXIMUM_CODE = 255;
private int pgmCodeOfGrayColor(GrayColor pixelGrayColor) {
return (int) (pixelGrayColor.getLuminosity() * (double) PGM_MAXIMUM_CODE);
}
}
package image;
import javafx.scene.paint.Color;
public class BlankImage implements Image{
private final int width;
private final int height;
public BlankImage(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public Color getPixelColor(int x, int y) {
return Color.WHITE;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
}
package image;
public class BlankImageFactory implements ImageFactory {
@Override
public Image makeImage() {
return new BlankImage(1000, 800);
}
}
package image;
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 02/10/2018.
* Created by Arnaud Labourel on 09/11/2018.
*/
public interface Image {
Color getPixelColor(int x, int y);
......
package image;
/**
* Created by Arnaud Labourel on 23/11/2018.
*/
public interface ImageFactory {
Image makeImage();
}
package image;
import javafx.scene.paint.Color;
/**
* Created by Arnaud Labourel on 09/11/2018.
*/
public class Pixel extends Point{
private Color color;
Pixel(int x, int y, Color color) {
super(x, y);
this.color = color;
}
public Color getColor() {
return color;
}
}
package image;
import java.util.Objects;
/**
* Created by Arnaud Labourel on 09/11/2018.
*/
public class Point {
public final int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point point)) return false;
return x == point.x &&
y == point.y;
}
@Override
public final int hashCode() {
return Objects.hash(x, y);
}
}
package image;
import javafx.scene.paint.Color;
public interface Shape {
/**
* Tests if a specified Point is inside the boundary of the Shape.
*
* @return true if the point is inside the shape and false otherwise.
*/
boolean contains(Point point);
/**
* Return the color of the interior of the Shape.
*
* @return the color of the shape.
*/
Color getColor();
}
package util;
import java.util.Objects;
/**
* Created by Arnaud Labourel on 23/11/2018.
*/
public class Matrices {
/**
* Ensures that the given matrix does not have null parts : itself being null, having null row or having
* null values.
*
* @throws NullPointerException if there are null parts in the matrix.
* @param matrix the matrix to be tested.
*/
public static void requiresNonNull(Object[][] matrix) {
Objects.requireNonNull(matrix, "The matrix must not be null.");
for (int x = 0; x < getRowCount(matrix); x++) {
Objects.requireNonNull(matrix[x], "The matrix must not have rows equals to null.");
for (int y = 0; y < matrix[x].length; y++) {
Objects.requireNonNull(matrix[x][y], "The matrix must not have values equals to null.");
}
}
}
/**
* Ensures that the given matrix (assumed to be rectangular) does not have zero rows or zero columns.
*
* @throws IllegalArgumentException if the matrix have zero rows or zero columns.
* @param matrix the matrix to be tested.
*/
public static void requiresNonZeroDimensions(Object[][] matrix) {
if (getRowCount(matrix) == 0) {
throw new IllegalArgumentException("The matrix must not have zero rows.");
}
if (getColumnCount(matrix) == 0) {
throw new IllegalArgumentException("The matrix must not have zero columns.");
}
}
/**
* Ensures that the given matrix is rectangular, i.e., all rows have the same size.
*
* @throws IllegalArgumentException if the matrix have rows with different sizes.
* @param matrix the matrix to be tested.
*/
public static void requiresRectangularMatrix(Object[][] matrix) {
for (int x = 1; x < getRowCount(matrix); x++) {
if (matrix[x].length != matrix[0].length)
throw new IllegalArgumentException("The matrix must be rectangular.");
}
}
/**
* Give the number of rows of a matrix.
*
* @param matrix the matrix.
* @return the number of rows of the matrix.
*/
public static int getRowCount(Object[][] matrix){
return matrix.length;
}
/**
* Give the number of columns of a matrix (assumed to be rectangular).
*
* @param matrix the matrix.
* @return the number of rows of the matrix.
*/
public static int getColumnCount(Object[][] matrix){
return matrix[0].length;
}
}
package viewer;
import image.BlankImageFactory;
import image.Image;
import image.ImageFactory;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
......@@ -14,19 +19,20 @@ public class Display implements Initializable {
@FXML
private Canvas canvas;
MatrixGrayImage image;
private Image image;
private ImageFactory imageFactory;
@Override
public void initialize(URL location, ResourceBundle resources) {
imageFactory = new BlankImageFactory();
// TODO : changer la fabrique d'image pour construire des images.
this.image = MatrixGrayImage.createImageFromPGMFile("images/luminy.pgm");
// TODO : Ajouter les transformations d'image.
this.image = imageFactory.makeImage();
render();
}
public void render() {
private void render() {
int pixelWidth = image.getWidth();
int pixelHeight = image.getHeight();
......@@ -34,7 +40,6 @@ public class Display implements Initializable {
canvas.setHeight(pixelHeight);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
PixelWriter pixelWriter = graphicsContext.getPixelWriter();
for (int i = 0; i < pixelWidth; i++) {
......
package viewer;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -16,7 +18,7 @@ public class Main extends Application
@Override
public void start(Stage primaryStage) throws IOException {
Parent root =FXMLLoader.load(getClass().getResource("fxml/Display.fxml"));
Parent root =FXMLLoader.load(getClass().getClassLoader().getResource("fxml/Display.fxml"));
primaryStage.setTitle("Image display");
primaryStage.setScene(new Scene(root));
primaryStage.show();
......
......@@ -6,6 +6,6 @@
<?import javafx.scene.canvas.Canvas?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="Display">
fx:controller="viewer.Display">
<Canvas fx:id="canvas"/>
</AnchorPane>
This diff is collapsed.
import image.BlankImage;
import javafx.scene.paint.Color;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class BlankImageTest {
@Test
public void testBlankImageGetWidth(){
BlankImage blankImage = new BlankImage(200, 300);
assertThat(blankImage.getWidth(), is(equalTo(200)));
}
@Test
public void testBlankImageGetHeight(){
BlankImage blankImage = new BlankImage(200, 300);
assertThat(blankImage.getHeight(), is(equalTo(300)));
}
@Test
public void testBlankImageGetPixelColor(){
BlankImage blankImage = new BlankImage(200, 300);
assertThat(blankImage.getPixelColor(0,0), is(equalTo(Color.WHITE)));
assertThat(blankImage.getPixelColor(100,100), is(equalTo(Color.WHITE)));
assertThat(blankImage.getPixelColor(199,299), is(equalTo(Color.WHITE)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment