| 1 |
/* |
| 2 |
* Copyright (c) 1999-2000 Image Power, Inc. and the University of |
| 3 |
* British Columbia. |
| 4 |
* Copyright (c) 2001-2002 Michael David Adams. |
| 5 |
* All rights reserved. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* __START_OF_JASPER_LICENSE__ |
| 9 |
* |
| 10 |
* JasPer Software License |
| 11 |
* |
| 12 |
* IMAGE POWER JPEG-2000 PUBLIC LICENSE |
| 13 |
* ************************************ |
| 14 |
* |
| 15 |
* GRANT: |
| 16 |
* |
| 17 |
* Permission is hereby granted, free of charge, to any person (the "User") |
| 18 |
* obtaining a copy of this software and associated documentation, to deal |
| 19 |
* in the JasPer Software without restriction, including without limitation |
| 20 |
* the right to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 |
* and/or sell copies of the JasPer Software (in source and binary forms), |
| 22 |
* and to permit persons to whom the JasPer Software is furnished to do so, |
| 23 |
* provided further that the License Conditions below are met. |
| 24 |
* |
| 25 |
* License Conditions |
| 26 |
* ****************** |
| 27 |
* |
| 28 |
* A. Redistributions of source code must retain the above copyright notice, |
| 29 |
* and this list of conditions, and the following disclaimer. |
| 30 |
* |
| 31 |
* B. Redistributions in binary form must reproduce the above copyright |
| 32 |
* notice, and this list of conditions, and the following disclaimer in |
| 33 |
* the documentation and/or other materials provided with the distribution. |
| 34 |
* |
| 35 |
* C. Neither the name of Image Power, Inc. nor any other contributor |
| 36 |
* (including, but not limited to, the University of British Columbia and |
| 37 |
* Michael David Adams) may be used to endorse or promote products derived |
| 38 |
* from this software without specific prior written permission. |
| 39 |
* |
| 40 |
* D. User agrees that it shall not commence any action against Image Power, |
| 41 |
* Inc., the University of British Columbia, Michael David Adams, or any |
| 42 |
* other contributors (collectively "Licensors") for infringement of any |
| 43 |
* intellectual property rights ("IPR") held by the User in respect of any |
| 44 |
* technology that User owns or has a right to license or sublicense and |
| 45 |
* which is an element required in order to claim compliance with ISO/IEC |
| 46 |
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property |
| 47 |
* rights worldwide arising under statutory or common law, and whether |
| 48 |
* or not perfected, including, without limitation, all (i) patents and |
| 49 |
* patent applications owned or licensable by User; (ii) rights associated |
| 50 |
* with works of authorship including copyrights, copyright applications, |
| 51 |
* copyright registrations, mask work rights, mask work applications, |
| 52 |
* mask work registrations; (iii) rights relating to the protection of |
| 53 |
* trade secrets and confidential information; (iv) any right analogous |
| 54 |
* to those set forth in subsections (i), (ii), or (iii) and any other |
| 55 |
* proprietary rights relating to intangible property (other than trademark, |
| 56 |
* trade dress, or service mark rights); and (v) divisions, continuations, |
| 57 |
* renewals, reissues and extensions of the foregoing (as and to the extent |
| 58 |
* applicable) now existing, hereafter filed, issued or acquired. |
| 59 |
* |
| 60 |
* E. If User commences an infringement action against any Licensor(s) then |
| 61 |
* such Licensor(s) shall have the right to terminate User's license and |
| 62 |
* all sublicenses that have been granted hereunder by User to other parties. |
| 63 |
* |
| 64 |
* F. This software is for use only in hardware or software products that |
| 65 |
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license |
| 66 |
* or right to this Software is granted for products that do not comply |
| 67 |
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased |
| 68 |
* from the ISO. |
| 69 |
* |
| 70 |
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. |
| 71 |
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
| 72 |
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND |
| 73 |
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY |
| 74 |
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, |
| 75 |
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, |
| 76 |
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING |
| 77 |
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE |
| 78 |
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING |
| 79 |
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. |
| 80 |
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE |
| 81 |
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE |
| 82 |
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY |
| 83 |
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY |
| 84 |
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, |
| 85 |
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, |
| 86 |
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE |
| 87 |
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., |
| 88 |
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE |
| 89 |
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO |
| 90 |
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR |
| 91 |
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, |
| 92 |
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR |
| 93 |
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF |
| 94 |
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY |
| 95 |
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT |
| 96 |
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR |
| 97 |
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING |
| 98 |
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, |
| 99 |
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT |
| 100 |
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE |
| 101 |
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY |
| 102 |
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE |
| 103 |
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS |
| 104 |
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT |
| 105 |
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING |
| 106 |
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS |
| 107 |
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE |
| 108 |
* NOTICE SPECIFIED IN THIS SECTION. |
| 109 |
* |
| 110 |
* __END_OF_JASPER_LICENSE__ |
| 111 |
*/ |
| 112 |
|
| 113 |
/* |
| 114 |
* Tree-Structured Filter Bank (TSFB) Library |
| 115 |
* |
| 116 |
* $Id$ |
| 117 |
*/ |
| 118 |
|
| 119 |
/******************************************************************************\ |
| 120 |
* Includes. |
| 121 |
\******************************************************************************/ |
| 122 |
|
| 123 |
#include <assert.h> |
| 124 |
|
| 125 |
#include "jasper/jas_malloc.h" |
| 126 |
#include "jasper/jas_seq.h" |
| 127 |
|
| 128 |
#include "jpc_tsfb.h" |
| 129 |
#include "jpc_cod.h" |
| 130 |
#include "jpc_cs.h" |
| 131 |
#include "jpc_util.h" |
| 132 |
|
| 133 |
/******************************************************************************\ |
| 134 |
* |
| 135 |
\******************************************************************************/ |
| 136 |
|
| 137 |
#define bandnotovind(tsfbnode, x) ((x) / (tsfbnode)->numhchans) |
| 138 |
#define bandnotohind(tsfbnode, x) ((x) % (tsfbnode)->numhchans) |
| 139 |
|
| 140 |
static jpc_tsfb_t *jpc_tsfb_create(void); |
| 141 |
static jpc_tsfbnode_t *jpc_tsfbnode_create(void); |
| 142 |
static void jpc_tsfbnode_destroy(jpc_tsfbnode_t *node); |
| 143 |
static void jpc_tsfbnode_synthesize(jpc_tsfbnode_t *node, int flags, jas_seq2d_t *x); |
| 144 |
static void jpc_tsfbnode_analyze(jpc_tsfbnode_t *node, int flags, jas_seq2d_t *x); |
| 145 |
static void qmfb2d_getbands(jpc_qmfb1d_t *hqmfb, jpc_qmfb1d_t *vqmfb, |
| 146 |
uint_fast32_t xstart, uint_fast32_t ystart, uint_fast32_t xend, |
| 147 |
uint_fast32_t yend, int maxbands, int *numbandsptr, jpc_tsfbnodeband_t *bands); |
| 148 |
static void jpc_tsfbnode_getbandstree(jpc_tsfbnode_t *node, uint_fast32_t posxstart, |
| 149 |
uint_fast32_t posystart, uint_fast32_t xstart, uint_fast32_t ystart, |
| 150 |
uint_fast32_t xend, uint_fast32_t yend, jpc_tsfb_band_t **bands); |
| 151 |
static int jpc_tsfbnode_findchild(jpc_tsfbnode_t *parnode, jpc_tsfbnode_t *cldnode); |
| 152 |
static int jpc_tsfbnode_getequivfilters(jpc_tsfbnode_t *tsfbnode, int cldind, |
| 153 |
int width, int height, jas_seq_t **vfilter, jas_seq_t **hfilter); |
| 154 |
|
| 155 |
/******************************************************************************\ |
| 156 |
* |
| 157 |
\******************************************************************************/ |
| 158 |
|
| 159 |
jpc_tsfb_t *jpc_tsfb_wavelet(jpc_qmfb1d_t *hqmfb, jpc_qmfb1d_t *vqmfb, int numdlvls) |
| 160 |
{ |
| 161 |
jpc_tsfb_t *tsfb; |
| 162 |
int dlvlno; |
| 163 |
jpc_tsfbnode_t *curnode; |
| 164 |
jpc_tsfbnode_t *prevnode; |
| 165 |
int childno; |
| 166 |
if (!(tsfb = jpc_tsfb_create())) { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
prevnode = 0; |
| 170 |
for (dlvlno = 0; dlvlno < numdlvls; ++dlvlno) { |
| 171 |
if (!(curnode = jpc_tsfbnode_create())) { |
| 172 |
jpc_tsfb_destroy(tsfb); |
| 173 |
return 0; |
| 174 |
} |
| 175 |
if (prevnode) { |
| 176 |
prevnode->children[0] = curnode; |
| 177 |
++prevnode->numchildren; |
| 178 |
curnode->parent = prevnode; |
| 179 |
} else { |
| 180 |
tsfb->root = curnode; |
| 181 |
curnode->parent = 0; |
| 182 |
} |
| 183 |
if (hqmfb) { |
| 184 |
curnode->numhchans = jpc_qmfb1d_getnumchans(hqmfb); |
| 185 |
if (!(curnode->hqmfb = jpc_qmfb1d_copy(hqmfb))) { |
| 186 |
jpc_tsfb_destroy(tsfb); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
} else { |
| 190 |
curnode->hqmfb = 0; |
| 191 |
curnode->numhchans = 1; |
| 192 |
} |
| 193 |
if (vqmfb) { |
| 194 |
curnode->numvchans = jpc_qmfb1d_getnumchans(vqmfb); |
| 195 |
if (!(curnode->vqmfb = jpc_qmfb1d_copy(vqmfb))) { |
| 196 |
jpc_tsfb_destroy(tsfb); |
| 197 |
return 0; |
| 198 |
} |
| 199 |
} else { |
| 200 |
curnode->vqmfb = 0; |
| 201 |
curnode->numvchans = 1; |
| 202 |
} |
| 203 |
curnode->maxchildren = curnode->numhchans * curnode->numvchans; |
| 204 |
for (childno = 0; childno < curnode->maxchildren; |
| 205 |
++childno) { |
| 206 |
curnode->children[childno] = 0; |
| 207 |
} |
| 208 |
prevnode = curnode; |
| 209 |
} |
| 210 |
return tsfb; |
| 211 |
} |
| 212 |
|
| 213 |
static jpc_tsfb_t *jpc_tsfb_create() |
| 214 |
{ |
| 215 |
jpc_tsfb_t *tsfb; |
| 216 |
if (!(tsfb = jas_malloc(sizeof(jpc_tsfb_t)))) { |
| 217 |
return 0; |
| 218 |
} |
| 219 |
tsfb->root = 0; |
| 220 |
return tsfb; |
| 221 |
} |
| 222 |
|
| 223 |
void jpc_tsfb_destroy(jpc_tsfb_t *tsfb) |
| 224 |
{ |
| 225 |
if (tsfb->root) { |
| 226 |
jpc_tsfbnode_destroy(tsfb->root); |
| 227 |
} |
| 228 |
jas_free(tsfb); |
| 229 |
} |
| 230 |
|
| 231 |
/******************************************************************************\ |
| 232 |
* |
| 233 |
\******************************************************************************/ |
| 234 |
|
| 235 |
void jpc_tsfb_analyze(jpc_tsfb_t *tsfb, int flags, jas_seq2d_t *x) |
| 236 |
{ |
| 237 |
if (tsfb->root) { |
| 238 |
jpc_tsfbnode_analyze(tsfb->root, flags, x); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
static void jpc_tsfbnode_analyze(jpc_tsfbnode_t *node, int flags, jas_seq2d_t *x) |
| 243 |
{ |
| 244 |
jpc_tsfbnodeband_t nodebands[JPC_TSFB_MAXBANDSPERNODE]; |
| 245 |
int numbands; |
| 246 |
jas_seq2d_t *y; |
| 247 |
int bandno; |
| 248 |
jpc_tsfbnodeband_t *band; |
| 249 |
|
| 250 |
if (node->vqmfb) { |
| 251 |
jpc_qmfb1d_analyze(node->vqmfb, flags | JPC_QMFB1D_VERT, x); |
| 252 |
} |
| 253 |
if (node->hqmfb) { |
| 254 |
jpc_qmfb1d_analyze(node->hqmfb, flags, x); |
| 255 |
} |
| 256 |
if (node->numchildren > 0) { |
| 257 |
qmfb2d_getbands(node->hqmfb, node->vqmfb, jas_seq2d_xstart(x), |
| 258 |
jas_seq2d_ystart(x), jas_seq2d_xend(x), jas_seq2d_yend(x), |
| 259 |
JPC_TSFB_MAXBANDSPERNODE, &numbands, nodebands); |
| 260 |
y = jas_seq2d_create(0, 0, 0, 0); |
| 261 |
assert(y); |
| 262 |
for (bandno = 0, band = nodebands; bandno < numbands; ++bandno, ++band) { |
| 263 |
if (node->children[bandno]) { |
| 264 |
if (band->xstart != band->xend && band->ystart != band->yend) { |
| 265 |
jas_seq2d_bindsub(y, x, band->locxstart, band->locystart, |
| 266 |
band->locxend, band->locyend); |
| 267 |
jas_seq2d_setshift(y, band->xstart, band->ystart); |
| 268 |
jpc_tsfbnode_analyze(node->children[bandno], flags, y); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
jas_matrix_destroy(y); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
void jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, int flags, jas_seq2d_t *x) |
| 277 |
{ |
| 278 |
if (tsfb->root) { |
| 279 |
jpc_tsfbnode_synthesize(tsfb->root, flags, x); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static void jpc_tsfbnode_synthesize(jpc_tsfbnode_t *node, int flags, jas_seq2d_t *x) |
| 284 |
{ |
| 285 |
jpc_tsfbnodeband_t nodebands[JPC_TSFB_MAXBANDSPERNODE]; |
| 286 |
int numbands; |
| 287 |
jas_seq2d_t *y; |
| 288 |
int bandno; |
| 289 |
jpc_tsfbnodeband_t *band; |
| 290 |
|
| 291 |
if (node->numchildren > 0) { |
| 292 |
qmfb2d_getbands(node->hqmfb, node->vqmfb, jas_seq2d_xstart(x), |
| 293 |
jas_seq2d_ystart(x), jas_seq2d_xend(x), jas_seq2d_yend(x), |
| 294 |
JPC_TSFB_MAXBANDSPERNODE, &numbands, nodebands); |
| 295 |
y = jas_seq2d_create(0, 0, 0, 0); |
| 296 |
for (bandno = 0, band = nodebands; bandno < numbands; ++bandno, ++band) { |
| 297 |
if (node->children[bandno]) { |
| 298 |
if (band->xstart != band->xend && band->ystart != band->yend) { |
| 299 |
jas_seq2d_bindsub(y, x, band->locxstart, band->locystart, |
| 300 |
band->locxend, band->locyend); |
| 301 |
jas_seq2d_setshift(y, band->xstart, band->ystart); |
| 302 |
jpc_tsfbnode_synthesize(node->children[bandno], flags, y); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
jas_seq2d_destroy(y); |
| 307 |
} |
| 308 |
if (node->hqmfb) { |
| 309 |
jpc_qmfb1d_synthesize(node->hqmfb, flags, x); |
| 310 |
} |
| 311 |
if (node->vqmfb) { |
| 312 |
jpc_qmfb1d_synthesize(node->vqmfb, flags | JPC_QMFB1D_VERT, x); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/******************************************************************************\ |
| 317 |
* |
| 318 |
\******************************************************************************/ |
| 319 |
|
| 320 |
|
| 321 |
int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart, uint_fast32_t ystart, |
| 322 |
uint_fast32_t xend, uint_fast32_t yend, jpc_tsfb_band_t *bands) |
| 323 |
{ |
| 324 |
jpc_tsfb_band_t *savbands; |
| 325 |
savbands = bands; |
| 326 |
if (!tsfb->root) { |
| 327 |
bands[0].xstart = xstart; |
| 328 |
bands[0].ystart = ystart; |
| 329 |
bands[0].xend = xend; |
| 330 |
bands[0].yend = yend; |
| 331 |
bands[0].locxstart = xstart; |
| 332 |
bands[0].locystart = ystart; |
| 333 |
bands[0].locxend = xend; |
| 334 |
bands[0].locyend = yend; |
| 335 |
bands[0].orient = JPC_TSFB_LL; |
| 336 |
bands[0].synenergywt = JPC_FIX_ONE; |
| 337 |
++bands; |
| 338 |
} else { |
| 339 |
jpc_tsfbnode_getbandstree(tsfb->root, xstart, ystart, |
| 340 |
xstart, ystart, xend, yend, &bands); |
| 341 |
} |
| 342 |
return bands - savbands; |
| 343 |
} |
| 344 |
|
| 345 |
static void jpc_tsfbnode_getbandstree(jpc_tsfbnode_t *node, uint_fast32_t posxstart, |
| 346 |
uint_fast32_t posystart, uint_fast32_t xstart, uint_fast32_t ystart, |
| 347 |
uint_fast32_t xend, uint_fast32_t yend, jpc_tsfb_band_t **bands) |
| 348 |
{ |
| 349 |
jpc_tsfbnodeband_t nodebands[JPC_TSFB_MAXBANDSPERNODE]; |
| 350 |
jpc_tsfbnodeband_t *nodeband; |
| 351 |
int nodebandno; |
| 352 |
int numnodebands; |
| 353 |
jpc_tsfb_band_t *band; |
| 354 |
jas_seq_t *hfilter; |
| 355 |
jas_seq_t *vfilter; |
| 356 |
|
| 357 |
qmfb2d_getbands(node->hqmfb, node->vqmfb, xstart, ystart, xend, yend, |
| 358 |
JPC_TSFB_MAXBANDSPERNODE, &numnodebands, nodebands); |
| 359 |
if (node->numchildren > 0) { |
| 360 |
for (nodebandno = 0, nodeband = nodebands; |
| 361 |
nodebandno < numnodebands; ++nodebandno, ++nodeband) { |
| 362 |
if (node->children[nodebandno]) { |
| 363 |
jpc_tsfbnode_getbandstree(node->children[ |
| 364 |
nodebandno], posxstart + |
| 365 |
nodeband->locxstart - xstart, posystart + |
| 366 |
nodeband->locystart - ystart, nodeband->xstart, |
| 367 |
nodeband->ystart, nodeband->xend, |
| 368 |
nodeband->yend, bands); |
| 369 |
|
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
assert(numnodebands == 4 || numnodebands == 3); |
| 374 |
for (nodebandno = 0, nodeband = nodebands; nodebandno < numnodebands; |
| 375 |
++nodebandno, ++nodeband) { |
| 376 |
if (!node->children[nodebandno]) { |
| 377 |
band = *bands; |
| 378 |
band->xstart = nodeband->xstart; |
| 379 |
band->ystart = nodeband->ystart; |
| 380 |
band->xend = nodeband->xend; |
| 381 |
band->yend = nodeband->yend; |
| 382 |
band->locxstart = posxstart + nodeband->locxstart - |
| 383 |
xstart; |
| 384 |
band->locystart = posystart + nodeband->locystart - |
| 385 |
ystart; |
| 386 |
band->locxend = band->locxstart + band->xend - |
| 387 |
band->xstart; |
| 388 |
band->locyend = band->locystart + band->yend - |
| 389 |
band->ystart; |
| 390 |
if (numnodebands == 4) { |
| 391 |
switch (nodebandno) { |
| 392 |
case 0: |
| 393 |
band->orient = JPC_TSFB_LL; |
| 394 |
break; |
| 395 |
case 1: |
| 396 |
band->orient = JPC_TSFB_HL; |
| 397 |
break; |
| 398 |
case 2: |
| 399 |
band->orient = JPC_TSFB_LH; |
| 400 |
break; |
| 401 |
case 3: |
| 402 |
band->orient = JPC_TSFB_HH; |
| 403 |
break; |
| 404 |
default: |
| 405 |
abort(); |
| 406 |
break; |
| 407 |
} |
| 408 |
} else { |
| 409 |
switch (nodebandno) { |
| 410 |
case 0: |
| 411 |
band->orient = JPC_TSFB_HL; |
| 412 |
break; |
| 413 |
case 1: |
| 414 |
band->orient = JPC_TSFB_LH; |
| 415 |
break; |
| 416 |
case 2: |
| 417 |
band->orient = JPC_TSFB_HH; |
| 418 |
break; |
| 419 |
default: |
| 420 |
abort(); |
| 421 |
break; |
| 422 |
} |
| 423 |
} |
| 424 |
jpc_tsfbnode_getequivfilters(node, nodebandno, band->xend - band->xstart, band->yend - band->ystart, &hfilter, &vfilter); |
| 425 |
band->synenergywt = jpc_fix_mul(jpc_seq_norm(hfilter), |
| 426 |
jpc_seq_norm(vfilter)); |
| 427 |
jas_seq_destroy(hfilter); |
| 428 |
jas_seq_destroy(vfilter); |
| 429 |
++(*bands); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/******************************************************************************\ |
| 435 |
* |
| 436 |
\******************************************************************************/ |
| 437 |
|
| 438 |
static jpc_tsfbnode_t *jpc_tsfbnode_create() |
| 439 |
{ |
| 440 |
jpc_tsfbnode_t *node; |
| 441 |
if (!(node = jas_malloc(sizeof(jpc_tsfbnode_t)))) { |
| 442 |
return 0; |
| 443 |
} |
| 444 |
node->numhchans = 0; |
| 445 |
node->numvchans = 0; |
| 446 |
node->numchildren = 0; |
| 447 |
node->maxchildren = 0; |
| 448 |
node->hqmfb = 0; |
| 449 |
node->vqmfb = 0; |
| 450 |
node->parent = 0; |
| 451 |
return node; |
| 452 |
} |
| 453 |
|
| 454 |
static void jpc_tsfbnode_destroy(jpc_tsfbnode_t *node) |
| 455 |
{ |
| 456 |
jpc_tsfbnode_t **child; |
| 457 |
int childno; |
| 458 |
for (childno = 0, child = node->children; childno < node->maxchildren; |
| 459 |
++childno, ++child) { |
| 460 |
if (*child) { |
| 461 |
jpc_tsfbnode_destroy(*child); |
| 462 |
} |
| 463 |
} |
| 464 |
if (node->hqmfb) { |
| 465 |
jpc_qmfb1d_destroy(node->hqmfb); |
| 466 |
} |
| 467 |
if (node->vqmfb) { |
| 468 |
jpc_qmfb1d_destroy(node->vqmfb); |
| 469 |
} |
| 470 |
jas_free(node); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
static void qmfb2d_getbands(jpc_qmfb1d_t *hqmfb, jpc_qmfb1d_t *vqmfb, |
| 481 |
uint_fast32_t xstart, uint_fast32_t ystart, uint_fast32_t xend, |
| 482 |
uint_fast32_t yend, int maxbands, int *numbandsptr, jpc_tsfbnodeband_t *bands) |
| 483 |
{ |
| 484 |
jpc_qmfb1dband_t hbands[JPC_QMFB1D_MAXCHANS]; |
| 485 |
jpc_qmfb1dband_t vbands[JPC_QMFB1D_MAXCHANS]; |
| 486 |
int numhbands; |
| 487 |
int numvbands; |
| 488 |
int numbands; |
| 489 |
int bandno; |
| 490 |
int hbandno; |
| 491 |
int vbandno; |
| 492 |
jpc_tsfbnodeband_t *band; |
| 493 |
|
| 494 |
if (hqmfb) { |
| 495 |
jpc_qmfb1d_getbands(hqmfb, 0, xstart, ystart, xend, yend, |
| 496 |
JPC_QMFB1D_MAXCHANS, &numhbands, hbands); |
| 497 |
} else { |
| 498 |
numhbands = 1; |
| 499 |
hbands[0].start = xstart; |
| 500 |
hbands[0].end = xend; |
| 501 |
hbands[0].locstart = xstart; |
| 502 |
hbands[0].locend = xend; |
| 503 |
} |
| 504 |
if (vqmfb) { |
| 505 |
jpc_qmfb1d_getbands(vqmfb, JPC_QMFB1D_VERT, xstart, ystart, xend, |
| 506 |
yend, JPC_QMFB1D_MAXCHANS, &numvbands, vbands); |
| 507 |
} else { |
| 508 |
numvbands = 1; |
| 509 |
vbands[0].start = ystart; |
| 510 |
vbands[0].end = yend; |
| 511 |
vbands[0].locstart = ystart; |
| 512 |
vbands[0].locend = yend; |
| 513 |
} |
| 514 |
numbands = numhbands * numvbands; |
| 515 |
*numbandsptr = numbands; |
| 516 |
for (bandno = 0, band = bands; bandno < numbands; ++bandno, ++band) { |
| 517 |
hbandno = bandno % numhbands; |
| 518 |
vbandno = bandno / numhbands; |
| 519 |
band->xstart = hbands[hbandno].start; |
| 520 |
band->ystart = vbands[vbandno].start; |
| 521 |
band->xend = hbands[hbandno].end; |
| 522 |
band->yend = vbands[vbandno].end; |
| 523 |
band->locxstart = hbands[hbandno].locstart; |
| 524 |
band->locystart = vbands[vbandno].locstart; |
| 525 |
band->locxend = hbands[hbandno].locend; |
| 526 |
band->locyend = vbands[vbandno].locend; |
| 527 |
assert(band->xstart <= band->xend && |
| 528 |
band->ystart <= band->yend); |
| 529 |
if (band->xstart == band->xend) { |
| 530 |
band->yend = band->ystart; |
| 531 |
band->locyend = band->locystart; |
| 532 |
} else if (band->ystart == band->yend) { |
| 533 |
band->xend = band->xstart; |
| 534 |
band->locxend = band->locxstart; |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
static int jpc_tsfbnode_getequivfilters(jpc_tsfbnode_t *tsfbnode, int cldind, |
| 540 |
int width, int height, jas_seq_t **hfilter, jas_seq_t **vfilter) |
| 541 |
{ |
| 542 |
jas_seq_t *hseq; |
| 543 |
jas_seq_t *vseq; |
| 544 |
jpc_tsfbnode_t *node; |
| 545 |
jas_seq2d_t *hfilters[JPC_QMFB1D_MAXCHANS]; |
| 546 |
jas_seq2d_t *vfilters[JPC_QMFB1D_MAXCHANS]; |
| 547 |
int numhchans; |
| 548 |
int numvchans; |
| 549 |
jas_seq_t *tmpseq; |
| 550 |
|
| 551 |
hseq = 0; |
| 552 |
vseq = 0; |
| 553 |
|
| 554 |
if (!(hseq = jas_seq_create(0, 1))) { |
| 555 |
goto error; |
| 556 |
} |
| 557 |
jas_seq_set(hseq, 0, jpc_inttofix(1)); |
| 558 |
if (!(vseq = jas_seq_create(0, 1))) { |
| 559 |
goto error; |
| 560 |
} |
| 561 |
jas_seq_set(vseq, 0, jpc_inttofix(1)); |
| 562 |
|
| 563 |
node = tsfbnode; |
| 564 |
while (node) { |
| 565 |
if (node->hqmfb) { |
| 566 |
numhchans = jpc_qmfb1d_getnumchans(node->hqmfb); |
| 567 |
if (jpc_qmfb1d_getsynfilters(node->hqmfb, width, hfilters)) { |
| 568 |
goto error; |
| 569 |
} |
| 570 |
if (!(tmpseq = jpc_seq_upsample(hseq, numhchans))) { |
| 571 |
goto error; |
| 572 |
} |
| 573 |
jas_seq_destroy(hseq); |
| 574 |
hseq = tmpseq; |
| 575 |
if (!(tmpseq = jpc_seq_conv(hseq, hfilters[bandnotohind(node, cldind)]))) { |
| 576 |
goto error; |
| 577 |
} |
| 578 |
jas_seq_destroy(hfilters[0]); |
| 579 |
jas_seq_destroy(hfilters[1]); |
| 580 |
jas_seq_destroy(hseq); |
| 581 |
hseq = tmpseq; |
| 582 |
} |
| 583 |
if (node->vqmfb) { |
| 584 |
numvchans = jpc_qmfb1d_getnumchans(node->vqmfb); |
| 585 |
if (jpc_qmfb1d_getsynfilters(node->vqmfb, height, vfilters)) { |
| 586 |
abort(); |
| 587 |
} |
| 588 |
if (!(tmpseq = jpc_seq_upsample(vseq, numvchans))) { |
| 589 |
goto error; |
| 590 |
} |
| 591 |
jas_seq_destroy(vseq); |
| 592 |
vseq = tmpseq; |
| 593 |
if (!(tmpseq = jpc_seq_conv(vseq, vfilters[bandnotovind(node, cldind)]))) { |
| 594 |
goto error; |
| 595 |
} |
| 596 |
jas_seq_destroy(vfilters[0]); |
| 597 |
jas_seq_destroy(vfilters[1]); |
| 598 |
jas_seq_destroy(vseq); |
| 599 |
vseq = tmpseq; |
| 600 |
} |
| 601 |
if (node->parent) { |
| 602 |
cldind = jpc_tsfbnode_findchild(node->parent, node); |
| 603 |
} |
| 604 |
node = node->parent; |
| 605 |
} |
| 606 |
|
| 607 |
*hfilter = hseq; |
| 608 |
*vfilter = vseq; |
| 609 |
|
| 610 |
return 0; |
| 611 |
|
| 612 |
error: |
| 613 |
if (hseq) { |
| 614 |
jas_seq_destroy(hseq); |
| 615 |
} |
| 616 |
if (vseq) { |
| 617 |
jas_seq_destroy(vseq); |
| 618 |
} |
| 619 |
return -1; |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
static int jpc_tsfbnode_findchild(jpc_tsfbnode_t *parnode, jpc_tsfbnode_t *cldnode) |
| 624 |
{ |
| 625 |
int i; |
| 626 |
|
| 627 |
for (i = 0; i < parnode->maxchildren; i++) { |
| 628 |
if (parnode->children[i] == cldnode) |
| 629 |
return i; |
| 630 |
} |
| 631 |
assert(0); |
| 632 |
return -1; |
| 633 |
} |
| 634 |
|
| 635 |
jpc_tsfb_t *jpc_cod_gettsfb(int qmfbid, int numlevels) |
| 636 |
{ |
| 637 |
jpc_tsfb_t *tsfb; |
| 638 |
|
| 639 |
switch (qmfbid) { |
| 640 |
case JPC_COX_RFT: |
| 641 |
qmfbid = JPC_QMFB1D_FT; |
| 642 |
break; |
| 643 |
case JPC_COX_INS: |
| 644 |
qmfbid = JPC_QMFB1D_NS; |
| 645 |
break; |
| 646 |
default: |
| 647 |
assert(0); |
| 648 |
qmfbid = 10; |
| 649 |
break; |
| 650 |
} |
| 651 |
|
| 652 |
{ |
| 653 |
jpc_qmfb1d_t *hqmfb; |
| 654 |
hqmfb = jpc_qmfb1d_make(qmfbid); |
| 655 |
assert(hqmfb); |
| 656 |
tsfb = jpc_tsfb_wavelet(hqmfb, hqmfb, numlevels); |
| 657 |
assert(tsfb); |
| 658 |
jpc_qmfb1d_destroy(hqmfb); |
| 659 |
} |
| 660 |
|
| 661 |
return tsfb; |
| 662 |
} |