text: // Fill out your copyright notice in the Description page of Project Settings.
  
  
  #include "LoadImage.h"
  #include"IImageWrapperModule.h"
  #include "IImageWrapper.h"
  
  
  bool ULoadImage::LoadImageToTexture2D(const FString& ImagePath, UTexture2D*& InTexture, float& Width, float& Height)
  {
      IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
      if (!PlatformFile.FileExists(*ImagePath))
      {
          return false;
      }
  
      TArray<uint8> ImageResultData;
      FFileHelper::LoadFileToArray(ImageResultData, *ImagePath);
      FString Ex = FPaths::GetExtension(ImagePath, false);
      EImageFormat ImageFormat = EImageFormat::Invalid;
  
      if (Ex.Equals(TEXT("jpg"), ESearchCase::IgnoreCase) || Ex.Equals(TEXT("jpeg"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::JPEG;
      }
      else if (Ex.Equals(TEXT("png"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::PNG;
      }
      else if (Ex.Equals(TEXT("bmp"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::BMP;
      }
      else if (Ex.Equals(TEXT("exr"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::EXR;
      }
      if (ImageFormat == EImageFormat::Invalid)
      {
          return false;
      }
  
      IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>("ImageWrapper");
      TSharedPtr<IImageWrapper> ImageWrapperPtr = ImageWrapperModule.CreateImageWrapper(ImageFormat);
      if (ImageWrapperPtr.IsValid() && ImageWrapperPtr->SetCompressed(ImageResultData.GetData(), ImageResultData.GetAllocatedSize()))
      {
          TArray<uint8>OutRawData;
          ImageWrapperPtr->GetRaw(ERGBFormat::BGRA, 8, OutRawData);
          Width = ImageWrapperPtr->GetWidth();
          Height = ImageWrapperPtr->GetHeight();
          InTexture = UTexture2D::CreateTransient(Width, Height,PF_B8G8R8A8);
          if (InTexture)
          {
              void* TextureData = InTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
              FMemory::Memcpy(TextureData, OutRawData.GetData(), OutRawData.Num());
              InTexture->PlatformData->Mips[0].BulkData.Unlock();
              InTexture->UpdateResource();
              return true;
          }
      }
      return false;
  }
  
  bool ULoadImage::LoadImgToTex2D(const FString& ImagePath, UTexture2D*& InTexture, float& Width, float& Height)
  
  {
      IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
      if (!PlatformFile.FileExists(*ImagePath))
      {
          return false;
      }
  
      TArray<uint8> ImageResultData;
      FFileHelper::LoadFileToArray(ImageResultData, *ImagePath);
      FString Ex = FPaths::GetExtension(ImagePath, false);
      EImageFormat ImageFormat = EImageFormat::Invalid;
      EPixelFormat PixelFormat = EPixelFormat::PF_Unknown;
  
      if (Ex.Equals(TEXT("jpg"), ESearchCase::IgnoreCase) || Ex.Equals(TEXT("jpeg"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::JPEG;
          PixelFormat = EPixelFormat::PF_FloatRGB;
      }
      else if (Ex.Equals(TEXT("png"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::PNG;
          PixelFormat = EPixelFormat::PF_B8G8R8A8;
      }
      else if (Ex.Equals(TEXT("bmp"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::BMP;
          PixelFormat = EPixelFormat::PF_B8G8R8A8;
      }
      else if (Ex.Equals(TEXT("exr"), ESearchCase::IgnoreCase))
      {
          ImageFormat = EImageFormat::EXR;
          PixelFormat = EPixelFormat::PF_B8G8R8A8;
      }
      if (ImageFormat == EImageFormat::Invalid)
      {
          return false;
      }
      if (PixelFormat == EPixelFormat::PF_Unknown)
      {
          return false;
      }
  
      IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>("ImageWrapper");
      TSharedPtr<IImageWrapper> ImageWrapperPtr = ImageWrapperModule.CreateImageWrapper(ImageFormat);
      if (ImageWrapperPtr.IsValid() && ImageWrapperPtr->SetCompressed(ImageResultData.GetData(), ImageResultData.GetAllocatedSize()))
      {
          TArray<uint8>OutRawData;
        
  
          ImageWrapperPtr->GetRaw(ERGBFormat::BGRA, 8, OutRawData);
          Width = ImageWrapperPtr->GetWidth();
          Height = ImageWrapperPtr->GetHeight();
          InTexture = UTexture2D::CreateTransient(Width, Height, PixelFormat);
          if (InTexture)
          {
              void* TextureData = InTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
              FMemory::Memcpy(TextureData, OutRawData.GetData(), OutRawData.Num());
              InTexture->PlatformData->Mips[0].BulkData.Unlock();
              InTexture->UpdateResource();
              return true;
          }
      }
      return false;
  }
  
  