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