Fixing inclusion tree when layers are skipped and 0xFF at the packet header end

This commit is contained in:
notmasteryet 2012-01-14 13:15:16 -06:00
parent ac592a2fdd
commit e43afd3fe5

View File

@ -81,42 +81,44 @@ var JpxImage = (function JpxImageClosure() {
})(); })();
var InclusionTree = (function InclusionTreeClosure() { var InclusionTree = (function InclusionTreeClosure() {
function InclusionTree(width, height) { function InclusionTree(width, height, defaultValue) {
var levelsLength = log2(Math.max(width, height)) + 1; var levelsLength = log2(Math.max(width, height)) + 1;
this.levels = []; this.levels = [];
for (var i = 0; i < levelsLength; i++) { for (var i = 0; i < levelsLength; i++) {
var items = new Uint8Array(width * height);
for (var j = 0, jj = items.length; j < jj; j++)
items[j] = defaultValue;
var level = { var level = {
width: width, width: width,
height: height, height: height,
items: new Uint8Array(width * height) items: items
}; };
this.levels.push(level); this.levels.push(level);
width = Math.ceil(width / 2); width = Math.ceil(width / 2);
height = Math.ceil(height / 2); height = Math.ceil(height / 2);
} }
this.value = 0;
} }
InclusionTree.prototype = { InclusionTree.prototype = {
incrementValue: function inclusionTreeIncrementValue() { reset: function inclusionTreeReset(i, j, stopValue) {
this.value++; var currentLevel = 0;
},
reset: function inclusionTreeReset(i, j) {
var currentLevel = 0, currentValue = this.value;
while (currentLevel < this.levels.length) { while (currentLevel < this.levels.length) {
var level = this.levels[currentLevel]; var level = this.levels[currentLevel];
var index = i + j * level.width; var index = i + j * level.width;
level.index = index;
var value = level.items[index]; var value = level.items[index];
if (value == currentValue + 1) { if (value == 0xFF)
this.currentLevel = currentLevel - 1;
// already know about this one, skiping values
this.skipValue();
return false;
}
if (value != currentValue)
break; break;
level.index = index; if (value > stopValue) {
this.currentLevel = currentLevel;
// already know about this one, propagating the value to top levels
this.propagateValues();
return false;
}
i >>= 1; i >>= 1;
j >>= 1; j >>= 1;
currentLevel++; currentLevel++;
@ -124,20 +126,33 @@ var JpxImage = (function JpxImageClosure() {
this.currentLevel = currentLevel - 1; this.currentLevel = currentLevel - 1;
return true; return true;
}, },
skipValue: function inclusionTreeSetValue() { incrementValue: function inclusionTreeIncrementValue(stopValue) {
var level = this.levels[this.currentLevel];
level.items[level.index] = stopValue + 1;
this.propagateValues();
},
propagateValues: function inclusionTreePropagateValues() {
var levelIndex = this.currentLevel; var levelIndex = this.currentLevel;
var currentValue = this.value + 1; var level = this.levels[levelIndex];
while (levelIndex >= 0) { var currentValue = level.items[level.index];
while (--levelIndex >= 0) {
var level = this.levels[levelIndex]; var level = this.levels[levelIndex];
level.items[level.index] = currentValue; level.items[level.index] = currentValue;
levelIndex--;
} }
}, },
nextLevel: function inclusionTreeNextLevel() { nextLevel: function inclusionTreeNextLevel() {
var level = this.levels[this.currentLevel]; var currentLevel = this.currentLevel;
level.items[level.index] |= 0x80; var level = this.levels[currentLevel];
this.currentLevel--; var value = level.items[level.index];
return this.currentLevel >= 0; level.items[level.index] = 0xFF;
currentLevel--;
if (currentLevel < 0)
return false;
this.currentLevel = currentLevel;
var level = this.levels[currentLevel];
level.items[level.index] = value;
return true;
} }
}; };
return InclusionTree; return InclusionTree;
@ -979,15 +994,7 @@ var JpxImage = (function JpxImageClosure() {
var codeblock = codeblocks[i]; var codeblock = codeblocks[i];
var precinctNumber = codeblock.precinctNumber; var precinctNumber = codeblock.precinctNumber;
} }
// building inclusion and zero bit-planes trees
subband.precincts = precincts; subband.precincts = precincts;
for (var i in precincts) {
var precinct = precincts[i];
var width = precinct.cbxMax - precinct.cbxMin + 1;
var height = precinct.cbyMax - precinct.cbyMin + 1;
precinct.inclusionTree = new InclusionTree(width, height);
precinct.zeroBitPlanesTree = new TagTree(width, height);
}
} }
function createPacket(resolution, precinctNumber, layerNumber) { function createPacket(resolution, precinctNumber, layerNumber) {
var precinctCodeblocks = []; var precinctCodeblocks = [];
@ -1207,7 +1214,10 @@ var JpxImage = (function JpxImageClosure() {
} }
function alignToByte() { function alignToByte() {
bufferSize = 0; bufferSize = 0;
skipNextBit = false; if (skipNextBit) {
position++;
skipNextBit = false;
}
} }
function readCodingpasses() { function readCodingpasses() {
var value = readBits(1); var value = readBits(1);
@ -1248,10 +1258,20 @@ var JpxImage = (function JpxImageClosure() {
} else { } else {
// reading inclusion tree // reading inclusion tree
var precinct = codeblock.precinct; var precinct = codeblock.precinct;
var inclusionTree = precinct.inclusionTree; var inclusionTree, zeroBitPlanesTree;
while (inclusionTree.value < layerNumber) if ('inclusionTree' in precinct) {
inclusionTree.incrementValue(); inclusionTree = precinct.inclusionTree;
if (inclusionTree.reset(codeblockColumn, codeblockRow)) { } else {
// building inclusion and zero bit-planes trees
var width = precinct.cbxMax - precinct.cbxMin + 1;
var height = precinct.cbyMax - precinct.cbyMin + 1;
inclusionTree = new InclusionTree(width, height, layerNumber);
zeroBitPlanesTree = new TagTree(width, height);
precinct.inclusionTree = inclusionTree;
precinct.zeroBitPlanesTree = zeroBitPlanesTree;
}
if (inclusionTree.reset(codeblockColumn, codeblockRow, layerNumber)) {
while (true) { while (true) {
if (readBits(1)) { if (readBits(1)) {
var valueReady = !inclusionTree.nextLevel(); var valueReady = !inclusionTree.nextLevel();
@ -1261,7 +1281,7 @@ var JpxImage = (function JpxImageClosure() {
break; break;
} }
} else { } else {
inclusionTree.skipValue(); inclusionTree.incrementValue(layerNumber);
break; break;
} }
} }
@ -1270,7 +1290,7 @@ var JpxImage = (function JpxImageClosure() {
if (!codeblockIncluded) if (!codeblockIncluded)
continue; continue;
if (firstTimeInclusion) { if (firstTimeInclusion) {
var zeroBitPlanesTree = precinct.zeroBitPlanesTree; zeroBitPlanesTree = precinct.zeroBitPlanesTree;
zeroBitPlanesTree.reset(codeblockColumn, codeblockRow); zeroBitPlanesTree.reset(codeblockColumn, codeblockRow);
while (true) { while (true) {
if (readBits(1)) { if (readBits(1)) {