Skip to content

Commit

Permalink
split inversion to separate function
Browse files Browse the repository at this point in the history
-we don't invert textures by default now
-you can invert by adding the inversion operation later
  • Loading branch information
getnamo committed May 20, 2017
1 parent c83eb1e commit 72f5751
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
25 changes: 19 additions & 6 deletions Source/TensorFlow/Private/TensorFlowBlueprintLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "TensorFlowBlueprintLibrary.h"


TArray<float> UTensorFlowBlueprintLibrary::Conv_Texture2DToFloatArray(UTexture2D* InTexture)
TArray<float> UTensorFlowBlueprintLibrary::Conv_GreyScaleTexture2DToFloatArray(UTexture2D* InTexture)
{
TArray<float> FloatArray;
FloatArray.SetNum(InTexture->GetSizeX()* InTexture->GetSizeY());
Expand All @@ -17,7 +17,7 @@ TArray<float> UTensorFlowBlueprintLibrary::Conv_Texture2DToFloatArray(UTexture2D
{
int MipPointer = i * 4;
float GreyscaleValue = (MipData[MipPointer] + MipData[MipPointer + 1] + MipData[MipPointer + 2]) / 3.f;
FloatArray[i] = 1 - (GreyscaleValue / 255.f); //inverse and normalize it
FloatArray[i] = GreyscaleValue / 255.f; //normalize it
}

// Unlock the texture
Expand All @@ -27,6 +27,19 @@ TArray<float> UTensorFlowBlueprintLibrary::Conv_Texture2DToFloatArray(UTexture2D
return FloatArray;
}

TArray<float> UTensorFlowBlueprintLibrary::InvertFloatArray(const TArray<float>& InFloatArray)
{
TArray<float> InvertedArray;
InvertedArray.SetNum(InFloatArray.Num());

for (int i=0;i<InFloatArray.Num();i++)
{
InvertedArray[i] = 1 - InFloatArray[i];
}

return InvertedArray;
}

UTexture2D* UTensorFlowBlueprintLibrary::Conv_FloatArrayToTexture2D(const TArray<float>& InFloatArray)
{
//Create square image and lock for writing
Expand All @@ -43,10 +56,10 @@ UTexture2D* UTensorFlowBlueprintLibrary::Conv_FloatArrayToTexture2D(const TArray
for (int i = 0; i < InFloatArray.Num(); i++)
{
int MipPointer = i * 4;
int InverseValue = (1 - InFloatArray[i]) * 255.f;
MipData[MipPointer] = InverseValue;
MipData[MipPointer + 1] = InverseValue;
MipData[MipPointer + 2] = InverseValue;
int GreyValue = InFloatArray[i] * 255.f;
MipData[MipPointer] = GreyValue;
MipData[MipPointer + 1] = GreyValue;
MipData[MipPointer + 2] = GreyValue;
MipData[MipPointer + 3] = 255; //Alpha
}

Expand Down
6 changes: 5 additions & 1 deletion Source/TensorFlow/Public/TensorFlowBlueprintLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class TENSORFLOW_API UTensorFlowBlueprintLibrary : public UBlueprintFunctionLibr

/** Convert a UTexture2D to float array, assuming a square texture*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "ToFloatArray", BlueprintAutocast), Category = "Utilities|TensorFlow")
static TArray<float> Conv_Texture2DToFloatArray(UTexture2D* InTexture);
static TArray<float> Conv_GreyScaleTexture2DToFloatArray(UTexture2D* InTexture);

/** Invert values in a given float array (1->0, 0->1) on a 0-1 scale. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "InvertFloatArray"), Category = "Utilities|TensorFlow")
static TArray<float> InvertFloatArray(const TArray<float>& InFloatArray);

/** Convert a float array to a UTexture2D, assuming square array */
UFUNCTION(BlueprintPure, meta = (DisplayName = "ToTexture2D", BlueprintAutocast), Category = "Utilities|TensorFlow")
Expand Down

0 comments on commit 72f5751

Please sign in to comment.