UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片

本文主要是介绍UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一,写带通道的RGBA图片(BMP)

1,写BMP图片的代码:

2,获取图片buffer,并写成代码

二,使用UE提供的接口写png图片

1, FTextureResource写成图片

2,UTextureRenderTarget2D写成图片

一,写带通道的RGBA图片(BMP)

1,写BMP图片的代码:

#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include <windows.h>
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"inline LONG BmpPitchCB(const BITMAPINFOHEADER* pFormat)
{return ((pFormat->biWidth * (pFormat->biBitCount >> 3) + 3) >> 2) << 2;
}inline HRESULT SaveBmp(LPCTSTR			pFileName,const void* pBuf,LONG			lBuf_W,LONG			lBuf_H,LONG			lBuf_Bit,BOOL			bAlign)
{if (pBuf == NULL){return E_INVALIDARG;}BITMAPFILEHEADER bfh;BITMAPINFOHEADER bih;memset(&bih, 0, sizeof(bih));bih.biSize = sizeof(bih);bih.biPlanes = 1;bih.biBitCount = (WORD)lBuf_Bit;bih.biWidth = lBuf_W;bih.biHeight = lBuf_H;bih.biSizeImage = BmpPitchCB(&bih) * abs(bih.biHeight);memset(&bfh, 0, sizeof(bfh));bfh.bfType = ((WORD)('M' << 8) | 'B');bfh.bfOffBits = 54;bfh.bfSize = 54 + bih.biSizeImage;// Correct the param// if (bAlign == false){if (bih.biSizeImage == (DWORD)bih.biWidth * bih.biBitCount * abs(bih.biHeight) / 8){bAlign = true;}}//HANDLE hFile = NULL;do{hFile = CreateFile(pFileName,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if (hFile == NULL || hFile == INVALID_HANDLE_VALUE){hFile = NULL;break;}DWORD dwWrited = 0;if (WriteFile(hFile, &bfh, sizeof(bfh), &dwWrited, NULL) == false){break;}if (WriteFile(hFile, &bih, sizeof(bih), &dwWrited, NULL) == false){break;}if (bAlign){if (WriteFile(hFile, pBuf, bih.biSizeImage, &dwWrited, NULL) == false){break;}}else{// bitmap format pitch// ...................................xxx for 32 bit aligned//const BYTE* pRow = static_cast<const BYTE*>(pBuf);const LONG   nRow = bih.biSizeImage / abs(bih.biHeight);LONG n = 0;for (n = abs(bih.biHeight); n > 1; n--){if (!WriteFile(hFile, pRow, nRow, &dwWrited, NULL)){break;}pRow += bih.biWidth * bih.biBitCount / 8;}if (n != 1){break;}if (!WriteFile(hFile, pRow, bih.biWidth * bih.biBitCount / 8, &dwWrited, NULL)){break;}LONG nPlus = nRow - bih.biWidth * bih.biBitCount / 8;if (nPlus > 0){if (!WriteFile(hFile, pRow, nPlus, &dwWrited, NULL)){break;}}}CloseHandle(hFile);return S_OK;} while (false);if (hFile){CloseHandle(hFile);}return E_FAIL;
}

2,获取图片buffer,并写成代码

UTextureRenderTarget2D* MediaRenderTarget; 				
FTextureResource* DstTextureRes = MediaRenderTarget->GetResource();
FTextureRHIRef DstTextureRef = DstTextureRes->TextureRHI;
FRHITexture* DstTexture = DstTextureRef->GetTexture2D();
//FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();
FIntRect RectTarget(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());
TArray<FColor> DataTarget;
ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImageTarget)([Texture = DstTexture, RectTarget, Data = &DataTarget](FRHICommandListImmediate& RHICmdList) mutable{RHICmdList.ReadSurfaceData(Texture, RectTarget, *Data, FReadSurfaceDataFlags(RCM_UNorm));});if (DataTarget.Num() == RectTarget.Area()){uint8* tempP = new uint8[RectTarget.Width() * RectTarget.Height() * 4];for (int32 i = 0; i < RectTarget.Width() * RectTarget.Height(); i++){tempP[i * 4] = DataTarget[i].B;tempP[i * 4 + 1] = DataTarget[i].G;tempP[i * 4 + 2] = DataTarget[i].R;tempP[i * 4 + 3] = DataTarget[i].A;}SaveBmp(TEXT("D://RHIUpdateTextureReference.bmp"), tempP, RectTarget.Width(), RectTarget.Height(), 32, true);}

二,使用UE提供的接口写png图片

1, FTextureResource写成图片

FTextureResource* rhiSource;	
FRHITexture* DstTexture = rhiSource->GetTexture2DRHI();
//FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();
FIntRect Rect(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());
FString FilePath = TEXT("D://rhiSource.png");
TArray<FColor> Data;
ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImage)([Texture = DstTexture, Rect, Data = &Data](FRHICommandListImmediate& RHICmdList) mutable
{RHICmdList.ReadSurfaceData(Texture, Rect, *Data, FReadSurfaceDataFlags(RCM_UNorm));});
if (Data.Num() == Rect.Area())
{TArray<uint8> Bitmap;FImageUtils::ThumbnailCompressImageArray(Rect.Width(), Rect.Height(), Data, Bitmap);FFileHelper::SaveArrayToFile(Bitmap, *FilePath);
}

2,UTextureRenderTarget2D写成图片

	UTextureRenderTarget2D* CapturingRenderTarget = MediaRenderTarget;if (CapturingRenderTarget){FRHITexture2D* DstTexture = CapturingRenderTarget->GetResource()->TextureRHI->GetTexture2D();FIntRect Rect(0, 0, DstTexture->GetSizeX(), DstTexture->GetSizeY());TArray<FColor> Data;ENQUEUE_RENDER_COMMAND(UPXMediaInputComponent_SaveToImage)([Texture = DstTexture, Rect, Data = &Data](FRHICommandListImmediate& RHICmdList) mutable{RHICmdList.ReadSurfaceData(Texture, Rect, *Data, FReadSurfaceDataFlags(RCM_UNorm));});FlushRenderingCommands();if (Data.Num() == Rect.Area()){TArray<uint8> Bitmap;FImageUtils::ThumbnailCompressImageArray(Rect.Width(), Rect.Height(), Data, Bitmap);FFileHelper::SaveArrayToFile(Bitmap, *FilePath);}}

这篇关于UE 将UTextureRenderTarget2D,FTextureResource,FTextureRHIRef,FRHITexture写成图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979868

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图