Merge pull request #13304 from Snuffleupagus/src-core-classes

Convert more code in `src/core/` to use standard classes
This commit is contained in:
Tim van der Meij 2021-04-27 19:37:09 +02:00 committed by GitHub
commit fae183b7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 978 additions and 1015 deletions

View File

@ -19,29 +19,28 @@
* license.
*/
import { info } from "../shared/util.js";
/**
* @typedef {Object} CCITTFaxDecoderSource
* @property {function} next - Method that return one byte of data for decoding,
* or -1 when EOF is reached.
*/
import { info } from "../shared/util.js";
const ccittEOL = -2;
const ccittEOF = -1;
const twoDimPass = 0;
const twoDimHoriz = 1;
const twoDimVert0 = 2;
const twoDimVertR1 = 3;
const twoDimVertL1 = 4;
const twoDimVertR2 = 5;
const twoDimVertL2 = 6;
const twoDimVertR3 = 7;
const twoDimVertL3 = 8;
const CCITTFaxDecoder = (function CCITTFaxDecoder() {
const ccittEOL = -2;
const ccittEOF = -1;
const twoDimPass = 0;
const twoDimHoriz = 1;
const twoDimVert0 = 2;
const twoDimVertR1 = 3;
const twoDimVertL1 = 4;
const twoDimVertR2 = 5;
const twoDimVertL2 = 6;
const twoDimVertR3 = 7;
const twoDimVertL3 = 8;
// prettier-ignore
const twoDimTable = [
// prettier-ignore
const twoDimTable = [
[-1, -1], [-1, -1], // 000000x
[7, twoDimVertL3], // 0000010
[7, twoDimVertR3], // 0000011
@ -107,10 +106,10 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[1, twoDimVert0], [1, twoDimVert0],
[1, twoDimVert0], [1, twoDimVert0],
[1, twoDimVert0], [1, twoDimVert0]
];
];
// prettier-ignore
const whiteTable1 = [
// prettier-ignore
const whiteTable1 = [
[-1, -1], // 00000
[12, ccittEOL], // 00001
[-1, -1], [-1, -1], // 0001x
@ -130,10 +129,10 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[12, 2432], // 11101
[12, 2496], // 11110
[12, 2560] // 11111
];
];
// prettier-ignore
const whiteTable2 = [
// prettier-ignore
const whiteTable2 = [
[-1, -1], [-1, -1], [-1, -1], [-1, -1], // 0000000xx
[8, 29], [8, 29], // 00000010x
[8, 30], [8, 30], // 00000011x
@ -295,10 +294,10 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[4, 7], [4, 7], [4, 7], [4, 7],
[4, 7], [4, 7], [4, 7], [4, 7],
[4, 7], [4, 7], [4, 7], [4, 7]
];
];
// prettier-ignore
const blackTable1 = [
// prettier-ignore
const blackTable1 = [
[-1, -1], [-1, -1], // 000000000000x
[12, ccittEOL], [12, ccittEOL], // 000000000001x
[-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000001xx
@ -357,10 +356,10 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[13, 1216], // 0000001110111
[10, 64], [10, 64], [10, 64], [10, 64], // 0000001111xxx
[10, 64], [10, 64], [10, 64], [10, 64]
];
];
// prettier-ignore
const blackTable2 = [
// prettier-ignore
const blackTable2 = [
[8, 13], [8, 13], [8, 13], [8, 13], // 00000100xxxx
[8, 13], [8, 13], [8, 13], [8, 13],
[8, 13], [8, 13], [8, 13], [8, 13],
@ -437,10 +436,10 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[7, 12], [7, 12], [7, 12], [7, 12],
[7, 12], [7, 12], [7, 12], [7, 12],
[7, 12], [7, 12], [7, 12], [7, 12]
];
];
// prettier-ignore
const blackTable3 = [
// prettier-ignore
const blackTable3 = [
[-1, -1], [-1, -1], [-1, -1], [-1, -1], // 0000xx
[6, 9], // 000100
[6, 8], // 000101
@ -459,14 +458,14 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
[2, 2], [2, 2], [2, 2], [2, 2],
[2, 2], [2, 2], [2, 2], [2, 2],
[2, 2], [2, 2], [2, 2], [2, 2]
];
];
/**
/**
* @param {CCITTFaxDecoderSource} source - The data which should be decoded.
* @param {Object} [options] - Decoding options.
*/
// eslint-disable-next-line no-shadow
function CCITTFaxDecoder(source, options = {}) {
class CCITTFaxDecoder {
constructor(source, options = {}) {
if (!source || typeof source.next !== "function") {
throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
}
@ -511,7 +510,6 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
}
}
CCITTFaxDecoder.prototype = {
readNextChar() {
if (this.eof) {
return -1;
@ -569,10 +567,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
code2 += code3 = this._getBlackCode();
} while (code3 >= 64);
}
this._addPixels(
codingLine[this.codingPos] + code1,
blackPixels
);
this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
if (codingLine[this.codingPos] < columns) {
this._addPixels(
codingLine[this.codingPos] + code2,
@ -845,7 +840,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
c ^= 0xff;
}
return c;
},
}
/**
* @private
@ -867,7 +862,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
codingLine[codingPos] = a1;
}
this.codingPos = codingPos;
},
}
/**
* @private
@ -900,7 +895,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
}
this.codingPos = codingPos;
},
}
/**
* This function returns the code found from the table.
@ -930,7 +925,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
}
}
return [false, 0, false];
},
}
/**
* @private
@ -953,7 +948,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
}
info("Bad two dim code");
return ccittEOF;
},
}
/**
* @private
@ -991,7 +986,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
info("bad white code");
this._eatBits(1);
return 1;
},
}
/**
* @private
@ -1034,7 +1029,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
info("bad black code");
this._eatBits(1);
return 1;
},
}
/**
* @private
@ -1052,7 +1047,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
this.inputBits += 8;
}
return (this.inputBuf >> (this.inputBits - n)) & (0xffff >> (16 - n));
},
}
/**
* @private
@ -1061,10 +1056,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
if ((this.inputBits -= n) < 0) {
this.inputBits = 0;
}
},
};
return CCITTFaxDecoder;
})();
}
}
export { CCITTFaxDecoder };

View File

@ -17,9 +17,10 @@ import { Dict, isDict } from "./primitives.js";
import { CCITTFaxDecoder } from "./ccitt.js";
import { DecodeStream } from "./stream.js";
const CCITTFaxStream = (function CCITTFaxStreamClosure() {
// eslint-disable-next-line no-shadow
function CCITTFaxStream(str, maybeLength, params) {
class CCITTFaxStream extends DecodeStream {
constructor(str, maybeLength, params) {
super(maybeLength);
this.str = str;
this.dict = str.dict;
@ -41,13 +42,9 @@ const CCITTFaxStream = (function CCITTFaxStreamClosure() {
EndOfBlock: params.get("EndOfBlock"),
BlackIs1: params.get("BlackIs1"),
});
DecodeStream.call(this, maybeLength);
}
CCITTFaxStream.prototype = Object.create(DecodeStream.prototype);
CCITTFaxStream.prototype.readBlock = function () {
readBlock() {
while (!this.eof) {
const c = this.ccittFaxDecoder.readNextChar();
if (c === -1) {
@ -57,9 +54,7 @@ const CCITTFaxStream = (function CCITTFaxStreamClosure() {
this.ensureBuffer(this.bufferLength + 1);
this.buffer[this.bufferLength++] = c;
}
};
return CCITTFaxStream;
})();
}
}
export { CCITTFaxStream };

View File

@ -22,33 +22,27 @@ import { shadow } from "../shared/util.js";
* For JBIG2's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams.
*/
const Jbig2Stream = (function Jbig2StreamClosure() {
// eslint-disable-next-line no-shadow
function Jbig2Stream(stream, maybeLength, dict, params) {
class Jbig2Stream extends DecodeStream {
constructor(stream, maybeLength, dict, params) {
super(maybeLength);
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
this.params = params;
DecodeStream.call(this, maybeLength);
}
Jbig2Stream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(Jbig2Stream.prototype, "bytes", {
get() {
get bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
}
Jbig2Stream.prototype.ensureBuffer = function (requested) {
ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`.
};
}
Jbig2Stream.prototype.readBlock = function () {
readBlock() {
if (this.eof) {
return;
}
@ -73,9 +67,7 @@ const Jbig2Stream = (function Jbig2StreamClosure() {
this.buffer = data;
this.bufferLength = dataLength;
this.eof = true;
};
return Jbig2Stream;
})();
}
}
export { Jbig2Stream };

View File

@ -22,9 +22,8 @@ import { shadow } from "../shared/util.js";
* For JPEG's we use a library to decode these images and the stream behaves
* like all the other DecodeStreams.
*/
const JpegStream = (function JpegStreamClosure() {
// eslint-disable-next-line no-shadow
function JpegStream(stream, maybeLength, dict, params) {
class JpegStream extends DecodeStream {
constructor(stream, maybeLength, dict, params) {
// Some images may contain 'junk' before the SOI (start-of-image) marker.
// Note: this seems to mainly affect inline images.
let ch;
@ -35,30 +34,25 @@ const JpegStream = (function JpegStreamClosure() {
break;
}
}
super(maybeLength);
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
this.params = params;
DecodeStream.call(this, maybeLength);
}
JpegStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpegStream.prototype, "bytes", {
get: function JpegStream_bytes() {
get bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
}
JpegStream.prototype.ensureBuffer = function (requested) {
ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`.
};
}
JpegStream.prototype.readBlock = function () {
readBlock() {
if (this.eof) {
return;
}
@ -105,9 +99,7 @@ const JpegStream = (function JpegStreamClosure() {
this.buffer = data;
this.bufferLength = data.length;
this.eof = true;
};
return JpegStream;
})();
}
}
export { JpegStream };

View File

@ -21,33 +21,27 @@ import { shadow } from "../shared/util.js";
* For JPEG 2000's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams.
*/
const JpxStream = (function JpxStreamClosure() {
// eslint-disable-next-line no-shadow
function JpxStream(stream, maybeLength, dict, params) {
class JpxStream extends DecodeStream {
constructor(stream, maybeLength, dict, params) {
super(maybeLength);
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
this.params = params;
DecodeStream.call(this, maybeLength);
}
JpxStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpxStream.prototype, "bytes", {
get: function JpxStream_bytes() {
get bytes() {
// If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true,
});
}
JpxStream.prototype.ensureBuffer = function (requested) {
ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`.
};
}
JpxStream.prototype.readBlock = function () {
readBlock() {
if (this.eof) {
return;
}
@ -87,9 +81,7 @@ const JpxStream = (function JpxStreamClosure() {
}
this.bufferLength = this.buffer.length;
this.eof = true;
};
return JpxStream;
})();
}
}
export { JpxStream };