Hello ,
I was trying to implement a way for the user to upload images from their drive to a local folder in the contents folder.
I’m attempting to do by using 3 functions from your library:
UVictoryBPFunctionLibrary::Victory_LoadTexture2D_FromFile to
UVictoryBPFunctionLibrary::Victory_GetPixelFromT2D to
UVictoryBPFunctionLibrary::Victory_SavePixels
Here it is in Blueprint:
And here are the code snippets:
My implementation of UVictoryBPFunctionLibrary::Victory_LoadTexture2D_FromFile:
UTexture2D * ULSI_FilePickerLibrary::LSI_LoadTextureFromFilePixels(const FString & FullFilePath, EJoyImageFormats ImageFormat, bool & IsValid, int32 & Width, int32 & Height, TArray<FLinearColor>& OutPixels)
{
OutPixels.Empty();
IsValid = false;
UTexture2D* LoadedTexture = nullptr;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(GetImageFormat(ImageFormat));
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath))
{
return nullptr;
}
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
const TArray<uint8>* UncompressedBGRA = nullptr;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedTexture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
if (!LoadedTexture)
{
return nullptr;
}
Width = ImageWrapper->GetWidth();
Height = ImageWrapper->GetHeight();
void* TextureData = LoadedTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
LoadedTexture->PlatformData->Mips[0].BulkData.Unlock();
LoadedTexture->UpdateResource();
}
}
IsValid = true;
return LoadedTexture;
}
My implementation of UVictoryBPFunctionLibrary::Victory_GetPixelFromT2D:
bool ULSI_FilePickerLibrary::LSI_GetPixelsFromText2D(UTexture2D * T2D, int32 & TextWidth, int32 & TextHeight, TArray<FLinearColor>& PixelArray)
{
if (!T2D)
{
return false;
}
PixelArray.Empty();
T2D->SRGB = false;
T2D->CompressionSettings = TC_VectorDisplacementmap;
T2D->UpdateResource();
FTexture2DMipMap& MyMipMap = T2D->PlatformData->Mips[0];
TextWidth = MyMipMap.SizeX;
TextHeight = MyMipMap.SizeY;
FByteBulkData* RawImageData = &MyMipMap.BulkData;
if (!RawImageData)
{
return false;
}
FColor* RawColorArray = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));
for (int32 x = 0; x < TextWidth; x++)
{
for (int32 y = 0; y < TextHeight; y++)
{
PixelArray.Add(RawColorArray[x* TextWidth + y]);
}
}
RawImageData->Unlock();
return true;
}
My implementation of UVictoryBPFunctionLibrary::Victory_SavePixels:
bool ULSI_FilePickerLibrary::LSI_SavePNGtoFile(const FString & FullFilePath, int32 Width, int32 Height, const TArray<FLinearColor>& ImagePixels, bool sRGB, FString & ErrorString)
{
if (FullFilePath.Len() < 1)
{
ErrorString = "No File Path";
return false;
}
FString NewAbsoluteFolderPath = FPaths::GetPath(FullFilePath);
FPaths::NormalizeDirectoryName(NewAbsoluteFolderPath);
if (!CreateDirectory(NewAbsoluteFolderPath))
{
ErrorString = "Folder could not be created, cheack read/write permissions-: " + NewAbsoluteFolderPath;
return false;
}
TArray<FColor> ColorArray;
for (const FLinearColor& Each : ImagePixels)
{
ColorArray.Add(Each.ToFColor(sRGB));
}
if (ColorArray.Num() != Width * Height)
{
ErrorString = "Error: height x width is not equal to the total pixel array length.";
return false;
}
FString FinalFileName = FPaths::GetBaseFilename(FullFilePath, false);
FinalFileName += ".png";
TArray<uint8> CompressedPNG;
FImageUtils::CompressImageArray(Width, Height, ColorArray, CompressedPNG);
return FFileHelper::SaveArrayToFile(CompressedPNG, *FinalFileName);
}
However after testing these, of my tests say nothing is going wrong, it makes it through the blueprint without error, and through the code as well, and everything appears to be working correctly, but there is no new .png file created in the directory. Upon further inspection of your source code I found that you have a comment that says that at some point these functions did not work for unknown reasons. Are they still not working or have you fixed that? If so, what have I wrong?
credit for implementation will go to you of course.