-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPMFile.java
More file actions
173 lines (147 loc) · 5.59 KB
/
PPMFile.java
File metadata and controls
173 lines (147 loc) · 5.59 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
package task2;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class PPMFile {
ArrayList<Integer> array;
private byte bytes[] = null;
private double doubles[] = null;
private String filename = null;
int height = 0;
int width = 0;
BufferedImage bi;
File f;
public PPMFile(String filename) throws FileNotFoundException, IOException {
array= new ArrayList<Integer>();
this.filename = filename;
readImage();
bi= PPMFile.ppm(width, height, 255, array.toArray());
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public byte[] getBytes() {
return bytes;
}
public double[] getDouble() {
return doubles;
}
private void readImage() throws FileNotFoundException, IOException, NumberFormatException {
// read PPM format image
bytes = null;
char buffer; // character in PPM header
String id = new String(); // PPM magic number
String dim = new String(); // image dimension as a string
int count = 0;
File f = new File(filename);
FileInputStream isr = new FileInputStream(f);
boolean weird = false;
do {
buffer = (char) isr.read();
id = id + buffer;
count++;
} while (buffer != '\n' && buffer != ' ');
if (id.charAt(0) == 'P') {
buffer = (char) isr.read();
count++;
if (buffer == '#') {
do {
buffer = (char) isr.read();
count++;
} while (buffer != '\n');
count++;
buffer = (char) isr.read();
}
// second header line is "width height\n"
do {
dim = dim + buffer;
buffer = (char) isr.read();
count++;
} while (buffer != ' ' && buffer != '\n');
width = Integer.parseInt(dim.trim());
dim = new String();
buffer = (char) isr.read();
count++;
do {
dim = dim + buffer;
buffer = (char) isr.read();
count++;
} while (buffer != ' ' && buffer != '\n');
height = Integer.parseInt(dim);
do {
buffer = (char) isr.read();
count++;
} while (buffer != ' ' && buffer != '\n');
bytes = new byte[height * width];
doubles = new double[height * width];
buffer= (char) isr.read();
String counter="";
while(!"A".equals(String.valueOf(buffer))){
if(buffer=='\n'){
array.add(Integer.parseInt(counter));
counter="";
}
if(buffer!='\n')
counter+=buffer;
buffer= (char) isr.read();
}
isr.close();
} else {
width = height = 0;
doubles = new double[0];
bytes = new byte[0];
throw new NumberFormatException("Wrong header information!");
}
}
static public BufferedImage ppm(int width, int height, int maxcolval, Object[] data2){
if(maxcolval<256){
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
if(maxcolval==255){
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data2.length);x++){
r=((int)data2[k++]) & 0xFF;
g=((int)data2[k++]) & 0xFF;
b=((int)data2[k++]) & 0xFF;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
else{
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data2.length);x++){
r=((int)data2[k++]) & 0xFF;r=((r*255)+(maxcolval>>1))/maxcolval;
g=((int)data2[k++]) & 0xFF;g=((g*255)+(maxcolval>>1))/maxcolval;
b=((int)data2[k++]) & 0xFF;b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
return image;
}
else{
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+6)<data2.length);x++){
r=(((int)data2[k++]) & 0xFF)|((((int)data2[k++]) & 0xFF)<<8);r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=(((int)data2[k++]) & 0xFF)|((((int)data2[k++]) & 0xFF)<<8);g=((g*255)+(maxcolval>>1))/maxcolval;
b=(((int)data2[k++]) & 0xFF)|((((int)data2[k++]) & 0xFF)<<8);b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
return image;
}
}
void save(String imgName) throws IOException{
f=new File("/Users/Maestro/Desktop/"+imgName+".jpg");
ImageIO.write(bi, "jpg", f);
}
}