1 package org.catacomb.graph.gui;
2
3
4 import org.catacomb.report.E;
5
6 import java.awt.Color;
7 import java.awt.image.BufferedImage;
8 import java.awt.image.WritableRaster;
9 import java.io.*;
10 import java.util.StringTokenizer;
11
12 import javax.imageio.ImageIO;
13
14 public class Iconizer {
15
16
17
18 public static void main(String[] argv) {
19 File fin = new File(argv[0]);
20 int dim = Integer.parseInt(argv[1]);
21 int r = Integer.parseInt(argv[2]);
22 int g = Integer.parseInt(argv[3]);
23 int b = Integer.parseInt(argv[4]);
24 File fout = new File(argv[5]);
25
26 double[][] xy = readPerimiter(readStringFromFile(fin));
27
28
29 Color cgray = new Color(140, 140, 140);
30
31 BufferedImage bim = filledPolygonImage(xy[0], xy[1], dim, cgray,
32 0.02, 0.04, 2.5);
33
34
35 BufferedImage bimc = filledPolygonImage(xy[0], xy[1], dim,
36 new Color(r, g, b), 0., 0., 1.5);
37
38 bim.getGraphics().drawImage(bimc, 0, 0, null);
39
40
41 try {
42 ImageIO.write(bim, "png", fout);
43 } catch (Exception ex) {
44 ex.printStackTrace();
45 }
46
47 }
48
49
50
51
52
53 public static BufferedImage filledPolygonImage(double[] xp, double[] yp,
54 int dim, Color col,
55 double osx, double osy,
56 double elf) {
57
58
59
60 BufferedImage transim = new BufferedImage(dim, dim,
61 BufferedImage.TYPE_INT_ARGB);
62
63 WritableRaster alpha = transim.getAlphaRaster();
64 WritableRaster raster = transim.getRaster();
65
66
67 double[][] d = alphaPixelize(xp, yp, dim, osx, osy, elf);
68
69
70 int w = dim;
71 int h = dim;
72
73 int r = col.getRed();
74 int g = col.getGreen();
75 int b = col.getBlue();
76
77
78
79 int[] itrans = new int[1];
80 itrans[0] = 0;
81
82 int[] ipxin = new int[4];
83
84 for (int i = 0; i < w; i++) {
85 for (int j = 0; j < h; j++) {
86
87 if (d[i][j] > 0.) {
88
89 ipxin[0] = r;
90 ipxin[1] = g;
91 ipxin[2] = b;
92
93 raster.setPixel(i, j, ipxin);
94 }
95
96
97
98 itrans[0] = (int)(255 * d[i][j]);
99 alpha.setPixel(i, j, itrans);
100 }
101 }
102
103 return transim;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public static double[][] alphaPixelize(double[] xp0, double[] yp0, int dim,
128 double osx, double osy,
129 double elf) {
130
131 double[][] ret = new double[dim][dim];
132
133 double[] xp = rerange(xp0);
134 double[] yp = rerange(yp0);
135
136
137 double dx = 1. / dim;
138 double dy = 1. / dim;
139
140 int nsamp = 10;
141
142
143 for (int i = 0; i < dim; i++) {
144 for (int j = 0; j < dim; j++) {
145 double x0 = (i + 0.5) * dx;
146 double y0 = (j + 0.5) * dy;
147
148 ret[i][j] = coverage(xp, yp, x0-osx, y0-osy,
149 elf * dx, elf * dy, nsamp);
150
151 }
152 }
153 return ret;
154 }
155
156
157
158
159
160
161 private static double coverage(double[] xp, double[] yp,
162 double xc0, double yc0, double dx0, double dy0,
163 int nsamp) {
164
165 int nin = 0;
166
167 double x0 = xc0 - 0.5 * dx0;
168 double y0 = yc0 - 0.5 * dy0;
169
170
171 double dx = dx0 / nsamp;
172 double dy = dy0 / nsamp;
173
174 for (int i = 0; i < nsamp; i++) {
175 for (int j = 0; j < nsamp; j++) {
176 double x = x0 + (i + 0.5) * dx;
177 double y = y0 + (i + 0.5) * dy;
178
179 if (pointIsInside(x, y, xp, yp)) {
180 nin++;
181 }
182 }
183 }
184
185 return (1. * nin) / (nsamp * nsamp);
186
187 }
188
189
190
191
192
193
194
195 private static double[] rerange(double[] ap0) {
196 double amin = ap0[0];
197 double amax = ap0[0];
198 for (int i = 0; i < ap0.length; i++) {
199 if (ap0[i] < amin) {
200 amin = ap0[i];
201 }
202 if (ap0[i] > amax) {
203 amax = ap0[i];
204 }
205 }
206
207 amin -= 0.1 * (amax - amin);
208 amax += 0.1 * (amax - amin);
209
210
211 double[] ap = new double[ap0.length];
212 for (int i = 0; i < ap0.length; i++) {
213 ap[i] = (ap0[i] - amin) / (amax - amin);
214 }
215 return ap;
216 }
217
218
219
220
221
222
223
224
225 private static boolean pointIsInside(double x, double y,
226 double[] xb, double[] yb) {
227 int n = xb.length;
228 int iwn = 0;
229 for (int i = 0; i < n; i++) {
230 int idir = 0;
231 int p = (i+1)%n;
232 if (yb[i] <= y && yb[p] > y) {
233 idir = 1;
234 }
235 if (yb[i] > y && yb[p] <= y) {
236 idir = -1;
237 }
238 if (idir != 0) {
239 double f = (y - yb[i]) / (yb[p] - yb[i]);
240 double xc = f * xb[p] + (1.-f) * xb[i];
241 int isid = (xc > x ? 1 : -1);
242 iwn += isid * idir;
243 }
244
245 }
246 return (iwn != 0);
247 }
248
249
250
251
252
253
254
255
256 private static double[][] readPerimiter(String s) {
257 StringTokenizer st = new StringTokenizer(s, "\n\t ,");
258
259 int n = st.countTokens() / 2;
260
261 double[][] sy = new double[2][n];
262
263 for (int i = 0; i < n; i++) {
264 sy[0][i] = (new Double(st.nextToken())).doubleValue();
265 sy[1][i] = (new Double(st.nextToken())).doubleValue();
266 }
267 return sy;
268 }
269
270
271
272
273
274
275 public static String readStringFromFile(File f) {
276 String sdat = "";
277 try {
278 InputStream ins = new FileInputStream(f);
279 InputStreamReader insr = new InputStreamReader(ins);
280 BufferedReader fr = new BufferedReader(insr);
281 StringBuffer sb = new StringBuffer();
282 while (fr.ready()) {
283 sb.append(fr.readLine());
284 sb.append("\n");
285 }
286 fr.close();
287 sdat = sb.toString();
288 } catch (IOException ex) {
289 E.error("file read error ");
290 ex.printStackTrace();
291 }
292 return sdat;
293 }
294
295
296 }
297