ReFuel.StbImage/StbiStreamWrapper.cs
H. Utku Maden bb5b3c638d
Some checks failed
Build / build (push) Failing after 1m3s
[v1.0.0] Create Quik.StbImage nuget package.
Make more shell scripts.

Fix removed old function.

Add a workflow.

Add docker-compose.yaml version.

Update .gitea/workflows/build.yaml

Trying to see if this helps.

Add executable bit to shell scripts.

Where did my files go?

Fix executable bit of higher repository.

Change build scripts slightly to maybe fix CI?

Update .gitea/workflows/build.yaml

List all files in home directory of the runner.

Another silly push.

List stuff.

YEss another useless push.

Yet another attempt to fix CI builds.

This is getting out of hand.

Chnage build script.

Use unencrypted storage for the key because Nuget is nuget.
2024-03-23 11:41:10 +03:00

60 lines
1.7 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Quik.Stb
{
public unsafe class StbiStreamWrapper : IDisposable
{
private Stream _stream;
private bool _keepOpen;
private bool _isDisposed;
private delegate int ReadProc(void *userdata, byte* buffer, int count);
private delegate void SkipProc(void *userdata, int count);
private delegate int Eof(void *userdata);
public StbiStreamWrapper(Stream stream, bool keepOpen = false)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
_stream = stream;
_keepOpen = keepOpen;
}
public void CreateCallbacks(out stbi_io_callbacks cb)
{
cb = default;
cb.read = Marshal.GetFunctionPointerForDelegate<ReadProc>(ReadCb);
cb.skip = Marshal.GetFunctionPointerForDelegate<SkipProc>(SkipCb);
cb.eof = Marshal.GetFunctionPointerForDelegate<Eof>(EofCb);
}
private int ReadCb(void *userdata, byte* buffer, int count)
{
Span<byte> bytes = new Span<byte>(buffer, count);
return _stream.Read(bytes);
}
private void SkipCb(void *userdata, int count)
{
_stream.Seek(count, SeekOrigin.Current);
}
private int EofCb(void *userdata)
{
if (!_stream.CanRead || _stream.Position == _stream.Length)
return 1;
return 0;
}
public void Dispose()
{
if (_isDisposed) return;
if (!_keepOpen) _stream.Dispose();
_isDisposed = true;
}
}
}