Modifications to decoding of JP2H box in order to be compatible with JP2 conformance...
[openjpeg.git] / libopenjpeg / dwt.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * Copyright (c) 2002-2004, Yannick Verschueren
4  * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * Copyright (c) 2005, Reiner Wahler
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /*
31  *  NOTE:
32  *  This is a modified version of the openjpeg dwt.c file.
33  *  Average speed improvement compared to the original file (measured on
34  *  my own machine, a P4 running at 3.0 GHz):
35  *  5x3 wavelets about 2 times faster
36  *  9x7 wavelets about 3 times faster
37  *  for both, encoding and decoding.
38  *
39  *  The better performance is caused by doing the 1-dimensional DWT
40  *  within a temporary buffer where the data can be accessed sequential
41  *  for both directions, horizontal and vertical. The 2d vertical DWT was
42  *  the major bottleneck in the former version.
43  *
44  *  I have also removed the "Add Patrick" part because it is not longer
45  *  needed.  
46  *
47  *  6/6/2005
48  *  -Ive (aka Reiner Wahler)
49  *  mail: ive@lilysoft.com
50  */
51
52
53 #include "dwt.h"
54 #include "int.h"
55 #include "fix.h"
56 #include "tcd.h"
57 #include <stdlib.h>
58 //#include <stdio.h>
59 //#include <math.h>
60
61 #define S(i) a[(i)*2]
62 #define D(i) a[(1+(i)*2)]
63 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
64 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
65 /* new */
66 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
67 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
68
69 /* <summary>                                                              */
70 /* This table contains the norms of the 5-3 wavelets for different bands. */
71 /* </summary>                                                             */
72 double dwt_norms[4][10] = {
73   {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
74   {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
75   {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
76   {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
77 };
78
79 /* <summary>                                                              */
80 /* This table contains the norms of the 9-7 wavelets for different bands. */
81 /* </summary>                                                             */
82 double dwt_norms_real[4][10] = {
83   {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
84   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
85   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
86   {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
87 };
88
89
90 /* <summary>                                     */
91 /* Forward lazy transform (horizontal).  */
92 /* </summary>                            */ 
93 void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
94     int i;
95     for (i=0; i<sn; i++) b[i]=a[2*i+cas];
96     for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
97 }
98
99 /* <summary>                             */  
100 /* Forward lazy transform (vertical).    */
101 /* </summary>                            */ 
102 void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
103     int i;
104     for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
105     for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
106 }
107
108 /* <summary>                             */
109 /* Inverse lazy transform (horizontal).  */
110 /* </summary>                            */
111 void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) {
112     int i;
113 //    for (i=0; i<sn; i++) b[2*i+cas]=a[i];
114 //    for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)];
115     int* ai;
116     int* bi;
117     ai=a;
118     bi=b+cas;
119     for (i=0; i<sn; i++) {
120       *bi = *ai;  bi+=2;  ai++;
121     }
122     ai=a+sn;
123     bi=b+1-cas;
124     for (i=0; i<dn; i++) {
125       *bi = *ai;  bi+=2;  ai++;
126     }
127 }
128
129 /* <summary>                             */  
130 /* Inverse lazy transform (vertical).    */
131 /* </summary>                            */ 
132 void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
133     int i;
134 //    for (i=0; i<sn; i++) b[2*i+cas]=a[i*x];
135 //    for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)*x];
136     int* ai;
137     int* bi;
138     ai=a;
139     bi=b+cas;
140     for (i=0; i<sn; i++) {
141       *bi = *ai;  bi+=2;  ai+=x;
142     }
143     ai=a+(sn*x);
144     bi=b+1-cas;
145     for (i=0; i<dn; i++) {
146       *bi = *ai;  bi+=2;  ai+=x;
147     }
148 }
149
150
151 /* <summary>                            */
152 /* Forward 5-3 wavelet tranform in 1-D. */
153 /* </summary>                           */
154 void dwt_encode_1(int *a, int dn, int sn, int cas)
155 {
156   int i;
157
158   if (!cas) {
159     if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
160       for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
161       for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
162     }
163   } else {
164     if (!sn && dn == 1)             /* NEW :  CASE ONE ELEMENT */
165       S(0) *= 2;
166     else {
167       for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
168       for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
169     }
170
171   }
172 }
173
174 /* <summary>                            */
175 /* Inverse 5-3 wavelet tranform in 1-D. */
176 /* </summary>                           */ 
177 void dwt_decode_1(int *a, int dn, int sn, int cas) 
178 {
179   int i;
180
181   if (!cas) {
182           if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
183       for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
184       for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
185     }
186   } else {
187     if (!sn  && dn == 1)          /* NEW :  CASE ONE ELEMENT */
188       S(0) /= 2;
189     else {
190       for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
191       for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
192     }
193   }
194 }
195
196
197 /* <summary>                            */
198 /* Forward 5-3 wavelet tranform in 2-D. */
199 /* </summary>                           */
200 void dwt_encode(tcd_tilecomp_t * tilec)
201 {
202   int i, j, k;
203   int* a;
204   int* aj;
205   int* bj;
206   int w, l;
207
208   w = tilec->x1-tilec->x0;
209   l = tilec->numresolutions-1;
210   a = tilec->data;
211
212   for (i = 0; i < l; i++) {
213     int rw;                     /* width of the resolution level computed                                                           */
214     int rh;                     /* heigth of the resolution level computed                                                          */
215     int rw1;            /* width of the resolution level once lower than computed one                                       */
216     int rh1;            /* height of the resolution level once lower than computed one                                      */
217     int cas_col;        /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
218     int cas_row;        /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
219     int dn, sn;
220
221     rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
222     rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
223     rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
224     rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
225
226     cas_row = tilec->resolutions[l - i].x0 % 2;
227     cas_col = tilec->resolutions[l - i].y0 % 2;
228
229
230     sn = rh1;
231     dn = rh - rh1;
232     bj=(int*)malloc(rh*sizeof(int));
233     for (j=0; j<rw; j++) {
234       aj=a+j;
235       for (k=0; k<rh; k++)  bj[k]=aj[k*w];
236       dwt_encode_1(bj, dn, sn, cas_col);
237       dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
238     }
239     free(bj);
240
241     sn = rw1;
242     dn = rw - rw1;
243     bj=(int*)malloc(rw*sizeof(int));
244     for (j=0; j<rh; j++) {
245       aj=a+j*w;
246       for (k=0; k<rw; k++)  bj[k]=aj[k];
247       dwt_encode_1(bj, dn, sn, cas_row);
248       dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
249     }
250     free(bj);
251   }
252 }
253
254
255 /* <summary>                            */
256 /* Inverse 5-3 wavelet tranform in 2-D. */
257 /* </summary>                           */
258 void dwt_decode(tcd_tilecomp_t * tilec, int stop)
259 {
260   int i, j, k;
261   int* a;
262   int* aj;
263   int* bj;
264   int w, l;
265
266   w = tilec->x1-tilec->x0;
267   l = tilec->numresolutions-1;
268   a = tilec->data;
269
270   for (i = l - 1; i >= stop; i--) {
271     int rw;                     /* width of the resolution level computed                                                           */
272     int rh;                     /* heigth of the resolution level computed                                                          */
273     int rw1;            /* width of the resolution level once lower than computed one                                       */
274     int rh1;            /* height of the resolution level once lower than computed one                                      */
275     int cas_col;        /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
276     int cas_row;        /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
277     int dn, sn;
278
279     rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
280     rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
281     rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
282     rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
283
284     cas_row = tilec->resolutions[l - i].x0 % 2;
285     cas_col = tilec->resolutions[l - i].y0 % 2;
286
287     sn = rw1;
288     dn = rw - rw1;
289     bj=(int*)malloc(rw*sizeof(int));
290     for (j = 0; j < rh; j++) {
291       aj = a+j*w;
292       dwt_interleave_h(aj, bj, dn, sn, cas_row);
293       dwt_decode_1(bj, dn, sn, cas_row);
294       for (k = 0; k < rw; k++)  aj[k] = bj[k];
295     }
296     free(bj);
297
298     sn = rh1;
299     dn = rh - rh1;
300     bj=(int*)malloc(rh*sizeof(int));
301     for (j = 0; j < rw; j++) {
302       aj = a+j;
303       dwt_interleave_v(aj, bj, dn, sn, w, cas_col);
304       dwt_decode_1(bj, dn, sn, cas_col);
305       for (k = 0; k < rh; k++)  aj[k * w] = bj[k];
306     }
307     free(bj);
308
309   }
310 }
311
312
313 /* <summary>                          */
314 /* Get gain of 5-3 wavelet transform. */
315 /* </summary>                         */
316 int dwt_getgain(int orient)
317 {
318   if (orient == 0)
319     return 0;
320   if (orient == 1 || orient == 2)
321     return 1;
322   return 2;
323 }
324
325 /* <summary>                */
326 /* Get norm of 5-3 wavelet. */
327 /* </summary>               */
328 double dwt_getnorm(int level, int orient)
329 {
330   return dwt_norms[orient][level];
331 }
332
333 /* <summary>                             */
334 /* Forward 9-7 wavelet transform in 1-D. */
335 /* </summary>                            */
336 void dwt_encode_1_real(int *a, int dn, int sn, int cas)
337 {
338   int i;
339   if (!cas) {
340     if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
341       for (i = 0; i < dn; i++)
342             D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
343       for (i = 0; i < sn; i++)
344             S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
345       for (i = 0; i < dn; i++)
346             D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
347       for (i = 0; i < sn; i++)
348             S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
349       for (i = 0; i < dn; i++)
350             D(i) = fix_mul(D(i), 5038); /*5038 */
351       for (i = 0; i < sn; i++)
352             S(i) = fix_mul(S(i), 6659); /*6660 */
353     }
354   } else {
355     if ((sn > 0) || (dn > 1)) { /* NEW :  CASE ONE ELEMENT */
356       for (i = 0; i < dn; i++)
357             S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
358       for (i = 0; i < sn; i++)
359             D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
360       for (i = 0; i < dn; i++)
361             S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
362       for (i = 0; i < sn; i++)
363             D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
364       for (i = 0; i < dn; i++)
365             S(i) = fix_mul(S(i), 5038); /*5038 */
366       for (i = 0; i < sn; i++)
367             D(i) = fix_mul(D(i), 6659); /*6660 */
368     }
369   }
370 }
371
372 /* <summary>                             */
373 /* Inverse 9-7 wavelet transform in 1-D. */
374 /* </summary>                            */
375 void dwt_decode_1_real(int *a, int dn, int sn, int cas)
376 {
377   int i;
378   if (!cas) {
379     if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
380       for (i = 0; i < sn; i++)
381             S(i) = fix_mul(S(i), 10078);        /* 10076 */
382       for (i = 0; i < dn; i++)
383             D(i) = fix_mul(D(i), 13318);        /* 13320 */
384       for (i = 0; i < sn; i++)
385             S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
386       for (i = 0; i < dn; i++)
387             D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
388       for (i = 0; i < sn; i++)
389             S(i) += fix_mul(D_(i - 1) + D_(i), 434);
390       for (i = 0; i < dn; i++)
391             D(i) += fix_mul(S_(i) + S_(i + 1), 12993);
392     }
393   } else {
394     if ((sn > 0) || (dn > 1)) { /* NEW :  CASE ONE ELEMENT */
395       for (i = 0; i < sn; i++)
396             D(i) = fix_mul(D(i), 10078);        /* 10076 */
397       for (i = 0; i < dn; i++)
398             S(i) = fix_mul(S(i), 13318);        /* 13320 */
399       for (i = 0; i < sn; i++)
400             D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
401       for (i = 0; i < dn; i++)
402             S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
403       for (i = 0; i < sn; i++)
404             D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
405       for (i = 0; i < dn; i++)
406         S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993);
407     }
408   }
409 }
410
411 /* <summary>                             */
412 /* Forward 9-7 wavelet transform in 2-D. */
413 /* </summary>                            */
414
415 void dwt_encode_real(tcd_tilecomp_t * tilec)
416 {
417   int i, j, k;
418   int* a;
419   int* aj;
420   int* bj;
421   int w, l;
422
423   w = tilec->x1-tilec->x0;
424   l = tilec->numresolutions-1;
425   a = tilec->data;
426
427   for (i = 0; i < l; i++) {
428     int rw;                     /* width of the resolution level computed                                                     */
429     int rh;                     /* heigth of the resolution level computed                                                    */
430     int rw1;            /* width of the resolution level once lower than computed one                                 */
431     int rh1;            /* height of the resolution level once lower than computed one                                */
432     int cas_col;        /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
433     int cas_row;        /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
434     int dn, sn;
435
436     rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
437     rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
438     rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
439     rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
440
441     cas_row = tilec->resolutions[l - i].x0 % 2;
442     cas_col = tilec->resolutions[l - i].y0 % 2;
443
444     sn = rh1;
445     dn = rh - rh1;
446     bj=(int*)malloc(rh*sizeof(int));
447     for (j = 0; j < rw; j++) {
448       aj = a + j;
449       for (k = 0; k < rh; k++)  bj[k] = aj[k*w];
450       dwt_encode_1_real(bj, dn, sn, cas_col);
451       dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
452     }
453     free(bj);
454
455     sn = rw1;
456     dn = rw - rw1;
457     bj=(int*)malloc(rw*sizeof(int));
458     for (j = 0; j < rh; j++) {
459       aj = a + j * w;
460       for (k = 0; k < rw; k++)  bj[k] = aj[k];
461       dwt_encode_1_real(bj, dn, sn, cas_row);
462       dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
463     }
464     free(bj);
465   }
466 }
467
468
469 /* <summary>                             */
470 /* Inverse 9-7 wavelet transform in 2-D. */
471 /* </summary>                            */
472 void dwt_decode_real(tcd_tilecomp_t * tilec, int stop)
473 {
474
475   int i, j, k;
476   int* a;
477   int* aj;
478   int* bj;
479   int w, l;
480
481   w = tilec->x1-tilec->x0;
482   l = tilec->numresolutions-1;
483   a = tilec->data;
484
485   for (i = l-1; i >= stop; i--) {
486     int rw;                     /* width of the resolution level computed                       */
487     int rh;                     /* heigth of the resolution level computed                      */
488     int rw1;            /* width of the resolution level once lower than computed one   */
489     int rh1;            /* height of the resolution level once lower than computed one  */
490     int cas_col;        /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
491     int cas_row;        /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
492     int dn, sn;
493
494     rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
495     rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
496     rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
497     rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
498
499     cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
500     cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
501
502     sn = rw1;
503     dn = rw-rw1;
504     bj = (int*)malloc(rw * sizeof(int));
505     for (j = 0; j < rh; j++) {
506       aj = a+j*w;
507       dwt_interleave_h(aj, bj, dn, sn, cas_col);
508       dwt_decode_1_real(bj, dn, sn, cas_col);
509       for (k = 0; k < rw; k++)  aj[k] = bj[k];
510     }
511     free(bj);
512
513     sn = rh1;
514     dn = rh-rh1;
515     bj = (int*)malloc(rh * sizeof(int));
516     for (j=0; j<rw; j++) {
517       aj = a+j;
518       dwt_interleave_v(aj, bj, dn, sn, w, cas_row);
519       dwt_decode_1_real(bj, dn, sn, cas_row);
520       for (k = 0; k < rh; k++)  aj[k * w] = bj[k];
521     }
522     free(bj);
523   }
524 }
525
526
527 /* <summary>                          */
528 /* Get gain of 9-7 wavelet transform. */
529 /* </summary>                         */
530 int dwt_getgain_real(int orient)
531 {
532   return 0;
533 }
534
535 /* <summary>                */
536 /* Get norm of 9-7 wavelet. */
537 /* </summary>               */
538 double dwt_getnorm_real(int level, int orient)
539 {
540   return dwt_norms_real[orient][level];
541 }