Skip to content

Commit

Permalink
Update content of loaded image, if same image is wrapped into a new I…
Browse files Browse the repository at this point in the history
…mageResource;
  • Loading branch information
onepiecefreak3 committed Feb 18, 2024
1 parent 6ab1910 commit 45a7ca0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 25 additions & 11 deletions ImGui.Forms/Factories/ImageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ImageFactory
private readonly GraphicsDevice _gd;
private readonly ImGuiRenderer _controller;

private readonly IDictionary<object, IntPtr> _inputPointers;
private readonly IDictionary<IntPtr, object> _inputPointersReverse;
private readonly IDictionary<Bitmap, IntPtr> _inputPointers;
private readonly IDictionary<IntPtr, Bitmap> _inputPointersReverse;
private readonly IDictionary<IntPtr, Texture> _ptrTextures;
private readonly IDictionary<IntPtr, int> _ptrTexturesRefCount;

Expand All @@ -23,8 +23,8 @@ public ImageFactory(GraphicsDevice gd, ImGuiRenderer controller)
{
_gd = gd;
_controller = controller;
_inputPointers = new Dictionary<object, IntPtr>();
_inputPointersReverse = new Dictionary<IntPtr, object>();
_inputPointers = new Dictionary<Bitmap, IntPtr>();
_inputPointersReverse = new Dictionary<IntPtr, Bitmap>();
_ptrTextures = new Dictionary<IntPtr, Texture>();
_ptrTexturesRefCount = new Dictionary<IntPtr, int>();
_unloadQueue = new List<IntPtr>();
Expand All @@ -37,6 +37,7 @@ public IntPtr LoadImage(Bitmap img)
if (_inputPointers.ContainsKey(img))
{
ptr = _inputPointers[img];
UpdateImage(ptr);

_ptrTexturesRefCount[ptr]++;

Expand All @@ -52,6 +53,14 @@ public IntPtr LoadImage(Bitmap img)
return ptr;
}

public void UpdateImage(IntPtr ptr)
{
if (!_ptrTextures.ContainsKey(ptr) || !_inputPointersReverse.ContainsKey(ptr))
return;

CopyImageData(_ptrTextures[ptr], _inputPointersReverse[ptr]);
}

public void UnloadImage(IntPtr ptr)
{
if (!_ptrTextures.ContainsKey(ptr))
Expand All @@ -66,13 +75,7 @@ private IntPtr LoadImageInternal(Bitmap image)
var texture = _gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D(
(uint)image.Width, (uint)image.Height, 1, 1, Veldrid.PixelFormat.B8_G8_R8_A8_UNorm, TextureUsage.Sampled));

var data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

_gd.UpdateTexture(texture, data.Scan0, (uint)(4 * image.Width * image.Height),
0, 0, 0, (uint)image.Width, (uint)image.Height, 1, 0, 0);

image.UnlockBits(data);
CopyImageData(texture, image);

// Add image pointer to cache
var imgPtr = _controller.GetOrCreateImGuiBinding(_gd.ResourceFactory, texture);
Expand All @@ -81,6 +84,17 @@ private IntPtr LoadImageInternal(Bitmap image)
return imgPtr;
}

private void CopyImageData(Texture texture, Bitmap image)
{
var data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

_gd.UpdateTexture(texture, data.Scan0, (uint)(4 * image.Width * image.Height),
0, 0, 0, (uint)image.Width, (uint)image.Height, 1, 0, 0);

image.UnlockBits(data);
}

internal void FreeTextures()
{
foreach (var toFree in _unloadQueue)
Expand Down
2 changes: 1 addition & 1 deletion ImGui.Forms/ImGui.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.0.51</version>
<version>1.0.52</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
Expand Down

0 comments on commit 45a7ca0

Please sign in to comment.