Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / libjasper / jpc_tsfb.c
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2004 Michael David Adams.
5  * All rights reserved.
6  */
7
8 /* __START_OF_JASPER_LICENSE__
9  * 
10  * JasPer License Version 2.0
11  * 
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  * 
16  * All rights reserved.
17  * 
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  * 
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  * 
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  * 
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * Tree-Structured Filter Bank (TSFB) Library
66  *
67  * $Id: jpc_tsfb.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
68  */
69
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73
74 #include <assert.h>
75
76 #include "jasper/jas_malloc.h"
77 #include "jasper/jas_seq.h"
78
79 #include "jpc_tsfb.h"
80 #include "jpc_cod.h"
81 #include "jpc_cs.h"
82 #include "jpc_util.h"
83 #include "jpc_math.h"
84
85 void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
86   int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
87   int numlvls);
88
89 /******************************************************************************\
90 *
91 \******************************************************************************/
92
93 jpc_tsfb_t *jpc_cod_gettsfb(int qmfbid, int numlvls)
94 {
95         jpc_tsfb_t *tsfb;
96
97         if (!(tsfb = malloc(sizeof(jpc_tsfb_t))))
98                 return 0;
99
100         if (numlvls > 0) {
101                 switch (qmfbid) {
102                 case JPC_COX_INS:
103                         tsfb->qmfb = &jpc_ns_qmfb2d;
104                         break;
105                 default:
106                 case JPC_COX_RFT:
107                         tsfb->qmfb = &jpc_ft_qmfb2d;
108                         break;
109                 }
110         } else {
111                 tsfb->qmfb = 0;
112         }
113         tsfb->numlvls = numlvls;
114         return tsfb;
115 }
116
117 void jpc_tsfb_destroy(jpc_tsfb_t *tsfb)
118 {
119         free(tsfb);
120 }
121
122 int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
123 {
124         return (tsfb->numlvls > 0) ? jpc_tsfb_analyze2(tsfb, jas_seq2d_getref(a,
125           jas_seq2d_xstart(a), jas_seq2d_ystart(a)), jas_seq2d_xstart(a),
126           jas_seq2d_ystart(a), jas_seq2d_width(a),
127           jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0;
128 }
129
130 int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart,
131   int width, int height, int stride, int numlvls)
132 {
133         if (width > 0 && height > 0) {
134                 if ((*tsfb->qmfb->analyze)(a, xstart, ystart, width, height, stride))
135                         return -1;
136                 if (numlvls > 0) {
137                         if (jpc_tsfb_analyze2(tsfb, a, JPC_CEILDIVPOW2(xstart,
138                           1), JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(
139                           xstart + width, 1) - JPC_CEILDIVPOW2(xstart, 1),
140                           JPC_CEILDIVPOW2(ystart + height, 1) -
141                           JPC_CEILDIVPOW2(ystart, 1), stride, numlvls - 1)) {
142                                 return -1;
143                         }
144                 }
145         }
146         return 0;
147 }
148
149 int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
150 {
151         return (tsfb->numlvls > 0) ? jpc_tsfb_synthesize2(tsfb,
152           jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
153           jas_seq2d_xstart(a), jas_seq2d_ystart(a), jas_seq2d_width(a),
154           jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0;
155 }
156
157 int jpc_tsfb_synthesize2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart,
158   int width, int height, int stride, int numlvls)
159 {
160         if (numlvls > 0) {
161                 if (jpc_tsfb_synthesize2(tsfb, a, JPC_CEILDIVPOW2(xstart, 1),
162                   JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(xstart + width,
163                   1) - JPC_CEILDIVPOW2(xstart, 1), JPC_CEILDIVPOW2(ystart +
164                   height, 1) - JPC_CEILDIVPOW2(ystart, 1), stride, numlvls -
165                   1)) {
166                         return -1;
167                 }
168         }
169         if (width > 0 && height > 0) {
170                 if ((*tsfb->qmfb->synthesize)(a, xstart, ystart, width, height, stride)) {
171                         return -1;
172                 }
173         }
174         return 0;
175 }
176
177 int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart,
178   uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend,
179   jpc_tsfb_band_t *bands)
180 {
181         jpc_tsfb_band_t *band;
182
183         band = bands;
184         if (tsfb->numlvls > 0) {
185                 jpc_tsfb_getbands2(tsfb, xstart, ystart, xstart, ystart, xend, yend,
186                   &band, tsfb->numlvls);
187         } else {
188
189                 band->xstart = xstart;
190                 band->ystart = ystart;
191                 band->xend = xend;
192                 band->yend = yend;
193                 band->locxstart = xstart;
194                 band->locystart = ystart;
195                 band->locxend = band->locxstart + band->xend - band->xstart;
196                 band->locyend = band->locystart + band->yend - band->ystart;
197                 band->orient = JPC_TSFB_LL;
198                 band->synenergywt = JPC_FIX_ONE;
199                 ++band;
200         }
201         return band - bands;
202 }
203
204 void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
205   int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
206   int numlvls)
207 {
208         int newxstart;
209         int newystart;
210         int newxend;
211         int newyend;
212         jpc_tsfb_band_t *band;
213
214         newxstart = JPC_CEILDIVPOW2(xstart, 1);
215         newystart = JPC_CEILDIVPOW2(ystart, 1);
216         newxend = JPC_CEILDIVPOW2(xend, 1);
217         newyend = JPC_CEILDIVPOW2(yend, 1);
218
219         if (numlvls > 0) {
220
221                 jpc_tsfb_getbands2(tsfb, locxstart, locystart, newxstart, newystart,
222                   newxend, newyend, bands, numlvls - 1);
223
224                 band = *bands;
225                 band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
226                 band->ystart = newystart;
227                 band->xend = JPC_FLOORDIVPOW2(xend, 1);
228                 band->yend = newyend;
229                 band->locxstart = locxstart + newxend - newxstart;
230                 band->locystart = locystart;
231                 band->locxend = band->locxstart + band->xend - band->xstart;
232                 band->locyend = band->locystart + band->yend - band->ystart;
233                 band->orient = JPC_TSFB_HL;
234                 band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
235                   tsfb->numlvls - numlvls] * tsfb->qmfb->lpenergywts[
236                   tsfb->numlvls - numlvls]);
237                 ++(*bands);
238
239                 band = *bands;
240                 band->xstart = newxstart;
241                 band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
242                 band->xend = newxend;
243                 band->yend = JPC_FLOORDIVPOW2(yend, 1);
244                 band->locxstart = locxstart;
245                 band->locystart = locystart + newyend - newystart;
246                 band->locxend = band->locxstart + band->xend - band->xstart;
247                 band->locyend = band->locystart + band->yend - band->ystart;
248                 band->orient = JPC_TSFB_LH;
249                 band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
250                   tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
251                   tsfb->numlvls - numlvls]);
252                 ++(*bands);
253
254                 band = *bands;
255                 band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
256                 band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
257                 band->xend = JPC_FLOORDIVPOW2(xend, 1);
258                 band->yend = JPC_FLOORDIVPOW2(yend, 1);
259                 band->locxstart = locxstart + newxend - newxstart;
260                 band->locystart = locystart + newyend - newystart;
261                 band->locxend = band->locxstart + band->xend - band->xstart;
262                 band->locyend = band->locystart + band->yend - band->ystart;
263                 band->orient = JPC_TSFB_HH;
264                 band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
265                   tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
266                   tsfb->numlvls - numlvls]);
267                 ++(*bands);
268
269         } else {
270
271                 band = *bands;
272                 band->xstart = xstart;
273                 band->ystart = ystart;
274                 band->xend = xend;
275                 band->yend = yend;
276                 band->locxstart = locxstart;
277                 band->locystart = locystart;
278                 band->locxend = band->locxstart + band->xend - band->xstart;
279                 band->locyend = band->locystart + band->yend - band->ystart;
280                 band->orient = JPC_TSFB_LL;
281                 band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
282                   tsfb->numlvls - numlvls - 1] * tsfb->qmfb->lpenergywts[
283                   tsfb->numlvls - numlvls - 1]);
284                 ++(*bands);
285
286         }
287
288 }