Coverage Report

Created: 2026-10-19 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/JPXStream.cc
Line
Count
Source
1
//========================================================================
2
//
3
// JPXStream.cc
4
//
5
// Copyright 2002-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <limits.h>
12
#include "gmem.h"
13
#include "gmempp.h"
14
#include "Error.h"
15
#include "JArithmeticDecoder.h"
16
#include "JPXStream.h"
17
18
//~ to do:
19
//  - ROI
20
//  - progression order changes
21
//  - packed packet headers
22
//  - support for palettes, channel maps, etc.
23
//  - make sure all needed JP2/JPX subboxes are parsed (readBoxes)
24
//  - can we assume that QCC segments must come after the QCD segment?
25
//  - handle tilePartToEOC in readTilePartData
26
//  - in coefficient decoding (readCodeBlockData):
27
//    - selective arithmetic coding bypass
28
//      (this also affects reading the cb->dataLen array)
29
//    - coeffs longer than 31 bits (should just ignore the extra bits?)
30
//  - handle boxes larger than 2^32 bytes
31
//  - the fixed-point arithmetic won't handle 16-bit pixels
32
33
//------------------------------------------------------------------------
34
35
// number of contexts for the arithmetic decoder
36
14.1k
#define jpxNContexts        19
37
38
62.1k
#define jpxContextSigProp    0  // 0 - 8: significance prop and cleanup
39
#define jpxContextSign       9  // 9 - 13: sign
40
#define jpxContextMagRef    14  // 14 - 16: magnitude refinement
41
426k
#define jpxContextRunLength 17  // cleanup: run length
42
96.3k
#define jpxContextUniform   18  // cleanup: first signif coeff
43
44
//------------------------------------------------------------------------
45
46
124k
#define jpxPassSigProp       0
47
54.1k
#define jpxPassMagRef        1
48
215k
#define jpxPassCleanup       2
49
50
//------------------------------------------------------------------------
51
52
// arithmetic decoder context for the significance propagation and
53
// cleanup passes:
54
//     [horiz][vert][diag][subband]
55
// where subband = 0 for HL
56
//               = 1 for LH and LL
57
//               = 2 for HH
58
static Guint sigPropContext[3][3][5][3] = {
59
  {{{ 0, 0, 0 },   // horiz=0, vert=0, diag=0
60
    { 1, 1, 3 },   // horiz=0, vert=0, diag=1
61
    { 2, 2, 6 },   // horiz=0, vert=0, diag=2
62
    { 2, 2, 8 },   // horiz=0, vert=0, diag=3
63
    { 2, 2, 8 }},  // horiz=0, vert=0, diag=4
64
   {{ 5, 3, 1 },   // horiz=0, vert=1, diag=0
65
    { 6, 3, 4 },   // horiz=0, vert=1, diag=1
66
    { 6, 3, 7 },   // horiz=0, vert=1, diag=2
67
    { 6, 3, 8 },   // horiz=0, vert=1, diag=3
68
    { 6, 3, 8 }},  // horiz=0, vert=1, diag=4
69
   {{ 8, 4, 2 },   // horiz=0, vert=2, diag=0
70
    { 8, 4, 5 },   // horiz=0, vert=2, diag=1
71
    { 8, 4, 7 },   // horiz=0, vert=2, diag=2
72
    { 8, 4, 8 },   // horiz=0, vert=2, diag=3
73
    { 8, 4, 8 }}}, // horiz=0, vert=2, diag=4
74
  {{{ 3, 5, 1 },   // horiz=1, vert=0, diag=0
75
    { 3, 6, 4 },   // horiz=1, vert=0, diag=1
76
    { 3, 6, 7 },   // horiz=1, vert=0, diag=2
77
    { 3, 6, 8 },   // horiz=1, vert=0, diag=3
78
    { 3, 6, 8 }},  // horiz=1, vert=0, diag=4
79
   {{ 7, 7, 2 },   // horiz=1, vert=1, diag=0
80
    { 7, 7, 5 },   // horiz=1, vert=1, diag=1
81
    { 7, 7, 7 },   // horiz=1, vert=1, diag=2
82
    { 7, 7, 8 },   // horiz=1, vert=1, diag=3
83
    { 7, 7, 8 }},  // horiz=1, vert=1, diag=4
84
   {{ 8, 7, 2 },   // horiz=1, vert=2, diag=0
85
    { 8, 7, 5 },   // horiz=1, vert=2, diag=1
86
    { 8, 7, 7 },   // horiz=1, vert=2, diag=2
87
    { 8, 7, 8 },   // horiz=1, vert=2, diag=3
88
    { 8, 7, 8 }}}, // horiz=1, vert=2, diag=4
89
  {{{ 4, 8, 2 },   // horiz=2, vert=0, diag=0
90
    { 4, 8, 5 },   // horiz=2, vert=0, diag=1
91
    { 4, 8, 7 },   // horiz=2, vert=0, diag=2
92
    { 4, 8, 8 },   // horiz=2, vert=0, diag=3
93
    { 4, 8, 8 }},  // horiz=2, vert=0, diag=4
94
   {{ 7, 8, 2 },   // horiz=2, vert=1, diag=0
95
    { 7, 8, 5 },   // horiz=2, vert=1, diag=1
96
    { 7, 8, 7 },   // horiz=2, vert=1, diag=2
97
    { 7, 8, 8 },   // horiz=2, vert=1, diag=3
98
    { 7, 8, 8 }},  // horiz=2, vert=1, diag=4
99
   {{ 8, 8, 2 },   // horiz=2, vert=2, diag=0
100
    { 8, 8, 5 },   // horiz=2, vert=2, diag=1
101
    { 8, 8, 7 },   // horiz=2, vert=2, diag=2
102
    { 8, 8, 8 },   // horiz=2, vert=2, diag=3
103
    { 8, 8, 8 }}}  // horiz=2, vert=2, diag=4
104
};
105
106
// arithmetic decoder context and xor bit for the sign bit in the
107
// significance propagation pass:
108
//     [horiz][vert][k]
109
// where horiz/vert are offset by 2 (i.e., range is -2 .. 2)
110
// and k = 0 for the context
111
//       = 1 for the xor bit
112
static Guint signContext[5][5][2] = {
113
  {{ 13, 1 },  // horiz=-2, vert=-2
114
   { 13, 1 },  // horiz=-2, vert=-1
115
   { 12, 1 },  // horiz=-2, vert= 0
116
   { 11, 1 },  // horiz=-2, vert=+1
117
   { 11, 1 }}, // horiz=-2, vert=+2
118
  {{ 13, 1 },  // horiz=-1, vert=-2
119
   { 13, 1 },  // horiz=-1, vert=-1
120
   { 12, 1 },  // horiz=-1, vert= 0
121
   { 11, 1 },  // horiz=-1, vert=+1
122
   { 11, 1 }}, // horiz=-1, vert=+2
123
  {{ 10, 1 },  // horiz= 0, vert=-2
124
   { 10, 1 },  // horiz= 0, vert=-1
125
   {  9, 0 },  // horiz= 0, vert= 0
126
   { 10, 0 },  // horiz= 0, vert=+1
127
   { 10, 0 }}, // horiz= 0, vert=+2
128
  {{ 11, 0 },  // horiz=+1, vert=-2
129
   { 11, 0 },  // horiz=+1, vert=-1
130
   { 12, 0 },  // horiz=+1, vert= 0
131
   { 13, 0 },  // horiz=+1, vert=+1
132
   { 13, 0 }}, // horiz=+1, vert=+2
133
  {{ 11, 0 },  // horiz=+2, vert=-2
134
   { 11, 0 },  // horiz=+2, vert=-1
135
   { 12, 0 },  // horiz=+2, vert= 0
136
   { 13, 0 },  // horiz=+2, vert=+1
137
   { 13, 0 }}, // horiz=+2, vert=+2
138
};
139
140
//------------------------------------------------------------------------
141
142
// constants used in the IDWT
143
1.02M
#define idwtAlpha  -1.586134342059924
144
1.07M
#define idwtBeta   -0.052980118572961
145
1.12M
#define idwtGamma   0.882911075530934
146
1.18M
#define idwtDelta   0.443506852043971
147
2.41M
#define idwtKappa   1.230174104914001
148
1.23M
#define idwtIKappa  (1.0 / idwtKappa)
149
150
// sum of the sample size (number of bits) and the number of bits to
151
// the right of the decimal point for the fixed point arithmetic used
152
// in the IDWT
153
11.2k
#define fracBits 24
154
155
//------------------------------------------------------------------------
156
157
// floor(x / y)
158
1.42M
#define jpxFloorDiv(x, y) ((x) / (y))
159
160
// floor(x / 2^y)
161
79.6k
#define jpxFloorDivPow2(x, y) ((x) >> (y))
162
163
// ceil(x / y)
164
5.90k
#define jpxCeilDiv(x, y) (((x) + (y) - 1) / (y))
165
166
// ceil(x / 2^y)
167
715k
#define jpxCeilDivPow2(x, y) (((x) + (1 << (y)) - 1) >> (y))
168
169
//------------------------------------------------------------------------
170
171
#if 1 //----- disable coverage tracking
172
173
#define cover(idx)
174
175
#else //----- enable coverage tracking
176
177
class JPXCover {
178
public:
179
180
  JPXCover(int sizeA);
181
  ~JPXCover();
182
  void incr(int idx);
183
184
private:
185
186
  int size, used;
187
  int *data;
188
};
189
190
JPXCover::JPXCover(int sizeA) {
191
  size = sizeA;
192
  used = -1;
193
  data = (int *)gmallocn(size, sizeof(int));
194
  memset(data, 0, size * sizeof(int));
195
}
196
197
JPXCover::~JPXCover() {
198
  int i;
199
200
  printf("JPX coverage:\n");
201
  for (i = 0; i <= used; ++i) {
202
    printf("  %4d: %8d\n", i, data[i]);
203
  }
204
  gfree(data);
205
}
206
207
void JPXCover::incr(int idx) {
208
  if (idx < size) {
209
    ++data[idx];
210
    if (idx > used) {
211
      used = idx;
212
    }
213
  }
214
}
215
216
JPXCover jpxCover(150);
217
218
#define cover(idx) jpxCover.incr(idx)
219
220
#endif //----- coverage tracking
221
222
//------------------------------------------------------------------------
223
224
JPXStream::JPXStream(Stream *strA):
225
12.3k
  FilterStream(strA)
226
12.3k
{
227
12.3k
  bufStr = new BufStream(str, 3);
228
229
12.3k
  decoded = gFalse;
230
12.3k
  nComps = 0;
231
12.3k
  bpc = NULL;
232
12.3k
  width = height = 0;
233
12.3k
  reduction = 0;
234
12.3k
  haveCS = gFalse;
235
236
12.3k
  palette.bpc = NULL;
237
12.3k
  palette.c = NULL;
238
12.3k
  havePalette = gFalse;
239
240
12.3k
  compMap.comp = NULL;
241
12.3k
  compMap.type = NULL;
242
12.3k
  compMap.pComp = NULL;
243
12.3k
  haveCompMap = gFalse;
244
245
12.3k
  channelDefn.idx = NULL;
246
12.3k
  channelDefn.type = NULL;
247
12.3k
  channelDefn.assoc = NULL;
248
12.3k
  haveChannelDefn = gFalse;
249
250
12.3k
  img.tiles = NULL;
251
252
12.3k
  bitBuf = 0;
253
12.3k
  bitBufLen = 0;
254
12.3k
  bitBufSkip = gFalse;
255
12.3k
  byteCount = 0;
256
12.3k
}
257
258
12.2k
JPXStream::~JPXStream() {
259
12.2k
  close();
260
12.2k
  delete bufStr;
261
12.2k
}
262
263
10.1k
Stream *JPXStream::copy() {
264
10.1k
  return new JPXStream(str->copy());
265
10.1k
}
266
267
2.99k
void JPXStream::reset() {
268
2.99k
  img.ySize = 0;
269
2.99k
  bufStr->reset();
270
2.99k
  decoded = gFalse;
271
2.99k
}
272
273
15.2k
void JPXStream::close() {
274
15.2k
  JPXTile *tile;
275
15.2k
  JPXTileComp *tileComp;
276
15.2k
  JPXResLevel *resLevel;
277
15.2k
  JPXPrecinct *precinct;
278
15.2k
  JPXSubband *subband;
279
15.2k
  JPXCodeBlock *cb;
280
15.2k
  Guint comp, i, k, r, pre, sb;
281
282
15.2k
  gfree(bpc);
283
15.2k
  bpc = NULL;
284
15.2k
  if (havePalette) {
285
0
    gfree(palette.bpc);
286
0
    gfree(palette.c);
287
0
    havePalette = gFalse;
288
0
  }
289
15.2k
  if (haveCompMap) {
290
0
    gfree(compMap.comp);
291
0
    gfree(compMap.type);
292
0
    gfree(compMap.pComp);
293
0
    haveCompMap = gFalse;
294
0
  }
295
15.2k
  if (haveChannelDefn) {
296
10
    gfree(channelDefn.idx);
297
10
    gfree(channelDefn.type);
298
10
    gfree(channelDefn.assoc);
299
10
    haveChannelDefn = gFalse;
300
10
  }
301
302
15.2k
  if (img.tiles) {
303
27.5k
    for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
304
25.9k
      tile = &img.tiles[i];
305
25.9k
      if (tile->tileComps) {
306
4.68M
  for (comp = 0; comp < img.nComps; ++comp) {
307
4.65M
    tileComp = &tile->tileComps[comp];
308
4.65M
    gfree(tileComp->quantSteps);
309
4.65M
    gfree(tileComp->data);
310
4.65M
    gfree(tileComp->buf);
311
4.65M
    if (tileComp->resLevels) {
312
202k
      for (r = 0; r <= tileComp->nDecompLevels; ++r) {
313
136k
        resLevel = &tileComp->resLevels[r];
314
136k
        if (resLevel->precincts) {
315
20.5k
    for (pre = 0; pre < resLevel->nPrecincts; ++pre) {
316
14.1k
      precinct = &resLevel->precincts[pre];
317
14.1k
      if (precinct->subbands) {
318
47.5k
        for (sb = 0; sb < (Guint)(r == 0 ? 1 : 3); ++sb) {
319
33.3k
          subband = &precinct->subbands[sb];
320
33.3k
          gfree(subband->inclusion);
321
33.3k
          gfree(subband->zeroBitPlane);
322
33.3k
          if (subband->cbs) {
323
180k
      for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) {
324
149k
        cb = &subband->cbs[k];
325
149k
        gfree(cb->dataLen);
326
149k
        gfree(cb->touched);
327
149k
        if (cb->arithDecoder) {
328
14.1k
          delete cb->arithDecoder;
329
14.1k
        }
330
149k
        if (cb->stats) {
331
14.1k
          delete cb->stats;
332
14.1k
        }
333
149k
      }
334
30.5k
      gfree(subband->cbs);
335
30.5k
          }
336
33.3k
        }
337
14.1k
        gfree(precinct->subbands);
338
14.1k
      }
339
14.1k
    }
340
6.38k
    gfree(resLevel->precincts);
341
6.38k
        }
342
136k
      }
343
66.0k
      gfree(tileComp->resLevels);
344
66.0k
    }
345
4.65M
  }
346
25.9k
  gfree(tile->tileComps);
347
25.9k
      }
348
25.9k
    }
349
1.59k
    gfree(img.tiles);
350
1.59k
    img.tiles = NULL;
351
1.59k
  }
352
15.2k
  bufStr->close();
353
15.2k
}
354
355
2.44k
void JPXStream::decodeImage() {
356
2.44k
  if (readBoxes() == jpxDecodeFatalError) {
357
    // readBoxes reported an error, so we go immediately to EOF
358
1.85k
    curY = img.ySize >> reduction;
359
1.85k
  } else {
360
588
    curY = img.yOffsetR;
361
588
  }
362
2.44k
  curX = img.xOffsetR;
363
2.44k
  curComp = 0;
364
2.44k
  readBufLen = 0;
365
2.44k
  decoded = gTrue;
366
2.44k
}
367
368
8.82M
int JPXStream::getChar() {
369
8.82M
  int c;
370
371
8.82M
  if (!decoded) {
372
2.44k
    decodeImage();
373
2.44k
  }
374
8.82M
  if (readBufLen < 8) {
375
21.1k
    fillReadBuf();
376
21.1k
  }
377
8.82M
  if (readBufLen == 8) {
378
681k
    c = readBuf & 0xff;
379
681k
    readBufLen = 0;
380
8.14M
  } else if (readBufLen > 8) {
381
8.13M
    c = (readBuf >> (readBufLen - 8)) & 0xff;
382
8.13M
    readBufLen -= 8;
383
8.13M
  } else if (readBufLen == 0) {
384
14.1k
    c = EOF;
385
14.1k
  } else {
386
1
    c = (readBuf << (8 - readBufLen)) & 0xff;
387
1
    readBufLen = 0;
388
1
  }
389
8.82M
  return c;
390
8.82M
}
391
392
8.74M
int JPXStream::lookChar() {
393
8.74M
  int c;
394
395
8.74M
  if (!decoded) {
396
0
    decodeImage();
397
0
  }
398
8.74M
  if (readBufLen < 8) {
399
686k
    fillReadBuf();
400
686k
  }
401
8.74M
  if (readBufLen == 8) {
402
675k
    c = readBuf & 0xff;
403
8.06M
  } else if (readBufLen > 8) {
404
8.06M
    c = (readBuf >> (readBufLen - 8)) & 0xff;
405
8.06M
  } else if (readBufLen == 0) {
406
10
    c = EOF;
407
10
  } else {
408
0
    c = (readBuf << (8 - readBufLen)) & 0xff;
409
0
  }
410
8.74M
  return c;
411
8.74M
}
412
413
707k
void JPXStream::fillReadBuf() {
414
707k
  JPXTileComp *tileComp;
415
707k
  Guint tileIdx, tx, ty;
416
707k
  int pix, pixBits, k;
417
707k
  GBool eol;
418
419
725k
  do {
420
725k
    if (curY >= (img.ySize >> reduction)) {
421
14.1k
      return;
422
14.1k
    }
423
711k
    tileIdx = (((curY << reduction) - img.yTileOffset) / img.yTileSize)
424
711k
                * img.nXTiles
425
711k
              + ((curX << reduction) - img.xTileOffset) / img.xTileSize;
426
711k
#if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
427
711k
    tileComp = &img.tiles[tileIdx].tileComps[curComp];
428
#else
429
    tileComp = &img.tiles[tileIdx].tileComps[havePalette ? 0 : curComp];
430
#endif
431
711k
    tx = jpxFloorDiv(curX, tileComp->hSep);
432
711k
    if (tx < tileComp->x0r) {
433
15.5k
      tx = 0;
434
696k
    } else {
435
696k
      tx -= tileComp->x0r;
436
696k
    }
437
711k
    ty = jpxFloorDiv(curY, tileComp->vSep);
438
711k
    if (ty < tileComp->y0r) {
439
24.8k
      ty  = 0;
440
686k
    } else {
441
686k
      ty -= tileComp->y0r;
442
686k
    }
443
711k
    pix = (int)tileComp->data[ty * tileComp->w + tx];
444
711k
    pixBits = tileComp->prec;
445
711k
    eol = gFalse;
446
711k
#if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
447
711k
    if (++curComp == img.nComps) {
448
#else
449
    if (havePalette) {
450
      if (pix >= 0 && pix < palette.nEntries) {
451
  pix = palette.c[pix * palette.nComps + curComp];
452
      } else {
453
  pix = 0;
454
      }
455
      pixBits = palette.bpc[curComp];
456
    }
457
    if (++curComp == (Guint)(havePalette ? palette.nComps : img.nComps)) {
458
#endif
459
688k
      curComp = 0;
460
688k
      if (++curX == (img.xSize >> reduction)) {
461
2.94k
  curX = img.xOffsetR;
462
2.94k
  ++curY;
463
2.94k
  eol = gTrue;
464
2.94k
      }
465
688k
    }
466
711k
    if (pixBits == 8) {
467
11.9k
      readBuf = (readBuf << 8) | (pix & 0xff);
468
699k
    } else {
469
699k
      readBuf = (readBuf << pixBits) | (pix & ((1 << pixBits) - 1));
470
699k
    }
471
711k
    readBufLen += pixBits;
472
711k
    if (eol && (k = readBufLen & 7)) {
473
954
      readBuf <<= 8 - k;
474
954
      readBufLen += 8 - k;
475
954
    }
476
711k
  } while (readBufLen < 8);
477
707k
}
478
479
GString *JPXStream::getPSFilter(int psLevel, const char *indent,
480
0
        GBool okToReadStream) {
481
0
  return NULL;
482
0
}
483
484
0
GBool JPXStream::isBinary(GBool last) {
485
0
  return str->isBinary(gTrue);
486
0
}
487
488
void JPXStream::getImageParams(int *bitsPerComponent,
489
1.81k
             StreamColorSpaceMode *csMode) {
490
1.81k
  Guint boxType, boxLen, dataLen, csEnum;
491
1.81k
  Guint bpc1, dummy;
492
1.81k
  int csMeth, csPrec, csPrec1, dummy2;
493
1.81k
  StreamColorSpaceMode csMode1;
494
1.81k
  GBool haveBPC, haveCSMode;
495
496
1.81k
  csPrec = 0; // make gcc happy
497
1.81k
  haveBPC = haveCSMode = gFalse;
498
1.81k
  bufStr->reset();
499
1.81k
  if (bufStr->lookChar() == 0xff) {
500
898
    getImageParams2(bitsPerComponent, csMode);
501
914
  } else {
502
4.64k
    while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
503
3.84k
      if (boxType == 0x6a703268) { // JP2 header
504
596
  cover(0);
505
  // skip the superbox
506
3.24k
      } else if (boxType == 0x69686472) { // image header
507
561
  cover(1);
508
561
  if (readULong(&dummy) &&
509
479
      readULong(&dummy) &&
510
395
      readUWord(&dummy) &&
511
313
      readUByte(&bpc1) &&
512
293
      readUByte(&dummy) &&
513
252
      readUByte(&dummy) &&
514
219
      readUByte(&dummy)) {
515
213
    *bitsPerComponent = bpc1 + 1;
516
213
    haveBPC = gTrue;
517
213
  }
518
2.68k
      } else if (boxType == 0x636F6C72) { // color specification
519
248
  cover(2);
520
248
  if (readByte(&csMeth) &&
521
225
      readByte(&csPrec1) &&
522
225
      readByte(&dummy2)) {
523
202
    if (csMeth == 1) {
524
173
      if (readULong(&csEnum)) {
525
105
        csMode1 = streamCSNone;
526
105
        if (csEnum == jpxCSBiLevel ||
527
76
      csEnum == jpxCSGrayscale) {
528
29
    csMode1 = streamCSDeviceGray;
529
76
        } else if (csEnum == jpxCSCMYK) {
530
0
    csMode1 = streamCSDeviceCMYK;
531
76
        } else if (csEnum == jpxCSsRGB ||
532
49
       csEnum == jpxCSCISesRGB ||
533
49
       csEnum == jpxCSROMMRGB) {
534
27
    csMode1 = streamCSDeviceRGB;
535
27
        }
536
105
        if (csMode1 != streamCSNone &&
537
56
      (!haveCSMode || csPrec1 > csPrec)) {
538
56
    *csMode = csMode1;
539
56
    csPrec = csPrec1;
540
56
    haveCSMode = gTrue;
541
56
        }
542
105
        if (dataLen > 7) {
543
29
    bufStr->discardChars(dataLen - 7);
544
29
        }
545
105
      }
546
173
    } else {
547
29
      if (dataLen > 3) {
548
11
        bufStr->discardChars(dataLen - 3);
549
11
      }
550
29
    }
551
202
  }
552
2.43k
      } else if (boxType == 0x6A703263) { // codestream
553
113
  cover(3);
554
113
  if (!(haveBPC && haveCSMode)) {
555
112
    getImageParams2(bitsPerComponent, csMode);
556
112
  }
557
113
  break;
558
2.32k
      } else {
559
2.32k
  cover(4);
560
2.32k
  bufStr->discardChars(dataLen);
561
2.32k
      }
562
3.84k
    }
563
914
  }
564
1.81k
  bufStr->close();
565
1.81k
}
566
567
// Get image parameters from the codestream.
568
void JPXStream::getImageParams2(int *bitsPerComponent,
569
1.01k
        StreamColorSpaceMode *csMode) {
570
1.01k
  int segType;
571
1.01k
  Guint segLen, nComps1, bpc1, dummy;
572
573
2.46k
  while (readMarkerHdr(&segType, &segLen)) {
574
2.32k
    if (segType == 0x51) { // SIZ - image and tile size
575
871
      cover(5);
576
871
      if (readUWord(&dummy) &&
577
851
    readULong(&dummy) &&
578
784
    readULong(&dummy) &&
579
655
    readULong(&dummy) &&
580
551
    readULong(&dummy) &&
581
446
    readULong(&dummy) &&
582
318
    readULong(&dummy) &&
583
216
    readULong(&dummy) &&
584
196
    readULong(&dummy) &&
585
157
    readUWord(&nComps1) &&
586
104
    readUByte(&bpc1)) {
587
82
  *bitsPerComponent = (bpc1 & 0x7f) + 1;
588
  // if there's no color space info, take a guess
589
82
  if (nComps1 == 1) {
590
19
    *csMode = streamCSDeviceGray;
591
63
  } else if (nComps1 == 3) {
592
17
    *csMode = streamCSDeviceRGB;
593
46
  } else if (nComps1 == 4) {
594
0
    *csMode = streamCSDeviceCMYK;
595
0
  }
596
82
      }
597
871
      break;
598
1.45k
    } else {
599
1.45k
      cover(6);
600
1.45k
      if (segLen > 2) {
601
283
  bufStr->discardChars(segLen - 2);
602
283
      }
603
1.45k
    }
604
2.32k
  }
605
1.01k
}
606
607
2.44k
JPXDecodeResult JPXStream::readBoxes() {
608
2.44k
  JPXDecodeResult result;
609
2.44k
  GBool haveCodestream;
610
2.44k
  Guint boxType, boxLen, dataLen;
611
2.44k
  Guint w, h, n, bpc1, compression, unknownColorspace, ipr;
612
2.44k
  Guint i, j;
613
614
2.44k
  haveImgHdr = gFalse;
615
2.44k
  haveCodestream = gFalse;
616
617
  // check for a naked JPEG 2000 codestream (without the JP2/JPX
618
  // wrapper) -- this appears to be a violation of the PDF spec, but
619
  // Acrobat allows it
620
2.44k
  if (bufStr->lookChar() == 0xff) {
621
2.01k
    cover(7);
622
2.01k
    error(errSyntaxWarning, getPos(),
623
2.01k
    "Naked JPEG 2000 codestream, missing JP2/JPX wrapper");
624
2.01k
    if ((result = readCodestream(0)) == jpxDecodeFatalError) {
625
1.57k
      return result;
626
1.57k
    }
627
437
    nComps = img.nComps;
628
437
    bpc = (Guint *)gmallocn(nComps, sizeof(Guint));
629
1.71k
    for (i = 0; i < nComps; ++i) {
630
1.28k
      bpc[i] = img.tiles[0].tileComps[i].prec;
631
1.28k
    }
632
437
    width = img.xSize - img.xOffset;
633
437
    height = img.ySize - img.yOffset;
634
437
    return result;
635
2.01k
  }
636
637
1.25k
  while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
638
1.10k
    switch (boxType) {
639
188
    case 0x6a703268:    // JP2 header
640
      // this is a grouping box ('superbox') which has no real
641
      // contents and doesn't appear to be used consistently, i.e.,
642
      // some things which should be subboxes of the JP2 header box
643
      // show up outside of it - so we simply ignore the JP2 header
644
      // box
645
188
      cover(8);
646
188
      break;
647
177
    case 0x69686472:    // image header
648
177
      cover(9);
649
177
      if (!readULong(&h) ||
650
153
    !readULong(&w) ||
651
122
    !readUWord(&n) ||
652
109
    !readUByte(&bpc1) ||
653
104
    !readUByte(&compression) ||
654
98
    !readUByte(&unknownColorspace) ||
655
110
    !readUByte(&ipr)) {
656
110
  error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
657
110
  return jpxDecodeFatalError;
658
110
      }
659
67
      if (compression != 7) {
660
62
  error(errSyntaxError, getPos(),
661
62
        "Unknown compression type in JPX stream");
662
62
  return jpxDecodeFatalError;
663
62
      }
664
5
      height = h;
665
5
      width = w;
666
5
      nComps = n;
667
5
      bpc = (Guint *)gmallocn(nComps, sizeof(Guint));
668
20
      for (i = 0; i < nComps; ++i) {
669
15
  bpc[i] = bpc1;
670
15
      }
671
5
      haveImgHdr = gTrue;
672
5
      break;
673
0
    case 0x62706363:    // bits per component
674
0
      cover(10);
675
0
      if (!haveImgHdr) {
676
0
  error(errSyntaxError, getPos(),
677
0
        "Found bits per component box before image header box in JPX stream");
678
0
  return jpxDecodeFatalError;
679
0
      }
680
0
      if (dataLen != nComps) {
681
0
  error(errSyntaxError, getPos(),
682
0
        "Invalid bits per component box in JPX stream");
683
0
  return jpxDecodeFatalError;
684
0
      }
685
0
      for (i = 0; i < nComps; ++i) {
686
0
  if (!readUByte(&bpc[i])) {
687
0
    error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
688
0
    return jpxDecodeFatalError;
689
0
  }
690
0
      }
691
0
      break;
692
16
    case 0x636F6C72:    // color specification
693
16
      cover(11);
694
16
      if (!readColorSpecBox(dataLen)) {
695
2
  return jpxDecodeFatalError;
696
2
      }
697
14
      break;
698
14
    case 0x70636c72:    // palette
699
0
      cover(12);
700
0
      if (!readUWord(&palette.nEntries) ||
701
0
    !readUByte(&palette.nComps)) {
702
0
  error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
703
0
  return jpxDecodeFatalError;
704
0
      }
705
0
      havePalette = gTrue;
706
0
      palette.bpc = (Guint *)gmallocn(palette.nComps, sizeof(Guint));
707
0
      palette.c =
708
0
          (int *)gmallocn(palette.nEntries * palette.nComps, sizeof(int));
709
0
      for (i = 0; i < palette.nComps; ++i) {
710
0
  if (!readUByte(&palette.bpc[i])) {
711
0
    error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
712
0
    return jpxDecodeFatalError;
713
0
  }
714
0
  ++palette.bpc[i];
715
0
      }
716
0
      for (i = 0; i < palette.nEntries; ++i) {
717
0
  for (j = 0; j < palette.nComps; ++j) {
718
0
    if (!readNBytes(((palette.bpc[j] & 0x7f) + 7) >> 3,
719
0
        (palette.bpc[j] & 0x80) ? gTrue : gFalse,
720
0
        &palette.c[i * palette.nComps + j])) {
721
0
      error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
722
0
      return jpxDecodeFatalError;
723
0
    }
724
0
  }
725
0
      }
726
0
      break;
727
0
    case 0x636d6170:    // component mapping
728
0
      cover(13);
729
0
      haveCompMap = gTrue;
730
0
      compMap.nChannels = dataLen / 4;
731
0
      compMap.comp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
732
0
      compMap.type = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
733
0
      compMap.pComp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
734
0
      for (i = 0; i < compMap.nChannels; ++i) {
735
0
  if (!readUWord(&compMap.comp[i]) ||
736
0
      !readUByte(&compMap.type[i]) ||
737
0
      !readUByte(&compMap.pComp[i])) {
738
0
    error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
739
0
    return jpxDecodeFatalError;
740
0
  }
741
0
      }
742
0
      break;
743
10
    case 0x63646566:    // channel definition
744
10
      cover(14);
745
10
      if (!readUWord(&channelDefn.nChannels)) {
746
0
  error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
747
0
  return jpxDecodeFatalError;
748
0
      }
749
10
      haveChannelDefn = gTrue;
750
10
      channelDefn.idx =
751
10
    (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
752
10
      channelDefn.type =
753
10
    (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
754
10
      channelDefn.assoc =
755
10
    (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
756
5.58k
      for (i = 0; i < channelDefn.nChannels; ++i) {
757
5.58k
  if (!readUWord(&channelDefn.idx[i]) ||
758
5.58k
      !readUWord(&channelDefn.type[i]) ||
759
5.58k
      !readUWord(&channelDefn.assoc[i])) {
760
6
    error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
761
6
    return jpxDecodeFatalError;
762
6
  }
763
5.58k
      }
764
4
      break;
765
13
    case 0x6A703263:    // contiguous codestream
766
13
      cover(15);
767
13
      if (!bpc) {
768
10
  error(errSyntaxError, getPos(),
769
10
        "JPX stream is missing the image header box");
770
10
      }
771
13
      if (!haveCS) {
772
0
  error(errSyntaxError, getPos(),
773
0
        "JPX stream has no supported color spec");
774
0
      }
775
13
      if ((result = readCodestream(dataLen)) != jpxDecodeOk) {
776
12
  return result;
777
12
      }
778
1
      haveCodestream = gTrue;
779
1
      break;
780
696
    default:
781
696
      cover(16);
782
696
      if (bufStr->discardChars(dataLen) != dataLen) {
783
89
  error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
784
89
  return haveCodestream ? jpxDecodeNonFatalError : jpxDecodeFatalError;
785
89
      }
786
607
      break;
787
1.10k
    }
788
1.10k
  }
789
150
  return jpxDecodeOk;
790
432
}
791
792
16
GBool JPXStream::readColorSpecBox(Guint dataLen) {
793
16
  JPXColorSpec newCS;
794
16
  Guint csApprox, csEnum;
795
16
  GBool ok;
796
797
16
  ok = gFalse;
798
16
  if (!readUByte(&newCS.meth) ||
799
16
      !readByte(&newCS.prec) ||
800
16
      !readUByte(&csApprox)) {
801
0
    goto err;
802
0
  }
803
16
  switch (newCS.meth) {
804
16
  case 1:     // enumerated colorspace
805
16
    cover(17);
806
16
    if (!readULong(&csEnum)) {
807
0
      goto err;
808
0
    }
809
16
    newCS.enumerated.type = (JPXColorSpaceType)csEnum;
810
16
    switch (newCS.enumerated.type) {
811
5
    case jpxCSBiLevel:
812
5
      ok = gTrue;
813
5
      break;
814
0
    case jpxCSYCbCr1:
815
0
      ok = gTrue;
816
0
      break;
817
0
    case jpxCSYCbCr2:
818
0
      ok = gTrue;
819
0
      break;
820
0
    case jpxCSYCBCr3:
821
0
      ok = gTrue;
822
0
      break;
823
0
    case jpxCSPhotoYCC:
824
0
      ok = gTrue;
825
0
      break;
826
0
    case jpxCSCMY:
827
0
      ok = gTrue;
828
0
      break;
829
0
    case jpxCSCMYK:
830
0
      ok = gTrue;
831
0
      break;
832
0
    case jpxCSYCCK:
833
0
      ok = gTrue;
834
0
      break;
835
0
    case jpxCSCIELab:
836
0
      if (dataLen == 7 + 7*4) {
837
0
  if (!readULong(&newCS.enumerated.cieLab.rl) ||
838
0
      !readULong(&newCS.enumerated.cieLab.ol) ||
839
0
      !readULong(&newCS.enumerated.cieLab.ra) ||
840
0
      !readULong(&newCS.enumerated.cieLab.oa) ||
841
0
      !readULong(&newCS.enumerated.cieLab.rb) ||
842
0
      !readULong(&newCS.enumerated.cieLab.ob) ||
843
0
      !readULong(&newCS.enumerated.cieLab.il)) {
844
0
    goto err;
845
0
  }
846
0
      } else if (dataLen == 7) {
847
  //~ this assumes the 8-bit case
848
0
  cover(92);
849
0
  newCS.enumerated.cieLab.rl = 100;
850
0
  newCS.enumerated.cieLab.ol = 0;
851
0
  newCS.enumerated.cieLab.ra = 255;
852
0
  newCS.enumerated.cieLab.oa = 128;
853
0
  newCS.enumerated.cieLab.rb = 255;
854
0
  newCS.enumerated.cieLab.ob = 96;
855
0
  newCS.enumerated.cieLab.il = 0x00443530;
856
0
      } else {
857
0
  goto err;
858
0
      }
859
0
      ok = gTrue;
860
0
      break;
861
9
    case jpxCSsRGB:
862
9
      ok = gTrue;
863
9
      break;
864
0
    case jpxCSGrayscale:
865
0
      ok = gTrue;
866
0
      break;
867
0
    case jpxCSBiLevel2:
868
0
      ok = gTrue;
869
0
      break;
870
0
    case jpxCSCIEJab:
871
      // not allowed in PDF
872
0
      goto err;
873
0
    case jpxCSCISesRGB:
874
0
      ok = gTrue;
875
0
      break;
876
0
    case jpxCSROMMRGB:
877
0
      ok = gTrue;
878
0
      break;
879
0
    case jpxCSsRGBYCbCr:
880
0
      ok = gTrue;
881
0
      break;
882
0
    case jpxCSYPbPr1125:
883
0
      ok = gTrue;
884
0
      break;
885
0
    case jpxCSYPbPr1250:
886
0
      ok = gTrue;
887
0
      break;
888
2
    default:
889
2
      goto err;
890
16
    }
891
14
    break;
892
14
  case 2:     // restricted ICC profile
893
0
  case 3:       // any ICC profile (JPX)
894
0
  case 4:     // vendor color (JPX)
895
0
    cover(18);
896
0
    if (dataLen > 3 &&
897
0
  bufStr->discardChars(dataLen - 3) != dataLen - 3) {
898
0
      goto err;
899
0
    }
900
0
    break;
901
16
  }
902
903
14
  if (ok && (!haveCS || newCS.prec > cs.prec)) {
904
14
    cs = newCS;
905
14
    haveCS = gTrue;
906
14
  }
907
908
14
  return gTrue;
909
910
2
 err:
911
2
  error(errSyntaxError, getPos(), "Error in JPX color spec");
912
2
  return gFalse;
913
16
}
914
915
2.02k
JPXDecodeResult JPXStream::readCodestream(Guint len) {
916
2.02k
  JPXTile *tile;
917
2.02k
  JPXTileComp *tileComp;
918
2.02k
  int segType;
919
2.02k
  GBool haveSIZ, haveCOD, haveQCD, haveSOT, ok;
920
2.02k
  Guint style, progOrder, nLayers, multiComp, nDecompLevels;
921
2.02k
  Guint codeBlockW, codeBlockH, codeBlockStyle, transform;
922
2.02k
  Guint precinctSize;
923
2.02k
  Guint segLen, capabilities, comp, i, j, r;
924
925
  //----- main header
926
2.02k
  haveSIZ = haveCOD = haveQCD = haveSOT = gFalse;
927
9.44k
  do {
928
9.44k
    if (!readMarkerHdr(&segType, &segLen)) {
929
224
      error(errSyntaxError, getPos(), "Error in JPX codestream");
930
224
      return jpxDecodeFatalError;
931
224
    }
932
9.22k
    switch (segType) {
933
1.82k
    case 0x4f:      // SOC - start of codestream
934
      // marker only
935
1.82k
      cover(19);
936
1.82k
      break;
937
1.93k
    case 0x51:      // SIZ - image and tile size
938
1.93k
      cover(20);
939
1.93k
      if (haveSIZ) {
940
8
  error(errSyntaxError, getPos(),
941
8
        "Duplicate SIZ marker segment in JPX stream");
942
8
  return jpxDecodeFatalError;
943
8
      }
944
1.92k
      if (!readUWord(&capabilities) ||
945
1.92k
    !readULong(&img.xSize) ||
946
1.90k
    !readULong(&img.ySize) ||
947
1.88k
    !readULong(&img.xOffset) ||
948
1.86k
    !readULong(&img.yOffset) ||
949
1.83k
    !readULong(&img.xTileSize) ||
950
1.78k
    !readULong(&img.yTileSize) ||
951
1.76k
    !readULong(&img.xTileOffset) ||
952
1.75k
    !readULong(&img.yTileOffset) ||
953
1.74k
    !readUWord(&img.nComps)) {
954
194
  error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
955
194
  return jpxDecodeFatalError;
956
194
      }
957
1.73k
      if (haveImgHdr && img.nComps != nComps) {
958
0
  error(errSyntaxError, getPos(),
959
0
        "Different number of components in JPX SIZ marker segment");
960
0
  return jpxDecodeFatalError;
961
0
      }
962
1.73k
      if (img.xSize == 0 || img.ySize == 0 ||
963
1.73k
    img.xOffset >= img.xSize || img.yOffset >= img.ySize ||
964
1.71k
    img.xTileSize == 0 || img.yTileSize == 0 ||
965
1.71k
    img.xTileOffset > img.xOffset ||
966
1.69k
    img.yTileOffset > img.yOffset ||
967
1.68k
    img.xTileSize + img.xTileOffset <= img.xOffset ||
968
1.68k
    img.yTileSize + img.yTileOffset <= img.yOffset ||
969
1.68k
    img.nComps == 0) {
970
45
  error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
971
45
  return jpxDecodeFatalError;
972
45
      }
973
1.68k
      img.xSizeR = jpxCeilDivPow2(img.xSize, reduction);
974
1.68k
      img.ySizeR = jpxCeilDivPow2(img.ySize, reduction);
975
1.68k
      img.xOffsetR = jpxCeilDivPow2(img.xOffset, reduction);
976
1.68k
      img.yOffsetR = jpxCeilDivPow2(img.yOffset, reduction);
977
1.68k
      img.nXTiles = (img.xSize - img.xTileOffset + img.xTileSize - 1)
978
1.68k
              / img.xTileSize;
979
1.68k
      img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1)
980
1.68k
              / img.yTileSize;
981
      // check for overflow before allocating memory
982
1.68k
      if (img.nXTiles <= 0 || img.nYTiles <= 0 ||
983
1.59k
    img.nXTiles >= INT_MAX / img.nYTiles) {
984
93
  error(errSyntaxError, getPos(),
985
93
        "Bad tile count in JPX SIZ marker segment");
986
93
  return jpxDecodeFatalError;
987
93
      }
988
1.59k
      img.tiles = (JPXTile *)gmallocn(img.nXTiles * img.nYTiles,
989
1.59k
              sizeof(JPXTile));
990
27.5k
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
991
25.9k
  img.tiles[i].init = gFalse;
992
25.9k
  img.tiles[i].nextTilePart = 0;
993
25.9k
  img.tiles[i].tileComps = NULL;
994
25.9k
      }
995
27.5k
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
996
25.9k
  img.tiles[i].tileComps = (JPXTileComp *)gmallocn(img.nComps,
997
25.9k
               sizeof(JPXTileComp));
998
4.68M
  for (comp = 0; comp < img.nComps; ++comp) {
999
4.65M
    img.tiles[i].tileComps[comp].quantSteps = NULL;
1000
4.65M
    img.tiles[i].tileComps[comp].data = NULL;
1001
4.65M
    img.tiles[i].tileComps[comp].buf = NULL;
1002
4.65M
    img.tiles[i].tileComps[comp].resLevels = NULL;
1003
4.65M
  }
1004
25.9k
      }
1005
14.2k
      for (comp = 0; comp < img.nComps; ++comp) {
1006
12.8k
  if (!readUByte(&img.tiles[0].tileComps[comp].prec) ||
1007
12.8k
      !readUByte(&img.tiles[0].tileComps[comp].hSep) ||
1008
12.8k
      !readUByte(&img.tiles[0].tileComps[comp].vSep)) {
1009
156
    error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
1010
156
    return jpxDecodeFatalError;
1011
156
  }
1012
12.7k
  if (img.tiles[0].tileComps[comp].hSep == 0 ||
1013
12.7k
      img.tiles[0].tileComps[comp].vSep == 0) {
1014
28
    error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
1015
28
    return jpxDecodeFatalError;
1016
28
  }
1017
12.6k
  img.tiles[0].tileComps[comp].sgned =
1018
12.6k
      (img.tiles[0].tileComps[comp].prec & 0x80) ? gTrue : gFalse;
1019
12.6k
  img.tiles[0].tileComps[comp].prec =
1020
12.6k
      (img.tiles[0].tileComps[comp].prec & 0x7f) + 1;
1021
78.5k
  for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
1022
65.8k
    img.tiles[i].tileComps[comp] = img.tiles[0].tileComps[comp];
1023
65.8k
  }
1024
12.6k
      }
1025
1.41k
      haveSIZ = gTrue;
1026
1.41k
      break;
1027
1.28k
    case 0x52:      // COD - coding style default
1028
1.28k
      cover(21);
1029
1.28k
      if (!haveSIZ) {
1030
5
  error(errSyntaxError, getPos(),
1031
5
        "JPX COD marker segment before SIZ segment");
1032
5
  return jpxDecodeFatalError;
1033
5
      }
1034
1.28k
      if (!readUByte(&style) ||
1035
1.28k
    !readUByte(&progOrder) ||
1036
1.28k
    !readUWord(&nLayers) ||
1037
1.28k
    !readUByte(&multiComp) ||
1038
1.28k
    !readUByte(&nDecompLevels) ||
1039
1.28k
    !readUByte(&codeBlockW) ||
1040
1.28k
    !readUByte(&codeBlockH) ||
1041
1.28k
    !readUByte(&codeBlockStyle) ||
1042
1.28k
    !readUByte(&transform)) {
1043
0
  error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1044
0
  return jpxDecodeFatalError;
1045
0
      }
1046
1.28k
      if (nDecompLevels < 1 ||
1047
1.28k
    nDecompLevels > 31 ||
1048
1.28k
    codeBlockW > 8 ||
1049
1.28k
    codeBlockH > 8) {
1050
0
  error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1051
0
  return jpxDecodeFatalError;
1052
0
      }
1053
1.28k
      codeBlockW += 2;
1054
1.28k
      codeBlockH += 2;
1055
23.3k
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1056
22.0k
  img.tiles[i].progOrder = progOrder;
1057
22.0k
  img.tiles[i].nLayers = nLayers;
1058
22.0k
  img.tiles[i].multiComp = multiComp;
1059
88.0k
  for (comp = 0; comp < img.nComps; ++comp) {
1060
66.0k
    img.tiles[i].tileComps[comp].style = style;
1061
66.0k
    img.tiles[i].tileComps[comp].nDecompLevels = nDecompLevels;
1062
66.0k
    img.tiles[i].tileComps[comp].codeBlockW = codeBlockW;
1063
66.0k
    img.tiles[i].tileComps[comp].codeBlockH = codeBlockH;
1064
66.0k
    img.tiles[i].tileComps[comp].codeBlockStyle = codeBlockStyle;
1065
66.0k
    img.tiles[i].tileComps[comp].transform = transform;
1066
66.0k
    img.tiles[i].tileComps[comp].resLevels =
1067
66.0k
        (JPXResLevel *)gmallocn(nDecompLevels + 1, sizeof(JPXResLevel));
1068
202k
    for (r = 0; r <= nDecompLevels; ++r) {
1069
136k
      img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL;
1070
136k
    }
1071
66.0k
  }
1072
22.0k
      }
1073
5.26k
      for (r = 0; r <= nDecompLevels; ++r) {
1074
3.99k
  if (style & 0x01) {
1075
1.41k
    cover(91);
1076
1.41k
    if (!readUByte(&precinctSize)) {
1077
0
      error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1078
0
      return jpxDecodeFatalError;
1079
0
    }
1080
1.41k
    if (r > 0 && ((precinctSize & 0x0f) == 0 ||
1081
703
      (precinctSize & 0xf0) == 0)) {
1082
3
      error(errSyntaxError, getPos(),
1083
3
      "Invalid precinct size in JPX COD marker segment");
1084
3
      return jpxDecodeFatalError;
1085
3
    }
1086
1.40k
    img.tiles[0].tileComps[0].resLevels[r].precinctWidth =
1087
1.40k
        precinctSize & 0x0f;
1088
1.40k
    img.tiles[0].tileComps[0].resLevels[r].precinctHeight =
1089
1.40k
        (precinctSize >> 4) & 0x0f;
1090
2.57k
  } else {
1091
2.57k
    img.tiles[0].tileComps[0].resLevels[r].precinctWidth = 15;
1092
2.57k
    img.tiles[0].tileComps[0].resLevels[r].precinctHeight = 15;
1093
2.57k
  }
1094
3.99k
      }
1095
23.3k
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1096
88.0k
  for (comp = 0; comp < img.nComps; ++comp) {
1097
66.0k
    if (!(i == 0 && comp == 0)) {
1098
197k
      for (r = 0; r <= nDecompLevels; ++r) {
1099
132k
        img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
1100
132k
      img.tiles[0].tileComps[0].resLevels[r].precinctWidth;
1101
132k
        img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
1102
132k
      img.tiles[0].tileComps[0].resLevels[r].precinctHeight;
1103
132k
      }
1104
64.7k
    }
1105
66.0k
  }
1106
22.0k
      }
1107
1.27k
      haveCOD = gTrue;
1108
1.27k
      break;
1109
1
    case 0x53:      // COC - coding style component
1110
1
      cover(22);
1111
1
      if (!haveCOD) {
1112
1
  error(errSyntaxError, getPos(),
1113
1
        "JPX COC marker segment before COD segment");
1114
1
  return jpxDecodeFatalError;
1115
1
      }
1116
0
      comp = 0;
1117
0
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1118
0
    (img.nComps <= 256 && !readUByte(&comp)) ||
1119
0
    comp >= img.nComps ||
1120
0
    !readUByte(&style) ||
1121
0
    !readUByte(&nDecompLevels) ||
1122
0
    !readUByte(&codeBlockW) ||
1123
0
    !readUByte(&codeBlockH) ||
1124
0
    !readUByte(&codeBlockStyle) ||
1125
0
    !readUByte(&transform)) {
1126
0
  error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
1127
0
  return jpxDecodeFatalError;
1128
0
      }
1129
0
      if (nDecompLevels < 1 ||
1130
0
    nDecompLevels > 31 ||
1131
0
    codeBlockW > 8 ||
1132
0
    codeBlockH > 8) {
1133
0
  error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
1134
0
  return jpxDecodeFatalError;
1135
0
      }
1136
0
      style = (img.tiles[0].tileComps[comp].style & ~1) | (style & 1);
1137
0
      codeBlockW += 2;
1138
0
      codeBlockH += 2;
1139
0
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1140
0
  img.tiles[i].tileComps[comp].style = style;
1141
0
  img.tiles[i].tileComps[comp].nDecompLevels = nDecompLevels;
1142
0
  img.tiles[i].tileComps[comp].codeBlockW = codeBlockW;
1143
0
  img.tiles[i].tileComps[comp].codeBlockH = codeBlockH;
1144
0
  img.tiles[i].tileComps[comp].codeBlockStyle = codeBlockStyle;
1145
0
  img.tiles[i].tileComps[comp].transform = transform;
1146
0
  img.tiles[i].tileComps[comp].resLevels =
1147
0
      (JPXResLevel *)greallocn(
1148
0
         img.tiles[i].tileComps[comp].resLevels,
1149
0
         nDecompLevels + 1,
1150
0
         sizeof(JPXResLevel));
1151
0
  for (r = 0; r <= nDecompLevels; ++r) {
1152
0
    img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL;
1153
0
  }
1154
0
      }
1155
0
      for (r = 0; r <= nDecompLevels; ++r) {
1156
0
  if (style & 0x01) {
1157
0
    if (!readUByte(&precinctSize)) {
1158
0
      error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1159
0
      return jpxDecodeFatalError;
1160
0
    }
1161
0
    if (r > 0 && ((precinctSize & 0x0f) == 0 ||
1162
0
      (precinctSize & 0xf0) == 0)) {
1163
0
      error(errSyntaxError, getPos(),
1164
0
      "Invalid precinct size in JPX COD marker segment");
1165
0
      return jpxDecodeFatalError;
1166
0
    }
1167
0
    img.tiles[0].tileComps[comp].resLevels[r].precinctWidth =
1168
0
        precinctSize & 0x0f;
1169
0
    img.tiles[0].tileComps[comp].resLevels[r].precinctHeight =
1170
0
        (precinctSize >> 4) & 0x0f;
1171
0
  } else {
1172
0
    img.tiles[0].tileComps[comp].resLevels[r].precinctWidth = 15;
1173
0
    img.tiles[0].tileComps[comp].resLevels[r].precinctHeight = 15;
1174
0
  }
1175
0
      }
1176
0
      for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
1177
0
  for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
1178
0
    img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
1179
0
        img.tiles[0].tileComps[comp].resLevels[r].precinctWidth;
1180
0
    img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
1181
0
        img.tiles[0].tileComps[comp].resLevels[r].precinctHeight;
1182
0
  }
1183
0
      }
1184
0
      break;
1185
1.30k
    case 0x5c:      // QCD - quantization default
1186
1.30k
      cover(23);
1187
1.30k
      if (!haveSIZ) {
1188
0
  error(errSyntaxError, getPos(),
1189
0
        "JPX QCD marker segment before SIZ segment");
1190
0
  return jpxDecodeFatalError;
1191
0
      }
1192
1.30k
      if (!readUByte(&img.tiles[0].tileComps[0].quantStyle)) {
1193
0
  error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1194
0
  return jpxDecodeFatalError;
1195
0
      }
1196
1.30k
      if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x00) {
1197
281
  if (segLen <= 3) {
1198
0
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1199
0
    return jpxDecodeFatalError;
1200
0
  }
1201
281
  img.tiles[0].tileComps[0].nQuantSteps = segLen - 3;
1202
281
  img.tiles[0].tileComps[0].quantSteps =
1203
281
      (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1204
281
             img.tiles[0].tileComps[0].nQuantSteps,
1205
281
             sizeof(Guint));
1206
783k
  for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
1207
783k
    if (!readUByte(&img.tiles[0].tileComps[0].quantSteps[i])) {
1208
159
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1209
159
      return jpxDecodeFatalError;
1210
159
    }
1211
783k
  }
1212
1.02k
      } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x01) {
1213
0
  img.tiles[0].tileComps[0].nQuantSteps = 1;
1214
0
  img.tiles[0].tileComps[0].quantSteps =
1215
0
      (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1216
0
             img.tiles[0].tileComps[0].nQuantSteps,
1217
0
             sizeof(Guint));
1218
0
  if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[0])) {
1219
0
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1220
0
    return jpxDecodeFatalError;
1221
0
  }
1222
1.02k
      } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x02) {
1223
1.00k
  if (segLen < 5) {
1224
3
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1225
3
    return jpxDecodeFatalError;
1226
3
  }
1227
999
  img.tiles[0].tileComps[0].nQuantSteps = (segLen - 3) / 2;
1228
999
  img.tiles[0].tileComps[0].quantSteps =
1229
999
      (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1230
999
             img.tiles[0].tileComps[0].nQuantSteps,
1231
999
             sizeof(Guint));
1232
16.9k
  for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
1233
15.9k
    if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[i])) {
1234
0
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1235
0
      return jpxDecodeFatalError;
1236
0
    }
1237
15.9k
  }
1238
999
      } else {
1239
23
  error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1240
23
  return jpxDecodeFatalError;
1241
23
      }
1242
21.9k
      for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1243
83.1k
  for (comp = 0; comp < img.nComps; ++comp) {
1244
62.3k
    if (!(i == 0 && comp == 0)) {
1245
61.2k
      img.tiles[i].tileComps[comp].quantStyle =
1246
61.2k
          img.tiles[0].tileComps[0].quantStyle;
1247
61.2k
      img.tiles[i].tileComps[comp].nQuantSteps =
1248
61.2k
          img.tiles[0].tileComps[0].nQuantSteps;
1249
61.2k
      img.tiles[i].tileComps[comp].quantSteps = 
1250
61.2k
          (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
1251
61.2k
           img.tiles[0].tileComps[0].nQuantSteps,
1252
61.2k
           sizeof(Guint));
1253
1.04M
      for (j = 0; j < img.tiles[0].tileComps[0].nQuantSteps; ++j) {
1254
983k
        img.tiles[i].tileComps[comp].quantSteps[j] =
1255
983k
      img.tiles[0].tileComps[0].quantSteps[j];
1256
983k
      }
1257
61.2k
    }
1258
62.3k
  }
1259
20.8k
      }
1260
1.12k
      haveQCD = gTrue;
1261
1.12k
      break;
1262
0
    case 0x5d:      // QCC - quantization component
1263
0
      cover(24);
1264
0
      if (!haveQCD) {
1265
0
  error(errSyntaxError, getPos(),
1266
0
        "JPX QCC marker segment before QCD segment");
1267
0
  return jpxDecodeFatalError;
1268
0
      }
1269
0
      comp = 0;
1270
0
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1271
0
    (img.nComps <= 256 && !readUByte(&comp)) ||
1272
0
    comp >= img.nComps ||
1273
0
    !readUByte(&img.tiles[0].tileComps[comp].quantStyle)) {
1274
0
  error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1275
0
  return jpxDecodeFatalError;
1276
0
      }
1277
0
      if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x00) {
1278
0
  if (segLen <= (img.nComps > 256 ? 5U : 4U)) {
1279
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1280
0
    return jpxDecodeFatalError;
1281
0
  }
1282
0
  img.tiles[0].tileComps[comp].nQuantSteps =
1283
0
      segLen - (img.nComps > 256 ? 5 : 4);
1284
0
  img.tiles[0].tileComps[comp].quantSteps =
1285
0
      (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1286
0
             img.tiles[0].tileComps[comp].nQuantSteps,
1287
0
             sizeof(Guint));
1288
0
  for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
1289
0
    if (!readUByte(&img.tiles[0].tileComps[comp].quantSteps[i])) {
1290
0
      error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1291
0
      return jpxDecodeFatalError;
1292
0
    }
1293
0
  }
1294
0
      } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x01) {
1295
0
  img.tiles[0].tileComps[comp].nQuantSteps = 1;
1296
0
  img.tiles[0].tileComps[comp].quantSteps =
1297
0
      (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1298
0
             img.tiles[0].tileComps[comp].nQuantSteps,
1299
0
             sizeof(Guint));
1300
0
  if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[0])) {
1301
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1302
0
    return jpxDecodeFatalError;
1303
0
  }
1304
0
      } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x02) {
1305
0
  if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) {
1306
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1307
0
    return jpxDecodeFatalError;
1308
0
  }
1309
0
  img.tiles[0].tileComps[comp].nQuantSteps =
1310
0
      (segLen - (img.nComps > 256 ? 5 : 4)) / 2;
1311
0
  img.tiles[0].tileComps[comp].quantSteps =
1312
0
      (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1313
0
             img.tiles[0].tileComps[comp].nQuantSteps,
1314
0
             sizeof(Guint));
1315
0
  for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
1316
0
    if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[i])) {
1317
0
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1318
0
      return jpxDecodeFatalError;
1319
0
    }
1320
0
  }
1321
0
      } else {
1322
0
  error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1323
0
  return jpxDecodeFatalError;
1324
0
      }
1325
0
      for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
1326
0
  img.tiles[i].tileComps[comp].quantStyle =
1327
0
      img.tiles[0].tileComps[comp].quantStyle;
1328
0
  img.tiles[i].tileComps[comp].nQuantSteps =
1329
0
      img.tiles[0].tileComps[comp].nQuantSteps;
1330
0
  img.tiles[i].tileComps[comp].quantSteps = 
1331
0
      (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
1332
0
             img.tiles[0].tileComps[comp].nQuantSteps,
1333
0
             sizeof(Guint));
1334
0
  for (j = 0; j < img.tiles[0].tileComps[comp].nQuantSteps; ++j) {
1335
0
    img.tiles[i].tileComps[comp].quantSteps[j] =
1336
0
        img.tiles[0].tileComps[comp].quantSteps[j];
1337
0
  }
1338
0
      }
1339
0
      break;
1340
185
    case 0x5e:      // RGN - region of interest
1341
185
      cover(25);
1342
185
#if 1 //~ ROI is unimplemented
1343
185
      error(errUnimplemented, -1, "got a JPX RGN segment");
1344
185
      if (segLen > 2 &&
1345
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1346
0
  error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
1347
0
  return jpxDecodeFatalError;
1348
0
      }
1349
#else
1350
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1351
    (img.nComps <= 256 && !readUByte(&comp)) ||
1352
    comp >= img.nComps ||
1353
    !readUByte(&compInfo[comp].defROI.style) ||
1354
    !readUByte(&compInfo[comp].defROI.shift)) {
1355
  error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
1356
  return jpxDecodeFatalError;
1357
      }
1358
#endif
1359
185
      break;
1360
185
    case 0x5f:      // POC - progression order change
1361
0
      cover(26);
1362
0
#if 1 //~ progression order changes are unimplemented
1363
0
      error(errUnimplemented, -1, "got a JPX POC segment");
1364
0
      if (segLen > 2 &&
1365
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1366
0
  error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
1367
0
  return jpxDecodeFatalError;
1368
0
      }
1369
#else
1370
      nProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
1371
      progs = (JPXProgOrder *)gmallocn(nProgs, sizeof(JPXProgOrder));
1372
      for (i = 0; i < nProgs; ++i) {
1373
  if (!readUByte(&progs[i].startRes) ||
1374
      !(img.nComps > 256 && readUWord(&progs[i].startComp)) ||
1375
      !(img.nComps <= 256 && readUByte(&progs[i].startComp)) ||
1376
      !readUWord(&progs[i].endLayer) ||
1377
      !readUByte(&progs[i].endRes) ||
1378
      !(img.nComps > 256 && readUWord(&progs[i].endComp)) ||
1379
      !(img.nComps <= 256 && readUByte(&progs[i].endComp)) ||
1380
      !readUByte(&progs[i].progOrder)) {
1381
    error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
1382
    return jpxDecodeFatalError;
1383
  }
1384
      }
1385
#endif
1386
0
      break;
1387
0
    case 0x60:      // PPM - packed packet headers, main header
1388
0
      cover(27);
1389
0
#if 1 //~ packed packet headers are unimplemented
1390
0
      error(errUnimplemented, -1, "Got a JPX PPM segment");
1391
0
      if (segLen > 2 &&
1392
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1393
0
  error(errSyntaxError, getPos(), "Error in JPX PPM marker segment");
1394
0
  return jpxDecodeFatalError;
1395
0
      }
1396
0
#endif
1397
0
      break;
1398
11
    case 0x55:      // TLM - tile-part lengths
1399
      // skipped
1400
11
      cover(28);
1401
11
      if (segLen > 2 &&
1402
11
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1403
0
  error(errSyntaxError, getPos(), "Error in JPX TLM marker segment");
1404
0
  return jpxDecodeFatalError;
1405
0
      }
1406
11
      break;
1407
20
    case 0x57:      // PLM - packet length, main header
1408
      // skipped
1409
20
      cover(29);
1410
20
      if (segLen > 2 &&
1411
12
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1412
0
  error(errSyntaxError, getPos(), "Error in JPX PLM marker segment");
1413
0
  return jpxDecodeFatalError;
1414
0
      }
1415
20
      break;
1416
26
    case 0x63:      // CRG - component registration
1417
      // skipped
1418
26
      cover(30);
1419
26
      if (segLen > 2 &&
1420
26
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1421
0
  error(errSyntaxError, getPos(), "Error in JPX CRG marker segment");
1422
0
  return jpxDecodeFatalError;
1423
0
      }
1424
26
      break;
1425
26
    case 0x64:      // COM - comment
1426
      // skipped
1427
0
      cover(31);
1428
0
      if (segLen > 2 &&
1429
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1430
0
  error(errSyntaxError, getPos(), "Error in JPX COM marker segment");
1431
0
  return jpxDecodeFatalError;
1432
0
      }
1433
0
      break;
1434
1.08k
    case 0x90:      // SOT - start of tile
1435
1.08k
      cover(32);
1436
1.08k
      haveSOT = gTrue;
1437
1.08k
      break;
1438
1.53k
    default:
1439
1.53k
      cover(33);
1440
1.53k
      error(errSyntaxError, getPos(),
1441
1.53k
      "Unknown marker segment {0:02x} in JPX stream", segType);
1442
1.53k
      if (segLen > 2) {
1443
212
  bufStr->discardChars(segLen - 2);
1444
212
      }
1445
1.53k
      break;
1446
9.22k
    }
1447
9.22k
  } while (!haveSOT);
1448
1449
1.08k
  if (!haveSIZ) {
1450
0
    error(errSyntaxError, getPos(),
1451
0
    "Missing SIZ marker segment in JPX stream");
1452
0
    return jpxDecodeFatalError;
1453
0
  }
1454
1.08k
  if (!haveCOD) {
1455
0
    error(errSyntaxError, getPos(),
1456
0
    "Missing COD marker segment in JPX stream");
1457
0
    return jpxDecodeFatalError;
1458
0
  }
1459
1.08k
  if (!haveQCD) {
1460
18
    error(errSyntaxError, getPos(),
1461
18
    "Missing QCD marker segment in JPX stream");
1462
18
    return jpxDecodeFatalError;
1463
18
  }
1464
1465
  //----- read the tile-parts
1466
1.06k
  ok = gTrue;
1467
1.30k
  while (1) {
1468
1.30k
    if (!readTilePart()) {
1469
955
      ok = gFalse;
1470
955
      break;
1471
955
    }
1472
348
    if (!readMarkerHdr(&segType, &segLen)) {
1473
55
      error(errSyntaxError, getPos(), "Error in JPX codestream");
1474
55
      ok = gFalse;
1475
55
      break;
1476
55
    }
1477
293
    if (segType != 0x90) { // SOT - start of tile
1478
57
      break;
1479
57
    }
1480
293
  }
1481
1482
1.06k
  if (segType != 0xd9) { // EOC - end of codestream
1483
1.06k
    error(errSyntaxError, getPos(), "Missing EOC marker in JPX codestream");
1484
1.06k
    ok = gFalse;
1485
1.06k
  }
1486
1487
  //----- finish decoding the image
1488
1.50k
  for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1489
1.07k
    tile = &img.tiles[i];
1490
1.07k
    if (!tile->init) {
1491
570
      error(errSyntaxError, getPos(), "Uninitialized tile in JPX codestream");
1492
570
      return jpxDecodeFatalError;
1493
570
    }
1494
1.92k
    for (comp = 0; comp < img.nComps; ++comp) {
1495
1.42k
      tileComp = &tile->tileComps[comp];
1496
1.42k
      inverseTransform(tileComp);
1497
1.42k
    }
1498
500
    if (!inverseMultiCompAndDC(tile)) {
1499
60
      return jpxDecodeFatalError;
1500
60
    }
1501
500
  }
1502
1503
  //~ can free memory below tileComps here, and also tileComp.buf
1504
1505
437
  return ok ? jpxDecodeOk : jpxDecodeNonFatalError;
1506
1.06k
}
1507
1508
1.30k
GBool JPXStream::readTilePart() {
1509
1.30k
  JPXTile *tile;
1510
1.30k
  JPXTileComp *tileComp;
1511
1.30k
  JPXResLevel *resLevel;
1512
1.30k
  JPXPrecinct *precinct;
1513
1.30k
  JPXSubband *subband;
1514
1.30k
  JPXCodeBlock *cb;
1515
1.30k
  int *sbCoeffs;
1516
1.30k
  GBool haveSOD;
1517
1.30k
  Guint tileIdx, tilePartLen, tilePartIdx, nTileParts;
1518
1.30k
  GBool tilePartToEOC;
1519
1.30k
  Guint style, progOrder, nLayers, multiComp, nDecompLevels;
1520
1.30k
  Guint codeBlockW, codeBlockH, codeBlockStyle, transform;
1521
1.30k
  Guint precinctSize, qStyle;
1522
1.30k
  Guint px0, py0, px1, py1;
1523
1.30k
  Guint preCol0, preCol1, preRow0, preRow1, preCol, preRow;
1524
1.30k
  Guint cbCol0, cbCol1, cbRow0, cbRow1, cbX, cbY;
1525
1.30k
  Guint n, nSBs, nx, ny, comp, segLen;
1526
1.30k
  Guint i, j, k, r, pre, sb, cbi, cbj;
1527
1.30k
  int segType, level;
1528
1529
  // process the SOT marker segment
1530
1.30k
  if (!readUWord(&tileIdx) ||
1531
1.30k
      !readULong(&tilePartLen) ||
1532
1.27k
      !readUByte(&tilePartIdx) ||
1533
1.27k
      !readUByte(&nTileParts)) {
1534
33
    error(errSyntaxError, getPos(), "Error in JPX SOT marker segment");
1535
33
    return gFalse;
1536
33
  }
1537
1538
  // check tileIdx and tilePartIdx
1539
  // (this ignores nTileParts, because some encoders get it wrong)
1540
1.27k
  if (tileIdx >= img.nXTiles * img.nYTiles ||
1541
1.27k
      tilePartIdx != img.tiles[tileIdx].nextTilePart ||
1542
1.13k
      (tilePartIdx > 0 && !img.tiles[tileIdx].init) ||
1543
1.13k
      (tilePartIdx == 0 && img.tiles[tileIdx].init)) {
1544
138
    error(errSyntaxError, getPos(), "Weird tile-part header in JPX stream");
1545
138
    return gFalse;
1546
138
  }
1547
1.13k
  ++img.tiles[tileIdx].nextTilePart;
1548
1549
1.13k
  tilePartToEOC = tilePartLen == 0;
1550
1.13k
  tilePartLen -= 12; // subtract size of SOT segment
1551
1552
1.13k
  haveSOD = gFalse;
1553
2.09k
  do {
1554
2.09k
    if (!readMarkerHdr(&segType, &segLen)) {
1555
21
      error(errSyntaxError, getPos(), "Error in JPX tile-part codestream");
1556
21
      return gFalse;
1557
21
    }
1558
2.07k
    tilePartLen -= 2 + segLen;
1559
2.07k
    switch (segType) {
1560
543
    case 0x52:      // COD - coding style default
1561
543
      cover(34);
1562
543
      if (tilePartIdx != 0) {
1563
169
  error(errSyntaxError, getPos(), "Extraneous JPX COD marker segment");
1564
169
  return gFalse;
1565
169
      }
1566
374
      if (!readUByte(&style) ||
1567
374
    !readUByte(&progOrder) ||
1568
374
    !readUWord(&nLayers) ||
1569
374
    !readUByte(&multiComp) ||
1570
374
    !readUByte(&nDecompLevels) ||
1571
374
    !readUByte(&codeBlockW) ||
1572
374
    !readUByte(&codeBlockH) ||
1573
374
    !readUByte(&codeBlockStyle) ||
1574
374
    !readUByte(&transform)) {
1575
53
  error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1576
53
  return gFalse;
1577
53
      }
1578
321
      if (nDecompLevels < 1 ||
1579
321
    nDecompLevels > 31 ||
1580
321
    codeBlockW > 8 ||
1581
321
    codeBlockH > 8) {
1582
0
  error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1583
0
  return gFalse;
1584
0
      }
1585
321
      codeBlockW += 2;
1586
321
      codeBlockH += 2;
1587
321
      img.tiles[tileIdx].progOrder = progOrder;
1588
321
      img.tiles[tileIdx].nLayers = nLayers;
1589
321
      img.tiles[tileIdx].multiComp = multiComp;
1590
1.28k
      for (comp = 0; comp < img.nComps; ++comp) {
1591
963
  img.tiles[tileIdx].tileComps[comp].style = style;
1592
963
  img.tiles[tileIdx].tileComps[comp].nDecompLevels = nDecompLevels;
1593
963
  img.tiles[tileIdx].tileComps[comp].codeBlockW = codeBlockW;
1594
963
  img.tiles[tileIdx].tileComps[comp].codeBlockH = codeBlockH;
1595
963
  img.tiles[tileIdx].tileComps[comp].codeBlockStyle = codeBlockStyle;
1596
963
  img.tiles[tileIdx].tileComps[comp].transform = transform;
1597
963
  img.tiles[tileIdx].tileComps[comp].resLevels =
1598
963
      (JPXResLevel *)greallocn(
1599
963
         img.tiles[tileIdx].tileComps[comp].resLevels,
1600
963
         nDecompLevels + 1,
1601
963
         sizeof(JPXResLevel));
1602
3.07k
  for (r = 0; r <= nDecompLevels; ++r) {
1603
2.11k
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL;
1604
2.11k
  }
1605
963
      }
1606
1.02k
      for (r = 0; r <= nDecompLevels; ++r) {
1607
705
  if (style & 0x01) {
1608
0
    if (!readUByte(&precinctSize)) {
1609
0
      error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1610
0
      return gFalse;
1611
0
    }
1612
0
    if (r > 0 && ((precinctSize & 0x0f) == 0 ||
1613
0
      (precinctSize & 0xf0) == 0)) {
1614
0
      error(errSyntaxError, getPos(),
1615
0
      "Invalid precinct size in JPX COD marker segment");
1616
0
      return gFalse;
1617
0
    }
1618
0
    img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth =
1619
0
        precinctSize & 0x0f;
1620
0
    img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight =
1621
0
        (precinctSize >> 4) & 0x0f;
1622
705
  } else {
1623
705
    img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth = 15;
1624
705
    img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight = 15;
1625
705
  }
1626
705
      }
1627
963
      for (comp = 1; comp < img.nComps; ++comp) {
1628
2.05k
  for (r = 0; r <= nDecompLevels; ++r) {
1629
1.41k
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
1630
1.41k
        img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth;
1631
1.41k
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
1632
1.41k
        img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight;
1633
1.41k
  }
1634
642
      }
1635
321
      break;
1636
38
    case 0x53:      // COC - coding style component
1637
38
      cover(35);
1638
38
      if (tilePartIdx != 0) {
1639
0
  error(errSyntaxError, getPos(), "Extraneous JPX COC marker segment");
1640
0
  return gFalse;
1641
0
      }
1642
38
      comp = 0;
1643
38
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1644
38
    (img.nComps <= 256 && !readUByte(&comp)) ||
1645
38
    comp >= img.nComps ||
1646
38
    !readUByte(&style) ||
1647
38
    !readUByte(&nDecompLevels) ||
1648
38
    !readUByte(&codeBlockW) ||
1649
38
    !readUByte(&codeBlockH) ||
1650
38
    !readUByte(&codeBlockStyle) ||
1651
38
    !readUByte(&transform)) {
1652
18
  error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
1653
18
  return gFalse;
1654
18
      }
1655
20
      if (nDecompLevels < 1 ||
1656
20
    nDecompLevels > 31 ||
1657
20
    codeBlockW > 8 ||
1658
20
    codeBlockH > 8) {
1659
20
  error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
1660
20
  return gFalse;
1661
20
      }
1662
0
      img.tiles[tileIdx].tileComps[comp].style =
1663
0
    (img.tiles[tileIdx].tileComps[comp].style & ~1) | (style & 1);
1664
0
      img.tiles[tileIdx].tileComps[comp].nDecompLevels = nDecompLevels;
1665
0
      img.tiles[tileIdx].tileComps[comp].codeBlockW = codeBlockW + 2;
1666
0
      img.tiles[tileIdx].tileComps[comp].codeBlockH = codeBlockH + 2;
1667
0
      img.tiles[tileIdx].tileComps[comp].codeBlockStyle = codeBlockStyle;
1668
0
      img.tiles[tileIdx].tileComps[comp].transform = transform;
1669
0
      img.tiles[tileIdx].tileComps[comp].resLevels =
1670
0
    (JPXResLevel *)greallocn(
1671
0
         img.tiles[tileIdx].tileComps[comp].resLevels,
1672
0
         nDecompLevels + 1,
1673
0
         sizeof(JPXResLevel));
1674
0
      for (r = 0; r <= nDecompLevels; ++r) {
1675
0
  img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL;
1676
0
      }
1677
0
      for (r = 0; r <= nDecompLevels; ++r) {
1678
0
  if (style & 0x01) {
1679
0
    if (!readUByte(&precinctSize)) {
1680
0
      error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
1681
0
      return gFalse;
1682
0
    }
1683
0
    if (r > 0 && ((precinctSize & 0x0f) == 0 ||
1684
0
      (precinctSize & 0xf0) == 0)) {
1685
0
      error(errSyntaxError, getPos(),
1686
0
      "Invalid precinct size in JPX COD marker segment");
1687
0
      return gFalse;
1688
0
    }
1689
0
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
1690
0
        precinctSize & 0x0f;
1691
0
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
1692
0
        (precinctSize >> 4) & 0x0f;
1693
0
  } else {
1694
0
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = 15;
1695
0
    img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = 15;
1696
0
  }
1697
0
      }
1698
0
      break;
1699
321
    case 0x5c:      // QCD - quantization default
1700
321
      cover(36);
1701
321
      if (tilePartIdx != 0) {
1702
0
  error(errSyntaxError, getPos(), "Extraneous JPX QCD marker segment");
1703
0
  return gFalse;
1704
0
      }
1705
321
      if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantStyle)) {
1706
0
  error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1707
0
  return gFalse;
1708
0
      }
1709
321
      if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x00) {
1710
300
  if (segLen <= 3) {
1711
131
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1712
131
    return gFalse;
1713
131
  }
1714
169
  img.tiles[tileIdx].tileComps[0].nQuantSteps = segLen - 3;
1715
169
  img.tiles[tileIdx].tileComps[0].quantSteps =
1716
169
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1717
169
             img.tiles[tileIdx].tileComps[0].nQuantSteps,
1718
169
             sizeof(Guint));
1719
5.57k
  for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
1720
5.40k
    if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
1721
0
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1722
0
      return gFalse;
1723
0
    }
1724
5.40k
  }
1725
169
      } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x01) {
1726
0
  img.tiles[tileIdx].tileComps[0].nQuantSteps = 1;
1727
0
  img.tiles[tileIdx].tileComps[0].quantSteps =
1728
0
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1729
0
             img.tiles[tileIdx].tileComps[0].nQuantSteps,
1730
0
             sizeof(Guint));
1731
0
  if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[0])) {
1732
0
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1733
0
    return gFalse;
1734
0
  }
1735
21
      } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x02) {
1736
21
  if (segLen < 5) {
1737
0
    error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1738
0
    return gFalse;
1739
0
  }
1740
21
  img.tiles[tileIdx].tileComps[0].nQuantSteps = (segLen - 3) / 2;
1741
21
  img.tiles[tileIdx].tileComps[0].quantSteps =
1742
21
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1743
21
             img.tiles[tileIdx].tileComps[0].nQuantSteps,
1744
21
             sizeof(Guint));
1745
357
  for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
1746
336
    if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
1747
0
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1748
0
      return gFalse;
1749
0
    }
1750
336
  }
1751
21
      } else {
1752
0
  error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1753
0
  return gFalse;
1754
0
      }
1755
570
      for (comp = 1; comp < img.nComps; ++comp) {
1756
380
  img.tiles[tileIdx].tileComps[comp].quantStyle =
1757
380
      img.tiles[tileIdx].tileComps[0].quantStyle;
1758
380
  img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1759
380
      img.tiles[tileIdx].tileComps[0].nQuantSteps;
1760
380
  img.tiles[tileIdx].tileComps[comp].quantSteps = 
1761
380
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1762
380
             img.tiles[tileIdx].tileComps[0].nQuantSteps,
1763
380
             sizeof(Guint));
1764
11.8k
  for (j = 0; j < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++j) {
1765
11.4k
    img.tiles[tileIdx].tileComps[comp].quantSteps[j] =
1766
11.4k
        img.tiles[tileIdx].tileComps[0].quantSteps[j];
1767
11.4k
  }
1768
380
      }
1769
190
      break;
1770
0
    case 0x5d:      // QCC - quantization component
1771
0
      cover(37);
1772
0
      if (tilePartIdx != 0) {
1773
0
  error(errSyntaxError, getPos(), "Extraneous JPX QCC marker segment");
1774
0
  return gFalse;
1775
0
      }
1776
0
      comp = 0;
1777
0
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1778
0
    (img.nComps <= 256 && !readUByte(&comp)) ||
1779
0
    comp >= img.nComps ||
1780
0
    !readUByte(&img.tiles[tileIdx].tileComps[comp].quantStyle)) {
1781
0
  error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1782
0
  return gFalse;
1783
0
      }
1784
0
      if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) == 0x00) {
1785
0
  if (segLen <= (img.nComps > 256 ? 5U : 4U)) {
1786
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1787
0
    return gFalse;
1788
0
  }
1789
0
  img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1790
0
      segLen - (img.nComps > 256 ? 5 : 4);
1791
0
  img.tiles[tileIdx].tileComps[comp].quantSteps =
1792
0
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1793
0
             img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1794
0
             sizeof(Guint));
1795
0
  for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
1796
0
    if (!readUByte(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
1797
0
      error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1798
0
      return gFalse;
1799
0
    }
1800
0
  }
1801
0
      } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
1802
0
     == 0x01) {
1803
0
  img.tiles[tileIdx].tileComps[comp].nQuantSteps = 1;
1804
0
  img.tiles[tileIdx].tileComps[comp].quantSteps =
1805
0
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1806
0
             img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1807
0
             sizeof(Guint));
1808
0
  if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[0])) {
1809
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1810
0
    return gFalse;
1811
0
  }
1812
0
      } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
1813
0
     == 0x02) {
1814
0
  if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) {
1815
0
    error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1816
0
    return gFalse;
1817
0
  }
1818
0
  img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1819
0
      (segLen - (img.nComps > 256 ? 5 : 4)) / 2;
1820
0
  img.tiles[tileIdx].tileComps[comp].quantSteps =
1821
0
      (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1822
0
             img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1823
0
             sizeof(Guint));
1824
0
  for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
1825
0
    if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
1826
0
      error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
1827
0
      return gFalse;
1828
0
    }
1829
0
  }
1830
0
      } else {
1831
0
  error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
1832
0
  return gFalse;
1833
0
      }
1834
0
      break;
1835
67
    case 0x5e:      // RGN - region of interest
1836
67
      cover(38);
1837
67
      if (tilePartIdx != 0) {
1838
67
  error(errSyntaxError, getPos(), "Extraneous JPX RGN marker segment");
1839
67
  return gFalse;
1840
67
      }
1841
0
#if 1 //~ ROI is unimplemented
1842
0
      error(errUnimplemented, -1, "Got a JPX RGN segment");
1843
0
      if (segLen > 2 &&
1844
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1845
0
  error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
1846
0
  return gFalse;
1847
0
      }
1848
#else
1849
      if ((img.nComps > 256 && !readUWord(&comp)) ||
1850
    (img.nComps <= 256 && !readUByte(&comp)) ||
1851
    comp >= img.nComps ||
1852
    !readUByte(&compInfo[comp].roi.style) ||
1853
    !readUByte(&compInfo[comp].roi.shift)) {
1854
  error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
1855
  return gFalse;
1856
      }
1857
#endif
1858
0
      break;
1859
0
    case 0x5f:      // POC - progression order change
1860
0
      cover(39);
1861
0
#if 1 //~ progression order changes are unimplemented
1862
0
      error(errUnimplemented, -1, "Got a JPX POC segment");
1863
0
      if (segLen > 2 &&
1864
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1865
0
  error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
1866
0
  return gFalse;
1867
0
      }
1868
#else
1869
      nTileProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
1870
      tileProgs = (JPXProgOrder *)gmallocn(nTileProgs, sizeof(JPXProgOrder));
1871
      for (i = 0; i < nTileProgs; ++i) {
1872
  if (!readUByte(&tileProgs[i].startRes) ||
1873
      !(img.nComps > 256 && readUWord(&tileProgs[i].startComp)) ||
1874
      !(img.nComps <= 256 && readUByte(&tileProgs[i].startComp)) ||
1875
      !readUWord(&tileProgs[i].endLayer) ||
1876
      !readUByte(&tileProgs[i].endRes) ||
1877
      !(img.nComps > 256 && readUWord(&tileProgs[i].endComp)) ||
1878
      !(img.nComps <= 256 && readUByte(&tileProgs[i].endComp)) ||
1879
      !readUByte(&tileProgs[i].progOrder)) {
1880
    error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
1881
    return gFalse;
1882
  }
1883
      }
1884
#endif
1885
0
      break;
1886
0
    case 0x61:      // PPT - packed packet headers, tile-part hdr
1887
0
      cover(40);
1888
0
#if 1 //~ packed packet headers are unimplemented
1889
0
      error(errUnimplemented, -1, "Got a JPX PPT segment");
1890
0
      if (segLen > 2 &&
1891
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1892
0
  error(errSyntaxError, getPos(), "Error in JPX PPT marker segment");
1893
0
  return gFalse;
1894
0
      }
1895
0
#endif
1896
0
    case 0x58:      // PLT - packet length, tile-part header
1897
      // skipped
1898
0
      cover(41);
1899
0
      if (segLen > 2 &&
1900
0
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1901
0
  error(errSyntaxError, getPos(), "Error in JPX PLT marker segment");
1902
0
  return gFalse;
1903
0
      }
1904
0
      break;
1905
136
    case 0x64:      // COM - comment
1906
      // skipped
1907
136
      cover(42);
1908
136
      if (segLen > 2 &&
1909
136
    bufStr->discardChars(segLen - 2) != segLen - 2) {
1910
136
  error(errSyntaxError, getPos(), "Error in JPX COM marker segment");
1911
136
  return gFalse;
1912
136
      }
1913
0
      break;
1914
517
    case 0x93:      // SOD - start of data
1915
517
      cover(43);
1916
517
      haveSOD = gTrue;
1917
517
      break;
1918
450
    default:
1919
450
      cover(44);
1920
450
      error(errSyntaxError, getPos(),
1921
450
      "Unknown marker segment {0:02x} in JPX tile-part stream",
1922
450
      segType);
1923
450
      if (segLen > 2) {
1924
359
  bufStr->discardChars(segLen - 2);
1925
359
      }
1926
450
      break;
1927
2.07k
    }
1928
2.07k
  } while (!haveSOD);
1929
1930
1.99k
  for (comp = 0; comp < img.nComps; ++comp) {
1931
1.47k
    tileComp = &img.tiles[tileIdx].tileComps[comp];
1932
1.47k
    qStyle = tileComp->quantStyle & 0x1f;
1933
1.47k
    if ((qStyle == 0 && tileComp->nQuantSteps < 3 * tileComp->nDecompLevels + 1) ||
1934
1.47k
  (qStyle == 1 && tileComp->nQuantSteps < 1) ||
1935
1.47k
  (qStyle == 2 && tileComp->nQuantSteps < 3 * tileComp->nDecompLevels + 1)) {
1936
0
      error(errSyntaxError, getPos(), "Too few quant steps in JPX tile part");
1937
0
      return gFalse;
1938
0
    }
1939
1.47k
  }
1940
1941
  //----- initialize the tile, precincts, and code-blocks
1942
517
  if (tilePartIdx == 0) {
1943
517
    tile = &img.tiles[tileIdx];
1944
517
    i = tileIdx / img.nXTiles;
1945
517
    j = tileIdx % img.nXTiles;
1946
517
    if ((tile->x0 = img.xTileOffset + j * img.xTileSize) < img.xOffset) {
1947
471
      tile->x0 = img.xOffset;
1948
471
    }
1949
517
    if ((tile->y0 = img.yTileOffset + i * img.yTileSize) < img.yOffset) {
1950
382
      tile->y0 = img.yOffset;
1951
382
    }
1952
517
    if ((tile->x1 = img.xTileOffset + (j + 1) * img.xTileSize) > img.xSize) {
1953
496
      tile->x1 = img.xSize;
1954
496
    }
1955
517
    if ((tile->y1 = img.yTileOffset + (i + 1) * img.yTileSize) > img.ySize) {
1956
504
      tile->y1 = img.ySize;
1957
504
    }
1958
517
    tile->comp = 0;
1959
517
    tile->res = 0;
1960
517
    tile->precinct = 0;
1961
517
    tile->layer = 0;
1962
517
    tile->done = gFalse;
1963
517
    tile->maxNDecompLevels = 0;
1964
517
    tile->maxNPrecincts = 0;
1965
1.99k
    for (comp = 0; comp < img.nComps; ++comp) {
1966
1.47k
      tileComp = &tile->tileComps[comp];
1967
1.47k
      if (tileComp->nDecompLevels > tile->maxNDecompLevels) {
1968
517
  tile->maxNDecompLevels = tileComp->nDecompLevels;
1969
517
      }
1970
1.47k
      tileComp->x0 = jpxCeilDiv(tile->x0, tileComp->hSep);
1971
1.47k
      tileComp->y0 = jpxCeilDiv(tile->y0, tileComp->vSep);
1972
1.47k
      tileComp->x1 = jpxCeilDiv(tile->x1, tileComp->hSep);
1973
1.47k
      tileComp->y1 = jpxCeilDiv(tile->y1, tileComp->vSep);
1974
1.47k
      tileComp->x0r = jpxCeilDivPow2(tileComp->x0, reduction);
1975
1.47k
      tileComp->w = jpxCeilDivPow2(tileComp->x1, reduction) - tileComp->x0r;
1976
1.47k
      tileComp->y0r = jpxCeilDivPow2(tileComp->y0, reduction);
1977
1.47k
      tileComp->h = jpxCeilDivPow2(tileComp->y1, reduction) - tileComp->y0r;
1978
1.47k
      if (tileComp->w == 0 || tileComp->h == 0 ||
1979
1.47k
    tileComp->w > INT_MAX / tileComp->h) {
1980
0
  error(errSyntaxError, getPos(),
1981
0
        "Invalid tile size or sample separation in JPX stream");
1982
0
  return gFalse;
1983
0
      }
1984
1.47k
      tileComp->data = (int *)gmallocn(tileComp->w * tileComp->h, sizeof(int));
1985
1.47k
      if (tileComp->x1 - tileComp->x0 > tileComp->y1 - tileComp->y0) {
1986
272
  n = tileComp->x1 - tileComp->x0;
1987
1.20k
      } else {
1988
1.20k
  n = tileComp->y1 - tileComp->y0;
1989
1.20k
      }
1990
1.47k
      tileComp->buf = (int *)gmallocn(n + 8, sizeof(int));
1991
7.93k
      for (r = 0; r <= tileComp->nDecompLevels; ++r) {
1992
6.46k
  resLevel = &tileComp->resLevels[r];
1993
6.46k
  resLevel->x0 = jpxCeilDivPow2(tileComp->x0,
1994
6.46k
              tileComp->nDecompLevels - r);
1995
6.46k
  resLevel->y0 = jpxCeilDivPow2(tileComp->y0,
1996
6.46k
              tileComp->nDecompLevels - r);
1997
6.46k
  resLevel->x1 = jpxCeilDivPow2(tileComp->x1,
1998
6.46k
              tileComp->nDecompLevels - r);
1999
6.46k
  resLevel->y1 = jpxCeilDivPow2(tileComp->y1,
2000
6.46k
              tileComp->nDecompLevels - r);
2001
6.46k
  resLevel->codeBlockW = r == 0 ? resLevel->precinctWidth
2002
6.46k
                                : resLevel->precinctWidth - 1;
2003
6.46k
  if (resLevel->codeBlockW > tileComp->codeBlockW) {
2004
5.94k
    resLevel->codeBlockW = tileComp->codeBlockW;
2005
5.94k
  }
2006
6.46k
  resLevel->cbW = 1 << resLevel->codeBlockW;
2007
6.46k
  resLevel->codeBlockH = r == 0 ? resLevel->precinctHeight
2008
6.46k
                                : resLevel->precinctHeight - 1;
2009
6.46k
  if (resLevel->codeBlockH > tileComp->codeBlockH) {
2010
5.96k
    resLevel->codeBlockH = tileComp->codeBlockH;
2011
5.96k
  }
2012
6.46k
  resLevel->cbH = 1 << resLevel->codeBlockH;
2013
  // the JPEG 2000 spec says that packets for empty res levels
2014
  // should all be present in the codestream (B.6, B.9, B.10),
2015
  // but it appears that encoders drop packets if the res level
2016
  // AND the subbands are all completely empty
2017
6.46k
  resLevel->empty = resLevel->x0 == resLevel->x1 ||
2018
4.63k
                    resLevel->y0 == resLevel->y1;
2019
6.46k
  if (r == 0) {
2020
1.47k
    nSBs = 1;
2021
1.47k
    resLevel->bx0[0] = resLevel->x0;
2022
1.47k
    resLevel->by0[0] = resLevel->y0;
2023
1.47k
    resLevel->bx1[0] = resLevel->x1;
2024
1.47k
    resLevel->by1[0] = resLevel->y1;
2025
1.47k
    resLevel->empty = resLevel->empty &&
2026
1.19k
                      (resLevel->bx0[0] == resLevel->bx1[0] ||
2027
121
           resLevel->by0[0] == resLevel->by1[0]);
2028
4.98k
  } else {
2029
4.98k
    nSBs = 3;
2030
4.98k
    resLevel->bx0[0] = jpxCeilDivPow2(resLevel->x0 - 1, 1);
2031
4.98k
    resLevel->by0[0] = jpxCeilDivPow2(resLevel->y0, 1);
2032
4.98k
    resLevel->bx1[0] = jpxCeilDivPow2(resLevel->x1 - 1, 1);
2033
4.98k
    resLevel->by1[0] = jpxCeilDivPow2(resLevel->y1, 1);
2034
4.98k
    resLevel->bx0[1] = jpxCeilDivPow2(resLevel->x0, 1);
2035
4.98k
    resLevel->by0[1] = jpxCeilDivPow2(resLevel->y0 - 1, 1);
2036
4.98k
    resLevel->bx1[1] = jpxCeilDivPow2(resLevel->x1, 1);
2037
4.98k
    resLevel->by1[1] = jpxCeilDivPow2(resLevel->y1 - 1, 1);
2038
4.98k
    resLevel->bx0[2] = jpxCeilDivPow2(resLevel->x0 - 1, 1);
2039
4.98k
    resLevel->by0[2] = jpxCeilDivPow2(resLevel->y0 - 1, 1);
2040
4.98k
    resLevel->bx1[2] = jpxCeilDivPow2(resLevel->x1 - 1, 1);
2041
4.98k
    resLevel->by1[2] = jpxCeilDivPow2(resLevel->y1 - 1, 1);
2042
4.98k
    resLevel->empty = resLevel->empty &&
2043
1.28k
                      (resLevel->bx0[0] == resLevel->bx1[0] ||
2044
536
           resLevel->by0[0] == resLevel->by1[0]) &&
2045
1.28k
                      (resLevel->bx0[1] == resLevel->bx1[1] ||
2046
259
           resLevel->by0[1] == resLevel->by1[1]) &&
2047
1.28k
                      (resLevel->bx0[2] == resLevel->bx1[2] ||
2048
536
           resLevel->by0[2] == resLevel->by1[2]);
2049
4.98k
  }
2050
6.46k
  preCol0 = jpxFloorDivPow2(resLevel->x0, resLevel->precinctWidth);
2051
6.46k
  preCol1 = jpxCeilDivPow2(resLevel->x1, resLevel->precinctWidth);
2052
6.46k
  preRow0 = jpxFloorDivPow2(resLevel->y0, resLevel->precinctHeight);
2053
6.46k
  preRow1 = jpxCeilDivPow2(resLevel->y1, resLevel->precinctHeight);
2054
6.46k
  resLevel->nPrecincts = (preCol1 - preCol0) * (preRow1 - preRow0);
2055
6.46k
  resLevel->precincts = (JPXPrecinct *)gmallocn(resLevel->nPrecincts,
2056
6.46k
                  sizeof(JPXPrecinct));
2057
6.46k
  if (resLevel->nPrecincts > tile->maxNPrecincts) {
2058
630
    tile->maxNPrecincts = resLevel->nPrecincts;
2059
630
  }
2060
20.6k
  for (pre = 0; pre < resLevel->nPrecincts; ++pre) {
2061
14.1k
    resLevel->precincts[pre].subbands = NULL;
2062
14.1k
  }
2063
6.46k
  precinct = resLevel->precincts;
2064
13.6k
  for (preRow = preRow0; preRow < preRow1; ++preRow) {
2065
21.4k
    for (preCol = preCol0; preCol < preCol1; ++preCol) {
2066
14.1k
      precinct->subbands =
2067
14.1k
          (JPXSubband *)gmallocn(nSBs, sizeof(JPXSubband));
2068
47.5k
      for (sb = 0; sb < nSBs; ++sb) {
2069
33.3k
        precinct->subbands[sb].inclusion = NULL;
2070
33.3k
        precinct->subbands[sb].zeroBitPlane = NULL;
2071
33.3k
        precinct->subbands[sb].cbs = NULL;
2072
33.3k
      }
2073
47.5k
      for (sb = 0; sb < nSBs; ++sb) {
2074
33.3k
        subband = &precinct->subbands[sb];
2075
33.3k
        if (r == 0) {
2076
4.59k
    px0 = preCol << resLevel->precinctWidth;
2077
4.59k
    px1 = (preCol + 1) << resLevel->precinctWidth;
2078
4.59k
    py0 = preRow << resLevel->precinctHeight;
2079
4.59k
    py1 = (preRow + 1) << resLevel->precinctHeight;
2080
28.7k
        } else {
2081
28.7k
    px0 = preCol << (resLevel->precinctWidth - 1);
2082
28.7k
    px1 = (preCol + 1) << (resLevel->precinctWidth - 1);
2083
28.7k
    py0 = preRow << (resLevel->precinctHeight - 1);
2084
28.7k
    py1 = (preRow + 1) << (resLevel->precinctHeight - 1);
2085
28.7k
        }
2086
33.3k
        if (px0 < resLevel->bx0[sb]) {
2087
14.4k
    px0 = resLevel->bx0[sb];
2088
14.4k
        }
2089
33.3k
        if (px1 > resLevel->bx1[sb]) {
2090
16.0k
    px1 = resLevel->bx1[sb];
2091
16.0k
        }
2092
33.3k
        if (py0 < resLevel->by0[sb]) {
2093
12.0k
    py0 = resLevel->by0[sb];
2094
12.0k
        }
2095
33.3k
        if (py1 > resLevel->by1[sb]) {
2096
21.2k
    py1 = resLevel->by1[sb];
2097
21.2k
        }
2098
33.3k
        if (r == 0) { // (NL)LL
2099
4.59k
    sbCoeffs = tileComp->data;
2100
28.7k
        } else if (sb == 0) { // (NL-r+1)HL
2101
9.58k
    sbCoeffs = tileComp->data
2102
9.58k
                           + resLevel->bx1[1] - resLevel->bx0[1];
2103
19.1k
        } else if (sb == 1) { // (NL-r+1)LH
2104
9.58k
    sbCoeffs = tileComp->data
2105
9.58k
               + (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w;
2106
9.58k
        } else { // (NL-r+1)HH
2107
9.58k
    sbCoeffs = tileComp->data
2108
9.58k
               + (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w
2109
9.58k
               + (resLevel->bx1[1] - resLevel->bx0[1]);
2110
9.58k
        }
2111
33.3k
        cbCol0 = jpxFloorDivPow2(px0, resLevel->codeBlockW);
2112
33.3k
        cbCol1 = jpxCeilDivPow2(px1, resLevel->codeBlockW);
2113
33.3k
        cbRow0 = jpxFloorDivPow2(py0, resLevel->codeBlockH);
2114
33.3k
        cbRow1 = jpxCeilDivPow2(py1, resLevel->codeBlockH);
2115
33.3k
        subband->nXCBs = cbCol1 - cbCol0;
2116
33.3k
        subband->nYCBs = cbRow1 - cbRow0;
2117
33.3k
        n = subband->nXCBs > subband->nYCBs ? subband->nXCBs
2118
33.3k
                                            : subband->nYCBs;
2119
33.3k
        for (subband->maxTTLevel = 0, --n;
2120
87.4k
       n;
2121
54.0k
       ++subband->maxTTLevel, n >>= 1) ;
2122
33.3k
        n = 0;
2123
120k
        for (level = subband->maxTTLevel; level >= 0; --level) {
2124
87.4k
    nx = jpxCeilDivPow2(subband->nXCBs, level);
2125
87.4k
    ny = jpxCeilDivPow2(subband->nYCBs, level);
2126
87.4k
    n += nx * ny;
2127
87.4k
        }
2128
33.3k
        subband->inclusion =
2129
33.3k
            (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
2130
33.3k
        subband->zeroBitPlane =
2131
33.3k
            (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
2132
301k
        for (k = 0; k < n; ++k) {
2133
268k
    subband->inclusion[k].finished = gFalse;
2134
268k
    subband->inclusion[k].val = 0;
2135
268k
    subband->zeroBitPlane[k].finished = gFalse;
2136
268k
    subband->zeroBitPlane[k].val = 0;
2137
268k
        }
2138
33.3k
        subband->cbs = (JPXCodeBlock *)gmallocn(subband->nXCBs *
2139
33.3k
                    subband->nYCBs,
2140
33.3k
                  sizeof(JPXCodeBlock));
2141
183k
        for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) {
2142
149k
    subband->cbs[k].dataLen = NULL;
2143
149k
    subband->cbs[k].touched = NULL;
2144
149k
    subband->cbs[k].arithDecoder = NULL;
2145
149k
    subband->cbs[k].stats = NULL;
2146
149k
        }
2147
33.3k
        cb = subband->cbs;
2148
169k
        for (cbY = cbRow0; cbY < cbRow1; ++cbY) {
2149
286k
    for (cbX = cbCol0; cbX < cbCol1; ++cbX) {
2150
149k
      cb->x0 = cbX << resLevel->codeBlockW;
2151
149k
      cb->x1 = cb->x0 + resLevel->cbW;
2152
149k
      if (cb->x0 < px0) {
2153
32.7k
        cb->x0 = px0;
2154
32.7k
      }
2155
149k
      if (cb->x1 > px1) {
2156
18.8k
        cb->x1 = px1;
2157
18.8k
      }
2158
149k
      cb->y0 = cbY << resLevel->codeBlockH;
2159
149k
      cb->y1 = cb->y0 + resLevel->cbH;
2160
149k
      if (cb->y0 < py0) {
2161
12.5k
        cb->y0 = py0;
2162
12.5k
      }
2163
149k
      if (cb->y1 > py1) {
2164
24.9k
        cb->y1 = py1;
2165
24.9k
      }
2166
149k
      cb->seen = gFalse;
2167
149k
      cb->lBlock = 3;
2168
149k
      cb->nextPass = jpxPassCleanup;
2169
149k
      cb->nZeroBitPlanes = 0;
2170
149k
      cb->dataLenSize = 1;
2171
149k
      cb->dataLen = (Guint *)gmalloc(sizeof(Guint));
2172
149k
      if (r <= tileComp->nDecompLevels - reduction) {
2173
149k
        cb->coeffs = sbCoeffs
2174
149k
                     + (cb->y0 - resLevel->by0[sb]) * tileComp->w
2175
149k
                     + (cb->x0 - resLevel->bx0[sb]);
2176
149k
        cb->touched = (char *)gmalloc(1 << (resLevel->codeBlockW
2177
149k
              + resLevel->codeBlockH));
2178
149k
        cb->len = 0;
2179
3.27M
        for (cbj = 0; cbj < cb->y1 - cb->y0; ++cbj) {
2180
8.47M
          for (cbi = 0; cbi < cb->x1 - cb->x0; ++cbi) {
2181
5.35M
      cb->coeffs[cbj * tileComp->w + cbi] = 0;
2182
5.35M
          }
2183
3.12M
        }
2184
149k
        memset(cb->touched, 0,
2185
149k
         ((size_t)1 << (resLevel->codeBlockW
2186
149k
            + resLevel->codeBlockH)));
2187
149k
      } else {
2188
0
        cb->coeffs = NULL;
2189
0
        cb->touched = NULL;
2190
0
        cb->len = 0;
2191
0
      }
2192
149k
      ++cb;
2193
149k
    }
2194
136k
        }
2195
33.3k
      }
2196
14.1k
      ++precinct;
2197
14.1k
    }
2198
7.22k
  }
2199
6.46k
      }
2200
1.47k
    }
2201
517
    tile->init = gTrue;
2202
517
  }
2203
2204
517
  return readTilePartData(tileIdx, tilePartLen, tilePartToEOC);
2205
517
}
2206
2207
GBool JPXStream::readTilePartData(Guint tileIdx,
2208
517
          Guint tilePartLen, GBool tilePartToEOC) {
2209
517
  JPXTile *tile;
2210
517
  JPXTileComp *tileComp;
2211
517
  JPXResLevel *resLevel;
2212
517
  JPXPrecinct *precinct;
2213
517
  JPXSubband *subband;
2214
517
  JPXCodeBlock *cb;
2215
517
  Guint ttVal;
2216
517
  Guint bits, cbX, cbY, nx, ny, i, j, n, sb;
2217
517
  int level;
2218
2219
517
  tile = &img.tiles[tileIdx];
2220
2221
  // read all packets from this tile-part
2222
7.44M
  while (1) {
2223
2224
    // if the tile is finished, skip any remaining data
2225
7.44M
    if (tile->done) {
2226
332
      bufStr->discardChars(tilePartLen);
2227
332
      return gTrue;
2228
332
    }
2229
2230
7.44M
    if (tilePartToEOC) {
2231
      //~ peek for an EOC marker
2232
0
      cover(93);
2233
7.44M
    } else if (tilePartLen == 0) {
2234
16
      break;
2235
16
    }
2236
2237
7.44M
    tileComp = &tile->tileComps[tile->comp];
2238
7.44M
    resLevel = &tileComp->resLevels[tile->res];
2239
7.44M
    precinct = &resLevel->precincts[tile->precinct];
2240
2241
7.44M
    if (resLevel->empty) {
2242
7.35M
      goto nextPacket;
2243
7.35M
    }
2244
2245
    //----- packet header
2246
2247
    // setup
2248
92.4k
    startBitBuf(tilePartLen);
2249
92.4k
    if (tileComp->style & 0x02) {
2250
66.4k
      skipSOP();
2251
66.4k
    }
2252
2253
    // zero-length flag
2254
92.4k
    if (!readBits(1, &bits)) {
2255
129
      goto err;
2256
129
    }
2257
92.3k
    if (!bits) {
2258
      // packet is empty -- clear all code-block inclusion flags
2259
84.2k
      cover(45);
2260
231k
      for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) {
2261
147k
  subband = &precinct->subbands[sb];
2262
463k
  for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2263
759k
    for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2264
442k
      cb = &subband->cbs[cbY * subband->nXCBs + cbX];
2265
442k
      cb->included = gFalse;
2266
442k
    }
2267
316k
  }
2268
147k
      }
2269
84.2k
    } else {
2270
2271
23.5k
      for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) {
2272
15.5k
  subband = &precinct->subbands[sb];
2273
50.1k
  for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2274
103k
    for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2275
69.2k
      cb = &subband->cbs[cbY * subband->nXCBs + cbX];
2276
2277
      // skip code-blocks with no coefficients
2278
69.2k
      if (cb->x0 >= cb->x1 || cb->y0 >= cb->y1) {
2279
8.26k
        cover(46);
2280
8.26k
        cb->included = gFalse;
2281
8.26k
        continue;
2282
8.26k
      }
2283
2284
      // code-block inclusion
2285
60.9k
      if (cb->seen) {
2286
21.7k
        cover(47);
2287
21.7k
        if (!readBits(1, &cb->included)) {
2288
18
    goto err;
2289
18
        }
2290
39.2k
      } else {
2291
39.2k
        cover(48);
2292
39.2k
        ttVal = 0;
2293
39.2k
        i = 0;
2294
135k
        for (level = subband->maxTTLevel; level >= 0; --level) {
2295
121k
    nx = jpxCeilDivPow2(subband->nXCBs, level);
2296
121k
    ny = jpxCeilDivPow2(subband->nYCBs, level);
2297
121k
    j = i + (cbY >> level) * nx + (cbX >> level);
2298
121k
    if (!subband->inclusion[j].finished &&
2299
53.3k
        !subband->inclusion[j].val) {
2300
33.8k
      subband->inclusion[j].val = ttVal;
2301
87.9k
    } else {
2302
87.9k
      ttVal = subband->inclusion[j].val;
2303
87.9k
    }
2304
187k
    while (!subband->inclusion[j].finished &&
2305
90.3k
           ttVal <= tile->layer) {
2306
65.2k
      if (!readBits(1, &bits)) {
2307
0
        goto err;
2308
0
      }
2309
65.2k
      if (bits == 1) {
2310
28.3k
        subband->inclusion[j].finished = gTrue;
2311
36.9k
      } else {
2312
36.9k
        ++ttVal;
2313
36.9k
      }
2314
65.2k
    }
2315
121k
    subband->inclusion[j].val = ttVal;
2316
121k
    if (ttVal > tile->layer) {
2317
25.0k
      break;
2318
25.0k
    }
2319
96.7k
    i += nx * ny;
2320
96.7k
        }
2321
39.2k
        cb->included = level < 0;
2322
39.2k
      }
2323
2324
60.9k
      if (cb->included) {
2325
24.5k
        cover(49);
2326
2327
        // zero bit-plane count
2328
24.5k
        if (!cb->seen) {
2329
14.1k
    cover(50);
2330
14.1k
    ttVal = 0;
2331
14.1k
    i = 0;
2332
73.8k
    for (level = subband->maxTTLevel; level >= 0; --level) {
2333
59.7k
      nx = jpxCeilDivPow2(subband->nXCBs, level);
2334
59.7k
      ny = jpxCeilDivPow2(subband->nYCBs, level);
2335
59.7k
      j = i + (cbY >> level) * nx + (cbX >> level);
2336
59.7k
      if (!subband->zeroBitPlane[j].finished &&
2337
26.0k
          !subband->zeroBitPlane[j].val) {
2338
26.0k
        subband->zeroBitPlane[j].val = ttVal;
2339
33.6k
      } else {
2340
33.6k
        ttVal = subband->zeroBitPlane[j].val;
2341
33.6k
      }
2342
113k
      while (!subband->zeroBitPlane[j].finished) {
2343
53.4k
        if (!readBits(1, &bits)) {
2344
0
          goto err;
2345
0
        }
2346
53.4k
        if (bits == 1) {
2347
26.0k
          subband->zeroBitPlane[j].finished = gTrue;
2348
27.3k
        } else {
2349
27.3k
          ++ttVal;
2350
27.3k
        }
2351
53.4k
      }
2352
59.7k
      subband->zeroBitPlane[j].val = ttVal;
2353
59.7k
      i += nx * ny;
2354
59.7k
    }
2355
14.1k
    cb->nZeroBitPlanes = ttVal;
2356
14.1k
        }
2357
2358
        // number of coding passes
2359
24.5k
        if (!readBits(1, &bits)) {
2360
0
    goto err;
2361
0
        }
2362
24.5k
        if (bits == 0) {
2363
11.6k
    cover(51);
2364
11.6k
    cb->nCodingPasses = 1;
2365
12.8k
        } else {
2366
12.8k
    if (!readBits(1, &bits)) {
2367
0
      goto err;
2368
0
    }
2369
12.8k
    if (bits == 0) {
2370
5.47k
      cover(52);
2371
5.47k
      cb->nCodingPasses = 2;
2372
7.33k
    } else {
2373
7.33k
      cover(53);
2374
7.33k
      if (!readBits(2, &bits)) {
2375
0
        goto err;
2376
0
      }
2377
7.33k
      if (bits < 3) {
2378
4.97k
        cover(54);
2379
4.97k
        cb->nCodingPasses = 3 + bits;
2380
4.97k
      } else {
2381
2.36k
        cover(55);
2382
2.36k
        if (!readBits(5, &bits)) {
2383
0
          goto err;
2384
0
        }
2385
2.36k
        if (bits < 31) {
2386
1.74k
          cover(56);
2387
1.74k
          cb->nCodingPasses = 6 + bits;
2388
1.74k
        } else {
2389
622
          cover(57);
2390
622
          if (!readBits(7, &bits)) {
2391
0
      goto err;
2392
0
          }
2393
622
          cb->nCodingPasses = 37 + bits;
2394
622
        }
2395
2.36k
      }
2396
7.33k
    }
2397
12.8k
        }
2398
2399
        // update Lblock
2400
107k
        while (1) {
2401
107k
    if (!readBits(1, &bits)) {
2402
22
      goto err;
2403
22
    }
2404
107k
    if (!bits) {
2405
24.4k
      break;
2406
24.4k
    }
2407
83.3k
    ++cb->lBlock;
2408
83.3k
        }
2409
2410
        // one codeword segment for each of the coding passes
2411
24.4k
        if (tileComp->codeBlockStyle & 0x04) {
2412
13.6k
    if (cb->nCodingPasses > cb->dataLenSize) {
2413
4.22k
      cb->dataLenSize = cb->nCodingPasses;
2414
4.22k
      cb->dataLen = (Guint *)greallocn(cb->dataLen,
2415
4.22k
               cb->dataLenSize,
2416
4.22k
               sizeof(Guint));
2417
4.22k
    }
2418
2419
    // read the lengths
2420
64.4k
    for (i = 0; i < cb->nCodingPasses; ++i) {
2421
50.7k
      if (!readBits(cb->lBlock, &cb->dataLen[i])) {
2422
0
        goto err;
2423
0
      }
2424
50.7k
    }
2425
2426
        // one codeword segment for all of the coding passes
2427
13.6k
        } else {
2428
2429
    // read the length
2430
10.8k
    for (n = cb->lBlock, i = cb->nCodingPasses >> 1;
2431
24.8k
         i;
2432
14.0k
         ++n, i >>= 1) ;
2433
10.8k
    if (!readBits(n, &cb->dataLen[0])) {
2434
0
      goto err;
2435
0
    }
2436
10.8k
        }
2437
24.4k
      }
2438
60.9k
    }
2439
34.6k
  }
2440
15.5k
      }
2441
8.05k
    }
2442
92.2k
    if (tileComp->style & 0x04) {
2443
66.3k
      skipEPH();
2444
66.3k
    }
2445
92.2k
    tilePartLen = finishBitBuf();
2446
2447
    //----- packet data
2448
2449
254k
    for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) {
2450
162k
      subband = &precinct->subbands[sb];
2451
513k
      for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2452
862k
  for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2453
511k
    cb = &subband->cbs[cbY * subband->nXCBs + cbX];
2454
511k
    if (cb->included) {
2455
24.3k
      if (!readCodeBlockData(tileComp, resLevel, precinct, subband,
2456
24.3k
           tile->res, sb, cb)) {
2457
0
        return gFalse;
2458
0
      }
2459
24.3k
      if (tileComp->codeBlockStyle & 0x04) {
2460
63.6k
        for (i = 0; i < cb->nCodingPasses; ++i) {
2461
50.1k
    tilePartLen -= cb->dataLen[i];
2462
50.1k
        }
2463
13.5k
      } else {
2464
10.8k
        tilePartLen -= cb->dataLen[0];
2465
10.8k
      }
2466
24.3k
      cb->seen = gTrue;
2467
24.3k
    }
2468
511k
  }
2469
351k
      }
2470
162k
    }
2471
2472
    //----- next packet
2473
2474
7.44M
  nextPacket:
2475
7.44M
    switch (tile->progOrder) {
2476
80.0k
    case 0: // layer, resolution level, component, precinct
2477
80.0k
      cover(58);
2478
296k
      do {
2479
296k
  if (++tile->precinct == tile->maxNPrecincts) {
2480
27.7k
    tile->precinct = 0;
2481
27.7k
    if (++tile->comp == img.nComps) {
2482
13.7k
      tile->comp = 0;
2483
13.7k
      if (++tile->res == tile->maxNDecompLevels + 1) {
2484
3.22k
        tile->res = 0;
2485
3.22k
        if (++tile->layer == tile->nLayers) {
2486
41
    tile->layer = 0;
2487
41
    tile->done = gTrue;
2488
41
        }
2489
3.22k
      }
2490
13.7k
    }
2491
27.7k
  }
2492
296k
      } while (!tile->done &&
2493
296k
         (tile->res > tile->tileComps[tile->comp].nDecompLevels ||
2494
296k
    tile->precinct >= tile->tileComps[tile->comp]
2495
296k
                                    .resLevels[tile->res].nPrecincts));
2496
80.0k
      break;
2497
7.73k
    case 1: // resolution level, layer, component, precinct
2498
7.73k
      cover(59);
2499
425k
      do {
2500
425k
  if (++tile->precinct == tile->maxNPrecincts) {
2501
5.34k
    tile->precinct = 0;
2502
5.34k
    if (++tile->comp == img.nComps) {
2503
1.77k
      tile->comp = 0;
2504
1.77k
      if (++tile->layer == tile->nLayers) {
2505
36
        tile->layer = 0;
2506
36
        if (++tile->res == tile->maxNDecompLevels + 1) {
2507
11
    tile->res = 0;
2508
11
    tile->done = gTrue;
2509
11
        }
2510
36
      }
2511
1.77k
    }
2512
5.34k
  }
2513
425k
      } while (!tile->done &&
2514
425k
         (tile->res > tile->tileComps[tile->comp].nDecompLevels ||
2515
425k
    tile->precinct >= tile->tileComps[tile->comp]
2516
425k
                                    .resLevels[tile->res].nPrecincts));
2517
7.73k
      break;
2518
7.36M
    case 2: // resolution level, precinct, component, layer
2519
7.36M
      cover(60);
2520
      //~ this is incorrect if there are subsampled components (?)
2521
7.36M
      do {
2522
7.36M
  if (++tile->layer == tile->nLayers) {
2523
4.26k
    tile->layer = 0;
2524
4.26k
    if (++tile->comp == img.nComps) {
2525
1.41k
      tile->comp = 0;
2526
1.41k
      if (++tile->precinct == tile->maxNPrecincts) {
2527
1.41k
        tile->precinct = 0;
2528
1.41k
        if (++tile->res == tile->maxNDecompLevels + 1) {
2529
277
    tile->res = 0;
2530
277
    tile->done = gTrue;
2531
277
        }
2532
1.41k
      }
2533
1.41k
    }
2534
4.26k
  }
2535
7.36M
      } while (!tile->done &&
2536
7.36M
         (tile->res > tile->tileComps[tile->comp].nDecompLevels ||
2537
7.36M
    tile->precinct >= tile->tileComps[tile->comp]
2538
7.36M
                                    .resLevels[tile->res].nPrecincts));
2539
7.36M
      break;
2540
0
    case 3: // precinct, component, resolution level, layer
2541
0
      cover(61);
2542
      //~ this is incorrect if there are subsampled components (?)
2543
0
      do {
2544
0
  if (++tile->layer == tile->nLayers) {
2545
0
    tile->layer = 0;
2546
0
    if (++tile->res == tile->maxNDecompLevels + 1) {
2547
0
      tile->res = 0;
2548
0
      if (++tile->comp == img.nComps) {
2549
0
        tile->comp = 0;
2550
0
        if (++tile->precinct == tile->maxNPrecincts) {
2551
0
    tile->precinct = 0;
2552
0
    tile->done = gTrue;
2553
0
        }
2554
0
      }
2555
0
    }
2556
0
  }
2557
0
      } while (!tile->done &&
2558
0
         (tile->res > tile->tileComps[tile->comp].nDecompLevels ||
2559
0
    tile->precinct >= tile->tileComps[tile->comp]
2560
0
                                    .resLevels[tile->res].nPrecincts));
2561
0
      break;
2562
108
    case 4: // component, precinct, resolution level, layer
2563
108
      cover(62);
2564
288
      do {
2565
288
  if (++tile->layer == tile->nLayers) {
2566
72
    tile->layer = 0;
2567
72
    if (++tile->res == tile->maxNDecompLevels + 1) {
2568
36
      tile->res = 0;
2569
36
      if (++tile->precinct == tile->maxNPrecincts) {
2570
6
        tile->precinct = 0;
2571
6
        if (++tile->comp == img.nComps) {
2572
3
    tile->comp = 0;
2573
3
    tile->done = gTrue;
2574
3
        }
2575
6
      }
2576
36
    }
2577
72
  }
2578
288
      } while (!tile->done &&
2579
285
         (tile->res > tile->tileComps[tile->comp].nDecompLevels ||
2580
285
    tile->precinct >= tile->tileComps[tile->comp]
2581
285
                                    .resLevels[tile->res].nPrecincts));
2582
108
      break;
2583
7.44M
    }
2584
7.44M
  }
2585
2586
16
  return gTrue;
2587
2588
169
 err:
2589
169
  error(errSyntaxError, getPos(), "Error in JPX stream");
2590
169
  return gFalse;
2591
517
}
2592
2593
GBool JPXStream::readCodeBlockData(JPXTileComp *tileComp,
2594
           JPXResLevel *resLevel,
2595
           JPXPrecinct *precinct,
2596
           JPXSubband *subband,
2597
           Guint res, Guint sb,
2598
24.3k
           JPXCodeBlock *cb) {
2599
24.3k
  int *coeff0, *coeff1, *coeff;
2600
24.3k
  char *touched0, *touched1, *touched;
2601
24.3k
  Guint horiz, vert, diag, all, cx, xorBit;
2602
24.3k
  int horizSign, vertSign, bit;
2603
24.3k
  int segSym;
2604
24.3k
  Guint n, i, x, y0, y1;
2605
2606
24.3k
  if (res > tileComp->nDecompLevels - reduction) {
2607
    // skip the codeblock data
2608
0
    if (tileComp->codeBlockStyle & 0x04) {
2609
0
      n = 0;
2610
0
      for (i = 0; i < cb->nCodingPasses; ++i) {
2611
0
  n += cb->dataLen[i];
2612
0
      }
2613
0
    } else {
2614
0
      n = cb->dataLen[0];
2615
0
    }
2616
0
    bufStr->discardChars(n);
2617
0
    return gTrue;
2618
0
  }
2619
2620
24.3k
  if (cb->arithDecoder) {
2621
10.1k
    cover(63);
2622
10.1k
    if (tileComp->codeBlockStyle & 0x04) {
2623
7.97k
      cb->arithDecoder->setStream(bufStr, cb->dataLen[0]);
2624
7.97k
      cb->arithDecoder->start();
2625
7.97k
    } else {
2626
2.21k
      cb->arithDecoder->restart(cb->dataLen[0]);
2627
2.21k
    }
2628
14.1k
  } else {
2629
14.1k
    cover(64);
2630
14.1k
    cb->arithDecoder = new JArithmeticDecoder();
2631
14.1k
    cb->arithDecoder->setStream(bufStr, cb->dataLen[0]);
2632
14.1k
    cb->arithDecoder->start();
2633
14.1k
    cb->stats = new JArithmeticDecoderStats(jpxNContexts);
2634
14.1k
    cb->stats->setEntry(jpxContextSigProp, 4, 0);
2635
14.1k
    cb->stats->setEntry(jpxContextRunLength, 3, 0);
2636
14.1k
    cb->stats->setEntry(jpxContextUniform, 46, 0);
2637
14.1k
  }
2638
2639
202k
  for (i = 0; i < cb->nCodingPasses; ++i) {
2640
178k
    if ((tileComp->codeBlockStyle & 0x04) && i > 0) {
2641
36.6k
      cb->arithDecoder->setStream(bufStr, cb->dataLen[i]);
2642
36.6k
      cb->arithDecoder->start();
2643
36.6k
    }
2644
2645
178k
    switch (cb->nextPass) {
2646
2647
    //----- significance propagation pass
2648
58.7k
    case jpxPassSigProp:
2649
58.7k
      cover(65);
2650
58.7k
      for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched;
2651
186k
     y0 < cb->y1;
2652
127k
     y0 += 4, coeff0 += 4 * tileComp->w,
2653
127k
       touched0 += 4 << resLevel->codeBlockW) {
2654
127k
  for (x = cb->x0, coeff1 = coeff0, touched1 = touched0;
2655
1.43M
       x < cb->x1;
2656
1.30M
       ++x, ++coeff1, ++touched1) {
2657
1.30M
    for (y1 = 0, coeff = coeff1, touched = touched1;
2658
5.25M
         y1 < 4 && y0+y1 < cb->y1;
2659
3.94M
         ++y1, coeff += tileComp->w, touched += resLevel->cbW) {
2660
3.94M
      if (!*coeff) {
2661
1.90M
        horiz = vert = diag = 0;
2662
1.90M
        horizSign = vertSign = 2;
2663
1.90M
        if (x > cb->x0) {
2664
1.62M
    if (coeff[-1]) {
2665
417k
      ++horiz;
2666
417k
      horizSign += coeff[-1] < 0 ? -1 : 1;
2667
417k
    }
2668
1.62M
    if (y0+y1 > cb->y0) {
2669
1.31M
      diag += coeff[-(int)tileComp->w - 1] ? 1 : 0;
2670
1.31M
    }
2671
1.62M
    if (y0+y1 < cb->y1 - 1 &&
2672
1.32M
        (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2673
1.32M
      diag += coeff[tileComp->w - 1] ? 1 : 0;
2674
1.32M
    }
2675
1.62M
        }
2676
1.90M
        if (x < cb->x1 - 1) {
2677
1.64M
    if (coeff[1]) {
2678
277k
      ++horiz;
2679
277k
      horizSign += coeff[1] < 0 ? -1 : 1;
2680
277k
    }
2681
1.64M
    if (y0+y1 > cb->y0) {
2682
1.33M
      diag += coeff[-(int)tileComp->w + 1] ? 1 : 0;
2683
1.33M
    }
2684
1.64M
    if (y0+y1 < cb->y1 - 1 &&
2685
1.34M
        (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2686
1.34M
      diag += coeff[tileComp->w + 1] ? 1 : 0;
2687
1.34M
    }
2688
1.64M
        }
2689
1.90M
        if (y0+y1 > cb->y0) {
2690
1.54M
    if (coeff[-(int)tileComp->w]) {
2691
434k
      ++vert;
2692
434k
      vertSign += coeff[-(int)tileComp->w] < 0 ? -1 : 1;
2693
434k
    }
2694
1.54M
        }
2695
1.90M
        if (y0+y1 < cb->y1 - 1 &&
2696
1.55M
      (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2697
1.55M
    if (coeff[tileComp->w]) {
2698
292k
      ++vert;
2699
292k
      vertSign += coeff[tileComp->w] < 0 ? -1 : 1;
2700
292k
    }
2701
1.55M
        }
2702
1.90M
        cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb];
2703
1.90M
        if (cx != 0) {
2704
686k
    if (cb->arithDecoder->decodeBit(cx, cb->stats)) {
2705
278k
      cx = signContext[horizSign][vertSign][0];
2706
278k
      xorBit = signContext[horizSign][vertSign][1];
2707
278k
      if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2708
138k
        *coeff = -1;
2709
139k
      } else {
2710
139k
        *coeff = 1;
2711
139k
      }
2712
278k
    }
2713
686k
    *touched = 1;
2714
686k
        }
2715
1.90M
      }
2716
3.94M
    }
2717
1.30M
  }
2718
127k
      }
2719
58.7k
      ++cb->nextPass;
2720
58.7k
      break;
2721
2722
    //----- magnitude refinement pass
2723
54.1k
    case jpxPassMagRef:
2724
54.1k
      cover(66);
2725
54.1k
      for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched;
2726
164k
     y0 < cb->y1;
2727
110k
     y0 += 4, coeff0 += 4 * tileComp->w,
2728
110k
       touched0 += 4 << resLevel->codeBlockW) {
2729
110k
  for (x = cb->x0, coeff1 = coeff0, touched1 = touched0;
2730
1.23M
       x < cb->x1;
2731
1.12M
       ++x, ++coeff1, ++touched1) {
2732
1.12M
    for (y1 = 0, coeff = coeff1, touched = touched1;
2733
4.44M
         y1 < 4 && y0+y1 < cb->y1;
2734
3.32M
         ++y1, coeff += tileComp->w, touched += resLevel->cbW) {
2735
3.32M
      if (*coeff && !*touched) {
2736
1.83M
        if (*coeff == 1 || *coeff == -1) {
2737
307k
    all = 0;
2738
307k
    if (x > cb->x0) {
2739
290k
      all += coeff[-1] ? 1 : 0;
2740
290k
      if (y0+y1 > cb->y0) {
2741
257k
        all += coeff[-(int)tileComp->w - 1] ? 1 : 0;
2742
257k
      }
2743
290k
      if (y0+y1 < cb->y1 - 1 &&
2744
255k
          (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2745
255k
        all += coeff[tileComp->w - 1] ? 1 : 0;
2746
255k
      }
2747
290k
    }
2748
307k
    if (x < cb->x1 - 1) {
2749
284k
      all += coeff[1] ? 1 : 0;
2750
284k
      if (y0+y1 > cb->y0) {
2751
252k
        all += coeff[-(int)tileComp->w + 1] ? 1 : 0;
2752
252k
      }
2753
284k
      if (y0+y1 < cb->y1 - 1 &&
2754
250k
          (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2755
250k
        all += coeff[tileComp->w + 1] ? 1 : 0;
2756
250k
      }
2757
284k
    }
2758
307k
    if (y0+y1 > cb->y0) {
2759
274k
      all += coeff[-(int)tileComp->w] ? 1 : 0;
2760
274k
    }
2761
307k
    if (y0+y1 < cb->y1 - 1 &&
2762
271k
        (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2763
271k
      all += coeff[tileComp->w] ? 1 : 0;
2764
271k
    }
2765
307k
    cx = all ? 15 : 14;
2766
1.52M
        } else {
2767
1.52M
    cx = 16;
2768
1.52M
        }
2769
1.83M
        bit = cb->arithDecoder->decodeBit(cx, cb->stats);
2770
1.83M
        if (*coeff < 0) {
2771
909k
    *coeff = (*coeff << 1) - bit;
2772
923k
        } else {
2773
923k
    *coeff = (*coeff << 1) + bit;
2774
923k
        }
2775
1.83M
        *touched = 1;
2776
1.83M
      }
2777
3.32M
    }
2778
1.12M
  }
2779
110k
      }
2780
54.1k
      ++cb->nextPass;
2781
54.1k
      break;
2782
2783
    //----- cleanup pass
2784
65.6k
    case jpxPassCleanup:
2785
65.6k
      cover(67);
2786
65.6k
      for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched;
2787
216k
     y0 < cb->y1;
2788
151k
     y0 += 4, coeff0 += 4 * tileComp->w,
2789
151k
       touched0 += 4 << resLevel->codeBlockW) {
2790
151k
  for (x = cb->x0, coeff1 = coeff0, touched1 = touched0;
2791
1.70M
       x < cb->x1;
2792
1.55M
       ++x, ++coeff1, ++touched1) {
2793
1.55M
    y1 = 0;
2794
1.55M
    if (y0 + 3 < cb->y1 &&
2795
980k
        !(*touched1) &&
2796
557k
        !(touched1[resLevel->cbW]) &&
2797
553k
        !(touched1[2 * resLevel->cbW]) &&
2798
551k
        !(touched1[3 * resLevel->cbW]) &&
2799
548k
        (x == cb->x0 || y0 == cb->y0 ||
2800
397k
         !coeff1[-(int)tileComp->w - 1]) &&
2801
481k
        (y0 == cb->y0 ||
2802
382k
         !coeff1[-(int)tileComp->w]) &&
2803
446k
        (x == cb->x1 - 1 || y0 == cb->y0 ||
2804
306k
         !coeff1[-(int)tileComp->w + 1]) &&
2805
427k
        (x == cb->x0 ||
2806
356k
         (!coeff1[-1] &&
2807
327k
    !coeff1[tileComp->w - 1] &&
2808
311k
    !coeff1[2 * tileComp->w - 1] && 
2809
302k
    !coeff1[3 * tileComp->w - 1])) &&
2810
366k
        (x == cb->x1 - 1 ||
2811
306k
         (!coeff1[1] &&
2812
306k
    !coeff1[tileComp->w + 1] &&
2813
306k
    !coeff1[2 * tileComp->w + 1] &&
2814
306k
    !coeff1[3 * tileComp->w + 1])) &&
2815
365k
        ((tileComp->codeBlockStyle & 0x08) ||
2816
362k
         ((x == cb->x0 || y0+4 == cb->y1 ||
2817
256k
     !coeff1[4 * tileComp->w - 1]) &&
2818
362k
    (y0+4 == cb->y1 ||
2819
304k
     !coeff1[4 * tileComp->w]) &&
2820
362k
    (x == cb->x1 - 1 || y0+4 == cb->y1 ||
2821
364k
     !coeff1[4 * tileComp->w + 1])))) {
2822
364k
      if (cb->arithDecoder->decodeBit(jpxContextRunLength, cb->stats)) {
2823
12.0k
        y1 = cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats);
2824
12.0k
        y1 = (y1 << 1) |
2825
12.0k
       cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats);
2826
12.0k
        coeff = &coeff1[y1 * tileComp->w];
2827
12.0k
        cx = signContext[2][2][0];
2828
12.0k
        xorBit = signContext[2][2][1];
2829
12.0k
        if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2830
6.21k
    *coeff = -1;
2831
6.21k
        } else {
2832
5.84k
    *coeff = 1;
2833
5.84k
        }
2834
12.0k
        ++y1;
2835
351k
      } else {
2836
351k
        y1 = 4;
2837
351k
      }
2838
364k
    }
2839
1.55M
    for (coeff = &coeff1[y1 * tileComp->w],
2840
1.55M
     touched = &touched1[y1 << resLevel->codeBlockW];
2841
4.94M
         y1 < 4 && y0 + y1 < cb->y1;
2842
3.39M
         ++y1, coeff += tileComp->w, touched += resLevel->cbW) {
2843
3.39M
      if (!*touched) {
2844
1.25M
        horiz = vert = diag = 0;
2845
1.25M
        horizSign = vertSign = 2;
2846
1.25M
        if (x > cb->x0) {
2847
1.17M
    if (coeff[-1]) {
2848
372k
      ++horiz;
2849
372k
      horizSign += coeff[-1] < 0 ? -1 : 1;
2850
372k
    }
2851
1.17M
    if (y0+y1 > cb->y0) {
2852
871k
      diag += coeff[-(int)tileComp->w - 1] ? 1 : 0;
2853
871k
    }
2854
1.17M
    if (y0+y1 < cb->y1 - 1 &&
2855
860k
        (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2856
860k
      diag += coeff[tileComp->w - 1] ? 1 : 0;
2857
860k
    }
2858
1.17M
        }
2859
1.25M
        if (x < cb->x1 - 1) {
2860
1.15M
    if (coeff[1]) {
2861
13.5k
      ++horiz;
2862
13.5k
      horizSign += coeff[1] < 0 ? -1 : 1;
2863
13.5k
    }
2864
1.15M
    if (y0+y1 > cb->y0) {
2865
849k
      diag += coeff[-(int)tileComp->w + 1] ? 1 : 0;
2866
849k
    }
2867
1.15M
    if (y0+y1 < cb->y1 - 1 &&
2868
836k
        (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2869
836k
      diag += coeff[tileComp->w + 1] ? 1 : 0;
2870
836k
    }
2871
1.15M
        }
2872
1.25M
        if (y0+y1 > cb->y0) {
2873
926k
    if (coeff[-(int)tileComp->w]) {
2874
377k
      ++vert;
2875
377k
      vertSign += coeff[-(int)tileComp->w] < 0 ? -1 : 1;
2876
377k
    }
2877
926k
        }
2878
1.25M
        if (y0+y1 < cb->y1 - 1 &&
2879
912k
      (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) {
2880
912k
    if (coeff[tileComp->w]) {
2881
8.14k
      ++vert;
2882
8.14k
      vertSign += coeff[tileComp->w] < 0 ? -1 : 1;
2883
8.14k
    }
2884
912k
        }
2885
1.25M
        cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb];
2886
1.25M
        if (cb->arithDecoder->decodeBit(cx, cb->stats)) {
2887
403k
    cx = signContext[horizSign][vertSign][0];
2888
403k
    xorBit = signContext[horizSign][vertSign][1];
2889
403k
    if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2890
200k
      *coeff = -1;
2891
202k
    } else {
2892
202k
      *coeff = 1;
2893
202k
    }
2894
403k
        }
2895
2.13M
      } else {
2896
2.13M
        *touched = 0;
2897
2.13M
      }
2898
3.39M
    }
2899
1.55M
  }
2900
151k
      }
2901
65.6k
      ++cb->len;
2902
      // look for a segmentation symbol
2903
65.6k
      if (tileComp->codeBlockStyle & 0x20) {
2904
2.51k
  segSym = cb->arithDecoder->decodeBit(jpxContextUniform,
2905
2.51k
               cb->stats) << 3;
2906
2.51k
  segSym |= cb->arithDecoder->decodeBit(jpxContextUniform,
2907
2.51k
                cb->stats) << 2;
2908
2.51k
  segSym |= cb->arithDecoder->decodeBit(jpxContextUniform,
2909
2.51k
                cb->stats) << 1;
2910
2.51k
  segSym |= cb->arithDecoder->decodeBit(jpxContextUniform,
2911
2.51k
                cb->stats);
2912
2.51k
  if (segSym != 0x0a) {
2913
    // in theory this should be a fatal error, but it seems to
2914
    // be problematic
2915
2.45k
    error(errSyntaxWarning, getPos(),
2916
2.45k
    "Missing or invalid segmentation symbol in JPX stream");
2917
2.45k
  }
2918
2.51k
      }
2919
65.6k
      cb->nextPass = jpxPassSigProp;
2920
65.6k
      break;
2921
178k
    }
2922
2923
178k
    if (tileComp->codeBlockStyle & 0x02) {
2924
48.0k
      cb->stats->reset();
2925
48.0k
      cb->stats->setEntry(jpxContextSigProp, 4, 0);
2926
48.0k
      cb->stats->setEntry(jpxContextRunLength, 3, 0);
2927
48.0k
      cb->stats->setEntry(jpxContextUniform, 46, 0);
2928
48.0k
    }
2929
2930
178k
    if (tileComp->codeBlockStyle & 0x04) {
2931
50.1k
      cb->arithDecoder->cleanup();
2932
50.1k
    }
2933
178k
  }
2934
2935
24.3k
  cb->arithDecoder->cleanup();
2936
24.3k
  return gTrue;
2937
24.3k
}
2938
2939
// Inverse quantization, and wavelet transform (IDWT).  This also does
2940
// the initial shift to convert to fixed point format.
2941
1.42k
void JPXStream::inverseTransform(JPXTileComp *tileComp) {
2942
1.42k
  JPXResLevel *resLevel;
2943
1.42k
  JPXPrecinct *precinct;
2944
1.42k
  JPXSubband *subband;
2945
1.42k
  JPXCodeBlock *cb;
2946
1.42k
  int *coeff0, *coeff;
2947
1.42k
  char *touched0, *touched;
2948
1.42k
  Guint qStyle, guard, eps, shift;
2949
1.42k
  int shift2;
2950
1.42k
  double mu;
2951
1.42k
  int val;
2952
1.42k
  Guint r, pre, cbX, cbY, x, y;
2953
2954
1.42k
  cover(68);
2955
2956
  //----- (NL)LL subband (resolution level 0)
2957
2958
1.42k
  resLevel = &tileComp->resLevels[0];
2959
2960
  // i-quant parameters
2961
1.42k
  qStyle = tileComp->quantStyle & 0x1f;
2962
1.42k
  guard = (tileComp->quantStyle >> 5) & 7;
2963
1.42k
  if (qStyle == 0) {
2964
342
    cover(69);
2965
342
    eps = (tileComp->quantSteps[0] >> 3) & 0x1f;
2966
342
    shift = guard + eps - 1;
2967
342
    mu = 0; // make gcc happy
2968
1.08k
  } else {
2969
1.08k
    cover(70);
2970
1.08k
    shift = guard - 1 + tileComp->prec;
2971
1.08k
    mu = (double)(0x800 + (tileComp->quantSteps[0] & 0x7ff)) / 2048.0;
2972
1.08k
  }
2973
1.42k
  if (tileComp->transform == 0) {
2974
1.01k
    cover(71);
2975
1.01k
    shift += fracBits - tileComp->prec;
2976
1.01k
  }
2977
2978
  // do fixed point adjustment and dequantization on (NL)LL
2979
5.90k
  for (pre = 0; pre < resLevel->nPrecincts; ++pre) {
2980
4.48k
    precinct = &resLevel->precincts[pre];
2981
4.48k
    subband = &precinct->subbands[0];
2982
4.48k
    cb = subband->cbs;
2983
9.36k
    for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2984
9.28k
      for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2985
4.39k
  for (y = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched;
2986
15.9k
       y < cb->y1;
2987
11.5k
       ++y, coeff0 += tileComp->w, touched0 += resLevel->cbW) {
2988
11.5k
    for (x = cb->x0, coeff = coeff0, touched = touched0;
2989
25.3k
         x < cb->x1;
2990
13.8k
         ++x, ++coeff, ++touched) {
2991
13.8k
      val = *coeff;
2992
13.8k
      if (val != 0) {
2993
1.58k
        shift2 = shift - (cb->nZeroBitPlanes + cb->len + *touched);
2994
1.58k
        if (shift2 > 0) {
2995
1.08k
    cover(94);
2996
1.08k
    if (val < 0) {
2997
718
      val = (val << shift2) - (1 << (shift2 - 1));
2998
718
    } else {
2999
371
      val = (val << shift2) + (1 << (shift2 - 1));
3000
371
    }
3001
1.08k
        } else {
3002
492
    cover(95);
3003
492
    val >>= -shift2;
3004
492
        }
3005
1.58k
        if (qStyle == 0) {
3006
817
    cover(96);
3007
817
    if (tileComp->transform == 0) {
3008
0
      cover(97);
3009
0
      val &= -1 << (fracBits - tileComp->prec);
3010
0
    }
3011
817
        } else {
3012
764
    cover(98);
3013
764
    val = (int)((double)val * mu);
3014
764
        }
3015
1.58k
      }
3016
13.8k
      *coeff = val;
3017
13.8k
    }
3018
11.5k
  }
3019
4.39k
  ++cb;
3020
4.39k
      }
3021
4.88k
    }
3022
4.48k
  }
3023
3024
  //----- IDWT for each level
3025
3026
6.35k
  for (r = 1; r <= tileComp->nDecompLevels - reduction; ++r) {
3027
4.93k
    resLevel = &tileComp->resLevels[r];
3028
3029
    // (n)LL is already in the upper-left corner of the
3030
    // tile-component data array -- interleave with (n)HL/LH/HH
3031
    // and inverse transform to get (n-1)LL, which will be stored
3032
    // in the upper-left corner of the tile-component data array
3033
4.93k
    inverseTransformLevel(tileComp, r, resLevel);
3034
4.93k
  }
3035
1.42k
}
3036
3037
// Do one level of the inverse transform:
3038
// - take (n)LL, (n)HL, (n)LH, and (n)HH from the upper-left corner
3039
//   of the tile-component data array
3040
// - leave the resulting (n-1)LL in the same place
3041
void JPXStream::inverseTransformLevel(JPXTileComp *tileComp,
3042
4.93k
              Guint r, JPXResLevel *resLevel) {
3043
4.93k
  JPXPrecinct *precinct;
3044
4.93k
  JPXSubband *subband;
3045
4.93k
  JPXCodeBlock *cb;
3046
4.93k
  int *coeff0, *coeff;
3047
4.93k
  char *touched0, *touched;
3048
4.93k
  Guint qStyle, guard, eps, shift, t;
3049
4.93k
  int shift2;
3050
4.93k
  double mu;
3051
4.93k
  int val;
3052
4.93k
  int *dataPtr, *bufPtr;
3053
4.93k
  Guint nx1, nx2, ny1, ny2, offset;
3054
4.93k
  Guint x, y, sb, pre, cbX, cbY;
3055
3056
4.93k
  qStyle = tileComp->quantStyle & 0x1f;
3057
4.93k
  guard = (tileComp->quantStyle >> 5) & 7;
3058
3059
  //----- compute subband bounds
3060
3061
  //    0   nx1  nx2
3062
  //    |    |    |
3063
  //    v    v    v
3064
  //   +----+----+
3065
  //   | LL | HL | <- 0
3066
  //   +----+----+
3067
  //   | LH | HH | <- ny1
3068
  //   +----+----+
3069
  //               <- ny2
3070
4.93k
  nx1 = resLevel->bx1[1] - resLevel->bx0[1];
3071
4.93k
  nx2 = nx1 + resLevel->bx1[0] - resLevel->bx0[0];
3072
4.93k
  ny1 = resLevel->by1[0] - resLevel->by0[0];
3073
4.93k
  ny2 = ny1 + resLevel->by1[1] - resLevel->by0[1];
3074
4.93k
  if (nx2 == 0 || ny2 == 0) {
3075
1.28k
    return;
3076
1.28k
  }
3077
3078
  //----- fixed-point adjustment and dequantization
3079
3080
14.5k
  for (sb = 0; sb < 3; ++sb) {
3081
3082
    // i-quant parameters
3083
10.9k
    if (qStyle == 0) {
3084
1.29k
      cover(100);
3085
1.29k
      eps = (tileComp->quantSteps[3*r - 2 + sb] >> 3) & 0x1f;
3086
1.29k
      shift = guard + eps - 1;
3087
1.29k
      mu = 0; // make gcc happy
3088
9.65k
    } else {
3089
9.65k
      cover(101);
3090
9.65k
      shift = guard + tileComp->prec;
3091
9.65k
      if (sb == 2) {
3092
3.21k
  cover(102);
3093
3.21k
  ++shift;
3094
3.21k
      }
3095
9.65k
      t = tileComp->quantSteps[qStyle == 1 ? 0 : (3*r - 2 + sb)];
3096
9.65k
      mu = (double)(0x800 + (t & 0x7ff)) / 2048.0;
3097
9.65k
    }
3098
10.9k
    if (tileComp->transform == 0) {
3099
9.08k
      cover(103);
3100
9.08k
      shift += fracBits - tileComp->prec;
3101
9.08k
    }
3102
3103
    // fixed point adjustment and dequantization
3104
3105
23.4k
    for (pre = 0; pre < resLevel->nPrecincts; ++pre) {
3106
12.5k
      precinct = &resLevel->precincts[pre];
3107
12.5k
      subband = &precinct->subbands[sb];
3108
12.5k
      cb = subband->cbs;
3109
42.7k
      for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
3110
78.9k
  for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
3111
48.7k
    for (y = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched;
3112
233k
         y < cb->y1;
3113
185k
         ++y, coeff0 += tileComp->w, touched0 += resLevel->cbW) {
3114
185k
      for (x = cb->x0, coeff = coeff0, touched = touched0;
3115
1.92M
     x < cb->x1;
3116
1.73M
     ++x, ++coeff, ++touched) {
3117
1.73M
        val = *coeff;
3118
1.73M
        if (val != 0) {
3119
478k
    shift2 = shift - (cb->nZeroBitPlanes + cb->len + *touched);
3120
478k
    if (shift2 > 0) {
3121
423k
      cover(74);
3122
423k
      if (val < 0) {
3123
212k
        val = (val << shift2) - (1 << (shift2 - 1));
3124
212k
      } else {
3125
211k
        val = (val << shift2) + (1 << (shift2 - 1));
3126
211k
      }
3127
423k
    } else {
3128
54.6k
      cover(75);
3129
54.6k
      val >>= -shift2;
3130
54.6k
    }
3131
478k
    if (qStyle == 0) {
3132
65.9k
      cover(76);
3133
65.9k
      if (tileComp->transform == 0) {
3134
0
        val &= -1 << (fracBits - tileComp->prec);
3135
0
      }
3136
412k
    } else {
3137
412k
      cover(77);
3138
412k
      val = (int)((double)val * mu);
3139
412k
    }
3140
478k
        }
3141
1.73M
        *coeff = val;
3142
1.73M
      }
3143
185k
    }
3144
48.7k
    ++cb;
3145
48.7k
  }
3146
30.2k
      }
3147
12.5k
    }
3148
10.9k
  }
3149
3150
  //----- inverse transform
3151
3152
  // horizontal (row) transforms
3153
3.64k
  offset = 3 + (resLevel->x0 & 1);
3154
68.8k
  for (y = 0, dataPtr = tileComp->data; y < ny2; ++y, dataPtr += tileComp->w) {
3155
65.2k
    if (resLevel->bx0[0] == resLevel->bx0[1]) {
3156
      // fetch LL/LH
3157
43.3k
      for (x = 0, bufPtr = tileComp->buf + offset;
3158
560k
     x < nx1;
3159
516k
     ++x, bufPtr += 2) {
3160
516k
  *bufPtr = dataPtr[x];
3161
516k
      }
3162
      // fetch HL/HH
3163
43.3k
      for (x = nx1, bufPtr = tileComp->buf + offset + 1;
3164
547k
     x < nx2;
3165
504k
     ++x, bufPtr += 2) {
3166
504k
  *bufPtr = dataPtr[x];
3167
504k
      }
3168
43.3k
    } else {
3169
      // fetch LL/LH
3170
21.8k
      for (x = 0, bufPtr = tileComp->buf + offset + 1;
3171
646k
     x < nx1;
3172
624k
     ++x, bufPtr += 2) {
3173
624k
  *bufPtr = dataPtr[x];
3174
624k
      }
3175
      // fetch HL/HH
3176
21.8k
      for (x = nx1, bufPtr = tileComp->buf + offset;
3177
664k
     x < nx2;
3178
642k
     ++x, bufPtr += 2) {
3179
642k
  *bufPtr = dataPtr[x];
3180
642k
      }
3181
21.8k
    }
3182
65.2k
    inverseTransform1D(tileComp, tileComp->buf, offset, nx2);
3183
2.35M
    for (x = 0, bufPtr = tileComp->buf + offset; x < nx2; ++x, ++bufPtr) {
3184
2.28M
      dataPtr[x] = *bufPtr;
3185
2.28M
    }
3186
65.2k
  }
3187
3188
  // vertical (column) transforms
3189
3.64k
  offset = 3 + (resLevel->y0 & 1);
3190
123k
  for (x = 0, dataPtr = tileComp->data; x < nx2; ++x, ++dataPtr) {
3191
120k
    if (resLevel->by0[0] == resLevel->by0[1]) {
3192
      // fetch LL/HL
3193
21.0k
      for (y = 0, bufPtr = tileComp->buf + offset;
3194
610k
     y < ny1;
3195
589k
     ++y, bufPtr += 2) {
3196
589k
  *bufPtr = dataPtr[y * tileComp->w];
3197
589k
      }
3198
      // fetch LH/HH
3199
21.0k
      for (y = ny1, bufPtr = tileComp->buf + offset + 1;
3200
601k
     y < ny2;
3201
580k
     ++y, bufPtr += 2) {
3202
580k
  *bufPtr = dataPtr[y * tileComp->w];
3203
580k
      }
3204
99.1k
    } else {
3205
      // fetch LL/HL
3206
99.1k
      for (y = 0, bufPtr = tileComp->buf + offset + 1;
3207
615k
     y < ny1;
3208
516k
     ++y, bufPtr += 2) {
3209
516k
  *bufPtr = dataPtr[y * tileComp->w];
3210
516k
      }
3211
      // fetch LH/HH
3212
99.1k
      for (y = ny1, bufPtr = tileComp->buf + offset;
3213
701k
     y < ny2;
3214
602k
     ++y, bufPtr += 2) {
3215
602k
  *bufPtr = dataPtr[y * tileComp->w];
3216
602k
      }
3217
99.1k
    }
3218
120k
    inverseTransform1D(tileComp, tileComp->buf, offset, ny2);
3219
2.40M
    for (y = 0, bufPtr = tileComp->buf + offset; y < ny2; ++y, ++bufPtr) {
3220
2.28M
      dataPtr[y * tileComp->w] = *bufPtr;
3221
2.28M
    }
3222
120k
  }
3223
3.64k
}
3224
3225
void JPXStream::inverseTransform1D(JPXTileComp *tileComp, int *data,
3226
185k
           Guint offset, Guint n) {
3227
185k
  Guint end, i;
3228
3229
  //----- special case for length = 1
3230
185k
  if (n == 1) {
3231
74.4k
    cover(79);
3232
74.4k
    if (offset == 4) {
3233
61.9k
      cover(104);
3234
61.9k
      *data >>= 1;
3235
61.9k
    }
3236
3237
110k
  } else {
3238
110k
    cover(80);
3239
3240
110k
    end = offset + n;
3241
3242
    //----- extend right
3243
110k
    data[end] = data[end - 2];
3244
110k
    if (n == 2) {
3245
19.0k
      cover(81);
3246
19.0k
      data[end+1] = data[offset + 1];
3247
19.0k
      data[end+2] = data[offset];
3248
19.0k
      data[end+3] = data[offset + 1];
3249
91.8k
    } else {
3250
91.8k
      cover(82);
3251
91.8k
      data[end+1] = data[end - 3];
3252
91.8k
      if (n == 3) {
3253
1.07k
  cover(105);
3254
1.07k
  data[end+2] = data[offset + 1];
3255
1.07k
  data[end+3] = data[offset + 2];
3256
90.7k
      } else {
3257
90.7k
  cover(106);
3258
90.7k
  data[end+2] = data[end - 4];
3259
90.7k
  if (n == 4) {
3260
1.97k
    cover(107);
3261
1.97k
    data[end+3] = data[offset + 1];
3262
88.8k
  } else {
3263
88.8k
    cover(108);
3264
88.8k
    data[end+3] = data[end - 5];
3265
88.8k
  }
3266
90.7k
      }
3267
91.8k
    }
3268
3269
    //----- extend left
3270
110k
    data[offset - 1] = data[offset + 1];
3271
110k
    data[offset - 2] = data[offset + 2];
3272
110k
    data[offset - 3] = data[offset + 3];
3273
110k
    if (offset == 4) {
3274
59.1k
      cover(83);
3275
59.1k
      data[0] = data[offset + 4];
3276
59.1k
    }
3277
3278
    //----- 9-7 irreversible filter
3279
3280
110k
    if (tileComp->transform == 0) {
3281
52.3k
      cover(84);
3282
      // step 1 (even)
3283
1.23M
      for (i = 1; i <= end + 2; i += 2) {
3284
1.18M
  data[i] = (int)(idwtKappa * data[i]);
3285
1.18M
      }
3286
      // step 2 (odd)
3287
1.28M
      for (i = 0; i <= end + 3; i += 2) {
3288
1.23M
  data[i] = (int)(idwtIKappa * data[i]);
3289
1.23M
      }
3290
      // step 3 (even)
3291
1.23M
      for (i = 1; i <= end + 2; i += 2) {
3292
1.18M
  data[i] = (int)(data[i] - idwtDelta * (data[i-1] + data[i+1]));
3293
1.18M
      }
3294
      // step 4 (odd)
3295
1.18M
      for (i = 2; i <= end + 1; i += 2) {
3296
1.12M
  data[i] = (int)(data[i] - idwtGamma * (data[i-1] + data[i+1]));
3297
1.12M
      }
3298
      // step 5 (even)
3299
1.12M
      for (i = 3; i <= end; i += 2) {
3300
1.07M
  data[i] = (int)(data[i] - idwtBeta * (data[i-1] + data[i+1]));
3301
1.07M
      }
3302
      // step 6 (odd)
3303
1.07M
      for (i = 4; i <= end - 1; i += 2) {
3304
1.02M
  data[i] = (int)(data[i] - idwtAlpha * (data[i-1] + data[i+1]));
3305
1.02M
      }
3306
3307
    //----- 5-3 reversible filter
3308
3309
58.5k
    } else {
3310
58.5k
      cover(85);
3311
      // step 1 (even)
3312
1.35M
      for (i = 3; i <= end; i += 2) {
3313
1.30M
  data[i] -= (data[i-1] + data[i+1] + 2) >> 2;
3314
1.30M
      }
3315
      // step 2 (odd)
3316
1.30M
      for (i = 4; i < end; i += 2) {
3317
1.24M
  data[i] += (data[i-1] + data[i+1]) >> 1;
3318
1.24M
      }
3319
58.5k
    }
3320
110k
  }
3321
185k
}
3322
3323
// Inverse multi-component transform and DC level shift.  This also
3324
// converts fixed point samples back to integers.
3325
500
GBool JPXStream::inverseMultiCompAndDC(JPXTile *tile) {
3326
500
  JPXTileComp *tileComp;
3327
500
  int coeff, d0, d1, d2, t, minVal, maxVal, zeroVal, shift, rounding;
3328
500
  int *dataPtr;
3329
500
  Guint j, comp, x, y;
3330
3331
  //----- inverse multi-component transform
3332
3333
500
  if (tile->multiComp == 1) {
3334
62
    cover(86);
3335
62
    if (img.nComps < 3 ||
3336
40
  tile->tileComps[0].hSep != tile->tileComps[1].hSep ||
3337
2
  tile->tileComps[0].vSep != tile->tileComps[1].vSep ||
3338
2
  tile->tileComps[1].hSep != tile->tileComps[2].hSep ||
3339
60
  tile->tileComps[1].vSep != tile->tileComps[2].vSep) {
3340
60
      return gFalse;
3341
60
    }
3342
3343
    // inverse irreversible multiple component transform
3344
2
    if (tile->tileComps[0].transform == 0) {
3345
2
      cover(87);
3346
2
      j = 0;
3347
514
      for (y = 0; y < tile->tileComps[0].h; ++y) {
3348
131k
  for (x = 0; x < tile->tileComps[0].w; ++x) {
3349
131k
    d0 = tile->tileComps[0].data[j];
3350
131k
    d1 = tile->tileComps[1].data[j];
3351
131k
    d2 = tile->tileComps[2].data[j];
3352
131k
    tile->tileComps[0].data[j] = (int)(d0 + 1.402 * d2 + 0.5);
3353
131k
    tile->tileComps[1].data[j] =
3354
131k
        (int)(d0 - 0.34413 * d1 - 0.71414 * d2 + 0.5);
3355
131k
    tile->tileComps[2].data[j] = (int)(d0 + 1.772 * d1 + 0.5);
3356
131k
    ++j;
3357
131k
  }
3358
512
      }
3359
3360
    // inverse reversible multiple component transform
3361
2
    } else {
3362
0
      cover(88);
3363
0
      j = 0;
3364
0
      for (y = 0; y < tile->tileComps[0].h; ++y) {
3365
0
  for (x = 0; x < tile->tileComps[0].w; ++x) {
3366
0
    d0 = tile->tileComps[0].data[j];
3367
0
    d1 = tile->tileComps[1].data[j];
3368
0
    d2 = tile->tileComps[2].data[j];
3369
0
    tile->tileComps[1].data[j] = t = d0 - ((d2 + d1) >> 2);
3370
0
    tile->tileComps[0].data[j] = d2 + t;
3371
0
    tile->tileComps[2].data[j] = d1 + t;
3372
0
    ++j;
3373
0
  }
3374
0
      }
3375
0
    }
3376
2
  }
3377
3378
  //----- DC level shift
3379
1.72k
  for (comp = 0; comp < img.nComps; ++comp) {
3380
1.28k
    tileComp = &tile->tileComps[comp];
3381
3382
    // signed: clip
3383
1.28k
    if (tileComp->sgned) {
3384
183
      cover(89);
3385
183
      minVal = -(1 << (tileComp->prec - 1));
3386
183
      maxVal = (1 << (tileComp->prec - 1)) - 1;
3387
183
      dataPtr = tileComp->data;
3388
3.97k
      for (y = 0; y < tileComp->h; ++y) {
3389
735k
  for (x = 0; x < tileComp->w; ++x) {
3390
732k
    coeff = *dataPtr;
3391
732k
    if (tileComp->transform == 0) {
3392
0
      cover(109);
3393
0
      coeff >>= fracBits - tileComp->prec;
3394
0
    }
3395
732k
    if (coeff < minVal) {
3396
330k
      cover(110);
3397
330k
      coeff = minVal;
3398
401k
    } else if (coeff > maxVal) {
3399
389k
      cover(111);
3400
389k
      coeff = maxVal;
3401
389k
    }
3402
732k
    *dataPtr++ = coeff;
3403
732k
  }
3404
3.79k
      }
3405
3406
    // unsigned: inverse DC level shift and clip
3407
1.10k
    } else {
3408
1.10k
      cover(90);
3409
1.10k
      maxVal = (1 << tileComp->prec) - 1;
3410
1.10k
      zeroVal = 1 << (tileComp->prec - 1);
3411
1.10k
      dataPtr = tileComp->data;
3412
1.10k
      shift = fracBits - tileComp->prec;
3413
1.10k
      rounding = 1 << (shift - 1);
3414
33.0k
      for (y = 0; y < tileComp->h; ++y) {
3415
715k
  for (x = 0; x < tileComp->w; ++x) {
3416
683k
    coeff = *dataPtr;
3417
683k
    if (tileComp->transform == 0) {
3418
609k
      cover(112);
3419
609k
      coeff = (coeff + rounding) >> shift;
3420
609k
    }
3421
683k
    coeff += zeroVal;
3422
683k
    if (coeff < 0) {
3423
171k
      cover(113);
3424
171k
      coeff = 0;
3425
511k
    } else if (coeff > maxVal) {
3426
133k
      cover(114);
3427
133k
      coeff = maxVal;
3428
133k
    }
3429
683k
    *dataPtr++ = coeff;
3430
683k
  }
3431
31.8k
      }
3432
1.10k
    }
3433
1.28k
  }
3434
3435
440
  return gTrue;
3436
500
}
3437
3438
5.89k
GBool JPXStream::readBoxHdr(Guint *boxType, Guint *boxLen, Guint *dataLen) {
3439
5.89k
  Guint len, lenH;
3440
3441
5.89k
  if (!readULong(&len) ||
3442
5.15k
      !readULong(boxType)) {
3443
790
    return gFalse;
3444
790
  }
3445
5.10k
  if (len == 1) {
3446
161
    if (!readULong(&lenH) || !readULong(&len)) {
3447
56
      return gFalse;
3448
56
    }
3449
105
    if (lenH) {
3450
105
      error(errSyntaxError, getPos(),
3451
105
      "JPX stream contains a box larger than 2^32 bytes");
3452
105
      return gFalse;
3453
105
    }
3454
0
    *boxLen = len;
3455
0
    *dataLen = len - 16;
3456
4.94k
  } else if (len == 0) {
3457
20
    *boxLen = 0;
3458
20
    *dataLen = 0;
3459
4.92k
  } else {
3460
4.92k
    *boxLen = len;
3461
4.92k
    *dataLen = len - 8;
3462
4.92k
  }
3463
4.94k
  return gTrue;
3464
5.10k
}
3465
3466
14.3k
int JPXStream::readMarkerHdr(int *segType, Guint *segLen) {
3467
14.3k
  int c;
3468
3469
16.0k
  do {
3470
350k
    do {
3471
350k
      if ((c = bufStr->getChar()) == EOF) {
3472
372
  return gFalse;
3473
372
      }
3474
350k
    } while (c != 0xff);
3475
18.6k
    do {
3476
18.6k
      if ((c = bufStr->getChar()) == EOF) {
3477
26
  return gFalse;
3478
26
      }
3479
18.6k
    } while (c == 0xff);
3480
15.6k
  } while (c == 0x00);
3481
13.9k
  *segType = c;
3482
13.9k
  if ((c >= 0x30 && c <= 0x3f) ||
3483
12.5k
      c == 0x4f || c == 0x92 || c == 0x93 || c == 0xd9) {
3484
4.84k
    *segLen = 0;
3485
4.84k
    return gTrue;
3486
4.84k
  }
3487
9.11k
  return readUWord(segLen);
3488
13.9k
}
3489
3490
847k
GBool JPXStream::readUByte(Guint *x) {
3491
847k
  int c0;
3492
3493
847k
  if ((c0 = bufStr->getChar()) == EOF) {
3494
550
    return gFalse;
3495
550
  }
3496
847k
  *x = (Guint)c0;
3497
847k
  return gTrue;
3498
847k
}
3499
3500
714
GBool JPXStream::readByte(int *x) {
3501
714
 int c0;
3502
3503
714
  if ((c0 = bufStr->getChar()) == EOF) {
3504
46
    return gFalse;
3505
46
  }
3506
668
  *x = c0;
3507
668
  if (c0 & 0x80) {
3508
173
    *x |= -1 - 0xff;
3509
173
  }
3510
668
  return gTrue;
3511
714
}
3512
3513
50.3k
GBool JPXStream::readUWord(Guint *x) {
3514
50.3k
  int c0, c1;
3515
3516
50.3k
  if ((c0 = bufStr->getChar()) == EOF ||
3517
50.2k
      (c1 = bufStr->getChar()) == EOF) {
3518
233
    return gFalse;
3519
233
  }
3520
50.1k
  *x = (Guint)((c0 << 8) | c1);
3521
50.1k
  return gTrue;
3522
50.3k
}
3523
3524
32.8k
GBool JPXStream::readULong(Guint *x) {
3525
32.8k
  int c0, c1, c2, c3;
3526
3527
32.8k
  if ((c0 = bufStr->getChar()) == EOF ||
3528
31.8k
      (c1 = bufStr->getChar()) == EOF ||
3529
31.5k
      (c2 = bufStr->getChar()) == EOF ||
3530
31.1k
      (c3 = bufStr->getChar()) == EOF) {
3531
2.03k
    return gFalse;
3532
2.03k
  }
3533
30.8k
  *x = (Guint)((c0 << 24) | (c1 << 16) | (c2 << 8) | c3);
3534
30.8k
  return gTrue;
3535
32.8k
}
3536
3537
0
GBool JPXStream::readNBytes(int nBytes, GBool signd, int *x) {
3538
0
  int y, c, i;
3539
3540
0
  y = 0;
3541
0
  for (i = 0; i < nBytes; ++i) {
3542
0
    if ((c = bufStr->getChar()) == EOF) {
3543
0
      return gFalse;
3544
0
    }
3545
0
    y = (y << 8) + c;
3546
0
  }
3547
0
  if (signd) {
3548
0
    if (y & (1 << (8 * nBytes - 1))) {
3549
0
      y |= -1 << (8 * nBytes);
3550
0
    }
3551
0
  }
3552
0
  *x = y;
3553
0
  return gTrue;
3554
0
}
3555
3556
92.4k
void JPXStream::startBitBuf(Guint byteCountA) {
3557
92.4k
  bitBufLen = 0;
3558
92.4k
  bitBufSkip = gFalse;
3559
92.4k
  byteCount = byteCountA;
3560
92.4k
}
3561
3562
449k
GBool JPXStream::readBits(int nBits, Guint *x) {
3563
449k
  int c;
3564
3565
627k
  while (bitBufLen < nBits) {
3566
177k
    if (byteCount == 0 || (c = bufStr->getChar()) == EOF) {
3567
169
      return gFalse;
3568
169
    }
3569
177k
    --byteCount;
3570
177k
    if (bitBufSkip) {
3571
13.1k
      bitBuf = (bitBuf << 7) | (c & 0x7f);
3572
13.1k
      bitBufLen += 7;
3573
164k
    } else {
3574
164k
      bitBuf = (bitBuf << 8) | (c & 0xff);
3575
164k
      bitBufLen += 8;
3576
164k
    }
3577
177k
    bitBufSkip = c == 0xff;
3578
177k
  }
3579
449k
  *x = (bitBuf >> (bitBufLen - nBits)) & ((1 << nBits) - 1);
3580
449k
  bitBufLen -= nBits;
3581
449k
  return gTrue;
3582
449k
}
3583
3584
66.4k
void JPXStream::skipSOP() {
3585
  // SOP occurs at the start of the packet header, so we don't need to
3586
  // worry about bit-stuff prior to it
3587
66.4k
  if (byteCount >= 6 &&
3588
66.4k
      bufStr->lookChar(0) == 0xff &&
3589
897
      bufStr->lookChar(1) == 0x91) {
3590
0
    bufStr->discardChars(6);
3591
0
    byteCount -= 6;
3592
0
    bitBufLen = 0;
3593
0
    bitBufSkip = gFalse;
3594
0
  }
3595
66.4k
}
3596
3597
66.3k
void JPXStream::skipEPH() {
3598
66.3k
  int k;
3599
3600
66.3k
  k = bitBufSkip ? 1 : 0;
3601
66.3k
  if (byteCount >= (Guint)(k + 2) &&
3602
66.3k
      bufStr->lookChar(k) == 0xff &&
3603
584
      bufStr->lookChar(k + 1) == 0x92) {
3604
0
    bufStr->discardChars(k + 2);
3605
0
    byteCount -= k + 2;
3606
0
    bitBufLen = 0;
3607
0
    bitBufSkip = gFalse;
3608
0
  }
3609
66.3k
}
3610
3611
92.2k
Guint JPXStream::finishBitBuf() {
3612
92.2k
  if (bitBufSkip) {
3613
198
    bufStr->getChar();
3614
198
    --byteCount;
3615
198
  }
3616
92.2k
  return byteCount;
3617
92.2k
}