Coverage Report

Created: 2026-08-13 06:56

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