Convert src/core/jbig2.js
to use standard classes
*Please note:* Ignoring whitespace-only changes is probably necessary in order to review this.
This commit is contained in:
parent
d59c9ab3ab
commit
0addf3a0d4
@ -24,39 +24,37 @@ class Jbig2Error extends BaseException {
|
||||
}
|
||||
}
|
||||
|
||||
const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
// Utility data structures
|
||||
function ContextCache() {}
|
||||
|
||||
ContextCache.prototype = {
|
||||
// Utility data structures
|
||||
class ContextCache {
|
||||
getContexts(id) {
|
||||
if (id in this) {
|
||||
return this[id];
|
||||
}
|
||||
return (this[id] = new Int8Array(1 << 16));
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function DecodingContext(data, start, end) {
|
||||
class DecodingContext {
|
||||
constructor(data, start, end) {
|
||||
this.data = data;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
DecodingContext.prototype = {
|
||||
get decoder() {
|
||||
const decoder = new ArithmeticDecoder(this.data, this.start, this.end);
|
||||
return shadow(this, "decoder", decoder);
|
||||
},
|
||||
}
|
||||
|
||||
get contextCache() {
|
||||
const cache = new ContextCache();
|
||||
return shadow(this, "contextCache", cache);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Annex A. Arithmetic Integer Decoding Procedure
|
||||
// A.2 Procedure for decoding values
|
||||
function decodeInteger(contextCache, procedure, decoder) {
|
||||
// Annex A. Arithmetic Integer Decoding Procedure
|
||||
// A.2 Procedure for decoding values
|
||||
function decodeInteger(contextCache, procedure, decoder) {
|
||||
const contexts = contextCache.getContexts(procedure);
|
||||
let prev = 1;
|
||||
|
||||
@ -64,8 +62,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
let v = 0;
|
||||
for (let i = 0; i < length; i++) {
|
||||
const bit = decoder.readBit(contexts, prev);
|
||||
prev =
|
||||
prev < 256 ? (prev << 1) | bit : (((prev << 1) | bit) & 511) | 256;
|
||||
prev = prev < 256 ? (prev << 1) | bit : (((prev << 1) | bit) & 511) | 256;
|
||||
v = (v << 1) | bit;
|
||||
}
|
||||
return v >>> 0;
|
||||
@ -92,10 +89,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
return -value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// A.3 The IAID decoding procedure
|
||||
function decodeIAID(contextCache, decoder, codeLength) {
|
||||
// A.3 The IAID decoding procedure
|
||||
function decodeIAID(contextCache, decoder, codeLength) {
|
||||
const contexts = contextCache.getContexts("IAID");
|
||||
|
||||
let prev = 1;
|
||||
@ -107,10 +104,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
return prev & ((1 << codeLength) - 1);
|
||||
}
|
||||
return prev & 0x7fffffff;
|
||||
}
|
||||
}
|
||||
|
||||
// 7.3 Segment types
|
||||
const SegmentTypes = [
|
||||
// 7.3 Segment types
|
||||
const SegmentTypes = [
|
||||
"SymbolDictionary",
|
||||
null,
|
||||
null,
|
||||
@ -174,9 +171,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
null,
|
||||
null,
|
||||
"Extension",
|
||||
];
|
||||
];
|
||||
|
||||
const CodingTemplates = [
|
||||
const CodingTemplates = [
|
||||
[
|
||||
{ x: -1, y: -2 },
|
||||
{ x: 0, y: -2 },
|
||||
@ -227,9 +224,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
{ x: -2, y: 0 },
|
||||
{ x: -1, y: 0 },
|
||||
],
|
||||
];
|
||||
];
|
||||
|
||||
const RefinementTemplates = [
|
||||
const RefinementTemplates = [
|
||||
{
|
||||
coding: [
|
||||
{ x: 0, y: -1 },
|
||||
@ -263,22 +260,22 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
{ x: 1, y: 1 },
|
||||
],
|
||||
},
|
||||
];
|
||||
];
|
||||
|
||||
// See 6.2.5.7 Decoding the bitmap.
|
||||
const ReusedContexts = [
|
||||
// See 6.2.5.7 Decoding the bitmap.
|
||||
const ReusedContexts = [
|
||||
0x9b25, // 10011 0110010 0101
|
||||
0x0795, // 0011 110010 101
|
||||
0x00e5, // 001 11001 01
|
||||
0x0195, // 011001 0101
|
||||
];
|
||||
];
|
||||
|
||||
const RefinementReusedContexts = [
|
||||
const RefinementReusedContexts = [
|
||||
0x0020, // '000' + '0' (coding) + '00010000' + '0' (reference)
|
||||
0x0008, // '0000' + '001000'
|
||||
];
|
||||
];
|
||||
|
||||
function decodeBitmapTemplate0(width, height, decodingContext) {
|
||||
function decodeBitmapTemplate0(width, height, decodingContext) {
|
||||
const decoder = decodingContext.decoder;
|
||||
const contexts = decodingContext.contextCache.getContexts("GB");
|
||||
const bitmap = [];
|
||||
@ -319,10 +316,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
// 6.2 Generic Region Decoding Procedure
|
||||
function decodeBitmap(
|
||||
// 6.2 Generic Region Decoding Procedure
|
||||
function decodeBitmap(
|
||||
mmr,
|
||||
width,
|
||||
height,
|
||||
@ -331,7 +328,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
skip,
|
||||
at,
|
||||
decodingContext
|
||||
) {
|
||||
) {
|
||||
if (mmr) {
|
||||
const input = new Reader(
|
||||
decodingContext.data,
|
||||
@ -482,10 +479,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
// 6.3.2 Generic Refinement Region Decoding Procedure
|
||||
function decodeRefinement(
|
||||
// 6.3.2 Generic Refinement Region Decoding Procedure
|
||||
function decodeRefinement(
|
||||
width,
|
||||
height,
|
||||
templateIndex,
|
||||
@ -495,7 +492,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
prediction,
|
||||
at,
|
||||
decodingContext
|
||||
) {
|
||||
) {
|
||||
let codingTemplate = RefinementTemplates[templateIndex].coding;
|
||||
if (templateIndex === 0) {
|
||||
codingTemplate = codingTemplate.concat([at[0]]);
|
||||
@ -555,12 +552,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
for (k = 0; k < referenceTemplateLength; k++) {
|
||||
i0 = i + referenceTemplateY[k] - offsetY;
|
||||
j0 = j + referenceTemplateX[k] - offsetX;
|
||||
if (
|
||||
i0 < 0 ||
|
||||
i0 >= referenceHeight ||
|
||||
j0 < 0 ||
|
||||
j0 >= referenceWidth
|
||||
) {
|
||||
if (i0 < 0 || i0 >= referenceHeight || j0 < 0 || j0 >= referenceWidth) {
|
||||
contextLabel <<= 1; // out of bound pixel
|
||||
} else {
|
||||
contextLabel = (contextLabel << 1) | referenceBitmap[i0][j0];
|
||||
@ -572,10 +564,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
// 6.5.5 Decoding the symbol dictionary
|
||||
function decodeSymbolDictionary(
|
||||
// 6.5.5 Decoding the symbol dictionary
|
||||
function decodeSymbolDictionary(
|
||||
huffman,
|
||||
refinement,
|
||||
symbols,
|
||||
@ -588,7 +580,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
refinementAt,
|
||||
decodingContext,
|
||||
huffmanInput
|
||||
) {
|
||||
) {
|
||||
if (huffman && refinement) {
|
||||
throw new Jbig2Error("symbol refinement with Huffman is not supported");
|
||||
}
|
||||
@ -626,11 +618,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
let bitmap;
|
||||
if (refinement) {
|
||||
// 6.5.8.2 Refinement/aggregate-coded symbol bitmap
|
||||
const numberOfInstances = decodeInteger(
|
||||
contextCache,
|
||||
"IAAI",
|
||||
decoder
|
||||
);
|
||||
const numberOfInstances = decodeInteger(contextCache, "IAAI", decoder);
|
||||
if (numberOfInstances > 1) {
|
||||
bitmap = decodeTextRegion(
|
||||
huffman,
|
||||
@ -654,11 +642,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
huffmanInput
|
||||
);
|
||||
} else {
|
||||
const symbolId = decodeIAID(
|
||||
contextCache,
|
||||
decoder,
|
||||
symbolCodeLength
|
||||
);
|
||||
const symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);
|
||||
const rdx = decodeInteger(contextCache, "IARDX", decoder); // 6.4.11.3
|
||||
const rdy = decodeInteger(contextCache, "IARDY", decoder); // 6.4.11.4
|
||||
const symbol =
|
||||
@ -776,9 +760,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
return exportedSymbols;
|
||||
}
|
||||
}
|
||||
|
||||
function decodeTextRegion(
|
||||
function decodeTextRegion(
|
||||
huffman,
|
||||
refinement,
|
||||
width,
|
||||
@ -798,7 +782,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
decodingContext,
|
||||
logStripSize,
|
||||
huffmanInput
|
||||
) {
|
||||
) {
|
||||
if (huffman && refinement) {
|
||||
throw new Jbig2Error("refinement with Huffman is not supported");
|
||||
}
|
||||
@ -942,16 +926,16 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
} while (true);
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
function decodePatternDictionary(
|
||||
function decodePatternDictionary(
|
||||
mmr,
|
||||
patternWidth,
|
||||
patternHeight,
|
||||
maxPatternIndex,
|
||||
template,
|
||||
decodingContext
|
||||
) {
|
||||
) {
|
||||
const at = [];
|
||||
if (!mmr) {
|
||||
at.push({
|
||||
@ -996,9 +980,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
patterns.push(patternBitmap);
|
||||
}
|
||||
return patterns;
|
||||
}
|
||||
}
|
||||
|
||||
function decodeHalftoneRegion(
|
||||
function decodeHalftoneRegion(
|
||||
mmr,
|
||||
patterns,
|
||||
template,
|
||||
@ -1014,16 +998,14 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
gridVectorX,
|
||||
gridVectorY,
|
||||
decodingContext
|
||||
) {
|
||||
) {
|
||||
const skip = null;
|
||||
if (enableSkip) {
|
||||
throw new Jbig2Error("skip is not supported");
|
||||
}
|
||||
if (combinationOperator !== 0) {
|
||||
throw new Jbig2Error(
|
||||
"operator " +
|
||||
combinationOperator +
|
||||
" is not supported in halftone region"
|
||||
`operator "${combinationOperator}" is not supported in halftone region`
|
||||
);
|
||||
}
|
||||
|
||||
@ -1142,9 +1124,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
return regionBitmap;
|
||||
}
|
||||
}
|
||||
|
||||
function readSegmentHeader(data, start) {
|
||||
function readSegmentHeader(data, start) {
|
||||
const segmentHeader = {};
|
||||
segmentHeader.number = readUint32(data, start);
|
||||
const flags = data[start + 4];
|
||||
@ -1243,9 +1225,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
segmentHeader.headerEnd = position;
|
||||
return segmentHeader;
|
||||
}
|
||||
}
|
||||
|
||||
function readSegments(header, data, start, end) {
|
||||
function readSegments(header, data, start, end) {
|
||||
const segments = [];
|
||||
let position = start;
|
||||
while (position < end) {
|
||||
@ -1273,10 +1255,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
return segments;
|
||||
}
|
||||
}
|
||||
|
||||
// 7.4.1 Region segment information field
|
||||
function readRegionSegmentInformation(data, start) {
|
||||
// 7.4.1 Region segment information field
|
||||
function readRegionSegmentInformation(data, start) {
|
||||
return {
|
||||
width: readUint32(data, start),
|
||||
height: readUint32(data, start + 4),
|
||||
@ -1284,10 +1266,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
y: readUint32(data, start + 12),
|
||||
combinationOperator: data[start + 16] & 7,
|
||||
};
|
||||
}
|
||||
const RegionSegmentInformationFieldLength = 17;
|
||||
}
|
||||
const RegionSegmentInformationFieldLength = 17;
|
||||
|
||||
function processSegment(segment, visitor) {
|
||||
function processSegment(segment, visitor) {
|
||||
const header = segment.header;
|
||||
|
||||
const data = segment.data,
|
||||
@ -1495,15 +1477,15 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
if (callbackName in visitor) {
|
||||
visitor[callbackName].apply(visitor, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processSegments(segments, visitor) {
|
||||
function processSegments(segments, visitor) {
|
||||
for (let i = 0, ii = segments.length; i < ii; i++) {
|
||||
processSegment(segments[i], visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseJbig2Chunks(chunks) {
|
||||
function parseJbig2Chunks(chunks) {
|
||||
const visitor = new SimpleSegmentVisitor();
|
||||
for (let i = 0, ii = chunks.length; i < ii; i++) {
|
||||
const chunk = chunks[i];
|
||||
@ -1511,9 +1493,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
processSegments(segments, visitor);
|
||||
}
|
||||
return visitor.buffer;
|
||||
}
|
||||
}
|
||||
|
||||
function parseJbig2(data) {
|
||||
function parseJbig2(data) {
|
||||
const end = data.length;
|
||||
let position = 0;
|
||||
|
||||
@ -1562,12 +1544,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
|
||||
return { imgData, width, height };
|
||||
}
|
||||
}
|
||||
|
||||
function SimpleSegmentVisitor() {}
|
||||
|
||||
SimpleSegmentVisitor.prototype = {
|
||||
onPageInformation: function SimpleSegmentVisitor_onPageInformation(info) {
|
||||
class SimpleSegmentVisitor {
|
||||
onPageInformation(info) {
|
||||
this.currentPageInfo = info;
|
||||
const rowSize = (info.width + 7) >> 3;
|
||||
const buffer = new Uint8ClampedArray(rowSize * info.height);
|
||||
@ -1579,8 +1559,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
this.buffer = buffer;
|
||||
},
|
||||
drawBitmap: function SimpleSegmentVisitor_drawBitmap(regionInfo, bitmap) {
|
||||
}
|
||||
|
||||
drawBitmap(regionInfo, bitmap) {
|
||||
const pageInfo = this.currentPageInfo;
|
||||
const width = regionInfo.width,
|
||||
height = regionInfo.height;
|
||||
@ -1632,13 +1613,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
`operator ${combinationOperator} is not supported`
|
||||
);
|
||||
}
|
||||
},
|
||||
onImmediateGenericRegion: function SimpleSegmentVisitor_onImmediateGenericRegion(
|
||||
region,
|
||||
data,
|
||||
start,
|
||||
end
|
||||
) {
|
||||
}
|
||||
|
||||
onImmediateGenericRegion(region, data, start, end) {
|
||||
const regionInfo = region.info;
|
||||
const decodingContext = new DecodingContext(data, start, end);
|
||||
const bitmap = decodeBitmap(
|
||||
@ -1652,11 +1629,13 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
decodingContext
|
||||
);
|
||||
this.drawBitmap(regionInfo, bitmap);
|
||||
},
|
||||
onImmediateLosslessGenericRegion: function SimpleSegmentVisitor_onImmediateLosslessGenericRegion() {
|
||||
}
|
||||
|
||||
onImmediateLosslessGenericRegion() {
|
||||
this.onImmediateGenericRegion.apply(this, arguments);
|
||||
},
|
||||
onSymbolDictionary: function SimpleSegmentVisitor_onSymbolDictionary(
|
||||
}
|
||||
|
||||
onSymbolDictionary(
|
||||
dictionary,
|
||||
currentSegment,
|
||||
referredSegments,
|
||||
@ -1705,14 +1684,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
decodingContext,
|
||||
huffmanInput
|
||||
);
|
||||
},
|
||||
onImmediateTextRegion: function SimpleSegmentVisitor_onImmediateTextRegion(
|
||||
region,
|
||||
referredSegments,
|
||||
data,
|
||||
start,
|
||||
end
|
||||
) {
|
||||
}
|
||||
|
||||
onImmediateTextRegion(region, referredSegments, data, start, end) {
|
||||
const regionInfo = region.info;
|
||||
let huffmanTables, huffmanInput;
|
||||
|
||||
@ -1762,10 +1736,12 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
huffmanInput
|
||||
);
|
||||
this.drawBitmap(regionInfo, bitmap);
|
||||
},
|
||||
onImmediateLosslessTextRegion: function SimpleSegmentVisitor_onImmediateLosslessTextRegion() {
|
||||
}
|
||||
|
||||
onImmediateLosslessTextRegion() {
|
||||
this.onImmediateTextRegion.apply(this, arguments);
|
||||
},
|
||||
}
|
||||
|
||||
onPatternDictionary(dictionary, currentSegment, data, start, end) {
|
||||
let patterns = this.patterns;
|
||||
if (!patterns) {
|
||||
@ -1780,7 +1756,8 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
dictionary.template,
|
||||
decodingContext
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
onImmediateHalftoneRegion(region, referredSegments, data, start, end) {
|
||||
// HalftoneRegion refers to exactly one PatternDictionary.
|
||||
const patterns = this.patterns[referredSegments[0]];
|
||||
@ -1804,20 +1781,23 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
decodingContext
|
||||
);
|
||||
this.drawBitmap(regionInfo, bitmap);
|
||||
},
|
||||
}
|
||||
|
||||
onImmediateLosslessHalftoneRegion() {
|
||||
this.onImmediateHalftoneRegion.apply(this, arguments);
|
||||
},
|
||||
}
|
||||
|
||||
onTables(currentSegment, data, start, end) {
|
||||
let customTables = this.customTables;
|
||||
if (!customTables) {
|
||||
this.customTables = customTables = {};
|
||||
}
|
||||
customTables[currentSegment] = decodeTablesSegment(data, start, end);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function HuffmanLine(lineData) {
|
||||
class HuffmanLine {
|
||||
constructor(lineData) {
|
||||
if (lineData.length === 2) {
|
||||
// OOB line.
|
||||
this.isOOB = true;
|
||||
@ -1837,8 +1817,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
this.isLowerRange = lineData[4] === "lower";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function HuffmanTreeNode(line) {
|
||||
class HuffmanTreeNode {
|
||||
constructor(line) {
|
||||
this.children = [];
|
||||
if (line) {
|
||||
// Leaf node
|
||||
@ -1853,7 +1835,6 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
|
||||
HuffmanTreeNode.prototype = {
|
||||
buildTree(line, shift) {
|
||||
const bit = (line.prefixCode >> shift) & 1;
|
||||
if (shift <= 0) {
|
||||
@ -1867,7 +1848,8 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
node.buildTree(line, shift - 1);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
decodeNode(reader) {
|
||||
if (this.isLeaf) {
|
||||
if (this.isOOB) {
|
||||
@ -1881,10 +1863,11 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
throw new Jbig2Error("invalid Huffman data");
|
||||
}
|
||||
return node.decodeNode(reader);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function HuffmanTable(lines, prefixCodesDone) {
|
||||
class HuffmanTable {
|
||||
constructor(lines, prefixCodesDone) {
|
||||
if (!prefixCodesDone) {
|
||||
this.assignPrefixCodes(lines);
|
||||
}
|
||||
@ -1898,10 +1881,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
|
||||
HuffmanTable.prototype = {
|
||||
decode(reader) {
|
||||
return this.rootNode.decodeNode(reader);
|
||||
},
|
||||
}
|
||||
|
||||
assignPrefixCodes(lines) {
|
||||
// Annex B.3 Assigning the prefix codes.
|
||||
const linesLength = lines.length;
|
||||
@ -1935,10 +1918,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
currentLength++;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function decodeTablesSegment(data, start, end) {
|
||||
function decodeTablesSegment(data, start, end) {
|
||||
// Decodes a Tables segment, i.e., a custom Huffman table.
|
||||
// Annex B.2 Code table structure.
|
||||
const flags = data[start];
|
||||
@ -1965,9 +1948,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
|
||||
// Lower range table line
|
||||
prefixLength = reader.readBits(prefixSizeBits);
|
||||
lines.push(
|
||||
new HuffmanLine([lowestValue - 1, prefixLength, 32, 0, "lower"])
|
||||
);
|
||||
lines.push(new HuffmanLine([lowestValue - 1, prefixLength, 32, 0, "lower"]));
|
||||
|
||||
// Upper range table line
|
||||
prefixLength = reader.readBits(prefixSizeBits);
|
||||
@ -1980,11 +1961,11 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
|
||||
return new HuffmanTable(lines, false);
|
||||
}
|
||||
}
|
||||
|
||||
const standardTablesCache = {};
|
||||
const standardTablesCache = {};
|
||||
|
||||
function getStandardTable(number) {
|
||||
function getStandardTable(number) {
|
||||
// Annex B.5 Standard Huffman tables.
|
||||
let table = standardTablesCache[number];
|
||||
if (table) {
|
||||
@ -2246,9 +2227,10 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
table = new HuffmanTable(lines, true);
|
||||
standardTablesCache[number] = table;
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
||||
function Reader(data, start, end) {
|
||||
class Reader {
|
||||
constructor(data, start, end) {
|
||||
this.data = data;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
@ -2257,7 +2239,6 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
this.currentByte = 0;
|
||||
}
|
||||
|
||||
Reader.prototype = {
|
||||
readBit() {
|
||||
if (this.shift < 0) {
|
||||
if (this.position >= this.end) {
|
||||
@ -2269,7 +2250,7 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
const bit = (this.currentByte >> this.shift) & 1;
|
||||
this.shift--;
|
||||
return bit;
|
||||
},
|
||||
}
|
||||
|
||||
readBits(numBits) {
|
||||
let result = 0,
|
||||
@ -2278,21 +2259,21 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
result |= this.readBit() << i;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
}
|
||||
|
||||
byteAlign() {
|
||||
this.shift = -1;
|
||||
},
|
||||
}
|
||||
|
||||
next() {
|
||||
if (this.position >= this.end) {
|
||||
return -1;
|
||||
}
|
||||
return this.data[this.position++];
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getCustomHuffmanTable(index, referredTo, customTables) {
|
||||
function getCustomHuffmanTable(index, referredTo, customTables) {
|
||||
// Returns a Tables segment that has been earlier decoded.
|
||||
// See 7.4.2.1.6 (symbol dictionary) or 7.4.3.1.6 (text region).
|
||||
let currentIndex = 0;
|
||||
@ -2306,15 +2287,15 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
}
|
||||
throw new Jbig2Error("can't find custom Huffman table");
|
||||
}
|
||||
}
|
||||
|
||||
function getTextRegionHuffmanTables(
|
||||
function getTextRegionHuffmanTables(
|
||||
textRegion,
|
||||
referredTo,
|
||||
customTables,
|
||||
numberOfSymbols,
|
||||
reader
|
||||
) {
|
||||
) {
|
||||
// 7.4.3.1.7 Symbol ID Huffman table decoding
|
||||
|
||||
// Read code lengths for RUNCODEs 0...34.
|
||||
@ -2435,13 +2416,13 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
tableDeltaS,
|
||||
tableDeltaT,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getSymbolDictionaryHuffmanTables(
|
||||
function getSymbolDictionaryHuffmanTables(
|
||||
dictionary,
|
||||
referredTo,
|
||||
customTables
|
||||
) {
|
||||
) {
|
||||
// 7.4.2.1.6 Symbol dictionary segment Huffman table selection
|
||||
|
||||
let customIndex = 0,
|
||||
@ -2509,9 +2490,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
tableBitmapSize,
|
||||
tableAggregateInstances,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function readUncompressedBitmap(reader, width, height) {
|
||||
function readUncompressedBitmap(reader, width, height) {
|
||||
const bitmap = [];
|
||||
for (let y = 0; y < height; y++) {
|
||||
const row = new Uint8Array(width);
|
||||
@ -2522,9 +2503,9 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
reader.byteAlign();
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
function decodeMMRBitmap(input, width, height, endOfBlock) {
|
||||
function decodeMMRBitmap(input, width, height, endOfBlock) {
|
||||
// MMR is the same compression algorithm as the PDF filter
|
||||
// CCITTFaxDecode with /K -1.
|
||||
const params = {
|
||||
@ -2569,25 +2550,19 @@ const Jbig2Image = (function Jbig2ImageClosure() {
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-shadow
|
||||
function Jbig2Image() {}
|
||||
|
||||
Jbig2Image.prototype = {
|
||||
class Jbig2Image {
|
||||
parseChunks(chunks) {
|
||||
return parseJbig2Chunks(chunks);
|
||||
},
|
||||
}
|
||||
|
||||
parse(data) {
|
||||
const { imgData, width, height } = parseJbig2(data);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
return imgData;
|
||||
},
|
||||
};
|
||||
|
||||
return Jbig2Image;
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
export { Jbig2Image };
|
||||
|
Loading…
x
Reference in New Issue
Block a user