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