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

16 bit depth images are saved using host endian #26

Open
ColonelThirtyTwo opened this issue May 12, 2018 · 0 comments
Open

16 bit depth images are saved using host endian #26

ColonelThirtyTwo opened this issue May 12, 2018 · 0 comments

Comments

@ColonelThirtyTwo
Copy link

ColonelThirtyTwo commented May 12, 2018

Test program that writes a 16-bit-depth RGBA image containing a gradient:

extern crate lodepng;
extern crate rgb;
use std::iter::repeat;
use rgb::RGBA;

fn main() {
	let height = 500;
	let row = (0..u16::max_value()).map(|i| RGBA{r: u16::max_value(), g: 0, b: 0, a: u16::max_value()-i}).collect::<Vec<_>>();
	let color_buffer: Vec<RGBA<u16>> = repeat(&row).take(height).flat_map(|row| row).map(|pixel| *pixel).collect::<Vec<_>>();
	let _ = lodepng::encode_file("test.png", color_buffer.as_slice(), u16::max_value() as usize, height, lodepng::ColorType::RGBA, 16);
}

This produces the following image on my x86-64 laptop, which is not a single solid gradient as expected:
several gradients
If I flip the endianness of the image, I get the correct result:

gm convert test.png -endian LSB -depth 16 RGBA:test.raw
gm convert -endian MSB -depth 16 -size 65535x500 RGBA:test.raw test-flipped.png

test-flipped

This is likely because encode_file doesn't actually use the type of the array its passed; it just converts it to a u8 array and feeds it straight into the encoder. While that seems like a good idea for making the encoder type agnostic, issues like this combined with the fact that rust doesn't guarentee that fields are stored in the order that they are specified make this a bad idea. Since you depend on the rgb crate anyway, you may as well take pixels using the rgb crate's structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant