Convert src/core/jpg.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 12:43:28 +02:00
parent d0a299713c
commit 69dea39a42

View File

@ -44,9 +44,8 @@ class EOIMarkerError extends BaseException {}
* (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf) * (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
*/ */
const JpegImage = (function JpegImageClosure() { // prettier-ignore
// prettier-ignore const dctZigZag = new Uint8Array([
const dctZigZag = new Uint8Array([
0, 0,
1, 8, 1, 8,
16, 9, 2, 16, 9, 2,
@ -64,22 +63,16 @@ const JpegImage = (function JpegImageClosure() {
63 63
]); ]);
const dctCos1 = 4017; // cos(pi/16) const dctCos1 = 4017; // cos(pi/16)
const dctSin1 = 799; // sin(pi/16) const dctSin1 = 799; // sin(pi/16)
const dctCos3 = 3406; // cos(3*pi/16) const dctCos3 = 3406; // cos(3*pi/16)
const dctSin3 = 2276; // sin(3*pi/16) const dctSin3 = 2276; // sin(3*pi/16)
const dctCos6 = 1567; // cos(6*pi/16) const dctCos6 = 1567; // cos(6*pi/16)
const dctSin6 = 3784; // sin(6*pi/16) const dctSin6 = 3784; // sin(6*pi/16)
const dctSqrt2 = 5793; // sqrt(2) const dctSqrt2 = 5793; // sqrt(2)
const dctSqrt1d2 = 2896; // sqrt(2) / 2 const dctSqrt1d2 = 2896; // sqrt(2) / 2
// eslint-disable-next-line no-shadow function buildHuffmanTable(codeLengths, values) {
function JpegImage({ decodeTransform = null, colorTransform = -1 } = {}) {
this._decodeTransform = decodeTransform;
this._colorTransform = colorTransform;
}
function buildHuffmanTable(codeLengths, values) {
let k = 0, let k = 0,
i, i,
j, j,
@ -114,13 +107,13 @@ const JpegImage = (function JpegImageClosure() {
} }
} }
return code[0].children; return code[0].children;
} }
function getBlockBufferOffset(component, row, col) { function getBlockBufferOffset(component, row, col) {
return 64 * ((component.blocksPerLine + 1) * row + col); return 64 * ((component.blocksPerLine + 1) * row + col);
} }
function decodeScan( function decodeScan(
data, data,
offset, offset,
frame, frame,
@ -131,7 +124,7 @@ const JpegImage = (function JpegImageClosure() {
successivePrev, successivePrev,
successive, successive,
parseDNLMarker = false parseDNLMarker = false
) { ) {
const mcusPerLine = frame.mcusPerLine; const mcusPerLine = frame.mcusPerLine;
const progressive = frame.progressive; const progressive = frame.progressive;
@ -335,8 +328,7 @@ const JpegImage = (function JpegImageClosure() {
if (component.blockData[offsetZ]) { if (component.blockData[offsetZ]) {
component.blockData[offsetZ] += sign * (readBit() << successive); component.blockData[offsetZ] += sign * (readBit() << successive);
} else { } else {
component.blockData[offsetZ] = component.blockData[offsetZ] = successiveACNextValue << successive;
successiveACNextValue << successive;
successiveACState = 0; successiveACState = 0;
} }
break; break;
@ -459,14 +451,14 @@ const JpegImage = (function JpegImageClosure() {
} }
return offset - startOffset; return offset - startOffset;
} }
// A port of poppler's IDCT method which in turn is taken from: // A port of poppler's IDCT method which in turn is taken from:
// Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz, // Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
// 'Practical Fast 1-D DCT Algorithms with 11 Multiplications', // 'Practical Fast 1-D DCT Algorithms with 11 Multiplications',
// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989, // IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
// 988-991. // 988-991.
function quantizeAndInverse(component, blockBufferOffset, p) { function quantizeAndInverse(component, blockBufferOffset, p) {
const qt = component.quantizationTable, const qt = component.quantizationTable,
blockData = component.blockData; blockData = component.blockData;
let v0, v1, v2, v3, v4, v5, v6, v7; let v0, v1, v2, v3, v4, v5, v6, v7;
@ -704,9 +696,9 @@ const JpegImage = (function JpegImageClosure() {
blockData[blockBufferOffset + col + 48] = p6; blockData[blockBufferOffset + col + 48] = p6;
blockData[blockBufferOffset + col + 56] = p7; blockData[blockBufferOffset + col + 56] = p7;
} }
} }
function buildComponentData(frame, component) { function buildComponentData(frame, component) {
const blocksPerLine = component.blocksPerLine; const blocksPerLine = component.blocksPerLine;
const blocksPerColumn = component.blocksPerColumn; const blocksPerColumn = component.blocksPerColumn;
const computationBuffer = new Int16Array(64); const computationBuffer = new Int16Array(64);
@ -718,9 +710,9 @@ const JpegImage = (function JpegImageClosure() {
} }
} }
return component.blockData; return component.blockData;
} }
function findNextFileMarker(data, currentPos, startPos = currentPos) { function findNextFileMarker(data, currentPos, startPos = currentPos) {
const maxPos = data.length - 1; const maxPos = data.length - 1;
let newPos = startPos < currentPos ? startPos : currentPos; let newPos = startPos < currentPos ? startPos : currentPos;
@ -747,9 +739,14 @@ const JpegImage = (function JpegImageClosure() {
marker: newMarker, marker: newMarker,
offset: newPos, offset: newPos,
}; };
}
class JpegImage {
constructor({ decodeTransform = null, colorTransform = -1 } = {}) {
this._decodeTransform = decodeTransform;
this._colorTransform = colorTransform;
} }
JpegImage.prototype = {
parse(data, { dnlScanLines = null } = {}) { parse(data, { dnlScanLines = null } = {}) {
function readDataBlock() { function readDataBlock() {
const length = readUint16(data, offset); const length = readUint16(data, offset);
@ -1101,7 +1098,7 @@ const JpegImage = (function JpegImageClosure() {
} }
this.numComponents = this.components.length; this.numComponents = this.components.length;
return undefined; return undefined;
}, }
_getLinearizedBlockData(width, height, isSourcePDF = false) { _getLinearizedBlockData(width, height, isSourcePDF = false) {
const scaleX = this.width / width, const scaleX = this.width / width,
@ -1174,7 +1171,7 @@ const JpegImage = (function JpegImageClosure() {
} }
} }
return data; return data;
}, }
get _isColorConversionNeeded() { get _isColorConversionNeeded() {
if (this.adobe) { if (this.adobe) {
@ -1206,9 +1203,9 @@ const JpegImage = (function JpegImageClosure() {
return true; return true;
} }
return false; return false;
}, }
_convertYccToRgb: function convertYccToRgb(data) { _convertYccToRgb(data) {
let Y, Cb, Cr; let Y, Cb, Cr;
for (let i = 0, length = data.length; i < length; i += 3) { for (let i = 0, length = data.length; i < length; i += 3) {
Y = data[i]; Y = data[i];
@ -1219,9 +1216,9 @@ const JpegImage = (function JpegImageClosure() {
data[i + 2] = Y - 226.816 + 1.772 * Cb; data[i + 2] = Y - 226.816 + 1.772 * Cb;
} }
return data; return data;
}, }
_convertYcckToRgb: function convertYcckToRgb(data) { _convertYcckToRgb(data) {
let Y, Cb, Cr, k; let Y, Cb, Cr, k;
let offset = 0; let offset = 0;
for (let i = 0, length = data.length; i < length; i += 4) { for (let i = 0, length = data.length; i < length; i += 4) {
@ -1289,9 +1286,9 @@ const JpegImage = (function JpegImageClosure() {
} }
// Ensure that only the converted RGB data is returned. // Ensure that only the converted RGB data is returned.
return data.subarray(0, offset); return data.subarray(0, offset);
}, }
_convertYcckToCmyk: function convertYcckToCmyk(data) { _convertYcckToCmyk(data) {
let Y, Cb, Cr; let Y, Cb, Cr;
for (let i = 0, length = data.length; i < length; i += 4) { for (let i = 0, length = data.length; i < length; i += 4) {
Y = data[i]; Y = data[i];
@ -1303,9 +1300,9 @@ const JpegImage = (function JpegImageClosure() {
// K in data[i + 3] is unchanged // K in data[i + 3] is unchanged
} }
return data; return data;
}, }
_convertCmykToRgb: function convertCmykToRgb(data) { _convertCmykToRgb(data) {
let c, m, y, k; let c, m, y, k;
let offset = 0; let offset = 0;
for (let i = 0, length = data.length; i < length; i += 4) { for (let i = 0, length = data.length; i < length; i += 4) {
@ -1373,7 +1370,7 @@ const JpegImage = (function JpegImageClosure() {
} }
// Ensure that only the converted RGB data is returned. // Ensure that only the converted RGB data is returned.
return data.subarray(0, offset); return data.subarray(0, offset);
}, }
getData({ width, height, forceRGB = false, isSourcePDF = false }) { getData({ width, height, forceRGB = false, isSourcePDF = false }) {
if ( if (
@ -1415,10 +1412,7 @@ const JpegImage = (function JpegImageClosure() {
} }
} }
return data; return data;
}, }
}; }
return JpegImage;
})();
export { JpegImage }; export { JpegImage };