Skip to content
New issue

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

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

Already on GitHub? # to your account

Memory Buffer API #9

Closed
Tracked by #4
SkyeHoefling opened this issue Sep 3, 2021 · 2 comments · Fixed by #30
Closed
Tracked by #4

Memory Buffer API #9

SkyeHoefling opened this issue Sep 3, 2021 · 2 comments · Fixed by #30
Assignees
Labels
⚒ Enhancement New feature or request

Comments

@SkyeHoefling
Copy link
Contributor

SkyeHoefling commented Sep 3, 2021

Description

Add memory buffer APIs that can return an array or a stream

New APIs

IImage

byte[] ToArray(int quality = 90)
Stream AsStream(int quality = 90)
ReadOnlySpan<byte> AsReadOnlySpan(int quality = 90)

Usage - ToArray()

byte[] buffer;
using (var image = new HeifImage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  buffer = primary.ToArray();
}

// do something with buffer

Usage - AsStream

Stream stream;
using (var image = new Heifimage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  stream = primary.AsStream();
}

// do something with stream

// don't forget to dispose of your stream
stream?.Dispose();

Usage - AsReadOnlySpan

This one is a little tricky as Span<T> gives us access to native memory. If we allow C# to operate on native memory we should ensure it happens within the IDisposable using block otherwise there will be a memory leak.

ReadOnlySpan<byte> span;
using (var image = new HeifImage("MyImage.heic"))
using (var primary = image.PrimaryImage())
{
  ReadOnlySpan<byte> span = primary.AsReadOnlySpan();

  // do something with span
}
@SkyeHoefling
Copy link
Contributor Author

@kenny-sellers
What do you think about this API structure? I am not sure if I want to keep it AsStream or call it ToStream

@SkyeHoefling SkyeHoefling self-assigned this Sep 3, 2021
@SkyeHoefling
Copy link
Contributor Author

I just updated the API specification to include JPEG quality

@SkyeHoefling SkyeHoefling mentioned this issue Sep 7, 2021
3 tasks
@SkyeHoefling SkyeHoefling added the ⚒ Enhancement New feature or request label Jan 6, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
⚒ Enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant