Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jasper/src/libjasper/jpc/jpc_tsfb.c
Line
Count
Source
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$
68
 */
69
70
/******************************************************************************\
71
* Includes.
72
\******************************************************************************/
73
74
#include "jpc_tsfb.h"
75
#include "jpc_cs.h"
76
#include "jpc_math.h"
77
#include "jpc_fix.h"
78
79
#include "jasper/jas_seq.h"
80
#include "jasper/jas_malloc.h"
81
82
#include <stdlib.h>
83
84
/******************************************************************************\
85
* Function prototypes.
86
\******************************************************************************/
87
88
static int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, jpc_fix_t *a, int xstart, int ystart,
89
  unsigned width, unsigned height, unsigned stride, unsigned numlvls);
90
91
static int jpc_tsfb_synthesize2(jpc_tsfb_t *tsfb, jpc_fix_t *a, int xstart, int ystart,
92
  unsigned width, unsigned height, unsigned stride, unsigned numlvls);
93
94
static void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
95
  int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
96
  unsigned numlvls);
97
98
/******************************************************************************\
99
*
100
\******************************************************************************/
101
102
jpc_tsfb_t *jpc_cod_gettsfb(unsigned qmfbid, unsigned numlvls)
103
29.7k
{
104
29.7k
  jpc_tsfb_t *tsfb;
105
106
29.7k
  if (!(tsfb = jas_malloc(sizeof(jpc_tsfb_t))))
107
0
    return 0;
108
109
29.7k
  if (numlvls > 0) {
110
18.7k
    switch (qmfbid) {
111
8.47k
    case JPC_COX_INS:
112
8.47k
      tsfb->qmfb = &jpc_ns_qmfb2d;
113
8.47k
      break;
114
0
    default:
115
10.2k
    case JPC_COX_RFT:
116
10.2k
      tsfb->qmfb = &jpc_ft_qmfb2d;
117
10.2k
      break;
118
18.7k
    }
119
18.7k
  } else {
120
11.0k
    tsfb->qmfb = 0;
121
11.0k
  }
122
29.7k
  tsfb->numlvls = numlvls;
123
29.7k
  return tsfb;
124
29.7k
}
125
126
void jpc_tsfb_destroy(jpc_tsfb_t *tsfb)
127
29.7k
{
128
29.7k
  jas_free(tsfb);
129
29.7k
}
130
131
int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
132
1.75k
{
133
1.75k
  if (tsfb->numlvls == 0)
134
0
    return 0;
135
136
1.75k
  return jpc_tsfb_analyze2(tsfb,
137
1.75k
         jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
138
1.75k
         jas_seq2d_xstart(a), jas_seq2d_ystart(a),
139
1.75k
         jas_seq2d_width(a), jas_seq2d_height(a),
140
1.75k
         jas_seq2d_rowstep(a), tsfb->numlvls - 1);
141
1.75k
}
142
143
static int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, jpc_fix_t *a, int xstart, int ystart,
144
  unsigned width, unsigned height, unsigned stride, unsigned numlvls)
145
8.77k
{
146
8.77k
  if (width == 0 || height == 0)
147
0
    return 0;
148
149
8.77k
  if ((*tsfb->qmfb->analyze)(a, xstart, ystart, width, height, stride))
150
0
    return -1;
151
152
8.77k
  if (numlvls == 0)
153
1.75k
    return 0;
154
155
7.01k
  return jpc_tsfb_analyze2(tsfb, a,
156
7.01k
         JPC_CEILDIVPOW2(xstart, 1),
157
7.01k
         JPC_CEILDIVPOW2(ystart, 1),
158
7.01k
         JPC_CEILDIVPOW2(xstart + width, 1) - JPC_CEILDIVPOW2(xstart, 1),
159
7.01k
         JPC_CEILDIVPOW2(ystart + height, 1) - JPC_CEILDIVPOW2(ystart, 1),
160
7.01k
         stride, numlvls - 1);
161
8.77k
}
162
163
int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
164
17.2k
{
165
17.2k
  if (tsfb->numlvls == 0 || jas_seq2d_empty(a))
166
6.85k
    return 0;
167
168
10.3k
  return jpc_tsfb_synthesize2(tsfb,
169
10.3k
            jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
170
10.3k
            jas_seq2d_xstart(a), jas_seq2d_ystart(a),
171
10.3k
            jas_seq2d_width(a), jas_seq2d_height(a),
172
10.3k
            jas_seq2d_rowstep(a),
173
10.3k
            tsfb->numlvls - 1);
174
17.2k
}
175
176
static int jpc_tsfb_synthesize2(jpc_tsfb_t *tsfb, jpc_fix_t *a, int xstart, int ystart,
177
  unsigned width, unsigned height, unsigned stride, unsigned numlvls)
178
90.6k
{
179
90.6k
  if (numlvls > 0) {
180
80.2k
    if (jpc_tsfb_synthesize2(tsfb, a, JPC_CEILDIVPOW2(xstart, 1),
181
80.2k
      JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(xstart + width,
182
80.2k
      1) - JPC_CEILDIVPOW2(xstart, 1), JPC_CEILDIVPOW2(ystart +
183
80.2k
      height, 1) - JPC_CEILDIVPOW2(ystart, 1), stride, numlvls -
184
80.2k
      1)) {
185
0
      return -1;
186
0
    }
187
80.2k
  }
188
189
90.6k
  if (width == 0 || height == 0)
190
18.2k
    return 0;
191
192
72.3k
  return tsfb->qmfb->synthesize(a, xstart, ystart, width, height, stride);
193
90.6k
}
194
195
int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart,
196
  uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend,
197
  jpc_tsfb_band_t *bands)
198
29.7k
{
199
29.7k
  jpc_tsfb_band_t *band;
200
201
29.7k
  band = bands;
202
29.7k
  if (tsfb->numlvls > 0) {
203
18.7k
    jpc_tsfb_getbands2(tsfb, xstart, ystart, xstart, ystart, xend, yend,
204
18.7k
      &band, tsfb->numlvls);
205
18.7k
  } else {
206
207
11.0k
    band->xstart = xstart;
208
11.0k
    band->ystart = ystart;
209
11.0k
    band->xend = xend;
210
11.0k
    band->yend = yend;
211
11.0k
    band->locxstart = xstart;
212
11.0k
    band->locystart = ystart;
213
11.0k
    band->locxend = band->locxstart + band->xend - band->xstart;
214
11.0k
    band->locyend = band->locystart + band->yend - band->ystart;
215
11.0k
    band->orient = JPC_TSFB_LL;
216
11.0k
    band->synenergywt = JPC_FIX_ONE;
217
11.0k
    ++band;
218
11.0k
  }
219
29.7k
  return band - bands;
220
29.7k
}
221
222
void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
223
  int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
224
  unsigned numlvls)
225
179k
{
226
179k
  int newxstart;
227
179k
  int newystart;
228
179k
  int newxend;
229
179k
  int newyend;
230
179k
  jpc_tsfb_band_t *band;
231
232
179k
  newxstart = JPC_CEILDIVPOW2(xstart, 1);
233
179k
  newystart = JPC_CEILDIVPOW2(ystart, 1);
234
179k
  newxend = JPC_CEILDIVPOW2(xend, 1);
235
179k
  newyend = JPC_CEILDIVPOW2(yend, 1);
236
237
179k
  if (numlvls > 0) {
238
239
160k
    jpc_tsfb_getbands2(tsfb, locxstart, locystart, newxstart, newystart,
240
160k
      newxend, newyend, bands, numlvls - 1);
241
242
160k
    band = *bands;
243
160k
    band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
244
160k
    band->ystart = newystart;
245
160k
    band->xend = JPC_FLOORDIVPOW2(xend, 1);
246
160k
    band->yend = newyend;
247
160k
    band->locxstart = locxstart + newxend - newxstart;
248
160k
    band->locystart = locystart;
249
160k
    band->locxend = band->locxstart + band->xend - band->xstart;
250
160k
    band->locyend = band->locystart + band->yend - band->ystart;
251
160k
    band->orient = JPC_TSFB_HL;
252
160k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
253
160k
      tsfb->numlvls - numlvls] * tsfb->qmfb->lpenergywts[
254
160k
      tsfb->numlvls - numlvls]);
255
160k
    ++(*bands);
256
257
160k
    band = *bands;
258
160k
    band->xstart = newxstart;
259
160k
    band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
260
160k
    band->xend = newxend;
261
160k
    band->yend = JPC_FLOORDIVPOW2(yend, 1);
262
160k
    band->locxstart = locxstart;
263
160k
    band->locystart = locystart + newyend - newystart;
264
160k
    band->locxend = band->locxstart + band->xend - band->xstart;
265
160k
    band->locyend = band->locystart + band->yend - band->ystart;
266
160k
    band->orient = JPC_TSFB_LH;
267
160k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
268
160k
      tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
269
160k
      tsfb->numlvls - numlvls]);
270
160k
    ++(*bands);
271
272
160k
    band = *bands;
273
160k
    band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
274
160k
    band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
275
160k
    band->xend = JPC_FLOORDIVPOW2(xend, 1);
276
160k
    band->yend = JPC_FLOORDIVPOW2(yend, 1);
277
160k
    band->locxstart = locxstart + newxend - newxstart;
278
160k
    band->locystart = locystart + newyend - newystart;
279
160k
    band->locxend = band->locxstart + band->xend - band->xstart;
280
160k
    band->locyend = band->locystart + band->yend - band->ystart;
281
160k
    band->orient = JPC_TSFB_HH;
282
160k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
283
160k
      tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
284
160k
      tsfb->numlvls - numlvls]);
285
160k
    ++(*bands);
286
287
160k
  } else {
288
289
18.7k
    band = *bands;
290
18.7k
    band->xstart = xstart;
291
18.7k
    band->ystart = ystart;
292
18.7k
    band->xend = xend;
293
18.7k
    band->yend = yend;
294
18.7k
    band->locxstart = locxstart;
295
18.7k
    band->locystart = locystart;
296
18.7k
    band->locxend = band->locxstart + band->xend - band->xstart;
297
18.7k
    band->locyend = band->locystart + band->yend - band->ystart;
298
18.7k
    band->orient = JPC_TSFB_LL;
299
18.7k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
300
18.7k
      tsfb->numlvls - numlvls - 1] * tsfb->qmfb->lpenergywts[
301
18.7k
      tsfb->numlvls - numlvls - 1]);
302
18.7k
    ++(*bands);
303
304
18.7k
  }
305
306
179k
}