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

Fix the interface of JpegStream/JpxStream/Jbig2Stream to agree with the other DecodeStreams

The interface of all of the "image" streams look kind of weird, and I'm actually a bit surprised that there hasn't been any errors because of it.
For example: None of them actually implement `readBlock` methods, and it seems more luck that anything else that we're not calling `getBytes()` (without providing a length) for those streams, since that would trigger a code-path in `getBytes` that assumes `readBlock` to exist.

To address this long-standing issue, the `ensureBuffer` methods are thus renamed to `readBlock`. Furthermore, the new `ensureBuffer` methods are now no-ops.
Finally, this patch also replaces `var` with `let` in a number of places.
This commit is contained in:
Jonas Jenwald 2017-10-26 13:15:57 +02:00
parent 36593d6bbc
commit de5297b9ea
3 changed files with 68 additions and 56 deletions

View file

@ -22,7 +22,7 @@ import { shadow } from '../shared/util';
* For JBIG2's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams.
*/
var Jbig2Stream = (function Jbig2StreamClosure() {
let Jbig2Stream = (function Jbig2StreamClosure() {
function Jbig2Stream(stream, maybeLength, dict, params) {
this.stream = stream;
this.maybeLength = maybeLength;
@ -36,36 +36,39 @@ var Jbig2Stream = (function Jbig2StreamClosure() {
Object.defineProperty(Jbig2Stream.prototype, 'bytes', {
get() {
// If this.maybeLength is null, we'll get the entire stream.
// 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(req) {
if (this.bufferLength) {
Jbig2Stream.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`.
};
Jbig2Stream.prototype.readBlock = function() {
if (this.eof) {
return;
}
let jbig2Image = new Jbig2Image();
var jbig2Image = new Jbig2Image();
var chunks = [];
let chunks = [];
if (isDict(this.params)) {
var globalsStream = this.params.get('JBIG2Globals');
let globalsStream = this.params.get('JBIG2Globals');
if (isStream(globalsStream)) {
var globals = globalsStream.getBytes();
let globals = globalsStream.getBytes();
chunks.push({ data: globals, start: 0, end: globals.length, });
}
}
chunks.push({ data: this.bytes, start: 0, end: this.bytes.length, });
var data = jbig2Image.parseChunks(chunks);
var dataLength = data.length;
let data = jbig2Image.parseChunks(chunks);
let dataLength = data.length;
// JBIG2 had black as 1 and white as 0, inverting the colors
for (var i = 0; i < dataLength; i++) {
for (let i = 0; i < dataLength; i++) {
data[i] ^= 0xFF;
}
this.buffer = data;
this.bufferLength = dataLength;
this.eof = true;