Skip to content

YuvImage

ReferenceType edited this page Mar 12, 2025 · 2 revisions

YuvImage Class

The YuvImage class represents a YUV420 planar image. It handles memory allocation and provides methods to access image data efficiently.

Table of Contents


Overview

The YuvImage class is designed to manage YUV420 images using unmanaged memory for performance efficiency. It supports creating instances with allocated memory or referencing existing image data.

Features:

  • Represents YUV420 planar format.
  • Handles both allocated and referenced unmanaged memory.
  • Provides a method to retrieve the raw byte data.
  • Implements IDisposable for proper memory management.
  • Only disposes unmanaged memory if its allocated by this class(owns).

Constructors

Constructor Description
YuvImage(int width, int height) Allocates unmanaged memory for a YUV420 image.
YuvImage(IntPtr data, int width, int height) References an existing unmanaged YUV420 image.
YuvImage(IntPtr data, int width, int height, int Ystride, int UVstride) References an existing YUV420 image with custom strides.

Properties

Property Type Description
Width int The image width in pixels.
Height int The image height in pixels.
strideY int The number of bytes per row in the Y plane.
strideUV int The number of bytes per row in the U and V planes.(one of them, both strides assumed the same)
ImageBytes IntPtr Pointer to the image data in unmanaged memory.

Methods

Method Return Type Description
ToYUVImagePointer() YUVImagePointer Creates a pointer structure for direct access to Y, U, and V channels.
GetBytes() byte[] Returns the raw image data as a byte array.
Dispose() void Releases unmanaged memory.

Usage Examples

Creating a New YUV Image

YuvImage img = new YuvImage(1920, 1080);

Referencing an Existing YUV Image

IntPtr existingData = GetNativeYUVData();
YuvImage img = new YuvImage(existingData, 1920, 1080);

Retrieving Image Data as Bytes

byte[] imageData = img.GetBytes();