-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.java
231 lines (210 loc) · 8.57 KB
/
Image.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
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class Image {
// GRAYSCALE
public static BufferedImage converttogrey(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;
}
// BRIGHTNESS JYDA KRNA
public static BufferedImage increasebrightness(BufferedImage inputImage, int increase) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Color pixel = new Color(inputImage.getRGB(x, y));
int r = pixel.getRed();
int g = pixel.getGreen();
int b = pixel.getBlue();
g = g + (increase * g / 100);
b = b + (increase * b / 100);
r = r + (increase * r / 100);
if (r > 255)
r = 255;
if (b > 255)
b = 255;
if (g > 255)
g = 255;
if (r < 0)
r = 0;
if (b < 0)
b = 0;
if (g < 0)
g = 0;
Color newpixel = new Color(r, g, b);
outImage.setRGB(x, y, newpixel.getRGB());
}
}
return outImage;
}
// BRIGHTNESS KM KRNA
public static BufferedImage decreasebrightness(BufferedImage inputImage, int decrease) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Color pixel = new Color(inputImage.getRGB(x, y));
int r = pixel.getRed();
int g = pixel.getGreen();
int b = pixel.getBlue();
g = g - (decrease * g / 100);
b = b - (decrease * b / 100);
r = r - (decrease * r / 100);
if (r > 255)
r = 255;
if (b > 255)
b = 255;
if (g > 255)
g = 255;
if (r < 0)
r = 0;
if (b < 0)
b = 0;
if (g < 0)
g = 0;
Color newpixel = new Color(r, g, b);
outImage.setRGB(x, y, newpixel.getRGB());
}
}
return outImage;
}
public static BufferedImage invertImage(BufferedImage inputImage) {
int height = inputImage.getHeight();
int width = inputImage.getWidth();
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Color pixel = new Color(inputImage.getRGB(x, y));
int r = 255 - pixel.getRed();
int g = 255 - pixel.getGreen();
int b = 255 - pixel.getBlue();
Color invertedPixel = new Color(r, g, b);
outputImage.setRGB(x, y, invertedPixel.getRGB());
}
}
return outputImage;
}
// ROTATE
public static BufferedImage rotate(BufferedImage inputImage, int degrees) {
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(degrees), inputImage.getWidth() / 2, inputImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage outputImage = new BufferedImage(inputImage.getHeight(), inputImage.getWidth(),
inputImage.getType());
op.filter(inputImage, outputImage);
return outputImage;
}
// BLURRR KARENGE
public static BufferedImage Blur(BufferedImage inputImage, int radius) {
int width = inputImage.getWidth();
int height = inputImage.getHeight();
BufferedImage blurredImage = new BufferedImage(width, height, inputImage.getType());
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int avgRed = 0;
int avgGreen = 0;
int avgBlue = 0;
int numPixels = 0;
for (int offsetY = -radius; offsetY <= radius; offsetY++) {
for (int offsetX = -radius; offsetX <= radius; offsetX++) {
int posX = x + offsetX;
int posY = y + offsetY;
if (posX >= 0 && posX < width && posY >= 0 && posY < height) {
int pixel = inputImage.getRGB(posX, posY);
avgRed += (pixel >> 16) & 0xFF;
avgGreen += (pixel >> 8) & 0xFF;
avgBlue += pixel & 0xFF;
numPixels++;
}
}
}
avgRed /= numPixels;
avgGreen /= numPixels;
avgBlue /= numPixels;
int blurredPixel = (avgRed << 16) | (avgGreen << 8) | avgBlue;
blurredImage.setRGB(x, y, blurredPixel);
}
}
return blurredImage;
}
private static void saveImage(BufferedImage image, String outputPath) {
try {
File output = new File(outputPath);
ImageIO.write(image, "jpg", output);
System.out.println("Image saved as " + outputPath);
} catch (IOException e) {
e.printStackTrace();
}
}
// MAIN CHIZ
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the path of the input image: ");
String inputImagePath = scanner.nextLine();
BufferedImage image = ImageIO.read(new File(inputImagePath));
if (image == null) {
System.out.println("Could not read the input image.");
return;
}
System.out.print("Enter the path for the output image: ");
String outputImagePath = scanner.nextLine();
System.out.println("Image loaded successfully!");
System.out.println("1. Increase Brightness");
System.out.println("2. Decrease Brightness");
System.out.println("3. Grayscale");
System.out.println("4. Rotate");
System.out.println("5. Blur");
System.out.println("6. Invert Image");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
image = increasebrightness(image, 20);
break;
case 2:
image = decreasebrightness(image, 20);
break;
case 3:
image = converttogrey(image);
break;
case 4:
image = rotate(image, 90);
break;
case 5:
image = Blur(image, 20);
break;
case 7:
System.out.println("Exiting...");
break;
case 6:
image = invertImage(image);
break;
default:
System.out.println("Invalid choice.");
}
if (choice >= 1 && choice <= 6) {
saveImage(image, outputImagePath);
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}