Skip to content
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

extractChannel does not preserve 16-bit image data intact. #3453

Closed
issuefiler opened this issue Nov 12, 2022 · 2 comments
Closed

extractChannel does not preserve 16-bit image data intact. #3453

issuefiler opened this issue Nov 12, 2022 · 2 comments

Comments

@issuefiler
Copy link

Version

Sharp 0.31.2.

Environment

  • System
    • Windows 10.0.19044
    • x64 Intel CPU
  • Binaries:
    • Node 18.2.0
    • NPM 9.1.1

Input

A horizontal 4-pixel 8-bit RGBA PNG image of a gradation from black to white, from left to right.
test

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.

@lovell
Copy link
Owner

lovell commented Nov 15, 2022

Thanks for reporting, this should be addressed by commit 01ffa80, which also improves some of the test expectations and error messaging relating to this feature.

@lovell
Copy link
Owner

lovell commented Dec 21, 2022

v0.31.3 now available, thanks again for reporting.

@lovell lovell closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants