/Users/lyon/j4p/src/ip/raul/MyOpenFrame.java
|
1 package ip.raul;
2
3 import futils.Futil;
4 import futils.WriterUtil;
5 import ip.gui.frames.WaveletFrame;
6
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.MouseListener;
11 import java.io.*;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
14 import java.util.zip.ZipOutputStream;
15
16 public class MyOpenFrame extends WaveletFrame
17 implements MouseListener {
18
19 private MenuBar mb = new MenuBar();
20 private Menu openLifting = new Menu("Lifting Open");
21 private Menu waveletMenu = new Menu("Hartley");
22 private MenuItem openLifts2_mi =
23 addMenuItem(openLifting, "openLifting(2)...");
24 private MenuItem openLifts4_mi =
25 addMenuItem(openLifting, "openLifting(4)...");
26 private MenuItem openLifts8_mi =
27 addMenuItem(openLifting, "openLifting(8)...");
28 private MenuItem openLifts16_mi =
29 addMenuItem(openLifting, "openLifting(16)...");
30 private MenuItem openLifts32_mi =
31 addMenuItem(openLifting, "openLifting(32)...");
32 private MenuItem openLifts64_mi =
33 addMenuItem(openLifting, "openLifting(64)...");
34 private MenuItem openLifts128_mi =
35 addMenuItem(openLifting, "openLifting(128)...");
36 private MenuItem openLifts256_mi =
37 addMenuItem(openLifting, "openLifting(256)...");
38
39 private MenuItem ThreeDImage_mi =
40 addMenuItem(openLifting, "View as 3D");
41 private MenuItem mandelbrot_mi = addMenuItem(openLifting,
42 "Mandelbrot");
43
44 private MenuItem saveLifts_mi =
45 addMenuItem(openLifting, "saveLifting...");
46 private MenuItem openImage_mi =
47 addMenuItem(openLifting, "open Image...");
48 private MenuItem forwardHartley_mi = addMenuItem(waveletMenu,
49 "Forward Hartley");
50 private MenuItem BackwardHartley_mi = addMenuItem(waveletMenu,
51 "Backward Hartley");
52
53 private double r1[][] = null;
54 private double g1[][] = null;
55 private double b1[][] = null;
56
57 private short Status = 1;//fractal, 2 Hartley
58
59 //fractal related
60 private boolean busy = false;
61 private Fractals fr = new Fractals();
62
63 public MyOpenFrame(String title) {
64 super(title);
65 mb.add(openLifting);
66 mb.add(waveletMenu);
67 setMenuBar(mb);
68 }
69
70 public static void main(String args[]) {
71 String title = "OpenLifting";
72 MyOpenFrame of =
73 new MyOpenFrame(title);
74 of.setVisible(true);
75 of.setSize(64, 64);
76
77 }
78
79 public void actionPerformed(ActionEvent e) {
80
81 if (match(e, openImage_mi)) {
82 openImage();
83 getDoubles();
84 return;
85 }
86 if (match(e, saveLifts_mi)) {
87 saveLifting();
88 return;
89 }
90 if (match(e, openLifts2_mi)) {
91 openLifting(0);
92 return;
93 }
94 if (match(e, openLifts4_mi)) {
95 openLifting(1);
96 return;
97 }
98 if (match(e, openLifts8_mi)) {
99 openLifting(2);
100 return;
101 }
102 if (match(e, openLifts16_mi)) {
103 openLifting(3);
104 return;
105 }
106 if (match(e, openLifts32_mi)) {
107 openLifting(4);
108 return;
109 }
110 if (match(e, openLifts64_mi)) {
111 openLifting(5);
112 return;
113 }
114 if (match(e, openLifts128_mi)) {
115 openLifting(6);
116 return;
117 }
118 if (match(e, openLifts256_mi)) {
119 openLifting(7);
120 return;
121 }
122 if (match(e, forwardHartley_mi)) {
123 Status = 2;
124 forwardHartley();
125 return;
126 }
127 if (match(e, BackwardHartley_mi)) {
128 Status = 2;
129 BackwardHartley();
130 return;
131 }
132 if (match(e, mandelbrot_mi)) {
133 mandelbrot();
134 return;
135 }
136 if (match(e, ThreeDImage_mi)) {
137 SnowManFrame.image3D(getImage(), shortImageBean.getR());
138 return;
139 }
140 super.actionPerformed(e);
141 }
142
143
144 public void mandelbrot() {
145 setImageWidth(256);
146 setImageHeight(256);
147 Status = 1;
148 short[][] r = new short[getImageWidth()][getImageHeight()];
149 shortImageBean.setR(r);
150 setG(new short[getImageWidth()][getImageHeight()]);
151 setB(new short[getImageWidth()][getImageHeight()]);
152 fr.mandelbrot(shortImageBean.getR(),
153 shortImageBean.getG(),
154 shortImageBean.getB());
155 setSize(getImageWidth(), getImageHeight());
156 short2Image();
157 getDoubles();
158 }
159
160
161 public void openLifting(int depth) {
162 openShortZIP(depth);
163 }
164
165 public void openShortZIP(int depth) {
166 String fn = Futil.getReadFileName();
167 if (fn == null) return;
168 File f = new File(fn);
169 if (!f.exists()) return;
170 setFileName(fn);
171 try {
172 readShortsZIP(fn, depth);
173 setSize(getImageWidth(), getImageHeight());
174 short2Image();
175 liftingBackwardHaar();
176 } catch (Exception e) {
177 System.out.println("Read PPM Exception:" + e);
178 }
179 repaint();
180 }
181
182
183 public void saveLifting() {
184 System.out.println("Saving as ForwardLiftingPPM...");
185 String fn = WriterUtil.getSaveFileName("Save as ForwardLifting");
186 if (fn == null) return;
187 liftingForwardHaar();
188 saveShortZip(fn);
189 liftingBackwardHaar();
190 }
191
192 private short[][] readZipEntry(ZipInputStream zis) {
193 short band[][] = null;
194 ObjectInputStream ois = null;
195 try {
196 //ZipEntry ze;
197 zis.getNextEntry();
198 ois = new ObjectInputStream(zis);
199 band = (short[][]) ois.readObject();
200 } catch (Exception e) {
201 System.out.println("Open getShortImageZip:" + e);
202 }
203 return band;
204 }
205
206 public void readShortsZIP(String fn, int depth) {
207 short bandR[][] = null;
208 short bandG[][] = null;
209 short bandB[][] = null;
210 int i = 0;
211 int j = 2;
212 int k = 0;
213 int sw = 1;
214 try {
215 FileInputStream fis = new FileInputStream(fn);
216 ZipInputStream zis = new ZipInputStream(fis);
217 while (i <= depth) {
218 //ZipEntry ze;
219 bandR = readZipEntry(zis);
220 bandG = readZipEntry(zis);
221 bandB = readZipEntry(zis);
222 if (sw == 1) {
223 setImageWidth(bandR.length);
224 int height1 = getImageWidth();
225 setImageHeight(height1);
226 short[][] r = new short[getImageWidth()][getImageHeight()];
227 shortImageBean.setR(r);
228 setG(new short[getImageWidth()][getImageHeight()]);
229 setB(new short[getImageWidth()][getImageHeight()]);
230 sw = 0;
231 }
232 for (int x = 0; x < getImageWidth(); x++)
233 for (int y = k; y < j; y++) {
234 shortImageBean.getR()[x][y] = bandR[x][y - k];
235 shortImageBean.getG()[x][y] = bandG[x][y - k];
236 shortImageBean.getB()[x][y] = bandB[x][y - k];
237 }
238 System.out.println("band " + i + " read.");
239 i++;
240 k = j;
241 j = j * 2;
242 }
243 zis.close();
244 fis.close();
245 System.out.println("done!");
246 } catch (Exception e) {
247 System.out.println("Open getShortImageZip:" + e);
248 }
249 }
250
251 private void writeZipEntry(ZipOutputStream zos,
252 String name,
253 short _band[][],
254 int rowStart,
255 int rowStop) {
256 short band[][] = new short[getImageWidth()][rowStop - rowStart];
257 try {
258 for (int x = 0; x < getImageWidth(); x++)
259 for (int y = rowStart; y < rowStop; y++)
260 band[x][y - rowStart] = _band[x][y];
261 ObjectOutputStream oos = null;
262 ZipEntry ze = new ZipEntry(name);
263 ze.setMethod(ZipEntry.DEFLATED);
264 zos.putNextEntry(ze);
265 oos = new ObjectOutputStream(zos);
266 oos.writeObject(band);
267 } catch (Exception e) {
268 }
269 }
270
271 public void saveShortZip(String fn) {
272 int i = 0;
273 int j = 2;
274 int k = 0;
275 try {
276 FileOutputStream fos = new FileOutputStream(fn);
277 ZipOutputStream zos = new ZipOutputStream(fos);
278 while (j <= getImageHeight()) {
279 writeZipEntry(zos, "R" + i, shortImageBean.getR(), k, j);
280 writeZipEntry(zos, "G" + i, shortImageBean.getG(), k, j);
281 writeZipEntry(zos, "B" + i, shortImageBean.getB(), k, j);
282 System.out.println("band " + i + " written.");
283 i++;
284 k = j;
285 j = j * 2;
286 }
287 zos.finish();
288 zos.close();
289 fos.close();
290 System.out.println("done!.");
291 } catch (Exception e) {
292 System.out.println("Save As9bitZip error:" + e);
293 }
294 }
295
296 public void getDoubles() {
297 r1 = new double[getImageWidth()][getImageHeight()];
298 g1 = new double[getImageWidth()][getImageHeight()];
299 b1 = new double[getImageWidth()][getImageHeight()];
300 for (int x = 0; x < getImageWidth(); x++)
301 for (int y = 0; y < getImageHeight(); y++) {
302 r1[x][y] = (double) (shortImageBean.getR()[x][y]);
303 g1[x][y] = (double) (shortImageBean.getG()[x][y]);
304 b1[x][y] = (double) (shortImageBean.getB()[x][y]);
305 }
306 }
307
308 public void fht2d(double f[][]) {
309 int k, l;
310 for (int i = 1; i < (getImageWidth() / 2); i++) {
311 k = getImageWidth() - i;
312 for (int j = 1; j < (getImageHeight() / 2); j++) {
313 l = getImageHeight() - j;
314 double e = ((f[i][j] + f[k][l]) - (f[i][l] + f[k][j])) /
315 2d;
316 f[i][j] -= e;
317 f[i][l] += e;
318 f[k][j] += e;
319 f[k][l] -= e;
320 }
321 }
322 }
323
324
325 public void swapQuads() {
326 double tmp = 0;
327 for (int x = 0; x < getImageWidth() / 2; x++)
328 for (int y = 0; y < getImageHeight() / 2; y++) {
329 tmp = r1[x][y];
330 r1[x][y] =
331 r1[getImageWidth() / 2 + x][getImageHeight() / 2 +
332 y];
333 r1[getImageWidth() / 2 + x][getImageHeight() / 2 + y] =
334 tmp;
335 tmp = g1[x][y];
336 g1[x][y] =
337 g1[getImageWidth() / 2 + x][getImageHeight() / 2 +
338 y];
339 g1[getImageWidth() / 2 + x][getImageHeight() / 2 + y] =
340 tmp;
341 tmp = b1[x][y];
342 b1[x][y] =
343 b1[getImageWidth() / 2 + x][getImageHeight() / 2 +
344 y];
345 b1[getImageWidth() / 2 + x][getImageHeight() / 2 + y] =
346 tmp;
347 tmp = r1[x + getImageWidth() / 2][y];
348 r1[x + getImageWidth() / 2][y] =
349 r1[x][getImageHeight() / 2 + y];
350 r1[x][getImageHeight() / 2 + y] = tmp;
351 tmp = g1[x + getImageWidth() / 2][y];
352 g1[x + getImageWidth() / 2][y] =
353 g1[x][getImageHeight() / 2 + y];
354 g1[x][getImageHeight() / 2 + y] = tmp;
355 tmp = b1[x + getImageWidth() / 2][y];
356 b1[x + getImageWidth() / 2][y] =
357 b1[x][getImageHeight() / 2 + y];
358 b1[x][getImageHeight() / 2 + y] = tmp;
359 }
360 }
361
362 public void getShorts() {
363 short[][] r = new short[getImageWidth()][getImageHeight()];
364 shortImageBean.setR(r);
365 setG(new short[getImageWidth()][getImageHeight()]);
366 setB(new short[getImageWidth()][getImageHeight()]);
367 double cMax = 0d;
368 double cMin = 0d;
369 double delta = 1d;
370 for (int x = 0; x < getImageWidth(); x++)
371 for (int y = 0; y < getImageHeight(); y++) {
372 if (r1[x][y] > cMax) cMax = r1[x][y];
373 if (g1[x][y] > cMax) cMax = g1[x][y];
374 if (b1[x][y] > cMax) cMax = b1[x][y];
375 if (r1[x][y] < cMin) cMin = r1[x][y];
376 if (g1[x][y] < cMin) cMin = g1[x][y];
377 if (b1[x][y] < cMin) cMin = b1[x][y];
378 }
379 delta = cMax + cMin;
380 delta = (255d / delta);
381 if (cMin < 0) cMin = -cMin;
382 for (int x = 0; x < getImageWidth(); x++)
383 for (int y = 0; y < getImageHeight(); y++) {
384 shortImageBean.getR()[x][y] = (short)
385 ((r1[x][y] + cMin) * delta);
386 shortImageBean.getG()[x][y] = (short)
387 ((g1[x][y] + cMin) * delta);
388 shortImageBean.getB()[x][y] = (short)
389 ((b1[x][y] + cMin) * delta);
390 }
391 }
392
393 public void forwardHartley() {
394 swapQuads();
395 forwardHartley(r1);
396 forwardHartley(g1);
397 forwardHartley(b1);
398 fht2d(r1);
399 fht2d(g1);
400 fht2d(b1);
401 getShorts();
402 short2Image();
403 }
404
405
406 public void BackwardHartley() {
407 for (int x = 0; x < getImageWidth(); x++)
408 for (int y = 0; y < getImageHeight(); y++) {
409 r1[x][y] = (r1[x][y] / (double) (getImageHeight()));
410 g1[x][y] = (g1[x][y] / (double) (getImageHeight()));
411 b1[x][y] = (b1[x][y] / (double) (getImageHeight()));
412 }
413 forwardHartley(r1);
414 forwardHartley(g1);
415 forwardHartley(b1);
416 fht2d(r1);
417 fht2d(g1);
418 fht2d(b1);
419 swapQuads();
420 getShorts();
421 short2Image();
422 }
423
424 public void forwardHartley(double in[][]) {
425 for (int x = 0; x < in.length; x++)
426 forwardHartley(in[x]);
427 in = transpose(in);
428 for (int x = 0; x < in.length; x++)
429 forwardHartley(in[x]);
430 in = transpose(in);
431 }
432
433 public void forwardHartley(double f[]) {
434 int n = f.length;
435 double fz[] = new double[n];
436 ;
437 int i, j, k, k1, k4, kx, l;
438 double x0, x1, x2, x3, x4, x5, x6;
439 double c1, s1, s2, c2, s3, c3;
440 int f1, f2, f3;
441 double sqrt2 = 1.414213562373095048801688724209698f;
442 fz = f;
443 for (k = 0; (1 << k) < n; k++) ;
444 k &= 1;
445 for (i = 1, j = 0; i < n; i++) {
446 for (l = n >> 1; (((j ^= l) & l) == 0); l >>= 1) ;
447 if (i > j) {
448 x0 = fz[i];
449 fz[i] = fz[j];
450 fz[j] = x0;
451 }
452 }
453 k1 = 1 << k;
454 k4 = k1 << 2;
455 kx = k1 >> 1;
456 f1 = k1;
457 f2 = f1 + k1;
458 f3 = f2 + k1;
459 if (k == 0)
460 for (i = 0; i < n; i += k4) {
461 x1 = fz[i] - fz[f1 + i];
462 x0 = fz[i] + fz[f1 + i];
463 x3 = fz[f2 + i] - fz[f3 + i];
464 x2 = fz[f2 + i] + fz[f3 + i];
465 fz[f2 + i] = x0 - x2;
466 fz[i] = x0 + x2;
467 fz[f3 + i] = x1 - x3;
468 fz[f1 + i] = x1 + x3;
469 }
470 else
471 for (i = 0, j = kx; i < n; i += k4, j += k4) {
472 x0 = fz[i] - fz[j];
473 x1 = fz[i] + fz[j];
474 x2 = fz[f1 + i] - fz[f1 + j];
475 x3 = fz[f1 + i] + fz[f1 + j];
476 x4 = x1 - x3;
477 x1 += x3;
478 x3 = x0 - x2;
479 x0 += x2;
480 x5 = fz[f2 + i] + fz[f2 + j];
481 x2 = fz[f2 + i] - fz[f2 + j];
482 x2 *= sqrt2;
483 fz[f2 + j] = x0 - x2;
484 fz[j] = x0 + x2;
485 x2 = fz[f3 + i] + fz[f3 + j];
486 x0 = fz[f3 + i] - fz[f3 + j];
487 x0 *= sqrt2;
488 fz[f3 + j] = x3 - x0;
489 fz[f1 + j] = x3 + x0;
490 x0 = x5 - x2;
491 x5 += x2;
492 fz[f2 + i] = x1 - x5;
493 fz[i] = x1 + x5;
494 fz[f3 + i] = x4 - x0;
495 fz[f1 + i] = x4 + x0;
496 }
497 while (k4 < n) {
498 k += 2;
499 k1 = 1 << k;
500 k4 = k1 << 2;
501 kx = k1 >> 1;
502 f1 = k1;
503 f2 = f1 + k1;
504 f3 = f2 + k1;
505 for (i = 0, j = kx; i < n; i += k4, j += k4) {
506 x1 = fz[i] - fz[f1 + i];
507 x0 = fz[i] + fz[f1 + i];
508 x3 = fz[f2 + i] - fz[f3 + i];
509 x2 = fz[f2 + i] + fz[f3 + i];
510 fz[f2 + i] = x0 - x2;
511 fz[i] = x0 + x2;
512 fz[f3 + i] = x1 - x3;
513 fz[f1 + i] = x1 + x3;
514 x1 = fz[j] - fz[f1 + j];
515 x0 = fz[j] + fz[f1 + j];
516 x3 = sqrt2 * fz[f3 + j];
517 x2 = sqrt2 * fz[f2 + j];
518 fz[f2 + j] = x0 - x2;
519 fz[j] = x0 + x2;
520 fz[f3 + j] = x1 - x3;
521 fz[f1 + j] = x1 + x3;
522 }
523
524 double angle = Math.PI / 2d / (double) (1 << k);
525 double angle_index = 0;
526 for (l = 1; l < kx; l++) {
527 angle_index++;
528 c1 = Math.cos(angle * angle_index);
529 s1 = Math.sin(angle * angle_index);
530 c2 = Math.cos(angle * (angle_index * 2d));
531 s2 = Math.sin(angle * (angle_index * 2d));
532 c3 = Math.cos(angle * (angle_index * 3d));
533 s3 = Math.sin(angle * (angle_index * 3d));
534 for (i = l, j = k1 - l; i < n; i += k4, j += k4) {
535 x0 = fz[f1 + i] * c2 + fz[f1 + j] * s2;
536 x1 = fz[f1 + i] * s2 - fz[f1 + j] * c2;
537 x2 = fz[f2 + i] * c1 + fz[f2 + j] * s1;
538 x3 = fz[f2 + i] * s1 - fz[f2 + j] * c1;
539 x4 = fz[f3 + i] * c3 + fz[f3 + j] * s3;
540 x5 = fz[f3 + i] * s3 - fz[f3 + j] * c3;
541 x6 = x2 - x4;
542 x4 += x2;
543 x2 = x3 - x5;
544 x5 += x3;
545 x3 = fz[i] - x0;
546 fz[f3 + i] = x3 + x2;
547 fz[f1 + i] = x3 - x2;
548 x3 = fz[i] + x0;
549 fz[f2 + i] = x3 - x4;
550 fz[i] = x3 + x4;
551 x3 = fz[j] - x1;
552 fz[f3 + j] = x3 - x5;
553 fz[f1 + j] = x3 + x5;
554 x3 = fz[j] + x1;
555 fz[f2 + j] = x3 - x6;
556 fz[j] = x3 + x6;
557 }
558 }
559 }
560 f = fz;
561 }
562
563 private static double[][] transpose(double in[][]) {
564 int width = in.length;
565 int height = in[0].length;
566 double[][] output = new double[height][width];
567 for (int j = 0; j < width; j++)
568 for (int i = 0; i < height; i++)
569 output[j][i] = in[i][j];
570 return output;
571 }
572
573 private int getX(MouseEvent e) {
574 return (e.getX());
575 }
576
577 private int getY(MouseEvent e) {
578 return (e.getY());
579 }
580
581 public void mouseReleased(MouseEvent e) {
582 }
583
584 public void mousePressed(MouseEvent e) {
585 }
586
587 public void mouseClicked(MouseEvent e) {
588 if (!busy) {
589 busy = true;
590 if (Status == 1) {
591 float x = (float) getX(e);
592 float y = (float) getY(e);
593 float dx = ((x - getImageWidth() / 2) / getImageWidth());
594 dx = dx * (Fractals.getxMax() - Fractals.getxMin());
595 float dy = ((y - getImageHeight() / 2) / getImageHeight());
596 dy = dy * (Fractals.getyMax() - Fractals.getyMin());
597 Fractals.setxMin(Fractals.getxMin() + dx);
598 Fractals.setyMin(Fractals.getyMin() + dy);
599 Fractals.setxMax(Fractals.getxMax() + dx);
600 Fractals.setyMax(Fractals.getyMax() + dy);
601
602 float dx1 = Fractals.getxMax() - Fractals.getxMin();
603 dx1 = dx1 / 5f;
604 float dy1 = Fractals.getyMax() - Fractals.getyMin();
605 dy1 = dy1 / 5f;
606 Fractals.setxMin(Fractals.getxMin() + dx1);
607 Fractals.setxMax(Fractals.getxMax() - dx1);
608 Fractals.setyMin(Fractals.getyMin() + dy1);
609 Fractals.setyMax(Fractals.getyMax() - dy1);
610
611 fr.mandelbrot(shortImageBean.getR(),
612 shortImageBean.getG(),
613 shortImageBean.getB());
614 short2Image();
615 System.out.println("ready for new click!");
616 }
617 if (Status == 2) {
618 int x = getX(e);
619 int y = getY(e);
620 for (int i = 0; i < 10; i++)
621 for (int j = 0; j < 10; j++) {
622 int x1 = x - 5 + i;
623 int y1 = y - 5 + j;
624 if ((x1 >= 0) &&
625 (y1 >= 0) &&
626 (x1 < getImageWidth()) &&
627 (y1 < getImageHeight())) {
628 shortImageBean.getR()[x1][y1] = shortImageBean.getG()[x1][y1] = shortImageBean.getB()[x1][y1] =
629 255;
630 r1[x1][y1] = g1[x1][y1] = b1[x1][y1] = 255d;
631 }
632 }
633 short2Image();
634 }
635
636 busy = false;
637 }
638 }
639
640 }
641