-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageEditorCui.java
437 lines (373 loc) · 17.6 KB
/
imageEditorCui.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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;
public class imageEditorCui {
public static BufferedImage convertToGray(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 rotate90Clockwise(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
outputImage.setRGB(i,j, inputImage.getRGB(j,i));
}
}
int temp=0;
for(int i=0; i<width; i++){
for(int j=0; j<height/2; j++){
temp = outputImage.getRGB(height-j-1, i);
outputImage.setRGB(height-j-1, i, outputImage.getRGB(j,i));
outputImage.setRGB(j, i, temp);
}
}
return outputImage;
}
public static BufferedImage rotate90AntiClockwise(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
for(int i=height-1; i>=0; i--){
for(int j=width-1; j>=0; j--){
outputImage.setRGB(height-i-1,width-j-1, inputImage.getRGB(j,i));
}
}
int temp=0;
for(int i=0; i<width; i++){
for(int j=0; j<height/2; j++){
temp = outputImage.getRGB(height-j-1, i);
outputImage.setRGB(height-j-1, i, outputImage.getRGB(j,i));
outputImage.setRGB(j, i, temp);
}
}
return outputImage;
}
public static BufferedImage horizInvert(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
int temp=0;
for(int i=0; i<height; i++){
for(int j=0; j<width/2; j++){
temp = inputImage.getRGB(width-j-1, i);
inputImage.setRGB(width-j-1, i, inputImage.getRGB(j,i));
inputImage.setRGB(j, i, temp);
}
}
return inputImage;
}
public static BufferedImage vertInvert(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
int temp=0;
for(int i=0; i<width; i++){
for(int j=0; j<height/2; j++){
temp = inputImage.getRGB(i, height-j-1);
inputImage.setRGB(i, height-j-1, inputImage.getRGB(i,j));
inputImage.setRGB(i, j, temp);
}
}
return inputImage;
}
public static BufferedImage changeBrightness(BufferedImage inputImage, int changePercentage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
System.out.println("Please input percentage by which you want to change brightness : ");
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color pixel = new Color(inputImage.getRGB(j, i));
int red = pixel.getRed() * (changePercentage+100)/100;
int blue = pixel.getBlue() * (changePercentage+100)/100;
int green = pixel.getGreen() * (changePercentage+100)/100;
if(red>255) red=255;
else if(red<0) red=0;
if(blue>255) blue=255;
else if(blue<0) blue=0;
if(green>255) green=255;
else if(green<0) green=0;
Color newPixel = new Color(red, green, blue);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static BufferedImage pixelBlur(BufferedImage inputImage, int pixelBlurAmount){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i+=pixelBlurAmount){
for(int j=0; j<width; j+=pixelBlurAmount){
int avgR = 0;
int avgG = 0;
int avgB = 0;
int count = 0;
for(int k=i; k<i+pixelBlurAmount; k++){
for(int l=j; l<j+pixelBlurAmount; l++){
Color pixel = new Color(inputImage.getRGB(l, k));
avgR += pixel.getRed();
avgB += pixel.getBlue();
avgG += pixel.getGreen();
count++;
}
}
avgR = avgR/count;
avgB = avgB/count;
avgG = avgG/count;
Color newPixel = new Color(avgR, avgG, avgB);
for(int k=i; k<i+pixelBlurAmount; k++){
for(int l=j; l<j+pixelBlurAmount; l++){
outputImage.setRGB(l, k, newPixel.getRGB());
}
}
}
}
return outputImage;
}
public static BufferedImage smoothBlur(BufferedImage inputImage, int smoothBlurAmount){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
int avgR = 0;
int avgG = 0;
int avgB = 0;
int count = 0;
for(int k=i-smoothBlurAmount/2; k<i+smoothBlurAmount/2 && k<height; k++){
for(int l=j-smoothBlurAmount/2; l<j+smoothBlurAmount/2 && l<width; l++){
if(k<0) k=0;
if(l<0) l=0;
Color pixel = new Color(inputImage.getRGB(l, k));
avgR += pixel.getRed();
avgB += pixel.getBlue();
avgG += pixel.getGreen();
count++;
}
}
avgR = avgR/count;
avgB = avgB/count;
avgG = avgG/count;
Color newPixel = new Color(avgR, avgG, avgB);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static BufferedImage changeContrast(BufferedImage inputImage,int contrastPercentage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color pixel = new Color(inputImage.getRGB(j, i));
int red = pixel.getRed();
int blue = pixel.getBlue();
int green = pixel.getGreen();
red = (int) (128 + ( (contrastPercentage+100) * (red - 128))/100);
green = (int) (128 + ( (contrastPercentage+100) * (green - 128))/100);
blue = (int) (128 + ( (contrastPercentage+100) * (blue - 128))/100);
red = Math.max(0, Math.min(255, red));
green = Math.max(0, Math.min(255, green));
blue = Math.max(0, Math.min(255, blue));
Color newPixel = new Color(red, green, blue);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static BufferedImage changeSaturation(BufferedImage inputImage,int saturationChange){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color pixel = new Color(inputImage.getRGB(j, i));
float[] hsl = Color.RGBtoHSB(pixel.getRed(), pixel.getGreen(), pixel.getBlue(), null);
hsl[1] *= ( saturationChange+100 )/100;
hsl[1] = Math.max(0, Math.min(1, hsl[1]));
int rgb = Color.HSBtoRGB(hsl[0], hsl[1], hsl[2]);
outputImage.setRGB(j, i, rgb);
}
}
return outputImage;
}
public static BufferedImage createNegativeImage(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color pixel = new Color(inputImage.getRGB(j, i));
int red = 255 - pixel.getRed();
int green = 255 - pixel.getGreen();
int blue = 255 - pixel.getBlue();
Color newPixel = new Color(red, green, blue);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static BufferedImage applyFilter(BufferedImage inputImage, int filterType) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color pixel = new Color(inputImage.getRGB(j, i));
int red = pixel.getRed() ;
int green = pixel.getGreen() ;
int blue = pixel.getBlue() ;
switch (filterType) {
case 1:
red = pixel.getRed() + 50;
green = pixel.getGreen() - 50;
blue = pixel.getBlue() - 50;
break;
case 2:
red = pixel.getRed() - 50;
green = pixel.getGreen() - 50;
blue = pixel.getBlue() + 50;
break;
case 3:
red = pixel.getRed() - 50;
green = pixel.getGreen() + 50;
blue = pixel.getBlue() - 50;
break;
default:
}
red = Math.max(0, Math.min(255, red));
green = Math.max(0, Math.min(255, green));
blue = Math.max(0, Math.min(255, blue));
Color newPixel = new Color(red, green, blue);
outputImage.setRGB(j, i, newPixel.getRGB());
}
}
return outputImage;
}
public static BufferedImage imageEgdeDetection(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 2; i < height; i++) {
for (int j = 2; j < width; j++) {
Color pixel1 = new Color(inputImage.getRGB(j, i));
Color pixel2 = new Color(inputImage.getRGB(j-2, i-2));
int redDiff = (pixel1.getRed() - pixel2.getRed());
int greenDiff = (pixel1.getGreen() - pixel2.getGreen());
int blueDiff = (pixel1.getBlue() - pixel2.getBlue());
redDiff = Math.max(0, Math.min(255, redDiff));
greenDiff = Math.max(0, Math.min(255, greenDiff));
blueDiff = Math.max(0, Math.min(255, blueDiff));
Color diffColor = new Color(redDiff, greenDiff, blueDiff);
outputImage.setRGB(j-2, i-2, diffColor.getRGB());
}
}
return outputImage;
}
public static void main(String Args[])throws IOException{
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("\nPlease provide input image pathname : ");
String inputPath = sc.next();
if(!inputPath.toLowerCase().endsWith(".jpg")){
System.out.println("\nGiven file is not of .jpg format!!");
continue;
}
File inputFile = new File(inputPath);
BufferedImage inputImage = ImageIO.read(inputFile);
BufferedImage outputImage = inputImage;
System.out.println("\nPlease select the operation to be performed : \n");
System.out.println("Select 1 for grayscaling the image. ");
System.out.println("Select 2 for rotating image by 90 degree clockwise. ");
System.out.println("Select 3 for rotating image by 90 degree Anticlockwise. ");
System.out.println("Select 4 for horizontal inverting of image. ");
System.out.println("Select 5 for vertical inverting of image. ");
System.out.println("Select 6 for changing brightness of image. ");
System.out.println("Select 7 for pixel blurring the image. ");
System.out.println("Select 8 for smooth blurring the image. ");
System.out.println("Select 9 for changing contrast of image. ");
System.out.println("Select 10 for changing saturation of image. ");
System.out.println("Select 11 for creating negative image of given image. ");
System.out.println("Select 12 for applying a filter(Red, Blue, Green) on image. ");
System.out.println("Select 13 for edge detection of image. ");
System.out.println("Select 14 to Exit ");
int option = sc.nextInt();
switch(option){
case 1:
outputImage = convertToGray(inputImage);
break;
case 2:
outputImage = rotate90Clockwise(inputImage);
break;
case 3:
outputImage = rotate90AntiClockwise(inputImage);
break;
case 4:
outputImage = horizInvert(inputImage);
break;
case 5:
outputImage = vertInvert(inputImage);
break;
case 6:
System.out.println("Please input percentage by which you want to change brightness : ");
int changePercentage = sc.nextInt();
outputImage = changeBrightness(inputImage, changePercentage);
break;
case 7:
System.out.println("Please input pixels by which you want to blur the image : ");
int pixelBlurAmount = sc.nextInt();
outputImage = pixelBlur(inputImage, pixelBlurAmount);
break;
case 8:
System.out.println("Please input pixels by which you want to blur the image : ");
int smoothBlurAmount = sc.nextInt();
outputImage = smoothBlur(inputImage, smoothBlurAmount);
break;
case 9:
System.out.println("Please input percentage by which you want to change contrast : ");
int contrastPercentage = sc.nextInt();
outputImage = changeContrast(inputImage, contrastPercentage);
break;
case 10:
System.out.println("Please input percentage by which you want to change saturation : ");
int saturationPercentage = sc.nextInt();
outputImage = changeSaturation(inputImage, saturationPercentage);
break;
case 11:
outputImage = createNegativeImage(inputImage);
break;
case 12:
System.out.println("\nSelect filter : \n1. Red \n2. Blue \n3. Green\n");
int filterType = sc.nextInt();
outputImage = applyFilter(inputImage, filterType);
break;
case 13:
outputImage = imageEgdeDetection(inputImage);
break;
case 14:
System.exit(0);
default:
System.out.println("\n INVALID OPTION \n");
}
System.out.println("\nPlease enter output file path : ");
String outputPath = sc.next();
File outputFile = new File(outputPath);
if(outputPath.toLowerCase().endsWith(".jpg")){
System.out.println("\nYour file has been saved");
}
ImageIO.write(outputImage, "jpg", outputFile);
}
}
}