Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] - Support use of ReadOnly/Span in APIs #923

Open
Clockwork-Muse opened this issue Apr 1, 2024 · 5 comments
Open

[FEATURE] - Support use of ReadOnly/Span in APIs #923

Clockwork-Muse opened this issue Apr 1, 2024 · 5 comments
Assignees

Comments

@Clockwork-Muse
Copy link

Clockwork-Muse commented Apr 1, 2024

Describe the feature
I'm writing a program that receives images from a connected device. As part of that, I'm using the new System.IO.Pipelines, which gives me some combination of;

  • ReadOnly/Span
  • ReadOnly/Memory
  • ReadOnlySequence

Unfortunately, it's not possible to get the underlying byte array that these types use for actual backing memory (if any such exists), meaning that in order to call, say, imdecode I'm required to copy to an array for the decoding to happen.

It would be helpful if instead most of the available APIs supported passing Span types.

An additional side option here is, if OpenCV allows access, to wrap the native memory allocated by OpenCV itself, and implement the MemoryManager API.

@sn4k3
Copy link

sn4k3 commented Apr 5, 2024

Take a look at EmguExtensions.cs search for Span

@Clockwork-Muse
Copy link
Author

@sn4k3 - I'm trying to call imdecode, and I start with a Span, not need one as the result of a call (at the moment).

@sn4k3
Copy link

sn4k3 commented Apr 5, 2024

@sn4k3 - I'm trying to call imdecode, and I start with a Span, not need one as the result of a call (at the moment).

Ok for that is required the implementation in the method.
But if you have a span you should also have the array and passing it without the need of copy your span to a new buffer?

@emgucv
Copy link
Owner

emgucv commented Apr 5, 2024

Are you trying to call Imdecode on a Span?

The Imdecode function expect a pointer to byte. I don't think you can get a pointer from Span based on the documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.span-1?view=net-8.0

If you can't get a pointer from a Span, you will need to copy it to a buffer that can give you a pointer.

@Clockwork-Muse
Copy link
Author

But if you have a span you should also have the array and passing it without the need of copy your span to a new buffer?

That might not be the case - my original code was using MemoryPool<byte> and IMemoryOwner<byte> as the more "modern" setup (as opposed to ArrayPool<byte>), so I didn't have access to the underlying array.

Are you trying to call Imdecode on a Span?

The Imdecode function expect a pointer to byte.

On a Span/ReadOnlySpan, yes.
(Or rather - I start from a ReadOnlySequence, but none of the native imdecode overloads would be able to work with an iterable set of buffers, so not expecting it to be usable here)

You can get a pointer via use of the fixed statement:

unsafe
{
    int[] numbers = [10, 20, 30, 40, 50];
    Span<int> interior = numbers.AsSpan()[1..^1];
    fixed (int* p = interior)
    {
        for (int i = 0; i < interior.Length; i++)
        {
            Console.Write(p[i]);  
        }
        // output: 203040
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants