lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.06k stars 421 forks source link

wrong color to decode this file #83

Open victor-fang opened 5 years ago

victor-fang commented 5 years ago

setting_64px_26276_easyicon net

wrong color to decode this file, please check it.

lvandeve commented 5 years ago

Hi,

I tried out decoding it with the "showpng" example (examples/example_sdl.cpp), and it looks correct to me.

The image has an alpha channel though, a translucent background. Which part has the wrong color when you view it, the gear, or the background?

Thanks!

missinglucy commented 5 years ago

https://blog.littlevgl.com/2018-10-05/png_converter see the image, red is converted to blue, blue is converted to read

lvandeve commented 5 years ago

Which color should the gear have? For me it's blue with gray

missinglucy commented 5 years ago

I read from https://lodev.org/lodepng/, found the words like below: So I change my code fork from 'https://blog.littlevgl.com/2018-10-05/png_converter'. change R to B, B to R, Then I display color right to my pc now.

    //c = LV_COLOR_MAKE(img_argb[i].red, img_argb[i].green, img_argb[i].blue);
    c = LV_COLOR_MAKE(img_argb[i].blue, img_argb[i].green, img_argb[i].red);

Q: The image is wrong when using LodePNG with BITMAPs in Visual Studio or win32, or with BMP images.

A: LodePNG uses raw buffers with RGBA or RGB pixels in the common order RGBARGBARGBA... or RGBRGBRGBRGB..., from top to bottom, without any special byte boundaries. BMP images and bitmaps in win32 use a different format. They do three things differently:

Instead of being from top to bottom, it goes from bottom to top (upside down).
Instead of using the order red, green, blue, it uses the order blue, green, red, or BGRBGRBGR..., (can result in wrong colors).
It has a limitation where rows always must be a multiple of 4 bytes, so if the width of the image is not a multiple of 4 some unused bytes are introduced at the end of each row (can result in a skewed image).

All of this are related to how BMP works, not how PNG or LodePNG work. When you're working with BMP, you need to take each of these three things into account and convert this to/from the buffer format that LodePNG uses. Also check out the png2bmp and bmp2png samples, which already do this.


lvandeve commented 5 years ago

Indeed! Thanks for pointing to BITMAP.

Indeed those examples show how to convert between BMP and PNG, direct links to them:

https://github.com/lvandeve/lodepng/blob/master/examples/example_png2bmp.cpp https://github.com/lvandeve/lodepng/blob/master/examples/example_bmp2png.cpp