-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixmap.java
More file actions
108 lines (79 loc) · 2.15 KB
/
Copy pathPixmap.java
File metadata and controls
108 lines (79 loc) · 2.15 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
import java.io.*;
public class Pixmap {
private static String MAGIC_PGM = "P5\n";
private final int width;
private final int height;
private final int size;
private byte[] data;
public Pixmap(int w, int h) {
width = w;
height = h;
size = width * height;
}
Pixmap(String fileName, String magic) throws IOException {
PixmapReader reader = new PixmapReader(fileName);
if ( !reader.matchKey(magic) )
throw new IOException(fileName + " : wrong magic number");
reader.skipComment('#');
width = reader.getInt();
height = reader.getInt();
size = width * height;
reader.skipLine();
reader.skipComment('#');
reader.skipLine();
data = reader.loadData(size);
reader.close();
}
public Pixmap(String fileName) throws IOException {
this(fileName, MAGIC_PGM);
}
public int getW(){
return width;
}
public int getH(){
return height;
}
final void write(String fileName, String magic, byte[] buffer) {
try {
PixmapWriter writer = new PixmapWriter(fileName);
writer.put(magic);
writer.put("#\n");
writer.put(width + " " + height + "\n255\n");
writer.write(buffer);
writer.close();
System.err.println("'" + fileName + "' wrote");
} catch (IOException e) {
System.err.println("can't write '"+fileName+"'");
}
}
/*
public abstract byte[] getBytes();
*/
public void write(String fileName) {
write(fileName, MAGIC_PGM, data);
}
public static int intValue(byte b) {
if ( b < 0 )
return b + 256;
else
return b;
}
public static byte byteValue(int v) {
//0 <= v <= 255
if ( v > 128 )
return (byte)(v - 256);
else
return (byte)v;
}
public int get(int i, int j){
//retourne entier entre 0 (blanc) et 255 (noir)
return intValue(data[j*getW()+i]);
}
public void set(int i, int j, int v){
assert((v>=0) && (v <=255));//retourne entier entre 0 (blanc) et 255 (noir)
data[j*getW()+i]=byteValue(v);
}
public void setData(){
data = new byte[size];
}
}