tgt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. /*
  33. ==========================================================
  34. Tag-tree coder interface
  35. ==========================================================
  36. */
  37. opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) {
  38. int nplh[32];
  39. int nplv[32];
  40. opj_tgt_node_t *node = NULL;
  41. opj_tgt_node_t *parentnode = NULL;
  42. opj_tgt_node_t *parentnode0 = NULL;
  43. opj_tgt_tree_t *tree = NULL;
  44. int i, j, k;
  45. int numlvls;
  46. int n;
  47. tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
  48. if(!tree) return NULL;
  49. tree->numleafsh = numleafsh;
  50. tree->numleafsv = numleafsv;
  51. numlvls = 0;
  52. nplh[0] = numleafsh;
  53. nplv[0] = numleafsv;
  54. tree->numnodes = 0;
  55. do {
  56. n = nplh[numlvls] * nplv[numlvls];
  57. nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
  58. nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
  59. tree->numnodes += n;
  60. ++numlvls;
  61. } while (n > 1);
  62. /* ADD */
  63. if (tree->numnodes == 0) {
  64. opj_free(tree);
  65. return NULL;
  66. }
  67. tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t));
  68. if(!tree->nodes) {
  69. opj_free(tree);
  70. return NULL;
  71. }
  72. node = tree->nodes;
  73. parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
  74. parentnode0 = parentnode;
  75. for (i = 0; i < numlvls - 1; ++i) {
  76. for (j = 0; j < nplv[i]; ++j) {
  77. k = nplh[i];
  78. while (--k >= 0) {
  79. node->parent = parentnode;
  80. ++node;
  81. if (--k >= 0) {
  82. node->parent = parentnode;
  83. ++node;
  84. }
  85. ++parentnode;
  86. }
  87. if ((j & 1) || j == nplv[i] - 1) {
  88. parentnode0 = parentnode;
  89. } else {
  90. parentnode = parentnode0;
  91. parentnode0 += nplh[i];
  92. }
  93. }
  94. }
  95. node->parent = 0;
  96. tgt_reset(tree);
  97. return tree;
  98. }
  99. void tgt_destroy(opj_tgt_tree_t *tree) {
  100. opj_free(tree->nodes);
  101. opj_free(tree);
  102. }
  103. void tgt_reset(opj_tgt_tree_t *tree) {
  104. int i;
  105. if (NULL == tree)
  106. return;
  107. for (i = 0; i < tree->numnodes; i++) {
  108. tree->nodes[i].value = 999;
  109. tree->nodes[i].low = 0;
  110. tree->nodes[i].known = 0;
  111. }
  112. }
  113. void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
  114. opj_tgt_node_t *node;
  115. node = &tree->nodes[leafno];
  116. while (node && node->value > value) {
  117. node->value = value;
  118. node = node->parent;
  119. }
  120. }
  121. void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
  122. opj_tgt_node_t *stk[31];
  123. opj_tgt_node_t **stkptr;
  124. opj_tgt_node_t *node;
  125. int low;
  126. stkptr = stk;
  127. node = &tree->nodes[leafno];
  128. while (node->parent) {
  129. *stkptr++ = node;
  130. node = node->parent;
  131. }
  132. low = 0;
  133. for (;;) {
  134. if (low > node->low) {
  135. node->low = low;
  136. } else {
  137. low = node->low;
  138. }
  139. while (low < threshold) {
  140. if (low >= node->value) {
  141. if (!node->known) {
  142. bio_write(bio, 1, 1);
  143. node->known = 1;
  144. }
  145. break;
  146. }
  147. bio_write(bio, 0, 1);
  148. ++low;
  149. }
  150. node->low = low;
  151. if (stkptr == stk)
  152. break;
  153. node = *--stkptr;
  154. }
  155. }
  156. int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
  157. opj_tgt_node_t *stk[31];
  158. opj_tgt_node_t **stkptr;
  159. opj_tgt_node_t *node;
  160. int low;
  161. stkptr = stk;
  162. node = &tree->nodes[leafno];
  163. while (node->parent) {
  164. *stkptr++ = node;
  165. node = node->parent;
  166. }
  167. low = 0;
  168. for (;;) {
  169. if (low > node->low) {
  170. node->low = low;
  171. } else {
  172. low = node->low;
  173. }
  174. while (low < threshold && low < node->value) {
  175. if (bio_read(bio, 1)) {
  176. node->value = low;
  177. } else {
  178. ++low;
  179. }
  180. }
  181. node->low = low;
  182. if (stkptr == stk) {
  183. break;
  184. }
  185. node = *--stkptr;
  186. }
  187. return (node->value < threshold) ? 1 : 0;
  188. }