Coverage Report

Created: 2026-12-01 06:37

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