Function createFromFile

Synopsis

#include <Source/Falcor/Utils/Image/Bitmap.h>

static UniqueConstPtr createFromFile(const std::string &filename, bool isTopDown)

Description

Create a new object from file.

Parameters

[ in ] filename - Filename, including a path. If the file can't be found relative to the current directory, Falcor will search for it in the common directories.

[ in ] isTopDown - Control the memory layout of the image. If true, the top-left pixel is the first pixel in the buffer, otherwise the bottom-left pixel is first.

Returns
If loading was successful, a new object. Otherwise, nullptr.

Source

Lines 191-308 in Source/Falcor/Utils/Image/Bitmap.cpp. Line 76 in Source/Falcor/Utils/Image/Bitmap.h.

Bitmap::UniqueConstPtr Bitmap::createFromFile(const std::string& filename, bool isTopDown)
{
    std::string fullpath;
    if (findFileInDataDirectories(filename, fullpath) == false)
    {
        logWarning("Error when loading image file. Can't find image file '" + filename + "'");
        return nullptr;
    }
    FREE_IMAGE_FORMAT fifFormat = FIF_UNKNOWN;
    fifFormat = FreeImage_GetFileType(fullpath.c_str(), 0);
    if (fifFormat == FIF_UNKNOWN)
    {
        // Can't get the format from the file. Use file extension
        fifFormat = FreeImage_GetFIFFromFilename(fullpath.c_str());
        if (fifFormat == FIF_UNKNOWN)
        {
            genWarning("Image type unknown", filename);
            return nullptr;
        }
    }
    // Check the library supports loading this image type
    if (FreeImage_FIFSupportsReading(fifFormat) == false)
    {
        genWarning("Library doesn't support the file format", filename);
        return nullptr;
    }
    // Read the DIB
    FIBITMAP* pDib = FreeImage_Load(fifFormat, fullpath.c_str());
    if (pDib == nullptr)
    {
        genWarning("Can't read image file", filename);
        return nullptr;
    }
    // Create the bitmap
    const uint32_t height = FreeImage_GetHeight(pDib);
    const uint32_t width = FreeImage_GetWidth(pDib);
    if (height == 0 || width == 0 || FreeImage_GetBits(pDib) == nullptr)
    {
        genWarning("Invalid image", filename);
        return nullptr;
    }
    // Convert palettized images to RGBA.
    FREE_IMAGE_COLOR_TYPE colorType = FreeImage_GetColorType(pDib);
    if (colorType == FIC_PALETTE)
    {
        auto pNew = FreeImage_ConvertTo32Bits(pDib);
        FreeImage_Unload(pDib);
        pDib = pNew;
        if (pDib == nullptr)
        {
            genWarning("Failed to convert palettized image to RGBA format", filename);
            return nullptr;
        }
    }
    ResourceFormat format = ResourceFormat::Unknown;
    uint32_t bpp = FreeImage_GetBPP(pDib);
    switch(bpp)
    {
    case 128:
        format = ResourceFormat::RGBA32Float;    // 4xfloat32 HDR format
        break;
    case 96:
        format = isRGB32fSupported() ? ResourceFormat::RGB32Float : ResourceFormat::RGBA32Float;     // 3xfloat32 HDR format
        break;
    case 64:
        format = ResourceFormat::RGBA16Float;    // 4xfloat16 HDR format
        break;
    case 48:
        format = ResourceFormat::RGB16Float;     // 3xfloat16 HDR format
        break;
    case 32:
        format = ResourceFormat::BGRA8Unorm;
        break;
    case 24:
        format = ResourceFormat::BGRX8Unorm;
        break;
    case 16:
        format = ResourceFormat::RG8Unorm;
        break;
    case 8:
        format = ResourceFormat::R8Unorm;
        break;
    default:
        genWarning("Unknown bits-per-pixel", filename);
        return nullptr;
    }
    // Convert the image to RGBX image
    if (bpp == 24)
    {
        bpp = 32;
        auto pNew = FreeImage_ConvertTo32Bits(pDib);
        FreeImage_Unload(pDib);
        pDib = pNew;
    }
    else if (bpp == 96 && (isRGB32fSupported() == false))
    {
        bpp = 128;
        auto pNew = convertToRGBAF(pDib);
        FreeImage_Unload(pDib);
        pDib = pNew;
    }
    UniqueConstPtr pBmp = UniqueConstPtr(new Bitmap(width, height, format));
    FreeImage_ConvertToRawBits(pBmp->getData(), pDib, pBmp->getRowPitch(), bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, isTopDown);
    FreeImage_Unload(pDib);
    return pBmp;
}





Add Discussion as Guest

Log in