Skip to content

Commit

Permalink
Merge pull request #4 from vvvv/master
Browse files Browse the repository at this point in the history
Error-handling for loading and support for non-4channel images
  • Loading branch information
torinos-yt authored Sep 23, 2024
2 parents ccae16e + 888fa1a commit 5813fc4
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 128 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Cargo.lock
# VL Assembly
VL*.dll
!VL.OpenEXR.dll
!VL.OpenEXR.Native.dll

# stride assembly
Stride*.dll
Binary file modified lib/net8.0/VL.OpenEXR.dll
Binary file not shown.
Binary file not shown.
Binary file modified runtimes/win-x64/native/VL.OpenEXR.Native.dll
100644 → 100755
Binary file not shown.
48 changes: 31 additions & 17 deletions src/VL.OpenEXR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ enum ExrPixelFormat
U32 = 0,
F16 = 1,
F32 = 2,
RGBF32 = 3
}

public enum ExrEncoding {
Expand All @@ -27,13 +26,19 @@ public static class ExrLoader
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]

[DllImport("VL.OpenEXR.Native.dll")]
static extern IntPtr load_from_path(string path, out int width, out int height, out ExrPixelFormat format);
static extern Int32 load_from_path(string path, out int width, out int height, out int num_channels, out ExrPixelFormat format, out IntPtr data);

public static Texture LoadFromPath(string path, GraphicsDevice device)
{
ExrPixelFormat exrFormat;
PixelFormat format;
IntPtr ptr = load_from_path(path, out var width, out var height, out exrFormat);
IntPtr ptr;
var result = load_from_path(path, out var width, out var height, out var numChannels, out exrFormat, out ptr);

if(result != 0) {
format = PixelFormat.None;
return null;
}

if(exrFormat == ExrPixelFormat.Unknown || ptr == IntPtr.Zero)
{
Expand All @@ -42,17 +47,26 @@ public static Texture LoadFromPath(string path, GraphicsDevice device)
}

int sizeInBytes = 0;
bool hasAlpha = true;
(format, sizeInBytes, hasAlpha) = exrFormat switch
(format, sizeInBytes) = (exrFormat, numChannels) switch
{
ExrPixelFormat.F16 => (PixelFormat.R16G16B16A16_Float, 2, true),
ExrPixelFormat.F32 => (PixelFormat.R32G32B32A32_Float, 4, true),
ExrPixelFormat.U32 => (PixelFormat.R32G32B32A32_UInt , 4, true),
ExrPixelFormat.RGBF32 => (PixelFormat.R32G32B32_Float, 4, false),
_ => (PixelFormat.None, 0, false),
(ExrPixelFormat.F16, 4) => (PixelFormat.R16G16B16A16_Float, 2),
(ExrPixelFormat.F32, 4) => (PixelFormat.R32G32B32A32_Float, 4),
(ExrPixelFormat.U32, 4) => (PixelFormat.R32G32B32A32_UInt , 4),

(ExrPixelFormat.F32, 3) => (PixelFormat.R32G32B32_Float, 4),
(ExrPixelFormat.U32, 3) => (PixelFormat.R32G32B32_UInt , 4),

(ExrPixelFormat.F16, 2) => (PixelFormat.R16G16_Float, 2),
(ExrPixelFormat.F32, 2) => (PixelFormat.R32G32_Float, 4),
(ExrPixelFormat.U32, 2) => (PixelFormat.R32G32_UInt , 4),

(ExrPixelFormat.F16, 1) => (PixelFormat.R16_Float, 2),
(ExrPixelFormat.F32, 1) => (PixelFormat.R32_Float, 4),
(ExrPixelFormat.U32, 1) => (PixelFormat.R32_UInt , 4),
_ => (PixelFormat.None, 0),
};

var rowPitch = width * (hasAlpha ? 4 : 3) * sizeInBytes;
var rowPitch = width * numChannels * sizeInBytes;

var texture = Texture.New(
device,
Expand All @@ -73,9 +87,9 @@ public static unsafe class ExrWriter
[DllImport("VL.OpenEXR.Native.dll")]
static extern int write_texture(string path, int width, int height, ExrPixelFormat format, ExrEncoding encoding, IntPtr data);

public static int WriteTexture(byte[] data, string path, int width, int height, PixelFormat format, ExrEncoding encoding)
{
return WriteTexture((ReadOnlySpan<byte>)data, path, width, height, format, encoding);
public static int WriteTexture(byte[] data, string path, int width, int height, PixelFormat format, ExrEncoding encoding)
{
return WriteTexture((ReadOnlySpan<byte>)data, path, width, height, format, encoding);
}

public static int WriteTexture(ReadOnlySpan<byte> data, string path, int width, int height, PixelFormat format, ExrEncoding encoding)
Expand All @@ -90,9 +104,9 @@ public static int WriteTexture(ReadOnlySpan<byte> data, string path, int width,

if(exrFormat == ExrPixelFormat.Unknown) return 1; //return with error

fixed (byte* pointer = data)
{
return write_texture(path, width, height, exrFormat, encoding, new IntPtr(pointer));
fixed (byte* pointer = data)
{
return write_texture(path, width, height, exrFormat, encoding, new IntPtr(pointer));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ echo "\033[32mbuild native plugins\033[m"
cd `dirname $0`
cd native
cargo build --target x86_64-pc-windows-gnu --release
cp target/x86_64-pc-windows-gnu/release/vl_openexr_native.dll ../../lib/native/VL.OpenEXR.Native.dll
cp target/x86_64-pc-windows-gnu/release/vl_openexr_native.dll ../../runtimes/win-x64/native/VL.OpenEXR.Native.dll
echo ""

echo "\033[32mbuild managed plugins\033[m"
cd ../
dotnet.exe build -c release -o ../lib/netstandard2.0
dotnet.exe build -c release
13 changes: 0 additions & 13 deletions src/native/.cargo/config.toml

This file was deleted.

1 change: 1 addition & 0 deletions src/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ name = "vl_openexr_native"
crate-type = ["cdylib"]

[dependencies]
anyhow = { version = "1.0.86", features = ["backtrace"] }
exr = "1.72.0"
radiant = "0.3.0"

Expand Down
Loading

0 comments on commit 5813fc4

Please sign in to comment.