dogo/oslib

Indexed PNG images (256 color) not loading palette

Closed this issue · 5 comments

When you load an indexed PNG (256 color palette) the loaded image is black, because all palette colors are set to black.

Maybe this is a libpng related issue, because 16 color images are sometimes loaded with wrong colors (if the palette is not exactly 16 colors big and they don't have an alpha channel).

Commenting line 102 in oslLoadImageFilePng.c, seems to solve the issue, as long as the images have exactly 16 or 256 color palettes, or they have an alpha channel.

Line 102 in oslLoadImageFilePng.c:
png_set_PLTE(pPngStruct, pPngInfo, palette, num_palette);

dogo commented

Thanks for reporting, @mills32. Could you please try out the code below and let me know if it works?

// If there is need for a palette ...
if (osl_pixelWidth[pixelFormat] <= 8) {
    img->palette = oslCreatePalette(oslMin(num_palette, 1 << osl_paletteSizes[pixelFormat]), OSL_PF_8888);
    if (img->palette) {
        // Make sure to not use too many colors!
        if (num_palette <= (1 << osl_paletteSizes[pixelFormat])) {
            // Suggestion: consider num_trans?
            for (i = 0; i < num_palette; i++) {
                r = palette[i].red;
                g = palette[i].green;
                b = palette[i].blue;
                a = 0xff;
                // Color key?
                if (osl_colorKeyEnabled && RGBA(palette[i].red, palette[i].green, palette[i].blue, 0) == (osl_colorKeyValue & 0x00ffffff))
                    a = 0;
                ((u32*)img->palette->data)[i] = RGBA(r, g, b, a);
            }
            oslUncachePalette(img->palette);
        } else {
            // Handle the case where num_palette is larger than expected
            // This might involve logging an error or adjusting the logic
        }
    }
}

@

That worked fine, thaks!.

I noticed the palette is not uploaded to VRAM, (I don't know if that is intended, or not). Maybe big images could be slower if gpu has to read the palette from ram when drawing (I also don't know ?.)

Thanks again!.

dogo commented

Hi @mills32, can I ask you a favor and test the branch (refactor/oslLoadImageFilePNG) from PR #17 to see if it works as well? I've made several improvements.

dogo commented

Everything seems fine

Sorry I could not test earlier, everything seems to be perfect. thanks!