Does StreamImage have a function like ImageToMemoryRaw

Does StreamImage have a function like ImageToMemoryRaw to transfer images to other visual processing software?

Hi @soultower,

ImageToMemoryRaw is currently not exported through the CVB.Net API and I think it is not necessarily the best solution for what you have in mind because it will always copy the image to into a block of memory. Most of the time when trying to interface to another software this is over the top - such copy operations are something most people will try to avoid as good as possible.

If you look at different software packages, more often than not a combination of width, height, base pointer and y increment is necessary to describe the memory layout of an image to a processing function, and for :cvb: images these are most often accessible without producing a deep copy of the image data. Every entry in the Stemmer.Cvb.Image.Planes collection has a method TryGetLinearAccess() (as well as its throw-on-error sibling GetLinearAccess()). If accessing the image’s pixels in a linear fashion (in other words: if a pixel’s memory offsets depend linearly on the pixel’s x and y position) this function will fill a Stemmer.Cvb.LinearAccessData structure for you that contains the base pointer (BasePtr), the x increment (XInc) and the y increment (YInc). Width and height are accessible through Stemmer.Cvb.Image.Width and Stemmer.Cvb.Image.Height. Usually this data does the trick - especially if the image comes from a camera. Just make sure you don’t Dispose() your image while the other library is still working on it.

If, on the other hand, linear access is not possible (TryGetLinearAccess() returns false or GetLinearAccess() raises a System.FormatException) then there will be no way around a deep copy of the image data before passing it to a different library (:cvb: is capable of processing the weirdest kinds of memory layout while almost all other libraries process only linearly arranged pixel data; luckily weird arrangements have become rare these days :slightly_smiling_face:). In that case I’d recommend using the Clone() method to prepare a deep copy of the data into a new Stemmer.Cvb.Image and access this one through TryGetLinearAccess() (the performance hit due to the deep copy will be identical to what ImageToMemoryRaw() would require.

ImageToMemoryRaw() will sooner or later be added to CVB.Net as well - you can expect to see it in the 13.02.000 release of the library.

ImageToMemoryRaw()I need it very much. When will 13.02 be released?

You can use it even today if you reference the iCVCImg.dll:

static class ImageExtensions
{
  public static unsafe byte[] ToMemoryBuffer(this Image image)
  {
    if (image == null)
      throw new ArgumentNullException(nameof(image));

    var sizeInBytes = Cvb.Image.ImageToMemoryRawSize(image.Handle);
    var buffer = new byte[sizeInBytes];
    fixed (byte* bufferPtr = buffer)
    {
      if (!Cvb.Image.ImageToMemoryRaw(image.Handle, (IntPtr)bufferPtr, sizeInBytes, Cvb.Image.TRawFileLayout.RFL_Interleaved))
        throw new Exception($"Could not create raw image buffer from {nameof(image)}");
    }
    return buffer;
  }
}

You can use it like this:

var buffer = myImage.ToMemoryBuffer();