-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrayImage.java
More file actions
136 lines (119 loc) · 3.63 KB
/
GrayImage.java
File metadata and controls
136 lines (119 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
****************************************************************************
* Flavio Andrade
* 4-25-2017
* Compilation: javac GrayImage.java
* Execution: java GrayImage Images/Ferrari.jpeg speed
*****************************************************************************
*/
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.net.URL;
import java.io.IOException;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GrayImage {
public BufferedImage image = null;
public int height, width;
public String picName;
public int i = 0;
public ImageIcon icon;
public JLabel label;
public JFrame frame;
// Make a new GrayImage object.
public GrayImage(String name) {
picName = name;
if (name == null) throw new IllegalArgumentException("Enter a valid file.");
// Read in the Image
try { image = ImageIO.read(new File(name));}
catch (IOException e) {}
height = image.getHeight();
width = image.getWidth();
}
// Return original image if it has not been modified yet.
public BufferedImage returnOriginal() {
return image;
}
// Take the image and make it GrayScale.
public BufferedImage makeGray(int start, int fin) {
for (int c = 0; c < width; c++) {
for (int h = start; h < fin; h++) {
int color = image.getRGB(c, h);
image.setRGB(c, h, this.makeColorGray(new Color(color)).getRGB());
}
}
start = fin;
return image;
}
// Take a Color object and make it gray.
public Color makeColorGray(Color color) {
if (color == null) throw new IllegalArgumentException("Provide a valid color.");
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
double grayValue = (0.299 * red) + (0.587 * blue) + (0.110 * green);
int g = (int) Math.round(grayValue);
return new Color(g, g, g);
}
// Set up the display;
public void setUp(BufferedImage img, int x, int y, String name) {
icon = new ImageIcon(image);
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setIcon(icon);
frame = new JFrame(name);
frame.setSize(x, y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setVisible(true);
}
// Display the image.
public void drawImage(BufferedImage img, int x, int y) {
this.setUp(img, x, y, picName);
}
// Show the image on the screen after it has been turned to grayscale.
public void showImage() {
this.drawImage(this.makeGray(0, height - 1), width, height);
}
// Turn the image gray overtime.
public void animateGray(int h, int x, int y) {
if (h > height / 100) { h = height / 100; }
this.setUp(image, x, y, "Animate Gray");
/* Keep track of previous location, so loop does not start at the
begining of image. */
int i = 0;
int a = 0;
int stop = height / h;
while (true) {
a += h;
if (a > h * stop) {
this.makeGray(stop * h, height - 1);
break;
}
this.makeGray(i, a);
i = a;
icon = new ImageIcon(image);
label.setIcon(icon);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setVisible(true);
frame.repaint();
}
}
// Save the new Image.
public void save(BufferedImage img, String name) {
File newImage = new File(name + ".jpg");
try { ImageIO.write(img, "jpg", newImage); }
catch(IOException e) {}
}
public static void main(String args[]) {
String name = args[0];
int h = Integer.parseInt(args[1]);
GrayImage gr = new GrayImage(name);
gr.animateGray(h, gr.width, gr.height);
gr.save(gr.image, "Modified");
}
}