FileOnQ / Imaging.Heif

A C#/.NET wrapper around libheif for decoding and processing high efficiency image formats (heif, heic).
GNU Lesser General Public License v3.0
15 stars 4 forks source link

Memory Buffer API #9

Closed SkyeHoefling closed 3 years ago

SkyeHoefling commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

I just updated the API specification to include JPEG quality