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