/Users/lyon/j4p/src/ip/gui/dialog/MorphLog.java
|
1 package ip.gui.dialog;
2
3 import ip.gui.frames.ColorFrame;
4 import ip.gui.frames.GridImageFrame;
5
6 import java.awt.*;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
10 public class MorphLog extends ColorFrame
11 implements ActionListener {
12
13 MenuBar mb = new MenuBar();
14 Menu SettingsMenu = getMenu("Settings");
15
16 MenuItem props_mi = addMenuItem(SettingsMenu, "[p]roperties");
17 MenuItem default_mi = addMenuItem(SettingsMenu, "[d]efault images");
18 MenuItem morph_mi = addMenuItem(SettingsMenu, "[m]orph");
19
20 short rm[][] = new short[0][0];
21 short gm[][] = new short[0][0];
22 short bm[][] = new short[0][0];
23
24 short rc[][] = new short[0][0];
25 short gc[][] = new short[0][0];
26 short bc[][] = new short[0][0];
27 Image img = null;
28 Polygon p = new Polygon();
29 boolean doMorph = false;
30 GridImageFrame SourceImage = null;
31 GridImageFrame StopImage = null;
32 int w = 128;
33 int h = 128;
34 int nPoints = 25;
35 int gridX = 5;
36 int gridY = 5;
37 int NumberOfFrames = 50;
38 boolean busy = false;
39 String prompts[] = {
40 "Grid X Elements=",
41 "Grid Y Elements=",
42 "Number Of Frames="};
43 String defaults[] = {
44 "" + gridX,
45 "" + gridY,
46 "" + NumberOfFrames};
47 ExpandoLog el = null;
48
49
50 public void actionPerformed(ActionEvent e) {
51
52 if (el != null) {
53 String s[] = el.getUserInput();
54 gridX = Integer.parseInt(s[0]);
55 gridY = Integer.parseInt(s[1]);
56 NumberOfFrames = Integer.parseInt(s[2]);
57 SourceImage.setGrid(gridX, gridY);
58 StopImage.setGrid(gridX, gridY);
59 el = null;
60 }
61
62 if (match(e, morph_mi)) {
63 Morph();
64 return;
65 }
66
67 if (match(e, props_mi)) {
68 Properties();
69 return;
70 }
71
72 if (match(e, default_mi)) {
73 default1();
74 return;
75 }
76
77 super.actionPerformed(e);
78 }
79
80 MorphLog(String title) {
81 super(title);
82 init();
83 mb.add(SettingsMenu);
84 setMenuBar(mb);
85 SourceImage = new GridImageFrame("Source Image", Color.red);
86 StopImage = new GridImageFrame("Stop Image", Color.green);
87 repaint();
88 }
89
90 private void init() {
91 }
92
93 public void Morph() {
94 int maxW = 0;
95 int maxH = 0;
96 int sourceX, sourceY;
97 int stopX, stopY;
98 // AnimateFrame an=new AnimateFrame();
99 double k = 0;
100 if (getImageWidth() > maxW) maxW = getImageWidth();
101 if (getImageHeight() > maxH) maxH = getImageHeight();
102 if (getImageWidth() > maxW) maxW = getImageWidth();
103 if (getImageHeight() > maxH) maxH = getImageHeight();
104 setSize(maxW, maxH);
105 repaint();
106 rm = new short[maxW][maxH];
107 gm = new short[maxW][maxH];
108 bm = new short[maxW][maxH];
109 rc = new short[maxW][maxH];
110 gc = new short[maxW][maxH];
111 bc = new short[maxW][maxH];
112 setImageHeight(maxH);
113 setImageWidth(maxW);
114 short[][] r = rm;
115 shortImageBean.setR(r);
116 setG(gm);
117 setB(bm);
118 short2Image();
119 img = getImage();
120
121 nPoints = (gridX + 1) * (gridY + 1);
122 p = new Polygon();
123 for (int j = 0; j < nPoints; j++) {
124 p.addPoint(SourceImage.p.xpoints[j],
125 SourceImage.p.ypoints[j]);
126 }
127
128 String files = "seq";//getFileNames();
129 busy = true;
130 for (int i = 0; i < NumberOfFrames; i++) {
131 k = (double) ((double) i / (double) (NumberOfFrames - 1));
132 for (int j = 0; j < nPoints; j++) {
133 sourceX = SourceImage.p.xpoints[j];
134 sourceY = SourceImage.p.ypoints[j];
135 stopX = StopImage.p.xpoints[j];
136 stopY = StopImage.p.ypoints[j];
137 p.xpoints[j] = (int) linearY(sourceX, stopX, k);
138 p.ypoints[j] = (int) linearY(sourceY, stopY, k);
139 }
140 applyBilinearMorph(k);
141 short[][] r1 = rm;
142 shortImageBean.setR(r1);
143 setG(gm);
144 setB(bm);
145 medianCut(256);
146 // img=getImage();
147 // an.addImage(img);
148 // System.out.println((NumberOfFrames-i-1) +" steps left...");
149
150 int outFileNumber = i;
151 String outFileName = "d:\\cadgfx\\pics\\morph\\" + outFileNumber + ".GIF";
152 System.out.println("writing:" + outFileName);
153 saveAsGif(outFileName);
154
155 }
156 // an.setSize(width,height);
157 // an.setVisible(true);
158 }
159
160
161 public void applyBilinearMorph(double k) {
162 Point s0,s1,s2,s3,m0,m1,m2,m3,d0,d1,d2,d3;
163 for (int i = 0; i < gridY; i++)
164 for (int j = 0; j < gridX; j++) {
165 s0 = new Point(SourceImage.p.xpoints[j + i * (gridX + 1)], SourceImage.p.ypoints[j + i * (gridX + 1)]);
166 s1 = new Point(SourceImage.p.xpoints[j + 1 + i * (gridX + 1)], SourceImage.p.ypoints[j + 1 + i * (gridX + 1)]);
167 s2 = new Point(SourceImage.p.xpoints[j + 1 + (i + 1) * (gridX + 1)], SourceImage.p.ypoints[j + 1 + (i + 1) * (gridX + 1)]);
168 s3 = new Point(SourceImage.p.xpoints[j + (i + 1) * (gridX + 1)], SourceImage.p.ypoints[j + (i + 1) * (gridX + 1)]);
169 m0 = new Point(p.xpoints[j + i * (gridX + 1)], p.ypoints[j + i * (gridX + 1)]);
170 m1 = new Point(p.xpoints[j + 1 + i * (gridX + 1)], p.ypoints[j + 1 + i * (gridX + 1)]);
171 m2 = new Point(p.xpoints[j + 1 + (i + 1) * (gridX + 1)], p.ypoints[j + 1 + (i + 1) * (gridX + 1)]);
172 m3 = new Point(p.xpoints[j + (i + 1) * (gridX + 1)], p.ypoints[j + (i + 1) * (gridX + 1)]);
173 d0 = new Point(StopImage.p.xpoints[j + i * (gridX + 1)], StopImage.p.ypoints[j + i * (gridX + 1)]);
174 d1 = new Point(StopImage.p.xpoints[j + 1 + i * (gridX + 1)], StopImage.p.ypoints[j + 1 + i * (gridX + 1)]);
175 d2 = new Point(StopImage.p.xpoints[j + 1 + (i + 1) * (gridX + 1)], StopImage.p.ypoints[j + 1 + (i + 1) * (gridX + 1)]);
176 d3 = new Point(StopImage.p.xpoints[j + (i + 1) * (gridX + 1)], StopImage.p.ypoints[j + (i + 1) * (gridX + 1)]);
177
178
179 solveMorph(s0, s1, s2, s3, m0, m1, m2, m3, d0, d1, d2, d3, k);
180 }
181 }
182
183 int Dist(Point a, Point b) {
184 return (int) (Math.sqrt(((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y))));
185 }
186
187 public void solveMorph(Point s0, Point s1, Point s2, Point s3, Point m0, Point m1, Point m2, Point m3, Point d0, Point d1, Point d2, Point d3, double k1) {
188 double xStart1, xEnd1,yStart1, yEnd1;
189 double xStart2, xEnd2,yStart2, yEnd2;
190 double xStart3, xEnd3,yStart3, yEnd3;
191 double k = 0;
192 short rS, gS, bS, rD, gD, bD;
193
194 int MAX = 0;
195 int MAX1 = 0;
196 int xM, yM, xS, yS, xD, yD;
197
198 MAX = Dist(d0, d1);
199 MAX1 = Dist(d1, d2);
200 if (MAX1 > MAX) MAX = MAX1;
201 MAX1 = Dist(d2, d3);
202 if (MAX1 > MAX) MAX = MAX1;
203 MAX1 = Dist(d3, d0);
204 if (MAX1 > MAX) MAX = MAX1;
205
206 MAX1 = Dist(s0, s1);
207 if (MAX1 > MAX) MAX = MAX1;
208 MAX1 = Dist(s1, s2);
209 if (MAX1 > MAX) MAX = MAX1;
210 MAX1 = Dist(s2, s3);
211 if (MAX1 > MAX) MAX = MAX1;
212 MAX1 = Dist(s3, s0);
213 if (MAX1 > MAX) MAX = MAX1;
214
215 MAX1 = Dist(m0, m1);
216 if (MAX1 > MAX) MAX = MAX1;
217 MAX1 = Dist(m1, m2);
218 if (MAX1 > MAX) MAX = MAX1;
219 MAX1 = Dist(m2, m3);
220 if (MAX1 > MAX) MAX = MAX1;
221 MAX1 = Dist(m3, m0);
222 if (MAX1 > MAX) MAX = MAX1;
223
224 MAX = MAX + 1;
225
226 for (int i = 0; i <= MAX; i++) {
227 k = (double) ((double) i / (double) MAX);
228 xStart1 = linearY(d0.x, d3.x, k);
229 yStart1 = linearY(d0.y, d3.y, k);
230 xEnd1 = linearY(d1.x, d2.x, k);
231 yEnd1 = linearY(d1.y, d2.y, k);
232 xStart2 = linearY(s0.x, s3.x, k);
233 yStart2 = linearY(s0.y, s3.y, k);
234 xEnd2 = linearY(s1.x, s2.x, k);
235 yEnd2 = linearY(s1.y, s2.y, k);
236
237 xStart3 = linearY(m0.x, m3.x, k);
238 yStart3 = linearY(m0.y, m3.y, k);
239 xEnd3 = linearY(m1.x, m2.x, k);
240 yEnd3 = linearY(m1.y, m2.y, k);
241 for (int j = 0; j <= MAX; j++) {
242 k = (double) ((double) j / (double) MAX);
243 xM = (int) (linearY(xStart3, xEnd3, k));
244 yM = (int) (linearY(yStart3, yEnd3, k));
245 xS = (int) (linearY(xStart2, xEnd2, k));
246 yS = (int) (linearY(yStart2, yEnd2, k));
247 xD = (int) (linearY(xStart1, xEnd1, k));
248 yD = (int) (linearY(yStart1, yEnd1, k));
249
250 if ((xS < getImageWidth()) && (yS < getImageHeight()) && (xS >= 0) && (yS >= 0) &&
251 (xD < getImageWidth()) && (yD < getImageHeight()) && (xD >= 0) && (yD >= 0) &&
252 (xM < getImageWidth()) && (yM < getImageHeight()) && (xM >= 0) && (yM >= 0)) {
253
254 rS = (short) shortImageBean.getR()[xS][yS];
255 gS = (short) shortImageBean.getG()[xS][yS];
256 bS = (short) shortImageBean.getB()[xS][yS];
257 rD = (short) shortImageBean.getR()[xD][yD];
258 gD = (short) shortImageBean.getG()[xD][yD];
259 bD = (short) shortImageBean.getB()[xD][yD];
260
261 rm[xM][yM] = (short) linearY(rS, rD, k1);
262 gm[xM][yM] = (short) linearY(gS, gD, k1);
263 bm[xM][yM] = (short) linearY(bS, bD, k1);
264 }
265 }
266 }
267 }
268
269
270 double linearY(double x1, double x2, double t) {
271 double dx = 0;
272 dx = (double) (x2 - x1);
273 return (double) (x1 + (double) (dx * t));
274 }
275
276 public void Properties() {
277 el = new ExpandoLog(new Frame(),
278 "Properties", prompts, defaults, 9);
279 el.setVisible(true);
280 el.setButton.addActionListener(this);
281 }
282
283
284 private void default1() {
285 gridX = 5;
286 gridY = 5;
287 setSize(getImageWidth(), getImageHeight());
288 SourceImage.default1();
289 StopImage.default1();
290 repaint();
291 }
292
293 public static void main(String args[]) {
294 MorphLog mL = new MorphLog(
295 "MorphLog");
296 mL.setSize(150, 150);
297 mL.setVisible(true);
298 }
299
300 }