mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 17:48:07 +02:00
Make the getBytes
/peekBytes
methods of Stream
/DecodeStream
/ChunkedStream
able to return Uint8ClampedArray
s
The built-in image decoders are already returning data as `Uint8ClampedArray`, and subsequently the JPEG/JBIG2/JPX streams are as well. However, for general streams we obviously don't want to force the use of `Uint8ClampedArray` unless an "Image" is actually being decoded. Hence this patch, which adds a parameter that allows the caller of the `getBytes`/`peekBytes` methods to force a `Uint8ClampedArray` (rather than a `Uint8Array`) to be returned.
This commit is contained in:
parent
2030d1718f
commit
32367c5968
3 changed files with 32 additions and 18 deletions
|
@ -56,30 +56,33 @@ var Stream = (function StreamClosure() {
|
|||
var b3 = this.getByte();
|
||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||
},
|
||||
// returns subarray of original buffer
|
||||
// should only be read
|
||||
getBytes: function Stream_getBytes(length) {
|
||||
// Returns subarray of original buffer, should only be read.
|
||||
getBytes(length, forceClamped = false) {
|
||||
var bytes = this.bytes;
|
||||
var pos = this.pos;
|
||||
var strEnd = this.end;
|
||||
|
||||
if (!length) {
|
||||
return bytes.subarray(pos, strEnd);
|
||||
let subarray = bytes.subarray(pos, strEnd);
|
||||
// `this.bytes` is always a `Uint8Array` here.
|
||||
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
|
||||
}
|
||||
var end = pos + length;
|
||||
if (end > strEnd) {
|
||||
end = strEnd;
|
||||
}
|
||||
this.pos = end;
|
||||
return bytes.subarray(pos, end);
|
||||
let subarray = bytes.subarray(pos, end);
|
||||
// `this.bytes` is always a `Uint8Array` here.
|
||||
return (forceClamped ? new Uint8ClampedArray(subarray) : subarray);
|
||||
},
|
||||
peekByte: function Stream_peekByte() {
|
||||
var peekedByte = this.getByte();
|
||||
this.pos--;
|
||||
return peekedByte;
|
||||
},
|
||||
peekBytes: function Stream_peekBytes(length) {
|
||||
var bytes = this.getBytes(length);
|
||||
peekBytes(length, forceClamped = false) {
|
||||
var bytes = this.getBytes(length, forceClamped);
|
||||
this.pos -= bytes.length;
|
||||
return bytes;
|
||||
},
|
||||
|
@ -181,7 +184,7 @@ var DecodeStream = (function DecodeStreamClosure() {
|
|||
var b3 = this.getByte();
|
||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||
},
|
||||
getBytes: function DecodeStream_getBytes(length) {
|
||||
getBytes(length, forceClamped = false) {
|
||||
var end, pos = this.pos;
|
||||
|
||||
if (length) {
|
||||
|
@ -203,15 +206,18 @@ var DecodeStream = (function DecodeStreamClosure() {
|
|||
}
|
||||
|
||||
this.pos = end;
|
||||
return this.buffer.subarray(pos, end);
|
||||
let subarray = this.buffer.subarray(pos, end);
|
||||
// `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
|
||||
return (forceClamped && !(subarray instanceof Uint8ClampedArray) ?
|
||||
new Uint8ClampedArray(subarray) : subarray);
|
||||
},
|
||||
peekByte: function DecodeStream_peekByte() {
|
||||
var peekedByte = this.getByte();
|
||||
this.pos--;
|
||||
return peekedByte;
|
||||
},
|
||||
peekBytes: function DecodeStream_peekBytes(length) {
|
||||
var bytes = this.getBytes(length);
|
||||
peekBytes(length, forceClamped = false) {
|
||||
var bytes = this.getBytes(length, forceClamped);
|
||||
this.pos -= bytes.length;
|
||||
return bytes;
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue