-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariousFilters.java
152 lines (132 loc) · 4.84 KB
/
VariousFilters.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
/*author: Srikanth Nemana, UT Computer Science & Mathematics
*
*/
public class VariousFilters {
public static void main(String[] args) throws IOException {
while (true) {
Scanner sc = new Scanner(System.in);
File fileName = checkFile(sc);
BufferedImage img = getImage(fileName);
BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
giveOptions(sc, img, result, fileName);
}
}
public static File checkFile(Scanner sc) {
System.out.print("Please enter the name of the file you wish to process: ");
File fileName = new File(sc.nextLine());
System.out.println();
return fileName;
}
/*
* @param sc: a Scanner connected to System.in
*
*/
public static void giveOptions(Scanner sc, BufferedImage original, BufferedImage end, File writeToOutput) throws IOException {
// TODO Auto-generated method stub
String keepProcessing = "y";
while (keepProcessing.equals("y")) {
System.out.println("Enter 1 to invert image colors\n"
+ "Enter 2 to blur image\n"
+ "Enter 3 to reflect image horizontally\n"
+ "Enter 4 to reflect image vertically\n");
System.out.print("Please choose an image processing method: ");
int result = sc.nextInt();
if (result == 1) {
invertColors(original, end, writeToOutput);
} else if (result == 2) {
blurColors(original, end, writeToOutput);
} else if (result == 3) {
flipOverYAxis(original, end, writeToOutput);
} else if (result == 4) {
flipOverXAxis(original, end, writeToOutput);
} else {
System.out.println("Invalid input.");
}
System.out.print("Do you wish to continue processing this image? ");
keepProcessing = sc.nextLine();
}
}
private static void flipOverXAxis(BufferedImage original, BufferedImage end, File writeToOutput) throws IOException {
// TODO Auto-generated method stub
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
end.setRGB(x, original.getHeight() - y - 1, original.getRGB(x, y));
}
}
ImageIO.write(end, "png", writeToOutput);
}
private static void flipOverYAxis(BufferedImage original, BufferedImage end, File writeToOutput) throws IOException {
// TODO Auto-generated method stub
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
end.setRGB(original.getWidth() - x -1, y, original.getRGB(x, y));
}
}
ImageIO.write(end, "png", writeToOutput);
}
/*
*
*/
private static void blurColors(BufferedImage original, BufferedImage end, File writeToOutput) throws IOException {
// TODO Auto-generated method stub
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
end.setRGB(x, y, getNeighAverage(original, x, y, 3).getRGB());
}
}
ImageIO.write(end, "png", writeToOutput);
}
private static Color getNeighAverage(BufferedImage original, int initX, int initY, int neighSize) {
int rTotal = 0;
int gTotal = 0;
int bTotal = 0;
int count = 0;
for (int x = initX - neighSize; x <= initX + neighSize; x++) {
for (int y = initY - neighSize; y <= initY + neighSize; y++) {
if (inbounds(x, y, original)) {
Color c1 = new Color(original.getRGB(x, y));
rTotal += c1.getRed();
gTotal += c1.getGreen();
bTotal += c1.getBlue();
count++;
}
}
}
return new Color(rTotal/count, gTotal/count, bTotal/count);
}
private static boolean inbounds(int x, int y, BufferedImage original) {
// TODO Auto-generated method stub
return 0 <= x && x < original.getWidth() && 0 <= y && y < original.getHeight();
}
public static BufferedImage getImage(File fileName) throws IOException {
BufferedImage img = null;
try {
img = ImageIO.read(fileName);
} catch (IOException e) {
System.out.println("That wasn't supposed to happen: uh - oh");
System.out.println("Invalid file name/not an image. Restart the program");
}
return img;
}
public static void invertColors(BufferedImage original, BufferedImage end, File fileName) throws IOException {
int max = 255;
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
Color pixel = new Color(original.getRGB(x, y));
int rd = max - pixel.getRed();
int g = max - pixel.getGreen();
int b = max - pixel.getBlue();
end.setRGB(x, y, new Color(rd, g, b).getRGB());
}
}
ImageIO.write(end, "png", fileName);
}
public static void panoramicStitcher(BufferedImage[] o1, BufferedImage end, File fileName) throws IOException {
}
}