Move StbImage to its own assembly.

This commit is contained in:
H. Utku Maden 2025-11-14 22:31:54 +03:00
parent 9ee6c1180d
commit 66c5eecc26
7 changed files with 143 additions and 19 deletions

View File

@ -0,0 +1,61 @@
using Dashboard.Pal;
namespace Dashboard.Drawing
{
public record ImageData(
TextureType Type,
PixelFormat Format,
int Width,
int Height,
byte[] Bitmap)
{
public int Depth { get; init; } = 1;
public int Levels { get; init; } = 1;
public bool Premultiplied { get; init; } = false;
public int Alignment { get; init; } = 4;
public long GetLevelOffset(int level)
{
ArgumentOutOfRangeException.ThrowIfLessThan(level, 0, nameof(level));
ArgumentOutOfRangeException.ThrowIfGreaterThan(level, Levels, nameof(level));
ArgumentOutOfRangeException.ThrowIfGreaterThan(level, Math.ILogB(Math.Max(Width, Height)));
long offset = 0;
long row = Width * Format switch
{
PixelFormat.R8I => 1,
PixelFormat.R16F => 2,
PixelFormat.Rg8I => 2,
PixelFormat.Rg16F => 4,
PixelFormat.Rgb8I => 3,
PixelFormat.Rgb16F => 6,
PixelFormat.Rgba8I => 4,
PixelFormat.Rgba16F => 8,
};
row += Alignment - (row % Alignment);
long plane = row * Height;
long volume = plane * Depth;
for (int i = 0; i < level; i++)
{
if (Depth == 1)
{
offset += plane / (1 << i) / (1 << i);
}
else
{
offset += volume / (1 << i) / (1 << i) / (1 << i);
}
}
return offset;
}
}
public interface IImageLoader : IApplicationExtension
{
public ImageData LoadImageData(Stream stream);
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Dashboard.Common\Dashboard.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ReFuel.StbImage" Version="2.1.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,47 @@
using Dashboard.Drawing;
using Dashboard.Pal;
using ReFuel.Stb;
namespace Dashboard.StbImage
{
public class StbImageLoader : IImageLoader
{
public string DriverName { get; } = "Dashboard Stb Image Loader";
public string DriverVendor { get; } = "Dashboard";
public Version DriverVersion { get; } = new Version(1, 0);
public void Dispose()
{
}
IContextBase IContextExtensionBase.Context => Context;
public void Require(Application context)
{
Context = context;
}
public ImageData LoadImageData(Stream stream)
{
using ReFuel.Stb.StbImage image = ReFuel.Stb.StbImage.Load(stream, StbiImageFormat.Rgba);
ReadOnlySpan<byte> data = image.AsSpan<byte>();
return new ImageData(TextureType.Texture2D, image.Format switch
{
StbiImageFormat.GreyAlpha => PixelFormat.Rg8I,
StbiImageFormat.Rgb => PixelFormat.Rgb8I,
StbiImageFormat.Rgba => PixelFormat.Rgba8I,
_ => PixelFormat.R8I,
},
image.Width,
image.Height,
data.ToArray());
}
public Application Context { get; private set; } = null!;
void IContextExtensionBase.Require(IContextBase context)
{
Require((Application)context);
}
}
}

View File

@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.OpenTK", "Dashboa
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.OpenGL", "Dashboard.OpenGL\Dashboard.OpenGL.csproj", "{33EB657C-B53A-41B4-BC3C-F38C09ABA577}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.OpenGL", "Dashboard.OpenGL\Dashboard.OpenGL.csproj", "{33EB657C-B53A-41B4-BC3C-F38C09ABA577}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.StbImage", "Dashboard.StbImage\Dashboard.StbImage.csproj", "{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -65,6 +67,11 @@ Global
{33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Debug|Any CPU.Build.0 = Debug|Any CPU {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.ActiveCfg = Release|Any CPU {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.Build.0 = Release|Any CPU {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.Build.0 = Release|Any CPU
{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7}.Release|Any CPU.Build.0 = Release|Any CPU
{14616F42-663B-4673-8561-5637FAD1B22F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -72,5 +79,6 @@ Global
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{7C90B90B-DF31-439B-9080-CD805383B014} = {9D6CCC74-4DF3-47CB-B9B2-6BB75DF2BC40} {7C90B90B-DF31-439B-9080-CD805383B014} = {9D6CCC74-4DF3-47CB-B9B2-6BB75DF2BC40}
{7B064228-2629-486E-95C6-BDDD4B4602C4} = {9B62A92D-ABF5-4704-B831-FD075515A82F} {7B064228-2629-486E-95C6-BDDD4B4602C4} = {9B62A92D-ABF5-4704-B831-FD075515A82F}
{85BCEB9E-DEC2-4A53-B2DA-6BFC6F3EE4E7} = {9B62A92D-ABF5-4704-B831-FD075515A82F}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -1,6 +1,5 @@
using System; using System;
using Dashboard.Drawing; using Dashboard.Drawing;
using Dashboard.Resources;
using Dashboard.Windowing; using Dashboard.Windowing;
namespace Dashboard.Controls namespace Dashboard.Controls

View File

@ -12,8 +12,4 @@
<EmbeddedResource Include="Resources\**\*.png"/> <EmbeddedResource Include="Resources\**\*.png"/>
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="ReFuel.StbImage" Version="2.1.0" />
</ItemGroup>
</Project> </Project>

View File

@ -1,22 +1,16 @@
using Dashboard.Drawing; using System.Drawing;
using System.Drawing;
using System.Text; using System.Text;
using Dashboard.Drawing.OpenGL; using Dashboard.Controls;
using Dashboard.ImmediateUI; using Dashboard.Drawing;
using Dashboard.Drawing.OpenGL.Pal;
using Dashboard.OpenTK.PAL2; using Dashboard.OpenTK.PAL2;
using OpenTK.Platform; using Dashboard.Pal;
using Dashboard.StbImage;
using OpenTK.Graphics.OpenGL; using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics; using OpenTK.Mathematics;
using Box2d = Dashboard.Box2d; using OpenTK.Platform;
using Image = Dashboard.Drawing.Image;
using TK = OpenTK.Platform.Toolkit; using TK = OpenTK.Platform.Toolkit;
using Dashboard;
using Dashboard.Controls;
using Dashboard.Drawing.OpenGL.Pal;
using Dashboard.Pal;
using Dashboard.Windowing;
using OpenTK.Windowing.GraphicsLibraryFramework;
using ReFuel.Stb;
using Image = Dashboard.Resources.Image;
using Vector4 = System.Numerics.Vector4; using Vector4 = System.Numerics.Vector4;
TK.Init(new ToolkitOptions() TK.Init(new ToolkitOptions()
@ -66,6 +60,7 @@ List<Vector3> points = new List<Vector3>();
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
app.Initialize(); app.Initialize();
app.ExtensionRequire<StbImageLoader>();
window = (PhysicalWindow)app.CreatePhysicalWindow(); window = (PhysicalWindow)app.CreatePhysicalWindow();