-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageEditor.java
More file actions
451 lines (336 loc) · 16.9 KB
/
imageEditor.java
File metadata and controls
451 lines (336 loc) · 16.9 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Scanner;
public class imageEditor{
// Function to perform inversion on an input image
static BufferedImage inversion(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++){
// Get the RGB values of the pixel
Color pixel = new Color(inputImage.getRGB(j,i));
int red = pixel.getRed();
int green = pixel.getGreen();
int blue = pixel.getBlue();
// Invert the color channels
red = 255-red;
green = 255-green;
blue = 255-blue;
// Create a new pixel with inverted colors
Color newPixel = new Color(red , green , blue);
// Set the pixel in the output image
outputImage.setRGB(j , i , newPixel.getRGB());
}
}
return outputImage;
}
// Function to perform edge detection on an input image
static BufferedImage edgeDetection(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
// Create an output image with slightly larger dimensions
BufferedImage outputImage = new BufferedImage(width+5 , height+5 ,BufferedImage.TYPE_INT_RGB);
// Copy the input image to the output image
for(int i=0 ; i<height ; i++){
for(int j=0 ; j<width ; j++){
Color pixel = new Color(inputImage.getRGB(j,i));
outputImage.setRGB(j , i , pixel.getRGB());
}
}
// Perform edge detection by subtracting nearby pixel values
for(int i=5 ; i<height+5 ; i++){
for(int j=5 ; j<width+5 ; j++){
Color pixel = new Color(inputImage.getRGB(j-5 , i-5));
Color newPixel = new Color(outputImage.getRGB(j, i));
int finalRed = newPixel.getRed()-pixel.getRed();
int finalGreen = newPixel.getGreen()-pixel.getGreen();
int finalBlue = newPixel.getBlue()-pixel.getBlue();
// Ensure color values are non-negative
if(finalRed<0) {finalRed=0;}
if(finalGreen<0) {finalGreen=0;}
if(finalBlue<0) {finalBlue=0;}
Color finalPixel = new Color(finalRed , finalGreen , finalBlue);
// Set the pixel in the output image
outputImage.setRGB(j , i , finalPixel.getRGB() );
}
}
return outputImage;
}
// Function to crop an input image to a circle with a given radius
static BufferedImage cropToCircle(BufferedImage inputImage , int radius){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
int centreRow = height/2;
int centreColumn = width/2;
BufferedImage outputImage = new BufferedImage(width , height , BufferedImage.TYPE_INT_RGB);
Color blackColor = new Color(0 ,0 ,0);
for(int i=0 ; i<height ; i++){
for(int j=0 ; j<width ; j++){
// Check if the pixel is outside the circle
if((j-centreColumn)*(j-centreColumn)+(i-centreRow)*(i-centreRow) > radius*radius){
// Set the pixel to black
outputImage.setRGB(j, i , blackColor.getRGB());
}else{
// Copy the pixel from the input image
outputImage.setRGB(j , i , inputImage.getRGB(j,i));
}
}
}
return outputImage;
}
// Function to adjust the contrast of an input image
static BufferedImage contrast(BufferedImage inputImage , int percentage){
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 green = pixel.getGreen();
int blue = pixel.getBlue();
// Adjust the color channels based on the percentage
if(red>127) {red = red + (red*percentage/100);}
else {red = red - (red*percentage/100);}
if(green>127) {green = green + (green*percentage/100);}
else {green = green - (green*percentage/100);}
if(blue>127) {blue = blue + (blue*percentage/100);}
else {blue = blue - (blue*percentage/100);}
// Ensure color values are within the valid range
if(red>255) {red=255;}
if(red<0) {red=0;}
if(green>255) {green=255;}
if(green<0) {green=0;}
if(blue>255) {blue=255;}
if(blue<0) {blue=0;}
Color newPixel = new Color(red , green , blue);
// Set the pixel in the output image
outputImage.setRGB(j ,i , newPixel.getRGB());
}
}
return outputImage;
}
// Function to apply a blur effect to an input image
static BufferedImage blurr(BufferedImage inputImage , int pixelCount){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width , height , BufferedImage.TYPE_INT_RGB);
int rowStart=0;
int rowEnd = pixelCount-1;
while(rowEnd<height){
int columnStart = 0;
int columnEnd = pixelCount-1 ;
while(columnEnd<width){
int sumRed = 0;
int sumGreen = 0;
int sumBlue = 0;
// Calculate the average color values in the pixel square
for(int i=rowStart ; i<=rowEnd ; i++){
for(int j=columnStart ; j<=columnEnd ; j++){
Color pixel = new Color(inputImage.getRGB(j,i));
sumRed += pixel.getRed();
sumBlue += pixel.getBlue();
sumGreen += pixel.getGreen();
}
}
int avgRed = sumRed/(pixelCount*pixelCount);
int avgBlue = sumBlue/(pixelCount*pixelCount);
int avgGreen = sumGreen/(pixelCount*pixelCount);
Color newPixel = new Color(avgRed , avgGreen , avgBlue);
// Set the entire pixel square in the output image to the average color
for(int i=rowStart ; i<=rowEnd ; i++){
for(int j=columnStart ; j<=columnEnd ; j++){
outputImage.setRGB(j , i , newPixel.getRGB() );
}
}
columnStart+=pixelCount;
columnEnd+=pixelCount;
}
rowStart+=pixelCount;
rowEnd+=pixelCount;
}
return outputImage;
}
// Function to mirror an input image vertically
static BufferedImage mirror(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/2 ; j++){
// Swap pixels horizontally to create a mirror effect
Color pixel = new Color(inputImage.getRGB(j,i));
outputImage.setRGB(j,i,inputImage.getRGB(inputImage.getWidth()-1-j , i));
outputImage.setRGB(inputImage.getWidth()-1-j , i , pixel.getRGB());
}
}
return outputImage;
}
// Function to rotate an input image anti-clockwise
static BufferedImage rotateAntiClockwise(BufferedImage inputImage){
BufferedImage outputImage = rotateClockwise(inputImage);
outputImage = rotateClockwise(outputImage);
outputImage = rotateClockwise(outputImage);
return outputImage;
}
// Function to rotate an input image clockwise
static BufferedImage rotateClockwise(BufferedImage inputImage){
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(height , width , BufferedImage.TYPE_INT_RGB);
//transpose
for(int i=0 ; i<width ; i++){
for(int j=0; j<height ; j++){
Color pixel = new Color(inputImage.getRGB(i,j));
outputImage.setRGB(j,i,pixel.getRGB());
}
}
// Mirror the transposed image to rotate clockwise
outputImage = mirror(outputImage);
return outputImage;
}
// Function to change the brightness of an input image
static BufferedImage changeBrightness(BufferedImage inputImage , int increase){
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 = pixel.getRed();
int blue = pixel.getBlue();
int green = pixel.getGreen();
red = red+(increase*red/100);
blue = blue+(increase*blue/100);
green = green+(increase*green/100);
// Adjust the color channels based on the increase percentage
if(red>255){red=255;}
if(blue>255){blue=255;}
if(green>255){green=255;}
// Ensure color values are within the valid range
if(red<0){red=0;}
if(blue<0){blue=0;}
if(green<0){green=0;}
Color newPixel = new Color(red , green , blue);
// Set the pixel in the output image
outputImage.setRGB(j,i,newPixel.getRGB());
}
}
return outputImage;
}
// Function to convert an input image to grayscale
static BufferedImage convertToGreyScale(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++){
// Set the grayscale pixel in the output image
outputImage.setRGB(j,i, inputImage.getRGB(j,i));
}
}
return outputImage;
}
// Function to create a pencil sketch effect from an input image
static BufferedImage pencilSketch(BufferedImage inputImage){
BufferedImage outputImage;
// Apply edge detection
outputImage = edgeDetection(inputImage);
// Invert the colors to create a sketch effect
outputImage = inversion(outputImage);
return outputImage;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
boolean iterate = true;
while(iterate){
System.out.println("chose the operation you would like to perform on the image:\n\n1.convert to grey-scale\n2.change brightness\n3.rotate clockwise\n4.rotate anti-clockwise\n5.mirror\n6.blurr.\n7.contrast\n8.crop to circle\n9.edge detection\n10.inversion\n11.pencil sketch\n12.exit\n");
System.out.print("enter choice: ");
int choice = sc.nextInt();
if(choice==12){
break;
}
sc.nextLine();
System.out.print("\nshare the address of the image: ");
String address = sc.nextLine();
File inputFile = new File(address);
try{
BufferedImage inputImage = ImageIO.read(inputFile);
switch(choice){
case 1: BufferedImage grayScale = convertToGreyScale(inputImage);
File grayScaleImage = new File("grayScaleImage.jpeg");
ImageIO.write(grayScale , "jpeg" , grayScaleImage);
System.out.println("\ndone!\n");
break;
case 2: System.out.print("change brightness by what percentage: ");
int factor = sc.nextInt();
BufferedImage changedBrightness = changeBrightness(inputImage , factor);
File changedBrightnessImage = new File("changedBrightness.jpeg");
ImageIO.write(changedBrightness , "jpeg" , changedBrightnessImage);
System.out.println("\ndone!\n");
break;
case 3: BufferedImage rotatedClockwise = rotateClockwise(inputImage);
File rotatedClockwiseImage = new File("rotatedClockwise.jpeg");
ImageIO.write(rotatedClockwise , "jpeg" , rotatedClockwiseImage);
System.out.println("\ndone!\n");
break;
case 4: BufferedImage rotatedAntiClockwise = rotateAntiClockwise(inputImage);
File rotatedAntiClockwiseImage = new File("rotatedAntiClockwise.jpeg");
ImageIO.write(rotatedAntiClockwise , "jpeg" , rotatedAntiClockwiseImage);
System.out.println("\ndone!\n");
break;
case 5: BufferedImage mirrored = mirror(inputImage);
File mirroredImage = new File("mirrored.jpeg");
ImageIO.write(mirrored , "jpeg" , mirroredImage);
System.out.println("\ndone!\n");
break;
case 6: System.out.print("enter length of pixel square: ");
int sideLength = sc.nextInt();
BufferedImage blurred = blurr(inputImage , sideLength);
File blurredImage = new File("blurred.jpeg");
ImageIO.write(blurred , "jpeg" , blurredImage);
System.out.println("\ndone!\n");
break;
case 7: System.out.print("\ncontrast by what percentage: ");
int percentage = sc.nextInt();
BufferedImage contrasted = contrast(inputImage , percentage);
File contrastedImage = new File("contrasted.jpeg");
ImageIO.write(contrasted , "jpeg" , contrastedImage);
System.out.println("\ndone!\n");
break;
case 8: System.out.print("\nenter radius: ");
int radius = sc.nextInt();
BufferedImage croppedToCircle = cropToCircle(inputImage , radius);
File croppedToCircleImage = new File("croppedToCircle.jpeg");
ImageIO.write(croppedToCircle , "jpeg" , croppedToCircleImage);
System.out.println("\ndone!\n");
break;
case 9: BufferedImage edgeDetected = edgeDetection(inputImage);
File edgeDetectedImage = new File("edgeDetected.jpeg");
ImageIO.write(edgeDetected , "jpeg" , edgeDetectedImage);
System.out.println("\ndone!\n");
break;
case 10: BufferedImage inverted = inversion(inputImage);
File invertedImage = new File("inverted.jpeg");
ImageIO.write(inverted , "jpeg" , invertedImage);
System.out.println("\ndone!\n");
break;
case 11: BufferedImage pencilSketched = pencilSketch(inputImage);
File pencilSketchedImage = new File("pencilSketched.jpeg");
ImageIO.write(pencilSketched , "jpeg" , pencilSketchedImage);
System.out.println("\ndone!\n");
break;
default: System.out.println("\nplease enter a valid option.\n");
}
}catch(IOException e){
e.printStackTrace();
}
}
System.out.println("\n\n----PROGRAM ENDED----\n\n");
}
}