2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
36 #define S(i) a[x*(i)*2]
37 #define D(i) a[x*(1+(i)*2)]
38 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
39 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
41 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
42 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
45 /* This table contains the norms of the 5-3 wavelets for different bands. */
47 double dwt_norms[4][10] = {
48 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
49 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
50 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
51 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
55 /* This table contains the norms of the 9-7 wavelets for different bands. */
57 double dwt_norms_real[4][10] = {
58 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
59 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
60 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
61 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
66 static int lastSizeOfB = 0;
81 /* Forward lazy transform. */
83 void dwt_deinterleave(int *a, int n, int x, int res, int cas)
88 if (lastSizeOfB != n) {
91 b = (int *) malloc(n * sizeof(int));
96 for (i = 0; i < sn; i++)
97 b[i] = a[(2 * i + 1) * x];
98 for (i = 0; i < dn; i++)
99 b[sn + i] = a[2 * i * x];
101 for (i = 0; i < sn; i++)
103 for (i = 0; i < dn; i++)
104 b[sn + i] = a[(2 * i + 1) * x];
106 for (i = 0; i < n; i++)
111 /* Forward lazy transform. */
113 void dwt_deinterleave_real(int *a, int n, int x, int res, int cas)
119 if (lastSizeOfB != n) {
122 b = (int *) malloc(n * sizeof(int));
127 for (i = 0; i < sn; i++)
128 b[i] = a[(2 * i + 1) * x];
129 for (i = 0; i < dn; i++)
130 b[sn + i] = a[2 * i * x];
132 for (i = 0; i < sn; i++)
134 for (i = 0; i < dn; i++)
135 b[sn + i] = a[(2 * i + 1) * x];
137 for (i = 0; i < n; i++)
143 /* Inverse lazy transform. */
145 void dwt_interleave(int *a, int n, int x, int res, int cas)
151 if (lastSizeOfB != n) {
154 b = (int *) malloc(n * sizeof(int));
159 for (i = 0; i < sn; i++)
160 b[2 * i + 1] = a[i * x];
161 for (i = 0; i < dn; i++)
162 b[2 * i] = a[(sn + i) * x];
164 for (i = 0; i < sn; i++)
166 for (i = 0; i < dn; i++)
167 b[2 * i + 1] = a[(sn + i) * x];
169 for (i = 0; i < n; i++)
174 /* Inverse lazy transform. */
176 void dwt_interleave_real(int *a, int n, int x, int res, int cas)
182 if (lastSizeOfB != n) {
185 b = (int *) malloc(n * sizeof(int));
190 for (i = 0; i < sn; i++)
191 b[2 * i + 1] = a[i * x];
192 for (i = 0; i < dn; i++)
193 b[2 * i] = a[(sn + i) * x];
195 for (i = 0; i < sn; i++)
197 for (i = 0; i < dn; i++)
198 b[2 * i + 1] = a[(sn + i) * x];
200 for (i = 0; i < n; i++)
206 /* Forward 5-3 wavelet tranform in 1-D. */
208 void dwt_encode_1(int *a, int n, int x, int res, int cas)
215 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
218 for (i = 0; i < dn; i++)
219 S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
220 for (i = 0; i < sn; i++)
221 D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
224 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
225 for (i = 0; i < dn; i++)
226 D(i) -= (S_(i) + S_(i + 1)) >> 1;
227 for (i = 0; i < sn; i++)
228 S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
231 dwt_deinterleave(a, n, x, res, cas);
235 /* Inverse 5-3 wavelet tranform in 1-D. */
237 void dwt_decode_1(int *a, int n, int x, int res, int cas)
245 dwt_interleave(a, n, x, res, cas);
247 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
250 for (i = 0; i < sn; i++)
251 D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
252 for (i = 0; i < dn; i++)
253 S(i) += (DD_(i) + DD_(i - 1)) >> 1;
256 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
257 for (i = 0; i < sn; i++)
258 S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
259 for (i = 0; i < dn; i++)
260 D(i) += (S_(i) + S_(i + 1)) >> 1;
266 /* Forward 5-3 wavelet tranform in 2-D. */
268 void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
270 int i, j, rw, rh, rw1, rh1;
271 int previous_rw, previous_rh, previous_rw1, previous_rh1;
273 for (i = 0; i < l; i++) {
276 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
277 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
279 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
281 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
283 /* Check the different cases for that it's necessary to invert high pass and low pass filter */
285 if (tilec->previous_row) {
287 tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
291 tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
295 /* inversion on the previous and propagation of the inversion on the other tile */
296 if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
297 || (tilec->resolutions[l - i - 1].cas_row
298 && previous_rw1 == previous_rw - previous_rw1))
302 if (tilec->previous_col) {
304 tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
308 tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
312 /* inversion on the previous and propagation of the inversion on the other tile */
313 if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
314 || (tilec->resolutions[l - i - 1].cas_col
315 && previous_rh1 == previous_rh - previous_rh1))
319 /* subband LL shorter than LH or HL */
325 /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
326 if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
328 if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
331 tilec->resolutions[l - i - 1].cas_row = cas_row;
332 tilec->resolutions[l - i - 1].cas_col = cas_col;
334 for (j = 0; j < rw; j++)
335 dwt_encode_1(a + j, rh, w, rh1, cas_col);
336 for (j = 0; j < rh; j++)
337 dwt_encode_1(a + j * w, rw, 1, rw1, cas_row);
344 /* Inverse 5-3 wavelet tranform in 2-D. */
346 void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
347 tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
349 int i, j, rw, rh, rw1, rh1;
350 int previous_rw, previous_rh, previous_rw1, previous_rh1;
351 for (i = l - 1; i >= 0; i--) {
355 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
356 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
358 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
360 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
362 if (tilec->previous_row) {
364 tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
368 tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
372 if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
373 || (row_tilec->resolutions[l - i - 1].cas_row
374 && previous_rw1 == previous_rw - previous_rw1))
378 if (tilec->previous_col) {
380 tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
384 tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
388 if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
389 || (col_tilec->resolutions[l - i - 1].cas_col
390 && previous_rh1 == previous_rh - previous_rh1))
399 /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
400 if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
402 if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
405 tilec->resolutions[l - i - 1].cas_row = cas_row;
406 tilec->resolutions[l - i - 1].cas_col = cas_col;
408 for (j = 0; j < rh; j++)
409 dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
410 for (j = 0; j < rw; j++)
411 dwt_decode_1(a + j, rh, w, rh1, cas_col);
417 /* Get gain of 5-3 wavelet transform. */
419 int dwt_getgain(int orient)
423 if (orient == 1 || orient == 2)
429 /* Get norm of 5-3 wavelet. */
431 double dwt_getnorm(int level, int orient)
433 return dwt_norms[orient][level];
437 /* Forward 9-7 wavelet transform in 1-D. */
439 void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
446 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
447 for (i = 0; i < dn; i++)
448 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
449 for (i = 0; i < sn; i++)
450 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
451 for (i = 0; i < dn; i++)
452 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
453 for (i = 0; i < sn; i++)
454 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
455 for (i = 0; i < dn; i++)
456 S(i) = fix_mul(S(i), 5038);
457 for (i = 0; i < sn; i++)
458 D(i) = fix_mul(D(i), 6660);
461 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
462 for (i = 0; i < dn; i++)
463 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
464 for (i = 0; i < sn; i++)
465 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
466 for (i = 0; i < dn; i++)
467 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
468 for (i = 0; i < sn; i++)
469 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
470 for (i = 0; i < dn; i++)
471 D(i) = fix_mul(D(i), 5038);
472 for (i = 0; i < sn; i++)
473 S(i) = fix_mul(S(i), 6660);
476 dwt_deinterleave_real(a, n, x, res, cas);
480 /* Inverse 9-7 wavelet transform in 1-D. */
482 void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
487 dwt_interleave_real(a, n, x, res, cas);
489 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
490 for (i = 0; i < sn; i++)
491 D(i) = fix_mul(D(i), 10076);
492 for (i = 0; i < dn; i++)
493 S(i) = fix_mul(S(i), 13320);
494 for (i = 0; i < sn; i++)
495 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
496 for (i = 0; i < dn; i++)
497 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
498 for (i = 0; i < sn; i++)
499 D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
500 for (i = 0; i < dn; i++)
501 S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993);
504 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
505 for (i = 0; i < sn; i++)
506 S(i) = fix_mul(S(i), 10076);
507 for (i = 0; i < dn; i++)
508 D(i) = fix_mul(D(i), 13320);
509 for (i = 0; i < sn; i++)
510 S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
511 for (i = 0; i < dn; i++)
512 D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
513 for (i = 0; i < sn; i++)
514 S(i) += fix_mul(D_(i - 1) + D_(i), 434);
515 for (i = 0; i < dn; i++)
516 D(i) += fix_mul(S_(i) + S_(i + 1), 12993);
522 /* Forward 9-7 wavelet transform in 2-D. */
525 void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
527 int i, j, rw, rh, rw1, rh1;
528 int previous_rw, previous_rh, previous_rw1, previous_rh1;
530 for (i = 0; i < l; i++) {
533 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
534 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
536 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
538 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
540 if (tilec->previous_row) {
542 tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
546 tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
550 if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
551 || (tilec->resolutions[l - i - 1].cas_row
552 && previous_rw1 == previous_rw - previous_rw1))
556 if (tilec->previous_col) {
558 tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
562 tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
566 if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
567 || (tilec->resolutions[l - i - 1].cas_col
568 && previous_rh1 == previous_rh - previous_rh1))
577 /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
578 if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
580 if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
583 tilec->resolutions[l - i - 1].cas_row = cas_row;
584 tilec->resolutions[l - i - 1].cas_col = cas_col;
586 for (j = 0; j < rw; j++)
587 dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
588 for (j = 0; j < rh; j++)
589 dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row);
594 /* Inverse 9-7 wavelet transform in 2-D. */
596 void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
597 tcd_tilecomp_t * row_tilec,
598 tcd_tilecomp_t * col_tilec)
600 int i, j, rw, rh, rw1, rh1;
601 int previous_rw, previous_rh, previous_rw1, previous_rh1;
603 for (i = l - 1; i >= 0; i--) {
607 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
608 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
610 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
612 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
614 if (tilec->previous_row) {
616 tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
620 tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
624 if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
625 || (row_tilec->resolutions[l - i - 1].cas_row
626 && previous_rw1 == previous_rw - previous_rw1))
630 if (tilec->previous_col) {
632 tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
636 tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
640 if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
641 || (col_tilec->resolutions[l - i - 1].cas_col
642 && previous_rh1 == previous_rh - previous_rh1))
651 /* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
652 if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
654 if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
657 tilec->resolutions[l - i - 1].cas_row = cas_row;
658 tilec->resolutions[l - i - 1].cas_col = cas_col;
660 for (j = 0; j < rh; j++)
661 dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
662 for (j = 0; j < rw; j++)
663 dwt_decode_1_real(a + j, rh, w, rh1, cas_col);
668 /* Get gain of 9-7 wavelet transform. */
670 int dwt_getgain_real(int orient)
676 /* Get norm of 9-7 wavelet. */
678 double dwt_getnorm_real(int level, int orient)
680 return dwt_norms_real[orient][level];