Upload final Commit

This commit is contained in:
Siwat Sirichai 2020-10-27 15:21:36 +07:00
parent 954ec312e8
commit a91a039e6c
16 changed files with 232 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Week9/Assignment9/pic1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

BIN
Week9/Assignment9/pic2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
Week9/Assignment9/pic3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

BIN
Week9/Assignment9/pic4.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

BIN
Week9/Assignment9/pic5.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

View File

@ -0,0 +1,42 @@
import java.io.File;
import java.util.Arrays;
public class DesaturateIt_Template{
public static void main(String [] args){
int [][][] rgb = Java101ImageUtil.getRGBArrayFromFile();
if(rgb==null){return;}
int[][][] gray = grayscale(rgb);
int[][][] sepia = sepia(rgb);
// fill code to show all three images
// - original, grayscale, sepia
//Array Structure
// [X][Y][COLOR]
int[][][][] images = new int[3][][][];
images[0]=rgb;
images[1]=gray;
images[2]=sepia;
Java101ImageUtil.showViewer(images,"show all");
}
public static int [][][] grayscale(int [][][] rgb) {
// fill code
int[][][] grayscale = new int [rgb.length][rgb[0].length][3];
for(int i=0;i<grayscale.length;i++) for(int j=0;j<grayscale[0].length;j++) {
int modifiedColor = (int)Math.ceil((rgb[i][j][0]+rgb[i][j][1]+rgb[i][j][2])/3.0);
for(int k = 0;k<3;k++)grayscale[i][j][k] = modifiedColor;
}
return grayscale;
}
public static int [][][] sepia(int [][][] rgb) {
// fill code
int[][][] sepia = new int [rgb.length][rgb[0].length][3];
for(int i=0;i<sepia.length;i++) for(int j=0;j<sepia[0].length;j++) {
int modifiedColor = (int)Math.ceil((rgb[i][j][0]+rgb[i][j][1]+rgb[i][j][2])/3.0);
sepia[i][j][0] = (int)(modifiedColor*0.8745);
sepia[i][j][1] = (int)(modifiedColor*0.6352);
sepia[i][j][2] = (int)(modifiedColor*0.4941);
}
return sepia;
}
}

View File

@ -0,0 +1,115 @@
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Java101ImageUtil{
public static int [][][] getRGBArrayFromFile(){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
return getRGBArrayOfSelectedFile(chooser.getSelectedFile());
}else{
return null;
}
}
public static int [][][] getRGBArrayOfSelectedFile(File file){
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch (IOException e) {
System.out.println("Cannot read the selected file.");
System.exit(1);
}
int h = img.getHeight();
int w = img.getWidth();
int[][][] rgb = new int[w][h][3];
for( int i = 0; i < w; i++ ){
for( int j = 0; j < h; j++ ){
Color c = new Color(img.getRGB(i,j));
rgb[i][j][0] = c.getRed();
rgb[i][j][1] = c.getGreen();
rgb[i][j][2] = c.getBlue();
}
}
return rgb;
}
public static void showViewer(int [][][] rgb,String title){
Java101Viewer frame = new Java101Viewer(rgb,title);
initViewer(frame);
}
public static void showViewer(int [][][] rgb1,int [][][] rgb2,String title){
Java101Viewer frame = new Java101Viewer(rgb1,rgb2,title);
initViewer(frame);
}
public static void showViewer(int [][][][] rgbs,String title){
Java101Viewer frame = new Java101Viewer(rgbs,title);
initViewer(frame);
}
private static void initViewer(Java101Viewer frame){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("Close the image viewer to exit the program.");
}
}
class Java101Viewer extends JFrame{
public Java101Viewer(int [][][] rgb,String title){
super(title);
setLayout(new GridLayout(1,1));
Java101DrawingPanel drawing = new Java101DrawingPanel(rgb);
add(drawing);
}
public Java101Viewer(int [][][] rgb1,int [][][] rgb2,String title){
super(title);
setLayout(new GridLayout(1,2));
Java101DrawingPanel drawingLeft = new Java101DrawingPanel(rgb1);
add(drawingLeft);
Java101DrawingPanel drawingRight = new Java101DrawingPanel(rgb2);
add(drawingRight);
}
public Java101Viewer(int [][][][] rgbs,String title){
super(title);
int num = rgbs.length;
Java101DrawingPanel [] drawings = new Java101DrawingPanel[num];
setLayout(new GridLayout(1,num));
for(int i=0;i<num;i++){
drawings[i] = new Java101DrawingPanel(rgbs[i]);
add(drawings[i]);
}
}
}
class Java101DrawingPanel extends JPanel{
BufferedImage bi;
public Java101DrawingPanel(int [][][] rgb){
int w = rgb.length;
int h = rgb[0].length;
bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
setPreferredSize(new Dimension(w,h));
System.out.println("Image size=("+w+","+h+")");
paintImage(rgb);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(bi,0,0,null);
}
private void paintImage(int [][][] rgb){
for(int x=0;x<rgb.length;x++){
for(int y=0;y<rgb[x].length;y++){
Color c = new Color(rgb[x][y][0],rgb[x][y][1],rgb[x][y][2]);
int rgbInt = c.getRGB();
bi.setRGB(x,y,rgbInt);
}
}
}
}

View File

@ -0,0 +1,75 @@
import java.util.Scanner;
public class Java101ImageUtilExample{
public static void main(String [] args){
int [][][] rgb1 = Java101ImageUtil.getRGBArrayFromFile();
if(rgb1==null){
return;
}
int [][][] rgb2 = flipVertical(rgb1);
int [][][] rgb3 = genAllRed(64,128);
char choice;
do {
choice = getUserChoice(genChoiceMenu());
String title;
switch(choice){
case '1':
title = "One image";
Java101ImageUtil.showViewer(rgb1,title);
break;
case '2':
title = "One image and its flipped version";
Java101ImageUtil.showViewer(rgb1,rgb2,title);
break;
case '3':
title = "One image, its flipped version, and a 64x128 all red";
int [][][][] rgbs = new int[3][][][];
rgbs[0] = rgb1;
rgbs[1] = rgb2;
rgbs[2] = rgb3;
Java101ImageUtil.showViewer(rgbs,title);
break;
default:
System.out.println("Bad choice. Bye bye");
}
} while (choice != 'q' && choice != 'Q');
}
public static int [][][] flipVertical(int [][][] rgb){
int w = rgb.length;
int h = rgb[0].length;
int [][][] newRgb = new int[w][h][3];
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
for(int channel=0;channel<3;channel++){
newRgb[i][j][channel] = rgb[i][(h-1)-j][channel];
}
}
}
return newRgb;
}
public static int [][][] genAllRed(int w,int h){
int [][][] rgb = new int[w][h][3];
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
rgb[i][j][0] = 255;
}
}
return rgb;
}
private static String genChoiceMenu(){
String menu = "";
menu = menu.concat("Pick how to show the images\n");
menu = menu.concat("------------------------------------------------------------\n");
menu = menu.concat("1 : Show only the loaded file\n");
menu = menu.concat("2 : Show the loaded file + its flipped version\n");
menu = menu.concat("3 : Show both + a red patch\n");
menu = menu.concat("------------------------------------------------------------\n");
menu = menu.concat(">>");
return menu;
}
private static char getUserChoice(String menu){
System.out.print(menu);
Scanner kb = new Scanner(System.in);
return kb.next().charAt(0);
}
}