-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageManipulator.java
188 lines (161 loc) · 6.7 KB
/
ImageManipulator.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
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
/*
File containing the ImageManipulator class with methods to manipulate images.
Author: Ibne Nahian (evilprince2009)
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.stream.Stream;
public class ImageManipulator {
private final String raw_image_path;
private BufferedImage image_buffer;
private File file_buffer;
private final String format;
public ImageManipulator(String raw_image_path, String format) {
this.raw_image_path = raw_image_path;
this.image_buffer = null;
this.file_buffer = null;
this.format = format;
}
public void addSepia(String out_image_path) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int red = (pixels >> 16) & 0xff;
int green = (pixels >> 8) & 0xff;
int blue = pixels & 0xff;
int avgRed = (int) (0.393 * red + 0.769 * green + 0.189 * blue);
int avgGreen = (int) (0.349 * red + 0.686 * green + 0.168 * blue);
int avgBlue = (int) (0.272 * red + 0.534 * green + 0.131 * blue);
red = Math.min(avgRed, 255);
green = Math.min(avgGreen, 255);
blue = Math.min(avgBlue, 255);
pixels = (autos << 24) | (red << 16) | (green << 8) | blue;
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(out_image_path, format);
}
public void addRedDevil(String out_image_path) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int red = (pixels >> 16) & 0xff;
pixels = (autos << 24) | (red << 16) | (0);
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(out_image_path, format);
}
public void addNegativeEffect(String out_image_path) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int red = (pixels >> 16) & 0xff;
int green = (pixels >> 8) & 0xff;
int blue = pixels & 0xff;
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
pixels = (autos << 24) | (red << 16) | (green << 8) | blue;
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(out_image_path, format);
}
public void addGreenMatrix(String out_image_path) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int green = (pixels >> 8) & 0xff;
pixels = (autos << 24) | (0) | (green << 8);
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(out_image_path, format);
}
public void addGrayScale(String out_image_path) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int red = (pixels >> 16) & 0xff;
int green = (pixels >> 8) & 0xff;
int blue = pixels & 0xff;
int average = (int) Stream.of(red, blue, green).mapToInt(Integer::intValue)
.summaryStatistics()
.getAverage();
pixels = (autos << 24) | (average << 16)
| (average << 8) | average;
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(out_image_path, format);
}
public void addBlueEffect(String outputImagePath) {
setBuffer();
int width = image_buffer.getWidth();
int height = image_buffer.getHeight();
for (int vertical = 0; vertical < height; vertical++) {
for (int horizontal = 0; horizontal < width; horizontal++) {
int pixels = image_buffer.getRGB(horizontal, vertical);
int autos = (pixels >> 24) & 0xff;
int blue = pixels & 0xff;
pixels = (autos << 24) | (0) | blue;
image_buffer.setRGB(horizontal, vertical, pixels);
}
}
writeImage(outputImagePath, format);
}
private void setBuffer() {
try {
file_buffer = new File(raw_image_path);
image_buffer = ImageIO.read(file_buffer);
} catch (IOException ex) {
logger(ex.getMessage());
}
}
private void writeImage(String file_path, String format) {
try {
file_buffer = new File(file_path);
ImageIO.write(image_buffer, format, file_buffer);
logger("Image successfully saved to " + file_path);
} catch (IOException ex) {
logger(ex.getMessage());
}
}
private void logger(String message) {
final String file_path = "details.log";
try {
File file = new File(file_path);
if (!file.exists())
file.createNewFile();
try (FileWriter writer = new FileWriter(file_path, true)) {
writer.append("\n").append(message);
}
} catch (Exception e) {
System.err.println("A full log can be found at " + file_path);
}
}
}