blob: e4ac94fc46fdaf67ba926d68892db40f1e177f9d [file] [log] [blame]
//========================================================================
//
// JPXStream.cc
//
// Copyright 2002-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2008, 2012 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2012 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2012 Even Rouault <even.rouault@mines-paris.org>
// Copyright (C) 2019 Robert Niemi <robert.den.klurige@gmail.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include <limits.h>
#include "gmem.h"
#include "Error.h"
#include "JArithmeticDecoder.h"
#include "JPXStream.h"
//~ to do:
// - precincts
// - ROI
// - progression order changes
// - packed packet headers
// - support for palettes, channel maps, etc.
// - make sure all needed JP2/JPX subboxes are parsed (readBoxes)
// - can we assume that QCC segments must come after the QCD segment?
// - handle tilePartToEOC in readTilePartData
// - progression orders 2, 3, and 4
// - in coefficient decoding (readCodeBlockData):
// - selective arithmetic coding bypass
// (this also affects reading the cb->dataLen array)
// - coeffs longer than 31 bits (should just ignore the extra bits?)
// - handle boxes larger than 2^32 bytes
// - the fixed-point arithmetic won't handle 16-bit pixels
//------------------------------------------------------------------------
// number of contexts for the arithmetic decoder
#define jpxNContexts 19
#define jpxContextSigProp 0 // 0 - 8: significance prop and cleanup
#define jpxContextSign 9 // 9 - 13: sign
#define jpxContextMagRef 14 // 14 -16: magnitude refinement
#define jpxContextRunLength 17 // cleanup: run length
#define jpxContextUniform 18 // cleanup: first signif coeff
//------------------------------------------------------------------------
#define jpxPassSigProp 0
#define jpxPassMagRef 1
#define jpxPassCleanup 2
//------------------------------------------------------------------------
// arithmetic decoder context for the significance propagation and
// cleanup passes:
// [horiz][vert][diag][subband]
// where subband = 0 for HL
// = 1 for LH and LL
// = 2 for HH
static const unsigned int sigPropContext[3][3][5][3] = {
{{{ 0, 0, 0 }, // horiz=0, vert=0, diag=0
{ 1, 1, 3 }, // horiz=0, vert=0, diag=1
{ 2, 2, 6 }, // horiz=0, vert=0, diag=2
{ 2, 2, 8 }, // horiz=0, vert=0, diag=3
{ 2, 2, 8 }}, // horiz=0, vert=0, diag=4
{{ 5, 3, 1 }, // horiz=0, vert=1, diag=0
{ 6, 3, 4 }, // horiz=0, vert=1, diag=1
{ 6, 3, 7 }, // horiz=0, vert=1, diag=2
{ 6, 3, 8 }, // horiz=0, vert=1, diag=3
{ 6, 3, 8 }}, // horiz=0, vert=1, diag=4
{{ 8, 4, 2 }, // horiz=0, vert=2, diag=0
{ 8, 4, 5 }, // horiz=0, vert=2, diag=1
{ 8, 4, 7 }, // horiz=0, vert=2, diag=2
{ 8, 4, 8 }, // horiz=0, vert=2, diag=3
{ 8, 4, 8 }}}, // horiz=0, vert=2, diag=4
{{{ 3, 5, 1 }, // horiz=1, vert=0, diag=0
{ 3, 6, 4 }, // horiz=1, vert=0, diag=1
{ 3, 6, 7 }, // horiz=1, vert=0, diag=2
{ 3, 6, 8 }, // horiz=1, vert=0, diag=3
{ 3, 6, 8 }}, // horiz=1, vert=0, diag=4
{{ 7, 7, 2 }, // horiz=1, vert=1, diag=0
{ 7, 7, 5 }, // horiz=1, vert=1, diag=1
{ 7, 7, 7 }, // horiz=1, vert=1, diag=2
{ 7, 7, 8 }, // horiz=1, vert=1, diag=3
{ 7, 7, 8 }}, // horiz=1, vert=1, diag=4
{{ 8, 7, 2 }, // horiz=1, vert=2, diag=0
{ 8, 7, 5 }, // horiz=1, vert=2, diag=1
{ 8, 7, 7 }, // horiz=1, vert=2, diag=2
{ 8, 7, 8 }, // horiz=1, vert=2, diag=3
{ 8, 7, 8 }}}, // horiz=1, vert=2, diag=4
{{{ 4, 8, 2 }, // horiz=2, vert=0, diag=0
{ 4, 8, 5 }, // horiz=2, vert=0, diag=1
{ 4, 8, 7 }, // horiz=2, vert=0, diag=2
{ 4, 8, 8 }, // horiz=2, vert=0, diag=3
{ 4, 8, 8 }}, // horiz=2, vert=0, diag=4
{{ 7, 8, 2 }, // horiz=2, vert=1, diag=0
{ 7, 8, 5 }, // horiz=2, vert=1, diag=1
{ 7, 8, 7 }, // horiz=2, vert=1, diag=2
{ 7, 8, 8 }, // horiz=2, vert=1, diag=3
{ 7, 8, 8 }}, // horiz=2, vert=1, diag=4
{{ 8, 8, 2 }, // horiz=2, vert=2, diag=0
{ 8, 8, 5 }, // horiz=2, vert=2, diag=1
{ 8, 8, 7 }, // horiz=2, vert=2, diag=2
{ 8, 8, 8 }, // horiz=2, vert=2, diag=3
{ 8, 8, 8 }}} // horiz=2, vert=2, diag=4
};
// arithmetic decoder context and xor bit for the sign bit in the
// significance propagation pass:
// [horiz][vert][k]
// where horiz/vert are offset by 2 (i.e., range is -2 .. 2)
// and k = 0 for the context
// = 1 for the xor bit
static const unsigned int signContext[5][5][2] = {
{{ 13, 1 }, // horiz=-2, vert=-2
{ 13, 1 }, // horiz=-2, vert=-1
{ 12, 1 }, // horiz=-2, vert= 0
{ 11, 1 }, // horiz=-2, vert=+1
{ 11, 1 }}, // horiz=-2, vert=+2
{{ 13, 1 }, // horiz=-1, vert=-2
{ 13, 1 }, // horiz=-1, vert=-1
{ 12, 1 }, // horiz=-1, vert= 0
{ 11, 1 }, // horiz=-1, vert=+1
{ 11, 1 }}, // horiz=-1, vert=+2
{{ 10, 1 }, // horiz= 0, vert=-2
{ 10, 1 }, // horiz= 0, vert=-1
{ 9, 0 }, // horiz= 0, vert= 0
{ 10, 0 }, // horiz= 0, vert=+1
{ 10, 0 }}, // horiz= 0, vert=+2
{{ 11, 0 }, // horiz=+1, vert=-2
{ 11, 0 }, // horiz=+1, vert=-1
{ 12, 0 }, // horiz=+1, vert= 0
{ 13, 0 }, // horiz=+1, vert=+1
{ 13, 0 }}, // horiz=+1, vert=+2
{{ 11, 0 }, // horiz=+2, vert=-2
{ 11, 0 }, // horiz=+2, vert=-1
{ 12, 0 }, // horiz=+2, vert= 0
{ 13, 0 }, // horiz=+2, vert=+1
{ 13, 0 }}, // horiz=+2, vert=+2
};
//------------------------------------------------------------------------
// constants used in the IDWT
#define idwtAlpha -1.586134342059924
#define idwtBeta -0.052980118572961
#define idwtGamma 0.882911075530934
#define idwtDelta 0.443506852043971
#define idwtKappa 1.230174104914001
#define idwtIKappa (1.0 / idwtKappa)
// number of bits to the right of the decimal point for the fixed
// point arithmetic used in the IDWT
#define fracBits 16
//------------------------------------------------------------------------
// floor(x / y)
#define jpxFloorDiv(x, y) ((x) / (y))
// floor(x / 2^y)
#define jpxFloorDivPow2(x, y) ((x) >> (y))
// ceil(x / y)
#define jpxCeilDiv(x, y) (((x) + (y) - 1) / (y))
// ceil(x / 2^y)
#define jpxCeilDivPow2(x, y) (((x) + (1 << (y)) - 1) >> (y))
//------------------------------------------------------------------------
#if 1 //----- disable coverage tracking
#define cover(idx)
#else //----- enable coverage tracking
class JPXCover {
public:
JPXCover(int sizeA);
~JPXCover();
void incr(int idx);
private:
int size, used;
int *data;
};
JPXCover::JPXCover(int sizeA) {
size = sizeA;
used = -1;
data = (int *)gmallocn(size, sizeof(int));
memset(data, 0, size * sizeof(int));
}
JPXCover::~JPXCover() {
int i;
printf("JPX coverage:\n");
for (i = 0; i <= used; ++i) {
printf(" %4d: %8d\n", i, data[i]);
}
gfree(data);
}
void JPXCover::incr(int idx) {
if (idx < size) {
++data[idx];
if (idx > used) {
used = idx;
}
}
}
JPXCover jpxCover(150);
#define cover(idx) jpxCover.incr(idx)
#endif //----- coverage tracking
//------------------------------------------------------------------------
JPXStream::JPXStream(Stream *strA):
FilterStream(strA)
{
bufStr = new BufStream(str, 2);
nComps = 0;
bpc = nullptr;
width = height = 0;
haveCS = false;
havePalette = false;
haveCompMap = false;
haveChannelDefn = false;
img.tiles = nullptr;
bitBuf = 0;
bitBufLen = 0;
bitBufSkip = false;
byteCount = 0;
curX = curY = 0;
curComp = 0;
readBufLen = 0;
}
JPXStream::~JPXStream() {
close();
delete bufStr;
}
void JPXStream::reset() {
bufStr->reset();
if (readBoxes()) {
curY = img.yOffset;
} else {
// readBoxes reported an error, so we go immediately to EOF
curY = img.ySize;
}
curX = img.xOffset;
curComp = 0;
readBufLen = 0;
}
void JPXStream::close() {
JPXTile *tile;
JPXTileComp *tileComp;
JPXResLevel *resLevel;
JPXPrecinct *precinct;
JPXSubband *subband;
JPXCodeBlock *cb;
unsigned int comp, i, k, r, pre, sb;
gfree(bpc);
bpc = nullptr;
if (havePalette) {
gfree(palette.bpc);
gfree(palette.c);
havePalette = false;
}
if (haveCompMap) {
gfree(compMap.comp);
gfree(compMap.type);
gfree(compMap.pComp);
haveCompMap = false;
}
if (haveChannelDefn) {
gfree(channelDefn.idx);
gfree(channelDefn.type);
gfree(channelDefn.assoc);
haveChannelDefn = false;
}
if (img.tiles) {
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
tile = &img.tiles[i];
if (tile->tileComps) {
for (comp = 0; comp < img.nComps; ++comp) {
tileComp = &tile->tileComps[comp];
gfree(tileComp->quantSteps);
gfree(tileComp->data);
gfree(tileComp->buf);
if (tileComp->resLevels) {
for (r = 0; r <= tileComp->nDecompLevels; ++r) {
resLevel = &tileComp->resLevels[r];
if (resLevel->precincts) {
for (pre = 0; pre < 1; ++pre) {
precinct = &resLevel->precincts[pre];
if (precinct->subbands) {
for (sb = 0; sb < (unsigned int)(r == 0 ? 1 : 3); ++sb) {
subband = &precinct->subbands[sb];
gfree(subband->inclusion);
gfree(subband->zeroBitPlane);
if (subband->cbs) {
for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) {
cb = &subband->cbs[k];
gfree(cb->dataLen);
gfree(cb->touched);
if (cb->arithDecoder) {
delete cb->arithDecoder;
}
if (cb->stats) {
delete cb->stats;
}
}
gfree(subband->cbs);
}
}
gfree(precinct->subbands);
}
}
gfree(img.tiles[i].tileComps[comp].resLevels[r].precincts);
}
}
gfree(img.tiles[i].tileComps[comp].resLevels);
}
}
gfree(img.tiles[i].tileComps);
}
}
gfree(img.tiles);
img.tiles = nullptr;
}
bufStr->close();
}
int JPXStream::getChar() {
int c;
if (readBufLen < 8) {
fillReadBuf();
}
if (readBufLen == 8) {
c = readBuf & 0xff;
readBufLen = 0;
} else if (readBufLen > 8) {
c = (readBuf >> (readBufLen - 8)) & 0xff;
readBufLen -= 8;
} else if (readBufLen == 0) {
c = EOF;
} else {
c = (readBuf << (8 - readBufLen)) & 0xff;
readBufLen = 0;
}
return c;
}
int JPXStream::lookChar() {
int c;
if (readBufLen < 8) {
fillReadBuf();
}
if (readBufLen == 8) {
c = readBuf & 0xff;
} else if (readBufLen > 8) {
c = (readBuf >> (readBufLen - 8)) & 0xff;
} else if (readBufLen == 0) {
c = EOF;
} else {
c = (readBuf << (8 - readBufLen)) & 0xff;
}
return c;
}
void JPXStream::fillReadBuf() {
JPXTileComp *tileComp;
unsigned int tileIdx, tx, ty;
int pix, pixBits;
do {
if (curY >= img.ySize) {
return;
}
tileIdx = ((curY - img.yTileOffset) / img.yTileSize) * img.nXTiles
+ (curX - img.xTileOffset) / img.xTileSize;
#if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
if (img.tiles == nullptr || tileIdx >= img.nXTiles * img.nYTiles || img.tiles[tileIdx].tileComps == nullptr) {
error(errSyntaxError, getPos(), "Unexpected tileIdx in fillReadBuf in JPX stream");
return;
}
tileComp = &img.tiles[tileIdx].tileComps[curComp];
#else
tileComp = &img.tiles[tileIdx].tileComps[havePalette ? 0 : curComp];
#endif
tx = jpxCeilDiv((curX - img.xTileOffset) % img.xTileSize, tileComp->hSep);
ty = jpxCeilDiv((curY - img.yTileOffset) % img.yTileSize, tileComp->vSep);
if (unlikely(ty >= (tileComp->y1 - tileComp->y0))) {
error(errSyntaxError, getPos(), "Unexpected ty in fillReadBuf in JPX stream");
return;
}
if (unlikely(tx >= (tileComp->x1 - tileComp->x0))) {
error(errSyntaxError, getPos(), "Unexpected tx in fillReadBuf in JPX stream");
return;
}
pix = (int)tileComp->data[ty * (tileComp->x1 - tileComp->x0) + tx];
pixBits = tileComp->prec;
#if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid
if (++curComp == img.nComps) {
#else
if (havePalette) {
if (pix >= 0 && pix < palette.nEntries) {
pix = palette.c[pix * palette.nComps + curComp];
} else {
pix = 0;
}
pixBits = palette.bpc[curComp];
}
if (++curComp == (unsigned int)(havePalette ? palette.nComps : img.nComps)) {
#endif
curComp = 0;
if (++curX == img.xSize) {
curX = img.xOffset;
++curY;
if (pixBits < 8) {
pix <<= 8 - pixBits;
pixBits = 8;
}
}
}
if (pixBits == 8) {
readBuf = (readBuf << 8) | (pix & 0xff);
} else {
readBuf = (readBuf << pixBits) | (pix & ((1 << pixBits) - 1));
}
readBufLen += pixBits;
} while (readBufLen < 8);
}
GooString *JPXStream::getPSFilter(int psLevel, const char *indent) {
return nullptr;
}
bool JPXStream::isBinary(bool last) {
return str->isBinary(true);
}
void JPXStream::getImageParams(int *bitsPerComponent,
StreamColorSpaceMode *csMode) {
unsigned int boxType, boxLen, dataLen, csEnum;
unsigned int bpc1, dummy, i;
int csMeth, csPrec, csPrec1, dummy2;
StreamColorSpaceMode csMode1;
bool haveBPC, haveCSMode;
csPrec = 0; // make gcc happy
haveBPC = haveCSMode = false;
bufStr->reset();
if (bufStr->lookChar() == 0xff) {
getImageParams2(bitsPerComponent, csMode);
} else {
while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
if (boxType == 0x6a703268) { // JP2 header
cover(0);
// skip the superbox
} else if (boxType == 0x69686472) { // image header
cover(1);
if (readULong(&dummy) &&
readULong(&dummy) &&
readUWord(&dummy) &&
readUByte(&bpc1) &&
readUByte(&dummy) &&
readUByte(&dummy) &&
readUByte(&dummy)) {
*bitsPerComponent = bpc1 + 1;
haveBPC = true;
}
} else if (boxType == 0x636F6C72) { // color specification
cover(2);
if (readByte(&csMeth) &&
readByte(&csPrec1) &&
readByte(&dummy2)) {
if (csMeth == 1) {
if (readULong(&csEnum)) {
csMode1 = streamCSNone;
if (csEnum == jpxCSBiLevel ||
csEnum == jpxCSGrayscale) {
csMode1 = streamCSDeviceGray;
} else if (csEnum == jpxCSCMYK) {
csMode1 = streamCSDeviceCMYK;
} else if (csEnum == jpxCSsRGB ||
csEnum == jpxCSCISesRGB ||
csEnum == jpxCSROMMRGB) {
csMode1 = streamCSDeviceRGB;
}
if (csMode1 != streamCSNone &&
(!haveCSMode || csPrec1 > csPrec)) {
*csMode = csMode1;
csPrec = csPrec1;
haveCSMode = true;
}
if( dataLen >= 7 ) {
for (i = 0; i < dataLen - 7; ++i) {
if (bufStr->getChar() == EOF)
break;
}
}
}
} else {
if( dataLen >= 3 ) {
for (i = 0; i < dataLen - 3; ++i) {
if (bufStr->getChar() == EOF)
break;
}
}
}
}
} else if (boxType == 0x6A703263) { // codestream
cover(3);
if (!(haveBPC && haveCSMode)) {
getImageParams2(bitsPerComponent, csMode);
}
break;
} else {
cover(4);
for (i = 0; i < dataLen; ++i) {
if (unlikely(bufStr->getChar() == EOF)) {
error(errSyntaxError, getPos(), "Unexpected EOF in getImageParams in JPX stream");
break;
}
}
}
}
}
bufStr->close();
}
// Get image parameters from the codestream.
void JPXStream::getImageParams2(int *bitsPerComponent,
StreamColorSpaceMode *csMode) {
int segType;
unsigned int segLen, nComps1, bpc1, dummy, i;
while (readMarkerHdr(&segType, &segLen)) {
if (segType == 0x51) { // SIZ - image and tile size
cover(5);
if (readUWord(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readULong(&dummy) &&
readUWord(&nComps1) &&
readUByte(&bpc1)) {
*bitsPerComponent = (bpc1 & 0x7f) + 1;
// if there's no color space info, take a guess
if (nComps1 == 1) {
*csMode = streamCSDeviceGray;
} else if (nComps1 == 3) {
*csMode = streamCSDeviceRGB;
} else if (nComps1 == 4) {
*csMode = streamCSDeviceCMYK;
}
}
break;
} else {
cover(6);
if (segLen > 2) {
for (i = 0; i < segLen - 2; ++i) {
bufStr->getChar();
}
}
}
}
}
bool JPXStream::readBoxes() {
unsigned int boxType, boxLen, dataLen;
unsigned int bpc1, compression, unknownColorspace, ipr;
unsigned int i, j;
haveImgHdr = false;
// initialize in case there is a parse error
img.xSize = img.ySize = 0;
img.xOffset = img.yOffset = 0;
img.xTileSize = img.yTileSize = 0;
img.xTileOffset = img.yTileOffset = 0;
img.nComps = 0;
// check for a naked JPEG 2000 codestream (without the JP2/JPX
// wrapper) -- this appears to be a violation of the PDF spec, but
// Acrobat allows it
if (bufStr->lookChar() == 0xff) {
cover(7);
error(errSyntaxWarning, getPos(),
"Naked JPEG 2000 codestream, missing JP2/JPX wrapper");
if (!readCodestream(0)) {
return false;
}
nComps = img.nComps;
bpc = (unsigned int *)gmallocn(nComps, sizeof(unsigned int));
for (i = 0; i < nComps; ++i) {
bpc[i] = img.tiles[0].tileComps[i].prec;
}
width = img.xSize - img.xOffset;
height = img.ySize - img.yOffset;
return true;
}
while (readBoxHdr(&boxType, &boxLen, &dataLen)) {
switch (boxType) {
case 0x6a703268: // JP2 header
// this is a grouping box ('superbox') which has no real
// contents and doesn't appear to be used consistently, i.e.,
// some things which should be subboxes of the JP2 header box
// show up outside of it - so we simply ignore the JP2 header
// box
cover(8);
break;
case 0x69686472: // image header
cover(9);
if (!readULong(&height) ||
!readULong(&width) ||
!readUWord(&nComps) ||
!readUByte(&bpc1) ||
!readUByte(&compression) ||
!readUByte(&unknownColorspace) ||
!readUByte(&ipr)) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
if (compression != 7) {
error(errSyntaxError, getPos(),
"Unknown compression type in JPX stream");
return false;
}
bpc = (unsigned int *)gmallocn(nComps, sizeof(unsigned int));
for (i = 0; i < nComps; ++i) {
bpc[i] = bpc1;
}
haveImgHdr = true;
break;
case 0x62706363: // bits per component
cover(10);
if (!haveImgHdr) {
error(errSyntaxError, getPos(),
"Found bits per component box before image header box in JPX stream");
return false;
}
if (dataLen != nComps) {
error(errSyntaxError, getPos(),
"Invalid bits per component box in JPX stream");
return false;
}
for (i = 0; i < nComps; ++i) {
if (!readUByte(&bpc[i])) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
}
break;
case 0x636F6C72: // color specification
cover(11);
if (!readColorSpecBox(dataLen)) {
return false;
}
break;
case 0x70636c72: // palette
cover(12);
if (!readUWord(&palette.nEntries) ||
!readUByte(&palette.nComps)) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
palette.bpc = (unsigned int *)gmallocn(palette.nComps, sizeof(unsigned int));
palette.c =
(int *)gmallocn(palette.nEntries * palette.nComps, sizeof(int));
for (i = 0; i < palette.nComps; ++i) {
if (!readUByte(&palette.bpc[i])) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
++palette.bpc[i];
}
for (i = 0; i < palette.nEntries; ++i) {
for (j = 0; j < palette.nComps; ++j) {
if (!readNBytes(((palette.bpc[j] & 0x7f) + 7) >> 3,
(palette.bpc[j] & 0x80) ? true : false,
&palette.c[i * palette.nComps + j])) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
}
}
havePalette = true;
break;
case 0x636d6170: // component mapping
cover(13);
compMap.nChannels = dataLen / 4;
compMap.comp = (unsigned int *)gmallocn(compMap.nChannels, sizeof(unsigned int));
compMap.type = (unsigned int *)gmallocn(compMap.nChannels, sizeof(unsigned int));
compMap.pComp = (unsigned int *)gmallocn(compMap.nChannels, sizeof(unsigned int));
for (i = 0; i < compMap.nChannels; ++i) {
if (!readUWord(&compMap.comp[i]) ||
!readUByte(&compMap.type[i]) ||
!readUByte(&compMap.pComp[i])) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
}
haveCompMap = true;
break;
case 0x63646566: // channel definition
cover(14);
if (!readUWord(&channelDefn.nChannels)) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
channelDefn.idx =
(unsigned int *)gmallocn(channelDefn.nChannels, sizeof(unsigned int));
channelDefn.type =
(unsigned int *)gmallocn(channelDefn.nChannels, sizeof(unsigned int));
channelDefn.assoc =
(unsigned int *)gmallocn(channelDefn.nChannels, sizeof(unsigned int));
for (i = 0; i < channelDefn.nChannels; ++i) {
if (!readUWord(&channelDefn.idx[i]) ||
!readUWord(&channelDefn.type[i]) ||
!readUWord(&channelDefn.assoc[i])) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
}
haveChannelDefn = true;
break;
case 0x6A703263: // contiguous codestream
cover(15);
if (!bpc) {
error(errSyntaxError, getPos(),
"JPX stream is missing the image header box");
}
if (!haveCS) {
error(errSyntaxError, getPos(),
"JPX stream has no supported color spec");
}
if (!readCodestream(dataLen)) {
return false;
}
break;
default:
cover(16);
for (i = 0; i < dataLen; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream");
return false;
}
}
break;
}
}
return true;
}
bool JPXStream::readColorSpecBox(unsigned int dataLen) {
JPXColorSpec newCS;
unsigned int csApprox, csEnum;
unsigned int i;
bool ok;
ok = false;
if (!readUByte(&newCS.meth) ||
!readByte(&newCS.prec) ||
!readUByte(&csApprox)) {
goto err;
}
switch (newCS.meth) {
case 1: // enumerated colorspace
cover(17);
if (!readULong(&csEnum)) {
goto err;
}
newCS.enumerated.type = (JPXColorSpaceType)csEnum;
switch (newCS.enumerated.type) {
case jpxCSBiLevel:
ok = true;
break;
case jpxCSYCbCr1:
ok = true;
break;
case jpxCSYCbCr2:
ok = true;
break;
case jpxCSYCBCr3:
ok = true;
break;
case jpxCSPhotoYCC:
ok = true;
break;
case jpxCSCMY:
ok = true;
break;
case jpxCSCMYK:
ok = true;
break;
case jpxCSYCCK:
ok = true;
break;
case jpxCSCIELab:
if (dataLen == 7 + 7*4) {
if (!readULong(&newCS.enumerated.cieLab.rl) ||
!readULong(&newCS.enumerated.cieLab.ol) ||
!readULong(&newCS.enumerated.cieLab.ra) ||
!readULong(&newCS.enumerated.cieLab.oa) ||
!readULong(&newCS.enumerated.cieLab.rb) ||
!readULong(&newCS.enumerated.cieLab.ob) ||
!readULong(&newCS.enumerated.cieLab.il)) {
goto err;
}
} else if (dataLen == 7) {
//~ this assumes the 8-bit case
cover(92);
newCS.enumerated.cieLab.rl = 100;
newCS.enumerated.cieLab.ol = 0;
newCS.enumerated.cieLab.ra = 255;
newCS.enumerated.cieLab.oa = 128;
newCS.enumerated.cieLab.rb = 255;
newCS.enumerated.cieLab.ob = 96;
newCS.enumerated.cieLab.il = 0x00443530;
} else {
goto err;
}
ok = true;
break;
case jpxCSsRGB:
ok = true;
break;
case jpxCSGrayscale:
ok = true;
break;
case jpxCSBiLevel2:
ok = true;
break;
case jpxCSCIEJab:
// not allowed in PDF
goto err;
case jpxCSCISesRGB:
ok = true;
break;
case jpxCSROMMRGB:
ok = true;
break;
case jpxCSsRGBYCbCr:
ok = true;
break;
case jpxCSYPbPr1125:
ok = true;
break;
case jpxCSYPbPr1250:
ok = true;
break;
default:
goto err;
}
break;
case 2: // restricted ICC profile
case 3: // any ICC profile (JPX)
case 4: // vendor color (JPX)
cover(18);
for (i = 0; i < dataLen - 3; ++i) {
if (bufStr->getChar() == EOF) {
goto err;
}
}
break;
}
if (ok && (!haveCS || newCS.prec > cs.prec)) {
cs = newCS;
haveCS = true;
}
return true;
err:
error(errSyntaxError, getPos(), "Error in JPX color spec");
return false;
}
bool JPXStream::readCodestream(unsigned int len) {
JPXTile *tile;
JPXTileComp *tileComp;
int segType;
bool haveSIZ, haveCOD, haveQCD, haveSOT;
unsigned int precinctSize, style, nDecompLevels;
unsigned int segLen, capabilities, comp, i, j, r;
//----- main header
haveSIZ = haveCOD = haveQCD = haveSOT = false;
do {
if (!readMarkerHdr(&segType, &segLen)) {
error(errSyntaxError, getPos(), "Error in JPX codestream");
return false;
}
switch (segType) {
case 0x4f: // SOC - start of codestream
// marker only
cover(19);
break;
case 0x51: // SIZ - image and tile size
cover(20);
if (haveSIZ) {
error(errSyntaxError, getPos(),
"Duplicate SIZ marker segment in JPX stream");
return false;
}
if (!readUWord(&capabilities) ||
!readULong(&img.xSize) ||
!readULong(&img.ySize) ||
!readULong(&img.xOffset) ||
!readULong(&img.yOffset) ||
!readULong(&img.xTileSize) ||
!readULong(&img.yTileSize) ||
!readULong(&img.xTileOffset) ||
!readULong(&img.yTileOffset) ||
!readUWord(&img.nComps)) {
error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
return false;
}
if (haveImgHdr && img.nComps != nComps) {
error(errSyntaxError, getPos(),
"Different number of components in JPX SIZ marker segment");
return false;
}
if (img.xSize == 0 || img.ySize == 0 ||
img.xOffset >= img.xSize || img.yOffset >= img.ySize ||
img.xTileSize == 0 || img.yTileSize == 0 ||
img.xTileOffset > img.xOffset ||
img.yTileOffset > img.yOffset ||
img.xTileSize + img.xTileOffset <= img.xOffset ||
img.yTileSize + img.yTileOffset <= img.yOffset) {
error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
return false;
}
img.nXTiles = (img.xSize - img.xTileOffset + img.xTileSize - 1)
/ img.xTileSize;
img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1)
/ img.yTileSize;
// check for overflow before allocating memory
if (img.nXTiles <= 0 || img.nYTiles <= 0 ||
img.nXTiles >= 65535 / img.nYTiles) {
error(errSyntaxError, getPos(),
"Bad tile count in JPX SIZ marker segment");
return false;
}
img.tiles = (JPXTile *)gmallocn(img.nXTiles * img.nYTiles,
sizeof(JPXTile));
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
img.tiles[i].init = false;
img.tiles[i].tileComps = (JPXTileComp *)gmallocn(img.nComps,
sizeof(JPXTileComp));
for (comp = 0; comp < img.nComps; ++comp) {
img.tiles[i].tileComps[comp].quantSteps = nullptr;
img.tiles[i].tileComps[comp].data = nullptr;
img.tiles[i].tileComps[comp].buf = nullptr;
img.tiles[i].tileComps[comp].resLevels = nullptr;
}
}
for (comp = 0; comp < img.nComps; ++comp) {
if (!readUByte(&img.tiles[0].tileComps[comp].prec) ||
!readUByte(&img.tiles[0].tileComps[comp].hSep) ||
!readUByte(&img.tiles[0].tileComps[comp].vSep)) {
error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
return false;
}
if (img.tiles[0].tileComps[comp].hSep == 0 ||
img.tiles[0].tileComps[comp].vSep == 0) {
error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment");
return false;
}
img.tiles[0].tileComps[comp].sgned =
(img.tiles[0].tileComps[comp].prec & 0x80) ? true : false;
img.tiles[0].tileComps[comp].prec =
(img.tiles[0].tileComps[comp].prec & 0x7f) + 1;
for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
img.tiles[i].tileComps[comp] = img.tiles[0].tileComps[comp];
}
}
haveSIZ = true;
break;
case 0x52: // COD - coding style default
cover(21);
if (!haveSIZ) {
error(errSyntaxError, getPos(),
"JPX COD marker segment before SIZ segment");
return false;
}
if (img.tiles == nullptr || img.nXTiles * img.nYTiles == 0 || img.tiles[0].tileComps == nullptr) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
if (!readUByte(&img.tiles[0].tileComps[0].style) ||
!readUByte(&img.tiles[0].progOrder) ||
!readUWord(&img.tiles[0].nLayers) ||
!readUByte(&img.tiles[0].multiComp) ||
!readUByte(&nDecompLevels) ||
!readUByte(&img.tiles[0].tileComps[0].codeBlockW) ||
!readUByte(&img.tiles[0].tileComps[0].codeBlockH) ||
!readUByte(&img.tiles[0].tileComps[0].codeBlockStyle) ||
!readUByte(&img.tiles[0].tileComps[0].transform)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
if (nDecompLevels > 32 ||
img.tiles[0].tileComps[0].codeBlockW > 8 ||
img.tiles[0].tileComps[0].codeBlockH > 8) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[0].tileComps[0].nDecompLevels = nDecompLevels;
img.tiles[0].tileComps[0].codeBlockW += 2;
img.tiles[0].tileComps[0].codeBlockH += 2;
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
if (i != 0) {
img.tiles[i].progOrder = img.tiles[0].progOrder;
img.tiles[i].nLayers = img.tiles[0].nLayers;
img.tiles[i].multiComp = img.tiles[0].multiComp;
}
for (comp = 0; comp < img.nComps; ++comp) {
if (!(i == 0 && comp == 0)) {
img.tiles[i].tileComps[comp].style =
img.tiles[0].tileComps[0].style;
img.tiles[i].tileComps[comp].nDecompLevels =
img.tiles[0].tileComps[0].nDecompLevels;
img.tiles[i].tileComps[comp].codeBlockW =
img.tiles[0].tileComps[0].codeBlockW;
img.tiles[i].tileComps[comp].codeBlockH =
img.tiles[0].tileComps[0].codeBlockH;
img.tiles[i].tileComps[comp].codeBlockStyle =
img.tiles[0].tileComps[0].codeBlockStyle;
img.tiles[i].tileComps[comp].transform =
img.tiles[0].tileComps[0].transform;
}
img.tiles[i].tileComps[comp].resLevels =
(JPXResLevel *)gmallocn_checkoverflow(
(img.tiles[i].tileComps[comp].nDecompLevels + 1),
sizeof(JPXResLevel));
if (img.tiles[i].tileComps[comp].resLevels == nullptr) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
img.tiles[i].tileComps[comp].resLevels[r].precincts = nullptr;
}
}
}
for (r = 0; r <= img.tiles[0].tileComps[0].nDecompLevels; ++r) {
if (img.tiles[0].tileComps[0].style & 0x01) {
cover(91);
if (!readUByte(&precinctSize)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[0].tileComps[0].resLevels[r].precinctWidth =
precinctSize & 0x0f;
img.tiles[0].tileComps[0].resLevels[r].precinctHeight =
(precinctSize >> 4) & 0x0f;
} else {
img.tiles[0].tileComps[0].resLevels[r].precinctWidth = 15;
img.tiles[0].tileComps[0].resLevels[r].precinctHeight = 15;
}
}
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
for (comp = 0; comp < img.nComps; ++comp) {
if (!(i == 0 && comp == 0)) {
for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
img.tiles[0].tileComps[0].resLevels[r].precinctWidth;
img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
img.tiles[0].tileComps[0].resLevels[r].precinctHeight;
}
}
}
}
haveCOD = true;
break;
case 0x53: // COC - coding style component
cover(22);
if (!haveCOD) {
error(errSyntaxError, getPos(),
"JPX COC marker segment before COD segment");
return false;
}
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&style) ||
!readUByte(&nDecompLevels) ||
!readUByte(&img.tiles[0].tileComps[comp].codeBlockW) ||
!readUByte(&img.tiles[0].tileComps[comp].codeBlockH) ||
!readUByte(&img.tiles[0].tileComps[comp].codeBlockStyle) ||
!readUByte(&img.tiles[0].tileComps[comp].transform)) {
error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
return false;
}
if (nDecompLevels > 32 ||
img.tiles[0].tileComps[comp].codeBlockW > 8 ||
img.tiles[0].tileComps[comp].codeBlockH > 8) {
error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
return false;
}
img.tiles[0].tileComps[comp].nDecompLevels = nDecompLevels;
img.tiles[0].tileComps[comp].style =
(img.tiles[0].tileComps[comp].style & ~1) | (style & 1);
img.tiles[0].tileComps[comp].codeBlockW += 2;
img.tiles[0].tileComps[comp].codeBlockH += 2;
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
if (i != 0) {
img.tiles[i].tileComps[comp].style =
img.tiles[0].tileComps[comp].style;
img.tiles[i].tileComps[comp].nDecompLevels =
img.tiles[0].tileComps[comp].nDecompLevels;
img.tiles[i].tileComps[comp].codeBlockW =
img.tiles[0].tileComps[comp].codeBlockW;
img.tiles[i].tileComps[comp].codeBlockH =
img.tiles[0].tileComps[comp].codeBlockH;
img.tiles[i].tileComps[comp].codeBlockStyle =
img.tiles[0].tileComps[comp].codeBlockStyle;
img.tiles[i].tileComps[comp].transform =
img.tiles[0].tileComps[comp].transform;
}
img.tiles[i].tileComps[comp].resLevels =
(JPXResLevel *)greallocn(
img.tiles[i].tileComps[comp].resLevels,
(img.tiles[i].tileComps[comp].nDecompLevels + 1),
sizeof(JPXResLevel));
for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
img.tiles[i].tileComps[comp].resLevels[r].precincts = nullptr;
}
}
for (r = 0; r <= img.tiles[0].tileComps[comp].nDecompLevels; ++r) {
if (img.tiles[0].tileComps[comp].style & 0x01) {
if (!readUByte(&precinctSize)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[0].tileComps[comp].resLevels[r].precinctWidth =
precinctSize & 0x0f;
img.tiles[0].tileComps[comp].resLevels[r].precinctHeight =
(precinctSize >> 4) & 0x0f;
} else {
img.tiles[0].tileComps[comp].resLevels[r].precinctWidth = 15;
img.tiles[0].tileComps[comp].resLevels[r].precinctHeight = 15;
}
}
for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) {
img.tiles[i].tileComps[comp].resLevels[r].precinctWidth =
img.tiles[0].tileComps[comp].resLevels[r].precinctWidth;
img.tiles[i].tileComps[comp].resLevels[r].precinctHeight =
img.tiles[0].tileComps[comp].resLevels[r].precinctHeight;
}
}
break;
case 0x5c: // QCD - quantization default
cover(23);
if (!haveSIZ) {
error(errSyntaxError, getPos(),
"JPX QCD marker segment before SIZ segment");
return false;
}
if (!readUByte(&img.tiles[0].tileComps[0].quantStyle)) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x00) {
if (segLen <= 3) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
img.tiles[0].tileComps[0].nQuantSteps = segLen - 3;
img.tiles[0].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[0].quantSteps,
img.tiles[0].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
if (!readUByte(&img.tiles[0].tileComps[0].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x01) {
img.tiles[0].tileComps[0].nQuantSteps = 1;
img.tiles[0].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[0].quantSteps,
img.tiles[0].tileComps[0].nQuantSteps,
sizeof(unsigned int));
if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[0])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
} else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x02) {
if (segLen < 5) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
img.tiles[0].tileComps[0].nQuantSteps = (segLen - 3) / 2;
img.tiles[0].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[0].quantSteps,
img.tiles[0].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) {
if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
for (comp = 0; comp < img.nComps; ++comp) {
if (!(i == 0 && comp == 0)) {
img.tiles[i].tileComps[comp].quantStyle =
img.tiles[0].tileComps[0].quantStyle;
img.tiles[i].tileComps[comp].nQuantSteps =
img.tiles[0].tileComps[0].nQuantSteps;
img.tiles[i].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
img.tiles[0].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (j = 0; j < img.tiles[0].tileComps[0].nQuantSteps; ++j) {
img.tiles[i].tileComps[comp].quantSteps[j] =
img.tiles[0].tileComps[0].quantSteps[j];
}
}
}
}
haveQCD = true;
break;
case 0x5d: // QCC - quantization component
cover(24);
if (!haveQCD) {
error(errSyntaxError, getPos(),
"JPX QCC marker segment before QCD segment");
return false;
}
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&img.tiles[0].tileComps[comp].quantStyle)) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x00) {
if (segLen <= (img.nComps > 256 ? 5U : 4U)) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
img.tiles[0].tileComps[comp].nQuantSteps =
segLen - (img.nComps > 256 ? 5 : 4);
img.tiles[0].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
img.tiles[0].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
if (!readUByte(&img.tiles[0].tileComps[comp].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
}
} else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x01) {
img.tiles[0].tileComps[comp].nQuantSteps = 1;
img.tiles[0].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
img.tiles[0].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[0])) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
} else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x02) {
if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
img.tiles[0].tileComps[comp].nQuantSteps =
(segLen - (img.nComps > 256 ? 5 : 4)) / 2;
img.tiles[0].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[0].tileComps[comp].quantSteps,
img.tiles[0].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) {
if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
for (i = 1; i < img.nXTiles * img.nYTiles; ++i) {
img.tiles[i].tileComps[comp].quantStyle =
img.tiles[0].tileComps[comp].quantStyle;
img.tiles[i].tileComps[comp].nQuantSteps =
img.tiles[0].tileComps[comp].nQuantSteps;
img.tiles[i].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[i].tileComps[comp].quantSteps,
img.tiles[0].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
for (j = 0; j < img.tiles[0].tileComps[comp].nQuantSteps; ++j) {
img.tiles[i].tileComps[comp].quantSteps[j] =
img.tiles[0].tileComps[comp].quantSteps[j];
}
}
break;
case 0x5e: // RGN - region of interest
cover(25);
#if 1 //~ ROI is unimplemented
error(errUnimplemented, -1, "got a JPX RGN segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
return false;
}
}
#else
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&compInfo[comp].defROI.style) ||
!readUByte(&compInfo[comp].defROI.shift)) {
error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
return false;
}
#endif
break;
case 0x5f: // POC - progression order change
cover(26);
#if 1 //~ progression order changes are unimplemented
error(errUnimplemented, -1, "got a JPX POC segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
return false;
}
}
#else
nProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
progs = (JPXProgOrder *)gmallocn(nProgs, sizeof(JPXProgOrder));
for (i = 0; i < nProgs; ++i) {
if (!readUByte(&progs[i].startRes) ||
!(img.nComps > 256 && readUWord(&progs[i].startComp)) ||
!(img.nComps <= 256 && readUByte(&progs[i].startComp)) ||
!readUWord(&progs[i].endLayer) ||
!readUByte(&progs[i].endRes) ||
!(img.nComps > 256 && readUWord(&progs[i].endComp)) ||
!(img.nComps <= 256 && readUByte(&progs[i].endComp)) ||
!readUByte(&progs[i].progOrder)) {
error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
return false;
}
}
#endif
break;
case 0x60: // PPM - packed packet headers, main header
cover(27);
#if 1 //~ packed packet headers are unimplemented
error(errUnimplemented, -1, "Got a JPX PPM segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX PPM marker segment");
return false;
}
}
#endif
break;
case 0x55: // TLM - tile-part lengths
// skipped
cover(28);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX TLM marker segment");
return false;
}
}
break;
case 0x57: // PLM - packet length, main header
// skipped
cover(29);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX PLM marker segment");
return false;
}
}
break;
case 0x63: // CRG - component registration
// skipped
cover(30);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX CRG marker segment");
return false;
}
}
break;
case 0x64: // COM - comment
// skipped
cover(31);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX COM marker segment");
return false;
}
}
break;
case 0x90: // SOT - start of tile
cover(32);
haveSOT = true;
break;
default:
cover(33);
error(errSyntaxError, getPos(),
"Unknown marker segment {0:02x} in JPX stream", segType);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
break;
}
}
break;
}
} while (!haveSOT);
if (!haveSIZ) {
error(errSyntaxError, getPos(),
"Missing SIZ marker segment in JPX stream");
return false;
}
if (!haveCOD) {
error(errSyntaxError, getPos(),
"Missing COD marker segment in JPX stream");
return false;
}
if (!haveQCD) {
error(errSyntaxError, getPos(),
"Missing QCD marker segment in JPX stream");
return false;
}
//----- read the tile-parts
while (1) {
if (!readTilePart()) {
return false;
}
if (!readMarkerHdr(&segType, &segLen)) {
error(errSyntaxError, getPos(), "Error in JPX codestream");
return false;
}
if (segType != 0x90) { // SOT - start of tile
break;
}
}
if (segType != 0xd9) { // EOC - end of codestream
error(errSyntaxError, getPos(), "Missing EOC marker in JPX codestream");
return false;
}
//----- finish decoding the image
for (i = 0; i < img.nXTiles * img.nYTiles; ++i) {
tile = &img.tiles[i];
if (!tile->init) {
error(errSyntaxError, getPos(), "Uninitialized tile in JPX codestream");
return false;
}
for (comp = 0; comp < img.nComps; ++comp) {
tileComp = &tile->tileComps[comp];
inverseTransform(tileComp);
}
if (!inverseMultiCompAndDC(tile)) {
return false;
}
}
//~ can free memory below tileComps here, and also tileComp.buf
return true;
}
bool JPXStream::readTilePart() {
JPXTile *tile;
JPXTileComp *tileComp;
JPXResLevel *resLevel;
JPXPrecinct *precinct;
JPXSubband *subband;
JPXCodeBlock *cb;
int *sbCoeffs;
bool haveSOD;
unsigned int tileIdx, tilePartLen, tilePartIdx, nTileParts;
bool tilePartToEOC;
unsigned int precinctSize, style, nDecompLevels;
unsigned int n, nSBs, nx, ny, sbx0, sby0, comp, segLen;
unsigned int i, j, k, cbX, cbY, r, pre, sb, cbi, cbj;
int segType, level;
// process the SOT marker segment
if (!readUWord(&tileIdx) ||
!readULong(&tilePartLen) ||
!readUByte(&tilePartIdx) ||
!readUByte(&nTileParts)) {
error(errSyntaxError, getPos(), "Error in JPX SOT marker segment");
return false;
}
if (tileIdx >= img.nXTiles * img.nYTiles ||
(tilePartIdx > 0 && !img.tiles[tileIdx].init)) {
error(errSyntaxError, getPos(), "Weird tile index in JPX stream");
return false;
}
tilePartToEOC = tilePartLen == 0;
tilePartLen -= 12; // subtract size of SOT segment
haveSOD = false;
do {
if (!readMarkerHdr(&segType, &segLen)) {
error(errSyntaxError, getPos(), "Error in JPX tile-part codestream");
return false;
}
tilePartLen -= 2 + segLen;
switch (segType) {
case 0x52: // COD - coding style default
cover(34);
if (!readUByte(&img.tiles[tileIdx].tileComps[0].style) ||
!readUByte(&img.tiles[tileIdx].progOrder) ||
!readUWord(&img.tiles[tileIdx].nLayers) ||
!readUByte(&img.tiles[tileIdx].multiComp) ||
!readUByte(&nDecompLevels) ||
!readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockW) ||
!readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockH) ||
!readUByte(&img.tiles[tileIdx].tileComps[0].codeBlockStyle) ||
!readUByte(&img.tiles[tileIdx].tileComps[0].transform)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
if (nDecompLevels > 32 ||
img.tiles[tileIdx].tileComps[0].codeBlockW > 8 ||
img.tiles[tileIdx].tileComps[0].codeBlockH > 8) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[0].nDecompLevels = nDecompLevels;
img.tiles[tileIdx].tileComps[0].codeBlockW += 2;
img.tiles[tileIdx].tileComps[0].codeBlockH += 2;
for (comp = 0; comp < img.nComps; ++comp) {
if (comp != 0) {
img.tiles[tileIdx].tileComps[comp].style =
img.tiles[tileIdx].tileComps[0].style;
img.tiles[tileIdx].tileComps[comp].nDecompLevels =
img.tiles[tileIdx].tileComps[0].nDecompLevels;
img.tiles[tileIdx].tileComps[comp].codeBlockW =
img.tiles[tileIdx].tileComps[0].codeBlockW;
img.tiles[tileIdx].tileComps[comp].codeBlockH =
img.tiles[tileIdx].tileComps[0].codeBlockH;
img.tiles[tileIdx].tileComps[comp].codeBlockStyle =
img.tiles[tileIdx].tileComps[0].codeBlockStyle;
img.tiles[tileIdx].tileComps[comp].transform =
img.tiles[tileIdx].tileComps[0].transform;
}
img.tiles[tileIdx].tileComps[comp].resLevels =
(JPXResLevel *)greallocn(
img.tiles[tileIdx].tileComps[comp].resLevels,
(img.tiles[tileIdx].tileComps[comp].nDecompLevels + 1),
sizeof(JPXResLevel));
for (r = 0;
r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels;
++r) {
img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = nullptr;
}
}
for (r = 0; r <= img.tiles[tileIdx].tileComps[0].nDecompLevels; ++r) {
if (img.tiles[tileIdx].tileComps[0].style & 0x01) {
if (!readUByte(&precinctSize)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth =
precinctSize & 0x0f;
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight =
(precinctSize >> 4) & 0x0f;
} else {
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth = 15;
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight = 15;
}
}
for (comp = 1; comp < img.nComps; ++comp) {
for (r = 0;
r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels;
++r) {
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth;
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight;
}
}
break;
case 0x53: // COC - coding style component
cover(35);
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&style) ||
!readUByte(&nDecompLevels) ||
!readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockW) ||
!readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockH) ||
!readUByte(&img.tiles[tileIdx].tileComps[comp].codeBlockStyle) ||
!readUByte(&img.tiles[tileIdx].tileComps[comp].transform)) {
error(errSyntaxError, getPos(), "Error in JPX COC marker segment");
return false;
}
if (nDecompLevels > 32 ||
img.tiles[tileIdx].tileComps[comp].codeBlockW > 8 ||
img.tiles[tileIdx].tileComps[comp].codeBlockH > 8) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[comp].nDecompLevels = nDecompLevels;
img.tiles[tileIdx].tileComps[comp].style =
(img.tiles[tileIdx].tileComps[comp].style & ~1) | (style & 1);
img.tiles[tileIdx].tileComps[comp].codeBlockW += 2;
img.tiles[tileIdx].tileComps[comp].codeBlockH += 2;
img.tiles[tileIdx].tileComps[comp].resLevels =
(JPXResLevel *)greallocn(
img.tiles[tileIdx].tileComps[comp].resLevels,
(img.tiles[tileIdx].tileComps[comp].nDecompLevels + 1),
sizeof(JPXResLevel));
for (r = 0; r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels; ++r) {
img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = nullptr;
}
for (r = 0; r <= img.tiles[tileIdx].tileComps[comp].nDecompLevels; ++r) {
if (img.tiles[tileIdx].tileComps[comp].style & 0x01) {
if (!readUByte(&precinctSize)) {
error(errSyntaxError, getPos(), "Error in JPX COD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth =
precinctSize & 0x0f;
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight =
(precinctSize >> 4) & 0x0f;
} else {
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = 15;
img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = 15;
}
}
break;
case 0x5c: // QCD - quantization default
cover(36);
if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantStyle)) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x00) {
if (segLen <= 3) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[0].nQuantSteps = segLen - 3;
img.tiles[tileIdx].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
img.tiles[tileIdx].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x01) {
img.tiles[tileIdx].tileComps[0].nQuantSteps = 1;
img.tiles[tileIdx].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
img.tiles[tileIdx].tileComps[0].nQuantSteps,
sizeof(unsigned int));
if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[0])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
} else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x02) {
if (segLen < 5) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
img.tiles[tileIdx].tileComps[0].nQuantSteps = (segLen - 3) / 2;
img.tiles[tileIdx].tileComps[0].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps,
img.tiles[tileIdx].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) {
if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
for (comp = 1; comp < img.nComps; ++comp) {
img.tiles[tileIdx].tileComps[comp].quantStyle =
img.tiles[tileIdx].tileComps[0].quantStyle;
img.tiles[tileIdx].tileComps[comp].nQuantSteps =
img.tiles[tileIdx].tileComps[0].nQuantSteps;
img.tiles[tileIdx].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
img.tiles[tileIdx].tileComps[0].nQuantSteps,
sizeof(unsigned int));
for (j = 0; j < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++j) {
img.tiles[tileIdx].tileComps[comp].quantSteps[j] =
img.tiles[tileIdx].tileComps[0].quantSteps[j];
}
}
break;
case 0x5d: // QCC - quantization component
cover(37);
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&img.tiles[tileIdx].tileComps[comp].quantStyle)) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) == 0x00) {
if (segLen <= (img.nComps > 256 ? 5U : 4U)) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
img.tiles[tileIdx].tileComps[comp].nQuantSteps =
segLen - (img.nComps > 256 ? 5 : 4);
img.tiles[tileIdx].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
img.tiles[tileIdx].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
if (!readUByte(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
}
} else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
== 0x01) {
img.tiles[tileIdx].tileComps[comp].nQuantSteps = 1;
img.tiles[tileIdx].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
img.tiles[tileIdx].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[0])) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
} else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f)
== 0x02) {
if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
img.tiles[tileIdx].tileComps[comp].nQuantSteps =
(segLen - (img.nComps > 256 ? 5 : 4)) / 2;
img.tiles[tileIdx].tileComps[comp].quantSteps =
(unsigned int *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps,
img.tiles[tileIdx].tileComps[comp].nQuantSteps,
sizeof(unsigned int));
for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) {
if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) {
error(errSyntaxError, getPos(), "Error in JPX QCD marker segment");
return false;
}
}
} else {
error(errSyntaxError, getPos(), "Error in JPX QCC marker segment");
return false;
}
break;
case 0x5e: // RGN - region of interest
cover(38);
#if 1 //~ ROI is unimplemented
error(errUnimplemented, -1, "Got a JPX RGN segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
return false;
}
}
#else
if ((img.nComps > 256 && !readUWord(&comp)) ||
(img.nComps <= 256 && !readUByte(&comp)) ||
comp >= img.nComps ||
!readUByte(&compInfo[comp].roi.style) ||
!readUByte(&compInfo[comp].roi.shift)) {
error(errSyntaxError, getPos(), "Error in JPX RGN marker segment");
return false;
}
#endif
break;
case 0x5f: // POC - progression order change
cover(39);
#if 1 //~ progression order changes are unimplemented
error(errUnimplemented, -1, "Got a JPX POC segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
return false;
}
}
#else
nTileProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7);
tileProgs = (JPXProgOrder *)gmallocn(nTileProgs, sizeof(JPXProgOrder));
for (i = 0; i < nTileProgs; ++i) {
if (!readUByte(&tileProgs[i].startRes) ||
!(img.nComps > 256 && readUWord(&tileProgs[i].startComp)) ||
!(img.nComps <= 256 && readUByte(&tileProgs[i].startComp)) ||
!readUWord(&tileProgs[i].endLayer) ||
!readUByte(&tileProgs[i].endRes) ||
!(img.nComps > 256 && readUWord(&tileProgs[i].endComp)) ||
!(img.nComps <= 256 && readUByte(&tileProgs[i].endComp)) ||
!readUByte(&tileProgs[i].progOrder)) {
error(errSyntaxError, getPos(), "Error in JPX POC marker segment");
return false;
}
}
#endif
break;
case 0x61: // PPT - packed packet headers, tile-part hdr
cover(40);
#if 1 //~ packed packet headers are unimplemented
error(errUnimplemented, -1, "Got a JPX PPT segment");
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX PPT marker segment");
return false;
}
}
#endif
case 0x58: // PLT - packet length, tile-part header
// skipped
cover(41);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX PLT marker segment");
return false;
}
}
break;
case 0x64: // COM - comment
// skipped
cover(42);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
error(errSyntaxError, getPos(), "Error in JPX COM marker segment");
return false;
}
}
break;
case 0x93: // SOD - start of data
cover(43);
haveSOD = true;
break;
default:
cover(44);
error(errSyntaxError, getPos(),
"Unknown marker segment {0:02x} in JPX tile-part stream",
segType);
for (i = 0; i < segLen - 2; ++i) {
if (bufStr->getChar() == EOF) {
break;
}
}
break;
}
} while (!haveSOD);
//----- initialize the tile, precincts, and code-blocks
if (tilePartIdx == 0) {
tile = &img.tiles[tileIdx];
tile->init = true;
i = tileIdx / img.nXTiles;
j = tileIdx % img.nXTiles;
if ((tile->x0 = img.xTileOffset + j * img.xTileSize) < img.xOffset) {
tile->x0 = img.xOffset;
}
if ((tile->y0 = img.yTileOffset + i * img.yTileSize) < img.yOffset) {
tile->y0 = img.yOffset;
}
if ((tile->x1 = img.xTileOffset + (j + 1) * img.xTileSize) > img.xSize) {
tile->x1 = img.xSize;
}
if ((tile->y1 = img.yTileOffset + (i + 1) * img.yTileSize) > img.ySize) {
tile->y1 = img.ySize;
}
tile->comp = 0;
tile->res = 0;
tile->precinct = 0;
tile->layer = 0;
tile->maxNDecompLevels = 0;
for (comp = 0; comp < img.nComps; ++comp) {
tileComp = &tile->tileComps[comp];
if (tileComp->nDecompLevels > tile->maxNDecompLevels) {
tile->maxNDecompLevels = tileComp->nDecompLevels;
}
tileComp->x0 = jpxCeilDiv(tile->x0, tileComp->hSep);
tileComp->y0 = jpxCeilDiv(tile->y0, tileComp->vSep);
tileComp->x1 = jpxCeilDiv(tile->x1, tileComp->hSep);
tileComp->y1 = jpxCeilDiv(tile->y1, tileComp->vSep);
tileComp->w = tileComp->x1 - tileComp->x0;
tileComp->cbW = 1 << tileComp->codeBlockW;
tileComp->cbH = 1 << tileComp->codeBlockH;
tileComp->data = (int *)gmallocn((tileComp->x1 - tileComp->x0) *
(tileComp->y1 - tileComp->y0),
sizeof(int));
if (tileComp->x1 - tileComp->x0 > tileComp->y1 - tileComp->y0) {
n = tileComp->x1 - tileComp->x0;
} else {
n = tileComp->y1 - tileComp->y0;
}
tileComp->buf = (int *)gmallocn(n + 8, sizeof(int));
for (r = 0; r <= tileComp->nDecompLevels; ++r) {
resLevel = &tileComp->resLevels[r];
k = r == 0 ? tileComp->nDecompLevels
: tileComp->nDecompLevels - r + 1;
resLevel->x0 = jpxCeilDivPow2(tileComp->x0, k);
resLevel->y0 = jpxCeilDivPow2(tileComp->y0, k);
resLevel->x1 = jpxCeilDivPow2(tileComp->x1, k);
resLevel->y1 = jpxCeilDivPow2(tileComp->y1, k);
if (r == 0) {
resLevel->bx0[0] = resLevel->x0;
resLevel->by0[0] = resLevel->y0;
resLevel->bx1[0] = resLevel->x1;
resLevel->by1[0] = resLevel->y1;
} else {
resLevel->bx0[0] = jpxCeilDivPow2(tileComp->x0 - (1 << (k-1)), k);
resLevel->by0[0] = resLevel->y0;
resLevel->bx1[0] = jpxCeilDivPow2(tileComp->x1 - (1 << (k-1)), k);
resLevel->by1[0] = resLevel->y1;
resLevel->bx0[1] = resLevel->x0;
resLevel->by0[1] = jpxCeilDivPow2(tileComp->y0 - (1 << (k-1)), k);
resLevel->bx1[1] = resLevel->x1;
resLevel->by1[1] = jpxCeilDivPow2(tileComp->y1 - (1 << (k-1)), k);
resLevel->bx0[2] = jpxCeilDivPow2(tileComp->x0 - (1 << (k-1)), k);
resLevel->by0[2] = jpxCeilDivPow2(tileComp->y0 - (1 << (k-1)), k);
resLevel->bx1[2] = jpxCeilDivPow2(tileComp->x1 - (1 << (k-1)), k);
resLevel->by1[2] = jpxCeilDivPow2(tileComp->y1 - (1 << (k-1)), k);
}
resLevel->precincts = (JPXPrecinct *)gmallocn(1, sizeof(JPXPrecinct));
for (pre = 0; pre < 1; ++pre) {
precinct = &resLevel->precincts[pre];
precinct->x0 = resLevel->x0;
precinct->y0 = resLevel->y0;
precinct->x1 = resLevel->x1;
precinct->y1 = resLevel->y1;
nSBs = r == 0 ? 1 : 3;
precinct->subbands =
(JPXSubband *)gmallocn(nSBs, sizeof(JPXSubband));
for (sb = 0; sb < nSBs; ++sb) {
subband = &precinct->subbands[sb];
subband->x0 = resLevel->bx0[sb];
subband->y0 = resLevel->by0[sb];
subband->x1 = resLevel->bx1[sb];
subband->y1 = resLevel->by1[sb];
subband->nXCBs = jpxCeilDivPow2(subband->x1,
tileComp->codeBlockW)
- jpxFloorDivPow2(subband->x0,
tileComp->codeBlockW);
subband->nYCBs = jpxCeilDivPow2(subband->y1,
tileComp->codeBlockH)
- jpxFloorDivPow2(subband->y0,
tileComp->codeBlockH);
n = subband->nXCBs > subband->nYCBs ? subband->nXCBs
: subband->nYCBs;
for (subband->maxTTLevel = 0, --n;
n;
++subband->maxTTLevel, n >>= 1) ;
n = 0;
for (level = subband->maxTTLevel; level >= 0; --level) {
nx = jpxCeilDivPow2(subband->nXCBs, level);
ny = jpxCeilDivPow2(subband->nYCBs, level);
n += nx * ny;
}
subband->inclusion =
(JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
subband->zeroBitPlane =
(JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode));
for (k = 0; k < n; ++k) {
subband->inclusion[k].finished = false;
subband->inclusion[k].val = 0;
subband->zeroBitPlane[k].finished = false;
subband->zeroBitPlane[k].val = 0;
}
subband->cbs = (JPXCodeBlock *)gmallocn(subband->nXCBs *
subband->nYCBs,
sizeof(JPXCodeBlock));
sbx0 = jpxFloorDivPow2(subband->x0, tileComp->codeBlockW);
sby0 = jpxFloorDivPow2(subband->y0, tileComp->codeBlockH);
if (r == 0) { // (NL)LL
sbCoeffs = tileComp->data;
} else if (sb == 0) { // (NL-r+1)HL
sbCoeffs = tileComp->data
+ resLevel->bx1[1] - resLevel->bx0[1];
} else if (sb == 1) { // (NL-r+1)LH
sbCoeffs = tileComp->data
+ (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w;
} else { // (NL-r+1)HH
sbCoeffs = tileComp->data
+ (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w
+ resLevel->bx1[1] - resLevel->bx0[1];
}
cb = subband->cbs;
for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
cb->x0 = (sbx0 + cbX) << tileComp->codeBlockW;
cb->x1 = cb->x0 + tileComp->cbW;
if (subband->x0 > cb->x0) {
cb->x0 = subband->x0;
}
if (subband->x1 < cb->x1) {
cb->x1 = subband->x1;
}
cb->y0 = (sby0 + cbY) << tileComp->codeBlockH;
cb->y1 = cb->y0 + tileComp->cbH;
if (subband->y0 > cb->y0) {
cb->y0 = subband->y0;
}
if (subband->y1 < cb->y1) {
cb->y1 = subband->y1;
}
cb->seen = false;
cb->lBlock = 3;
cb->nextPass = jpxPassCleanup;
cb->nZeroBitPlanes = 0;
cb->dataLenSize = 1;
cb->dataLen = (unsigned int *)gmalloc(sizeof(unsigned int));
cb->coeffs = sbCoeffs
+ (cb->y0 - subband->y0) * tileComp->w
+ (cb->x0 - subband->x0);
cb->touched = (char *)gmalloc(1 << (tileComp->codeBlockW
+ tileComp->codeBlockH));
cb->len = 0;
for (cbj = 0; cbj < cb->y1 - cb->y0; ++cbj) {
for (cbi = 0; cbi < cb->x1 - cb->x0; ++cbi) {
cb->coeffs[cbj * tileComp->w + cbi] = 0;
}
}
memset(cb->touched, 0,
(1 << (tileComp->codeBlockW + tileComp->codeBlockH)));
cb->arithDecoder = nullptr;
cb->stats = nullptr;
++cb;
}
}
}
}
}
}
}
return readTilePartData(tileIdx, tilePartLen, tilePartToEOC);
}
bool JPXStream::readTilePartData(unsigned int tileIdx,
unsigned int tilePartLen, bool tilePartToEOC) {
JPXTile *tile;
JPXTileComp *tileComp;
JPXResLevel *resLevel;
JPXPrecinct *precinct;
JPXSubband *subband;
JPXCodeBlock *cb;
unsigned int ttVal;
unsigned int bits, cbX, cbY, nx, ny, i, j, n, sb;
int level;
tile = &img.tiles[tileIdx];
// read all packets from this tile-part
while (1) {
if (tilePartToEOC) {
//~ peek for an EOC marker
cover(93);
} else if (tilePartLen == 0) {
break;
}
tileComp = &tile->tileComps[tile->comp];
resLevel = &tileComp->resLevels[tile->res];
precinct = &resLevel->precincts[tile->precinct];
//----- packet header
// setup
startBitBuf(tilePartLen);
if (tileComp->style & 0x02) {
skipSOP();
}
// zero-length flag
if (!readBits(1, &bits)) {
goto err;
}
if (!bits) {
// packet is empty -- clear all code-block inclusion flags
cover(45);
for (sb = 0; sb < (unsigned int)(tile->res == 0 ? 1 : 3); ++sb) {
subband = &precinct->subbands[sb];
for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
cb = &subband->cbs[cbY * subband->nXCBs + cbX];
cb->included = false;
}
}
}
} else {
for (sb = 0; sb < (unsigned int)(tile->res == 0 ? 1 : 3); ++sb) {
subband = &precinct->subbands[sb];
for (cbY = 0; cbY < subband->nYCBs; ++cbY) {
for (cbX = 0; cbX < subband->nXCBs; ++cbX) {
cb = &subband->cbs[cbY * subband->nXCBs + cbX];
// skip code-blocks with no coefficients
if (cb->x0 >= cb->x1 || cb->y0 >= cb->y1) {
cover(46);
cb->included = false;
continue;
}
// code-block inclusion
if (cb->seen) {
cover(47);
if (!readBits(1, &cb->included)) {
goto err;
}
} else {
cover(48);
ttVal = 0;
i = 0;
for (level = subband->maxTTLevel; level >= 0; --level) {
nx = jpxCeilDivPow2(subband->nXCBs, level);
ny = jpxCeilDivPow2(subband->nYCBs, level);
j = i + (cbY >> level) * nx + (cbX >> level);
if (!subband->inclusion[j].finished &&
!subband->inclusion[j].val) {
subband->inclusion[j].val = ttVal;
} else {
ttVal = subband->inclusion[j].val;
}
while (!subband->inclusion[j].finished &&
ttVal <= tile->layer) {
if (!readBits(1, &bits)) {
goto err;
}
if (bits == 1) {
subband->inclusion[j].finished = true;
} else {
++ttVal;
}
}
subband->inclusion[j].val = ttVal;
if (ttVal > tile->layer) {
break;
}
i += nx * ny;
}
cb->included = level < 0;
}
if (cb->included) {
cover(49);
// zero bit-plane count