-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformationFunctions.java
293 lines (256 loc) · 12.2 KB
/
TransformationFunctions.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
public class TransformationFunctions {
public static final int maxRGB = 255;
public static final int minRGB = 0;
private static int[] checkBoundary(int r, int g, int b) {
if (r < 0) r = minRGB; if (r > 255) r = maxRGB;
if (g < 0) g = minRGB; if (g > 255) g = maxRGB;
if (b < 0) b = minRGB; if (b > 255) b = maxRGB;
return new int[] { r, g, b };
}
public static void clearTheWindow(BufferedImage image) {
if (image == null)
return;
EditorFrame.removePreviousImageFromPanel();
}
static int[][] imageArray;
public static void storeCurrentImageState(BufferedImage image) {
if (image == null)
return;
imageArray = new int[image.getWidth()][image.getHeight()];
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
imageArray[x][y] = image.getRGB(x, y);
}
}
System.out.println("The current state is stored!");
System.out.println("Image Width: " + image.getWidth() + " Image Height: " + image.getHeight());
System.out.println("Array Width: " + imageArray.length + " Array Height: " + imageArray[0].length);
}
public static void recoverToPreviousCurrentState(BufferedImage image) {
if (imageArray == null) {
return;
}
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
image.setRGB(x, y, imageArray[x][y]);
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void grayScaleFilter(BufferedImage image) {
if (image == null)
return;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int avg = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
Color gray = new Color(avg, avg, avg);
image.setRGB(x, y, gray.getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void negativeFilter(BufferedImage image) {
if (image == null)
return;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int nR = 255 - color.getRed();
int nG = 255 - color.getGreen();
int nB = 255 - color.getBlue();
int[] vals = checkBoundary(nR, nG, nB);
nR = vals[0];
nG = vals[1];
nB = vals[2];
Color negaiveColor = new Color(nR, nG, nB);
image.setRGB(x, y, negaiveColor.getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void logTransformation(BufferedImage image, float scalingFactor, float upscalingFactor) {
if (image == null)
return;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int logR = (int) (scalingFactor * Math.log1p(color.getRed() + 1) + upscalingFactor);
int logG = (int) (scalingFactor * Math.log1p(color.getGreen() + 1) + upscalingFactor);
int logB = (int) (scalingFactor * Math.log1p(color.getBlue() + 1) + upscalingFactor);
int[] vals = checkBoundary(logR, logG, logB);
logR = vals[0];
logG = vals[1];
logB = vals[2];
image.setRGB(x, y, new Color(logR, logG, logB).getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void powerLawTransformation(BufferedImage image, float scalingFactor, float gamma) {
if (image == null)
return;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int powR = (int) (scalingFactor * Math.pow(color.getRed(), gamma));
int powG = (int) (scalingFactor * Math.pow(color.getGreen(), gamma));
int powB = (int) (scalingFactor * Math.pow(color.getBlue(), gamma));
int[] vals = checkBoundary(powR, powG, powB);
powR = vals[0];
powG = vals[1];
powB = vals[2];
if (powR > 255 || powG > 255 || powB > 255)
System.out.println(powR + ": " + powG + ": " + powB);
image.setRGB(x, y, new Color(powR, powG, powB).getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void exponentialTransformation(BufferedImage image, float scalingFactor) {
if (image == null)
return;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int expR = (int) (scalingFactor * Math.exp(color.getRed()));
int expG = (int) (scalingFactor * Math.exp(color.getGreen()));
int expB = (int) (scalingFactor * Math.exp(color.getBlue()));
int[] vals = checkBoundary(expR, expG, expB);
expR = vals[0];
expG = vals[1];
expB = vals[2];
image.setRGB(x, y, new Color(expR, expG, expB).getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void rotateTheImage(BufferedImage image, double angleInDegree) {
if (image == null) {
JOptionPane.showMessageDialog(null, "The canvas is empty!");
return;
}
image = createRotatedImage(image, angleInDegree);
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
private static BufferedImage createRotatedImage(BufferedImage originalImage, double angleInDegree) {
int width = originalImage.getWidth();
int height = originalImage.getHeight();
BufferedImage rotatedImage = new BufferedImage(width, height, originalImage.getType());
Graphics2D g = rotatedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.rotate(Math.toRadians(angleInDegree), width / 2, height / 2);
g.drawImage(originalImage, 0, 0, null);
g.dispose();
return rotatedImage;
}
private static boolean isCloseColor(Color color, Color colorToRemove) {
int[] removeRGB = new int[3];
removeRGB[0] = colorToRemove.getRed();
removeRGB[1] = colorToRemove.getGreen();
removeRGB[2] = colorToRemove.getBlue();
int max = 0, maxIndex = 0;
for(int i = 0; i<removeRGB.length; i++){
if(max < removeRGB[i]){
max = removeRGB[i];
maxIndex = i;
}
}
// spatial domain for closer range for finding closer tone alpha
var closerToneRed = new int[]{6, 10, 30};
var closerToneGreen = new int[]{10, 6, 30};
var closerToneBlue = new int[]{30, 10, 6};
// var closerToneRed = new int[]{50, 50, 50};
// var closerToneGreen = new int[]{50, 50, 50};
// var closerToneBlue = new int[]{50, 50, 50};
int[] colorRGB = new int[3];
colorRGB[0] = color.getRed();
colorRGB[1] = color.getGreen();
colorRGB[2] = color.getBlue();
if(maxIndex == 0){
if((colorRGB[0] >= removeRGB[0] - closerToneRed[0]) && (colorRGB[0] <= removeRGB[0] + closerToneRed[0])) return true;
if((colorRGB[1] >= removeRGB[1] - closerToneRed[1]) && (colorRGB[1] <= removeRGB[1] + closerToneRed[1])) return true;
if((colorRGB[2] >= removeRGB[2] - closerToneRed[2]) && (colorRGB[2] <= removeRGB[2] + closerToneRed[2])) return true;
} else if(maxIndex == 1){
if((colorRGB[0] >= removeRGB[0] - closerToneGreen[0]) && (colorRGB[0] <= removeRGB[0] + closerToneGreen[0])) return true;
if((colorRGB[1] >= removeRGB[1] - closerToneGreen[1]) && (colorRGB[1] <= removeRGB[1] + closerToneGreen[1])) return true;
if((colorRGB[2] >= removeRGB[2] - closerToneGreen[2]) && (colorRGB[2] <= removeRGB[2] + closerToneGreen[2])) return true;
} else if(maxIndex == 2){
if((colorRGB[0] >= removeRGB[0] - closerToneBlue[0]) && (colorRGB[0] <= removeRGB[0] + closerToneBlue[0])) return true;
if((colorRGB[1] >= removeRGB[1] - closerToneBlue[1]) && (colorRGB[1] <= removeRGB[1] + closerToneBlue[1])) return true;
if((colorRGB[2] >= removeRGB[2] - closerToneBlue[2]) && (colorRGB[2] <= removeRGB[2] + closerToneBlue[2])) return true;
} else {
return false;
}
return false;
}
public static void replaceTheColor(BufferedImage image, Color colorToRemove, Color colorToPut) {
if (image == null || colorToPut == null || colorToRemove == null)
return;
System.out.println("Color to remove: " + colorToRemove + " Color to put: " + colorToPut);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
if (color.equals(colorToRemove) || isCloseColor(color, colorToRemove)) {
image.setRGB(x, y, colorToPut.getRGB());
}
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void scalingPos(BufferedImage image, float delX, float delY){
if(delX < 0 || delX > 255) return;
if(delY < 0 || delY > 255) return;
for(int y = 0; y<image.getHeight(); y++){
for(int x = 0; x<image.getWidth(); x++){
int newX = (int)(x * delX);
int newY = (int)(y * delY);
try{
if(newX < 0 || newY < 0) continue;
else if(newX >= image.getWidth() || newY >= image.getHeight()) continue;
else if(newX >= image.getHeight() || newY >= image.getWidth()) continue;
else image.setRGB(x, y, image.getRGB(newX, newY));
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println("Coordinate out of bounds Exception: ");
System.out.println("x: " + x + " y: " + y + " newX: " + newX + " newY: " + newY);
}
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
public static void shiftRGB(BufferedImage image, Color shiftingColorValues, boolean isPositiveShift){
if(image == null || shiftingColorValues == null) return;
int factor = 1;
if(isPositiveShift) factor = 1;
else factor = -1;
for(int y = 0; y<image.getHeight(); y++){
for(int x = 0; x<image.getWidth(); x++){
Color color = new Color(image.getRGB(x, y));
int shiftRed = color.getRed() + factor * shiftingColorValues.getRed();
int shiftGreen = color.getGreen() + factor * shiftingColorValues.getGreen();
int shiftBlue = color.getBlue() + factor * shiftingColorValues.getBlue();
int vals[] = checkBoundary(shiftRed, shiftGreen, shiftBlue);
shiftRed = vals[0];
shiftGreen = vals[1];
shiftBlue = vals[2];
image.setRGB(x, y, new Color(shiftRed, shiftGreen, shiftBlue).getRGB());
}
}
EditorFrame.removePreviousImageFromPanel();
EditorFrame.displayImage(image);
}
}