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.

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

Source

Lines 185-290 in Source/Falcor/Utils/Image/Bitmap.cpp. Line 65 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)
    {
        logError("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)
        {
            genError("Image Type unknown", filename);
            return nullptr;
        }
    }
    // Check the the library supports loading this image Type
    if (FreeImage_FIFSupportsReading(fifFormat) == false)
    {
        genError("Library doesn't support the file format", filename);
        return nullptr;
    }
    // Read the DIB
    FIBITMAP* pDib = FreeImage_Load(fifFormat, fullpath.c_str());
    if (pDib == nullptr)
    {
        genError("Can't read image file", filename);
        return nullptr;
    }
    // Create the bitmap
    auto pBmp = new Bitmap;
    pBmp->mHeight = FreeImage_GetHeight(pDib);
    pBmp->mWidth = FreeImage_GetWidth(pDib);
    if (pBmp->mHeight == 0 || pBmp->mWidth == 0 || FreeImage_GetBits(pDib) == nullptr)
    {
        genError("Invalid image", filename);
        return nullptr;
    }
    uint32_t bpp = FreeImage_GetBPP(pDib);
    switch(bpp)
    {
    case 128:
        pBmp->mFormat = ResourceFormat::RGBA32Float;    // 4xfloat32 HDR format
        break;
    case 96:
        pBmp->mFormat = isRGB32fSupported() ? ResourceFormat::RGB32Float : ResourceFormat::RGBA32Float;     // 3xfloat32 HDR format
        break;
    case 64:
        pBmp->mFormat = ResourceFormat::RGBA16Float;    // 4xfloat16 HDR format
        break;
    case 48:
        pBmp->mFormat = ResourceFormat::RGB16Float;     // 3xfloat16 HDR format
        break;
    case 32:
        pBmp->mFormat = ResourceFormat::BGRA8Unorm;
        break;
    case 24:
        pBmp->mFormat = ResourceFormat::BGRX8Unorm;
        break;
    case 16:
        pBmp->mFormat = ResourceFormat::RG8Unorm;
        break;
    case 8:
        pBmp->mFormat = ResourceFormat::R8Unorm;
        break;
    default:
        genError("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;
    }
    uint32_t bytesPerPixel = bpp / 8;
    pBmp->mpData = new uint8_t[pBmp->mHeight * pBmp->mWidth * bytesPerPixel];
    FreeImage_ConvertToRawBits(pBmp->mpData, pDib, pBmp->mWidth * bytesPerPixel, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, isTopDown);
    FreeImage_Unload(pDib);
    return UniqueConstPtr(pBmp);
}





Add Discussion as Guest

Log in to DocsForge