jpc_tsfb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Tree-Structured Filter Bank (TSFB) Library
  64. *
  65. * $Id: jpc_tsfb.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <assert.h>
  71. #include "jasper/jas_malloc.h"
  72. #include "jasper/jas_seq.h"
  73. #include "jpc_tsfb.h"
  74. #include "jpc_cod.h"
  75. #include "jpc_cs.h"
  76. #include "jpc_util.h"
  77. #include "jpc_math.h"
  78. void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
  79. int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
  80. int numlvls);
  81. /******************************************************************************\
  82. *
  83. \******************************************************************************/
  84. jpc_tsfb_t *jpc_cod_gettsfb(int qmfbid, int numlvls)
  85. {
  86. jpc_tsfb_t *tsfb;
  87. if (!(tsfb = malloc(sizeof(jpc_tsfb_t))))
  88. return 0;
  89. if (numlvls > 0) {
  90. switch (qmfbid) {
  91. case JPC_COX_INS:
  92. tsfb->qmfb = &jpc_ns_qmfb2d;
  93. break;
  94. default:
  95. case JPC_COX_RFT:
  96. tsfb->qmfb = &jpc_ft_qmfb2d;
  97. break;
  98. }
  99. } else {
  100. tsfb->qmfb = 0;
  101. }
  102. tsfb->numlvls = numlvls;
  103. return tsfb;
  104. }
  105. void jpc_tsfb_destroy(jpc_tsfb_t *tsfb)
  106. {
  107. free(tsfb);
  108. }
  109. int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
  110. {
  111. return (tsfb->numlvls > 0) ? jpc_tsfb_analyze2(tsfb, jas_seq2d_getref(a,
  112. jas_seq2d_xstart(a), jas_seq2d_ystart(a)), jas_seq2d_xstart(a),
  113. jas_seq2d_ystart(a), jas_seq2d_width(a),
  114. jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0;
  115. }
  116. int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart,
  117. int width, int height, int stride, int numlvls)
  118. {
  119. if (width > 0 && height > 0) {
  120. if ((*tsfb->qmfb->analyze)(a, xstart, ystart, width, height, stride))
  121. return -1;
  122. if (numlvls > 0) {
  123. if (jpc_tsfb_analyze2(tsfb, a, JPC_CEILDIVPOW2(xstart,
  124. 1), JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(
  125. xstart + width, 1) - JPC_CEILDIVPOW2(xstart, 1),
  126. JPC_CEILDIVPOW2(ystart + height, 1) -
  127. JPC_CEILDIVPOW2(ystart, 1), stride, numlvls - 1)) {
  128. return -1;
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
  135. {
  136. return (tsfb->numlvls > 0) ? jpc_tsfb_synthesize2(tsfb,
  137. jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
  138. jas_seq2d_xstart(a), jas_seq2d_ystart(a), jas_seq2d_width(a),
  139. jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0;
  140. }
  141. int jpc_tsfb_synthesize2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart,
  142. int width, int height, int stride, int numlvls)
  143. {
  144. if (numlvls > 0) {
  145. if (jpc_tsfb_synthesize2(tsfb, a, JPC_CEILDIVPOW2(xstart, 1),
  146. JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(xstart + width,
  147. 1) - JPC_CEILDIVPOW2(xstart, 1), JPC_CEILDIVPOW2(ystart +
  148. height, 1) - JPC_CEILDIVPOW2(ystart, 1), stride, numlvls -
  149. 1)) {
  150. return -1;
  151. }
  152. }
  153. if (width > 0 && height > 0) {
  154. if ((*tsfb->qmfb->synthesize)(a, xstart, ystart, width, height, stride)) {
  155. return -1;
  156. }
  157. }
  158. return 0;
  159. }
  160. int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart,
  161. uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend,
  162. jpc_tsfb_band_t *bands)
  163. {
  164. jpc_tsfb_band_t *band;
  165. band = bands;
  166. if (tsfb->numlvls > 0) {
  167. jpc_tsfb_getbands2(tsfb, xstart, ystart, xstart, ystart, xend, yend,
  168. &band, tsfb->numlvls);
  169. } else {
  170. band->xstart = xstart;
  171. band->ystart = ystart;
  172. band->xend = xend;
  173. band->yend = yend;
  174. band->locxstart = xstart;
  175. band->locystart = ystart;
  176. band->locxend = band->locxstart + band->xend - band->xstart;
  177. band->locyend = band->locystart + band->yend - band->ystart;
  178. band->orient = JPC_TSFB_LL;
  179. band->synenergywt = JPC_FIX_ONE;
  180. ++band;
  181. }
  182. return band - bands;
  183. }
  184. void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart,
  185. int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands,
  186. int numlvls)
  187. {
  188. int newxstart;
  189. int newystart;
  190. int newxend;
  191. int newyend;
  192. jpc_tsfb_band_t *band;
  193. newxstart = JPC_CEILDIVPOW2(xstart, 1);
  194. newystart = JPC_CEILDIVPOW2(ystart, 1);
  195. newxend = JPC_CEILDIVPOW2(xend, 1);
  196. newyend = JPC_CEILDIVPOW2(yend, 1);
  197. if (numlvls > 0) {
  198. jpc_tsfb_getbands2(tsfb, locxstart, locystart, newxstart, newystart,
  199. newxend, newyend, bands, numlvls - 1);
  200. band = *bands;
  201. band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
  202. band->ystart = newystart;
  203. band->xend = JPC_FLOORDIVPOW2(xend, 1);
  204. band->yend = newyend;
  205. band->locxstart = locxstart + newxend - newxstart;
  206. band->locystart = locystart;
  207. band->locxend = band->locxstart + band->xend - band->xstart;
  208. band->locyend = band->locystart + band->yend - band->ystart;
  209. band->orient = JPC_TSFB_HL;
  210. band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
  211. tsfb->numlvls - numlvls] * tsfb->qmfb->lpenergywts[
  212. tsfb->numlvls - numlvls]);
  213. ++(*bands);
  214. band = *bands;
  215. band->xstart = newxstart;
  216. band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
  217. band->xend = newxend;
  218. band->yend = JPC_FLOORDIVPOW2(yend, 1);
  219. band->locxstart = locxstart;
  220. band->locystart = locystart + newyend - newystart;
  221. band->locxend = band->locxstart + band->xend - band->xstart;
  222. band->locyend = band->locystart + band->yend - band->ystart;
  223. band->orient = JPC_TSFB_LH;
  224. band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
  225. tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
  226. tsfb->numlvls - numlvls]);
  227. ++(*bands);
  228. band = *bands;
  229. band->xstart = JPC_FLOORDIVPOW2(xstart, 1);
  230. band->ystart = JPC_FLOORDIVPOW2(ystart, 1);
  231. band->xend = JPC_FLOORDIVPOW2(xend, 1);
  232. band->yend = JPC_FLOORDIVPOW2(yend, 1);
  233. band->locxstart = locxstart + newxend - newxstart;
  234. band->locystart = locystart + newyend - newystart;
  235. band->locxend = band->locxstart + band->xend - band->xstart;
  236. band->locyend = band->locystart + band->yend - band->ystart;
  237. band->orient = JPC_TSFB_HH;
  238. band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[
  239. tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[
  240. tsfb->numlvls - numlvls]);
  241. ++(*bands);
  242. } else {
  243. band = *bands;
  244. band->xstart = xstart;
  245. band->ystart = ystart;
  246. band->xend = xend;
  247. band->yend = yend;
  248. band->locxstart = locxstart;
  249. band->locystart = locystart;
  250. band->locxend = band->locxstart + band->xend - band->xstart;
  251. band->locyend = band->locystart + band->yend - band->ystart;
  252. band->orient = JPC_TSFB_LL;
  253. band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[
  254. tsfb->numlvls - numlvls - 1] * tsfb->qmfb->lpenergywts[
  255. tsfb->numlvls - numlvls - 1]);
  256. ++(*bands);
  257. }
  258. }