1 package org.catacomb.dataview.gui;
2
3
4
5 import java.awt.image.BufferedImage;
6 import java.io.File;
7
8 import org.catacomb.druid.gui.edit.DruButton;
9 import org.catacomb.druid.gui.edit.DruSlider;
10 import org.catacomb.interlish.annotation.IOPoint;
11 import org.catacomb.interlish.structure.Controller;
12 import org.catacomb.interlish.structure.FrameDisplay;
13 import org.catacomb.movie.gif.AnimatedGifEncoder;
14 import org.catacomb.report.E;
15 import org.catacomb.util.AWTUtil;
16
17
18 public class FramePlayerController implements Controller {
19
20
21 @IOPoint(xid = "slider")
22 public DruSlider frameSlider;
23
24 @IOPoint(xid = "PauseButton")
25 public DruButton pauseButton;
26
27 String sourcePath;
28 String displayPath;
29
30 int[] indexes;
31 String[] displayValues;
32
33
34
35
36
37
38
39 double speed = 1.;
40
41 int shownFrame;
42
43 FramePlayer framePlayer;
44
45 boolean isPaused;
46
47 FrameDisplay frameDisplay;
48
49
50 public FramePlayerController() {
51
52 }
53
54
55
56 public void attached() {
57
58 }
59
60
61 public void applyData(double[] dat) {
62 displayValues = new String[dat.length];
63 indexes = new int[dat.length];
64 for (int i = 0; i < dat.length; i++) {
65 displayValues[i] = String.format("%.4g", new Double(dat[i]));
66 indexes[i] = i;
67 }
68 frameSlider.setValues(displayValues);
69 showFrame(0);
70 }
71
72
73 public void showFrame(int iframein) {
74 int iframe = iframein;
75 if (iframe < 0) {
76 iframe = 0;
77 }
78
79 if (iframe >= indexes.length) {
80 iframe = indexes.length - 1;
81 }
82
83
84 if (frameSlider != null) {
85 frameSlider.showValue(iframe);
86 }
87 shownFrame = iframe;
88 if (frameDisplay != null) {
89 frameDisplay.showFrame(iframe);
90 }
91 }
92
93
94 public void show(Object obj) {
95 }
96
97
98
99 public void sliderMoved() {
100 int ival = frameSlider.getValue();
101 showFrame(ival);
102 }
103
104
105 public void rewind() {
106 stop();
107 showFrame(0);
108 }
109
110
111
112 public void pause() {
113 if (isPaused) {
114 dePause();
115 start();
116
117 } else {
118 rePause();
119 stop();
120 }
121 }
122
123
124 public void dePause() {
125 isPaused = false;
126 pauseButton.setLabelText(" pause ");
127 }
128
129
130 private void rePause() {
131 isPaused = true;
132 pauseButton.setLabelText("resume");
133 }
134
135
136 public void play() {
137 stop();
138 rewind();
139 start();
140 dePause();
141 }
142
143
144 private void start() {
145 if (indexes != null) {
146 framePlayer = new FramePlayer(this);
147 framePlayer.start();
148 }
149 }
150
151
152 public void stop() {
153 if (framePlayer != null) {
154 framePlayer.stop();
155 }
156 }
157
158
159
160 public void faster() {
161 speed *= 1.3;
162 }
163
164
165 public void slower() {
166 speed /= 1.3;
167 }
168
169
170 public boolean canAdvance() {
171 return (indexes != null && shownFrame < indexes.length - 1);
172 }
173
174
175 public void advance() {
176 showFrame(shownFrame + 1);
177 }
178
179
180 public double getSpeed() {
181 return speed;
182 }
183
184
185
186 public void record() {
187 E.missing();
188
189
190
191
192
193 }
194
195
196 public void miniRecord() {
197 E.missing();
198
199
200
201
202 }
203
204
205
206 public void makeMovie(File f) {
207 makeAnimatedGif(f);
208 }
209
210
211 public void makeThumbnailMovie(File f) {
212 makeMiniAnimatedGif(f, 160, 160);
213 }
214
215
216
217 public void makeAnimatedGif(File f) {
218 stop();
219 rewind();
220
221
222 AnimatedGifEncoder enc = new AnimatedGifEncoder();
223
224 enc.start(f);
225 enc.setDelay(160);
226
227 int ifr = 0;
228 E.info("animated gif - frame " + ifr);
229
230
231 while (canAdvance()) {
232 advance();
233 ifr += 1;
234 E.info("animated gif - frame " + ifr);
235
236 }
237
238 enc.finish();
239
240 }
241
242
243
244 public void makeMiniAnimatedGif(File f, int wsclin, int hsclin) {
245 int wscl = wsclin;
246 int hscl = hsclin;
247 stop();
248 rewind();
249
250
251 AnimatedGifEncoder enc = new AnimatedGifEncoder();
252
253 enc.start(f);
254 enc.setDelay(200);
255
256 int nfr = indexes.length;
257
258 int stepsize = nfr / 10;
259 if (stepsize < 1) {
260 stepsize = 1;
261 }
262
263 int ifr = 0;
264
265
266
267
268 while (canAdvance()) {
269 E.info("animated gif - frame " + ifr);
270 BufferedImage bim = null;
271
272 if (ifr == 0) {
273 int wf = bim.getWidth();
274 int hf = bim.getHeight();
275 double fo = ((float)wscl) / hscl;
276 double ff = ((float)wf) / hf;
277 if (ff > fo) {
278 hscl = (int)(wscl / ff);
279 } else {
280 wscl = (int)(hscl * ff);
281 }
282
283
284
285 }
286
287 BufferedImage bufim = AWTUtil.getScaledBufferedImage(bim, wscl, hscl);
288
289 enc.addFrame(bufim);
290 ifr += 1;
291
292 for (int i = 0; i < stepsize; i++) {
293 if (canAdvance()) {
294 advance();
295 }
296 }
297 }
298
299 enc.finish();
300
301 }
302
303
304 public void setFrameDisplay(FrameDisplay fd) {
305 frameDisplay = fd;
306
307 }
308
309
310 }