Coverage Report

Created: 2026-07-12 06:50

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