// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
namespace Intel.RealSense
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class VideoFrame : Frame
{
public VideoFrame(IntPtr ptr)
: base(ptr)
{
object error;
Debug.Assert(NativeMethods.rs2_is_frame_extendable_to(ptr, Extension.VideoFrame, out error) > 0);
}
/// Gets the frame width in pixels
/// frame width in pixels
public int Width
{
get
{
object error;
var w = NativeMethods.rs2_get_frame_width(Handle, out error);
return w;
}
}
/// Gets the frame height in pixels
/// frame height in pixels
public int Height
{
get
{
object error;
var h = NativeMethods.rs2_get_frame_height(Handle, out error);
return h;
}
}
/// Gets the frame stride, meaning the actual line width in memory in bytes (not the logical image width)
/// stride in bytes
public int Stride
{
get
{
object error;
var stride = NativeMethods.rs2_get_frame_stride_in_bytes(Handle, out error);
return stride;
}
}
/// Gets the bits per pixels in the frame image
///
/// (note that bits per pixel is not necessarily divided by 8, as in 12bpp)
///
/// bits per pixel
public int BitsPerPixel
{
get
{
object error;
var bpp = NativeMethods.rs2_get_frame_bits_per_pixel(Handle, out error);
return bpp;
}
}
///
/// Copy frame data to managed typed array
///
/// array element type
/// array to copy to
/// Thrown when is null
public void CopyTo(T[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
var handle = GCHandle.Alloc(array, GCHandleType.Pinned);
try
{
// System.Diagnostics.Debug.Assert((array.Length * Marshal.SizeOf(typeof(T))) == (Stride * Height));
CopyTo(handle.AddrOfPinnedObject());
}
finally
{
handle.Free();
}
}
///
/// Copy frame data to pointer
///
/// destination pointer
/// Thrown when is null
public void CopyTo(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(ptr));
}
NativeMethods.Memcpy(ptr, Data, Stride * Height);
}
///
/// Copy data from managed array
///
/// array element type
/// array to copy from
/// Thrown when is null
public void CopyFrom(T[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
var handle = GCHandle.Alloc(array, GCHandleType.Pinned);
try
{
// System.Diagnostics.Debug.Assert((array.Length * Marshal.SizeOf(typeof(T))) == (Stride * Height));
CopyFrom(handle.AddrOfPinnedObject());
}
finally
{
handle.Free();
}
}
///
/// Copy data from pointer
///
/// source pointer
/// Thrown when is null
public void CopyFrom(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(ptr));
}
NativeMethods.Memcpy(Data, ptr, Stride * Height);
}
}
}