-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject3.java
280 lines (242 loc) · 8.21 KB
/
project3.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
import java.util.*;
import java.io.*;
public class project3{
public static void main(String[] args) throws FileNotFoundException{
int mode = Integer.parseInt(args[0]);
//The variable 'mode' will show which part should be operated.
File f = new File(args[1]);
//The variable 'f' is the file which is input.
Scanner file = new Scanner(f);
//I use scanner here to get format and integers easily.
String imageFormat = file.nextLine();
//'imageFormat' represents 'P3' everywhere in my code.
int rows = file.nextInt();
int columns = file.nextInt();
//'rows' and 'columns' shows how much column and row ppm file has.
int max = file.nextInt();
//'max' is the maximum value of a pixel.
//Here, I read the ppm file and create an 3D array.
int[][][] imageArray = new int[rows][columns][3];
for(int e=0; e<rows; e++){
for(int r=0; r<columns; r++){
for(int k=0; k<3; k++){
imageArray[e][r][k]= file.nextInt();
}
}
}
//Now, due to the value of 'mode', it's operation time.
if(mode==0){
//when 'mode' is zero,code just does the writing another file.
writingPPMFile(imageFormat,max,imageArray);
}if(mode==1){
//when 'mode' is one, code gives a black-and-white version of image.
blackAndWhite(imageArray, imageFormat, max);
}if(mode==2){
//when 'mode' is two, code should operate a filter on image.
//before the method, first I changed filter to a 2D array.
File filter2 = new File(args[2]);
Scanner file2 = new Scanner(filter2);
String size = file2.nextLine();
int a = Integer.parseInt(size.substring(0,size.indexOf("x")));
int b = Integer.parseInt(size.substring(size.indexOf("x")+1));
int[][] filter = new int[a][b];
for(int h=0; h<a; h++){
for(int e=0; e<b; e++){
filter[h][e] = file2.nextInt();
}
}
//Then,method time.
filter(imageArray,max,filter,imageFormat);
}if(mode==3){
//when 'mode' is three, code should quantize the image.
//First, I took the range.
int range = Integer.parseInt(args[2]);
//Then, the method.
quantization(imageArray,range,imageFormat,max);
}
}
//To write pixels to another file.
public static void writingPPMFile(String imageFormat, int max, int[][][] array)throws FileNotFoundException {
//First, to create a new file, we should throw FileNotFoundException.
//Here, I create the output file.
File f = new File("output.ppm");
PrintStream output = new PrintStream(f);
int rows = array.length;
int columns = array[0].length;
//First I write to the file the necessary things.
output.println(imageFormat);
output.println(rows + " " + columns);
output.println(max);
//then, I wrote the value of pixels.
for(int e=0; e<rows; e++ ){
for(int r=0; r<columns; r++){
for(int k=0; k<3; k++){
output.print(array[e][r][k]+ " ");
}
output.print("\t");
}
}
}
//To get a black-and-version of image.
public static void blackAndWhite(int[][][] array, String imageFormat, int max)throws FileNotFoundException{
//First, to create a new file, we should throw FileNotFoundException.
//To get a black-and-white version, we should average the pixel values.
for(int e=0; e<array.length; e++){
for(int r=0; r<array[0].length; r++){
int sum =0;
for(int k=0; k<3; k++){
sum += array[e][r][k];
}
for(int k=0; k<3; k++){
array[e][r][k]=sum/3;
}
}
}
//Now, in the array pixel values like 'x,x,x'.
// Rest of method is the same as writingPPMFile method.
File f = new File("black-and-white.ppm");
PrintStream output = new PrintStream(f);
int rows = array.length;
int columns = array[0].length;
output.println(imageFormat);
output.println(rows + " " + columns);
output.println(max);
for(int e=0; e<rows; e++ ){
for(int r=0; r<columns; r++){
for(int k=0; k<3; k++){
output.print(array[e][r][k]+ " ");
}
output.print("\t");
}
}
}
//To operate filter on image.
public static void filter(int[][][] array,int max, int[][] filter, String imageFormat)throws FileNotFoundException{
//First, to create a new file, we should throw FileNotFoundException.
int a = filter.length;
int b = filter[0].length;
int[][][] arrayNew = new int[array.length-2][array[0].length-2][3];
//I create a new array to storage results of calculations. It is smaller than first array.
for(int e=0; e<array.length-a+1; e++){
for(int r=0; r<array[0].length-b+1; r++){
for(int k=0; k<3; k++){
int sum=0;
for(int h=0; h<a; h++){
for(int t=0; t<b; t++){
sum += filter[h][t]*array[e+h][r+t][k];
}
}
if(sum/a*b<0){
sum=0;
}if(sum/a*b>max){
sum=max;
}
arrayNew[e][r][k] = sum/a*b;
}
}
}
// now, arrayNew should be black-and-white. This part is same as black-and-white method.
for(int e=0; e<arrayNew.length; e++){
for(int r=0; r<arrayNew[0].length; r++){
int sum =0;
for(int k=0; k<3; k++){
sum += arrayNew[e][r][k];
}
for(int k=0; k<3; k++){
arrayNew[e][r][k]=sum/3;
}
}
}
//This part is the same as writingPPMFile method.
File f = new File("convolution.ppm");
PrintStream output = new PrintStream(f);
int rows = arrayNew.length;
int columns = arrayNew[0].length;
output.println(imageFormat);
output.println(rows + " " + columns);
output.println(max);
for(int e=0; e<rows; e++ ){
for(int r=0; r<columns; r++){
for(int k=0; k<3; k++){
output.print(arrayNew[e][r][k]+ " ");
}
output.print("\t");
}
}
}
//To quantize the image.
public static void quantization(int[][][] array,int range, String imageFormat, int max)throws FileNotFoundException{
//First, to create a new file, we should throw FileNotFoundException.
//I looked every pixel to look every neighbor.
for(int k=0; k<3; k++){
for(int r=0; r<array[0].length; r++){
for(int e=0; e<array.length; e++){
int value = array[e][r][k];
recursionQuan(array, value, range, e, r, k);
}
}
}
//This part is the same as writingPPMFile method.
File f = new File("quantized.ppm");
PrintStream output = new PrintStream(f);
int rows = array.length;
int columns = array[0].length;
output.println(imageFormat);
output.println(rows + " " + columns);
output.println(max);
for(int e=0; e<rows; e++ ){
for(int r=0; r<columns; r++){
for(int k=0; k<3; k++){
output.print(array[e][r][k]+ " ");
}
output.print("\t");
}
}
}
//To look every neighbor of pixel.
public static int[][][] recursionQuan(int[][][] array, int value, int range, int e, int r, int k){
if(e<array.length-1){
//I looked the right pixel.
if(array[e+1][r][k]<=value+range && value-range<=array[e+1][r][k] && !(array[e+1][r][k]==value)){
array[e+1][r][k] = value;
recursionQuan(array, value, range, e+1, r, k);
}
}
if(e>0){
//I looked the left pixel.
if(array[e-1][r][k]<=value+range && value-range<=array[e-1][r][k] && !(array[e-1][r][k]==value)){
array[e-1][r][k] = value;
recursionQuan(array, value, range, e-1, r, k);
}
}
if(r<array[0].length-1){
//I looked the under.
if(array[e][r+1][k]<=value+range && value-range<=array[e][r+1][k] && !(array[e][r+1][k]==value)){
array[e][r+1][k] = value;
recursionQuan(array, value, range, e, r+1, k);
}
}
if(r>0){
//I looked the top.
if(array[e][r-1][k]<=value+range && value-range<=array[e][r-1][k] && !(array[e][r-1][k]==value)){
array[e][r-1][k] = value;
recursionQuan(array, value, range, e, r-1, k);
}
}
if(k<2){
//I looked inside.
if(array[e][r][k+1]<=value+range && value-range<=array[e][r][k+1] && !(array[e][r][k+1]==value)){
array[e][r][k+1] = value;
recursionQuan(array, value, range, e, r, k+1);
}
}
if(k>0){
//I looked outside.
if(array[e][r][k-1]<=value+range && value-range<=array[e][r][k-1] && !(array[e][r][k-1]==value)){
array[e][r][k-1] = value;
recursionQuan(array, value, range, e, r, k-1);
}
}
return array;
}
}