Version
Sharp 0.31.2.
Environment
- System
- Windows 10.0.19044
- x64 Intel CPU
- Binaries:
Input
A horizontal 4-pixel 8-bit RGBA PNG image of a gradation from black to white, from left to right.

Bug
const {data: image_data, info: image_info} = await Sharp("test.png")
.flatten({background: "#FFFFFF"})
.toColorspace("grey16")
//.extractChannel(0)
.raw({depth: "ushort"})
.toBuffer({resolveWithObject: true});
const pixel_values = Float64Array
.from(new Uint16Array(image_data.buffer), value => value / 0xFFFF);
console.log(image_data.length, image_data, pixel_values, image_info);
8 <Buffer 00 00 7f 7f c3 c3 ff ff> Float64Array(4) [ 0, 0.4980392156862745, 0.7647058823529411, 1 ] {
format: 'raw',
width: 4,
height: 1,
channels: 1,
depth: 'ushort',
premultiplied: false,
size: 8
}
This works fine.
const {data: image_data, info: image_info} = await Sharp("test.png")
.flatten({background: "#FFFFFF"})
.toColorspace("grey16")
.extractChannel(0) // Uncommented
.raw({depth: "ushort"})
.toBuffer({resolveWithObject: true});
const pixel_values = Float64Array
.from(new Uint16Array(image_data.buffer), value => value / 0xFFFF);
console.log(image_data.length, image_data, pixel_values, image_info);
8 <Buffer 00 00 7f 00 c3 00 ff 00> Float64Array(4) [
0,
0.0019378957808804456,
0.002975509269855802,
0.0038910505836575876
] {
format: 'raw',
width: 4,
height: 1,
channels: 1,
depth: 'ushort',
premultiplied: false,
size: 8
}
If you add .extractChannel(0), the significant bytes are cut off.
The expected behavior
Adding .extractChannel(0) should be doing nothing here.
Version
Sharp 0.31.2.
Environment
Input
A horizontal 4-pixel 8-bit RGBA PNG image of a gradation from black to white, from left to right.

Bug
This works fine.
If you add
.extractChannel(0), the significant bytes are cut off.The expected behavior
Adding
.extractChannel(0)should be doing nothing here.