Coverage Report

Created: 2026-03-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openjpeg/src/lib/openjp2/tgt.c
Line
Count
Source
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
80.4M
{
51
80.4M
    OPJ_INT32 nplh[32];
52
80.4M
    OPJ_INT32 nplv[32];
53
80.4M
    opj_tgt_node_t *node = 00;
54
80.4M
    opj_tgt_node_t *l_parent_node = 00;
55
80.4M
    opj_tgt_node_t *l_parent_node0 = 00;
56
80.4M
    opj_tgt_tree_t *tree = 00;
57
80.4M
    OPJ_UINT32 i;
58
80.4M
    OPJ_INT32  j, k;
59
80.4M
    OPJ_UINT32 numlvls;
60
80.4M
    OPJ_UINT32 n;
61
62
80.4M
    tree = (opj_tgt_tree_t *) opj_calloc(1, sizeof(opj_tgt_tree_t));
63
80.4M
    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
80.4M
    tree->numleafsh = numleafsh;
69
80.4M
    tree->numleafsv = numleafsv;
70
71
80.4M
    numlvls = 0;
72
80.4M
    nplh[0] = (OPJ_INT32)numleafsh;
73
80.4M
    nplv[0] = (OPJ_INT32)numleafsv;
74
80.4M
    tree->numnodes = 0;
75
86.6M
    do {
76
86.6M
        n = (OPJ_UINT32)(nplh[numlvls] * nplv[numlvls]);
77
86.6M
        nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
78
86.6M
        nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
79
86.6M
        tree->numnodes += n;
80
86.6M
        ++numlvls;
81
86.6M
    } while (n > 1);
82
83
    /* ADD */
84
80.4M
    if (tree->numnodes == 0) {
85
1.87M
        opj_free(tree);
86
1.87M
        return 00;
87
1.87M
    }
88
89
78.6M
    tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes,
90
78.6M
                  sizeof(opj_tgt_node_t));
91
78.6M
    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
78.6M
    tree->nodes_size = tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
98
99
78.6M
    node = tree->nodes;
100
78.6M
    l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv];
101
78.6M
    l_parent_node0 = l_parent_node;
102
103
84.7M
    for (i = 0; i < numlvls - 1; ++i) {
104
24.6M
        for (j = 0; j < nplv[i]; ++j) {
105
18.5M
            k = nplh[i];
106
60.0M
            while (--k >= 0) {
107
41.5M
                node->parent = l_parent_node;
108
41.5M
                ++node;
109
41.5M
                if (--k >= 0) {
110
28.0M
                    node->parent = l_parent_node;
111
28.0M
                    ++node;
112
28.0M
                }
113
41.5M
                ++l_parent_node;
114
41.5M
            }
115
18.5M
            if ((j & 1) || j == nplv[i] - 1) {
116
10.8M
                l_parent_node0 = l_parent_node;
117
10.8M
            } else {
118
7.67M
                l_parent_node = l_parent_node0;
119
7.67M
                l_parent_node0 += nplh[i];
120
7.67M
            }
121
18.5M
        }
122
6.17M
    }
123
78.6M
    node->parent = 0;
124
78.6M
    opj_tgt_reset(tree);
125
78.6M
    return tree;
126
78.6M
}
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
4.89M
{
139
4.89M
    OPJ_INT32 l_nplh[32];
140
4.89M
    OPJ_INT32 l_nplv[32];
141
4.89M
    opj_tgt_node_t *l_node = 00;
142
4.89M
    opj_tgt_node_t *l_parent_node = 00;
143
4.89M
    opj_tgt_node_t *l_parent_node0 = 00;
144
4.89M
    OPJ_UINT32 i;
145
4.89M
    OPJ_INT32 j, k;
146
4.89M
    OPJ_UINT32 l_num_levels;
147
4.89M
    OPJ_UINT32 n;
148
4.89M
    OPJ_UINT32 l_node_size;
149
150
4.89M
    if (! p_tree) {
151
0
        return 00;
152
0
    }
153
154
4.89M
    if ((p_tree->numleafsh != p_num_leafs_h) ||
155
4.84M
            (p_tree->numleafsv != p_num_leafs_v)) {
156
162k
        p_tree->numleafsh = p_num_leafs_h;
157
162k
        p_tree->numleafsv = p_num_leafs_v;
158
159
162k
        l_num_levels = 0;
160
162k
        l_nplh[0] = (OPJ_INT32)p_num_leafs_h;
161
162k
        l_nplv[0] = (OPJ_INT32)p_num_leafs_v;
162
162k
        p_tree->numnodes = 0;
163
358k
        do {
164
358k
            n = (OPJ_UINT32)(l_nplh[l_num_levels] * l_nplv[l_num_levels]);
165
358k
            l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
166
358k
            l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
167
358k
            p_tree->numnodes += n;
168
358k
            ++l_num_levels;
169
358k
        } while (n > 1);
170
171
        /* ADD */
172
162k
        if (p_tree->numnodes == 0) {
173
24.5k
            opj_tgt_destroy(p_tree);
174
24.5k
            return 00;
175
24.5k
        }
176
138k
        l_node_size = p_tree->numnodes * (OPJ_UINT32)sizeof(opj_tgt_node_t);
177
178
138k
        if (l_node_size > p_tree->nodes_size) {
179
52.9k
            opj_tgt_node_t* new_nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes,
180
52.9k
                                        l_node_size);
181
52.9k
            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
52.9k
            p_tree->nodes = new_nodes;
188
52.9k
            memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0,
189
52.9k
                   l_node_size - p_tree->nodes_size);
190
52.9k
            p_tree->nodes_size = l_node_size;
191
52.9k
        }
192
138k
        l_node = p_tree->nodes;
193
138k
        l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
194
138k
        l_parent_node0 = l_parent_node;
195
196
333k
        for (i = 0; i < l_num_levels - 1; ++i) {
197
819k
            for (j = 0; j < l_nplv[i]; ++j) {
198
624k
                k = l_nplh[i];
199
1.74M
                while (--k >= 0) {
200
1.12M
                    l_node->parent = l_parent_node;
201
1.12M
                    ++l_node;
202
1.12M
                    if (--k >= 0) {
203
601k
                        l_node->parent = l_parent_node;
204
601k
                        ++l_node;
205
601k
                    }
206
1.12M
                    ++l_parent_node;
207
1.12M
                }
208
624k
                if ((j & 1) || j == l_nplv[i] - 1) {
209
358k
                    l_parent_node0 = l_parent_node;
210
358k
                } else {
211
265k
                    l_parent_node = l_parent_node0;
212
265k
                    l_parent_node0 += l_nplh[i];
213
265k
                }
214
624k
            }
215
195k
        }
216
138k
        l_node->parent = 0;
217
138k
    }
218
4.87M
    opj_tgt_reset(p_tree);
219
220
4.87M
    return p_tree;
221
4.89M
}
222
223
void opj_tgt_destroy(opj_tgt_tree_t *p_tree)
224
80.3M
{
225
80.3M
    if (! p_tree) {
226
1.76M
        return;
227
1.76M
    }
228
229
78.6M
    if (p_tree->nodes) {
230
78.6M
        opj_free(p_tree->nodes);
231
78.6M
        p_tree->nodes = 00;
232
78.6M
    }
233
78.6M
    opj_free(p_tree);
234
78.6M
}
235
236
void opj_tgt_reset(opj_tgt_tree_t *p_tree)
237
153M
{
238
153M
    OPJ_UINT32 i;
239
153M
    opj_tgt_node_t * l_current_node = 00;;
240
241
153M
    if (! p_tree) {
242
141k
        return;
243
141k
    }
244
245
153M
    l_current_node = p_tree->nodes;
246
552M
    for (i = 0; i < p_tree->numnodes; ++i) {
247
399M
        l_current_node->value = 999;
248
399M
        l_current_node->low = 0;
249
399M
        l_current_node->known = 0;
250
399M
        ++l_current_node;
251
399M
    }
252
153M
}
253
254
void opj_tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value)
255
56.0M
{
256
56.0M
    opj_tgt_node_t *node;
257
56.0M
    node = &tree->nodes[leafno];
258
136M
    while (node && node->value > value) {
259
80.1M
        node->value = value;
260
80.1M
        node = node->parent;
261
80.1M
    }
262
56.0M
}
263
264
void opj_tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno,
265
                    OPJ_INT32 threshold)
266
56.0M
{
267
56.0M
    opj_tgt_node_t *stk[31];
268
56.0M
    opj_tgt_node_t **stkptr;
269
56.0M
    opj_tgt_node_t *node;
270
56.0M
    OPJ_INT32 low;
271
272
56.0M
    stkptr = stk;
273
56.0M
    node = &tree->nodes[leafno];
274
168M
    while (node->parent) {
275
112M
        *stkptr++ = node;
276
112M
        node = node->parent;
277
112M
    }
278
279
56.0M
    low = 0;
280
168M
    for (;;) {
281
168M
        if (low > node->low) {
282
52.8M
            node->low = low;
283
115M
        } else {
284
115M
            low = node->low;
285
115M
        }
286
287
186M
        while (low < threshold) {
288
99.4M
            if (low >= node->value) {
289
81.9M
                if (!node->known) {
290
35.5M
                    opj_bio_putbit(bio, 1);
291
35.5M
                    node->known = 1;
292
35.5M
                }
293
81.9M
                break;
294
81.9M
            }
295
17.5M
            opj_bio_putbit(bio, 0);
296
17.5M
            ++low;
297
17.5M
        }
298
299
168M
        node->low = low;
300
168M
        if (stkptr == stk) {
301
56.0M
            break;
302
56.0M
        }
303
112M
        node = *--stkptr;
304
112M
    }
305
56.0M
}
306
307
OPJ_UINT32 opj_tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree,
308
                          OPJ_UINT32 leafno, OPJ_INT32 threshold)
309
10.8M
{
310
10.8M
    opj_tgt_node_t *stk[31];
311
10.8M
    opj_tgt_node_t **stkptr;
312
10.8M
    opj_tgt_node_t *node;
313
10.8M
    OPJ_INT32 low;
314
315
10.8M
    stkptr = stk;
316
10.8M
    node = &tree->nodes[leafno];
317
76.1M
    while (node->parent) {
318
65.3M
        *stkptr++ = node;
319
65.3M
        node = node->parent;
320
65.3M
    }
321
322
10.8M
    low = 0;
323
76.1M
    for (;;) {
324
76.1M
        if (low > node->low) {
325
11.3M
            node->low = low;
326
64.8M
        } else {
327
64.8M
            low = node->low;
328
64.8M
        }
329
79.6M
        while (low < threshold && low < node->value) {
330
3.43M
            if (opj_bio_read(bio, 1)) {
331
1.34M
                node->value = low;
332
2.08M
            } else {
333
2.08M
                ++low;
334
2.08M
            }
335
3.43M
        }
336
76.1M
        node->low = low;
337
76.1M
        if (stkptr == stk) {
338
10.8M
            break;
339
10.8M
        }
340
65.3M
        node = *--stkptr;
341
65.3M
    }
342
343
10.8M
    return (node->value < threshold) ? 1 : 0;
344
10.8M
}