Skip to content

Converter

ReferenceType edited this page Mar 12, 2025 · 11 revisions

Converter Class

The Converter class provides static methods for image format conversions, memory allocation, and configuration settings. It supports RGB, BGR, RGBA, BGRA to YUV420P conversions, YUV420P to RGB conversions, YUV NV12 to RGB conversions, YUV NV12 to YUV I420 conversions, and image downscaling.

Table of Contents


Overview

The Converter class is designed to provide efficient image conversion and memory management functionalities. It leverages native implementations for performance-critical operations and offers various configurations to optimize conversions.

Features:

  • RGB/BGR/RGBA/BGRA to YUV420P conversion.
  • YUV420P to RGB conversion.
  • YUV NV12 to RGB conversion.
  • YUV NV12 to YUV I420 conversion.
  • Image downscaling.
  • Aligned native memory allocation and freeing.
  • Global configuration settings.
  • SIMD optimizations (SSE, NEON, AVX2).

Static Methods

Method Return Type Description
SetConfig(ConverterConfig config) void Sets global configuration for the converter.
GetCurrentConfig() ConverterConfig Gets the current configuration.
SetOption(ConverterOption option, int value) void Sets a configuration option.
AllocAllignedNative(int size) IntPtr Allocates aligned native memory.
FreeAllignedNative(IntPtr p) void Frees native memory allocated by AllocAllignedNative.
Rgb2Yuv(RgbImage from, YuvImage yuv) void Converts RGB, BGR, RGBA, BGRA to YUV420P.
Yuv2Rgb(YuvImage yuv, RgbImage image) void Converts YUV420P image to RGB format.
Yuv2Rgb(YUVImagePointer yuv, RgbImage image) void Converts YUV420P image to RGB format using YUVImagePointer.
Yuv2Rgb(YUVNV12ImagePointer yuv, RgbImage image) void Converts YUV NV12 planar image to RGB format.
YuvNV12toYV12(YUVNV12ImagePointer nv12, YuvImage yv12) void Converts YUV NV12 planar image to YUV I420.
Downscale(RgbImage from, RgbImage to, int multiplier) void Downscales an image by a given factor.

Name Description
NumThreads Number of chunks that image is divided and sent to threadpool.
EnableSSE Allows use of SSE SIMD implementations of Converter operations. Does nothing on ARM.
EnableNeon Allows use of NEON SIMD implementations of Converter operations. Does nothing on x86 systems.
EnableAvx2 Allows use of AVX2 SIMD implementations of Converter operations. Does nothing on ARM.
EnableAvx512 Not supported yet.
EnableCustomThreadPool Enables use of Custom Threadpool. On windows default pool is Windows pool provided on ppl.h. You can disable this behaviour and use custom pool. Depending hardware performance may vary.
EnableDebugPrints EnablesDebugPrints
ForceNaiveConversion For test purposes only, when no SIMD enabled, uses Fixed point approximation naive converter.

Usage Examples

Setting Converter Configuration

ConverterConfig config = Converter.GetCurrentConfig();
config.NumThreads = 4;
Converter.SetConfig(config);

Setting Converter Options

Converter.SetOption(ConverterOption.EnableAvx2, 1);
Converter.SetOption(ConverterOption.NumThreads, 8);

RGB to YUV Conversion

RgbImage rgbImage = new RgbImage(ImageFormat.Rgb, 800, 600);
YuvImage yuvImage = new YuvImage(800, 600);
Converter.Rgb2Yuv(rgbImage, yuvImage);

YUV to RGB Conversion

YuvImage yuvImage = new YuvImage(800, 600);
RgbImage rgbImage = new RgbImage(ImageFormat.Rgb, 800, 600);
Converter.Yuv2Rgb(yuvImage, rgbImage);

Image Downscaling

RgbImage fromImage = new RgbImage(ImageFormat.Rgb, 1600, 1200);
RgbImage toImage = new RgbImage(ImageFormat.Rgb, 800, 600);
Converter.Downscale(fromImage, toImage, 2);

Allocating and Freeing Native Memory

IntPtr nativePtr = Converter.AllocAllignedNative(1024 * 1024); // Allocate 1MB, alligns to 64 bytes
Converter.FreeAllignedNative(nativePtr);
Clone this wiki locally