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

Given that we're using modules, meaning that only explicitly `export`ed things are visible to the outside, it's no longer necessary to wrap all of the code in a closure.
This commit is contained in:
Jonas Jenwald 2021-04-27 12:09:40 +02:00
parent ca668587c6
commit 8ff213871b

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 };