2017-10-26 20:00:09 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
import { createObjectURL, shadow } from "../shared/util";
|
|
|
|
import { DecodeStream } from "./stream";
|
|
|
|
import { isDict } from "./primitives";
|
|
|
|
import { JpegImage } from "./jpg";
|
2017-10-26 20:00:09 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Depending on the type of JPEG a JpegStream is handled in different ways. For
|
|
|
|
* JPEG's that are supported natively such as DeviceGray and DeviceRGB the image
|
2017-10-26 20:15:57 +09:00
|
|
|
* data is stored and then loaded by the browser. For unsupported JPEG's we use
|
2017-10-26 20:00:09 +09:00
|
|
|
* a library to decode these images and the stream behaves like all the other
|
|
|
|
* DecodeStreams.
|
|
|
|
*/
|
2017-10-26 20:15:57 +09:00
|
|
|
let JpegStream = (function JpegStreamClosure() {
|
2017-10-26 20:00:09 +09:00
|
|
|
function JpegStream(stream, maybeLength, dict, params) {
|
|
|
|
// Some images may contain 'junk' before the SOI (start-of-image) marker.
|
|
|
|
// Note: this seems to mainly affect inline images.
|
2017-10-26 20:15:57 +09:00
|
|
|
let ch;
|
2017-10-26 20:00:09 +09:00
|
|
|
while ((ch = stream.getByte()) !== -1) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (ch === 0xff) {
|
|
|
|
// Find the first byte of the SOI marker (0xFFD8).
|
2017-10-26 20:00:09 +09:00
|
|
|
stream.skip(-1); // Reset the stream position to the SOI.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.stream = stream;
|
|
|
|
this.maybeLength = maybeLength;
|
|
|
|
this.dict = dict;
|
|
|
|
this.params = params;
|
|
|
|
|
|
|
|
DecodeStream.call(this, maybeLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
JpegStream.prototype = Object.create(DecodeStream.prototype);
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
Object.defineProperty(JpegStream.prototype, "bytes", {
|
2017-10-26 20:00:09 +09:00
|
|
|
get: function JpegStream_bytes() {
|
2017-10-26 20:15:57 +09:00
|
|
|
// If `this.maybeLength` is null, we'll get the entire stream.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
|
2017-10-26 20:00:09 +09:00
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
|
2017-10-26 20:15:57 +09:00
|
|
|
JpegStream.prototype.ensureBuffer = function(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() {
|
|
|
|
if (this.eof) {
|
2017-10-26 20:00:09 +09:00
|
|
|
return;
|
|
|
|
}
|
2018-05-16 20:49:01 +09:00
|
|
|
let jpegOptions = {
|
|
|
|
decodeTransform: undefined,
|
|
|
|
colorTransform: undefined,
|
|
|
|
};
|
2017-10-26 20:00:09 +09:00
|
|
|
|
|
|
|
// Checking if values need to be transformed before conversion.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
let decodeArr = this.dict.getArray("Decode", "D");
|
2017-10-26 20:00:09 +09:00
|
|
|
if (this.forceRGB && Array.isArray(decodeArr)) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
let bitsPerComponent = this.dict.get("BitsPerComponent") || 8;
|
2017-10-26 20:15:57 +09:00
|
|
|
let decodeArrLength = decodeArr.length;
|
|
|
|
let transform = new Int32Array(decodeArrLength);
|
|
|
|
let transformNeeded = false;
|
|
|
|
let maxValue = (1 << bitsPerComponent) - 1;
|
|
|
|
for (let i = 0; i < decodeArrLength; i += 2) {
|
2017-10-26 20:00:09 +09:00
|
|
|
transform[i] = ((decodeArr[i + 1] - decodeArr[i]) * 256) | 0;
|
|
|
|
transform[i + 1] = (decodeArr[i] * maxValue) | 0;
|
|
|
|
if (transform[i] !== 256 || transform[i + 1] !== 0) {
|
|
|
|
transformNeeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (transformNeeded) {
|
2018-05-16 20:49:01 +09:00
|
|
|
jpegOptions.decodeTransform = transform;
|
2017-10-26 20:00:09 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fetching the 'ColorTransform' entry, if it exists.
|
|
|
|
if (isDict(this.params)) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
let colorTransform = this.params.get("ColorTransform");
|
2017-10-26 20:00:09 +09:00
|
|
|
if (Number.isInteger(colorTransform)) {
|
2018-05-16 20:49:01 +09:00
|
|
|
jpegOptions.colorTransform = colorTransform;
|
2017-10-26 20:00:09 +09:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 20:49:01 +09:00
|
|
|
const jpegImage = new JpegImage(jpegOptions);
|
2017-10-26 20:00:09 +09:00
|
|
|
|
|
|
|
jpegImage.parse(this.bytes);
|
2018-09-02 20:55:27 +09:00
|
|
|
let data = jpegImage.getData({
|
|
|
|
width: this.drawWidth,
|
|
|
|
height: this.drawHeight,
|
|
|
|
forceRGB: this.forceRGB,
|
|
|
|
isSourcePDF: true,
|
|
|
|
});
|
2017-10-26 20:00:09 +09:00
|
|
|
this.buffer = data;
|
|
|
|
this.bufferLength = data.length;
|
|
|
|
this.eof = true;
|
|
|
|
};
|
|
|
|
|
2017-10-26 20:15:57 +09:00
|
|
|
JpegStream.prototype.getIR = function(forceDataSchema = false) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return createObjectURL(this.bytes, "image/jpeg", forceDataSchema);
|
2017-10-26 20:00:09 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return JpegStream;
|
|
|
|
})();
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { JpegStream };
|