Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jasper/src/libjasper/jpc/jpc_t2cod.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3
 *   British Columbia.
4
 * Copyright (c) 2001-2003 Michael David Adams.
5
 * All rights reserved.
6
 */
7
8
/* __START_OF_JASPER_LICENSE__
9
 * 
10
 * JasPer License Version 2.0
11
 * 
12
 * Copyright (c) 2001-2006 Michael David Adams
13
 * Copyright (c) 1999-2000 Image Power, Inc.
14
 * Copyright (c) 1999-2000 The University of British Columbia
15
 * 
16
 * All rights reserved.
17
 * 
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:
25
 * 
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.
29
 * 
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
32
 * written permission.
33
 * 
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.
60
 * 
61
 * __END_OF_JASPER_LICENSE__
62
 */
63
64
/*
65
 * Tier-2 Coding Library
66
 *
67
 * $Id$
68
 */
69
70
/******************************************************************************\
71
*
72
\******************************************************************************/
73
74
#include "jpc_t2cod.h"
75
#include "jpc_cs.h"
76
#include "jpc_math.h"
77
78
#include "jasper/jas_math.h"
79
#include "jasper/jas_malloc.h"
80
#include "jasper/jas_debug.h"
81
82
#include <assert.h>
83
84
/******************************************************************************\
85
*
86
\******************************************************************************/
87
88
static int jpc_pi_nextlrcp(jpc_pi_t *pi);
89
static int jpc_pi_nextrlcp(jpc_pi_t *pi);
90
static int jpc_pi_nextrpcl(jpc_pi_t *pi);
91
static int jpc_pi_nextpcrl(jpc_pi_t *pi);
92
static int jpc_pi_nextcprl(jpc_pi_t *pi);
93
94
/******************************************************************************\
95
*
96
\******************************************************************************/
97
98
static void jpc_pirlvl_destroy(jpc_pirlvl_t *rlvl)
99
258k
{
100
258k
  if (rlvl->prclyrnos) {
101
258k
    jas_free(rlvl->prclyrnos);
102
258k
  }
103
258k
}
104
105
static void jpc_picomp_destroy(jpc_picomp_t *picomp)
106
41.2k
{
107
41.2k
  unsigned rlvlno;
108
41.2k
  jpc_pirlvl_t *pirlvl;
109
41.2k
  if (picomp->pirlvls) {
110
299k
    for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
111
299k
      picomp->numrlvls; ++rlvlno, ++pirlvl) {
112
258k
      jpc_pirlvl_destroy(pirlvl);
113
258k
    }
114
40.8k
    jas_free(picomp->pirlvls);
115
40.8k
  }
116
41.2k
}
117
118
/******************************************************************************\
119
*
120
\******************************************************************************/
121
122
int jpc_pi_next(jpc_pi_t *pi)
123
28.2M
{
124
28.2M
  int ret;
125
126
28.2M
  for (;;) {
127
128
28.2M
    pi->valid = false;
129
130
28.2M
    if (!pi->pchg) {
131
54.9k
      ++pi->pchgno;
132
54.9k
      pi->compno = 0;
133
54.9k
      pi->rlvlno = 0;
134
54.9k
      pi->prcno = 0;
135
54.9k
      pi->lyrno = 0;
136
54.9k
      pi->prgvolfirst = true;
137
54.9k
      if (JAS_CAST(unsigned, pi->pchgno) <
138
54.9k
        jpc_pchglist_numpchgs(pi->pchglist)) {
139
37.9k
        pi->pchg = jpc_pchglist_get(pi->pchglist, pi->pchgno);
140
37.9k
      } else if (JAS_CAST(unsigned, pi->pchgno) ==
141
16.9k
        jpc_pchglist_numpchgs(pi->pchglist)) {
142
14.1k
        pi->pchg = &pi->defaultpchg;
143
14.1k
      } else {
144
2.78k
        JAS_LOGDEBUGF(10, "jpc_pi_next returning 1\n");
145
2.78k
        return 1;
146
2.78k
      }
147
54.9k
    }
148
149
28.2M
    const jpc_pchg_t *pchg = pi->pchg;
150
28.2M
    switch (pchg->prgord) {
151
891k
    case JPC_COD_LRCPPRG:
152
891k
      ret = jpc_pi_nextlrcp(pi);
153
891k
      break;
154
110k
    case JPC_COD_RLCPPRG:
155
110k
      ret = jpc_pi_nextrlcp(pi);
156
110k
      break;
157
9.00M
    case JPC_COD_RPCLPRG:
158
9.00M
      ret = jpc_pi_nextrpcl(pi);
159
9.00M
      break;
160
16.2M
    case JPC_COD_PCRLPRG:
161
16.2M
      ret = jpc_pi_nextpcrl(pi);
162
16.2M
      break;
163
2.01M
    case JPC_COD_CPRLPRG:
164
2.01M
      ret = jpc_pi_nextcprl(pi);
165
2.01M
      break;
166
26.2k
    default:
167
26.2k
      ret = -1;
168
26.2k
      break;
169
28.2M
    }
170
28.2M
    if (!ret) {
171
28.2M
      pi->valid = true;
172
28.2M
      ++pi->pktno;
173
28.2M
      return 0;
174
28.2M
    }
175
40.6k
    pi->pchg = 0;
176
40.6k
  }
177
28.2M
}
178
179
static int jpc_pi_nextlrcp(register jpc_pi_t *pi)
180
891k
{
181
891k
  unsigned *prclyrno;
182
183
891k
  JAS_LOGDEBUGF(10, "jpc_pi_nextlrcp\n");
184
185
891k
  const jpc_pchg_t *pchg = pi->pchg;
186
891k
  if (!pi->prgvolfirst) {
187
876k
    prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
188
876k
    goto skip;
189
876k
  } else {
190
15.3k
    pi->prgvolfirst = false;
191
15.3k
  }
192
193
4.42M
  for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
194
4.42M
    pchg->lyrnoend; ++pi->lyrno) {
195
13.0M
    for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
196
8.93M
      pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
197
8.68M
      for (pi->compno = pchg->compnostart, pi->picomp =
198
13.0M
        &pi->picomps[pi->compno]; pi->compno < pi->numcomps
199
8.98M
        && pi->compno < pchg->compnoend; ++pi->compno,
200
4.39M
        ++pi->picomp) {
201
4.39M
        if (pi->rlvlno >= pi->picomp->numrlvls) {
202
95.6k
          continue;
203
95.6k
        }
204
4.30M
        pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
205
4.30M
        for (pi->prcno = 0, prclyrno =
206
5.17M
          pi->pirlvl->prclyrnos; pi->prcno <
207
5.17M
          pi->pirlvl->numprcs; ++pi->prcno,
208
881k
          ++prclyrno) {
209
881k
          if (pi->lyrno >= *prclyrno) {
210
880k
            *prclyrno = pi->lyrno;
211
880k
            ++(*prclyrno);
212
880k
            return 0;
213
880k
          }
214
876k
skip:
215
876k
          ;
216
876k
        }
217
4.30M
      }
218
8.68M
    }
219
4.41M
  }
220
10.8k
  return 1;
221
15.3k
}
222
223
static int jpc_pi_nextrlcp(register jpc_pi_t *pi)
224
110k
{
225
110k
  unsigned *prclyrno;
226
110k
  JAS_LOGDEBUGF(10, "jpc_pi_nextrlcp\n");
227
228
110k
  const jpc_pchg_t *pchg = pi->pchg;
229
110k
  if (!pi->prgvolfirst) {
230
108k
    assert(pi->prcno < pi->pirlvl->numprcs);
231
108k
    prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
232
108k
    goto skip;
233
108k
  } else {
234
2.49k
    pi->prgvolfirst = 0;
235
2.49k
  }
236
237
5.74k
  for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
238
5.46k
    pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
239
17.2M
    for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
240
17.2M
      pchg->lyrnoend; ++pi->lyrno) {
241
17.2M
      for (pi->compno = pchg->compnostart, pi->picomp =
242
42.5M
        &pi->picomps[pi->compno]; pi->compno < pi->numcomps &&
243
27.5M
        pi->compno < pchg->compnoend; ++pi->compno,
244
25.3M
        ++pi->picomp) {
245
25.3M
        if (pi->rlvlno >= pi->picomp->numrlvls) {
246
829k
          continue;
247
829k
        }
248
24.5M
        pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
249
24.5M
        for (pi->prcno = 0, prclyrno = pi->pirlvl->prclyrnos;
250
24.6M
          pi->prcno < pi->pirlvl->numprcs; ++pi->prcno, ++prclyrno) {
251
116k
          if (pi->lyrno >= *prclyrno) {
252
110k
            *prclyrno = pi->lyrno;
253
110k
            ++(*prclyrno);
254
110k
            return 0;
255
110k
          }
256
114k
skip:
257
114k
          ;
258
114k
        }
259
24.5M
      }
260
17.2M
    }
261
5.36k
  }
262
379
  return 1;
263
2.49k
}
264
265
static int jpc_pi_nextrpcl(register jpc_pi_t *pi)
266
9.00M
{
267
9.00M
  unsigned rlvlno;
268
9.00M
  unsigned *prclyrno;
269
9.00M
  unsigned compno;
270
9.00M
  unsigned xstep;
271
9.00M
  unsigned ystep;
272
9.00M
  uint_fast32_t r;
273
9.00M
  uint_fast32_t rpx;
274
9.00M
  uint_fast32_t rpy;
275
9.00M
  uint_fast32_t trx0;
276
9.00M
  uint_fast32_t try0;
277
278
9.00M
  JAS_LOGDEBUGF(10, "jpc_pi_nextrpcl\n");
279
280
9.00M
  const jpc_pchg_t *pchg = pi->pchg;
281
9.00M
  if (!pi->prgvolfirst) {
282
8.99M
    goto skip;
283
8.99M
  } else {
284
2.83k
    pi->xstep = 0;
285
2.83k
    pi->ystep = 0;
286
2.83k
    const jpc_picomp_t *picomp;
287
10.8k
    for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
288
8.01k
      ++compno, ++picomp) {
289
8.01k
      const jpc_pirlvl_t *pirlvl;
290
39.7k
      for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
291
39.7k
        picomp->numrlvls; ++rlvlno, ++pirlvl) {
292
        // Check for the potential for overflow problems.
293
31.6k
        if (pirlvl->prcwidthexpn + picomp->numrlvls >
294
31.6k
          JAS_UINTFAST32_NUMBITS - 2 ||
295
31.6k
          pirlvl->prcheightexpn + picomp->numrlvls >
296
31.6k
          JAS_UINTFAST32_NUMBITS - 2) {
297
0
          return -1;
298
0
        }
299
31.6k
        xstep = picomp->hsamp * (JAS_CAST(uint_fast32_t, 1) <<
300
31.6k
          (pirlvl->prcwidthexpn + picomp->numrlvls - rlvlno - 1));
301
31.6k
        ystep = picomp->vsamp * (JAS_CAST(uint_fast32_t, 1) <<
302
31.6k
          (pirlvl->prcheightexpn + picomp->numrlvls - rlvlno - 1));
303
31.6k
        pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep);
304
31.6k
        pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep);
305
31.6k
      }
306
8.01k
    }
307
2.83k
    pi->prgvolfirst = 0;
308
2.83k
  }
309
310
2.83k
  if (pi->xstep == 0 || pi->ystep == 0) {
311
    /* avoid division by zero */
312
0
    jas_logerrorf("xstep and ystep must be nonzero\n");
313
0
    return -1;
314
0
  }
315
316
5.56k
  for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pchg->rlvlnoend &&
317
4.22k
    pi->rlvlno < pi->maxrlvls; ++pi->rlvlno) {
318
45.8k
    for (pi->y = pi->ystart; pi->y < pi->yend; pi->y +=
319
43.1k
      pi->ystep - (pi->y % pi->ystep)) {
320
182k
      for (pi->x = pi->xstart; pi->x < pi->xend; pi->x +=
321
140k
        pi->xstep - (pi->x % pi->xstep)) {
322
140k
        for (pi->compno = pchg->compnostart,
323
140k
          pi->picomp = &pi->picomps[pi->compno];
324
362k
          pi->compno < pchg->compnoend && pi->compno <
325
268k
          pi->numcomps; ++pi->compno, ++pi->picomp) {
326
222k
          if (pi->rlvlno >= pi->picomp->numrlvls) {
327
25.9k
            continue;
328
25.9k
          }
329
196k
          pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
330
196k
          if (pi->pirlvl->numprcs == 0) {
331
62.0k
            continue;
332
62.0k
          }
333
134k
          r = pi->picomp->numrlvls - 1 - pi->rlvlno;
334
134k
          rpx = r + pi->pirlvl->prcwidthexpn;
335
134k
          rpy = r + pi->pirlvl->prcheightexpn;
336
134k
          trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
337
134k
          try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
338
134k
          if (((pi->x == pi->xstart &&
339
6.34k
            ((trx0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpx)))
340
132k
            || !(pi->x % (pi->picomp->hsamp << rpx))) &&
341
72.6k
            ((pi->y == pi->ystart &&
342
53.1k
            ((try0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpy)))
343
70.5k
            || !(pi->y % (pi->picomp->vsamp << rpy)))) {
344
10.4k
            const unsigned prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x,
345
10.4k
              pi->picomp->hsamp << r), pi->pirlvl->prcwidthexpn) -
346
10.4k
              JPC_FLOORDIVPOW2(trx0, pi->pirlvl->prcwidthexpn);
347
10.4k
            const unsigned prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y,
348
10.4k
              pi->picomp->vsamp << r), pi->pirlvl->prcheightexpn) -
349
10.4k
              JPC_FLOORDIVPOW2(try0, pi->pirlvl->prcheightexpn);
350
10.4k
            pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
351
10.4k
            if (pi->prcno >= pi->pirlvl->numprcs) {
352
0
              return -1;
353
0
            }
354
355
9.01M
            for (pi->lyrno = 0; pi->lyrno <
356
9.01M
              pi->numlyrs && pi->lyrno < pchg->lyrnoend; ++pi->lyrno) {
357
9.00M
              prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
358
9.00M
              if (pi->lyrno >= *prclyrno) {
359
8.99M
                ++(*prclyrno);
360
8.99M
                return 0;
361
8.99M
              }
362
9.00M
skip:
363
9.00M
              ;
364
9.00M
            }
365
10.4k
          }
366
134k
        }
367
140k
      }
368
43.1k
    }
369
3.74k
  }
370
1.81k
  return 1;
371
2.83k
}
372
373
static int jpc_pi_nextpcrl(register jpc_pi_t *pi)
374
16.2M
{
375
16.2M
  unsigned rlvlno;
376
16.2M
  unsigned *prclyrno;
377
16.2M
  unsigned compno;
378
16.2M
  unsigned xstep;
379
16.2M
  unsigned ystep;
380
16.2M
  uint_fast32_t trx0;
381
16.2M
  uint_fast32_t try0;
382
16.2M
  uint_fast32_t r;
383
16.2M
  uint_fast32_t rpx;
384
16.2M
  uint_fast32_t rpy;
385
386
16.2M
  JAS_LOGDEBUGF(10, "jpc_pi_nextpcrl\n");
387
388
16.2M
  const jpc_pchg_t *pchg = pi->pchg;
389
16.2M
  if (!pi->prgvolfirst) {
390
16.2M
    goto skip;
391
16.2M
  } else {
392
3.12k
    pi->xstep = 0;
393
3.12k
    pi->ystep = 0;
394
3.12k
    const jpc_picomp_t *picomp;
395
13.2k
    for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
396
10.1k
      ++compno, ++picomp) {
397
10.1k
      const jpc_pirlvl_t *pirlvl;
398
72.3k
      for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
399
72.3k
        picomp->numrlvls; ++rlvlno, ++pirlvl) {
400
        // Check for the potential for overflow problems.
401
62.2k
        if (pirlvl->prcwidthexpn + picomp->numrlvls >
402
62.2k
          JAS_UINTFAST32_NUMBITS - 2 ||
403
62.2k
          pirlvl->prcheightexpn + picomp->numrlvls >
404
62.2k
          JAS_UINTFAST32_NUMBITS - 2) {
405
0
          return -1;
406
0
        }
407
62.2k
        xstep = picomp->hsamp * (JAS_CAST(uint_fast32_t, 1) <<
408
62.2k
          (pirlvl->prcwidthexpn + picomp->numrlvls - rlvlno - 1));
409
62.2k
        ystep = picomp->vsamp * (JAS_CAST(uint_fast32_t, 1) <<
410
62.2k
          (pirlvl->prcheightexpn + picomp->numrlvls - rlvlno - 1));
411
62.2k
        pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep);
412
62.2k
        pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep);
413
62.2k
      }
414
10.1k
    }
415
3.12k
    pi->prgvolfirst = 0;
416
3.12k
  }
417
418
3.12k
  if (pi->xstep == 0 || pi->ystep == 0) {
419
    /* avoid division by zero */
420
0
    jas_logerrorf("xstep and ystep must be nonzero\n");
421
0
    return -1;
422
0
  }
423
424
38.8k
  for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += pi->ystep -
425
38.2k
    (pi->y % pi->ystep)) {
426
268k
    for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += pi->xstep -
427
233k
      (pi->x % pi->xstep)) {
428
233k
      for (pi->compno = pchg->compnostart, pi->picomp =
429
399k
        &pi->picomps[pi->compno]; pi->compno < pi->numcomps
430
205k
        && pi->compno < pchg->compnoend; ++pi->compno,
431
169k
        ++pi->picomp) {
432
169k
        for (pi->rlvlno = pchg->rlvlnostart,
433
169k
          pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
434
663k
          pi->rlvlno < pi->picomp->numrlvls &&
435
499k
          pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno,
436
496k
          ++pi->pirlvl) {
437
496k
          if (pi->pirlvl->numprcs == 0) {
438
168k
            continue;
439
168k
          }
440
327k
          r = pi->picomp->numrlvls - 1 - pi->rlvlno;
441
327k
          trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
442
327k
          try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
443
327k
          rpx = r + pi->pirlvl->prcwidthexpn;
444
327k
          rpy = r + pi->pirlvl->prcheightexpn;
445
327k
          if (((pi->x == pi->xstart &&
446
90.2k
            ((trx0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpx))) ||
447
243k
            !(pi->x % (pi->picomp->hsamp << rpx))) &&
448
202k
            ((pi->y == pi->ystart &&
449
59.4k
            ((try0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpy))) ||
450
155k
            !(pi->y % (pi->picomp->vsamp << rpy)))) {
451
138k
            const unsigned prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x,
452
138k
              pi->picomp->hsamp << r), pi->pirlvl->prcwidthexpn) -
453
138k
              JPC_FLOORDIVPOW2(trx0, pi->pirlvl->prcwidthexpn);
454
138k
            const unsigned prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y,
455
138k
              pi->picomp->vsamp << r), pi->pirlvl->prcheightexpn) -
456
138k
              JPC_FLOORDIVPOW2(try0, pi->pirlvl->prcheightexpn);
457
138k
            pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
458
138k
            assert(pi->prcno < pi->pirlvl->numprcs);
459
16.3M
            for (pi->lyrno = 0; pi->lyrno < pi->numlyrs &&
460
16.2M
              pi->lyrno < pchg->lyrnoend;
461
16.2M
              ++pi->lyrno) {
462
16.2M
              prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
463
16.2M
              if (pi->lyrno >= *prclyrno) {
464
16.2M
                ++(*prclyrno);
465
16.2M
                return 0;
466
16.2M
              }
467
16.2M
skip:
468
16.2M
              ;
469
16.2M
            }
470
138k
          }
471
327k
        }
472
169k
      }
473
233k
    }
474
38.2k
  }
475
537
  return 1;
476
3.12k
}
477
478
static int jpc_pi_nextcprl(register jpc_pi_t *pi)
479
2.01M
{
480
2.01M
  unsigned rlvlno;
481
2.01M
  unsigned *prclyrno;
482
2.01M
  uint_fast32_t trx0;
483
2.01M
  uint_fast32_t try0;
484
2.01M
  uint_fast32_t r;
485
2.01M
  uint_fast32_t rpx;
486
2.01M
  uint_fast32_t rpy;
487
488
2.01M
  JAS_LOGDEBUGF(10, "jpc_pi_nextcprl\n");
489
490
2.01M
  const jpc_pchg_t *pchg = pi->pchg;
491
2.01M
  if (!pi->prgvolfirst) {
492
2.01M
    goto skip;
493
2.01M
  } else {
494
2.18k
    pi->prgvolfirst = 0;
495
2.18k
  }
496
497
#if 0
498
  /*
499
  This disabled code is wrong.  xstep and ystep need not be set yet.
500
  This will cause some valid code streams to fail to be decoded.
501
  */
502
  if (pi->xstep == 0 || pi->ystep == 0) {
503
    /* avoid later division by zero */
504
    jas_logerrorf("xstep and ystep must be nonzero\n");
505
    return -1;
506
  }
507
#endif
508
509
2.18k
  for (pi->compno = pchg->compnostart, pi->picomp = &pi->picomps[pi->compno];
510
3.49k
    pi->compno < pchg->compnoend && pi->compno < pi->numcomps;
511
2.87k
    ++pi->compno, ++pi->picomp) {
512
2.87k
    const jpc_pirlvl_t *pirlvl = pi->picomp->pirlvls;
513
    // Check for the potential for overflow problems.
514
2.87k
    if (pirlvl->prcwidthexpn + pi->picomp->numrlvls >
515
2.87k
      JAS_UINTFAST32_NUMBITS - 2 ||
516
2.87k
      pirlvl->prcheightexpn + pi->picomp->numrlvls >
517
2.87k
      JAS_UINTFAST32_NUMBITS - 2) {
518
0
      jas_logerrorf("overflow detected\n");
519
0
      return -1;
520
0
    }
521
2.87k
    pi->xstep = pi->picomp->hsamp * (JAS_CAST(uint_fast32_t, 1) <<
522
2.87k
      (pirlvl->prcwidthexpn + pi->picomp->numrlvls - 1));
523
2.87k
    pi->ystep = pi->picomp->vsamp * (JAS_CAST(uint_fast32_t, 1) <<
524
2.87k
      (pirlvl->prcheightexpn + pi->picomp->numrlvls - 1));
525
2.87k
    for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1];
526
8.63k
      rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) {
527
      /* Perform the following calculation in an overflow-safe manner,
528
      setting the result to zero upon overflow:
529
      pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp *
530
        (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcwidthexpn +
531
        pi->picomp->numrlvls - rlvlno - 1)));
532
      */
533
5.96k
      pi->xstep = JAS_MIN(pi->xstep, jas_safeui64_to_int(
534
5.96k
        jas_safeui64_mul(
535
5.96k
          jas_safeui64_from_intmax(pi->picomp->hsamp),
536
5.96k
          jas_safeui64_pow2_intmax(pirlvl->prcwidthexpn +
537
5.96k
            pi->picomp->numrlvls - rlvlno - 1)), 0));
538
5.96k
      if (!pi->xstep) {
539
163
        jas_logerrorf("overflow in x-step calculation\n");
540
163
        return -1;
541
163
      }
542
543
      /* Perform the following calculation in an overflow-safe manner,
544
      setting the result to zero upon overflow:
545
      pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp *
546
        (JAS_CAST(uint_fast32_t, 1) << (pirlvl->prcheightexpn +
547
        pi->picomp->numrlvls - rlvlno - 1)));
548
      */
549
5.80k
      pi->ystep = JAS_MIN(pi->ystep, jas_safeui64_to_int(
550
5.80k
        jas_safeui64_mul(
551
5.80k
          jas_safeui64_from_intmax(pi->picomp->vsamp),
552
5.80k
          jas_safeui64_pow2_intmax(pirlvl->prcheightexpn +
553
5.80k
            pi->picomp->numrlvls - rlvlno - 1)), 0));
554
5.80k
      if (!pi->ystep) {
555
52
        jas_logerrorf("overflow in y-step calculation\n");
556
52
        return -1;
557
52
      }
558
5.80k
    }
559
600k
    for (pi->y = pi->ystart; pi->y < pi->yend;
560
599k
      pi->y += pi->ystep - (pi->y % pi->ystep)) {
561
1.32M
      for (pi->x = pi->xstart; pi->x < pi->xend;
562
724k
        pi->x += pi->xstep - (pi->x % pi->xstep)) {
563
724k
        for (pi->rlvlno = pchg->rlvlnostart,
564
724k
          pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
565
1.80M
          pi->rlvlno < pi->picomp->numrlvls && pi->rlvlno <
566
1.08M
          pchg->rlvlnoend; ++pi->rlvlno, ++pi->pirlvl) {
567
1.08M
          if (pi->pirlvl->numprcs == 0) {
568
942k
            continue;
569
942k
          }
570
141k
          r = pi->picomp->numrlvls - 1 - pi->rlvlno;
571
141k
          trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
572
141k
          try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
573
141k
          rpx = r + pi->pirlvl->prcwidthexpn;
574
141k
          rpy = r + pi->pirlvl->prcheightexpn;
575
141k
          if (((pi->x == pi->xstart &&
576
12.3k
            ((trx0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpx))) ||
577
133k
            !(pi->x % (pi->picomp->hsamp << rpx))) &&
578
133k
            ((pi->y == pi->ystart &&
579
104k
            ((try0 << r) % (JAS_CAST(uint_fast32_t, 1) << rpy))) ||
580
131k
            !(pi->y % (pi->picomp->vsamp << rpy)))) {
581
111k
            const unsigned prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x,
582
111k
              pi->picomp->hsamp << r), pi->pirlvl->prcwidthexpn) -
583
111k
              JPC_FLOORDIVPOW2(trx0, pi->pirlvl->prcwidthexpn);
584
111k
            const unsigned prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y,
585
111k
              pi->picomp->vsamp << r), pi->pirlvl->prcheightexpn) -
586
111k
              JPC_FLOORDIVPOW2(try0, pi->pirlvl->prcheightexpn);
587
111k
            pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
588
111k
            assert(pi->prcno < pi->pirlvl->numprcs);
589
2.12M
            for (pi->lyrno = 0; pi->lyrno < pi->numlyrs &&
590
2.11M
              pi->lyrno < pchg->lyrnoend;
591
2.01M
              ++pi->lyrno) {
592
2.01M
              prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
593
2.01M
              if (pi->lyrno >= *prclyrno) {
594
2.01M
                ++(*prclyrno);
595
2.01M
                return 0;
596
2.01M
              }
597
2.01M
skip:
598
2.01M
              ;
599
2.01M
            }
600
111k
          }
601
141k
        }
602
724k
      }
603
599k
    }
604
2.66k
  }
605
618
  return 1;
606
2.18k
}
607
608
/******************************************************************************\
609
*
610
\******************************************************************************/
611
612
void jpc_pi_destroy(jpc_pi_t *pi)
613
15.9k
{
614
15.9k
  jpc_picomp_t *picomp;
615
15.9k
  unsigned compno;
616
15.9k
  if (pi->picomps) {
617
57.1k
    for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
618
41.2k
      ++compno, ++picomp) {
619
41.2k
      jpc_picomp_destroy(picomp);
620
41.2k
    }
621
15.9k
    jas_free(pi->picomps);
622
15.9k
  }
623
15.9k
  if (pi->pchglist) {
624
15.9k
    jpc_pchglist_destroy(pi->pchglist);
625
15.9k
  }
626
15.9k
  jas_free(pi);
627
15.9k
}
628
629
jpc_pi_t *jpc_pi_create0()
630
15.9k
{
631
15.9k
  jpc_pi_t *pi;
632
15.9k
  if (!(pi = jas_malloc(sizeof(jpc_pi_t)))) {
633
1
    return 0;
634
1
  }
635
15.9k
  pi->picomps = 0;
636
15.9k
  pi->pchgno = 0;
637
15.9k
  if (!(pi->pchglist = jpc_pchglist_create())) {
638
1
    jas_free(pi);
639
1
    return 0;
640
1
  }
641
15.9k
  return pi;
642
15.9k
}
643
644
int jpc_pi_init(jpc_pi_t *pi)
645
15.8k
{
646
15.8k
  unsigned compno;
647
15.8k
  unsigned rlvlno;
648
15.8k
  unsigned prcno;
649
15.8k
  unsigned *prclyrno;
650
651
15.8k
  pi->prgvolfirst = 0;
652
15.8k
  pi->valid = 0;
653
15.8k
  pi->pktno = -1;
654
15.8k
  pi->pchgno = -1;
655
15.8k
  pi->pchg = 0;
656
657
15.8k
  const jpc_picomp_t *picomp;
658
55.6k
  for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
659
39.8k
    ++compno, ++picomp) {
660
39.8k
    const jpc_pirlvl_t *pirlvl;
661
271k
    for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
662
271k
      picomp->numrlvls; ++rlvlno, ++pirlvl) {
663
231k
      for (prcno = 0, prclyrno = pirlvl->prclyrnos;
664
11.0M
        prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
665
10.8M
        *prclyrno = 0;
666
10.8M
      }
667
231k
    }
668
39.8k
  }
669
15.8k
  return 0;
670
15.8k
}
671
672
int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg)
673
47.3k
{
674
47.3k
  return jpc_pchglist_insert(pi->pchglist, -1, pchg);
675
47.3k
}
676
677
/* For debugging only. */
678
void jpc_pi_dump(const jpc_pi_t *pi)
679
0
{
680
0
  jas_eprintf("numlyrs=%d\n", pi->numlyrs);
681
0
  jas_eprintf("maxrlvls=%d\n", pi->maxrlvls);
682
0
  jas_eprintf("numcomps=%d\n", pi->numcomps);
683
0
  jas_eprintf("compno=%d\n", pi->compno);
684
0
  jas_eprintf("rlvlno=%d\n", pi->rlvlno);
685
0
  jas_eprintf("prcno=%d\n", pi->prcno);
686
0
  jas_eprintf("lyrno=%d\n", pi->lyrno);
687
0
  jas_eprintf("x=%d\n", pi->x);
688
0
  jas_eprintf("y=%d\n", pi->y);
689
0
  jas_eprintf("xstep=%d\n", pi->xstep);
690
0
  jas_eprintf("ystep=%d\n", pi->ystep);
691
0
  jas_eprintf("xstart=%d\n", pi->xstart);
692
0
  jas_eprintf("ystart=%d\n", pi->ystart);
693
0
  jas_eprintf("xend=%d\n", pi->xend);
694
0
  jas_eprintf("yend=%d\n", pi->yend);
695
0
  jas_eprintf("defaultpchg=%d\n", pi->defaultpchg);
696
0
  jas_eprintf("pchgno=%d\n", pi->pchgno);
697
0
  jas_eprintf("prgvolfirst=%d\n", pi->prgvolfirst);
698
0
  jas_eprintf("valid=%d\n", pi->valid);
699
0
  jas_eprintf("pktno=%d\n", pi->pktno);
700
0
}
701
702
/******************************************************************************\
703
*
704
\******************************************************************************/
705
706
jpc_pchglist_t *jpc_pchglist_create()
707
57.6k
{
708
57.6k
  jpc_pchglist_t *pchglist;
709
57.6k
  if (!(pchglist = jas_malloc(sizeof(jpc_pchglist_t)))) {
710
1
    return 0;
711
1
  }
712
57.6k
  pchglist->numpchgs = 0;
713
57.6k
  pchglist->maxpchgs = 0;
714
57.6k
  pchglist->pchgs = 0;
715
57.6k
  return pchglist;
716
57.6k
}
717
718
int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg)
719
151k
{
720
151k
  jpc_pchg_t **newpchgs;
721
151k
  if (pchgno < 0) {
722
151k
    pchgno = pchglist->numpchgs;
723
151k
  }
724
151k
  if (pchglist->numpchgs >= pchglist->maxpchgs) {
725
3.44k
    const unsigned newmaxpchgs = pchglist->maxpchgs + 128;
726
3.44k
    if (!(newpchgs = jas_realloc2(pchglist->pchgs, newmaxpchgs,
727
3.44k
      sizeof(jpc_pchg_t *)))) {
728
0
      return -1;
729
0
    }
730
3.44k
    pchglist->maxpchgs = newmaxpchgs;
731
3.44k
    pchglist->pchgs = newpchgs;
732
3.44k
  }
733
151k
  for (unsigned i = pchglist->numpchgs; i > (unsigned)pchgno; --i) {
734
0
    pchglist->pchgs[i] = pchglist->pchgs[i - 1];
735
0
  }
736
151k
  pchglist->pchgs[pchgno] = pchg;
737
151k
  ++pchglist->numpchgs;
738
151k
  return 0;
739
151k
}
740
741
jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, unsigned pchgno)
742
7.31k
{
743
7.31k
  jpc_pchg_t *pchg;
744
7.31k
  assert(pchgno < pchglist->numpchgs);
745
7.31k
  pchg = pchglist->pchgs[pchgno];
746
376k
  for (unsigned i = pchgno + 1; i < pchglist->numpchgs; ++i) {
747
369k
    pchglist->pchgs[i - 1] = pchglist->pchgs[i];
748
369k
  }
749
7.31k
  --pchglist->numpchgs;
750
7.31k
  return pchg;
751
7.31k
}
752
753
jpc_pchg_t *jpc_pchg_copy(const jpc_pchg_t *pchg)
754
151k
{
755
151k
  jpc_pchg_t *newpchg;
756
151k
  if (!(newpchg = jas_malloc(sizeof(jpc_pchg_t)))) {
757
0
    return 0;
758
0
  }
759
151k
  *newpchg = *pchg;
760
151k
  return newpchg;
761
151k
}
762
763
jpc_pchglist_t *jpc_pchglist_copy(const jpc_pchglist_t *pchglist)
764
14.6k
{
765
14.6k
  jpc_pchglist_t *newpchglist;
766
14.6k
  jpc_pchg_t *newpchg;
767
14.6k
  if (!(newpchglist = jpc_pchglist_create())) {
768
0
    return 0;
769
0
  }
770
17.3k
  for (unsigned pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
771
2.69k
    if (!(newpchg = jpc_pchg_copy(pchglist->pchgs[pchgno])) ||
772
2.69k
      jpc_pchglist_insert(newpchglist, -1, newpchg)) {
773
0
      jpc_pchglist_destroy(newpchglist);
774
0
      return 0;
775
0
    }
776
2.69k
  }
777
14.6k
  return newpchglist;
778
14.6k
}
779
780
void jpc_pchglist_destroy(jpc_pchglist_t *pchglist)
781
57.6k
{
782
57.6k
  if (pchglist->pchgs) {
783
146k
    for (unsigned pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
784
144k
      jpc_pchg_destroy(pchglist->pchgs[pchgno]);
785
144k
    }
786
2.41k
    jas_free(pchglist->pchgs);
787
2.41k
  }
788
57.6k
  jas_free(pchglist);
789
57.6k
}
790
791
void jpc_pchg_destroy(jpc_pchg_t *pchg)
792
151k
{
793
151k
  jas_free(pchg);
794
151k
}
795
796
const jpc_pchg_t *jpc_pchglist_get(const jpc_pchglist_t *pchglist, unsigned pchgno)
797
85.3k
{
798
85.3k
  return pchglist->pchgs[pchgno];
799
85.3k
}
800
801
unsigned jpc_pchglist_numpchgs(const jpc_pchglist_t *pchglist)
802
133k
{
803
133k
  return pchglist->numpchgs;
804
133k
}