1 //========================================================================
5 // Copyright 2002-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
17 #include "JArithmeticDecoder.h"
18 #include "JPXStream.h"
23 // - progression order changes
24 // - packed packet headers
25 // - support for palettes, channel maps, etc.
26 // - make sure all needed JP2/JPX subboxes are parsed (readBoxes)
27 // - can we assume that QCC segments must come after the QCD segment?
28 // - skip EPH markers (readTilePartData)
29 // - handle tilePartToEOC in readTilePartData
30 // - deal with multiple codeword segments (readTilePartData,
32 // - progression orders 2, 3, and 4
33 // - in coefficient decoding (readCodeBlockData):
34 // - termination pattern: terminate after every coding pass
35 // - error resilience segmentation symbol
36 // - selective arithmetic coding bypass
37 // - vertically causal context formation
38 // - coeffs longer than 31 bits (should just ignore the extra bits?)
39 // - handle boxes larger than 2^32 bytes
40 // - the fixed-point arithmetic won't handle 16-bit pixels
42 //------------------------------------------------------------------------
44 // number of contexts for the arithmetic decoder
45 #define jpxNContexts 19
47 #define jpxContextSigProp 0 // 0 - 8: significance prop and cleanup
48 #define jpxContextSign 9 // 9 - 13: sign
49 #define jpxContextMagRef 14 // 14 -16: magnitude refinement
50 #define jpxContextRunLength 17 // cleanup: run length
51 #define jpxContextUniform 18 // cleanup: first signif coeff
53 //------------------------------------------------------------------------
55 #define jpxPassSigProp 0
56 #define jpxPassMagRef 1
57 #define jpxPassCleanup 2
59 //------------------------------------------------------------------------
61 // arithmetic decoder context for the significance propagation and
63 // [horiz][vert][diag][subband]
64 // where subband = 0 for HL
67 static Guint sigPropContext[3][3][5][3] = {
68 {{{ 0, 0, 0 }, // horiz=0, vert=0, diag=0
69 { 1, 1, 3 }, // horiz=0, vert=0, diag=1
70 { 2, 2, 6 }, // horiz=0, vert=0, diag=2
71 { 2, 2, 8 }, // horiz=0, vert=0, diag=3
72 { 2, 2, 8 }}, // horiz=0, vert=0, diag=4
73 {{ 5, 3, 1 }, // horiz=0, vert=1, diag=0
74 { 6, 3, 4 }, // horiz=0, vert=1, diag=1
75 { 6, 3, 7 }, // horiz=0, vert=1, diag=2
76 { 6, 3, 8 }, // horiz=0, vert=1, diag=3
77 { 6, 3, 8 }}, // horiz=0, vert=1, diag=4
78 {{ 8, 4, 2 }, // horiz=0, vert=2, diag=0
79 { 8, 4, 5 }, // horiz=0, vert=2, diag=1
80 { 8, 4, 7 }, // horiz=0, vert=2, diag=2
81 { 8, 4, 8 }, // horiz=0, vert=2, diag=3
82 { 8, 4, 8 }}}, // horiz=0, vert=2, diag=4
83 {{{ 3, 5, 1 }, // horiz=1, vert=0, diag=0
84 { 3, 6, 4 }, // horiz=1, vert=0, diag=1
85 { 3, 6, 7 }, // horiz=1, vert=0, diag=2
86 { 3, 6, 8 }, // horiz=1, vert=0, diag=3
87 { 3, 6, 8 }}, // horiz=1, vert=0, diag=4
88 {{ 7, 7, 2 }, // horiz=1, vert=1, diag=0
89 { 7, 7, 5 }, // horiz=1, vert=1, diag=1
90 { 7, 7, 7 }, // horiz=1, vert=1, diag=2
91 { 7, 7, 8 }, // horiz=1, vert=1, diag=3
92 { 7, 7, 8 }}, // horiz=1, vert=1, diag=4
93 {{ 8, 7, 2 }, // horiz=1, vert=2, diag=0
94 { 8, 7, 5 }, // horiz=1, vert=2, diag=1
95 { 8, 7, 7 }, // horiz=1, vert=2, diag=2
96 { 8, 7, 8 }, // horiz=1, vert=2, diag=3
97 { 8, 7, 8 }}}, // horiz=1, vert=2, diag=4
98 {{{ 4, 8, 2 }, // horiz=2, vert=0, diag=0
99 { 4, 8, 5 }, // horiz=2, vert=0, diag=1
100 { 4, 8, 7 }, // horiz=2, vert=0, diag=2
101 { 4, 8, 8 }, // horiz=2, vert=0, diag=3
102 { 4, 8, 8 }}, // horiz=2, vert=0, diag=4
103 {{ 7, 8, 2 }, // horiz=2, vert=1, diag=0
104 { 7, 8, 5 }, // horiz=2, vert=1, diag=1
105 { 7, 8, 7 }, // horiz=2, vert=1, diag=2
106 { 7, 8, 8 }, // horiz=2, vert=1, diag=3
107 { 7, 8, 8 }}, // horiz=2, vert=1, diag=4
108 {{ 8, 8, 2 }, // horiz=2, vert=2, diag=0
109 { 8, 8, 5 }, // horiz=2, vert=2, diag=1
110 { 8, 8, 7 }, // horiz=2, vert=2, diag=2
111 { 8, 8, 8 }, // horiz=2, vert=2, diag=3
112 { 8, 8, 8 }}} // horiz=2, vert=2, diag=4
115 // arithmetic decoder context and xor bit for the sign bit in the
116 // significance propagation pass:
118 // where horiz/vert are offset by 2 (i.e., range is -2 .. 2)
119 // and k = 0 for the context
120 // = 1 for the xor bit
121 static Guint signContext[5][5][2] = {
122 {{ 13, 1 }, // horiz=-2, vert=-2
123 { 13, 1 }, // horiz=-2, vert=-1
124 { 12, 1 }, // horiz=-2, vert= 0
125 { 11, 1 }, // horiz=-2, vert=+1
126 { 11, 1 }}, // horiz=-2, vert=+2
127 {{ 13, 1 }, // horiz=-1, vert=-2
128 { 13, 1 }, // horiz=-1, vert=-1
129 { 12, 1 }, // horiz=-1, vert= 0
130 { 11, 1 }, // horiz=-1, vert=+1
131 { 11, 1 }}, // horiz=-1, vert=+2
132 {{ 10, 1 }, // horiz= 0, vert=-2
133 { 10, 1 }, // horiz= 0, vert=-1
134 { 9, 0 }, // horiz= 0, vert= 0
135 { 10, 0 }, // horiz= 0, vert=+1
136 { 10, 0 }}, // horiz= 0, vert=+2
137 {{ 11, 0 }, // horiz=+1, vert=-2
138 { 11, 0 }, // horiz=+1, vert=-1
139 { 12, 0 }, // horiz=+1, vert= 0
140 { 13, 0 }, // horiz=+1, vert=+1
141 { 13, 0 }}, // horiz=+1, vert=+2
142 {{ 11, 0 }, // horiz=+2, vert=-2
143 { 11, 0 }, // horiz=+2, vert=-1
144 { 12, 0 }, // horiz=+2, vert= 0
145 { 13, 0 }, // horiz=+2, vert=+1
146 { 13, 0 }}, // horiz=+2, vert=+2
149 //------------------------------------------------------------------------
151 // constants used in the IDWT
152 #define idwtAlpha -1.586134342059924
153 #define idwtBeta -0.052980118572961
154 #define idwtGamma 0.882911075530934
155 #define idwtDelta 0.443506852043971
156 #define idwtKappa 1.230174104914001
157 #define idwtIKappa (1.0 / idwtKappa)
159 // number of bits to the right of the decimal point for the fixed
160 // point arithmetic used in the IDWT
163 //------------------------------------------------------------------------
166 #define jpxFloorDiv(x, y) ((x) / (y))
169 #define jpxFloorDivPow2(x, y) ((x) >> (y))
172 #define jpxCeilDiv(x, y) (((x) + (y) - 1) / (y))
175 #define jpxCeilDivPow2(x, y) (((x) + (1 << (y)) - 1) >> (y))
177 //------------------------------------------------------------------------
179 JPXStream::JPXStream(Stream *strA):
186 havePalette = gFalse;
187 haveCompMap = gFalse;
188 haveChannelDefn = gFalse;
197 JPXStream::~JPXStream() {
199 JPXTileComp *tileComp;
200 JPXResLevel *resLevel;
201 JPXPrecinct *precinct;
204 Guint comp, i, k, r, pre, sb;
214 gfree(compMap.pComp);
216 if (haveChannelDefn) {
217 gfree(channelDefn.idx);
218 gfree(channelDefn.type);
219 gfree(channelDefn.assoc);
223 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
224 tile = &img.tiles[i];
225 if (tile->tileComps) {
226 for (comp = 0; comp < img.nComps; ++comp) {
227 tileComp = &tile->tileComps[comp];
228 gfree(tileComp->quantSteps);
229 gfree(tileComp->data);
230 gfree(tileComp->buf);
231 if (tileComp->resLevels) {
232 for (r = 0; r <= tileComp->nDecompLevels; ++r) {
233 resLevel = &tileComp->resLevels[r];
234 if (resLevel->precincts) {
235 for (pre = 0; pre < 1; ++pre) {
236 precinct = &resLevel->precincts[pre];
237 if (precinct->subbands) {
238 for (sb = 0; sb < (r == 0 ? 1 : 3); ++sb) {
239 subband = &precinct->subbands[sb];
240 gfree(subband->inclusion);
241 gfree(subband->zeroBitPlane);
243 for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) {
244 cb = &subband->cbs[k];
246 if (cb->arithDecoder) {
247 delete cb->arithDecoder;
256 gfree(precinct->subbands);
259 gfree(img.tiles[i].tileComps[comp].resLevels[r].precincts);
262 gfree(img.tiles[i].tileComps[comp].resLevels);
265 gfree(img.tiles[i].tileComps);
273 void JPXStream::reset() {
278 // readBoxes reported an error, so we go immediately to EOF
286 int JPXStream::getChar() {
289 if (readBufLen < 8) {
292 if (readBufLen == 8) {
295 } else if (readBufLen > 8) {
296 c = (readBuf >> (readBufLen - 8)) & 0xff;
298 } else if (readBufLen == 0) {
301 c = (readBuf << (8 - readBufLen)) & 0xff;
307 int JPXStream::lookChar() {
310 if (readBufLen < 8) {
313 if (readBufLen == 8) {
315 } else if (readBufLen > 8) {
316 c = (readBuf >> (readBufLen - 8)) & 0xff;
317 } else if (readBufLen == 0) {
320 c = (readBuf << (8 - readBufLen)) & 0xff;
325 void JPXStream::fillReadBuf() {
326 JPXTileComp *tileComp;
327 Guint tileIdx, tx, ty;
331 if (curY >= img.ySize) {
334 tileIdx = ((curY - img.yTileOffset) / img.yTileSize) * img.nXTiles
335 + (curX - img.xTileOffset) / img.xTileSize;
336 #if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
337 tileComp = &img.tiles[tileIdx].tileComps[curComp];
339 tileComp = &img.tiles[tileIdx].tileComps[havePalette ? 0 : curComp];
341 tx = jpxCeilDiv((curX - img.xTileOffset) % img.xTileSize, tileComp->hSep);
342 ty = jpxCeilDiv((curY - img.yTileOffset) % img.yTileSize, tileComp->vSep);
343 pix = (int)tileComp->data[ty * (tileComp->x1 - tileComp->x0) + tx];
344 pixBits = tileComp->prec;
345 #if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
346 if (++curComp == img.nComps) {
349 if (pix >= 0 && pix < palette.nEntries) {
350 pix = palette.c[pix * palette.nComps + curComp];
353 pixBits = palette.bpc[curComp];
355 if (++curComp == (Guint)(havePalette ? palette.nComps : img.nComps)) {
358 if (++curX == img.xSize) {
364 readBuf = (readBuf << 8) | (pix & 0xff);
366 readBuf = (readBuf << pixBits) | (pix & ((1 << pixBits) - 1));
368 readBufLen += pixBits;
369 } while (readBufLen < 8);
372 GString *JPXStream::getPSFilter(int psLevel, char *indent) {
376 GBool JPXStream::isBinary(GBool last) {
377 return str->isBinary(gTrue);
380 void JPXStream::getImageParams(int *bitsPerComponent,
381 StreamColorSpaceMode *csMode) {
382 Guint boxType, boxLen, dataLen, csEnum;
383 Guint bpc1, dummy, i;
384 int csMeth, csPrec, csPrec1, dummy2;
385 StreamColorSpaceMode csMode1;
386 GBool haveBPC, haveCSMode;
388 csPrec = 0; // make gcc happy
389 haveBPC = haveCSMode = gFalse;
391 if (str->lookChar() == 0xff) {
392 getImageParams2(bitsPerComponent, csMode);
394 while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
395 if (boxType == 0x6a703268) { // JP2 header
397 } else if (boxType == 0x69686472) { // image header
398 if (readULong(&dummy) &&
405 *bitsPerComponent = bpc1 + 1;
408 } else if (boxType == 0x636F6C72) { // color specification
409 if (readByte(&csMeth) &&
410 readByte(&csPrec1) &&
413 if (readULong(&csEnum)) {
414 csMode1 = streamCSNone;
415 if (csEnum == jpxCSBiLevel ||
416 csEnum == jpxCSGrayscale) {
417 csMode1 = streamCSDeviceGray;
418 } else if (csEnum == jpxCSCMYK) {
419 csMode1 = streamCSDeviceCMYK;
420 } else if (csEnum == jpxCSsRGB ||
421 csEnum == jpxCSCISesRGB ||
422 csEnum == jpxCSROMMRGB) {
423 csMode1 = streamCSDeviceRGB;
425 if (csMode1 != streamCSNone &&
426 (!haveCSMode || csPrec1 > csPrec)) {
431 for (i = 0; i < dataLen - 7; ++i) {
436 for (i = 0; i < dataLen - 3; ++i) {
441 } else if (boxType == 0x6A703263) { // codestream
442 if (!(haveBPC && haveCSMode)) {
443 getImageParams2(bitsPerComponent, csMode);
447 for (i = 0; i < dataLen; ++i) {
456 // Get image parameters from the codestream.
457 void JPXStream::getImageParams2(int *bitsPerComponent,
458 StreamColorSpaceMode *csMode) {
460 Guint segLen, nComps1, bpc1, dummy, i;
462 while (readMarkerHdr(&segType, &segLen)) {
463 if (segType == 0x51) { // SIZ - image and tile size
464 if (readUWord(&dummy) &&
473 readUWord(&nComps1) &&
475 *bitsPerComponent = (bpc1 & 0x7f) + 1;
476 // if there's no color space info, take a guess
478 *csMode = streamCSDeviceGray;
479 } else if (nComps1 == 3) {
480 *csMode = streamCSDeviceRGB;
481 } else if (nComps1 == 4) {
482 *csMode = streamCSDeviceCMYK;
488 for (i = 0; i < segLen - 2; ++i) {
496 GBool JPXStream::readBoxes() {
497 Guint boxType, boxLen, dataLen;
498 Guint bpc1, compression, unknownColorspace, ipr;
503 // check for a naked JPEG 2000 codestream (without the JP2/JPX
504 // wrapper) -- this appears to be a violation of the PDF spec, but
506 if (str->lookChar() == 0xff) {
507 error(getPos(), "Naked JPEG 2000 codestream, missing JP2/JPX wrapper");
510 bpc = (Guint *)gmallocn(nComps, sizeof(Guint));
511 for (i = 0; i < nComps; ++i) {
512 bpc[i] = img.tiles[0].tileComps[i].prec;
514 width = img.xSize - img.xOffset;
515 height = img.ySize - img.yOffset;
519 while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
521 case 0x6a703268: // JP2 header
522 // this is a grouping box ('superbox') which has no real
523 // contents and doesn't appear to be used consistently, i.e.,
524 // some things which should be subboxes of the JP2 header box
525 // show up outside of it - so we simply ignore the JP2 header
528 case 0x69686472: // image header
529 if (!readULong(&height) ||
530 !readULong(&width) ||
531 !readUWord(&nComps) ||
533 !readUByte(&compression) ||
534 !readUByte(&unknownColorspace) ||
536 error(getPos(), "Unexpected EOF in JPX stream");
539 if (compression != 7) {
540 error(getPos(), "Unknown compression type in JPX stream");
543 bpc = (Guint *)gmallocn(nComps, sizeof(Guint));
544 for (i = 0; i < nComps; ++i) {
549 case 0x62706363: // bits per component
551 error(getPos(), "Found bits per component box before image header box in JPX stream");
554 if (dataLen != nComps) {
555 error(getPos(), "Invalid bits per component box in JPX stream");
558 for (i = 0; i < nComps; ++i) {
559 if (!readUByte(&bpc[i])) {
560 error(getPos(), "Unexpected EOF in JPX stream");
565 case 0x636F6C72: // color specification
566 if (!readColorSpecBox(dataLen)) {
570 case 0x70636c72: // palette
571 if (!readUWord(&palette.nEntries) ||
572 !readUByte(&palette.nComps)) {
573 error(getPos(), "Unexpected EOF in JPX stream");
576 palette.bpc = (Guint *)gmallocn(palette.nComps, sizeof(Guint));
578 (int *)gmallocn(palette.nEntries * palette.nComps, sizeof(int));
579 for (i = 0; i < palette.nComps; ++i) {
580 if (!readUByte(&palette.bpc[i])) {
581 error(getPos(), "Unexpected EOF in JPX stream");
586 for (i = 0; i < palette.nEntries; ++i) {
587 for (j = 0; j < palette.nComps; ++j) {
588 if (!readNBytes(((palette.bpc[j] & 0x7f) + 7) >> 3,
589 (palette.bpc[j] & 0x80) ? gTrue : gFalse,
590 &palette.c[i * palette.nComps + j])) {
591 error(getPos(), "Unexpected EOF in JPX stream");
598 case 0x636d6170: // component mapping
599 compMap.nChannels = dataLen / 4;
600 compMap.comp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
601 compMap.type = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
602 compMap.pComp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint));
603 for (i = 0; i < compMap.nChannels; ++i) {
604 if (!readUWord(&compMap.comp[i]) ||
605 !readUByte(&compMap.type[i]) ||
606 !readUByte(&compMap.pComp[i])) {
607 error(getPos(), "Unexpected EOF in JPX stream");
613 case 0x63646566: // channel definition
614 if (!readUWord(&channelDefn.nChannels)) {
615 error(getPos(), "Unexpected EOF in JPX stream");
619 (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
621 (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
623 (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint));
624 for (i = 0; i < channelDefn.nChannels; ++i) {
625 if (!readUWord(&channelDefn.idx[i]) ||
626 !readUWord(&channelDefn.type[i]) ||
627 !readUWord(&channelDefn.assoc[i])) {
628 error(getPos(), "Unexpected EOF in JPX stream");
632 haveChannelDefn = gTrue;
634 case 0x6A703263: // contiguous codestream
636 error(getPos(), "JPX stream is missing the image header box");
639 error(getPos(), "JPX stream has no supported color spec");
641 if (!readCodestream(dataLen)) {
646 for (i = 0; i < dataLen; ++i) {
647 if (str->getChar() == EOF) {
648 error(getPos(), "Unexpected EOF in JPX stream");
658 GBool JPXStream::readColorSpecBox(Guint dataLen) {
660 Guint csApprox, csEnum;
665 if (!readUByte(&newCS.meth) ||
666 !readByte(&newCS.prec) ||
667 !readUByte(&csApprox)) {
670 switch (newCS.meth) {
671 case 1: // enumerated colorspace
672 if (!readULong(&csEnum)) {
675 newCS.enumerated.type = (JPXColorSpaceType)csEnum;
676 switch (newCS.enumerated.type) {
702 if (dataLen == 7 + 7*4) {
703 if (!readULong(&newCS.enumerated.cieLab.rl) ||
704 !readULong(&newCS.enumerated.cieLab.ol) ||
705 !readULong(&newCS.enumerated.cieLab.ra) ||
706 !readULong(&newCS.enumerated.cieLab.oa) ||
707 !readULong(&newCS.enumerated.cieLab.rb) ||
708 !readULong(&newCS.enumerated.cieLab.ob) ||
709 !readULong(&newCS.enumerated.cieLab.il)) {
712 } else if (dataLen == 7) {
713 //~ this assumes the 8-bit case
714 newCS.enumerated.cieLab.rl = 100;
715 newCS.enumerated.cieLab.ol = 0;
716 newCS.enumerated.cieLab.ra = 255;
717 newCS.enumerated.cieLab.oa = 128;
718 newCS.enumerated.cieLab.rb = 255;
719 newCS.enumerated.cieLab.ob = 96;
720 newCS.enumerated.cieLab.il = 0x00443530;
736 // not allowed in PDF
757 case 2: // restricted ICC profile
758 case 3: // any ICC profile (JPX)
759 case 4: // vendor color (JPX)
760 for (i = 0; i < dataLen - 3; ++i) {
761 if (str->getChar() == EOF) {
768 if (ok && (!haveCS || newCS.prec > cs.prec)) {
776 error(getPos(), "Error in JPX color spec");
780 GBool JPXStream::readCodestream(Guint len) {
782 JPXTileComp *tileComp;
784 GBool haveSIZ, haveCOD, haveQCD, haveSOT;
785 Guint precinctSize, style;
786 Guint segLen, capabilities, nTiles, comp, i, j, r;
789 haveSIZ = haveCOD = haveQCD = haveSOT = gFalse;
791 if (!readMarkerHdr(&segType, &segLen)) {
792 error(getPos(), "Error in JPX codestream");
796 case 0x4f: // SOC - start of codestream
799 case 0x51: // SIZ - image and tile size
800 if (!readUWord(&capabilities) ||
801 !readULong(&img.xSize) ||
802 !readULong(&img.ySize) ||
803 !readULong(&img.xOffset) ||
804 !readULong(&img.yOffset) ||
805 !readULong(&img.xTileSize) ||
806 !readULong(&img.yTileSize) ||
807 !readULong(&img.xTileOffset) ||
808 !readULong(&img.yTileOffset) ||
809 !readUWord(&img.nComps)) {
810 error(getPos(), "Error in JPX SIZ marker segment");
813 if (haveImgHdr && img.nComps != nComps) {
814 error(getPos(), "Different number of components in JPX SIZ marker segment");
817 img.nXTiles = (img.xSize - img.xTileOffset + img.xTileSize - 1)
819 img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1)
821 nTiles = img.nXTiles * img.nYTiles;
822 // check for overflow before allocating memory
823 if (nTiles == 0 || nTiles / img.nXTiles != img.nYTiles) {
824 error(getPos(), "Bad tile count in JPX SIZ marker segment");
827 img.tiles = (JPXTile *)gmallocn(nTiles, sizeof(JPXTile));
828 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
829 img.tiles[i].tileComps = (JPXTileComp *)gmallocn(img.nComps,
830 sizeof(JPXTileComp));
831 for (comp = 0; comp < img.nComps; ++comp) {
832 img.tiles[i].tileComps[comp].quantSteps = NULL;
833 img.tiles[i].tileComps[comp].data = NULL;
834 img.tiles[i].tileComps[comp].buf = NULL;
835 img.tiles[i].tileComps[comp].resLevels = NULL;
838 for (comp = 0; comp < img.nComps; ++comp) {
839 if (!readUByte(&img.tiles[0].tileComps[comp].prec) ||
840 !readUByte(&img.tiles[0].tileComps[comp].hSep) ||
841 !readUByte(&img.tiles[0].tileComps[comp].vSep)) {
842 error(getPos(), "Error in JPX SIZ marker segment");
845 img.tiles[0].tileComps[comp].sgned =
846 (img.tiles[0].tileComps[comp].prec & 0x80) ? gTrue : gFalse;
847 img.tiles[0].tileComps[comp].prec =
848 (img.tiles[0].tileComps[comp].prec & 0x7f) + 1;
849 for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
850 img.tiles[i].tileComps[comp] = img.tiles[0].tileComps[comp];
855 case 0x52: // COD - coding style default
856 if (!readUByte(&img.tiles[0].tileComps[0].style) ||
857 !readUByte(&img.tiles[0].progOrder) ||
858 !readUWord(&img.tiles[0].nLayers) ||
859 !readUByte(&img.tiles[0].multiComp) ||
860 !readUByte(&img.tiles[0].tileComps[0].nDecompLevels) ||
861 !readUByte(&img.tiles[0].tileComps[0].codeBlockW) ||
862 !readUByte(&img.tiles[0].tileComps[0].codeBlockH) ||
863 !readUByte(&img.tiles[0].tileComps[0].codeBlockStyle) ||
864 !readUByte(&img.tiles[0].tileComps[0].transform)) {
865 error(getPos(), "Error in JPX COD marker segment");
868 img.tiles[0].tileComps[0].codeBlockW += 2;
869 img.tiles[0].tileComps[0].codeBlockH += 2;
870 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
872 img.tiles[i].progOrder = img.tiles[0].progOrder;
873 img.tiles[i].nLayers = img.tiles[0].nLayers;
874 img.tiles[i].multiComp = img.tiles[0].multiComp;
876 for (comp = 0; comp < img.nComps; ++comp) {
877 if (!(i == 0 && comp == 0)) {
878 img.tiles[i].tileComps[comp].style =
879 img.tiles[0].tileComps[0].style;
880 img.tiles[i].tileComps[comp].nDecompLevels =
881 img.tiles[0].tileComps[0].nDecompLevels;
882 img.tiles[i].tileComps[comp].codeBlockW =
883 img.tiles[0].tileComps[0].codeBlockW;
884 img.tiles[i].tileComps[comp].codeBlockH =
885 img.tiles[0].tileComps[0].codeBlockH;
886 img.tiles[i].tileComps[comp].codeBlockStyle =
887 img.tiles[0].tileComps[0].codeBlockStyle;
888 img.tiles[i].tileComps[comp].transform =
889 img.tiles[0].tileComps[0].transform;
891 img.tiles[i].tileComps[comp].resLevels =
892 (JPXResLevel *)gmallocn(
893 (img.tiles[i].tileComps[comp].nDecompLevels + 1),
894 sizeof(JPXResLevel));
895 for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
896 img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL;
900 for (r = 0; r <= img.tiles[0].tileComps[0].nDecompLevels; ++r) {
901 if (img.tiles[0].tileComps[0].style & 0x01) {
902 if (!readUByte(&precinctSize)) {
903 error(getPos(), "Error in JPX COD marker segment");
906 img.tiles[0].tileComps[0].resLevels[r].precinctWidth =
908 img.tiles[0].tileComps[0].resLevels[r].precinctHeight =
909 (precinctSize >> 4) & 0x0f;
911 img.tiles[0].tileComps[0].resLevels[r].precinctWidth = 15;
912 img.tiles[0].tileComps[0].resLevels[r].precinctHeight = 15;
915 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
916 for (comp = 0; comp < img.nComps; ++comp) {
917 if (!(i == 0 && comp == 0)) {
918 for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
919 img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
920 img.tiles[0].tileComps[0].resLevels[r].precinctWidth;
921 img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
922 img.tiles[0].tileComps[0].resLevels[r].precinctHeight;
929 case 0x53: // COC - coding style component
931 error(getPos(), "JPX COC marker segment before COD segment");
934 if ((img.nComps > 256 && !readUWord(&comp)) ||
935 (img.nComps <= 256 && !readUByte(&comp)) ||
936 comp >= img.nComps ||
937 !readUByte(&style) ||
938 !readUByte(&img.tiles[0].tileComps[comp].nDecompLevels) ||
939 !readUByte(&img.tiles[0].tileComps[comp].codeBlockW) ||
940 !readUByte(&img.tiles[0].tileComps[comp].codeBlockH) ||
941 !readUByte(&img.tiles[0].tileComps[comp].codeBlockStyle) ||
942 !readUByte(&img.tiles[0].tileComps[comp].transform)) {
943 error(getPos(), "Error in JPX COC marker segment");
946 img.tiles[0].tileComps[comp].style =
947 (img.tiles[0].tileComps[comp].style & ~1) | (style & 1);
948 img.tiles[0].tileComps[comp].codeBlockW += 2;
949 img.tiles[0].tileComps[comp].codeBlockH += 2;
950 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
952 img.tiles[i].tileComps[comp].style =
953 img.tiles[0].tileComps[comp].style;
954 img.tiles[i].tileComps[comp].nDecompLevels =
955 img.tiles[0].tileComps[comp].nDecompLevels;
956 img.tiles[i].tileComps[comp].codeBlockW =
957 img.tiles[0].tileComps[comp].codeBlockW;
958 img.tiles[i].tileComps[comp].codeBlockH =
959 img.tiles[0].tileComps[comp].codeBlockH;
960 img.tiles[i].tileComps[comp].codeBlockStyle =
961 img.tiles[0].tileComps[comp].codeBlockStyle;
962 img.tiles[i].tileComps[comp].transform =
963 img.tiles[0].tileComps[comp].transform;
965 img.tiles[i].tileComps[comp].resLevels =
966 (JPXResLevel *)greallocn(
967 img.tiles[i].tileComps[comp].resLevels,
968 (img.tiles[i].tileComps[comp].nDecompLevels + 1),
969 sizeof(JPXResLevel));
970 for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
971 img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL;
974 for (r = 0; r <= img.tiles[0].tileComps[comp].nDecompLevels; ++r) {
975 if (img.tiles[0].tileComps[comp].style & 0x01) {
976 if (!readUByte(&precinctSize)) {
977 error(getPos(), "Error in JPX COD marker segment");
980 img.tiles[0].tileComps[comp].resLevels[r].precinctWidth =
982 img.tiles[0].tileComps[comp].resLevels[r].precinctHeight =
983 (precinctSize >> 4) & 0x0f;
985 img.tiles[0].tileComps[comp].resLevels[r].precinctWidth = 15;
986 img.tiles[0].tileComps[comp].resLevels[r].precinctHeight = 15;
989 for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
990 for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
991 img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
992 img.tiles[0].tileComps[comp].resLevels[r].precinctWidth;
993 img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
994 img.tiles[0].tileComps[comp].resLevels[r].precinctHeight;
998 case 0x5c: // QCD - quantization default
999 if (!readUByte(&img.tiles[0].tileComps[0].quantStyle)) {
1000 error(getPos(), "Error in JPX QCD marker segment");
1003 if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x00) {
1004 img.tiles[0].tileComps[0].nQuantSteps = segLen - 3;
1005 img.tiles[0].tileComps[0].quantSteps =
1006 (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1007 img.tiles[0].tileComps[0].nQuantSteps,
1009 for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
1010 if (!readUByte(&img.tiles[0].tileComps[0].quantSteps[i])) {
1011 error(getPos(), "Error in JPX QCD marker segment");
1015 } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x01) {
1016 img.tiles[0].tileComps[0].nQuantSteps = 1;
1017 img.tiles[0].tileComps[0].quantSteps =
1018 (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1019 img.tiles[0].tileComps[0].nQuantSteps,
1021 if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[0])) {
1022 error(getPos(), "Error in JPX QCD marker segment");
1025 } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x02) {
1026 img.tiles[0].tileComps[0].nQuantSteps = (segLen - 3) / 2;
1027 img.tiles[0].tileComps[0].quantSteps =
1028 (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps,
1029 img.tiles[0].tileComps[0].nQuantSteps,
1031 for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
1032 if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[i])) {
1033 error(getPos(), "Error in JPX QCD marker segment");
1038 error(getPos(), "Error in JPX QCD marker segment");
1041 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1042 for (comp = 0; comp < img.nComps; ++comp) {
1043 if (!(i == 0 && comp == 0)) {
1044 img.tiles[i].tileComps[comp].quantStyle =
1045 img.tiles[0].tileComps[0].quantStyle;
1046 img.tiles[i].tileComps[comp].nQuantSteps =
1047 img.tiles[0].tileComps[0].nQuantSteps;
1048 img.tiles[i].tileComps[comp].quantSteps =
1049 (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
1050 img.tiles[0].tileComps[0].nQuantSteps,
1052 for (j = 0; j < img.tiles[0].tileComps[0].nQuantSteps; ++j) {
1053 img.tiles[i].tileComps[comp].quantSteps[j] =
1054 img.tiles[0].tileComps[0].quantSteps[j];
1061 case 0x5d: // QCC - quantization component
1063 error(getPos(), "JPX QCC marker segment before QCD segment");
1066 if ((img.nComps > 256 && !readUWord(&comp)) ||
1067 (img.nComps <= 256 && !readUByte(&comp)) ||
1068 comp >= img.nComps ||
1069 !readUByte(&img.tiles[0].tileComps[comp].quantStyle)) {
1070 error(getPos(), "Error in JPX QCC marker segment");
1073 if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x00) {
1074 img.tiles[0].tileComps[comp].nQuantSteps =
1075 segLen - (img.nComps > 256 ? 5 : 4);
1076 img.tiles[0].tileComps[comp].quantSteps =
1077 (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1078 img.tiles[0].tileComps[comp].nQuantSteps,
1080 for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
1081 if (!readUByte(&img.tiles[0].tileComps[comp].quantSteps[i])) {
1082 error(getPos(), "Error in JPX QCC marker segment");
1086 } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x01) {
1087 img.tiles[0].tileComps[comp].nQuantSteps = 1;
1088 img.tiles[0].tileComps[comp].quantSteps =
1089 (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1090 img.tiles[0].tileComps[comp].nQuantSteps,
1092 if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[0])) {
1093 error(getPos(), "Error in JPX QCC marker segment");
1096 } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x02) {
1097 img.tiles[0].tileComps[comp].nQuantSteps =
1098 (segLen - (img.nComps > 256 ? 5 : 4)) / 2;
1099 img.tiles[0].tileComps[comp].quantSteps =
1100 (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
1101 img.tiles[0].tileComps[comp].nQuantSteps,
1103 for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
1104 if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[i])) {
1105 error(getPos(), "Error in JPX QCD marker segment");
1110 error(getPos(), "Error in JPX QCC marker segment");
1113 for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
1114 img.tiles[i].tileComps[comp].quantStyle =
1115 img.tiles[0].tileComps[comp].quantStyle;
1116 img.tiles[i].tileComps[comp].nQuantSteps =
1117 img.tiles[0].tileComps[comp].nQuantSteps;
1118 img.tiles[i].tileComps[comp].quantSteps =
1119 (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
1120 img.tiles[0].tileComps[comp].nQuantSteps,
1122 for (j = 0; j < img.tiles[0].tileComps[comp].nQuantSteps; ++j) {
1123 img.tiles[i].tileComps[comp].quantSteps[j] =
1124 img.tiles[0].tileComps[comp].quantSteps[j];
1128 case 0x5e: // RGN - region of interest
1129 #if 1 //~ ROI is unimplemented
1130 fprintf(stderr, "RGN\n");
1131 for (i = 0; i < segLen - 2; ++i) {
1132 if (str->getChar() == EOF) {
1133 error(getPos(), "Error in JPX PPM marker segment");
1138 if ((img.nComps > 256 && !readUWord(&comp)) ||
1139 (img.nComps <= 256 && !readUByte(&comp)) ||
1140 comp >= img.nComps ||
1141 !readUByte(&compInfo[comp].defROI.style) ||
1142 !readUByte(&compInfo[comp].defROI.shift)) {
1143 error(getPos(), "Error in JPX RGN marker segment");
1148 case 0x5f: // POC - progression order change
1149 #if 1 //~ progression order changes are unimplemented
1150 fprintf(stderr, "POC\n");
1151 for (i = 0; i < segLen - 2; ++i) {
1152 if (str->getChar() == EOF) {
1153 error(getPos(), "Error in JPX PPM marker segment");
1158 nProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
1159 progs = (JPXProgOrder *)gmallocn(nProgs, sizeof(JPXProgOrder));
1160 for (i = 0; i < nProgs; ++i) {
1161 if (!readUByte(&progs[i].startRes) ||
1162 !(img.nComps > 256 && readUWord(&progs[i].startComp)) ||
1163 !(img.nComps <= 256 && readUByte(&progs[i].startComp)) ||
1164 !readUWord(&progs[i].endLayer) ||
1165 !readUByte(&progs[i].endRes) ||
1166 !(img.nComps > 256 && readUWord(&progs[i].endComp)) ||
1167 !(img.nComps <= 256 && readUByte(&progs[i].endComp)) ||
1168 !readUByte(&progs[i].progOrder)) {
1169 error(getPos(), "Error in JPX POC marker segment");
1175 case 0x60: // PPM - packed packet headers, main header
1176 #if 1 //~ packed packet headers are unimplemented
1177 fprintf(stderr, "PPM\n");
1178 for (i = 0; i < segLen - 2; ++i) {
1179 if (str->getChar() == EOF) {
1180 error(getPos(), "Error in JPX PPM marker segment");
1186 case 0x55: // TLM - tile-part lengths
1188 for (i = 0; i < segLen - 2; ++i) {
1189 if (str->getChar() == EOF) {
1190 error(getPos(), "Error in JPX TLM marker segment");
1195 case 0x57: // PLM - packet length, main header
1197 for (i = 0; i < segLen - 2; ++i) {
1198 if (str->getChar() == EOF) {
1199 error(getPos(), "Error in JPX PLM marker segment");
1204 case 0x63: // CRG - component registration
1206 for (i = 0; i < segLen - 2; ++i) {
1207 if (str->getChar() == EOF) {
1208 error(getPos(), "Error in JPX CRG marker segment");
1213 case 0x64: // COM - comment
1215 for (i = 0; i < segLen - 2; ++i) {
1216 if (str->getChar() == EOF) {
1217 error(getPos(), "Error in JPX COM marker segment");
1222 case 0x90: // SOT - start of tile
1226 error(getPos(), "Unknown marker segment %02x in JPX stream", segType);
1227 for (i = 0; i < segLen - 2; ++i) {
1228 if (str->getChar() == EOF) {
1237 error(getPos(), "Missing SIZ marker segment in JPX stream");
1241 error(getPos(), "Missing COD marker segment in JPX stream");
1245 error(getPos(), "Missing QCD marker segment in JPX stream");
1249 //----- read the tile-parts
1251 if (!readTilePart()) {
1254 if (!readMarkerHdr(&segType, &segLen)) {
1255 error(getPos(), "Error in JPX codestream");
1258 if (segType != 0x90) { // SOT - start of tile
1263 if (segType != 0xd9) { // EOC - end of codestream
1264 error(getPos(), "Missing EOC marker in JPX codestream");
1268 //----- finish decoding the image
1269 for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
1270 tile = &img.tiles[i];
1271 for (comp = 0; comp < img.nComps; ++comp) {
1272 tileComp = &tile->tileComps[comp];
1273 inverseTransform(tileComp);
1275 if (!inverseMultiCompAndDC(tile)) {
1280 //~ can free memory below tileComps here, and also tileComp.buf
1285 GBool JPXStream::readTilePart() {
1287 JPXTileComp *tileComp;
1288 JPXResLevel *resLevel;
1289 JPXPrecinct *precinct;
1290 JPXSubband *subband;
1293 Guint tileIdx, tilePartLen, tilePartIdx, nTileParts;
1294 GBool tilePartToEOC;
1295 Guint precinctSize, style;
1296 Guint n, nSBs, nx, ny, sbx0, sby0, comp, segLen;
1297 Guint i, j, k, cbX, cbY, r, pre, sb, cbi;
1300 // process the SOT marker segment
1301 if (!readUWord(&tileIdx) ||
1302 !readULong(&tilePartLen) ||
1303 !readUByte(&tilePartIdx) ||
1304 !readUByte(&nTileParts)) {
1305 error(getPos(), "Error in JPX SOT marker segment");
1309 if (tileIdx >= img.nXTiles * img.nYTiles) {
1310 error(getPos(), "Weird tile index in JPX stream");
1314 tilePartToEOC = tilePartLen == 0;
1315 tilePartLen -= 12; // subtract size of SOT segment
1319 if (!readMarkerHdr(&segType, &segLen)) {
1320 error(getPos(), "Error in JPX tile-part codestream");
1323 tilePartLen -= 2 + segLen;
1325 case 0x52: // COD - coding style default
1326 if (!readUByte(&img.tiles[tileIdx].tileComps[0].style) ||
1327 !readUByte(&img.tiles[tileIdx].progOrder) ||
1328 !readUWord(&img.tiles[tileIdx].nLayers) ||
1329 !readUByte(&img.tiles[tileIdx].multiComp) ||
1330 !readUByte(&img.tiles[tileIdx].tileComps[0].nDecompLevels) ||
1331 !readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockW) ||
1332 !readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockH) ||
1333 !readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockStyle) ||
1334 !readUByte(&img.tiles[tileIdx].tileComps[0].transform)) {
1335 error(getPos(), "Error in JPX COD marker segment");
1338 img.tiles[tileIdx].tileComps[0].codeBlockW += 2;
1339 img.tiles[tileIdx].tileComps[0].codeBlockH += 2;
1340 for (comp = 0; comp < img.nComps; ++comp) {
1342 img.tiles[tileIdx].tileComps[comp].style =
1343 img.tiles[tileIdx].tileComps[0].style;
1344 img.tiles[tileIdx].tileComps[comp].nDecompLevels =
1345 img.tiles[tileIdx].tileComps[0].nDecompLevels;
1346 img.tiles[tileIdx].tileComps[comp].codeBlockW =
1347 img.tiles[tileIdx].tileComps[0].codeBlockW;
1348 img.tiles[tileIdx].tileComps[comp].codeBlockH =
1349 img.tiles[tileIdx].tileComps[0].codeBlockH;
1350 img.tiles[tileIdx].tileComps[comp].codeBlockStyle =
1351 img.tiles[tileIdx].tileComps[0].codeBlockStyle;
1352 img.tiles[tileIdx].tileComps[comp].transform =
1353 img.tiles[tileIdx].tileComps[0].transform;
1355 img.tiles[tileIdx].tileComps[comp].resLevels =
1356 (JPXResLevel *)greallocn(
1357 img.tiles[tileIdx].tileComps[comp].resLevels,
1358 (img.tiles[tileIdx].tileComps[comp].nDecompLevels + 1),
1359 sizeof(JPXResLevel));
1361 r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels;
1363 img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL;
1366 for (r = 0; r <= img.tiles[tileIdx].tileComps[0].nDecompLevels; ++r) {
1367 if (img.tiles[tileIdx].tileComps[0].style & 0x01) {
1368 if (!readUByte(&precinctSize)) {
1369 error(getPos(), "Error in JPX COD marker segment");
1372 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth =
1373 precinctSize & 0x0f;
1374 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight =
1375 (precinctSize >> 4) & 0x0f;
1377 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth = 15;
1378 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight = 15;
1381 for (comp = 1; comp < img.nComps; ++comp) {
1383 r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels;
1385 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
1386 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth;
1387 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
1388 img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight;
1392 case 0x53: // COC - coding style component
1393 if ((img.nComps > 256 && !readUWord(&comp)) ||
1394 (img.nComps <= 256 && !readUByte(&comp)) ||
1395 comp >= img.nComps ||
1396 !readUByte(&style) ||
1397 !readUByte(&img.tiles[tileIdx].tileComps[comp].nDecompLevels) ||
1398 !readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockW) ||
1399 !readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockH) ||
1400 !readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockStyle) ||
1401 !readUByte(&img.tiles[tileIdx].tileComps[comp].transform)) {
1402 error(getPos(), "Error in JPX COC marker segment");
1405 img.tiles[tileIdx].tileComps[comp].style =
1406 (img.tiles[tileIdx].tileComps[comp].style & ~1) | (style & 1);
1407 img.tiles[tileIdx].tileComps[comp].codeBlockW += 2;
1408 img.tiles[tileIdx].tileComps[comp].codeBlockH += 2;
1409 img.tiles[tileIdx].tileComps[comp].resLevels =
1410 (JPXResLevel *)greallocn(
1411 img.tiles[tileIdx].tileComps[comp].resLevels,
1412 (img.tiles[tileIdx].tileComps[comp].nDecompLevels + 1),
1413 sizeof(JPXResLevel));
1414 for (r = 0; r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels; ++r) {
1415 img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL;
1417 for (r = 0; r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels; ++r) {
1418 if (img.tiles[tileIdx].tileComps[comp].style & 0x01) {
1419 if (!readUByte(&precinctSize)) {
1420 error(getPos(), "Error in JPX COD marker segment");
1423 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
1424 precinctSize & 0x0f;
1425 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
1426 (precinctSize >> 4) & 0x0f;
1428 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = 15;
1429 img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = 15;
1433 case 0x5c: // QCD - quantization default
1434 if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantStyle)) {
1435 error(getPos(), "Error in JPX QCD marker segment");
1438 if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x00) {
1439 img.tiles[tileIdx].tileComps[0].nQuantSteps =
1441 img.tiles[tileIdx].tileComps[0].quantSteps =
1442 (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1443 img.tiles[tileIdx].tileComps[0].nQuantSteps,
1445 for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
1446 if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
1447 error(getPos(), "Error in JPX QCD marker segment");
1451 } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x01) {
1452 img.tiles[tileIdx].tileComps[0].nQuantSteps = 1;
1453 img.tiles[tileIdx].tileComps[0].quantSteps =
1454 (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1455 img.tiles[tileIdx].tileComps[0].nQuantSteps,
1457 if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[0])) {
1458 error(getPos(), "Error in JPX QCD marker segment");
1461 } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x02) {
1462 img.tiles[tileIdx].tileComps[0].nQuantSteps = (segLen - 3) / 2;
1463 img.tiles[tileIdx].tileComps[0].quantSteps =
1464 (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
1465 img.tiles[tileIdx].tileComps[0].nQuantSteps,
1467 for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
1468 if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
1469 error(getPos(), "Error in JPX QCD marker segment");
1474 error(getPos(), "Error in JPX QCD marker segment");
1477 for (comp = 1; comp < img.nComps; ++comp) {
1478 img.tiles[tileIdx].tileComps[comp].quantStyle =
1479 img.tiles[tileIdx].tileComps[0].quantStyle;
1480 img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1481 img.tiles[tileIdx].tileComps[0].nQuantSteps;
1482 img.tiles[tileIdx].tileComps[comp].quantSteps =
1483 (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1484 img.tiles[tileIdx].tileComps[0].nQuantSteps,
1486 for (j = 0; j < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++j) {
1487 img.tiles[tileIdx].tileComps[comp].quantSteps[j] =
1488 img.tiles[tileIdx].tileComps[0].quantSteps[j];
1492 case 0x5d: // QCC - quantization component
1493 if ((img.nComps > 256 && !readUWord(&comp)) ||
1494 (img.nComps <= 256 && !readUByte(&comp)) ||
1495 comp >= img.nComps ||
1496 !readUByte(&img.tiles[tileIdx].tileComps[comp].quantStyle)) {
1497 error(getPos(), "Error in JPX QCC marker segment");
1500 if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) == 0x00) {
1501 img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1502 segLen - (img.nComps > 256 ? 5 : 4);
1503 img.tiles[tileIdx].tileComps[comp].quantSteps =
1504 (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1505 img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1507 for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
1508 if (!readUByte(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
1509 error(getPos(), "Error in JPX QCC marker segment");
1513 } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
1515 img.tiles[tileIdx].tileComps[comp].nQuantSteps = 1;
1516 img.tiles[tileIdx].tileComps[comp].quantSteps =
1517 (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1518 img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1520 if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[0])) {
1521 error(getPos(), "Error in JPX QCC marker segment");
1524 } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
1526 img.tiles[tileIdx].tileComps[comp].nQuantSteps =
1527 (segLen - (img.nComps > 256 ? 5 : 4)) / 2;
1528 img.tiles[tileIdx].tileComps[comp].quantSteps =
1529 (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
1530 img.tiles[tileIdx].tileComps[comp].nQuantSteps,
1532 for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
1533 if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
1534 error(getPos(), "Error in JPX QCD marker segment");
1539 error(getPos(), "Error in JPX QCC marker segment");
1543 case 0x5e: // RGN - region of interest
1544 #if 1 //~ ROI is unimplemented
1545 fprintf(stderr, "RGN\n");
1546 for (i = 0; i < segLen - 2; ++i) {
1547 if (str->getChar() == EOF) {
1548 error(getPos(), "Error in JPX PPM marker segment");
1553 if ((img.nComps > 256 && !readUWord(&comp)) ||
1554 (img.nComps <= 256 && !readUByte(&comp)) ||
1555 comp >= img.nComps ||
1556 !readUByte(&compInfo[comp].roi.style) ||
1557 !readUByte(&compInfo[comp].roi.shift)) {
1558 error(getPos(), "Error in JPX RGN marker segment");
1563 case 0x5f: // POC - progression order change
1564 #if 1 //~ progression order changes are unimplemented
1565 fprintf(stderr, "POC\n");
1566 for (i = 0; i < segLen - 2; ++i) {
1567 if (str->getChar() == EOF) {
1568 error(getPos(), "Error in JPX PPM marker segment");
1573 nTileProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
1574 tileProgs = (JPXProgOrder *)gmallocn(nTileProgs, sizeof(JPXProgOrder));
1575 for (i = 0; i < nTileProgs; ++i) {
1576 if (!readUByte(&tileProgs[i].startRes) ||
1577 !(img.nComps > 256 && readUWord(&tileProgs[i].startComp)) ||
1578 !(img.nComps <= 256 && readUByte(&tileProgs[i].startComp)) ||
1579 !readUWord(&tileProgs[i].endLayer) ||
1580 !readUByte(&tileProgs[i].endRes) ||
1581 !(img.nComps > 256 && readUWord(&tileProgs[i].endComp)) ||
1582 !(img.nComps <= 256 && readUByte(&tileProgs[i].endComp)) ||
1583 !readUByte(&tileProgs[i].progOrder)) {
1584 error(getPos(), "Error in JPX POC marker segment");
1590 case 0x61: // PPT - packed packet headers, tile-part hdr
1591 #if 1 //~ packed packet headers are unimplemented
1592 fprintf(stderr, "PPT\n");
1593 for (i = 0; i < segLen - 2; ++i) {
1594 if (str->getChar() == EOF) {
1595 error(getPos(), "Error in JPX PPT marker segment");
1600 case 0x58: // PLT - packet length, tile-part header
1602 for (i = 0; i < segLen - 2; ++i) {
1603 if (str->getChar() == EOF) {
1604 error(getPos(), "Error in JPX PLT marker segment");
1609 case 0x64: // COM - comment
1611 for (i = 0; i < segLen - 2; ++i) {
1612 if (str->getChar() == EOF) {
1613 error(getPos(), "Error in JPX COM marker segment");
1618 case 0x93: // SOD - start of data
1622 error(getPos(), "Unknown marker segment %02x in JPX tile-part stream",
1624 for (i = 0; i < segLen - 2; ++i) {
1625 if (str->getChar() == EOF) {
1633 //----- initialize the tile, precincts, and code-blocks
1634 if (tilePartIdx == 0) {
1635 tile = &img.tiles[tileIdx];
1636 i = tileIdx / img.nXTiles;
1637 j = tileIdx % img.nXTiles;
1638 if ((tile->x0 = img.xTileOffset + j * img.xTileSize) < img.xOffset) {
1639 tile->x0 = img.xOffset;
1641 if ((tile->y0 = img.yTileOffset + i * img.yTileSize) < img.yOffset) {
1642 tile->y0 = img.yOffset;
1644 if ((tile->x1 = img.xTileOffset + (j + 1) * img.xTileSize) > img.xSize) {
1645 tile->x1 = img.xSize;
1647 if ((tile->y1 = img.yTileOffset + (i + 1) * img.yTileSize) > img.ySize) {
1648 tile->y1 = img.ySize;
1654 tile->maxNDecompLevels = 0;
1655 for (comp = 0; comp < img.nComps; ++comp) {
1656 tileComp = &tile->tileComps[comp];
1657 if (tileComp->nDecompLevels > tile->maxNDecompLevels) {
1658 tile->maxNDecompLevels = tileComp->nDecompLevels;
1660 tileComp->x0 = jpxCeilDiv(tile->x0, tileComp->hSep);
1661 tileComp->y0 = jpxCeilDiv(tile->y0, tileComp->hSep);
1662 tileComp->x1 = jpxCeilDiv(tile->x1, tileComp->hSep);
1663 tileComp->y1 = jpxCeilDiv(tile->y1, tileComp->hSep);
1664 tileComp->cbW = 1 << tileComp->codeBlockW;
1665 tileComp->cbH = 1 << tileComp->codeBlockH;
1666 tileComp->data = (int *)gmallocn((tileComp->x1 - tileComp->x0) *
1667 (tileComp->y1 - tileComp->y0),
1669 if (tileComp->x1 - tileComp->x0 > tileComp->y1 - tileComp->y0) {
1670 n = tileComp->x1 - tileComp->x0;
1672 n = tileComp->y1 - tileComp->y0;
1674 tileComp->buf = (int *)gmallocn(n + 8, sizeof(int));
1675 for (r = 0; r <= tileComp->nDecompLevels; ++r) {
1676 resLevel = &tileComp->resLevels[r];
1677 k = r == 0 ? tileComp->nDecompLevels
1678 : tileComp->nDecompLevels - r + 1;
1679 resLevel->x0 = jpxCeilDivPow2(tileComp->x0, k);
1680 resLevel->y0 = jpxCeilDivPow2(tileComp->y0, k);
1681 resLevel->x1 = jpxCeilDivPow2(tileComp->x1, k);
1682 resLevel->y1 = jpxCeilDivPow2(tileComp->y1, k);
1684 resLevel->bx0[0] = resLevel->x0;
1685 resLevel->by0[0] = resLevel->y0;
1686 resLevel->bx1[0] = resLevel->x1;
1687 resLevel->by1[0] = resLevel->y1;
1689 resLevel->bx0[0] = jpxCeilDivPow2(tileComp->x0 - (1 << (k-1)), k);
1690 resLevel->by0[0] = resLevel->y0;
1691 resLevel->bx1[0] = jpxCeilDivPow2(tileComp->x1 - (1 << (k-1)), k);
1692 resLevel->by1[0] = resLevel->y1;
1693 resLevel->bx0[1] = resLevel->x0;
1694 resLevel->by0[1] = jpxCeilDivPow2(tileComp->y0 - (1 << (k-1)), k);
1695 resLevel->bx1[1] = resLevel->x1;
1696 resLevel->by1[1] = jpxCeilDivPow2(tileComp->y1 - (1 << (k-1)), k);
1697 resLevel->bx0[2] = jpxCeilDivPow2(tileComp->x0 - (1 << (k-1)), k);
1698 resLevel->by0[2] = jpxCeilDivPow2(tileComp->y0 - (1 << (k-1)), k);
1699 resLevel->bx1[2] = jpxCeilDivPow2(tileComp->x1 - (1 << (k-1)), k);
1700 resLevel->by1[2] = jpxCeilDivPow2(tileComp->y1 - (1 << (k-1)), k);
1702 resLevel->precincts = (JPXPrecinct *)gmallocn(1, sizeof(JPXPrecinct));
1703 for (pre = 0; pre < 1; ++pre) {
1704 precinct = &resLevel->precincts[pre];
1705 precinct->x0 = resLevel->x0;
1706 precinct->y0 = resLevel->y0;
1707 precinct->x1 = resLevel->x1;
1708 precinct->y1 = resLevel->y1;
1709 nSBs = r == 0 ? 1 : 3;
1710 precinct->subbands =
1711 (JPXSubband *)gmallocn(nSBs, sizeof(JPXSubband));
1712 for (sb = 0; sb < nSBs; ++sb) {
1713 subband = &precinct->subbands[sb];
1714 subband->x0 = resLevel->bx0[sb];
1715 subband->y0 = resLevel->by0[sb];
1716 subband->x1 = resLevel->bx1[sb];
1717 subband->y1 = resLevel->by1[sb];
1718 subband->nXCBs = jpxCeilDivPow2(subband->x1,
1719 tileComp->codeBlockW)
1720 - jpxFloorDivPow2(subband->x0,
1721 tileComp->codeBlockW);
1722 subband->nYCBs = jpxCeilDivPow2(subband->y1,
1723 tileComp->codeBlockH)
1724 - jpxFloorDivPow2(subband->y0,
1725 tileComp->codeBlockH);
1726 n = subband->nXCBs > subband->nYCBs ? subband->nXCBs
1728 for (subband->maxTTLevel = 0, --n;
1730 ++subband->maxTTLevel, n >>= 1) ;
1732 for (level = subband->maxTTLevel; level >= 0; --level) {
1733 nx = jpxCeilDivPow2(subband->nXCBs, level);
1734 ny = jpxCeilDivPow2(subband->nYCBs, level);
1737 subband->inclusion =
1738 (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
1739 subband->zeroBitPlane =
1740 (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
1741 for (k = 0; k < n; ++k) {
1742 subband->inclusion[k].finished = gFalse;
1743 subband->inclusion[k].val = 0;
1744 subband->zeroBitPlane[k].finished = gFalse;
1745 subband->zeroBitPlane[k].val = 0;
1747 subband->cbs = (JPXCodeBlock *)gmallocn(subband->nXCBs *
1749 sizeof(JPXCodeBlock));
1750 sbx0 = jpxFloorDivPow2(subband->x0, tileComp->codeBlockW);
1751 sby0 = jpxFloorDivPow2(subband->y0, tileComp->codeBlockH);
1753 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
1754 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
1755 cb->x0 = (sbx0 + cbX) << tileComp->codeBlockW;
1756 cb->x1 = cb->x0 + tileComp->cbW;
1757 if (subband->x0 > cb->x0) {
1758 cb->x0 = subband->x0;
1760 if (subband->x1 < cb->x1) {
1761 cb->x1 = subband->x1;
1763 cb->y0 = (sby0 + cbY) << tileComp->codeBlockH;
1764 cb->y1 = cb->y0 + tileComp->cbH;
1765 if (subband->y0 > cb->y0) {
1766 cb->y0 = subband->y0;
1768 if (subband->y1 < cb->y1) {
1769 cb->y1 = subband->y1;
1773 cb->nextPass = jpxPassCleanup;
1774 cb->nZeroBitPlanes = 0;
1776 (JPXCoeff *)gmallocn((1 << (tileComp->codeBlockW
1777 + tileComp->codeBlockH)),
1780 cbi < (Guint)(1 << (tileComp->codeBlockW
1781 + tileComp->codeBlockH));
1783 cb->coeffs[cbi].flags = 0;
1784 cb->coeffs[cbi].len = 0;
1785 cb->coeffs[cbi].mag = 0;
1787 cb->arithDecoder = NULL;
1798 return readTilePartData(tileIdx, tilePartLen, tilePartToEOC);
1801 GBool JPXStream::readTilePartData(Guint tileIdx,
1802 Guint tilePartLen, GBool tilePartToEOC) {
1804 JPXTileComp *tileComp;
1805 JPXResLevel *resLevel;
1806 JPXPrecinct *precinct;
1807 JPXSubband *subband;
1810 Guint bits, cbX, cbY, nx, ny, i, j, n, sb;
1813 tile = &img.tiles[tileIdx];
1815 // read all packets from this tile-part
1817 if (tilePartToEOC) {
1818 //~ peek for an EOC marker
1819 } else if (tilePartLen == 0) {
1823 tileComp = &tile->tileComps[tile->comp];
1824 resLevel = &tileComp->resLevels[tile->res];
1825 precinct = &resLevel->precincts[tile->precinct];
1827 //----- packet header
1830 if (!readBits(1, &bits)) {
1834 // packet is empty -- clear all code-block inclusion flags
1835 for (sb = 0; sb < (tile->res == 0 ? 1 : 3); ++sb) {
1836 subband = &precinct->subbands[sb];
1837 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
1838 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
1839 cb = &subband->cbs[cbY * subband->nXCBs + cbX];
1840 cb->included = gFalse;
1846 for (sb = 0; sb < (tile->res == 0 ? 1 : 3); ++sb) {
1847 subband = &precinct->subbands[sb];
1848 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
1849 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
1850 cb = &subband->cbs[cbY * subband->nXCBs + cbX];
1852 // skip code-blocks with no coefficients
1853 if (cb->x0 >= cb->x1 || cb->y0 >= cb->y1) {
1854 cb->included = gFalse;
1858 // code-block inclusion
1860 if (!readBits(1, &cb->included)) {
1866 for (level = subband->maxTTLevel; level >= 0; --level) {
1867 nx = jpxCeilDivPow2(subband->nXCBs, level);
1868 ny = jpxCeilDivPow2(subband->nYCBs, level);
1869 j = i + (cbY >> level) * nx + (cbX >> level);
1870 if (!subband->inclusion[j].finished &&
1871 !subband->inclusion[j].val) {
1872 subband->inclusion[j].val = ttVal;
1874 ttVal = subband->inclusion[j].val;
1876 while (!subband->inclusion[j].finished &&
1877 ttVal <= tile->layer) {
1878 if (!readBits(1, &bits)) {
1882 subband->inclusion[j].finished = gTrue;
1887 subband->inclusion[j].val = ttVal;
1888 if (ttVal > tile->layer) {
1893 cb->included = level < 0;
1898 // zero bit-plane count
1902 for (level = subband->maxTTLevel; level >= 0; --level) {
1903 nx = jpxCeilDivPow2(subband->nXCBs, level);
1904 ny = jpxCeilDivPow2(subband->nYCBs, level);
1905 j = i + (cbY >> level) * nx + (cbX >> level);
1906 if (!subband->zeroBitPlane[j].finished &&
1907 !subband->zeroBitPlane[j].val) {
1908 subband->zeroBitPlane[j].val = ttVal;
1910 ttVal = subband->zeroBitPlane[j].val;
1912 while (!subband->zeroBitPlane[j].finished) {
1913 if (!readBits(1, &bits)) {
1917 subband->zeroBitPlane[j].finished = gTrue;
1922 subband->zeroBitPlane[j].val = ttVal;
1925 cb->nZeroBitPlanes = ttVal;
1928 // number of coding passes
1929 if (!readBits(1, &bits)) {
1933 cb->nCodingPasses = 1;
1935 if (!readBits(1, &bits)) {
1939 cb->nCodingPasses = 2;
1941 if (!readBits(2, &bits)) {
1945 cb->nCodingPasses = 3 + bits;
1947 if (!readBits(5, &bits)) {
1951 cb->nCodingPasses = 6 + bits;
1953 if (!readBits(7, &bits)) {
1956 cb->nCodingPasses = 37 + bits;
1964 if (!readBits(1, &bits)) {
1973 // length of compressed data
1974 //~ deal with multiple codeword segments
1975 for (n = cb->lBlock, i = cb->nCodingPasses >> 1;
1978 if (!readBits(n, &cb->dataLen)) {
1986 tilePartLen -= byteCount;
1991 for (sb = 0; sb < (tile->res == 0 ? 1 : 3); ++sb) {
1992 subband = &precinct->subbands[sb];
1993 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
1994 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
1995 cb = &subband->cbs[cbY * subband->nXCBs + cbX];
1997 if (!readCodeBlockData(tileComp, resLevel, precinct, subband,
1998 tile->res, sb, cb)) {
2001 tilePartLen -= cb->dataLen;
2010 switch (tile->progOrder) {
2011 case 0: // layer, resolution level, component, precinct
2012 if (++tile->comp == img.nComps) {
2014 if (++tile->res == tile->maxNDecompLevels + 1) {
2016 if (++tile->layer == tile->nLayers) {
2022 case 1: // resolution level, layer, component, precinct
2023 if (++tile->comp == img.nComps) {
2025 if (++tile->layer == tile->nLayers) {
2027 if (++tile->res == tile->maxNDecompLevels + 1) {
2033 case 2: // resolution level, precinct, component, layer
2034 //~ this isn't correct -- see B.12.1.3
2035 if (++tile->layer == tile->nLayers) {
2037 if (++tile->comp == img.nComps) {
2039 if (++tile->res == tile->maxNDecompLevels + 1) {
2045 case 3: // precinct, component, resolution level, layer
2046 //~ this isn't correct -- see B.12.1.4
2047 if (++tile->layer == tile->nLayers) {
2049 if (++tile->res == tile->maxNDecompLevels + 1) {
2051 if (++tile->comp == img.nComps) {
2057 case 4: // component, precinct, resolution level, layer
2058 //~ this isn't correct -- see B.12.1.5
2059 if (++tile->layer == tile->nLayers) {
2061 if (++tile->res == tile->maxNDecompLevels + 1) {
2063 if (++tile->comp == img.nComps) {
2075 error(getPos(), "Error in JPX stream");
2079 GBool JPXStream::readCodeBlockData(JPXTileComp *tileComp,
2080 JPXResLevel *resLevel,
2081 JPXPrecinct *precinct,
2082 JPXSubband *subband,
2083 Guint res, Guint sb,
2085 JPXCoeff *coeff0, *coeff1, *coeff;
2086 Guint horiz, vert, diag, all, cx, xorBit;
2087 int horizSign, vertSign;
2088 Guint i, x, y0, y1, y2;
2090 if (cb->arithDecoder) {
2091 cb->arithDecoder->restart(cb->dataLen);
2093 cb->arithDecoder = new JArithmeticDecoder();
2094 cb->arithDecoder->setStream(str, cb->dataLen);
2095 cb->arithDecoder->start();
2096 cb->stats = new JArithmeticDecoderStats(jpxNContexts);
2097 cb->stats->setEntry(jpxContextSigProp, 4, 0);
2098 cb->stats->setEntry(jpxContextRunLength, 3, 0);
2099 cb->stats->setEntry(jpxContextUniform, 46, 0);
2102 for (i = 0; i < cb->nCodingPasses; ++i) {
2103 switch (cb->nextPass) {
2105 //----- significance propagation pass
2106 case jpxPassSigProp:
2107 for (y0 = cb->y0, coeff0 = cb->coeffs;
2109 y0 += 4, coeff0 += 4 << tileComp->codeBlockW) {
2110 for (x = cb->x0, coeff1 = coeff0;
2113 for (y1 = 0, coeff = coeff1;
2114 y1 < 4 && y0+y1 < cb->y1;
2115 ++y1, coeff += tileComp->cbW) {
2116 if (!(coeff->flags & jpxCoeffSignificant)) {
2117 horiz = vert = diag = 0;
2118 horizSign = vertSign = 2;
2120 if (coeff[-1].flags & jpxCoeffSignificant) {
2122 horizSign += (coeff[-1].flags & jpxCoeffSign) ? -1 : 1;
2124 if (y0+y1 > cb->y0) {
2125 diag += (coeff[-(int)tileComp->cbW - 1].flags
2126 >> jpxCoeffSignificantB) & 1;
2128 if (y0+y1 < cb->y1 - 1) {
2129 diag += (coeff[tileComp->cbW - 1].flags
2130 >> jpxCoeffSignificantB) & 1;
2133 if (x < cb->x1 - 1) {
2134 if (coeff[1].flags & jpxCoeffSignificant) {
2136 horizSign += (coeff[1].flags & jpxCoeffSign) ? -1 : 1;
2138 if (y0+y1 > cb->y0) {
2139 diag += (coeff[-(int)tileComp->cbW + 1].flags
2140 >> jpxCoeffSignificantB) & 1;
2142 if (y0+y1 < cb->y1 - 1) {
2143 diag += (coeff[tileComp->cbW + 1].flags
2144 >> jpxCoeffSignificantB) & 1;
2147 if (y0+y1 > cb->y0) {
2148 if (coeff[-(int)tileComp->cbW].flags & jpxCoeffSignificant) {
2150 vertSign += (coeff[-(int)tileComp->cbW].flags & jpxCoeffSign)
2154 if (y0+y1 < cb->y1 - 1) {
2155 if (coeff[tileComp->cbW].flags & jpxCoeffSignificant) {
2157 vertSign += (coeff[tileComp->cbW].flags & jpxCoeffSign)
2161 cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb];
2163 if (cb->arithDecoder->decodeBit(cx, cb->stats)) {
2164 coeff->flags |= jpxCoeffSignificant | jpxCoeffFirstMagRef;
2165 coeff->mag = (coeff->mag << 1) | 1;
2166 cx = signContext[horizSign][vertSign][0];
2167 xorBit = signContext[horizSign][vertSign][1];
2168 if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2169 coeff->flags |= jpxCoeffSign;
2173 coeff->flags |= jpxCoeffTouched;
2182 //----- magnitude refinement pass
2184 for (y0 = cb->y0, coeff0 = cb->coeffs;
2186 y0 += 4, coeff0 += 4 << tileComp->codeBlockW) {
2187 for (x = cb->x0, coeff1 = coeff0;
2190 for (y1 = 0, coeff = coeff1;
2191 y1 < 4 && y0+y1 < cb->y1;
2192 ++y1, coeff += tileComp->cbW) {
2193 if ((coeff->flags & jpxCoeffSignificant) &&
2194 !(coeff->flags & jpxCoeffTouched)) {
2195 if (coeff->flags & jpxCoeffFirstMagRef) {
2198 all += (coeff[-1].flags >> jpxCoeffSignificantB) & 1;
2199 if (y0+y1 > cb->y0) {
2200 all += (coeff[-(int)tileComp->cbW - 1].flags
2201 >> jpxCoeffSignificantB) & 1;
2203 if (y0+y1 < cb->y1 - 1) {
2204 all += (coeff[tileComp->cbW - 1].flags
2205 >> jpxCoeffSignificantB) & 1;
2208 if (x < cb->x1 - 1) {
2209 all += (coeff[1].flags >> jpxCoeffSignificantB) & 1;
2210 if (y0+y1 > cb->y0) {
2211 all += (coeff[-(int)tileComp->cbW + 1].flags
2212 >> jpxCoeffSignificantB) & 1;
2214 if (y0+y1 < cb->y1 - 1) {
2215 all += (coeff[tileComp->cbW + 1].flags
2216 >> jpxCoeffSignificantB) & 1;
2219 if (y0+y1 > cb->y0) {
2220 all += (coeff[-(int)tileComp->cbW].flags
2221 >> jpxCoeffSignificantB) & 1;
2223 if (y0+y1 < cb->y1 - 1) {
2224 all += (coeff[tileComp->cbW].flags
2225 >> jpxCoeffSignificantB) & 1;
2231 coeff->mag = (coeff->mag << 1) |
2232 cb->arithDecoder->decodeBit(cx, cb->stats);
2234 coeff->flags |= jpxCoeffTouched;
2235 coeff->flags &= ~jpxCoeffFirstMagRef;
2243 //----- cleanup pass
2244 case jpxPassCleanup:
2245 for (y0 = cb->y0, coeff0 = cb->coeffs;
2247 y0 += 4, coeff0 += 4 << tileComp->codeBlockW) {
2248 for (x = cb->x0, coeff1 = coeff0;
2252 if (y0 + 3 < cb->y1 &&
2253 !(coeff1->flags & jpxCoeffTouched) &&
2254 !(coeff1[tileComp->cbW].flags & jpxCoeffTouched) &&
2255 !(coeff1[2 * tileComp->cbW].flags & jpxCoeffTouched) &&
2256 !(coeff1[3 * tileComp->cbW].flags & jpxCoeffTouched) &&
2257 (x == cb->x0 || y0 == cb->y0 ||
2258 !(coeff1[-(int)tileComp->cbW - 1].flags
2259 & jpxCoeffSignificant)) &&
2261 !(coeff1[-(int)tileComp->cbW].flags
2262 & jpxCoeffSignificant)) &&
2263 (x == cb->x1 - 1 || y0 == cb->y0 ||
2264 !(coeff1[-(int)tileComp->cbW + 1].flags
2265 & jpxCoeffSignificant)) &&
2267 (!(coeff1[-1].flags & jpxCoeffSignificant) &&
2268 !(coeff1[tileComp->cbW - 1].flags
2269 & jpxCoeffSignificant) &&
2270 !(coeff1[2 * tileComp->cbW - 1].flags
2271 & jpxCoeffSignificant) &&
2272 !(coeff1[3 * tileComp->cbW - 1].flags
2273 & jpxCoeffSignificant))) &&
2275 (!(coeff1[1].flags & jpxCoeffSignificant) &&
2276 !(coeff1[tileComp->cbW + 1].flags
2277 & jpxCoeffSignificant) &&
2278 !(coeff1[2 * tileComp->cbW + 1].flags
2279 & jpxCoeffSignificant) &&
2280 !(coeff1[3 * tileComp->cbW + 1].flags
2281 & jpxCoeffSignificant))) &&
2282 (x == cb->x0 || y0+4 == cb->y1 ||
2283 !(coeff1[4 * tileComp->cbW - 1].flags & jpxCoeffSignificant)) &&
2285 !(coeff1[4 * tileComp->cbW].flags & jpxCoeffSignificant)) &&
2286 (x == cb->x1 - 1 || y0+4 == cb->y1 ||
2287 !(coeff1[4 * tileComp->cbW + 1].flags
2288 & jpxCoeffSignificant))) {
2289 if (cb->arithDecoder->decodeBit(jpxContextRunLength, cb->stats)) {
2290 y1 = cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats);
2292 cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats);
2293 for (y2 = 0, coeff = coeff1;
2295 ++y2, coeff += tileComp->cbW) {
2298 coeff->flags |= jpxCoeffSignificant | jpxCoeffFirstMagRef;
2299 coeff->mag = (coeff->mag << 1) | 1;
2301 cx = signContext[2][2][0];
2302 xorBit = signContext[2][2][1];
2303 if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2304 coeff->flags |= jpxCoeffSign;
2308 for (y1 = 0, coeff = coeff1;
2310 ++y1, coeff += tileComp->cbW) {
2316 for (coeff = &coeff1[y1 << tileComp->codeBlockW];
2317 y1 < 4 && y0 + y1 < cb->y1;
2318 ++y1, coeff += tileComp->cbW) {
2319 if (!(coeff->flags & jpxCoeffTouched)) {
2320 horiz = vert = diag = 0;
2321 horizSign = vertSign = 2;
2323 if (coeff[-1].flags & jpxCoeffSignificant) {
2325 horizSign += (coeff[-1].flags & jpxCoeffSign) ? -1 : 1;
2327 if (y0+y1 > cb->y0) {
2328 diag += (coeff[-(int)tileComp->cbW - 1].flags
2329 >> jpxCoeffSignificantB) & 1;
2331 if (y0+y1 < cb->y1 - 1) {
2332 diag += (coeff[tileComp->cbW - 1].flags
2333 >> jpxCoeffSignificantB) & 1;
2336 if (x < cb->x1 - 1) {
2337 if (coeff[1].flags & jpxCoeffSignificant) {
2339 horizSign += (coeff[1].flags & jpxCoeffSign) ? -1 : 1;
2341 if (y0+y1 > cb->y0) {
2342 diag += (coeff[-(int)tileComp->cbW + 1].flags
2343 >> jpxCoeffSignificantB) & 1;
2345 if (y0+y1 < cb->y1 - 1) {
2346 diag += (coeff[tileComp->cbW + 1].flags
2347 >> jpxCoeffSignificantB) & 1;
2350 if (y0+y1 > cb->y0) {
2351 if (coeff[-(int)tileComp->cbW].flags & jpxCoeffSignificant) {
2353 vertSign += (coeff[-(int)tileComp->cbW].flags & jpxCoeffSign)
2357 if (y0+y1 < cb->y1 - 1) {
2358 if (coeff[tileComp->cbW].flags & jpxCoeffSignificant) {
2360 vertSign += (coeff[tileComp->cbW].flags & jpxCoeffSign)
2364 cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb];
2365 if (cb->arithDecoder->decodeBit(cx, cb->stats)) {
2366 coeff->flags |= jpxCoeffSignificant | jpxCoeffFirstMagRef;
2367 coeff->mag = (coeff->mag << 1) | 1;
2368 cx = signContext[horizSign][vertSign][0];
2369 xorBit = signContext[horizSign][vertSign][1];
2370 if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) {
2371 coeff->flags |= jpxCoeffSign;
2376 coeff->flags &= ~jpxCoeffTouched;
2381 cb->nextPass = jpxPassSigProp;
2386 cb->arithDecoder->cleanup();
2390 // Inverse quantization, and wavelet transform (IDWT). This also does
2391 // the initial shift to convert to fixed point format.
2392 void JPXStream::inverseTransform(JPXTileComp *tileComp) {
2393 JPXResLevel *resLevel;
2394 JPXPrecinct *precinct;
2395 JPXSubband *subband;
2397 JPXCoeff *coeff0, *coeff;
2398 Guint qStyle, guard, eps, shift;
2403 Guint nx0, ny0, nx1, ny1;
2404 Guint r, cbX, cbY, x, y;
2406 //----- (NL)LL subband (resolution level 0)
2408 resLevel = &tileComp->resLevels[0];
2409 precinct = &resLevel->precincts[0];
2410 subband = &precinct->subbands[0];
2412 // i-quant parameters
2413 qStyle = tileComp->quantStyle & 0x1f;
2414 guard = (tileComp->quantStyle >> 5) & 7;
2416 eps = (tileComp->quantSteps[0] >> 3) & 0x1f;
2417 shift = guard + eps - 1;
2418 mu = 0; // make gcc happy
2420 shift = guard - 1 + tileComp->prec;
2421 mu = (double)(0x800 + (tileComp->quantSteps[0] & 0x7ff)) / 2048.0;
2423 if (tileComp->transform == 0) {
2427 // copy (NL)LL into the upper-left corner of the data array, doing
2428 // the fixed point adjustment and dequantization along the way
2430 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2431 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2432 for (y = cb->y0, coeff0 = cb->coeffs;
2434 ++y, coeff0 += tileComp->cbW) {
2435 dataPtr = &tileComp->data[(y - subband->y0)
2436 * (tileComp->x1 - tileComp->x0)
2437 + (cb->x0 - subband->x0)];
2438 for (x = cb->x0, coeff = coeff0; x < cb->x1; ++x, ++coeff) {
2439 val = (int)coeff->mag;
2441 shift2 = shift - (cb->nZeroBitPlanes + coeff->len);
2443 val = (val << shift2) + (1 << (shift2 - 1));
2448 if (tileComp->transform == 0) {
2449 val &= -1 << fracBits;
2452 val = (int)((double)val * mu);
2454 if (coeff->flags & jpxCoeffSign) {
2465 //----- IDWT for each level
2467 for (r = 1; r <= tileComp->nDecompLevels; ++r) {
2468 resLevel = &tileComp->resLevels[r];
2470 // (n)LL is already in the upper-left corner of the
2471 // tile-component data array -- interleave with (n)HL/LH/HH
2472 // and inverse transform to get (n-1)LL, which will be stored
2473 // in the upper-left corner of the tile-component data array
2474 if (r == tileComp->nDecompLevels) {
2480 nx0 = tileComp->resLevels[r+1].x0;
2481 ny0 = tileComp->resLevels[r+1].y0;
2482 nx1 = tileComp->resLevels[r+1].x1;
2483 ny1 = tileComp->resLevels[r+1].y1;
2485 inverseTransformLevel(tileComp, r, resLevel, nx0, ny0, nx1, ny1);
2489 // Do one level of the inverse transform:
2490 // - take (n)LL from the tile-component data array
2491 // - take (n)HL/LH/HH from <resLevel>
2492 // - leave the resulting (n-1)LL in the tile-component data array
2493 void JPXStream::inverseTransformLevel(JPXTileComp *tileComp,
2494 Guint r, JPXResLevel *resLevel,
2495 Guint nx0, Guint ny0,
2496 Guint nx1, Guint ny1) {
2497 JPXPrecinct *precinct;
2498 JPXSubband *subband;
2500 JPXCoeff *coeff0, *coeff;
2501 Guint qStyle, guard, eps, shift, t;
2507 Guint x, y, sb, cbX, cbY;
2513 for (yy = resLevel->y1 - 1; yy >= (int)resLevel->y0; --yy) {
2514 for (xx = resLevel->x1 - 1; xx >= (int)resLevel->x0; --xx) {
2515 tileComp->data[(2 * yy - ny0) * (tileComp->x1 - tileComp->x0)
2517 tileComp->data[(yy - resLevel->y0) * (tileComp->x1 - tileComp->x0)
2518 + (xx - resLevel->x0)];
2522 // i-quant parameters
2523 qStyle = tileComp->quantStyle & 0x1f;
2524 guard = (tileComp->quantStyle >> 5) & 7;
2526 // interleave HL/LH/HH
2527 precinct = &resLevel->precincts[0];
2528 for (sb = 0; sb < 3; ++sb) {
2530 // i-quant parameters
2532 eps = (tileComp->quantSteps[3*r - 2 + sb] >> 3) & 0x1f;
2533 shift = guard + eps - 1;
2534 mu = 0; // make gcc happy
2536 shift = guard + tileComp->prec;
2540 t = tileComp->quantSteps[qStyle == 1 ? 0 : (3*r - 2 + sb)];
2541 mu = (double)(0x800 + (t & 0x7ff)) / 2048.0;
2543 if (tileComp->transform == 0) {
2547 // copy the subband coefficients into the data array, doing the
2548 // fixed point adjustment and dequantization along the way
2549 xo = (sb & 1) ? 0 : 1;
2550 yo = (sb > 0) ? 1 : 0;
2551 subband = &precinct->subbands[sb];
2553 for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
2554 for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
2555 for (y = cb->y0, coeff0 = cb->coeffs;
2557 ++y, coeff0 += tileComp->cbW) {
2558 dataPtr = &tileComp->data[(2 * y + yo - ny0)
2559 * (tileComp->x1 - tileComp->x0)
2560 + (2 * cb->x0 + xo - nx0)];
2561 for (x = cb->x0, coeff = coeff0; x < cb->x1; ++x, ++coeff) {
2562 val = (int)coeff->mag;
2564 shift2 = shift - (cb->nZeroBitPlanes + coeff->len);
2566 val = (val << shift2) + (1 << (shift2 - 1));
2571 if (tileComp->transform == 0) {
2572 val &= -1 << fracBits;
2575 val = (int)((double)val * mu);
2577 if (coeff->flags & jpxCoeffSign) {
2590 //----- horizontal (row) transforms
2591 dataPtr = tileComp->data;
2592 for (y = 0; y < ny1 - ny0; ++y) {
2593 inverseTransform1D(tileComp, dataPtr, 1, nx0, nx1);
2594 dataPtr += tileComp->x1 - tileComp->x0;
2597 //----- vertical (column) transforms
2598 dataPtr = tileComp->data;
2599 for (x = 0; x < nx1 - nx0; ++x) {
2600 inverseTransform1D(tileComp, dataPtr,
2601 tileComp->x1 - tileComp->x0, ny0, ny1);
2606 void JPXStream::inverseTransform1D(JPXTileComp *tileComp,
2607 int *data, Guint stride,
2608 Guint i0, Guint i1) {
2610 Guint offset, end, i;
2612 //----- special case for length = 1
2620 // choose an offset: this makes even buf[] indexes correspond to
2621 // odd values of i, and vice versa
2622 offset = 3 + (i0 & 1);
2623 end = offset + i1 - i0;
2626 buf = tileComp->buf;
2627 for (i = 0; i < i1 - i0; ++i) {
2628 buf[offset + i] = data[i * stride];
2631 //----- extend right
2632 buf[end] = buf[end - 2];
2634 buf[end+1] = buf[offset + 1];
2635 buf[end+2] = buf[offset];
2636 buf[end+3] = buf[offset + 1];
2638 buf[end+1] = buf[end - 3];
2640 buf[end+2] = buf[offset + 1];
2641 buf[end+3] = buf[offset + 2];
2643 buf[end+2] = buf[end - 4];
2645 buf[end+3] = buf[offset + 1];
2647 buf[end+3] = buf[end - 5];
2653 buf[offset - 1] = buf[offset + 1];
2654 buf[offset - 2] = buf[offset + 2];
2655 buf[offset - 3] = buf[offset + 3];
2657 buf[0] = buf[offset + 4];
2660 //----- 9-7 irreversible filter
2662 if (tileComp->transform == 0) {
2664 for (i = 1; i <= end + 2; i += 2) {
2665 buf[i] = (int)(idwtKappa * buf[i]);
2668 for (i = 0; i <= end + 3; i += 2) {
2669 buf[i] = (int)(idwtIKappa * buf[i]);
2672 for (i = 1; i <= end + 2; i += 2) {
2673 buf[i] = (int)(buf[i] - idwtDelta * (buf[i-1] + buf[i+1]));
2676 for (i = 2; i <= end + 1; i += 2) {
2677 buf[i] = (int)(buf[i] - idwtGamma * (buf[i-1] + buf[i+1]));
2680 for (i = 3; i <= end; i += 2) {
2681 buf[i] = (int)(buf[i] - idwtBeta * (buf[i-1] + buf[i+1]));
2684 for (i = 4; i <= end - 1; i += 2) {
2685 buf[i] = (int)(buf[i] - idwtAlpha * (buf[i-1] + buf[i+1]));
2688 //----- 5-3 reversible filter
2692 for (i = 3; i <= end; i += 2) {
2693 buf[i] -= (buf[i-1] + buf[i+1] + 2) >> 2;
2696 for (i = 4; i < end; i += 2) {
2697 buf[i] += (buf[i-1] + buf[i+1]) >> 1;
2702 for (i = 0; i < i1 - i0; ++i) {
2703 data[i * stride] = buf[offset + i];
2708 // Inverse multi-component transform and DC level shift. This also
2709 // converts fixed point samples back to integers.
2710 GBool JPXStream::inverseMultiCompAndDC(JPXTile *tile) {
2711 JPXTileComp *tileComp;
2712 int coeff, d0, d1, d2, t, minVal, maxVal, zeroVal;
2714 Guint j, comp, x, y;
2716 //----- inverse multi-component transform
2718 if (tile->multiComp == 1) {
2719 if (img.nComps < 3 ||
2720 tile->tileComps[0].hSep != tile->tileComps[1].hSep ||
2721 tile->tileComps[0].vSep != tile->tileComps[1].vSep ||
2722 tile->tileComps[1].hSep != tile->tileComps[2].hSep ||
2723 tile->tileComps[1].vSep != tile->tileComps[2].vSep) {
2727 // inverse irreversible multiple component transform
2728 if (tile->tileComps[0].transform == 0) {
2730 for (y = 0; y < tile->tileComps[0].y1 - tile->tileComps[0].y0; ++y) {
2731 for (x = 0; x < tile->tileComps[0].x1 - tile->tileComps[0].x0; ++x) {
2732 d0 = tile->tileComps[0].data[j];
2733 d1 = tile->tileComps[1].data[j];
2734 d2 = tile->tileComps[2].data[j];
2735 tile->tileComps[0].data[j] = (int)(d0 + 1.402 * d2 + 0.5);
2736 tile->tileComps[1].data[j] =
2737 (int)(d0 - 0.34413 * d1 - 0.71414 * d2 + 0.5);
2738 tile->tileComps[2].data[j] = (int)(d0 + 1.772 * d1 + 0.5);
2743 // inverse reversible multiple component transform
2746 for (y = 0; y < tile->tileComps[0].y1 - tile->tileComps[0].y0; ++y) {
2747 for (x = 0; x < tile->tileComps[0].x1 - tile->tileComps[0].x0; ++x) {
2748 d0 = tile->tileComps[0].data[j];
2749 d1 = tile->tileComps[1].data[j];
2750 d2 = tile->tileComps[2].data[j];
2751 tile->tileComps[1].data[j] = t = d0 - ((d2 + d1) >> 2);
2752 tile->tileComps[0].data[j] = d2 + t;
2753 tile->tileComps[2].data[j] = d1 + t;
2760 //----- DC level shift
2761 for (comp = 0; comp < img.nComps; ++comp) {
2762 tileComp = &tile->tileComps[comp];
2765 if (tileComp->sgned) {
2766 minVal = -(1 << (tileComp->prec - 1));
2767 maxVal = (1 << (tileComp->prec - 1)) - 1;
2768 dataPtr = tileComp->data;
2769 for (y = 0; y < tileComp->y1 - tileComp->y0; ++y) {
2770 for (x = 0; x < tileComp->x1 - tileComp->x0; ++x) {
2772 if (tileComp->transform == 0) {
2775 if (coeff < minVal) {
2777 } else if (coeff > maxVal) {
2784 // unsigned: inverse DC level shift and clip
2786 maxVal = (1 << tileComp->prec) - 1;
2787 zeroVal = 1 << (tileComp->prec - 1);
2788 dataPtr = tileComp->data;
2789 for (y = 0; y < tileComp->y1 - tileComp->y0; ++y) {
2790 for (x = 0; x < tileComp->x1 - tileComp->x0; ++x) {
2792 if (tileComp->transform == 0) {
2798 } else if (coeff > maxVal) {
2810 GBool JPXStream::readBoxHdr(Guint *boxType, Guint *boxLen, Guint *dataLen) {
2813 if (!readULong(&len) ||
2814 !readULong(boxType)) {
2818 if (!readULong(&lenH) || !readULong(&len)) {
2822 error(getPos(), "JPX stream contains a box larger than 2^32 bytes");
2826 *dataLen = len - 16;
2827 } else if (len == 0) {
2837 int JPXStream::readMarkerHdr(int *segType, Guint *segLen) {
2842 if ((c = str->getChar()) == EOF) {
2845 } while (c != 0xff);
2847 if ((c = str->getChar()) == EOF) {
2850 } while (c == 0xff);
2851 } while (c == 0x00);
2853 if ((c >= 0x30 && c <= 0x3f) ||
2854 c == 0x4f || c == 0x92 || c == 0x93 || c == 0xd9) {
2858 return readUWord(segLen);
2861 GBool JPXStream::readUByte(Guint *x) {
2864 if ((c0 = str->getChar()) == EOF) {
2871 GBool JPXStream::readByte(int *x) {
2874 if ((c0 = str->getChar()) == EOF) {
2884 GBool JPXStream::readUWord(Guint *x) {
2887 if ((c0 = str->getChar()) == EOF ||
2888 (c1 = str->getChar()) == EOF) {
2891 *x = (Guint)((c0 << 8) | c1);
2895 GBool JPXStream::readULong(Guint *x) {
2898 if ((c0 = str->getChar()) == EOF ||
2899 (c1 = str->getChar()) == EOF ||
2900 (c2 = str->getChar()) == EOF ||
2901 (c3 = str->getChar()) == EOF) {
2904 *x = (Guint)((c0 << 24) | (c1 << 16) | (c2 << 8) | c3);
2908 GBool JPXStream::readNBytes(int nBytes, GBool signd, int *x) {
2912 for (i = 0; i < nBytes; ++i) {
2913 if ((c = str->getChar()) == EOF) {
2919 if (y & (1 << (8 * nBytes - 1))) {
2920 y |= -1 << (8 * nBytes);
2927 GBool JPXStream::readBits(int nBits, Guint *x) {
2930 while (bitBufLen < nBits) {
2931 if ((c = str->getChar()) == EOF) {
2936 bitBuf = (bitBuf << 7) | (c & 0x7f);
2939 bitBuf = (bitBuf << 8) | (c & 0xff);
2942 bitBufSkip = c == 0xff;
2944 *x = (bitBuf >> (bitBufLen - nBits)) & ((1 << nBits) - 1);
2949 void JPXStream::clearBitBuf() {
2951 bitBufSkip = gFalse;