Coverage Report

Created: 2025-08-26 07:24

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