/Users/lyon/j4p/src/ip/transforms/Lifting.java
|
1 package ip.transforms;
2
3 public class Lifting {
4
5 //standard decomposition
6 public static void forwardHaar(short in[][]) {
7 for (int x = 0; x < in.length; x++)
8 forwardHaar(in[x], in.length);
9 in = transpose(in);
10 for (int x = 0; x < in.length; x++)
11 forwardHaar(in[x], in.length);
12 in = transpose(in);
13 }
14
15 public static void forwardHaar(int in[][]) {
16 for (int x = 0; x < in.length; x++)
17 forwardHaar(in[x], in.length);
18 in = transpose(in);
19 for (int x = 0; x < in.length; x++)
20 forwardHaar(in[x], in.length);
21 in = transpose(in);
22 }
23
24 //standard reconstruction
25 public static void backwardHaar(short in[][]) {
26 for (int x = 0; x < in.length; x++)
27 backwardHaar(in[x]);
28 in = transpose(in);
29 for (int x = 0; x < in.length; x++)
30 backwardHaar(in[x]);
31 in = transpose(in);
32 }
33
34 public static void backwardHaar(int in[][]) {
35 for (int x = 0; x < in.length; x++)
36 backwardHaar(in[x]);
37 in = transpose(in);
38 for (int x = 0; x < in.length; x++)
39 backwardHaar(in[x]);
40 in = transpose(in);
41 }
42
43 //nonstandard decomposition is lossy
44 //Find out why!
45 public static void forwardHaarNSD(short in[][]) {
46 int width = in.length;
47 int height = in[0].length;
48 short temp[] = new short[width];
49 for (int i = 0; i < width; i++)
50 forwardHaar(in[i], in.length);
51 for (int j = 0; j < height; j++) {
52 for (int i = 0; i < width; i++)
53 temp[i] = in[i][j];
54 forwardHaar(temp, temp.length);
55 for (int i = 0; i < width; i++)
56 in[i][j] = temp[i];
57 }
58 }
59
60
61 //nonstandard reconstruction
62 public static void backwardHaarNSR(short in[][]) {
63 int width = in.length;
64 int height = in[0].length;
65 short out[] = new short[width];
66 for (int i = 0; i < width; i++)
67 backwardHaar(in[i]);
68 for (int j = 0; j < height; j++) {
69 for (int i = 0; i < width; i++)
70 out[i] = in[i][j];
71 backwardHaar(out);
72 for (int i = 0; i < width; i++)
73 in[i][j] = out[i];
74 }
75 }
76
77 public static void forwardHaar(short in[], int n) {
78 int i;
79 if (n < 2) return;
80 int nOn2 = n / 2;
81 short s[] = new short[nOn2];
82 short d[] = new short[nOn2];
83 for (i = 0; i < nOn2; i++) {
84 s[i] = in[2 * i];
85 d[i] = in[2 * i + 1];
86 }
87 for (i = 0; i < s.length; i++) {
88 d[i] = (short) (d[i] - s[i]);
89 s[i] = (short) (s[i] + d[i] / 2);
90 }
91 for (i = 0; i < s.length; i++) {
92 in[i] = s[i];
93 in[i + nOn2] = d[i];
94 }
95 forwardHaar(in, nOn2);
96 }
97
98 public static void forwardHaar(int in[], int n) {
99 int i;
100 if (n < 2) return;
101 int nOn2 = n / 2;
102 int s[] = new int[nOn2];
103 int d[] = new int[nOn2];
104 for (i = 0; i < nOn2; i++) {
105 s[i] = in[2 * i];
106 d[i] = in[2 * i + 1];
107 }
108 for (i = 0; i < s.length; i++) {
109 d[i] = (int) (d[i] - s[i]);
110 s[i] = (int) (s[i] + d[i] / 2);
111 }
112 for (i = 0; i < s.length; i++) {
113 in[i] = s[i];
114 in[i + nOn2] = d[i];
115 }
116 forwardHaar(in, nOn2);
117 }
118
119 public static void backwardHaar(short in[]) {
120 int n = in.length;
121 if (n < 2) return;
122 int nOn2 = n / 2;
123 short s[] = new short[nOn2];
124 for (int i = 0; i < nOn2; i++)
125 s[i] = in[i];
126 backwardHaar(s);
127 nOn2 = in.length / 2;
128 short d[] = new short[nOn2];
129 for (int i = 0; i < d.length; i++)
130 d[i] = in[i + nOn2];
131 for (int i = 0; i < s.length; i++)
132 s[i] = (short) (s[i] - d[i] / 2);
133 for (int i = 0; i < s.length; i++)
134 d[i] = (short) (d[i] + s[i]);
135 for (int i = 0; i < s.length; i++) {
136 in[2 * i] = s[i];
137 in[2 * i + 1] = d[i];
138 }
139 }
140
141 public static void backwardHaar(int in[]) {
142 int n = in.length;
143 if (n < 2) return;
144 int nOn2 = n / 2;
145 int s[] = new int[nOn2];
146 for (int i = 0; i < nOn2; i++)
147 s[i] = in[i];
148 backwardHaar(s);
149 nOn2 = in.length / 2;
150 int d[] = new int[nOn2];
151 for (int i = 0; i < d.length; i++)
152 d[i] = in[i + nOn2];
153 for (int i = 0; i < s.length; i++)
154 s[i] = (int) (s[i] - d[i] / 2);
155 for (int i = 0; i < s.length; i++)
156 d[i] = (int) (d[i] + s[i]);
157 for (int i = 0; i < s.length; i++) {
158 in[2 * i] = s[i];
159 in[2 * i + 1] = d[i];
160 }
161 }
162
163 // Transpose matrix input.
164 private static short[][] transpose(short in[][]) {
165 int width = in.length;
166 int height = in[0].length;
167 short[][] output = new short[height][width];
168
169 for (int j = 0; j < width; j++)
170 for (int i = 0; i < height; i++)
171 output[j][i] = in[i][j];
172
173 return output;
174 } // End of function transpose().
175
176 private static int[][] transpose(int in[][]) {
177 int width = in.length;
178 int height = in[0].length;
179 int[][] output = new int[height][width];
180
181 for (int j = 0; j < width; j++)
182 for (int i = 0; i < height; i++)
183 output[j][i] = in[i][j];
184
185 return output;
186 } // End of function transpose().
187
188
189 public static void print(short in[]) {
190 for (int i = 0; i < in.length; i++)
191 System.out.print(in[i] + " ");
192 System.out.println();
193 }
194
195 public static void print(short in[][]) {
196 for (int x = 0; x < in.length; x++) {
197 for (int y = 0; y < in[0].length; y++)
198 System.out.print(in[x][y] + " ");
199 System.out.println();
200 }
201 }
202
203
204 public static void main(String[] args) {
205
206 short input[][] = {
207 {1, 2, 2, 2},
208 {251, 0, 254, 254},
209 {251, 254, 254, 254},
210 {251, 254, 254, 254}};
211
212 System.out.println("Input:");
213 print(input);
214
215 // since forwardHaar uses recursion, so we can see the procedure of coding
216 forwardHaar(input);
217 System.out.println("Transmission:");
218 print(input);
219 backwardHaar(input);
220 System.out.println("Reconstruction:");
221 print(input);
222 }
223
224 public static void main1d(String[] args) {
225
226 short input[] = {1, 2, 251, 254, 255, 0, 128, 64, 0};
227
228 System.out.println("Input:");
229 print(input);
230
231 // since forwardHaar uses recursion, so we can see the procedure of coding
232 forwardHaar(input, input.length);
233 System.out.println("Transmission:");
234 print(input);
235 backwardHaar(input);
236 System.out.println("Reconstruction:");
237 print(input);
238 }
239
240 }