Add support for TypedArray input with byteOffset and length

This commit is contained in:
codepage949 2022-03-28 04:18:58 +09:00 committed by GitHub
parent 1d36936954
commit c3a0d5f5d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -39,7 +39,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
if (input.length === 0) { if (input.length === 0) {
throw Error('Input Bit Array is empty'); throw Error('Input Bit Array is empty');
} }
inputDescriptor.buffer = Buffer.from(input.buffer); inputDescriptor.buffer = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
} else if (is.plainObject(input) && !is.defined(inputOptions)) { } else if (is.plainObject(input) && !is.defined(inputOptions)) {
// Plain Object descriptor, e.g. create // Plain Object descriptor, e.g. create
inputOptions = input; inputOptions = input;

View File

@ -182,6 +182,24 @@ describe('Input/output', function () {
assert.strictEqual(info.height, 1); assert.strictEqual(info.height, 1);
}); });
it('Read from Uint8ClampedArray with byteOffset and output to Buffer', async () => {
// since a Uint8ClampedArray is the same as Uint8Array but clamps the values
// between 0-255 it seemed good to add this also
const uint8array = Uint8ClampedArray.from([0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 255, 255]);
const uint8ArrayWithByteOffset = new Uint8ClampedArray(uint8array.buffer, 3, 6);
const { data, info } = await sharp(uint8ArrayWithByteOffset, {
raw: {
width: 2,
height: 1,
channels: 3
}
}).toBuffer({ resolveWithObject: true });
assert.deepStrictEqual(Uint8ClampedArray.from([255, 255, 255, 0, 0, 0]), new Uint8ClampedArray(data));
assert.strictEqual(info.width, 2);
assert.strictEqual(info.height, 1);
});
it('Stream should emit info event', function (done) { it('Stream should emit info event', function (done) {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);