/Users/lyon/j4p/src/ip/hak/Mountain.java
|
1 package ip.hak;
2
3 import java.awt.*;
4 import java.awt.image.ColorModel;
5 import java.awt.image.MemoryImageSource;
6
7 public class Mountain
8 extends ip.gui.frames.ClosableFrame {
9 Image img = null;
10 boolean paintOn = false;
11
12 public Mountain(String title) {
13 super(title);
14 }
15
16 public static void main(String args[]) {
17 Mountain m = new Mountain("Mountain Generator");
18 m.start();
19 }
20
21 public void paint(Graphics g) {
22 if (!paintOn)
23 return;
24
25 int w = img.getWidth(this);
26 int h = img.getHeight(this);
27
28 setSize(20 + w, 30 + h);
29 setVisible(true);
30 g.drawImage(img, 10, 20, w, h, this);
31 }
32
33 public void start() {
34 makeRandomImage();
35 takeDHT();
36 multOneOnF();
37 takeIDHT();
38 threeDImage();
39 }
40
41 int n = 256;
42 short r[][] = new short[n][n];
43 short g[][] = new short[n][n];
44 short b[][] = new short[n][n];
45
46 float rf[][] = new float[n][n];
47 float gf[][] = new float[n][n];
48 float bf[][] = new float[n][n];
49
50 public void makeRandomImage() {
51 for (int x = 0; x < n; x++)
52 for (int y = 0; y < n; y++) {
53 r[x][y] = (short) (Math.random() * 256);
54 g[x][y] = (short) (Math.random() * 256);
55 b[x][y] = (short) (Math.random() * 256);
56 }
57 }
58
59 public void takeDHT() {
60 for (int x = 0; x < n; x++)
61 for (int y = 0; y < n; y++) {
62 rf[x][y] = (float) r[y][x];
63 gf[x][y] = (float) g[y][x];
64 bf[x][y] = (float) b[y][x];
65 }
66 rf = DHT2D.forwardDHT2D(rf);
67 gf = DHT2D.forwardDHT2D(gf);
68 bf = DHT2D.forwardDHT2D(bf);
69 }
70
71 public void multOneOnF() {
72 int hn = n / 2;
73
74 for (int x = 0; x < n; x++)
75 for (int y = 0; y < n; y++) {
76 double f = oneOnF(x, y, hn, hn);
77 rf[x][y] = (float) (rf[x][y] * f);
78 gf[x][y] = (float) (gf[x][y] * f);
79 bf[x][y] = (float) (bf[x][y] * f);
80 }
81 }
82
83 public double oneOnF(int x, int y, int xc, int yc) {
84 double dx = x - xc;
85 double dy = y - yc;
86 double dx2 = dx * dx;
87 double dy2 = dy * dy;
88
89 return 1 / Math.sqrt(dx2 + dy2 + 1);
90 }
91
92 public void takeIDHT() {
93 rf = DHT2D.inverseDHT2D(rf);
94 gf = DHT2D.inverseDHT2D(gf);
95 bf = DHT2D.inverseDHT2D(bf);
96 }
97
98 public Image short2Image(int wi, int he) {
99 Toolkit tk = Toolkit.getDefaultToolkit();
100 ColorModel cm = ColorModel.getRGBdefault();
101
102 int pels[] = new int[wi * he];
103 for (int x = 0; x < wi; x++)
104 for (int y = 0; y < he; y++) {
105 pels[x + y * wi] = 0xff000000 | (r[x][y] << 16)
106 | (g[x][y] << 8)
107 | b[x][y];
108 }
109 Image i = tk.createImage(new MemoryImageSource(wi, he, cm, pels, 0, wi));
110 return i;
111 }
112
113 public void waitForImage(Component component, Image image) {
114 MediaTracker tracker = new MediaTracker(component);
115 try {
116 tracker.addImage(image, 0);
117 tracker.waitForID(0);
118 if (!tracker.checkID(0))
119 System.out.println("Load failure!");
120 } catch (InterruptedException e) {
121 }
122 }
123
124 public void threeDImage() {
125 for (int x = 0; x < n; x++)
126 for (int y = 0; y < n; y++) {
127 r[x][y] = (short) (rf[y][x]);
128 g[x][y] = (short) (gf[y][x]);
129 b[x][y] = (short) (bf[y][x]);
130 }
131 img = short2Image(n, n);
132 waitForImage(this, img);
133 paintOn = true;
134 graphics.ddd.MainFrame.image3D(img, r);
135 }
136 }
137