| 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 |
* Tier-2 Coding Library |
| 115 |
* |
| 116 |
* $Id$ |
| 117 |
*/ |
| 118 |
|
| 119 |
#include "jasper/jas_math.h" |
| 120 |
#include "jasper/jas_malloc.h" |
| 121 |
#include "jasper/jas_math.h" |
| 122 |
|
| 123 |
#include "jpc_cs.h" |
| 124 |
#include "jpc_t2cod.h" |
| 125 |
#include "jpc_math.h" |
| 126 |
|
| 127 |
static int jpc_pi_nextlrcp(jpc_pi_t *pi); |
| 128 |
static int jpc_pi_nextrlcp(jpc_pi_t *pi); |
| 129 |
static int jpc_pi_nextrpcl(jpc_pi_t *pi); |
| 130 |
static int jpc_pi_nextpcrl(jpc_pi_t *pi); |
| 131 |
static int jpc_pi_nextcprl(jpc_pi_t *pi); |
| 132 |
|
| 133 |
int jpc_pi_next(jpc_pi_t *pi) |
| 134 |
{ |
| 135 |
jpc_pchg_t *pchg; |
| 136 |
int ret; |
| 137 |
|
| 138 |
|
| 139 |
for (;;) { |
| 140 |
|
| 141 |
pi->valid = false; |
| 142 |
|
| 143 |
if (!pi->pchg) { |
| 144 |
++pi->pchgno; |
| 145 |
pi->compno = 0; |
| 146 |
pi->rlvlno = 0; |
| 147 |
pi->prcno = 0; |
| 148 |
pi->lyrno = 0; |
| 149 |
pi->prgvolfirst = true; |
| 150 |
if (pi->pchgno < jpc_pchglist_numpchgs(pi->pchglist)) { |
| 151 |
pi->pchg = jpc_pchglist_get(pi->pchglist, pi->pchgno); |
| 152 |
} else if (pi->pchgno == jpc_pchglist_numpchgs(pi->pchglist)) { |
| 153 |
pi->pchg = &pi->defaultpchg; |
| 154 |
} else { |
| 155 |
return 1; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
pchg = pi->pchg; |
| 160 |
switch (pchg->prgord) { |
| 161 |
case JPC_COD_LRCPPRG: |
| 162 |
ret = jpc_pi_nextlrcp(pi); |
| 163 |
break; |
| 164 |
case JPC_COD_RLCPPRG: |
| 165 |
ret = jpc_pi_nextrlcp(pi); |
| 166 |
break; |
| 167 |
case JPC_COD_RPCLPRG: |
| 168 |
ret = jpc_pi_nextrpcl(pi); |
| 169 |
break; |
| 170 |
case JPC_COD_PCRLPRG: |
| 171 |
ret = jpc_pi_nextpcrl(pi); |
| 172 |
break; |
| 173 |
case JPC_COD_CPRLPRG: |
| 174 |
ret = jpc_pi_nextcprl(pi); |
| 175 |
break; |
| 176 |
default: |
| 177 |
ret = -1; |
| 178 |
break; |
| 179 |
} |
| 180 |
if (!ret) { |
| 181 |
pi->valid = true; |
| 182 |
++pi->pktno; |
| 183 |
return 0; |
| 184 |
} |
| 185 |
pi->pchg = 0; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static int jpc_pi_nextlrcp(jpc_pi_t *pi) |
| 190 |
{ |
| 191 |
jpc_pchg_t *pchg; |
| 192 |
int *prclyrno; |
| 193 |
|
| 194 |
pchg = pi->pchg; |
| 195 |
if (!pi->prgvolfirst) { |
| 196 |
prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; |
| 197 |
goto skip; |
| 198 |
} else { |
| 199 |
pi->prgvolfirst = false; |
| 200 |
} |
| 201 |
|
| 202 |
for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno < |
| 203 |
pchg->lyrnoend; ++pi->lyrno) { |
| 204 |
for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls && |
| 205 |
pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) { |
| 206 |
for (pi->compno = pchg->compnostart, pi->picomp = |
| 207 |
&pi->picomps[pi->compno]; pi->compno < pi->numcomps |
| 208 |
&& pi->compno < pchg->compnoend; ++pi->compno, |
| 209 |
++pi->picomp) { |
| 210 |
if (pi->rlvlno >= pi->picomp->numrlvls) { |
| 211 |
continue; |
| 212 |
} |
| 213 |
pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; |
| 214 |
for (pi->prcno = 0, prclyrno = |
| 215 |
pi->pirlvl->prclyrnos; pi->prcno < |
| 216 |
pi->pirlvl->numprcs; ++pi->prcno, |
| 217 |
++prclyrno) { |
| 218 |
if (pi->lyrno >= *prclyrno) { |
| 219 |
*prclyrno = pi->lyrno; |
| 220 |
++(*prclyrno); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
skip: |
| 224 |
; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
static int jpc_pi_nextrlcp(jpc_pi_t *pi) |
| 233 |
{ |
| 234 |
jpc_pchg_t *pchg; |
| 235 |
int *prclyrno; |
| 236 |
|
| 237 |
pchg = pi->pchg; |
| 238 |
if (!pi->prgvolfirst) { |
| 239 |
assert(pi->prcno < pi->pirlvl->numprcs); |
| 240 |
prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; |
| 241 |
goto skip; |
| 242 |
} else { |
| 243 |
pi->prgvolfirst = 0; |
| 244 |
} |
| 245 |
|
| 246 |
for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls && |
| 247 |
pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) { |
| 248 |
for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno < |
| 249 |
pchg->lyrnoend; ++pi->lyrno) { |
| 250 |
for (pi->compno = pchg->compnostart, pi->picomp = |
| 251 |
&pi->picomps[pi->compno]; pi->compno < pi->numcomps && |
| 252 |
pi->compno < pchg->compnoend; ++pi->compno, ++pi->picomp) { |
| 253 |
if (pi->rlvlno >= pi->picomp->numrlvls) { |
| 254 |
continue; |
| 255 |
} |
| 256 |
pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; |
| 257 |
for (pi->prcno = 0, prclyrno = pi->pirlvl->prclyrnos; |
| 258 |
pi->prcno < pi->pirlvl->numprcs; ++pi->prcno, ++prclyrno) { |
| 259 |
if (pi->lyrno >= *prclyrno) { |
| 260 |
*prclyrno = pi->lyrno; |
| 261 |
++(*prclyrno); |
| 262 |
return 0; |
| 263 |
} |
| 264 |
skip: |
| 265 |
; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
static int jpc_pi_nextrpcl(jpc_pi_t *pi) |
| 274 |
{ |
| 275 |
int rlvlno; |
| 276 |
jpc_pirlvl_t *pirlvl; |
| 277 |
jpc_pchg_t *pchg; |
| 278 |
int prchind; |
| 279 |
int prcvind; |
| 280 |
int *prclyrno; |
| 281 |
int compno; |
| 282 |
jpc_picomp_t *picomp; |
| 283 |
int xstep; |
| 284 |
int ystep; |
| 285 |
uint_fast32_t r; |
| 286 |
uint_fast32_t rpx; |
| 287 |
uint_fast32_t rpy; |
| 288 |
uint_fast32_t trx0; |
| 289 |
uint_fast32_t try0; |
| 290 |
|
| 291 |
pchg = pi->pchg; |
| 292 |
if (!pi->prgvolfirst) { |
| 293 |
goto skip; |
| 294 |
} else { |
| 295 |
pi->xstep = 0; |
| 296 |
pi->ystep = 0; |
| 297 |
for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; |
| 298 |
++compno, ++picomp) { |
| 299 |
for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
| 300 |
picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 301 |
xstep = picomp->hsamp * (1 << (pirlvl->prcwidthexpn + |
| 302 |
picomp->numrlvls - rlvlno - 1)); |
| 303 |
ystep = picomp->vsamp * (1 << (pirlvl->prcheightexpn + |
| 304 |
picomp->numrlvls - rlvlno - 1)); |
| 305 |
pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep); |
| 306 |
pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep); |
| 307 |
} |
| 308 |
} |
| 309 |
pi->prgvolfirst = 0; |
| 310 |
} |
| 311 |
|
| 312 |
for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pchg->rlvlnoend && |
| 313 |
pi->rlvlno < pi->maxrlvls; ++pi->rlvlno) { |
| 314 |
for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += |
| 315 |
pi->ystep - (pi->y % pi->ystep)) { |
| 316 |
for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += |
| 317 |
pi->xstep - (pi->x % pi->xstep)) { |
| 318 |
for (pi->compno = pchg->compnostart, |
| 319 |
pi->picomp = &pi->picomps[pi->compno]; |
| 320 |
pi->compno < pchg->compnoend && pi->compno < |
| 321 |
pi->numcomps; ++pi->compno, ++pi->picomp) { |
| 322 |
if (pi->rlvlno >= pi->picomp->numrlvls) { |
| 323 |
continue; |
| 324 |
} |
| 325 |
pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; |
| 326 |
if (pi->pirlvl->numprcs == 0) { |
| 327 |
continue; |
| 328 |
} |
| 329 |
r = pi->picomp->numrlvls - 1 - pi->rlvlno; |
| 330 |
rpx = r + pi->pirlvl->prcwidthexpn; |
| 331 |
rpy = r + pi->pirlvl->prcheightexpn; |
| 332 |
trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); |
| 333 |
try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); |
| 334 |
if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) |
| 335 |
|| !(pi->x % (1 << rpx))) && |
| 336 |
((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) |
| 337 |
|| !(pi->y % (1 << rpy)))) { |
| 338 |
prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp |
| 339 |
<< r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, |
| 340 |
pi->pirlvl->prcwidthexpn); |
| 341 |
prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp |
| 342 |
<< r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, |
| 343 |
pi->pirlvl->prcheightexpn); |
| 344 |
pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind; |
| 345 |
|
| 346 |
assert(pi->prcno < pi->pirlvl->numprcs); |
| 347 |
for (pi->lyrno = 0; pi->lyrno < |
| 348 |
pi->numlyrs && pi->lyrno < pchg->lyrnoend; ++pi->lyrno) { |
| 349 |
prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; |
| 350 |
if (pi->lyrno >= *prclyrno) { |
| 351 |
++(*prclyrno); |
| 352 |
return 0; |
| 353 |
} |
| 354 |
skip: |
| 355 |
; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
return 1; |
| 363 |
} |
| 364 |
|
| 365 |
static int jpc_pi_nextpcrl(jpc_pi_t *pi) |
| 366 |
{ |
| 367 |
int rlvlno; |
| 368 |
jpc_pirlvl_t *pirlvl; |
| 369 |
jpc_pchg_t *pchg; |
| 370 |
int prchind; |
| 371 |
int prcvind; |
| 372 |
int *prclyrno; |
| 373 |
int compno; |
| 374 |
jpc_picomp_t *picomp; |
| 375 |
int xstep; |
| 376 |
int ystep; |
| 377 |
uint_fast32_t trx0; |
| 378 |
uint_fast32_t try0; |
| 379 |
uint_fast32_t r; |
| 380 |
uint_fast32_t rpx; |
| 381 |
uint_fast32_t rpy; |
| 382 |
|
| 383 |
pchg = pi->pchg; |
| 384 |
if (!pi->prgvolfirst) { |
| 385 |
goto skip; |
| 386 |
} else { |
| 387 |
pi->xstep = 0; |
| 388 |
pi->ystep = 0; |
| 389 |
for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; |
| 390 |
++compno, ++picomp) { |
| 391 |
for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
| 392 |
picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 393 |
xstep = picomp->hsamp * (1 << |
| 394 |
(pirlvl->prcwidthexpn + picomp->numrlvls - |
| 395 |
rlvlno - 1)); |
| 396 |
ystep = picomp->vsamp * (1 << |
| 397 |
(pirlvl->prcheightexpn + picomp->numrlvls - |
| 398 |
rlvlno - 1)); |
| 399 |
pi->xstep = (!pi->xstep) ? xstep : |
| 400 |
JAS_MIN(pi->xstep, xstep); |
| 401 |
pi->ystep = (!pi->ystep) ? ystep : |
| 402 |
JAS_MIN(pi->ystep, ystep); |
| 403 |
} |
| 404 |
} |
| 405 |
pi->prgvolfirst = 0; |
| 406 |
} |
| 407 |
|
| 408 |
for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += pi->ystep - |
| 409 |
(pi->y % pi->ystep)) { |
| 410 |
for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += pi->xstep - |
| 411 |
(pi->x % pi->xstep)) { |
| 412 |
for (pi->compno = pchg->compnostart, pi->picomp = |
| 413 |
&pi->picomps[pi->compno]; pi->compno < pi->numcomps |
| 414 |
&& pi->compno < pchg->compnoend; ++pi->compno, |
| 415 |
++pi->picomp) { |
| 416 |
for (pi->rlvlno = pchg->rlvlnostart, |
| 417 |
pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; |
| 418 |
pi->rlvlno < pi->picomp->numrlvls && |
| 419 |
pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno, |
| 420 |
++pi->pirlvl) { |
| 421 |
if (pi->pirlvl->numprcs == 0) { |
| 422 |
continue; |
| 423 |
} |
| 424 |
r = pi->picomp->numrlvls - 1 - pi->rlvlno; |
| 425 |
trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); |
| 426 |
try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); |
| 427 |
rpx = r + pi->pirlvl->prcwidthexpn; |
| 428 |
rpy = r + pi->pirlvl->prcheightexpn; |
| 429 |
if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) || |
| 430 |
!(pi->x % (pi->picomp->hsamp << rpx))) && |
| 431 |
((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) || |
| 432 |
!(pi->y % (pi->picomp->vsamp << rpy)))) { |
| 433 |
prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp |
| 434 |
<< r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, |
| 435 |
pi->pirlvl->prcwidthexpn); |
| 436 |
prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp |
| 437 |
<< r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, |
| 438 |
pi->pirlvl->prcheightexpn); |
| 439 |
pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind; |
| 440 |
assert(pi->prcno < pi->pirlvl->numprcs); |
| 441 |
for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && |
| 442 |
pi->lyrno < pchg->lyrnoend; ++pi->lyrno) { |
| 443 |
prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; |
| 444 |
if (pi->lyrno >= *prclyrno) { |
| 445 |
++(*prclyrno); |
| 446 |
return 0; |
| 447 |
} |
| 448 |
skip: |
| 449 |
; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
return 1; |
| 457 |
} |
| 458 |
|
| 459 |
static int jpc_pi_nextcprl(jpc_pi_t *pi) |
| 460 |
{ |
| 461 |
int rlvlno; |
| 462 |
jpc_pirlvl_t *pirlvl; |
| 463 |
jpc_pchg_t *pchg; |
| 464 |
int prchind; |
| 465 |
int prcvind; |
| 466 |
int *prclyrno; |
| 467 |
uint_fast32_t trx0; |
| 468 |
uint_fast32_t try0; |
| 469 |
uint_fast32_t r; |
| 470 |
uint_fast32_t rpx; |
| 471 |
uint_fast32_t rpy; |
| 472 |
|
| 473 |
pchg = pi->pchg; |
| 474 |
if (!pi->prgvolfirst) { |
| 475 |
goto skip; |
| 476 |
} else { |
| 477 |
pi->prgvolfirst = 0; |
| 478 |
} |
| 479 |
|
| 480 |
for (pi->compno = pchg->compnostart, pi->picomp = |
| 481 |
&pi->picomps[pi->compno]; pi->compno < pchg->compnoend; ++pi->compno, |
| 482 |
++pi->picomp) { |
| 483 |
pirlvl = pi->picomp->pirlvls; |
| 484 |
pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn + |
| 485 |
pi->picomp->numrlvls - 1)); |
| 486 |
pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn + |
| 487 |
pi->picomp->numrlvls - 1)); |
| 488 |
for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1]; |
| 489 |
rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 490 |
pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 << |
| 491 |
(pirlvl->prcwidthexpn + pi->picomp->numrlvls - |
| 492 |
rlvlno - 1))); |
| 493 |
pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 << |
| 494 |
(pirlvl->prcheightexpn + pi->picomp->numrlvls - |
| 495 |
rlvlno - 1))); |
| 496 |
} |
| 497 |
for (pi->y = pi->ystart; pi->y < pi->yend; |
| 498 |
pi->y += pi->ystep - (pi->y % pi->ystep)) { |
| 499 |
for (pi->x = pi->xstart; pi->x < pi->xend; |
| 500 |
pi->x += pi->xstep - (pi->x % pi->xstep)) { |
| 501 |
for (pi->rlvlno = pchg->rlvlnostart, |
| 502 |
pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; |
| 503 |
pi->rlvlno < pi->picomp->numrlvls && pi->rlvlno < |
| 504 |
pchg->rlvlnoend; ++pi->rlvlno, ++pi->pirlvl) { |
| 505 |
if (pi->pirlvl->numprcs == 0) { |
| 506 |
continue; |
| 507 |
} |
| 508 |
r = pi->picomp->numrlvls - 1 - pi->rlvlno; |
| 509 |
trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); |
| 510 |
try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); |
| 511 |
rpx = r + pi->pirlvl->prcwidthexpn; |
| 512 |
rpy = r + pi->pirlvl->prcheightexpn; |
| 513 |
if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) || |
| 514 |
!(pi->x % (pi->picomp->hsamp << rpx))) && |
| 515 |
((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) || |
| 516 |
!(pi->y % (pi->picomp->vsamp << rpy)))) { |
| 517 |
prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp |
| 518 |
<< r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, |
| 519 |
pi->pirlvl->prcwidthexpn); |
| 520 |
prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp |
| 521 |
<< r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, |
| 522 |
pi->pirlvl->prcheightexpn); |
| 523 |
pi->prcno = prcvind * |
| 524 |
pi->pirlvl->numhprcs + |
| 525 |
prchind; |
| 526 |
assert(pi->prcno < |
| 527 |
pi->pirlvl->numprcs); |
| 528 |
for (pi->lyrno = 0; pi->lyrno < |
| 529 |
pi->numlyrs && pi->lyrno < pchg->lyrnoend; ++pi->lyrno) { |
| 530 |
prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; |
| 531 |
if (pi->lyrno >= *prclyrno) { |
| 532 |
++(*prclyrno); |
| 533 |
return 0; |
| 534 |
} |
| 535 |
skip: |
| 536 |
; |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
return 1; |
| 544 |
} |
| 545 |
|
| 546 |
static void pirlvl_destroy(jpc_pirlvl_t *rlvl) |
| 547 |
{ |
| 548 |
if (rlvl->prclyrnos) { |
| 549 |
jas_free(rlvl->prclyrnos); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
static void jpc_picomp_destroy(jpc_picomp_t *picomp) |
| 554 |
{ |
| 555 |
int rlvlno; |
| 556 |
jpc_pirlvl_t *pirlvl; |
| 557 |
if (picomp->pirlvls) { |
| 558 |
for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
| 559 |
picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 560 |
pirlvl_destroy(pirlvl); |
| 561 |
} |
| 562 |
jas_free(picomp->pirlvls); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
void jpc_pi_destroy(jpc_pi_t *pi) |
| 567 |
{ |
| 568 |
jpc_picomp_t *picomp; |
| 569 |
int compno; |
| 570 |
if (pi->picomps) { |
| 571 |
for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; |
| 572 |
++compno, ++picomp) { |
| 573 |
jpc_picomp_destroy(picomp); |
| 574 |
} |
| 575 |
jas_free(pi->picomps); |
| 576 |
} |
| 577 |
if (pi->pchglist) { |
| 578 |
jpc_pchglist_destroy(pi->pchglist); |
| 579 |
} |
| 580 |
jas_free(pi); |
| 581 |
} |
| 582 |
|
| 583 |
jpc_pi_t *jpc_pi_create0(void) |
| 584 |
{ |
| 585 |
jpc_pi_t *pi; |
| 586 |
if (!(pi = jas_malloc(sizeof(jpc_pi_t)))) { |
| 587 |
return 0; |
| 588 |
} |
| 589 |
pi->picomps = 0; |
| 590 |
pi->pchgno = 0; |
| 591 |
if (!(pi->pchglist = jpc_pchglist_create())) { |
| 592 |
jas_free(pi); |
| 593 |
return 0; |
| 594 |
} |
| 595 |
return pi; |
| 596 |
} |
| 597 |
|
| 598 |
int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg) |
| 599 |
{ |
| 600 |
return jpc_pchglist_insert(pi->pchglist, -1, pchg); |
| 601 |
} |
| 602 |
|
| 603 |
jpc_pchglist_t *jpc_pchglist_create(void) |
| 604 |
{ |
| 605 |
jpc_pchglist_t *pchglist; |
| 606 |
if (!(pchglist = jas_malloc(sizeof(jpc_pchglist_t)))) { |
| 607 |
return 0; |
| 608 |
} |
| 609 |
pchglist->numpchgs = 0; |
| 610 |
pchglist->maxpchgs = 0; |
| 611 |
pchglist->pchgs = 0; |
| 612 |
return pchglist; |
| 613 |
} |
| 614 |
|
| 615 |
int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg) |
| 616 |
{ |
| 617 |
int i; |
| 618 |
int newmaxpchgs; |
| 619 |
jpc_pchg_t **newpchgs; |
| 620 |
if (pchgno < 0) { |
| 621 |
pchgno = pchglist->numpchgs; |
| 622 |
} |
| 623 |
if (pchglist->numpchgs >= pchglist->maxpchgs) { |
| 624 |
newmaxpchgs = pchglist->maxpchgs + 128; |
| 625 |
if (!(newpchgs = jas_realloc(pchglist->pchgs, newmaxpchgs * sizeof(jpc_pchg_t *)))) { |
| 626 |
return -1; |
| 627 |
} |
| 628 |
pchglist->maxpchgs = newmaxpchgs; |
| 629 |
pchglist->pchgs = newpchgs; |
| 630 |
} |
| 631 |
for (i = pchglist->numpchgs; i > pchgno; --i) { |
| 632 |
pchglist->pchgs[i] = pchglist->pchgs[i - 1]; |
| 633 |
} |
| 634 |
pchglist->pchgs[pchgno] = pchg; |
| 635 |
++pchglist->numpchgs; |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, int pchgno) |
| 640 |
{ |
| 641 |
int i; |
| 642 |
jpc_pchg_t *pchg; |
| 643 |
assert(pchgno < pchglist->numpchgs); |
| 644 |
pchg = pchglist->pchgs[pchgno]; |
| 645 |
for (i = pchgno + 1; i < pchglist->numpchgs; ++i) { |
| 646 |
pchglist->pchgs[i - 1] = pchglist->pchgs[i]; |
| 647 |
} |
| 648 |
--pchglist->numpchgs; |
| 649 |
return pchg; |
| 650 |
} |
| 651 |
|
| 652 |
jpc_pchg_t *jpc_pchg_copy(jpc_pchg_t *pchg) |
| 653 |
{ |
| 654 |
jpc_pchg_t *newpchg; |
| 655 |
if (!(newpchg = jas_malloc(sizeof(jpc_pchg_t)))) { |
| 656 |
return 0; |
| 657 |
} |
| 658 |
*newpchg = *pchg; |
| 659 |
return newpchg; |
| 660 |
} |
| 661 |
|
| 662 |
jpc_pchglist_t *jpc_pchglist_copy(jpc_pchglist_t *pchglist) |
| 663 |
{ |
| 664 |
jpc_pchglist_t *newpchglist; |
| 665 |
jpc_pchg_t *newpchg; |
| 666 |
int pchgno; |
| 667 |
if (!(newpchglist = jpc_pchglist_create())) { |
| 668 |
return 0; |
| 669 |
} |
| 670 |
for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) { |
| 671 |
if (!(newpchg = jpc_pchg_copy(pchglist->pchgs[pchgno])) || |
| 672 |
jpc_pchglist_insert(newpchglist, -1, newpchg)) { |
| 673 |
jpc_pchglist_destroy(newpchglist); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
} |
| 677 |
return newpchglist; |
| 678 |
} |
| 679 |
|
| 680 |
void jpc_pchglist_destroy(jpc_pchglist_t *pchglist) |
| 681 |
{ |
| 682 |
int pchgno; |
| 683 |
if (pchglist->pchgs) { |
| 684 |
for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) { |
| 685 |
jpc_pchg_destroy(pchglist->pchgs[pchgno]); |
| 686 |
} |
| 687 |
jas_free(pchglist->pchgs); |
| 688 |
} |
| 689 |
jas_free(pchglist); |
| 690 |
} |
| 691 |
|
| 692 |
void jpc_pchg_destroy(jpc_pchg_t *pchg) |
| 693 |
{ |
| 694 |
jas_free(pchg); |
| 695 |
} |
| 696 |
|
| 697 |
jpc_pchg_t *jpc_pchglist_get(jpc_pchglist_t *pchglist, int pchgno) |
| 698 |
{ |
| 699 |
return pchglist->pchgs[pchgno]; |
| 700 |
} |
| 701 |
|
| 702 |
int jpc_pchglist_numpchgs(jpc_pchglist_t *pchglist) |
| 703 |
{ |
| 704 |
return pchglist->numpchgs; |
| 705 |
} |
| 706 |
|
| 707 |
int jpc_pi_init(jpc_pi_t *pi) |
| 708 |
{ |
| 709 |
int compno; |
| 710 |
int rlvlno; |
| 711 |
int prcno; |
| 712 |
jpc_picomp_t *picomp; |
| 713 |
jpc_pirlvl_t *pirlvl; |
| 714 |
int *prclyrno; |
| 715 |
|
| 716 |
pi->prgvolfirst = 0; |
| 717 |
pi->valid = 0; |
| 718 |
pi->pktno = -1; |
| 719 |
pi->pchgno = -1; |
| 720 |
pi->pchg = 0; |
| 721 |
|
| 722 |
for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; |
| 723 |
++compno, ++picomp) { |
| 724 |
for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
| 725 |
picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 726 |
for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
| 727 |
prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
| 728 |
*prclyrno = 0; |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
return 0; |
| 733 |
} |