2016-12-08 08:52:44 -05:00
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
2016-07-18 11:58:33 -04:00
# include "LandscapeFileFormatPng.h"
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
# include "Misc/FileHelper.h"
# include "Modules/ModuleManager.h"
2016-07-18 11:58:33 -04:00
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
# include "Interfaces/IImageWrapperModule.h"
# include "Containers/Algo/Transform.h"
2016-07-18 11:58:33 -04:00
# define LOCTEXT_NAMESPACE "LandscapeEditor.NewLandscape"
FLandscapeHeightmapFileFormat_Png : : FLandscapeHeightmapFileFormat_Png ( )
{
FileTypeInfo . Description = LOCTEXT ( " FileFormatPng_HeightmapDesc " , " Heightmap .png files " ) ;
FileTypeInfo . Extensions . Add ( " .png " ) ;
FileTypeInfo . bSupportsExport = true ;
}
FLandscapeHeightmapInfo FLandscapeHeightmapFileFormat_Png : : Validate ( const TCHAR * HeightmapFilename ) const
{
FLandscapeHeightmapInfo Result ;
TArray < uint8 > ImportData ;
if ( ! FFileHelper : : LoadFileToArray ( ImportData , HeightmapFilename , FILEREAD_Silent ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileReadError " , " Error reading heightmap file " ) ;
}
else
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
if ( ! ImageWrapper - > SetCompressed ( ImportData . GetData ( ) , ImportData . Num ( ) ) | | ImageWrapper - > GetWidth ( ) < = 0 | | ImageWrapper - > GetHeight ( ) < = 0 )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileCorruptPng " , " The heightmap file cannot be read (corrupt png?) " ) ;
}
else
{
if ( ImageWrapper - > GetFormat ( ) ! = ERGBFormat : : Gray )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileColorPng " , " The heightmap file appears to be a color png, grayscale is expected. The import *can* continue, but the result may not be what you expect... " ) ;
}
else if ( ImageWrapper - > GetBitDepth ( ) ! = 16 )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileLowBitDepth " , " The heightmap file appears to be an 8-bit png, 16-bit is preferred. The import *can* continue, but the result may be lower quality than desired. " ) ;
}
FLandscapeFileResolution ImportResolution ;
ImportResolution . Width = ImageWrapper - > GetWidth ( ) ;
ImportResolution . Height = ImageWrapper - > GetHeight ( ) ;
Result . PossibleResolutions . Add ( ImportResolution ) ;
}
}
// possible todo: support sCAL (XY scale) and pCAL (Z scale) png chunks for filling out Result.DataScale
// I don't know if any heightmap generation software uses these or not
// if we support their import we should make the exporter write them too
return Result ;
}
FLandscapeHeightmapImportData FLandscapeHeightmapFileFormat_Png : : Import ( const TCHAR * HeightmapFilename , FLandscapeFileResolution ExpectedResolution ) const
{
FLandscapeHeightmapImportData Result ;
TArray < uint8 > TempData ;
if ( ! FFileHelper : : LoadFileToArray ( TempData , HeightmapFilename , FILEREAD_Silent ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileReadError " , " Error reading heightmap file " ) ;
}
else
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
if ( ! ImageWrapper - > SetCompressed ( TempData . GetData ( ) , TempData . Num ( ) ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileCorruptPng " , " The heightmap file cannot be read (corrupt png?) " ) ;
}
else if ( ImageWrapper - > GetWidth ( ) ! = ExpectedResolution . Width | | ImageWrapper - > GetHeight ( ) ! = ExpectedResolution . Height )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapResolutionMismatch " , " The heightmap file's resolution does not match the requested resolution " ) ;
}
else
{
if ( ImageWrapper - > GetFormat ( ) ! = ERGBFormat : : Gray )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileColorPng " , " The heightmap file appears to be a color png, grayscale is expected. The import *can* continue, but the result may not be what you expect... " ) ;
}
else if ( ImageWrapper - > GetBitDepth ( ) ! = 16 )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileLowBitDepth " , " The heightmap file appears to be an 8-bit png, 16-bit is preferred. The import *can* continue, but the result may be lower quality than desired. " ) ;
}
const TArray < uint8 > * RawData = nullptr ;
if ( ImageWrapper - > GetBitDepth ( ) < = 8 )
{
if ( ! ImageWrapper - > GetRaw ( ERGBFormat : : Gray , 8 , RawData ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileCorruptPng " , " The heightmap file cannot be read (corrupt png?) " ) ;
}
else
{
Result . Data . Empty ( ExpectedResolution . Width * ExpectedResolution . Height ) ;
Algo : : Transform ( * RawData , Result . Data , [ ] ( uint8 Value ) { return Value * 0x101 ; } ) ; // Expand to 16-bit
}
}
else
{
if ( ! ImageWrapper - > GetRaw ( ERGBFormat : : Gray , 16 , RawData ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_HeightmapFileCorruptPng " , " The heightmap file cannot be read (corrupt png?) " ) ;
}
else
{
Result . Data . Empty ( ExpectedResolution . Width * ExpectedResolution . Height ) ;
Result . Data . AddUninitialized ( ExpectedResolution . Width * ExpectedResolution . Height ) ;
FMemory : : Memcpy ( Result . Data . GetData ( ) , RawData - > GetData ( ) , ExpectedResolution . Width * ExpectedResolution . Height * 2 ) ;
}
}
}
}
return Result ;
}
void FLandscapeHeightmapFileFormat_Png : : Export ( const TCHAR * HeightmapFilename , TArrayView < const uint16 > Data , FLandscapeFileResolution DataResolution , FVector Scale ) const
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
if ( ImageWrapper - > SetRaw ( Data . GetData ( ) , Data . Num ( ) * 2 , DataResolution . Width , DataResolution . Height , ERGBFormat : : Gray , 16 ) )
{
const TArray < uint8 > & TempData = ImageWrapper - > GetCompressed ( ) ;
FFileHelper : : SaveArrayToFile ( TempData , HeightmapFilename ) ;
}
}
//////////////////////////////////////////////////////////////////////////
FLandscapeWeightmapFileFormat_Png : : FLandscapeWeightmapFileFormat_Png ( )
{
FileTypeInfo . Description = LOCTEXT ( " FileFormatPng_WeightmapDesc " , " Layer .png files " ) ;
FileTypeInfo . Extensions . Add ( " .png " ) ;
FileTypeInfo . bSupportsExport = true ;
}
FLandscapeWeightmapInfo FLandscapeWeightmapFileFormat_Png : : Validate ( const TCHAR * WeightmapFilename , FName LayerName ) const
{
FLandscapeWeightmapInfo Result ;
TArray < uint8 > ImportData ;
if ( ! FFileHelper : : LoadFileToArray ( ImportData , WeightmapFilename , FILEREAD_Silent ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerFileReadError " , " Error reading layer file " ) ;
}
else
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
if ( ! ImageWrapper - > SetCompressed ( ImportData . GetData ( ) , ImportData . Num ( ) ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerCorruptPng " , " The layer file cannot be read (corrupt png?) " ) ;
}
else
{
if ( ImageWrapper - > GetFormat ( ) ! = ERGBFormat : : Gray )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerColorPng " , " The layer file appears to be a color png, grayscale is expected. The import *can* continue, but the result may not be what you expect... " ) ;
}
FLandscapeFileResolution ImportResolution ;
ImportResolution . Width = ImageWrapper - > GetWidth ( ) ;
ImportResolution . Height = ImageWrapper - > GetHeight ( ) ;
Result . PossibleResolutions . Add ( ImportResolution ) ;
}
}
return Result ;
}
FLandscapeWeightmapImportData FLandscapeWeightmapFileFormat_Png : : Import ( const TCHAR * WeightmapFilename , FName LayerName , FLandscapeFileResolution ExpectedResolution ) const
{
FLandscapeWeightmapImportData Result ;
TArray < uint8 > TempData ;
if ( ! FFileHelper : : LoadFileToArray ( TempData , WeightmapFilename , FILEREAD_Silent ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerFileReadError " , " Error reading layer file " ) ;
}
else
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
const TArray < uint8 > * RawData = nullptr ;
if ( ! ImageWrapper - > SetCompressed ( TempData . GetData ( ) , TempData . Num ( ) ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerCorruptPng " , " The layer file cannot be read (corrupt png?) " ) ;
}
else if ( ImageWrapper - > GetWidth ( ) ! = ExpectedResolution . Width | | ImageWrapper - > GetHeight ( ) ! = ExpectedResolution . Height )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerResolutionMismatch " , " The layer file's resolution does not match the requested resolution " ) ;
}
else if ( ! ImageWrapper - > GetRaw ( ERGBFormat : : Gray , 8 , RawData ) )
{
Result . ResultCode = ELandscapeImportResult : : Error ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerCorruptPng " , " The layer file cannot be read (corrupt png?) " ) ;
}
else
{
if ( ImageWrapper - > GetFormat ( ) ! = ERGBFormat : : Gray )
{
Result . ResultCode = ELandscapeImportResult : : Warning ;
Result . ErrorMessage = LOCTEXT ( " Import_LayerColorPng " , " The layer file appears to be a color png, grayscale is expected. The import *can* continue, but the result may not be what you expect... " ) ;
}
Result . Data = * RawData ; // agh I want to use MoveTemp() here
}
}
return Result ;
}
void FLandscapeWeightmapFileFormat_Png : : Export ( const TCHAR * WeightmapFilename , FName LayerName , TArrayView < const uint8 > Data , FLandscapeFileResolution DataResolution ) const
{
IImageWrapperModule & ImageWrapperModule = FModuleManager : : LoadModuleChecked < IImageWrapperModule > ( " ImageWrapper " ) ;
IImageWrapperPtr ImageWrapper = ImageWrapperModule . CreateImageWrapper ( EImageFormat : : PNG ) ;
if ( ImageWrapper - > SetRaw ( Data . GetData ( ) , Data . Num ( ) , DataResolution . Width , DataResolution . Height , ERGBFormat : : Gray , 8 ) )
{
const TArray < uint8 > & TempData = ImageWrapper - > GetCompressed ( ) ;
FFileHelper : : SaveArrayToFile ( TempData , WeightmapFilename ) ;
}
}
2016-07-19 06:59:15 -04:00
# undef LOCTEXT_NAMESPACE