diff --git a/.vscode/launch.json b/.vscode/launch.json index 3d66b63..bdcff70 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,27 @@ // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "java", + "name": "Launch TheaterApplication", + "request": "launch", + "mainClass": "lab8_tableaux.TheaterApplication", + "projectName": "Labo_6a2f7ad1" + }, + { + "type": "java", + "name": "Launch Task1", + "request": "launch", + "mainClass": "lab8_tableaux.Task1", + "projectName": "Labo_6a2f7ad1" + }, + { + "type": "java", + "name": "Launch App", + "request": "launch", + "mainClass": "lab8_tableaux.App", + "projectName": "Labo_6a2f7ad1" + }, { "type": "java", "name": "Launch Task2Runner", diff --git a/src/lab8_tableaux/08-Arrays-FR.pdf b/src/lab8_tableaux/08-Arrays-FR.pdf new file mode 100644 index 0000000..b315b0b Binary files /dev/null and b/src/lab8_tableaux/08-Arrays-FR.pdf differ diff --git a/src/lab8_tableaux/Seat.java b/src/lab8_tableaux/Seat.java new file mode 100644 index 0000000..3d113f6 --- /dev/null +++ b/src/lab8_tableaux/Seat.java @@ -0,0 +1,19 @@ +package lab8_tableaux; + +public class Seat { + public boolean busy; + private char row; + private int column; + + Seat(char row, int column){ + this.row = row; + this.column = column; + } + + public String getPlace(){ + String s = ""; + s += this.column; + s += this.row; + return s; + } +} diff --git a/src/lab8_tableaux/Task0.java b/src/lab8_tableaux/Task0.java new file mode 100644 index 0000000..2022601 --- /dev/null +++ b/src/lab8_tableaux/Task0.java @@ -0,0 +1,19 @@ +package lab8_tableaux; + +public class Task0 { + public static void main(String[] args) { + int[] foo = even_array(5); + for (int i = 0; i < foo.length; i++) { + System.out.println(foo[i]); + } + + } + + public static int[] even_array(int n){ + int[] foo = new int[n]; + for (int i = 0; i < foo.length; i++) { + foo[i] = i*2; + } + return foo; + } +} diff --git a/src/lab8_tableaux/Task1.java b/src/lab8_tableaux/Task1.java new file mode 100644 index 0000000..1f089d2 --- /dev/null +++ b/src/lab8_tableaux/Task1.java @@ -0,0 +1,8 @@ +package lab8_tableaux; + +public class Task1 { + public static void main(String[] args) { + Seat mySeat = new Seat('a', 7); + System.out.println(mySeat.busy); + } +} diff --git a/src/lab8_tableaux/Theater.java b/src/lab8_tableaux/Theater.java new file mode 100644 index 0000000..0378ef3 --- /dev/null +++ b/src/lab8_tableaux/Theater.java @@ -0,0 +1,104 @@ +package lab8_tableaux; + +public class Theater { + private Seat[][] seats; + + Theater(int r, int c){ + seats = new Seat[c][r]; + for (int i = 1; i <= c; i++) { + for (char j = 'A'; j < 'A'+r; j++) { + seats[i-1][j-'A'] = new Seat(j,i); + } + } + } + + public String getSeat(int row, int column){ + return seats[column][row].getPlace(); + } + + public boolean isSeatBusy (int row, int column){ + return seats[column][row].busy; + } + + public boolean occupySeat(int row, int column) { + if (isSeatBusy(row, column)) { + return false; + } + seats[column][row].busy = true; + return true; + } + + private int numberOfBusySeats(){ + int total =0 ; + int r = seats[0].length; + int c = seats.length; + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + if(isSeatBusy(i, j)){ + total++; + } + } + } + return total; + } + + private int numberOfSeats(){ + int total =0 ; + int r = seats[0].length; + int c = seats.length; + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { + total++; + } + } + return total; + } + + public String occupation() { + String s = ""; + s += "Theater occupation: "; + s += numberOfBusySeats(); + s += " / "; + s += numberOfSeats(); + return s; + } + + public String toString(){ + String s = ""; + s += "Theater seats occupation:"; + s += "\n"; + s += "\n"; + s += " "; + int r = seats[0].length; + int c = seats.length; + System.out.println(c + "x" + r); + for (int i = 0; i < c; i++) { + s += " "; + s += i; + } + s += "\n"; + for (int i = 0; i < r; i++) { + s += i; + s += " "; + for (int j = 0; j < c; j++) { + if (isSeatBusy(i, j)) { + s += "X"; + } else { + s += " "; + } + s += space(j); + } + s += "\n"; + } + return s; + } + + private String space (int i){ + String s = ""; + int length = String.valueOf(i).length(); + for (int j = 0; j < length; j++) { + s += " "; + } + return s; + } +} diff --git a/src/lab8_tableaux/TheaterApplication.java b/src/lab8_tableaux/TheaterApplication.java new file mode 100644 index 0000000..d20f70b --- /dev/null +++ b/src/lab8_tableaux/TheaterApplication.java @@ -0,0 +1,24 @@ +package lab8_tableaux; + + +public class TheaterApplication { + public static void main(String[] args) { + Theater cinema = new Theater(10, 20); + cinema.occupySeat(2, 1); + cinema.occupySeat(2, 3); + cinema.occupySeat(2, 5); + cinema.occupySeat(2, 8); + cinema.occupySeat(0, 1); + cinema.occupySeat(0, 3); + cinema.occupySeat(0, 5); + cinema.occupySeat(0, 8); + cinema.occupySeat(0, 9); + cinema.occupySeat(0, 10); + cinema.occupySeat(0, 11); + cinema.occupySeat(0, 13); + cinema.occupySeat(0, 15); + cinema.occupySeat(0, 18); + System.out.println(cinema); + System.out.println(cinema.occupation()); + } +} diff --git a/src/lab9_image_processing/ImageFilters.java b/src/lab9_image_processing/ImageFilters.java new file mode 100644 index 0000000..51ea42d --- /dev/null +++ b/src/lab9_image_processing/ImageFilters.java @@ -0,0 +1,62 @@ +package lab9_image_processing; +/** + * This class implements the various image filters + * @author Pierre-André Mudry + * @date 2012 + * @version 1.0 + */ +public class ImageFilters { + + /** + * Simply duplicates every pixel from the source image + * @param a + * @return + */ + public static int[][] duplicate(int[][] a) { + /** + * Write your code hereunder + */ + + return null; + } + + /** + * Dichotomy of the image + * @param a + * @param threshold + * @return + */ + public static int[][] threshold(int[][] a, int threshold) { + return a; + } + + /** + * Mean filter that blurs the image a bit + * @param a + * @return + */ + public static int[][] mean(int[][] a) { + return a; + } + + + /** + * Derivative of the image + * @param a + * @return + */ + public static int[][] derivative(int[][] a) { + return a; + } + + /** + * Sobel filter of the image + * @param a + * @param intensity + * @return + */ + public static int[][] sobel(int[][] a, double intensity) { + return a; + } + +} diff --git a/src/lab9_image_processing/ImageProcessing.java b/src/lab9_image_processing/ImageProcessing.java new file mode 100644 index 0000000..4e1a1cd --- /dev/null +++ b/src/lab9_image_processing/ImageProcessing.java @@ -0,0 +1,22 @@ +package lab9_image_processing; +import hevs.graphics.ImageGraphics; + +public class ImageProcessing { + + + public static void main(String[] args) { + final String imageUsed = "/images/rice.jpg"; + + /** + * Create the windows from images + */ + ImageGraphics org = new ImageGraphics(imageUsed, "Original", -450, -250); + ImageGraphics cpy = new ImageGraphics(imageUsed, "Copy", 0, -250); + + int[][] thePixels = org.getPixelsBW(); + int[][] theCopy = ImageFilters.duplicate(thePixels); + + // Simple copy and display + cpy.setPixelsBW(theCopy); + } +} diff --git a/src/lab9_image_processing/hevs/graphics/ImageGraphics.java b/src/lab9_image_processing/hevs/graphics/ImageGraphics.java new file mode 100644 index 0000000..1e1ccd1 --- /dev/null +++ b/src/lab9_image_processing/hevs/graphics/ImageGraphics.java @@ -0,0 +1,232 @@ +package lab9_image_processing.hevs.graphics; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; + +import javax.imageio.ImageIO; +import javax.swing.JFrame; + +/** + * This class was made to deal with images as grayscale multidimensional arrays. + * Mainly used in the ImageProcessing lab + * + * It expects the images to reside in the src directory + * + * @author Pierre-Andre Mudry 2011 + */ +public class ImageGraphics extends JFrame { + private static final long serialVersionUID = 6832022057915586803L; + + private BufferedImage backgroundBitmap = null; + private int w, h; + + public ImageGraphics(String backGroundFilePath, String title, int xPositionOffset, int yPositionOffset) { + + try { + // Fill the frame content with the image + try { + backgroundBitmap = ImageIO.read(ImageGraphics.class.getResource(backGroundFilePath)); + w = backgroundBitmap.getWidth(); + h = backgroundBitmap.getHeight(); + } catch (Exception e) { + System.out.println("Could not find image " + backGroundFilePath + ", exiting !"); + e.printStackTrace(); + System.exit(-1); + } + + this.setResizable(false); + this.setSize(backgroundBitmap.getWidth(),backgroundBitmap.getHeight()); + this.setTitle(title); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Get the size of the screen + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + + // Determine the new location of the window + int w = this.getSize().width; + int h = this.getSize().height; + int x = (dim.width - w) / 2 + xPositionOffset; + int y = (dim.height - h) / 2 + yPositionOffset; + + // Move the window + this.setLocation(x, y); + this.setVisible(true); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Sets a grayscale pixel, does not sets values for invalid pixels + * outside the screen. Does not repaint the screen either because it + * is slow. If required, please call {@link #repaint()} if needed after + * you have updated all the pixels you need. + * + * @param x + * @param y + * @param intensity + */ + public void setPixelBW(int x, int y, int intensity) { + if (!((x < 0) || (y < 0) || (x >= w) || (y >= h))) { + backgroundBitmap.setRGB(x, y, intensity << 16 | intensity << 8| intensity); + } + } + + /** + * Sets an array of grayscale pixels (from 0 to 255) and displays them + * @param pixels + */ + public void setPixelsBW(int[][] pixels) { + try { + if (pixels[0].length != h || pixels.length != w) { + throw new Exception("Invalid size of the pixel array !"); + } + + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + // FIXME this is slow, should use rasters instead + int c = pixels[i][j] << 16 | pixels[i][j] << 8 | pixels[i][j]; + backgroundBitmap.setRGB(i, j, c); + } + + this.repaint(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Sets an array of pixels of Color and displays them + * @param pixels + */ + public void setPixelsColor(Color[][] pixels) { + try { + if (pixels[0].length != h || pixels.length != w) { + throw new Exception("Invalid size of the pixel array !"); + } + + // FIXME this is slow, should use rasters instead + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + backgroundBitmap.setRGB(i, j, pixels[i][j].getRGB()); + } + + this.repaint(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Gets a single pixel from the background image and returns its + * grayscale value + * + * @param x + * @param y + * @return + */ + public int getPixelBW(int x, int y) { + if ((x < 0) || (y < 0) || (x >= w) || (y >= h)) { + return 0; + } else { + // Inside the image. Make the gray conversion and return the value + Color c = new Color(backgroundBitmap.getRGB(x, y)); + return (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue()); + } + } + + /** + * Gets the array of the pixels (which have been converted to grayscale + * if required) + * + * @return The arrays of gray pixels + */ + public int[][] getPixelsBW() { + int[][] values = new int[w][h]; + + // FIXME this is slow + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + Color c = new Color(backgroundBitmap.getRGB(i, j)); + values[i][j] = (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue()); + } + + return values; + } + + /** + * Gets the array of the pixels as Colors (see #Color) + * @return The arrays of pixels + */ + public Color[][] getPixelsColor(){ + Color[][] values = new Color[w][h]; + + // FIXME this is slow + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + values[i][j] = new Color(backgroundBitmap.getRGB(i, j)); + } + + return values; + } + + /** + * Converts a color array to a black-or-white array + * @param c The color array + * @return The array converted to BW + */ + public static Color[][] convertToGray(Color[][] c){ + int w = c.length; int h = c[0].length; + Color[][] values = new Color[w][h]; + + // FIXME this is slow + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + Color col = c[i][j]; + int intColor = (int)(0.3 * col.getRed() + 0.59 * col.getGreen() + 0.11 * col.getBlue()); + values[i][j] = new Color(intColor, intColor, intColor); + } + + return values; + } + + /** + * Converts a color array to a black-or-white array + * @param c The color array + * @return The array converted to BW + */ + public static int[][] convertToGrayInt(Color[][] c){ + int w = c.length; int h = c[0].length; + int[][] values = new int[w][h]; + + // FIXME this is slow + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) { + Color col = c[i][j]; + int intColor = (int)(0.3 * col.getRed() + 0.59 * col.getGreen() + 0.11 * col.getBlue()); + values[i][j] = intColor; + } + + return values; + } + + /** + * Paint method + */ + public void paint(Graphics g) { + + g.drawImage(backgroundBitmap, 0, 0, null); + g.dispose(); + } + + public static void main(String args[]) { + final String imageUsed = "/images/lena.bmp"; + ImageGraphics org = new ImageGraphics(imageUsed, "Original", 0, 0); + } +} diff --git a/src/lab9_image_processing/images/Dead_tree.png b/src/lab9_image_processing/images/Dead_tree.png new file mode 100644 index 0000000..08bf780 Binary files /dev/null and b/src/lab9_image_processing/images/Dead_tree.png differ diff --git a/src/lab9_image_processing/images/imageProcessing.jpg b/src/lab9_image_processing/images/imageProcessing.jpg new file mode 100644 index 0000000..92ae49b Binary files /dev/null and b/src/lab9_image_processing/images/imageProcessing.jpg differ diff --git a/src/lab9_image_processing/images/imageProcessing_empty.jpg b/src/lab9_image_processing/images/imageProcessing_empty.jpg new file mode 100644 index 0000000..c415c7e Binary files /dev/null and b/src/lab9_image_processing/images/imageProcessing_empty.jpg differ diff --git a/src/lab9_image_processing/images/lena.bmp b/src/lab9_image_processing/images/lena.bmp new file mode 100644 index 0000000..901034d Binary files /dev/null and b/src/lab9_image_processing/images/lena.bmp differ diff --git a/src/lab9_image_processing/images/moire1.png b/src/lab9_image_processing/images/moire1.png new file mode 100644 index 0000000..fa73e3d Binary files /dev/null and b/src/lab9_image_processing/images/moire1.png differ diff --git a/src/lab9_image_processing/images/moire2.jpg b/src/lab9_image_processing/images/moire2.jpg new file mode 100644 index 0000000..b0d296a Binary files /dev/null and b/src/lab9_image_processing/images/moire2.jpg differ diff --git a/src/lab9_image_processing/images/rice.jpg b/src/lab9_image_processing/images/rice.jpg new file mode 100644 index 0000000..c778fe1 Binary files /dev/null and b/src/lab9_image_processing/images/rice.jpg differ