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

Pixel order error #12

Open
diegotibi opened this issue Jan 4, 2023 · 1 comment
Open

Pixel order error #12

diegotibi opened this issue Jan 4, 2023 · 1 comment

Comments

@diegotibi
Copy link

Hi @EliotJones,
I've found a problem with your lib trying to decode this file:

test

I was using a simple loop to read pixel by pixel the file but something in the output wasn't correct so I tried to simply copy the image into a new one, with this code:

var stream = File.OpenRead("test.png");
var image = Png.Open(stream);
var streamOut = File.Create("testOut.png");
var builder = PngBuilder.FromPng(image);
builder.Save(streamOut);
streamOut.Close();

The output image is this:

testout

The input file seems to be ok (you can inspect it here)
With other files (created using the same process) this problem doesn't shows up.

Any thoughts?

Thanks

@plaurin
Copy link

plaurin commented Apr 9, 2024

I've found a bug in RawPngData.cs that will result in the skewing effect seen in the output image above. In my case I had a 4bits per pixel image of width 287, an odd number. On line 48 RawPngData.cs we can see this

var bytesInRow = (1 + (width / pixelsPerByte));

Because pixelsPerByte is 2 (we can fit 2 pixels data of 4bits each in a 8bits byte) we get 1 + 143.5 but because we are working with int the .5 gets trunked. So it is bytesInRow = 1 + 143 = 144 which is obviously wrong because an image of width 286 would give us 1 + (286 / 2) = 144 too!

This means all image with an odd width and less than 16 colors will have this bug.

My dirty solution was to round up the division like this:

var bytesInRow = (int)(1 + Math.Ceiling((double)width / pixelsPerByte));

I hope this could help.

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

2 participants