Coverage Report

Created: 2025-07-23 07:06

/src/opencv/3rdparty/openjpeg/openjp2/tgt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The copyright in this software is being made available under the 2-clauses
3
 * BSD License, included below. This software may be subject to other third
4
 * party and contributor rights, including patent rights, and no such rights
5
 * are granted under this license.
6
 *
7
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8
 * Copyright (c) 2002-2014, Professor Benoit Macq
9
 * Copyright (c) 2001-2003, David Janssens
10
 * Copyright (c) 2002-2003, Yannick Verschueren
11
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12
 * Copyright (c) 2003-2014, Antonin Descampe
13
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14
 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
15
 * Copyright (c) 2012, CS Systemes d'Information, France
16
 * All rights reserved.
17
 *
18
 * Redistribution and use in source and binary forms, with or without
19
 * modification, are permitted provided that the following conditions
20
 * are met:
21
 * 1. Redistributions of source code must retain the above copyright
22
 *    notice, this list of conditions and the following disclaimer.
23
 * 2. Redistributions in binary form must reproduce the above copyright
24
 *    notice, this list of conditions and the following disclaimer in the
25
 *    documentation and/or other materials provided with the distribution.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
 * POSSIBILITY OF SUCH DAMAGE.
38
 */
39
40
#include "opj_includes.h"
41
42
/*
43
==========================================================
44
   Tag-tree coder interface
45
==========================================================
46
*/
47
48
opj_tgt_tree_t *opj_tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv,
49
                               opj_event_mgr_t *p_manager)
50
436k
{
51
436k
    OPJ_INT32 nplh[32];
52
436k
    OPJ_INT32 nplv[32];
53
436k
    opj_tgt_node_t *node = 00;
54
436k
    opj_tgt_node_t *l_parent_node = 00;
55
436k
    opj_tgt_node_t *l_parent_node0 = 00;
56
436k
    opj_tgt_tree_t *tree = 00;
57
436k
    OPJ_UINT32 i;
58
436k
    OPJ_INT32  j, k;
59
436k
    OPJ_UINT32 numlvls;
60
436k
    OPJ_UINT32 n;
61
62
436k
    tree = (opj_tgt_tree_t *) opj_calloc(1, sizeof(opj_tgt_tree_t));
63
436k
    if (!tree) {
64
0
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tag-tree\n");
65
0
        return 00;
66
0
    }
67
68
436k
    tree->numleafsh = numleafsh;
69
436k
    tree->numleafsv = numleafsv;
70
71
436k
    numlvls = 0;
72
436k
    nplh[0] = (OPJ_INT32)numleafsh;
73
436k
    nplv[0] = (OPJ_INT32)numleafsv;
74
436k
    tree->numnodes = 0;
75
536k
    do {
76
536k
        n = (OPJ_UINT32)(nplh[numlvls] * nplv[numlvls]);
77
536k
        nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
78
536k
        nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
79
536k
        tree->numnodes += n;
80
536k
        ++numlvls;
81
536k
    } while (n > 1);
82
83
    /* ADD */
84
436k
    if (tree->numnodes == 0) {
85
518
        opj_free(tree);
86
518
        return 00;
87
518
    }
88
89
436k
    tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes,
90
436k
                  sizeof(opj_tgt_node_t));
91
436k
    if (!tree->nodes) {
92
0
        opj_event_msg(p_manager, EVT_ERROR,
93
0
                      "Not enough memory to create Tag-tree nodes\n");
94
0
        opj_free(tree);
95
0
        return 00;
96
0
    }
97
436k
    tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
98
99
436k
    node = tree->nodes;
100
436k
    l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv];
101
436k
    l_parent_node0 = l_parent_node;
102
103
535k
    for (i = 0; i < numlvls - 1; ++i) {
104
1.17M
        for (j = 0; j < nplv[i]; ++j) {
105
1.07M
            k = nplh[i];
106
3.88M
            while (--k >= 0) {
107
2.81M
                node->parent = l_parent_node;
108
2.81M
                ++node;
109
2.81M
                if (--k >= 0) {
110
1.80M
                    node->parent = l_parent_node;
111
1.80M
                    ++node;
112
1.80M
                }
113
2.81M
                ++l_parent_node;
114
2.81M
            }
115
1.07M
            if ((j & 1) || j == nplv[i] - 1) {
116
542k
                l_parent_node0 = l_parent_node;
117
542k
            } else {
118
533k
                l_parent_node = l_parent_node0;
119
533k
                l_parent_node0 += nplh[i];
120
533k
            }
121
1.07M
        }
122
99.3k
    }
123
436k
    node->parent = 0;
124
436k
    opj_tgt_reset(tree);
125
436k
    return tree;
126
436k
}
127
128
/**
129
 * Reinitialises a tag-tree from an existing one.
130
 *
131
 * @param       p_tree                          the tree to reinitialize.
132
 * @param       p_num_leafs_h           the width of the array of leafs of the tree
133
 * @param       p_num_leafs_v           the height of the array of leafs of the tree
134
 * @return      a new tag-tree if successful, NULL otherwise
135
*/
136
opj_tgt_tree_t *opj_tgt_init(opj_tgt_tree_t * p_tree, OPJ_UINT32 p_num_leafs_h,
137
                             OPJ_UINT32 p_num_leafs_v, opj_event_mgr_t *p_manager)
138
12.8k
{
139
12.8k
    OPJ_INT32 l_nplh[32];
140
12.8k
    OPJ_INT32 l_nplv[32];
141
12.8k
    opj_tgt_node_t *l_node = 00;
142
12.8k
    opj_tgt_node_t *l_parent_node = 00;
143
12.8k
    opj_tgt_node_t *l_parent_node0 = 00;
144
12.8k
    OPJ_UINT32 i;
145
12.8k
    OPJ_INT32 j, k;
146
12.8k
    OPJ_UINT32 l_num_levels;
147
12.8k
    OPJ_UINT32 n;
148
12.8k
    OPJ_UINT32 l_node_size;
149
150
12.8k
    if (! p_tree) {
151
0
        return 00;
152
0
    }
153
154
12.8k
    if ((p_tree->numleafsh != p_num_leafs_h) ||
155
12.8k
            (p_tree->numleafsv != p_num_leafs_v)) {
156
1.99k
        p_tree->numleafsh = p_num_leafs_h;
157
1.99k
        p_tree->numleafsv = p_num_leafs_v;
158
159
1.99k
        l_num_levels = 0;
160
1.99k
        l_nplh[0] = (OPJ_INT32)p_num_leafs_h;
161
1.99k
        l_nplv[0] = (OPJ_INT32)p_num_leafs_v;
162
1.99k
        p_tree->numnodes = 0;
163
5.02k
        do {
164
5.02k
            n = (OPJ_UINT32)(l_nplh[l_num_levels] * l_nplv[l_num_levels]);
165
5.02k
            l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
166
5.02k
            l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
167
5.02k
            p_tree->numnodes += n;
168
5.02k
            ++l_num_levels;
169
5.02k
        } while (n > 1);
170
171
        /* ADD */
172
1.99k
        if (p_tree->numnodes == 0) {
173
0
            opj_tgt_destroy(p_tree);
174
0
            return 00;
175
0
        }
176
1.99k
        l_node_size = p_tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
177
178
1.99k
        if (l_node_size > p_tree->nodes_size) {
179
80
            opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes,
180
80
                                        l_node_size);
181
80
            if (! new_nodes) {
182
0
                opj_event_msg(p_manager, EVT_ERROR,
183
0
                              "Not enough memory to reinitialize the tag tree\n");
184
0
                opj_tgt_destroy(p_tree);
185
0
                return 00;
186
0
            }
187
80
            p_tree->nodes = new_nodes;
188
80
            memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0,
189
80
                   l_node_size - p_tree->nodes_size);
190
80
            p_tree->nodes_size = l_node_size;
191
80
        }
192
1.99k
        l_node = p_tree->nodes;
193
1.99k
        l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
194
1.99k
        l_parent_node0 = l_parent_node;
195
196
5.02k
        for (i = 0; i < l_num_levels - 1; ++i) {
197
13.6k
            for (j = 0; j < l_nplv[i]; ++j) {
198
10.5k
                k = l_nplh[i];
199
22.2k
                while (--k >= 0) {
200
11.6k
                    l_node->parent = l_parent_node;
201
11.6k
                    ++l_node;
202
11.6k
                    if (--k >= 0) {
203
5.48k
                        l_node->parent = l_parent_node;
204
5.48k
                        ++l_node;
205
5.48k
                    }
206
11.6k
                    ++l_parent_node;
207
11.6k
                }
208
10.5k
                if ((j & 1) || j == l_nplv[i] - 1) {
209
6.17k
                    l_parent_node0 = l_parent_node;
210
6.17k
                } else {
211
4.41k
                    l_parent_node = l_parent_node0;
212
4.41k
                    l_parent_node0 += l_nplh[i];
213
4.41k
                }
214
10.5k
            }
215
3.03k
        }
216
1.99k
        l_node->parent = 0;
217
1.99k
    }
218
12.8k
    opj_tgt_reset(p_tree);
219
220
12.8k
    return p_tree;
221
12.8k
}
222
223
void opj_tgt_destroy(opj_tgt_tree_t *p_tree)
224
436k
{
225
436k
    if (! p_tree) {
226
458
        return;
227
458
    }
228
229
436k
    if (p_tree->nodes) {
230
436k
        opj_free(p_tree->nodes);
231
436k
        p_tree->nodes = 00;
232
436k
    }
233
436k
    opj_free(p_tree);
234
436k
}
235
236
void opj_tgt_reset(opj_tgt_tree_t *p_tree)
237
897k
{
238
897k
    OPJ_UINT32 i;
239
897k
    opj_tgt_node_t * l_current_node = 00;;
240
241
897k
    if (! p_tree) {
242
0
        return;
243
0
    }
244
245
897k
    l_current_node = p_tree->nodes;
246
11.1M
    for (i = 0; i < p_tree->numnodes; ++i) {
247
10.2M
        l_current_node->value = 999;
248
10.2M
        l_current_node->low = 0;
249
10.2M
        l_current_node->known = 0;
250
10.2M
        ++l_current_node;
251
10.2M
    }
252
897k
}
253
254
void opj_tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value)
255
0
{
256
0
    opj_tgt_node_t *node;
257
0
    node = &tree->nodes[leafno];
258
0
    while (node && node->value > value) {
259
0
        node->value = value;
260
0
        node = node->parent;
261
0
    }
262
0
}
263
264
void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno,
265
                    OPJ_INT32 threshold)
266
0
{
267
0
    opj_tgt_node_t *stk[31];
268
0
    opj_tgt_node_t **stkptr;
269
0
    opj_tgt_node_t *node;
270
0
    OPJ_INT32 low;
271
272
0
    stkptr = stk;
273
0
    node = &tree->nodes[leafno];
274
0
    while (node->parent) {
275
0
        *stkptr++ = node;
276
0
        node = node->parent;
277
0
    }
278
279
0
    low = 0;
280
0
    for (;;) {
281
0
        if (low > node->low) {
282
0
            node->low = low;
283
0
        } else {
284
0
            low = node->low;
285
0
        }
286
287
0
        while (low < threshold) {
288
0
            if (low >= node->value) {
289
0
                if (!node->known) {
290
0
                    opj_bio_putbit(bio, 1);
291
0
                    node->known = 1;
292
0
                }
293
0
                break;
294
0
            }
295
0
            opj_bio_putbit(bio, 0);
296
0
            ++low;
297
0
        }
298
299
0
        node->low = low;
300
0
        if (stkptr == stk) {
301
0
            break;
302
0
        }
303
0
        node = *--stkptr;
304
0
    }
305
0
}
306
307
OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree,
308
                          OPJ_UINT32 leafno, OPJ_INT32 threshold)
309
6.12M
{
310
6.12M
    opj_tgt_node_t *stk[31];
311
6.12M
    opj_tgt_node_t **stkptr;
312
6.12M
    opj_tgt_node_t *node;
313
6.12M
    OPJ_INT32 low;
314
315
6.12M
    stkptr = stk;
316
6.12M
    node = &tree->nodes[leafno];
317
58.1M
    while (node->parent) {
318
52.0M
        *stkptr++ = node;
319
52.0M
        node = node->parent;
320
52.0M
    }
321
322
6.12M
    low = 0;
323
58.1M
    for (;;) {
324
58.1M
        if (low > node->low) {
325
6.17M
            node->low = low;
326
51.9M
        } else {
327
51.9M
            low = node->low;
328
51.9M
        }
329
59.6M
        while (low < threshold && low < node->value) {
330
1.51M
            if (opj_bio_read(bio, 1)) {
331
842k
                node->value = low;
332
842k
            } else {
333
673k
                ++low;
334
673k
            }
335
1.51M
        }
336
58.1M
        node->low = low;
337
58.1M
        if (stkptr == stk) {
338
6.12M
            break;
339
6.12M
        }
340
52.0M
        node = *--stkptr;
341
52.0M
    }
342
343
6.12M
    return (node->value < threshold) ? 1 : 0;
344
6.12M
}