Convert src/core/jpx.js to use standard classes

*Please note:* Ignoring whitespace-only changes is probably necessary in order to review this.
This commit is contained in:
Jonas Jenwald 2021-05-05 13:02:58 +02:00
parent cb65b762eb
commit ce14171cf0

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { BaseException, info, warn } from "../shared/util.js";
import { BaseException, info, unreachable, warn } from "../shared/util.js";
import { log2, readUint16, readUint32 } from "./core_utils.js";
import { ArithmeticDecoder } from "./arithmetic_decoder.js";
@ -23,7 +23,6 @@ class JpxError extends BaseException {
}
}
const JpxImage = (function JpxImageClosure() {
// Table E.1
const SubbandsGainLog2 = {
LL: 0,
@ -32,12 +31,12 @@ const JpxImage = (function JpxImageClosure() {
HH: 2,
};
// eslint-disable-next-line no-shadow
function JpxImage() {
class JpxImage {
constructor() {
this.failOnCorruptedImage = false;
}
JpxImage.prototype = {
parse: function JpxImage_parse(data) {
parse(data) {
const head = readUint16(data, 0);
// No box header, immediate start of codestream (SOC)
if (head === 0xff4f) {
@ -114,15 +113,16 @@ const JpxImage = (function JpxImageClosure() {
(tbox >> 8) & 0xff,
tbox & 0xff
);
warn("Unsupported header type " + tbox + " (" + headerType + ")");
warn(`Unsupported header type ${tbox} (${headerType}).`);
break;
}
if (jumpDataLength) {
position += dataLength;
}
}
},
parseImageProperties: function JpxImage_parseImageProperties(stream) {
}
parseImageProperties(stream) {
let newByte = stream.getByte();
while (newByte >= 0) {
const oldByte = newByte;
@ -146,8 +146,9 @@ const JpxImage = (function JpxImageClosure() {
}
}
throw new JpxError("No size marker found in JPX stream");
},
parseCodestream: function JpxImage_parseCodestream(data, start, end) {
}
parseCodestream(data, start, end) {
const context = {};
let doNotRecover = false;
try {
@ -350,9 +351,7 @@ const JpxImage = (function JpxImageClosure() {
}
if (unsupported.length > 0) {
doNotRecover = true;
warn(
`JPX: Unsupported COD options (${unsupported.join(", ")}).`
);
warn(`JPX: Unsupported COD options (${unsupported.join(", ")}).`);
}
if (context.mainHeader) {
context.COD = cod;
@ -417,8 +416,9 @@ const JpxImage = (function JpxImageClosure() {
this.width = context.SIZ.Xsiz - context.SIZ.XOsiz;
this.height = context.SIZ.Ysiz - context.SIZ.YOsiz;
this.componentsCount = context.SIZ.Csiz;
},
};
}
}
function calculateComponentDimensions(component, siz) {
// Section B.2 Component mapping
component.x0 = Math.ceil(siz.XOsiz / component.XRsiz);
@ -507,8 +507,7 @@ const JpxImage = (function JpxImageClosure() {
// coordinates of a point in the LL band and child subband, respectively.
const isZeroRes = resolution.resLevel === 0;
const precinctWidthInSubband = 1 << (dimensions.PPx + (isZeroRes ? 0 : -1));
const precinctHeightInSubband =
1 << (dimensions.PPy + (isZeroRes ? 0 : -1));
const precinctHeightInSubband = 1 << (dimensions.PPy + (isZeroRes ? 0 : -1));
const numprecinctswide =
resolution.trx1 > resolution.trx0
? Math.ceil(resolution.trx1 / precinctWidth) -
@ -1584,9 +1583,8 @@ const JpxImage = (function JpxImageClosure() {
}
// Section B.10.2 Tag trees
const TagTree = (function TagTreeClosure() {
// eslint-disable-next-line no-shadow
function TagTree(width, height) {
class TagTree {
constructor(width, height) {
const levelsLength = log2(Math.max(width, height)) + 1;
this.levels = [];
for (let i = 0; i < levelsLength; i++) {
@ -1600,8 +1598,8 @@ const JpxImage = (function JpxImageClosure() {
height = Math.ceil(height / 2);
}
}
TagTree.prototype = {
reset: function TagTree_reset(i, j) {
reset(i, j) {
let currentLevel = 0,
value = 0,
level;
@ -1622,12 +1620,14 @@ const JpxImage = (function JpxImageClosure() {
level.items[level.index] = value;
this.currentLevel = currentLevel;
delete this.value;
},
incrementValue: function TagTree_incrementValue() {
}
incrementValue() {
const level = this.levels[this.currentLevel];
level.items[level.index]++;
},
nextLevel: function TagTree_nextLevel() {
}
nextLevel() {
let currentLevel = this.currentLevel;
let level = this.levels[currentLevel];
const value = level.items[level.index];
@ -1641,14 +1641,11 @@ const JpxImage = (function JpxImageClosure() {
level = this.levels[currentLevel];
level.items[level.index] = value;
return true;
},
};
return TagTree;
})();
}
}
const InclusionTree = (function InclusionTreeClosure() {
// eslint-disable-next-line no-shadow
function InclusionTree(width, height, defaultValue) {
class InclusionTree {
constructor(width, height, defaultValue) {
const levelsLength = log2(Math.max(width, height)) + 1;
this.levels = [];
for (let i = 0; i < levelsLength; i++) {
@ -1668,8 +1665,8 @@ const JpxImage = (function JpxImageClosure() {
height = Math.ceil(height / 2);
}
}
InclusionTree.prototype = {
reset: function InclusionTree_reset(i, j, stopValue) {
reset(i, j, stopValue) {
let currentLevel = 0;
while (currentLevel < this.levels.length) {
const level = this.levels[currentLevel];
@ -1694,13 +1691,15 @@ const JpxImage = (function JpxImageClosure() {
}
this.currentLevel = currentLevel - 1;
return true;
},
incrementValue: function InclusionTree_incrementValue(stopValue) {
}
incrementValue(stopValue) {
const level = this.levels[this.currentLevel];
level.items[level.index] = stopValue + 1;
this.propagateValues();
},
propagateValues: function InclusionTree_propagateValues() {
}
propagateValues() {
let levelIndex = this.currentLevel;
let level = this.levels[levelIndex];
const currentValue = level.items[level.index];
@ -1708,8 +1707,9 @@ const JpxImage = (function JpxImageClosure() {
level = this.levels[levelIndex];
level.items[level.index] = currentValue;
}
},
nextLevel: function InclusionTree_nextLevel() {
}
nextLevel() {
let currentLevel = this.currentLevel;
let level = this.levels[currentLevel];
const value = level.items[level.index];
@ -1723,10 +1723,8 @@ const JpxImage = (function JpxImageClosure() {
level = this.levels[currentLevel];
level.items[level.index] = value;
return true;
},
};
return InclusionTree;
})();
}
}
// Section D. Coefficient bit modeling
const BitModel = (function BitModelClosure() {
@ -1755,7 +1753,8 @@ const JpxImage = (function JpxImageClosure() {
]);
// eslint-disable-next-line no-shadow
function BitModel(width, height, subband, zeroBitPlanes, mb) {
class BitModel {
constructor(width, height, subband, zeroBitPlanes, mb) {
this.width = width;
this.height = height;
@ -1797,11 +1796,11 @@ const JpxImage = (function JpxImageClosure() {
this.reset();
}
BitModel.prototype = {
setDecoder: function BitModel_setDecoder(decoder) {
setDecoder(decoder) {
this.decoder = decoder;
},
reset: function BitModel_reset() {
}
reset() {
// We have 17 contexts that are accessed via context labels,
// plus the uniform and runlength context.
this.contexts = new Int8Array(19);
@ -1811,12 +1810,9 @@ const JpxImage = (function JpxImageClosure() {
this.contexts[0] = (4 << 1) | 0;
this.contexts[UNIFORM_CONTEXT] = (46 << 1) | 0;
this.contexts[RUNLENGTH_CONTEXT] = (3 << 1) | 0;
},
setNeighborsSignificance: function BitModel_setNeighborsSignificance(
row,
column,
index
) {
}
setNeighborsSignificance(row, column, index) {
const neighborsSignificance = this.neighborsSignificance;
const width = this.width,
height = this.height;
@ -1853,8 +1849,9 @@ const JpxImage = (function JpxImageClosure() {
neighborsSignificance[index + 1] += 0x01;
}
neighborsSignificance[index] |= 0x80;
},
runSignificancePropagationPass: function BitModel_runSignificancePropagationPass() {
}
runSignificancePropagationPass() {
const decoder = this.decoder;
const width = this.width,
height = this.height;
@ -1880,10 +1877,7 @@ const JpxImage = (function JpxImageClosure() {
// clear processed flag first
processingFlags[index] &= processedInverseMask;
if (
coefficentsMagnitude[index] ||
!neighborsSignificance[index]
) {
if (coefficentsMagnitude[index] || !neighborsSignificance[index]) {
continue;
}
@ -1901,8 +1895,9 @@ const JpxImage = (function JpxImageClosure() {
}
}
}
},
decodeSignBit: function BitModel_decodeSignBit(row, column, index) {
}
decodeSignBit(row, column, index) {
const width = this.width,
height = this.height;
const coefficentsMagnitude = this.coefficentsMagnitude;
@ -1953,8 +1948,9 @@ const JpxImage = (function JpxImageClosure() {
decoded = this.decoder.readBit(this.contexts, contextLabel) ^ 1;
}
return decoded;
},
runMagnitudeRefinementPass: function BitModel_runMagnitudeRefinementPass() {
}
runMagnitudeRefinementPass() {
const decoder = this.decoder;
const width = this.width,
height = this.height;
@ -1996,8 +1992,9 @@ const JpxImage = (function JpxImageClosure() {
}
}
}
},
runCleanupPass: function BitModel_runCleanupPass() {
}
runCleanupPass() {
const decoder = this.decoder;
const width = this.width,
height = this.height;
@ -2090,8 +2087,9 @@ const JpxImage = (function JpxImageClosure() {
}
}
}
},
checkSegmentationSymbol: function BitModel_checkSegmentationSymbol() {
}
checkSegmentationSymbol() {
const decoder = this.decoder;
const contexts = this.contexts;
const symbol =
@ -2102,29 +2100,29 @@ const JpxImage = (function JpxImageClosure() {
if (symbol !== 0xa) {
throw new JpxError("Invalid segmentation symbol");
}
},
};
}
}
return BitModel;
})();
// Section F, Discrete wavelet transformation
const Transform = (function TransformClosure() {
// eslint-disable-next-line no-shadow
function Transform() {}
class Transform {
constructor() {
if (this.constructor === Transform) {
unreachable("Cannot initialize Transform.");
}
}
Transform.prototype.calculate = function transformCalculate(
subbands,
u0,
v0
) {
calculate(subbands, u0, v0) {
let ll = subbands[0];
for (let i = 1, ii = subbands.length; i < ii; i++) {
ll = this.iterate(ll, subbands[i], u0, v0);
}
return ll;
};
Transform.prototype.extend = function extend(buffer, offset, size) {
}
extend(buffer, offset, size) {
// Section F.3.7 extending... using max extension of 4
let i1 = offset - 1,
j1 = offset + 1;
@ -2138,13 +2136,13 @@ const JpxImage = (function JpxImageClosure() {
buffer[j2++] = buffer[i2--];
buffer[i1] = buffer[j1];
buffer[j2] = buffer[i2];
};
Transform.prototype.iterate = function Transform_iterate(
ll,
hl_lh_hh,
u0,
v0
) {
}
filter(x, offset, length) {
unreachable("Abstract method `filter` called");
}
iterate(ll, hl_lh_hh, u0, v0) {
const llWidth = ll.width,
llHeight = ll.height;
let llItems = ll.items;
@ -2181,10 +2179,7 @@ const JpxImage = (function JpxImageClosure() {
this.extend(rowBuffer, bufferPadding, width);
this.filter(rowBuffer, bufferPadding, width);
items.set(
rowBuffer.subarray(bufferPadding, bufferPadding + width),
k
);
items.set(rowBuffer.subarray(bufferPadding, bufferPadding + width), k);
}
}
@ -2241,28 +2236,13 @@ const JpxImage = (function JpxImageClosure() {
}
}
return {
width,
height,
items,
};
};
return Transform;
})();
// Section 3.8.2 Irreversible 9-7 filter
const IrreversibleTransform = (function IrreversibleTransformClosure() {
// eslint-disable-next-line no-shadow
function IrreversibleTransform() {
Transform.call(this);
return { width, height, items };
}
}
IrreversibleTransform.prototype = Object.create(Transform.prototype);
IrreversibleTransform.prototype.filter = function irreversibleTransformFilter(
x,
offset,
length
) {
// Section 3.8.2 Irreversible 9-7 filter
class IrreversibleTransform extends Transform {
filter(x, offset, length) {
const len = length >> 1;
offset = offset | 0;
let j, n, current, next;
@ -2343,24 +2323,12 @@ const JpxImage = (function JpxImageClosure() {
}
}
}
};
return IrreversibleTransform;
})();
// Section 3.8.1 Reversible 5-3 filter
const ReversibleTransform = (function ReversibleTransformClosure() {
// eslint-disable-next-line no-shadow
function ReversibleTransform() {
Transform.call(this);
}
}
ReversibleTransform.prototype = Object.create(Transform.prototype);
ReversibleTransform.prototype.filter = function reversibleTransformFilter(
x,
offset,
length
) {
// Section 3.8.1 Reversible 5-3 filter
class ReversibleTransform extends Transform {
filter(x, offset, length) {
const len = length >> 1;
offset = offset | 0;
let j, n;
@ -2372,12 +2340,7 @@ const JpxImage = (function JpxImageClosure() {
for (j = offset + 1, n = len; n--; j += 2) {
x[j] += (x[j - 1] + x[j + 1]) >> 1;
}
};
return ReversibleTransform;
})();
return JpxImage;
})();
}
}
export { JpxImage };