2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61 * __END_OF_JASPER_LICENSE__
65 * Tier-2 Coding Library
67 * $Id: jpc_t2cod.c,v 1.1 2005/05/22 18:33:05 malaterre Exp $
70 #include "jasper/jas_math.h"
71 #include "jasper/jas_malloc.h"
72 #include "jasper/jas_math.h"
75 #include "jpc_t2cod.h"
78 static int jpc_pi_nextlrcp(jpc_pi_t *pi);
79 static int jpc_pi_nextrlcp(jpc_pi_t *pi);
80 static int jpc_pi_nextrpcl(jpc_pi_t *pi);
81 static int jpc_pi_nextpcrl(jpc_pi_t *pi);
82 static int jpc_pi_nextcprl(jpc_pi_t *pi);
84 int jpc_pi_next(jpc_pi_t *pi)
100 pi->prgvolfirst = true;
101 if (pi->pchgno < jpc_pchglist_numpchgs(pi->pchglist)) {
102 pi->pchg = jpc_pchglist_get(pi->pchglist, pi->pchgno);
103 } else if (pi->pchgno == jpc_pchglist_numpchgs(pi->pchglist)) {
104 pi->pchg = &pi->defaultpchg;
111 switch (pchg->prgord) {
112 case JPC_COD_LRCPPRG:
113 ret = jpc_pi_nextlrcp(pi);
115 case JPC_COD_RLCPPRG:
116 ret = jpc_pi_nextrlcp(pi);
118 case JPC_COD_RPCLPRG:
119 ret = jpc_pi_nextrpcl(pi);
121 case JPC_COD_PCRLPRG:
122 ret = jpc_pi_nextpcrl(pi);
124 case JPC_COD_CPRLPRG:
125 ret = jpc_pi_nextcprl(pi);
140 static int jpc_pi_nextlrcp(register jpc_pi_t *pi)
146 if (!pi->prgvolfirst) {
147 prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
150 pi->prgvolfirst = false;
153 for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
154 JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
155 for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
156 pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
157 for (pi->compno = pchg->compnostart, pi->picomp =
158 &pi->picomps[pi->compno]; pi->compno < pi->numcomps
159 && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
161 if (pi->rlvlno >= pi->picomp->numrlvls) {
164 pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
165 for (pi->prcno = 0, prclyrno =
166 pi->pirlvl->prclyrnos; pi->prcno <
167 pi->pirlvl->numprcs; ++pi->prcno,
169 if (pi->lyrno >= *prclyrno) {
170 *prclyrno = pi->lyrno;
183 static int jpc_pi_nextrlcp(register jpc_pi_t *pi)
189 if (!pi->prgvolfirst) {
190 assert(pi->prcno < pi->pirlvl->numprcs);
191 prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
197 for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
198 pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
199 for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
200 JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
201 for (pi->compno = pchg->compnostart, pi->picomp =
202 &pi->picomps[pi->compno]; pi->compno < pi->numcomps &&
203 pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, ++pi->picomp) {
204 if (pi->rlvlno >= pi->picomp->numrlvls) {
207 pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
208 for (pi->prcno = 0, prclyrno = pi->pirlvl->prclyrnos;
209 pi->prcno < pi->pirlvl->numprcs; ++pi->prcno, ++prclyrno) {
210 if (pi->lyrno >= *prclyrno) {
211 *prclyrno = pi->lyrno;
224 static int jpc_pi_nextrpcl(register jpc_pi_t *pi)
227 jpc_pirlvl_t *pirlvl;
233 jpc_picomp_t *picomp;
243 if (!pi->prgvolfirst) {
248 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
249 ++compno, ++picomp) {
250 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
251 picomp->numrlvls; ++rlvlno, ++pirlvl) {
252 xstep = picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
253 picomp->numrlvls - rlvlno - 1));
254 ystep = picomp->vsamp * (1 << (pirlvl->prcheightexpn +
255 picomp->numrlvls - rlvlno - 1));
256 pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep);
257 pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep);
263 for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pchg->rlvlnoend &&
264 pi->rlvlno < pi->maxrlvls; ++pi->rlvlno) {
265 for (pi->y = pi->ystart; pi->y < pi->yend; pi->y +=
266 pi->ystep - (pi->y % pi->ystep)) {
267 for (pi->x = pi->xstart; pi->x < pi->xend; pi->x +=
268 pi->xstep - (pi->x % pi->xstep)) {
269 for (pi->compno = pchg->compnostart,
270 pi->picomp = &pi->picomps[pi->compno];
271 pi->compno < JAS_CAST(int, pchg->compnoend) && pi->compno <
272 pi->numcomps; ++pi->compno, ++pi->picomp) {
273 if (pi->rlvlno >= pi->picomp->numrlvls) {
276 pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
277 if (pi->pirlvl->numprcs == 0) {
280 r = pi->picomp->numrlvls - 1 - pi->rlvlno;
281 rpx = r + pi->pirlvl->prcwidthexpn;
282 rpy = r + pi->pirlvl->prcheightexpn;
283 trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
284 try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
285 if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx)))
286 || !(pi->x % (1 << rpx))) &&
287 ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy)))
288 || !(pi->y % (1 << rpy)))) {
289 prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
290 << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
291 pi->pirlvl->prcwidthexpn);
292 prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
293 << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
294 pi->pirlvl->prcheightexpn);
295 pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
297 assert(pi->prcno < pi->pirlvl->numprcs);
298 for (pi->lyrno = 0; pi->lyrno <
299 pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
300 prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
301 if (pi->lyrno >= *prclyrno) {
316 static int jpc_pi_nextpcrl(register jpc_pi_t *pi)
319 jpc_pirlvl_t *pirlvl;
325 jpc_picomp_t *picomp;
335 if (!pi->prgvolfirst) {
340 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
341 ++compno, ++picomp) {
342 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
343 picomp->numrlvls; ++rlvlno, ++pirlvl) {
344 xstep = picomp->hsamp * (1 <<
345 (pirlvl->prcwidthexpn + picomp->numrlvls -
347 ystep = picomp->vsamp * (1 <<
348 (pirlvl->prcheightexpn + picomp->numrlvls -
350 pi->xstep = (!pi->xstep) ? xstep :
351 JAS_MIN(pi->xstep, xstep);
352 pi->ystep = (!pi->ystep) ? ystep :
353 JAS_MIN(pi->ystep, ystep);
359 for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += pi->ystep -
360 (pi->y % pi->ystep)) {
361 for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += pi->xstep -
362 (pi->x % pi->xstep)) {
363 for (pi->compno = pchg->compnostart, pi->picomp =
364 &pi->picomps[pi->compno]; pi->compno < pi->numcomps
365 && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
367 for (pi->rlvlno = pchg->rlvlnostart,
368 pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
369 pi->rlvlno < pi->picomp->numrlvls &&
370 pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno,
372 if (pi->pirlvl->numprcs == 0) {
375 r = pi->picomp->numrlvls - 1 - pi->rlvlno;
376 trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
377 try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
378 rpx = r + pi->pirlvl->prcwidthexpn;
379 rpy = r + pi->pirlvl->prcheightexpn;
380 if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) ||
381 !(pi->x % (pi->picomp->hsamp << rpx))) &&
382 ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) ||
383 !(pi->y % (pi->picomp->vsamp << rpy)))) {
384 prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
385 << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
386 pi->pirlvl->prcwidthexpn);
387 prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
388 << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
389 pi->pirlvl->prcheightexpn);
390 pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
391 assert(pi->prcno < pi->pirlvl->numprcs);
392 for (pi->lyrno = 0; pi->lyrno < pi->numlyrs &&
393 pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
394 prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
395 if (pi->lyrno >= *prclyrno) {
410 static int jpc_pi_nextcprl(register jpc_pi_t *pi)
413 jpc_pirlvl_t *pirlvl;
425 if (!pi->prgvolfirst) {
431 for (pi->compno = pchg->compnostart, pi->picomp =
432 &pi->picomps[pi->compno]; pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
434 pirlvl = pi->picomp->pirlvls;
435 pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
436 pi->picomp->numrlvls - 1));
437 pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn +
438 pi->picomp->numrlvls - 1));
439 for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1];
440 rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) {
441 pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 <<
442 (pirlvl->prcwidthexpn + pi->picomp->numrlvls -
444 pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 <<
445 (pirlvl->prcheightexpn + pi->picomp->numrlvls -
448 for (pi->y = pi->ystart; pi->y < pi->yend;
449 pi->y += pi->ystep - (pi->y % pi->ystep)) {
450 for (pi->x = pi->xstart; pi->x < pi->xend;
451 pi->x += pi->xstep - (pi->x % pi->xstep)) {
452 for (pi->rlvlno = pchg->rlvlnostart,
453 pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
454 pi->rlvlno < pi->picomp->numrlvls && pi->rlvlno <
455 pchg->rlvlnoend; ++pi->rlvlno, ++pi->pirlvl) {
456 if (pi->pirlvl->numprcs == 0) {
459 r = pi->picomp->numrlvls - 1 - pi->rlvlno;
460 trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
461 try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
462 rpx = r + pi->pirlvl->prcwidthexpn;
463 rpy = r + pi->pirlvl->prcheightexpn;
464 if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) ||
465 !(pi->x % (pi->picomp->hsamp << rpx))) &&
466 ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) ||
467 !(pi->y % (pi->picomp->vsamp << rpy)))) {
468 prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
469 << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
470 pi->pirlvl->prcwidthexpn);
471 prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
472 << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
473 pi->pirlvl->prcheightexpn);
474 pi->prcno = prcvind *
475 pi->pirlvl->numhprcs +
478 pi->pirlvl->numprcs);
479 for (pi->lyrno = 0; pi->lyrno <
480 pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
481 prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
482 if (pi->lyrno >= *prclyrno) {
497 static void pirlvl_destroy(jpc_pirlvl_t *rlvl)
499 if (rlvl->prclyrnos) {
500 jas_free(rlvl->prclyrnos);
504 static void jpc_picomp_destroy(jpc_picomp_t *picomp)
507 jpc_pirlvl_t *pirlvl;
508 if (picomp->pirlvls) {
509 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
510 picomp->numrlvls; ++rlvlno, ++pirlvl) {
511 pirlvl_destroy(pirlvl);
513 jas_free(picomp->pirlvls);
517 void jpc_pi_destroy(jpc_pi_t *pi)
519 jpc_picomp_t *picomp;
522 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
523 ++compno, ++picomp) {
524 jpc_picomp_destroy(picomp);
526 jas_free(pi->picomps);
529 jpc_pchglist_destroy(pi->pchglist);
534 jpc_pi_t *jpc_pi_create0()
537 if (!(pi = jas_malloc(sizeof(jpc_pi_t)))) {
542 if (!(pi->pchglist = jpc_pchglist_create())) {
549 int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg)
551 return jpc_pchglist_insert(pi->pchglist, -1, pchg);
554 jpc_pchglist_t *jpc_pchglist_create()
556 jpc_pchglist_t *pchglist;
557 if (!(pchglist = jas_malloc(sizeof(jpc_pchglist_t)))) {
560 pchglist->numpchgs = 0;
561 pchglist->maxpchgs = 0;
566 int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg)
570 jpc_pchg_t **newpchgs;
572 pchgno = pchglist->numpchgs;
574 if (pchglist->numpchgs >= pchglist->maxpchgs) {
575 newmaxpchgs = pchglist->maxpchgs + 128;
576 if (!(newpchgs = jas_realloc(pchglist->pchgs, newmaxpchgs * sizeof(jpc_pchg_t *)))) {
579 pchglist->maxpchgs = newmaxpchgs;
580 pchglist->pchgs = newpchgs;
582 for (i = pchglist->numpchgs; i > pchgno; --i) {
583 pchglist->pchgs[i] = pchglist->pchgs[i - 1];
585 pchglist->pchgs[pchgno] = pchg;
586 ++pchglist->numpchgs;
590 jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, int pchgno)
594 assert(pchgno < pchglist->numpchgs);
595 pchg = pchglist->pchgs[pchgno];
596 for (i = pchgno + 1; i < pchglist->numpchgs; ++i) {
597 pchglist->pchgs[i - 1] = pchglist->pchgs[i];
599 --pchglist->numpchgs;
603 jpc_pchg_t *jpc_pchg_copy(jpc_pchg_t *pchg)
606 if (!(newpchg = jas_malloc(sizeof(jpc_pchg_t)))) {
613 jpc_pchglist_t *jpc_pchglist_copy(jpc_pchglist_t *pchglist)
615 jpc_pchglist_t *newpchglist;
618 if (!(newpchglist = jpc_pchglist_create())) {
621 for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
622 if (!(newpchg = jpc_pchg_copy(pchglist->pchgs[pchgno])) ||
623 jpc_pchglist_insert(newpchglist, -1, newpchg)) {
624 jpc_pchglist_destroy(newpchglist);
631 void jpc_pchglist_destroy(jpc_pchglist_t *pchglist)
634 if (pchglist->pchgs) {
635 for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
636 jpc_pchg_destroy(pchglist->pchgs[pchgno]);
638 jas_free(pchglist->pchgs);
643 void jpc_pchg_destroy(jpc_pchg_t *pchg)
648 jpc_pchg_t *jpc_pchglist_get(jpc_pchglist_t *pchglist, int pchgno)
650 return pchglist->pchgs[pchgno];
653 int jpc_pchglist_numpchgs(jpc_pchglist_t *pchglist)
655 return pchglist->numpchgs;
658 int jpc_pi_init(jpc_pi_t *pi)
663 jpc_picomp_t *picomp;
664 jpc_pirlvl_t *pirlvl;
673 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
674 ++compno, ++picomp) {
675 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
676 picomp->numrlvls; ++rlvlno, ++pirlvl) {
677 for (prcno = 0, prclyrno = pirlvl->prclyrnos;
678 prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {