1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Merge pull request #9729 from Snuffleupagus/gulp-image_decoders

Add a `gulp image_decoders` command to package the image decoders (i.e. jpg.js, jpx.js, jbig2.js) separately, and publish them in pdfjs-dist
This commit is contained in:
Tim van der Meij 2018-06-26 23:27:32 +02:00 committed by GitHub
commit 14b69a4c1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 288 additions and 14 deletions

View file

@ -1192,6 +1192,47 @@ var Jbig2Image = (function Jbig2ImageClosure() {
return visitor.buffer;
}
function parseJbig2(data) {
let position = 0, end = data.length;
if (data[position] !== 0x97 || data[position + 1] !== 0x4A ||
data[position + 2] !== 0x42 || data[position + 3] !== 0x32 ||
data[position + 4] !== 0x0D || data[position + 5] !== 0x0A ||
data[position + 6] !== 0x1A || data[position + 7] !== 0x0A) {
throw new Jbig2Error('parseJbig2 - invalid header.');
}
let header = Object.create(null);
position += 8;
const flags = data[position++];
header.randomAccess = !(flags & 1);
if (!(flags & 2)) {
header.numberOfPages = readUint32(data, position);
position += 4;
}
let segments = readSegments(header, data, position, end);
let visitor = new SimpleSegmentVisitor();
processSegments(segments, visitor);
const { width, height, } = visitor.currentPageInfo;
const bitPacked = visitor.buffer;
let imgData = new Uint8ClampedArray(width * height);
let q = 0, k = 0;
for (let i = 0; i < height; i++) {
let mask = 0, buffer;
for (let j = 0; j < width; j++) {
if (!mask) {
mask = 128; buffer = bitPacked[k++];
}
imgData[q++] = (buffer & mask) ? 0 : 255;
mask >>= 1;
}
}
return { imgData, width, height, };
}
function SimpleSegmentVisitor() {}
SimpleSegmentVisitor.prototype = {
@ -2095,9 +2136,16 @@ var Jbig2Image = (function Jbig2ImageClosure() {
function Jbig2Image() {}
Jbig2Image.prototype = {
parseChunks: function Jbig2Image_parseChunks(chunks) {
parseChunks(chunks) {
return parseJbig2Chunks(chunks);
},
parse(data) {
const { imgData, width, height, } = parseJbig2(data);
this.width = width;
this.height = height;
return imgData;
},
};
return Jbig2Image;

View file

@ -63,7 +63,10 @@ let JpegStream = (function JpegStreamClosure() {
if (this.eof) {
return;
}
let jpegImage = new JpegImage();
let jpegOptions = {
decodeTransform: undefined,
colorTransform: undefined,
};
// Checking if values need to be transformed before conversion.
let decodeArr = this.dict.getArray('Decode', 'D');
@ -81,16 +84,17 @@ let JpegStream = (function JpegStreamClosure() {
}
}
if (transformNeeded) {
jpegImage.decodeTransform = transform;
jpegOptions.decodeTransform = transform;
}
}
// Fetching the 'ColorTransform' entry, if it exists.
if (isDict(this.params)) {
let colorTransform = this.params.get('ColorTransform');
if (Number.isInteger(colorTransform)) {
jpegImage.colorTransform = colorTransform;
jpegOptions.colorTransform = colorTransform;
}
}
const jpegImage = new JpegImage(jpegOptions);
jpegImage.parse(this.bytes);
let data = jpegImage.getData(this.drawWidth, this.drawHeight,

View file

@ -94,9 +94,9 @@ var JpegImage = (function JpegImageClosure() {
var dctSqrt2 = 5793; // sqrt(2)
var dctSqrt1d2 = 2896; // sqrt(2) / 2
function JpegImage() {
this.decodeTransform = null;
this.colorTransform = -1;
function JpegImage({ decodeTransform = null, colorTransform = -1, } = {}) {
this._decodeTransform = decodeTransform;
this._colorTransform = colorTransform;
}
function buildHuffmanTable(codeLengths, values) {
@ -1013,7 +1013,7 @@ var JpegImage = (function JpegImageClosure() {
}
// decodeTransform contains pairs of multiplier (-256..256) and additive
var transform = this.decodeTransform;
const transform = this._decodeTransform;
if (transform) {
for (i = 0; i < dataLength;) {
for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
@ -1030,7 +1030,7 @@ var JpegImage = (function JpegImageClosure() {
return !!this.adobe.transformCode;
}
if (this.numComponents === 3) {
if (this.colorTransform === 0) {
if (this._colorTransform === 0) {
// If the Adobe transform marker is not present and the image
// dictionary has a 'ColorTransform' entry, explicitly set to `0`,
// then the colours should *not* be transformed.
@ -1039,7 +1039,7 @@ var JpegImage = (function JpegImageClosure() {
return true;
}
// `this.numComponents !== 3`
if (this.colorTransform === 1) {
if (this._colorTransform === 1) {
// If the Adobe transform marker is not present and the image
// dictionary has a 'ColorTransform' entry, explicitly set to `1`,
// then the colours should be transformed.