Repository navigation
Expand file tree
/
Copy pathImageProcessing.java
More file actions
257 lines (245 loc) · 8.41 KB
/
Copy pathImageProcessing.java
File metadata and controls
257 lines (245 loc) · 8.41 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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageProcessing {
public static int [][][] loadBMP(String filename) { //converts an image to a string array
BufferedImage img = null;
try {
img = ImageIO.read(new File(filename));
} catch (IOException e) {
System.out.println(e);
}
int height = img.getHeight();
int width = img.getWidth();
int image[][][] = new int[height][width][3];
int rgb;
for (int h=0; h<height; h++)
{
for (int w = 0; w<width; w++)
{
rgb = img.getRGB(w, h);
image[h][w][0] = (rgb >> 16 ) & 0x000000FF;
image[h][w][1] = (rgb >> 8 ) & 0x000000FF;
image[h][w][2] = (rgb) & 0x000000FF;
}
}
return image;
}
public static void saveBMP(int image[][][], String filename) { //saves an array with depth 3 as a picture with color bmp type
int height = image.length;
int width = image[0].length;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int p, r, g, b;
for (int h=0; h<height; h++)
{
for (int w = 0; w<width; w++)
{
r = image[h][w][0];
g = image[h][w][1];
b = image[h][w][2];
// set the pixel value
p = (r << 16) | (g << 8) | b;
img.setRGB(w, h, p);
}
}
// write image
try {
File f = new File(filename);
ImageIO.write(img, "bmp", f);
}
catch (IOException e) {
System.out.println(e);
}
}
public static void saveGrayscaleBMP(int image[][], String filename) { //saves an array with depth 2 as a gray scale picture bmp type
int height = image.length;
int width = image[0].length;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int h=0; h<height; h++)
{
for (int w = 0; w<width; w++)
{
int p = (image[h][w] << 16) | (image[h][w] << 8) | image[h][w];
img.setRGB(w, h, p);
}
}
// write image
try {
File f = new File(filename);
ImageIO.write(img, "bmp", f);
}
catch (IOException e) {
System.out.println(e);
}
}
public static int numOfNumbers(String a) { //finds how many numbers there are in a string
int nums=0;
for(int i=0;i<a.length();i++) {
for(int c='0';c<='9';c++) {
if(a.charAt(i)==c)
nums++;
}
}
return nums;
}
public static void main(String[] args) {
String photo_edit=args[0]; //what type of photo edit function the user chooses
switch (photo_edit) { //6 different functions with certain names
case "-display":
if(numOfNumbers(args[args.length-2])!=args[args.length-2].length()) //Case error 4: checks if the string is not a number
System.out.println("Missing arguments about pixel position. Program is terminated");
else {
int x=Integer.parseInt(args[args.length-2]), y=Integer.parseInt(args[args.length-1]);
for(int i=1;i<args.length-2;i++)
display(args[i],x,y);
}
break;
case "-hflip":
for(int i=1;i<args.length;i++)
hflip(args[i]);
break;
case "-vflip":
for(int i=1;i<args.length;i++)
vflip(args[i]);
break;
case "-right90":
for(int i=1;i<args.length;i++)
right90(args[i]);
break;
case "-left90":
for(int i=1;i<args.length;i++)
left90(args[i]);
break;
case "-grey":
for(int i=1;i<args.length;i++)
grey(args[i]);
break;
default: //In case the photo edit function given is not available within this program or it is written incorrectly
System.out.println("Wrong option given. Program is terminated");
}
}
public static void display(String pic_name, int x, int y) { //Gives the values or Red, Green and Blue in integer numbers (color pick RGB values)
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length-1, height=arr.length-1;
if(x>height) //Case error 1: Out of bounds height
System.out.println("Height out of bounds!");
else if(y>width) //Case error 2: Out of bounds width
System.out.println("Width out of bounds!");
else if(!pic_name.endsWith(".bmp")) //Case error 3: Not a BMP picture
System.out.println("Image is not bmp. Program is terminated");
else
System.out.println("RGB["+x+','+y+"] = ("+arr[x][y][0]+','+arr[x][y][1]+','+arr[x][y][2]+')');
}
public static void hflip(String pic_name) { //Flips an image horizontally
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length, height=arr.length, color=arr[0][0].length;
int temp;
if(!pic_name.endsWith(".bmp")) //Case error 3
System.out.println("Image is not bmp. Program is terminated");
else {
for(int h=0;h<height;h++) {
for(int w=0;w<=width/2;w++) {
for(int c=0;c<color;c++) {
temp=arr[h][w][c];
arr[h][w][c]=arr[h][width-1-w][c];
arr[h][width-1-w][c]=temp;
}
}
}
saveBMP(arr, "hflip-"+pic_name);
}
}
public static void vflip(String pic_name) { //Flips an image vertically
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length, height=arr.length, color=3;
int temp;
if(!pic_name.endsWith(".bmp")) //Case error 3
System.out.println("Image is not bmp. Program is terminated");
else {
for(int w=0;w<width;w++) {
for(int h=0;h<height/2;h++) {
for(int c=0;c<color;c++) {
temp=arr[h][w][c];
arr[h][w][c]=arr[height-1-h][w][c];
arr[height-1-h][w][c]=temp;
}
}
}
saveBMP(arr, "vflip-"+pic_name);
}
}
public static void right90(String pic_name) { //Rotates an image right by 90 degrees
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length, height=arr.length, color=arr[0][0].length;
int[][][] arr_r90=new int[width][height][color];
if(!pic_name.endsWith(".bmp")) //Case error 3
System.out.println("Image is not bmp. Program is terminated");
else {
for(int w=0;w<width;w++) {
for(int h=0;h<height;h++) {
for(int c=0;c<color;c++) {
arr_r90[w][height-1-h][c]=arr[h][w][c];
}
}
}
saveBMP(arr_r90, "right90-"+pic_name);
}
}
public static void left90(String pic_name) { //Rotates an image left by 90 degrees
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length, height=arr.length, color=arr[0][0].length;
int[][][] arr_l90=new int[width][height][color];
if(!pic_name.endsWith(".bmp")) //Case error 3
System.out.println("Image is not bmp. Program is terminated");
else {
for(int h=0;h<height;h++) {
for(int w=0;w<width;w++) {
for(int c=0;c<color;c++) {
arr_l90[width-1-w][h][c]=arr[h][w][c];
}
}
}
saveBMP(arr_l90, "left90-"+pic_name);
}
}
public static void grey(String pic_name) { //Turns a colorful image to a black and white image (grey scale filter)
int[][][] arr=loadBMP(pic_name);
int width=arr[0].length, height=arr.length;
int[][] arr_grey=new int[height][width];
if(!pic_name.endsWith(".bmp")) //Case error 3
System.out.println("Image is not bmp. Program is terminated");
else {
for(int h=0;h<height;h++) {
for(int w=0;w<width;w++) {
arr_grey[h][w]=(int)(arr[h][w][0]*0.299+arr[h][w][1]*0.587+arr[h][w][2]*0.114);
}
}
saveGrayscaleBMP(arr_grey,"grey-"+pic_name);
}
}
}
/**
* Author: Theonitsa Chrysanthou
* Written: 09/11/2024
* Last updated: 09/11/2024
*
* Compilation: javac ImageProcessing.java
* Execution: java ImageProcessing -grey image1.bmp
* java ImageProcessing -vflip image1.bmp image2.bmp
* java ImageProcessing hflip image2.bmp
* java ImageProcessing -grey image1.jpg
* java ImageProcessing -left90 image3.bmp
* java ImageProcessing -right90 image1.bmp
* java ImageProcessing -display image1.bmp 1 1
* java ImageProcessing -display 3x4 3 0
* java ImageProcessing -display 3x4.bmp 0
* java ImageProcessing -display 3x4.bmp 0 4
* java ImageProcessing -hflip image1.bmp
* java ImageProcessing -display 3x4
*
* The program above is a simple photo editor with basic functions (horizontal flip, vertical flip, left rotation by 90 degrees, right rotation by 90 degrees,
* gray scale filter, color pick RGB values) and it can only process pictures of the bmp type. The logic of the program lays within multi-dementional tables' logic,
* since the pictures being processed are first turned into 3D tables of integer values that represent the values of red, green and blue that are used in a certain
* pixel to create its color.
*/