-
Notifications
You must be signed in to change notification settings - Fork 1
/
SkinMuncher.java
302 lines (287 loc) · 12.7 KB
/
SkinMuncher.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.imageio.ImageIO;
public class SkinMuncher {
private static class Mapping {
public int srcX, srcY, srcWidth, srcHeight;
public int dstX, dstY;
}
private static int outputWidth, outputHeight;
private static int skinWidth, skinHeight;
private static int displayWidth, displayHeight;
private static List<Mapping> mappings = new ArrayList<Mapping>();
private static List<String> addenda = new ArrayList<String>();
private static class ParseException extends RuntimeException {
public ParseException(int line, Throwable cause) {
super("At line " + line, cause);
}
}
private static String inputName, outputName;
private static void loadMunchScript(String name) throws IOException, ParseException {
InputStream is = new FileInputStream(name);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
int lineno = 0;
int skipped = 0;
while ((line = reader.readLine()) != null) {
lineno++;
int hash = line.indexOf('#');
if (hash != -1)
line = line.substring(0, hash);
line = line.trim();
if (line.length() == 0)
continue;
try {
StringTokenizer tok = new StringTokenizer(line);
String t = tok.nextToken().toLowerCase();
if (t.equals("input")) {
inputName = tok.nextToken();
} else if (t.equals("output")) {
outputName = tok.nextToken().replace("$APPSUPPORT", System.getenv("HOME") + "/Library/Application Support");
} else if (t.equals("map")) {
Mapping m = new Mapping();
m.srcX = Integer.parseInt(tok.nextToken());
m.srcY = Integer.parseInt(tok.nextToken());
m.srcWidth = Integer.parseInt(tok.nextToken());
m.srcHeight = Integer.parseInt(tok.nextToken());
t = tok.nextToken().toLowerCase();
if (!t.equals("to"))
throw new Exception("syntax error");
m.dstX = Integer.parseInt(tok.nextToken());
m.dstY = Integer.parseInt(tok.nextToken());
mappings.add(m);
} else if (t.equals("skip")) {
Mapping m = mappings.remove(mappings.size() - 1);
while (tok.hasMoreTokens()) {
String range = tok.nextToken();
int dash = range.indexOf('-');
int from, to;
if (dash == -1)
from = to = Integer.parseInt(range);
else {
from = Integer.parseInt(range.substring(0, dash));
to = Integer.parseInt(range.substring(dash + 1));
}
if (to < from || from < m.srcY || to > m.srcY + m.srcHeight)
throw new Exception("bad skipper " + range);
if (m.srcX < skinWidth)
skipped += to - from;
if (from > m.srcY) {
Mapping nm = new Mapping();
nm.srcX = m.srcX;
nm.srcY = m.srcY;
nm.srcWidth = m.srcWidth;
nm.srcHeight = from - m.srcY;
nm.dstX = m.dstX;
nm.dstY = m.dstY;
mappings.add(nm);
m.dstY += nm.srcHeight;
}
m.srcHeight -= to - m.srcY;
m.srcY = to;
}
if (m.srcHeight > 0)
mappings.add(m);
} else if (t.equals("skin")) {
skinWidth = Integer.parseInt(tok.nextToken());
skinHeight = Integer.parseInt(tok.nextToken());
} else if (t.equals("displaysize")) {
displayWidth = Integer.parseInt(tok.nextToken());
displayHeight = Integer.parseInt(tok.nextToken());
} else if (t.equals("add")) {
addenda.add(line.substring(4).trim());
} else {
throw new Exception("unrecognized token \"" + t + "\"");
}
try {
t = tok.nextToken();
throw new Exception("extraneous token \"" + t + "\"");
} catch (Exception e2) {}
} catch (Exception e) {
throw new ParseException(lineno, e);
}
}
for (int i = 0; i < mappings.size(); i++) {
Mapping m = mappings.get(i);
int bottom = m.dstY + m.srcHeight;
int right = m.dstX + m.srcWidth;
if (bottom > outputHeight)
outputHeight = bottom;
if (right > outputWidth)
outputWidth = right;
}
skinHeight -= skipped;
}
private static void munchGif() throws IOException {
BufferedImage srcImage = ImageIO.read(new File(inputName + ".gif"));
WritableRaster srcRaster = srcImage.getRaster();
WritableRaster dstRaster = srcImage.getRaster().createCompatibleWritableRaster(outputWidth, outputHeight);
for (int i = 0; i < mappings.size(); i++) {
Mapping mapping = mappings.get(i);
int xmax = mapping.srcX + mapping.srcWidth;
int ymax = mapping.srcY + mapping.srcHeight;
int xoff = mapping.dstX - mapping.srcX;
int yoff = mapping.dstY - mapping.srcY;
for (int x = mapping.srcX; x < xmax; x++) {
for (int y = mapping.srcY; y < ymax; y++) {
dstRaster.setDataElements(x + xoff, y + yoff, srcRaster.getDataElements(x, y, null));
}
}
}
BufferedImage dstImage = new BufferedImage(srcImage.getColorModel(), dstRaster, true, null);
ImageIO.write(dstImage, "GIF", new File(outputName + ".gif"));
}
private static String transformPoint(String coords) throws Exception {
StringTokenizer tok = new StringTokenizer(coords, ",");
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
for (int i = 0; i < mappings.size(); i++) {
Mapping mapping = mappings.get(i);
if (x >= mapping.srcX && x < mapping.srcX + mapping.srcWidth
&& y >= mapping.srcY && y < mapping.srcY + mapping.srcHeight)
return (x - mapping.srcX + mapping.dstX) + "," + (y - mapping.srcY + mapping.dstY);
}
throw new Exception("unmappable point: " + coords);
}
private static int min(int x, int y) {
return x < y ? x : y;
}
private static int max(int x, int y) {
return x > y ? x : y;
}
private static int abs(int x) {
return x < 0 ? -x : x;
}
private static String transformRect(String coords) throws Exception {
StringTokenizer tok = new StringTokenizer(coords, ",");
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
int w = Integer.parseInt(tok.nextToken());
int h = Integer.parseInt(tok.nextToken());
int xo = 0, yo = 0;
int amt = 0;
int cx = 0, cy = 0, cw = 0, ch = 0;
for (int i = 0; i < mappings.size(); i++) {
Mapping mapping = mappings.get(i);
if (x + w < mapping.srcX || mapping.srcX + mapping.srcWidth < x)
continue;
if (y + h < mapping.srcY || mapping.srcY + mapping.srcHeight < y)
continue;
int xx = max(x, mapping.srcX);
int yy = max(y, mapping.srcY);
int ww = min(x + w, mapping.srcX + mapping.srcWidth) - xx;
int hh = min(y + h, mapping.srcY + mapping.srcHeight) - yy;
int a = ww * hh;
if (a > amt) {
xo = mapping.srcX - mapping.dstX;
yo = mapping.srcY - mapping.dstY;
amt = a;
cx = xx;
cy = yy;
cw = ww;
ch = hh;
}
}
if (amt == 0)
throw new Exception("unmappable point: " + coords);
return (cx - xo) + "," + (cy - yo) + "," + cw + "," + ch;
}
private static void munchLayout() throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(inputName + ".layout"));
PrintWriter writer = new PrintWriter(outputName + ".layout");
String line;
while ((line = reader.readLine()) != null) {
StringTokenizer tok = new StringTokenizer(line, " ");
StringBuffer buf = new StringBuffer();
if (line.startsWith("Skin:")) {
buf.append(tok.nextToken());
buf.append(" ");
buf.append("0,0," + skinWidth + "," + skinHeight);
writer.println(buf.toString());
} else if (line.startsWith("Display:")) {
buf.append(tok.nextToken());
buf.append(" ");
buf.append(transformPoint(tok.nextToken()));
while (tok.hasMoreTokens()) {
buf.append(" ");
buf.append(tok.nextToken());
}
writer.println(buf.toString());
if (skinWidth > skinHeight)
writer.println("DisplaySize: " + displayWidth + "," + displayHeight + " -1 -1 " + displayHeight);
for (int i = 0; i < addenda.size(); i++)
writer.println(addenda.get(i));
} else if (line.startsWith("Key:")) {
buf.append(tok.nextToken());
buf.append(" ");
buf.append(tok.nextToken());
buf.append(" ");
buf.append(transformRect(tok.nextToken()));
buf.append(" ");
buf.append(transformRect(tok.nextToken()));
buf.append(" ");
buf.append(transformPoint(tok.nextToken()));
writer.println(buf.toString());
} else if (line.startsWith("Annunciator:") || line.startsWith("AltBkgd:")) {
buf.append(tok.nextToken());
buf.append(" ");
buf.append(tok.nextToken());
buf.append(" ");
buf.append(transformRect(tok.nextToken()));
buf.append(" ");
buf.append(transformPoint(tok.nextToken()));
writer.println(buf.toString());
} else if (line.startsWith("AltKey:")) {
buf.append(tok.nextToken());
buf.append(" ");
buf.append(tok.nextToken());
buf.append(" ");
buf.append(tok.nextToken());
buf.append(" ");
buf.append(transformPoint(tok.nextToken()));
writer.println(buf.toString());
} else {
writer.println(line);
}
}
writer.close();
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java SkinMuncher <munch-script>");
return;
}
try {
loadMunchScript(args[0]);
if (mappings.isEmpty())
throw new Exception("no mappings specified");
if (inputName == null)
throw new Exception("no input name specified");
if (outputName == null)
throw new Exception("no output name specified");
if (skinWidth == 0 || skinHeight == 0)
throw new Exception("no skin size specified");
if (skinWidth > skinHeight && (displayWidth == 0 || displayHeight == 0))
throw new Exception("no display size specified");
munchGif();
munchLayout();
} catch (Exception e) {
e.printStackTrace();
if (outputName != null) {
new File(outputName + ".gif").delete();
new File(outputName + ".layout").delete();
}
}
}
}