Freeimage FreeImage_ConvertTo24Bits returns null pointer

188 views Asked by At

I think that in previous versions FreeImage_ConvertTo24Bits(FIBITMAP*) work ok in any type of FIBITMAP* but in 3.18 it returns nullptr if the FIBITMAP* is a floating point texture.

Is there anything I missed? Anyone has noticed this change too? Should I make a intermediate transform?

Thank you in advance for any tip.

1

There are 1 answers

0
Frank Escobar On

I didn't manage to make it work so I coded my own version I'll put this here for future readers:

inline BYTE LinToSRGB(float C)
{
    C = std::fmin( std::fmax(C, 0.0f), 1.0f);
    if (C > 0.0031308f) {
        C = 1.055f * (pow(C, (1.0f / 2.4f))) - 0.055f;
    } else {
        C = 12.92f * C;
    }
    return static_cast<BYTE>(C*255.0f);
}

FIBITMAP* ConvertTo24Bits(FIBITMAP* origImage)
{
    FIBITMAP* modImage = FreeImage_ConvertTo24Bits(origImage);
    if(modImage!=nullptr)
    {
        return modImage;
    }

    const FREE_IMAGE_TYPE src_type = FreeImage_GetImageType(origImage);
    const unsigned width = FreeImage_GetWidth(origImage);
    const unsigned height = FreeImage_GetHeight(origImage);

    modImage = FreeImage_Allocate(width, height,24);

    const unsigned src_pitch = FreeImage_GetPitch(origImage);
    const unsigned dst_pitch = FreeImage_GetPitch(modImage);

    const BYTE *src_bits = (BYTE*)FreeImage_GetBits(origImage);
    BYTE *dst_bits = (BYTE*)FreeImage_GetBits(modImage);


    if(src_type == FIT_RGBAF)
    {
        for(unsigned y = 0; y < height; y++) {
            const FIRGBAF *src_pixel = (FIRGBAF*) src_bits;

            for(unsigned x = 0; x < width; x++) {
                // convert and skip alpha channel
                *dst_bits = LinToSRGB(src_pixel[x].blue);
                dst_bits++;
                *dst_bits = LinToSRGB(src_pixel[x].green);
                dst_bits++;
                *dst_bits = LinToSRGB(src_pixel[x].red);
                dst_bits++;
            }
            src_bits += src_pitch;
        }
    }
    else if(src_type == FIT_RGBF)
    {
        for(unsigned y = 0; y < height; y++) {
            const FIRGBF *src_pixel = (FIRGBF*) src_bits;
            for(unsigned x = 0; x < width; x++) {
                // convert and skip alpha channel
                *dst_bits = LinToSRGB(src_pixel[x].blue);
                dst_bits++;
                *dst_bits = LinToSRGB(src_pixel[x].green);
                dst_bits++;
                *dst_bits = LinToSRGB(src_pixel[x].red);
                dst_bits++;
            }
            src_bits += src_pitch;
        }
    }

    return modImage;
}