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

2.2.1

parent 644eb325
Branches
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
/ByteGrayColor.class
/GrayColor.class
/GrayImage.class
/Image.class
/Main$ImagePanel.class
/Main.class
/MatrixGrayImage.class
/Transform.class
/Invert.class
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tp4</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import java.awt.Color;
/**
* Classe ByteGrayColor: Pour manipuler le coleur d'un pixel en niveau de gris (grey level).
*/
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;
public static final ByteGrayColor WHITE = new ByteGrayColor(MAXIMUM_GRAY_LEVEL);
public static final ByteGrayColor BLACK = new ByteGrayColor(MINIMUM_GRAY_LEVEL);
private final int grayLevel;
public ByteGrayColor(){
this.grayLevel = MINIMUM_GRAY_LEVEL;
}
public ByteGrayColor(int grayLevel) {
this.grayLevel = grayLevel;
}
public ByteGrayColor(double luminosity) {
double lum = MAXIMUM_GRAY_LEVEL * (int)luminosity;
this.grayLevel = (int)lum;
}
@Override
public int getGrayLevel(){
return grayLevel;
}
@Override
public double getLuminosity() {
// TODO : Retourner la luminosité de la couleur (entre 0 noir et 1 blanc)
return 0;
}
@Override
public Color getColor(){
float component = (float)getLuminosity();
return new Color(component, component, component);
}
@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;
}
@Override
public GrayColor invert() {
// TODO Auto-generated method stub
return null;
}
}
import java.awt.Color;
/**
* Interface correspondant à une couleur de gris.
*/
public interface GrayColor extends Comparable<GrayColor> {
int getGrayLevel();
double getLuminosity();
Color getColor();
// Add methode Invert()
public GrayColor invert() {
}
}
/**
* Interface pour representer un image en version noir et blanc.
*/
public interface GrayImage extends Image {
void setPixel(GrayColor gray, int x, int y);
GrayColor getPixelGrayColor(int x, int y);
}
import java.awt.Color;
/**
* Interface pour representer un image.
*/
public interface Image {
Color getPixelColor(int x, int y);
int getWidth();
int getHeight();
}
public class Invert implements Transform {
public void invert() {
}
public void applyTo(GrayImage image) {
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.IOException;
public class Main
{
public static void main(String[] args) {
MatrixGrayImage image =
MatrixGrayImage.createImageFromPGMFile("luminy.pgm");
// Add code here;
display(image);
}
/** Methode pour visulaiser l'image. NE PAS MODIFIER !! **/
public static void display(MatrixGrayImage image) {
JFrame frame = new JFrame();
int width = image.getWidth();
int height = image.getHeight();
frame.setSize(width, height);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(new ImagePanel(width, height, image));
frame.setVisible(true);
}
private static class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel(int width, int height, MatrixGrayImage myimage) {
this.image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
image.setRGB(i, j, myimage.getPixelByteColor(i,j));
}
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
import java.awt.Color;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Classe pour representer un image comme un 2D tableau de pixels ou chaque pixel est un niveau de gris.
*/
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.
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);
}
@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;
}
public int getPixelByteColor(int x, int y) {
return (int)(getPixelGrayColor(x, y).getGrayLevel()*PGM_MAXIMUM_CODE);
}
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.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);
}
}
/**
Interface for transformations to Image
**/
public interface Transform {
void applyTo(GrayImage image);
}
tp4/luminy.pgm 0 → 100755
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment