-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageFileUtilities.java
105 lines (96 loc) · 2.98 KB
/
ImageFileUtilities.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
import java.util.Scanner;
import java.io.*;
public class ImageFileUtilities{
public static Image read(String filename) throws IOException{
Scanner sc = new Scanner(new File(filename));
//Store Format
String metadata = sc.nextLine();
//Store comments
String s = "#";
while(sc.hasNext(s)){
String comments = sc.nextLine();
}
//store width, height, maxRange
int width = sc.nextInt();
int height = sc.nextInt();
int maxRange = sc.nextInt();
//declare an array of type Pixel to read in pixel vales. Also declare an array of ints for groups of red, green and blue.
Pixel[][] data = new Pixel[height][width];
int[] rgb = new int[3];
//Reads P2 file format
if(metadata.equals("P2")){
while(sc.hasNextInt()){
for(int i=0; i<data.length; i++){
for(int j=0; j<data[0].length; j++){
Pixel grey = new Pixel(sc.nextInt());
data[i][j] = grey;
}
}
}
sc.close();
Image image = new Image(metadata, maxRange, data);
return image;
}
//Reads P3 file format
else if(metadata.equals("P3")){
while(sc.hasNextInt()){
for(int i=0; i<data.length; i++){
for(int j=0; j<data[0].length; j++){
for(int k=0; k<3; k++){
rgb[k] = sc.nextInt();
}
Pixel colour = new Pixel(rgb[0], rgb[1], rgb[2]);
data[i][j]= colour;
}
}
}
sc.close();
Image image = new Image(metadata, maxRange, data);
return image;
}
//Exception
else{
sc.close();
throw new IOException("Must be .pnm or .pgm format");
}
}
//Write Images in P3 format
public static void writePnm(Image image, String filename) throws IOException{
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
int height = image.getHeight();
int width = image.getWidth();
int maxRange = image.getMaxRange();
bw.write("P3 \n");
bw.write(width + " " + height + "\n");
bw.write(maxRange + "\n");
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
bw.write(image.getPixel(i,j).getRed() + " " + image.getPixel(i,j).getGreen() + " " +image.getPixel(i,j).getBlue()+ " ");
}
bw.write("\n");
}
bw.close();
fw.close();
}
//Write images in P2 format
public static void writePgm(Image image, String filename) throws IOException{
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
int height = image.getHeight();
int width = image.getWidth();
int maxRange = image.getMaxRange();
bw.write("P2 \n");
bw.write(width + " " + height + "\n");
bw.write(maxRange + "\n");
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
int intensity = image.getPixel(i,j).grey();
bw.write(intensity + " ");
}
bw.write("\n");
}
bw.close();
fw.close();
}
}