-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageEditor.java
More file actions
190 lines (179 loc) · 7.89 KB
/
imageEditor.java
File metadata and controls
190 lines (179 loc) · 7.89 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
class imageEditor {
public static BufferedImage pixelatedBlur(BufferedImage inputImage, int strength) throws Exception {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
if (strength > height || strength > width) {
throw new Exception("Invalid Strength!");
}
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < height / strength; i++) {
for (int j = 0; j < width / strength; j++) {
long red = 0;
long blue = 0;
long green = 0;
for (int k = 0; k < strength; k++) {
for (int l = 0; l < strength; l++) {
Color color = new Color(inputImage.getRGB(strength * j + l, strength * i + k));
red += color.getRed();
blue += color.getBlue();
green += color.getGreen();
}
}
int averageRed = (int) (red / (strength * strength));
int averageBlue = (int) (blue / (strength * strength));
int averageGreen = (int) (green / (strength * strength));
Color averageColor = new Color(averageRed, averageGreen, averageBlue);
for (int k = 0; k < strength; k++) {
for (int l = 0; l < strength; l++) {
outputImage.setRGB(strength * j + l, strength * i + k, averageColor.getRGB());
}
}
}
}
return outputImage;
}
public static BufferedImage rotateImageClockwise(BufferedImage inputImage) {
int height = inputImage.getWidth();
int width = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
outputImage.setRGB(j, i, inputImage.getRGB(i, width - 1 - j));
}
}
return outputImage;
}
public static BufferedImage rotateImageAntiClockwise(BufferedImage inputImage) {
BufferedImage outputImage = rotateImageClockwise(inputImage);
outputImage = rotateImageClockwise(outputImage);
outputImage = rotateImageClockwise(outputImage);
return outputImage;
}
public static BufferedImage convertToGrayScale(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_GRAY);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
outputImage.setRGB(j, i, inputImage.getRGB(j, i));
}
}
return outputImage;
}
public static BufferedImage horizontalInvert(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
outputImage.setRGB(j, i, inputImage.getRGB(width - j - 1, i));
}
}
return outputImage;
}
public static BufferedImage verticalInvert(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
outputImage.setRGB(j, i, inputImage.getRGB(j, height - i - 1));
}
}
return outputImage;
}
public static int updateValue(int n) {
int increased = n + n / 5;
if (increased <= 255)
return increased;
return 255;
}
public static BufferedImage increaseBrightness(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color pixel = new Color(inputImage.getRGB(j, i));
int red = updateValue(pixel.getRed());
int blue = updateValue(pixel.getBlue());
int green = updateValue(pixel.getGreen());
Color newPixel = new Color(red, green, blue);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter filename to edit (must be in the same folder): ");
String inputFilePath = scanner.nextLine();
File inputFile = new File(inputFilePath);
try {
BufferedImage inputImage = ImageIO.read(inputFile);
while (true) {
System.out.println();
System.out.println("===== SELECT A FILTER =====");
System.out.println("1: grayscaled");
System.out.println("2: horizontal flip");
System.out.println("3: vertical flip");
System.out.println("4: rotate clockwise");
System.out.println("5: rotate anticlockwise");
System.out.println("6: increase brightness");
System.out.println("7: blur");
System.out.println("q: QUIT");
System.out.print("\nEnter an option: ");
String choice = scanner.next();
BufferedImage filtered;
if (choice.equals("1")) {
filtered = convertToGrayScale(inputImage);
} else if (choice.equals("2")) {
filtered = horizontalInvert(inputImage);
} else if (choice.equals("3")) {
filtered = verticalInvert(inputImage);
} else if (choice.equals("4")) {
filtered = rotateImageClockwise(inputImage);
} else if (choice.equals("5")) {
filtered = rotateImageAntiClockwise(inputImage);
} else if (choice.equals("6")) {
filtered = increaseBrightness(inputImage);
} else if (choice.equals("7")) {
System.out.print("Enter blur strength (<= width && height): ");
int strength = scanner.nextInt();
try {
filtered = pixelatedBlur(inputImage, strength);
} catch (Exception e) {
System.out.println("Invalid Strength!");
continue;
}
} else if (choice.equals("q")) {
System.out.println("BYE");
break;
} else {
System.out.println("Select a valid option");
continue;
}
System.out.print("Enter the name for output file: ");
String outFileName = scanner.next();
File outputFile = new File(outFileName + ".jpg");
boolean didWrite = ImageIO.write(filtered, "jpg", outputFile);
System.out.println(didWrite ? "\nEdit Successfull!" : "\nEdit Failed!");
}
} catch (IOException a) {
System.out.println("Invalid input file!");
}
scanner.close();
}
}