forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpm.java
More file actions
executable file
·159 lines (154 loc) · 4.6 KB
/
Copy pathxpm.java
File metadata and controls
executable file
·159 lines (154 loc) · 4.6 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
package javaforce;
/** XPM image api
*
* Supports XPM3 only.
*
* @author pquiring
*/
import java.io.*;
import java.awt.*;
public class xpm {
private int pos = 0;
private byte data[];
private String getToken() throws Exception {
String ret = "", remark = "";
boolean quote = false, comment = false;
do {
if (pos == data.length) throw new Exception("eof");
char c = (char)data[pos++];
if (quote) {
if (c == '\"') {
return ret;
}
ret += c;
} else if (comment) {
remark += c;
if (remark.endsWith("*/")) {
comment = false;
remark = "";
}
} else {
if (c == '/') {
if (ret.length() > 0) {
pos--;
return ret;
}
comment = true;
continue;
}
if (c == '\"') {
if (ret.length() > 0) {
pos--;
return ret;
}
quote = true;
continue;
}
if (c >= 'a' && c <= 'z') {
ret += c;
} else if (c >= 'A' && c <= 'Z') {
ret += c;
} else if (c >= '0' && c <= '9') {
ret += c;
} else if (c == '_') {
ret += c;
} else if (c == ' ' || c == '\r' || c == '\n') {
if (ret.length() > 0) {
return ret;
}
} else {
//symbol
if (ret.length() > 0) {
pos--;
return ret;
}
ret += c;
return ret;
}
}
} while (true);
};
private int px[];
private int clrs[];
public int[] load(InputStream is, Dimension d) {
try {
data = JF.readAll(is);
pos = 0;
//static char * name[] = {
if (!getToken().equals("static")) throw new Exception("static");
if (!getToken().equals("char")) throw new Exception("char");
if (!getToken().equals("*")) throw new Exception("*");
getToken(); //name - ignore
if (!getToken().equals("[")) throw new Exception("[");
if (!getToken().equals("]")) throw new Exception("]");
if (!getToken().equals("=")) throw new Exception("=");
if (!getToken().equals("{")) throw new Exception("{");
String values = getToken();
String f[] = values.split(" ");
int cols = Integer.valueOf(f[0]);
int rows = Integer.valueOf(f[1]);
int nclrs = Integer.valueOf(f[2]);
int nchars = Integer.valueOf(f[3]);
if (nchars < 1 || nchars > 2) {
throw new Exception("nchars");
}
//hotspot = f[4] f[5] if defined
px = new int[rows * cols];
switch (nchars) {
case 1: clrs = new int[256]; break;
case 2: clrs = new int[65536]; break;
}
if (!getToken().equals(",")) throw new Exception(",");
for(int a=0;a<nclrs;a++) {
String clr = getToken();
String code = clr.substring(0, nchars);
String c = clr.substring(nchars+1, nchars+2);
String value = clr.substring(nchars+3).toLowerCase(); //#123456 None grey100
int idx = 0;
switch (nchars) {
case 1: idx = code.charAt(0); break;
case 2: idx = (code.charAt(0) << 8) + code.charAt(1); break;
}
int rgb = 0;
switch (c.charAt(0)) {
case 'c': {
if (value.equals("none")) {
rgb = 0;
} else if (value.startsWith("grey")) {
int g = Integer.valueOf(value.substring(4));
rgb = (g << 16) + (g << 8) + (g) + 0xff000000;
} else if (value.charAt(0) == '#') {
rgb = Integer.valueOf(value.substring(1), 16) + 0xff000000;
}
clrs[idx] = rgb;
}
}
if (!getToken().equals(",")) throw new Exception(",");
}
int pxpos = 0;
for(int y=0;y<rows;y++) {
String col = getToken();
int p = 0;
int idx = 0;
for(int x=0;x<cols;x++) {
switch (nchars) {
case 1: idx = col.charAt(p++); break;
case 2: idx = (col.charAt(p++) << 8) + col.charAt(p++); break;
}
px[pxpos++] = clrs[idx];
}
if (y < rows-1) {
if (!getToken().equals(",")) throw new Exception(",");
}
}
if (!getToken().equals("}")) throw new Exception("}");
if (!getToken().equals(";")) throw new Exception(";");
d.width = cols;
d.height = rows;
return px;
} catch (Exception e) {
JFLog.log(e);
}
return null;
}
}