-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sharp converts 16bit pngs to 8bit #3809
Comments
Did you see the https://sharp.pixelplumbing.com/api-output#raw If you require - const rawOutput = await sharp(buffer).raw().toColorspace("grey16").toBuffer();
+ const rawOutput = await sharp(buffer).raw({ depth: "ushort" }).toColorspace("grey16").toBuffer(); |
Just tried that (here) with no luck. It still seems to convert the pixels to 8bit range. |
I also have a similar issue. In my experiment, I created an image of 256x256 pixels with grayscale values ranging from 0 to 65535. However, when I read the generated image again, the values I read are different from what I input. I don't know where the problem is. I suspect that even though I used a Uint16Array to construct the Sharp object, the image is still being stored in an 8-bit format in terms of data storage. I've conducted numerous tests and found that this issue exists. const createStd16bit = async () => {
const height = 256;
const width = 256;
const uint16rawArray = [];
for (let i = 0; i < 65536; i++) {
uint16rawArray.push(i)
if (i % 256 === 0) {
console.log(`put data ${i / 256}:${i}`)
}
}
await sharp(Uint16Array.from(uint16rawArray), {
raw: {
width: width,
height: height,
channels: 1
}
})
.png({progressive: false, force: true})
.withMetadata({icc: 'p3'})
.toFile('__local/16bit_std.png');
}
const assertStd16bit = async () => {
const path = '__local/16bit_std.png'
const target = await sharp(path);
const metadata = await target.metadata();
const {data, info} = await target
.toColorspace('grey16')
.raw({depth: 'ushort'})
.toBuffer({resolveWithObject: true});
console.log(info)
console.log(`data.length ::: ${data.length}`)
const {width, height} = info;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (x === 0 || x === 1) {
const pixelIndex = (y * width + x) * info.channels * 2;
const actual = data.readUint16BE(pixelIndex);
const expect = pixelIndex / 2;
console.log(`position x,y == (${x},${y}) == expect ${expect} actual == ${actual} diff == ${Math.abs(actual - expect)}`)
}
}
}
} the logs is below Details
position x,y == (0,0) == expect 0 actual == 0 diff == 0 When I use
|
@jacobdong Are you referring to #3808 ? I'd prefer to track these as one issue if so. |
@lovell |
@Siyer2 To build a - return new Uint16Array(rawOutput);
+ return new Uint16Array(rawOutput.buffer, 0, rawOutput.length / 2); |
Possible bug
Is this a possible bug in a feature of sharp, unrelated to installation?
npm install sharp
completes without error.node -e "require('sharp')"
completes without error.If you cannot confirm both of these, please open an installation issue instead.
Are you using the latest version of sharp?
sharp
as reported bynpm view sharp dist-tags.latest
.If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.
If you are using another package which depends on a version of
sharp
that is not the latest, please open an issue against that package instead.What is the output of running
npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp
?What are the steps to reproduce?
I have reproduced the issue in this minimal repo
What is the expected behaviour?
For a 256x256 image, I expect there to be an array of size 65536 where each pixel is between 0 and 65535. While I get the correct array size, the pixel values all seem to be 8bit range not 16bit range. I have included a similar operation with imageJs to indicate what I expect.
Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem
Minimal repo here
Please provide sample image(s) that help explain this problem
Image is in repo
The text was updated successfully, but these errors were encountered: