-
Notifications
You must be signed in to change notification settings - Fork 427
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
lodepng::decode - bug with Greyscale 16 bit #149
Comments
I just wrote a quick test and lodepng seems to be working correctly in this case. I suspect you might have a bug in how you create your buffer lodepng::decode(pixels, width, height, "noise.png", 16); I suspect this is what you meant: lodepng::decode(pixels, width, height, "noise.png", LCT_GREY, 16); Here is a test program I wrote that confirms the byte order between encoding and decoding is consistent. I hope this helps. It prints the output /*
issue_149.cpp - Don Cross <[email protected]>
*/
#include <cstdio>
#include <vector>
#include "lodepng.h"
unsigned func(unsigned x, unsigned y)
{
return (x * y) & 0xffff;
}
int main()
{
const unsigned width = 600;
const unsigned height = 400;
const char * const filename = "image.png";
// Generate a reference image in 'pixels'
std::vector<u_char> pixels;
for (unsigned y = 0; y < height; ++y)
{
for (unsigned x = 0; x < width; ++x)
{
unsigned p = func(x, y);
pixels.push_back(p & 0xff);
pixels.push_back((p >> 8) & 0xff);
}
}
// Encode to output PNG file.
lodepng::encode(filename, pixels, width, height, LCT_GREY, 16);
// Read the image back in to a different buffer 'check'.
std::vector<u_char> check;
unsigned check_width, check_height;
lodepng::decode(check, check_width, check_height, filename, LCT_GREY, 16);
// Compare 'check' against 'pixels'.
unsigned index = 0;
for (unsigned y = 0; y < height; ++y)
{
for (unsigned x = 0; x < width; ++x)
{
unsigned p = func(x, y);
unsigned q = check[index] | (check[index+1] << 8);
if (p != q)
{
printf("FAIL: p=0x%x, q=0x%x, x=%d, y=%d\n", p, q, x, y);
return 1;
}
index += 2;
}
}
printf("PASS\n");
return 0;
}
|
Thanks for the answer. Your test is also working correctly. Then, it turns out, the problem in the other - Photoshop PNG 16bit Greyscale uses the opposite order of bytes. |
I don't know much about the details of lodepng, but I did find the following comments inside lodepng.h. It looks like what you are encountering is an unfortunate consequence of using a big-endian file format on a little-endian computer. A similar unpleasant reality happens with network programming, where it is common to have to swap byte orders between network traffic and locally used multi-byte integers.
|
Hello.
I generate perlin noise and save him to PNG:
It works correctly, I look in Photoshop and see that everything is fine.
Then I load this fPNG:
And this is where the problem comes in. It looks like the lodepng::decode is confuses the high and low bytes.
If I manually change the high and low bytes like this:
Then the result is correct and everything works fine.
Is this a bug? If not, what am I doing wrong?
The text was updated successfully, but these errors were encountered: