Skip to content

Latest commit

 

History

History
138 lines (133 loc) · 3.06 KB

Image&Texture.md

File metadata and controls

138 lines (133 loc) · 3.06 KB

Image&Texture

Load Data from File

Check File Exist

#include "PlatformFilemanager.h"
FString InFileName;
FPlatformFileManager::Get().GetPlatformFile().FileExists(*InFileName)

Load Data to TArray from File

#include "FileHelper.h"
TArray<uint8> InData;
FFileHelper::LoadFileToArray(InData, *InFileName)

Parse Format

Module Manager

#include "ModuleManager.h"

Load IImageWrapperModule

#include "ImageWrapper/Public/IImageWrapperModule.h"
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));

IImageWrapper

#include "ImageWrapper/Public/IImageWrapper.h"
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

Data

ImageWrapper->SetCompressed(InData.GetData(),LoadData.Num())
ImageWrapper->GetRaw(ERGBFormat::RGBA,8,Data)
int32 Width, Height;
Height = ImageWrapper->GetHeight();
Width = ImageWrapper->GetWidth();

Save

ImageWrapper->SetRaw(Data->GetData(),Data->Num(),Width,Height,ERGBFormat::RGBA,8)
TArray<uint8> InData;
OutData = ImageWrapper->GetCompressed()
#include "FileManagerGeneric.h"
FFileManagerGeneric::Get().DirectoryExists(*OutFile)
#include "Paths.h"
FFileManagerGeneric::Get().MakeDirectory(*FPaths::GetPath(OutFile), true)
FFileHelper::SaveArrayToFile(OutData, *OutFile)
TArray<FColor> uint8ToFColor(const TArray<uint8> origin)
{
	TArray<FColor> uncompressedFColor;
	FColor auxDst;
	for (int i = 0; i < origin.Num(); i++) 
	{
		auxDst.R = origin[i];
		i++;
		auxDst.G = origin[i];
		i++;
		auxDst.B = origin[i];
		i++;
		auxDst.A = origin[i];
		uncompressedFColor.Add(auxDst);
	}
	return uncompressedFColor;
}
bool PNG2JPG(const FString PNGFile, const FString JPGFile)
{
	check(PNGFile.EndsWith(TEXT(".png")) && JPGFile.EndsWith(TEXT(".jpg")));
	TArray<uint8> PNGData;
	if (!CheckFileAndLoadData(PNGFile, PNGData)) { return false; }
	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	TSharedPtr<IImageWrapper> PNGWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
	if (PNGWrapper&&PNGWrapper->SetCompressed(PNGData.GetData(),PNGData.Num()))
	{
		const TArray<uint8>* Data = nullptr;
		if (PNGWrapper->GetRaw(ERGBFormat::RGBA, 8, Data))
		{
			TSharedPtr<IImageWrapper> JPGWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
			if (JPGWrapper->SetRaw(
				Data->GetData(),
				Data->Num(),
				PNGWrapper->GetWidth(),
				PNGWrapper->GetHeight(),
				ERGBFormat::RGBA,
				8
			))
			{
				if (!FFileManagerGeneric::Get().DirectoryExists(*JPGFile))
				{
					FFileManagerGeneric::Get().MakeDirectory(*FPaths::GetPath(JPGFile), true);
				}
				TArray<uint8> JPGData;
				JPGData = JPGWrapper->GetCompressed();
				return FFileHelper::SaveArrayToFile(JPGData, *JPGFile);
			}
		}
	}
	return false;
}