libsdl-org / SDL_image

Image decoding for many popular formats for Simple Directmedia Layer.
zlib License
572 stars 186 forks source link

SDL_image + libpng load some PNGs incorrectly #460

Closed Starbuck5 closed 3 months ago

Starbuck5 commented 3 months ago

Hello, I'm one of the maintainers of pygame-ce. One of our users reported a png file loading incorrectly in their program.

The image: 2_img_broken

image It should look like the left, but instead loads as the right.

This only seems to be an issue when SDL_image is built with libpng (as we do for distribution with pygame-ce). I tested on an SDL_image prebuilt for Windows (built with stb-image instead), and didn't see this bad output.

I looked into how this might be solved in the libpng code, but the libpng code is extremely difficult to understand. Checking the libpng docs I see that they have a "simplified API" for reading and writing, so I spent 2 days hacking on it and have a draft of using the libpng simple API instead of the older API in SDL2_image: https://github.com/pygame-community/SDL_image/blob/a89d6d9b7069e851bdfeecb15e8cc0de6b4542c6/src/IMG_png.c#L233-L367. That branch is still WIP right now, but wanted to float the idea of potentially moving over to that strategy for libpng. It seems a lot simpler than the other libpng code or even the stb-image code. (And it fixes this issue too)

pygame-ce issue: https://github.com/pygame-community/pygame-ce/issues/3036

slouken commented 3 months ago

Yes, this looks good to me!

slouken commented 3 months ago

Also, I fixed the underlying problem in IMG_png.c

Starbuck5 commented 3 months ago

Thanks @slouken!

Gave it a run through the pygame-ce test suite and on that image, all worked as expected.

slouken commented 3 months ago

You're welcome!