Coverage Report

Created: 2025-12-03 07:28

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
36.2k
{
104
36.2k
  jpc_tsfb_t *tsfb;
105
106
36.2k
  if (!(tsfb = jas_malloc(sizeof(jpc_tsfb_t))))
107
0
    return 0;
108
109
36.2k
  if (numlvls > 0) {
110
26.3k
    switch (qmfbid) {
111
14.1k
    case JPC_COX_INS:
112
14.1k
      tsfb->qmfb = &jpc_ns_qmfb2d;
113
14.1k
      break;
114
0
    default:
115
12.2k
    case JPC_COX_RFT:
116
12.2k
      tsfb->qmfb = &jpc_ft_qmfb2d;
117
12.2k
      break;
118
26.3k
    }
119
26.3k
  } else {
120
9.96k
    tsfb->qmfb = 0;
121
9.96k
  }
122
36.2k
  tsfb->numlvls = numlvls;
123
36.2k
  return tsfb;
124
36.2k
}
125
126
void jpc_tsfb_destroy(jpc_tsfb_t *tsfb)
127
36.2k
{
128
36.2k
  jas_free(tsfb);
129
36.2k
}
130
131
int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
132
2.75k
{
133
2.75k
  if (tsfb->numlvls == 0)
134
0
    return 0;
135
136
2.75k
  return jpc_tsfb_analyze2(tsfb,
137
2.75k
         jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
138
2.75k
         jas_seq2d_xstart(a), jas_seq2d_ystart(a),
139
2.75k
         jas_seq2d_width(a), jas_seq2d_height(a),
140
2.75k
         jas_seq2d_rowstep(a), tsfb->numlvls - 1);
141
2.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
13.7k
{
146
13.7k
  if (width == 0 || height == 0)
147
0
    return 0;
148
149
13.7k
  if ((*tsfb->qmfb->analyze)(a, xstart, ystart, width, height, stride))
150
0
    return -1;
151
152
13.7k
  if (numlvls == 0)
153
2.75k
    return 0;
154
155
11.0k
  return jpc_tsfb_analyze2(tsfb, a,
156
11.0k
         JPC_CEILDIVPOW2(xstart, 1),
157
11.0k
         JPC_CEILDIVPOW2(ystart, 1),
158
11.0k
         JPC_CEILDIVPOW2(xstart + width, 1) - JPC_CEILDIVPOW2(xstart, 1),
159
11.0k
         JPC_CEILDIVPOW2(ystart + height, 1) - JPC_CEILDIVPOW2(ystart, 1),
160
11.0k
         stride, numlvls - 1);
161
13.7k
}
162
163
int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
164
22.4k
{
165
22.4k
  if (tsfb->numlvls == 0 || jas_seq2d_empty(a))
166
6.48k
    return 0;
167
168
16.0k
  return jpc_tsfb_synthesize2(tsfb,
169
16.0k
            jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
170
16.0k
            jas_seq2d_xstart(a), jas_seq2d_ystart(a),
171
16.0k
            jas_seq2d_width(a), jas_seq2d_height(a),
172
16.0k
            jas_seq2d_rowstep(a),
173
16.0k
            tsfb->numlvls - 1);
174
22.4k
}
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.9k
{
179
90.9k
  if (numlvls > 0) {
180
74.8k
    if (jpc_tsfb_synthesize2(tsfb, a, JPC_CEILDIVPOW2(xstart, 1),
181
74.8k
      JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(xstart + width,
182
74.8k
      1) - JPC_CEILDIVPOW2(xstart, 1), JPC_CEILDIVPOW2(ystart +
183
74.8k
      height, 1) - JPC_CEILDIVPOW2(ystart, 1), stride, numlvls -
184
74.8k
      1)) {
185
0
      return -1;
186
0
    }
187
74.8k
  }
188
189
90.9k
  if (width == 0 || height == 0)
190
12.7k
    return 0;
191
192
78.1k
  return tsfb->qmfb->synthesize(a, xstart, ystart, width, height, stride);
193
90.9k
}
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
36.2k
{
199
36.2k
  jpc_tsfb_band_t *band;
200
201
36.2k
  band = bands;
202
36.2k
  if (tsfb->numlvls > 0) {
203
26.3k
    jpc_tsfb_getbands2(tsfb, xstart, ystart, xstart, ystart, xend, yend,
204
26.3k
      &band, tsfb->numlvls);
205
26.3k
  } else {
206
207
9.96k
    band->xstart = xstart;
208
9.96k
    band->ystart = ystart;
209
9.96k
    band->xend = xend;
210
9.96k
    band->yend = yend;
211
9.96k
    band->locxstart = xstart;
212
9.96k
    band->locystart = ystart;
213
9.96k
    band->locxend = band->locxstart + band->xend - band->xstart;
214
9.96k
    band->locyend = band->locystart + band->yend - band->ystart;
215
9.96k
    band->orient = JPC_TSFB_LL;
216
9.96k
    band->synenergywt = JPC_FIX_ONE;
217
9.96k
    ++band;
218
9.96k
  }
219
36.2k
  return band - bands;
220
36.2k
}
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
171k
{
226
171k
  int newxstart;
227
171k
  int newystart;
228
171k
  int newxend;
229
171k
  int newyend;
230
171k
  jpc_tsfb_band_t *band;
231
232
171k
  newxstart = JPC_CEILDIVPOW2(xstart, 1);
233
171k
  newystart = JPC_CEILDIVPOW2(ystart, 1);
234
171k
  newxend = JPC_CEILDIVPOW2(xend, 1);
235
171k
  newyend = JPC_CEILDIVPOW2(yend, 1);
236
237
171k
  if (numlvls > 0) {
238
239
145k
    jpc_tsfb_getbands2(tsfb, locxstart, locystart, newxstart, newystart,
240
145k
      newxend, newyend, bands, numlvls - 1);
241
242
145k
    band = *bands;
243
145k
    band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
244
145k
    band->ystart = newystart;
245
145k
    band->xend = JPC_FLOORDIVPOW2(xend, 1);
246
145k
    band->yend = newyend;
247
145k
    band->locxstart = locxstart + newxend - newxstart;
248
145k
    band->locystart = locystart;
249
145k
    band->locxend = band->locxstart + band->xend - band->xstart;
250
145k
    band->locyend = band->locystart + band->yend - band->ystart;
251
145k
    band->orient = JPC_TSFB_HL;
252
145k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
253
145k
      tsfb->numlvls - numlvls] * tsfb->qmfb->lpenergywts[
254
145k
      tsfb->numlvls - numlvls]);
255
145k
    ++(*bands);
256
257
145k
    band = *bands;
258
145k
    band->xstart = newxstart;
259
145k
    band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
260
145k
    band->xend = newxend;
261
145k
    band->yend = JPC_FLOORDIVPOW2(yend, 1);
262
145k
    band->locxstart = locxstart;
263
145k
    band->locystart = locystart + newyend - newystart;
264
145k
    band->locxend = band->locxstart + band->xend - band->xstart;
265
145k
    band->locyend = band->locystart + band->yend - band->ystart;
266
145k
    band->orient = JPC_TSFB_LH;
267
145k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
268
145k
      tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
269
145k
      tsfb->numlvls - numlvls]);
270
145k
    ++(*bands);
271
272
145k
    band = *bands;
273
145k
    band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
274
145k
    band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
275
145k
    band->xend = JPC_FLOORDIVPOW2(xend, 1);
276
145k
    band->yend = JPC_FLOORDIVPOW2(yend, 1);
277
145k
    band->locxstart = locxstart + newxend - newxstart;
278
145k
    band->locystart = locystart + newyend - newystart;
279
145k
    band->locxend = band->locxstart + band->xend - band->xstart;
280
145k
    band->locyend = band->locystart + band->yend - band->ystart;
281
145k
    band->orient = JPC_TSFB_HH;
282
145k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
283
145k
      tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
284
145k
      tsfb->numlvls - numlvls]);
285
145k
    ++(*bands);
286
287
145k
  } else {
288
289
26.3k
    band = *bands;
290
26.3k
    band->xstart = xstart;
291
26.3k
    band->ystart = ystart;
292
26.3k
    band->xend = xend;
293
26.3k
    band->yend = yend;
294
26.3k
    band->locxstart = locxstart;
295
26.3k
    band->locystart = locystart;
296
26.3k
    band->locxend = band->locxstart + band->xend - band->xstart;
297
26.3k
    band->locyend = band->locystart + band->yend - band->ystart;
298
26.3k
    band->orient = JPC_TSFB_LL;
299
26.3k
    band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
300
26.3k
      tsfb->numlvls - numlvls - 1] * tsfb->qmfb->lpenergywts[
301
26.3k
      tsfb->numlvls - numlvls - 1]);
302
26.3k
    ++(*bands);
303
304
26.3k
  }
305
306
171k
}