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
@ -181,16 +181,17 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||||
},
|
},
|
||||||
|
|
||||||
// returns subarray of original buffer
|
// Returns subarray of original buffer, should only be read.
|
||||||
// should only be read
|
getBytes(length, forceClamped = false) {
|
||||||
getBytes: function ChunkedStream_getBytes(length) {
|
|
||||||
var bytes = this.bytes;
|
var bytes = this.bytes;
|
||||||
var pos = this.pos;
|
var pos = this.pos;
|
||||||
var strEnd = this.end;
|
var strEnd = this.end;
|
||||||
|
|
||||||
if (!length) {
|
if (!length) {
|
||||||
this.ensureRange(pos, strEnd);
|
this.ensureRange(pos, strEnd);
|
||||||
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;
|
var end = pos + length;
|
||||||
@ -200,7 +201,9 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
this.ensureRange(pos, end);
|
this.ensureRange(pos, end);
|
||||||
|
|
||||||
this.pos = end;
|
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 ChunkedStream_peekByte() {
|
peekByte: function ChunkedStream_peekByte() {
|
||||||
@ -209,8 +212,8 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
return peekedByte;
|
return peekedByte;
|
||||||
},
|
},
|
||||||
|
|
||||||
peekBytes: function ChunkedStream_peekBytes(length) {
|
peekBytes(length, forceClamped = false) {
|
||||||
var bytes = this.getBytes(length);
|
var bytes = this.getBytes(length, forceClamped);
|
||||||
this.pos -= bytes.length;
|
this.pos -= bytes.length;
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
},
|
||||||
|
@ -56,30 +56,33 @@ var Stream = (function StreamClosure() {
|
|||||||
var b3 = this.getByte();
|
var b3 = this.getByte();
|
||||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||||
},
|
},
|
||||||
// returns subarray of original buffer
|
// Returns subarray of original buffer, should only be read.
|
||||||
// should only be read
|
getBytes(length, forceClamped = false) {
|
||||||
getBytes: function Stream_getBytes(length) {
|
|
||||||
var bytes = this.bytes;
|
var bytes = this.bytes;
|
||||||
var pos = this.pos;
|
var pos = this.pos;
|
||||||
var strEnd = this.end;
|
var strEnd = this.end;
|
||||||
|
|
||||||
if (!length) {
|
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;
|
var end = pos + length;
|
||||||
if (end > strEnd) {
|
if (end > strEnd) {
|
||||||
end = strEnd;
|
end = strEnd;
|
||||||
}
|
}
|
||||||
this.pos = end;
|
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() {
|
peekByte: function Stream_peekByte() {
|
||||||
var peekedByte = this.getByte();
|
var peekedByte = this.getByte();
|
||||||
this.pos--;
|
this.pos--;
|
||||||
return peekedByte;
|
return peekedByte;
|
||||||
},
|
},
|
||||||
peekBytes: function Stream_peekBytes(length) {
|
peekBytes(length, forceClamped = false) {
|
||||||
var bytes = this.getBytes(length);
|
var bytes = this.getBytes(length, forceClamped);
|
||||||
this.pos -= bytes.length;
|
this.pos -= bytes.length;
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
},
|
||||||
@ -181,7 +184,7 @@ var DecodeStream = (function DecodeStreamClosure() {
|
|||||||
var b3 = this.getByte();
|
var b3 = this.getByte();
|
||||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||||
},
|
},
|
||||||
getBytes: function DecodeStream_getBytes(length) {
|
getBytes(length, forceClamped = false) {
|
||||||
var end, pos = this.pos;
|
var end, pos = this.pos;
|
||||||
|
|
||||||
if (length) {
|
if (length) {
|
||||||
@ -203,15 +206,18 @@ var DecodeStream = (function DecodeStreamClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.pos = end;
|
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() {
|
peekByte: function DecodeStream_peekByte() {
|
||||||
var peekedByte = this.getByte();
|
var peekedByte = this.getByte();
|
||||||
this.pos--;
|
this.pos--;
|
||||||
return peekedByte;
|
return peekedByte;
|
||||||
},
|
},
|
||||||
peekBytes: function DecodeStream_peekBytes(length) {
|
peekBytes(length, forceClamped = false) {
|
||||||
var bytes = this.getBytes(length);
|
var bytes = this.getBytes(length, forceClamped);
|
||||||
this.pos -= bytes.length;
|
this.pos -= bytes.length;
|
||||||
return bytes;
|
return bytes;
|
||||||
},
|
},
|
||||||
|
@ -59,6 +59,11 @@ describe('stream', function() {
|
|||||||
expect(result).toMatchTypedArray(
|
expect(result).toMatchTypedArray(
|
||||||
new Uint8Array([100, 3, 101, 2, 102, 1])
|
new Uint8Array([100, 3, 101, 2, 102, 1])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
predictor.reset();
|
||||||
|
let clampedResult = predictor.getBytes(6, /* forceClamped = */ true);
|
||||||
|
expect(clampedResult).toEqual(
|
||||||
|
new Uint8ClampedArray([100, 3, 101, 2, 102, 1]));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user