0

In a c++ class in UnrealEngine, I have a runtime generated PNG image that I need to store in a buffer, to convert to a texture later.

What I miss is the step from my PNG in-memory image to a TArray buffer (ima_buf in the code below).

texture = FImageUtils::ImportBufferAsTexture2D(ima_buf);

1 Answer 1

0

Note: This code is based heavily on code from Rama, well-known Unreal Forums' user and engine contributor

If you have your image as a buffer, and the buffer is in a TArray<uint8>, here is the function. In the .h:

#include "ImageUtils.h"
#include "IImageWrapper.h"
#include "IImageWrapperModule.h"

// in some class USomeClassOfYours (replace with needed name)
    static UTexture2D* LoadTexture2D_FromBuffer(const TArray<uint8>& RawFileData, EImageFormat ImageFormat, bool& IsValid, int32& Width, int32& Height);

And in the .cpp

UTexture2D* USomeClassOfYours::LoadTexture2D_FromBuffer(const TArray<uint8>& RawFileData, EImageFormat ImageFormat, bool& IsValid, int32& Width, int32& Height)
{
    IsValid = false;
    UTexture2D* LoadedT2D = nullptr;
    
    IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
    TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat);
 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
    //Create T2D!
    if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
    { 
        TArray<uint8> UncompressedBGRA;
        if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
        {
            LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
            
            //Valid?
            if(!LoadedT2D) return nullptr;
            //~~~~~~~~~~~~~~
            
            //Out!
            Width = ImageWrapper->GetWidth();
            Height = ImageWrapper->GetHeight();
             
            //Copy!
            void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
            FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
            LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();

            //Update!
            LoadedT2D->UpdateResource();
        }
    }
     
    // Success!
    IsValid = true;
    return LoadedT2D;
}

If you need to import from file, add a second function that will use the functoin from above. Here is .h:

    /** Load a Texture2D from a JPG,PNG,BMP,ICO,EXR,ICNS file! IsValid tells you if file path was valid or not. Enjoy! -Rama */
    static UTexture2D* LoadTexture2D_FromFile(const FString& FullFilePath, EImageFormat ImageFormat, bool& IsValid, int32& Width, int32& Height);

and .cpp

UTexture2D* USomeClassOfYours::LoadTexture2D_FromFile(const FString& FullFilePath, EImageFormat ImageFormat, bool& IsValid, int32& Width, int32& Height)
{ 
    //Load From File
    TArray<uint8> RawFileData;
    if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) return nullptr;
    
    return LoadTexture2D_FromBuffer(RawFileData, ImageFormat, IsValid, Width, Height);
}

For a .png image, you should use EImageFormat::PNG as the format input variable of course.

8
  • I have an image in memory, I could save and the reload it (to follow your code), but I'd prefer to avoid the I/O. So, my main concern is: how can I have a TArray<uint8> from an in-memory png image? Mar 6, 2023 at 15:19
  • Sorry, I don't understand. You don't need to save it if you already have it in memory. In what way is it stored in memory now? I assumed it's stored as TArray<uint8> because it's the most direct Unreal-native byte array type.
    – goose_lake
    Mar 7, 2023 at 19:28
  • In the above function, ImageWrapper->SetCompressed(X, Y)) is where you pass the buffer of the image. X is a pointer to the memory where it is, and Y is the number of bytes. You don't have to use TArray for this, it can be any memory whatsoever.
    – goose_lake
    Mar 7, 2023 at 19:30
  • I didn't explain it right: I have an in-memory image from pyplot, just want to trasform it in a buffer suitable for use in a code like: texture = FImageUtils::ImportBufferAsTexture2D(ima_buf); Mar 9, 2023 at 15:29
  • Can you please describe the type of the ima_buf variable? I am not familiar with pyplot and its internals. If you provide more info, I will edit the answer to fit your exact problem, but without knowing anything about the type, I can't do that.
    – goose_lake
    Mar 10, 2023 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.