libsdl-org / SDL_image

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

stb_image is failing to load grayscale pngs #294

Closed Jigoku closed 2 years ago

Jigoku commented 2 years ago

With SDL_image 2.6.0, not all png types are supported, as greyscale images fail to load with stb_image.

Example images that fail: 16levelstriping.png: PNG image data, 512 x 512, 4-bit grayscale, non-interlaced 16levelstriping

capturedfuzz.png: PNG image data, 512 x 512, 8-bit grayscale, non-interlaced capturedfuzz

A workaround solution is to add --disable-stb-image to the configure flags in SDL_image so that full png support is used via libpng, although this doesn't seem to be set as default in some distributions package builds, which is problematic.

lsalzman commented 2 years ago

This also fails with grayscale images with alpha... Example: https://sourceforge.net/p/sauerbraten/code/HEAD/tree/packages/fonts/default0.png

It appears that the failure is because IMG_stb.c only tries to parse STBI_rgb or STBI_rgb_alpha. But STBI_grey and STBI_grey_alpha are valid output from stb_image, and it would be nice if these were handled in the same way IMG_png does (i.e. grey outputs an 8bpp single component paletted surface, whereas gray-alpha outputs a 32bpp RGBA image). Otherwise, any existing SDL2_image users that previously relied on grayscale PNG support are now broken by default.

slouken commented 2 years ago

Fixed, thanks!

klaus-halfmann commented 2 years ago

Some other file that is failing https://github.com/widelands/widelands/blob/master/data/tribes/initialization/frisians/images/flag_0.5.png they where optimized with some tool named PNGQuant.

When will this fix make it into e.g macoOS Homebrew and in which version?

slouken commented 2 years ago

Some other file that is failing https://github.com/widelands/widelands/blob/master/data/tribes/initialization/frisians/images/flag_0.5.png they where optimized with some tool named PNGQuant.

This file is loading and displaying correctly here. What SDL_image error are you getting?

When will this fix make it into e.g macoOS Homebrew and in which version?

I'm not sure how macOS homebrew pulls updates. You can contact them and see what their policy is.

klaus-halfmann commented 2 years ago

@slouken I found this in https://github.com/widelands/widelands/issues/5479

I get "Error loading tribes/initialization/frisians/images/flag_0.5_pc.png: Unknown pixel format" Code looks like:

SDL_Surface* sdlsurf = IMG_Load_RW(SDL_RWFromMem(fr.data(0), fr.get_size()), 1);
if (sdlsurf == nullptr) {
    throw ImageLoadingError(fname, IMG_GetError());
}
return sdlsurf;

From: https://github.com/widelands/widelands/blob/master/src/graphic/image_io.cc#L75

I dont know much about SDL, though

lsalzman commented 2 years ago

https://github.com/widelands/widelands/blob/master/data/tribes/initialization/frisians/images/flag_0.5_pc.png Seems like that file is 1-bit grayscale. Maybe stb_image is not able to parse that?

slouken commented 2 years ago

Ah, okay, that was just fixed as part of this bug resolution. Thanks!

slouken commented 2 years ago

This bugfix has been released in the 2.6.1 update: https://github.com/libsdl-org/SDL_image/releases/tag/release-2.6.1

klaus-halfmann commented 2 years ago

Homebrew Upgraded to: sdl2_image--2.6.0_1 whixh fixed the Issue for me, thanks.