Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Generic YUV conversion

The .NET Color Conversion provides direct color conversion to an image. Generic YUV conversion provides the ability to convert any YUV format to any supported color space, using the ConversionYuvParameters class and adhering to the restrictions listed below. After defining the YUV format, you can proceed with the conversion normally, just like any other conversion. Use Convert method to perform the conversion and call Stop method to free the conversion resources used. Currently the conversion from any color space to Generic YUV is not supported.

Generic YUV conversion currently has the following restrictions:

  1. No sub-sampling of Y is supported.
  2. The number of Y elements must be a multiple of both U, and V.
  3. With non-planar formats, vertical sub-sampling of Y, U, and V is not supported.
  4. No alignment supported in planar format; line width must not contain additional bytes.
  5. The horizontal sub-sampling periods of U, and V must be multiples of each other, and the vertical sub-sampling periods of U, and V must be multiples of each other.

Examples

1. Converting Y41P to RGB using Generic YUV Conversion:

[Visual Basic]


Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ColorConversion
 
' This example converts Y41P to RGB color space using Generic YUV Conversion.
Private Function TestConvertGenericY41PToRgb(ByVal yuvBuffer As Byte(), ByVal width As Integer, ByVal height As Integer) As Byte()
  ' StartUp the ColorConversion.
  RasterColorConverter.Startup()

  ' Initialize the Lab buffer array
  Dim rgbBuffer As Byte() = New Byte(yuvBuffer.Length - 1){}

  ' Byte ordering of the format; for Y41P:
  ' U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 Y7
  ' 0    1    2    3    4    5    6    7    8   9    10  11
  ' Put the Y component order first, then the U and V last as follows:
  ' Y positions: 1,3,5,7,8,9,10,11
  ' U positions: 0,4
  ' V positions: 2,6   
  Dim offset As Integer() = { 1, 3, 5, 7, 8, 9, 10, 11, 0, 4, 2, 6 }

  ' Initialize a new ConversionParameters class object.
  Dim convParams As ConversionParameters = New ConversionParameters()

  ' Initialize the YuvParameters class property.
  Dim yuvParameters As ConversionYuvParameters = ConversionYuvParameters.Empty

  ' Determine the horizontal sub-sampling of U
  yuvParameters.UH = 4

  ' Determine the vertical sub-sampling of U
  yuvParameters.UV = 1

  ' Determine the horizontal sub-sampling of V
  yuvParameters.VH = 4

  ' Determine the vertical sub-sampling of V
  yuvParameters.VV = 1

  ' Set the byte ordering
  yuvParameters.Offsets = offset

  ' The YUV values range
  yuvParameters.Range = ConversionYuvRange.UseFull

  ' This represents the macro pixels(smallest group of pixels allowed), 
  ' which indicates how many actual pixels are in the macro pixel. 
  ' This value is important only in non – planar format
  yuvParameters.MacroPixel = 8

  ' This is a Boolean value that represents the type of the YUV format (Planar = true, or non – Planar = false.)
  yuvParameters.Planar = False

  convParams.YuvParameters = yuvParameters

  ' Determine the type of conversion to be used in the conversion, for YUVGeneric, only use UseBuiltIn
  convParams.Method = ConversionMethodFlags.UseBuiltIn

  ' Determine the type of conversion to be activated. For YUVGeneric, only UseBuiltIn
  convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn

  ' Initialize a new Converter object
  Dim converter As RasterColorConverter = New RasterColorConverter()

  Try
    'Initialize the conversion
    converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Rgb, convParams)

    ' Convert the Buffer from Yuv to Rgb
    converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0)

    ' Stop the conversion
    converter.Stop()
  Catch ex As Exception
    MessageBox.Show(ex.Message)
    Return Nothing
  End Try

  ' Shutdown the ColorConversion.
  RasterColorConverter.Shutdown()

  ' return the converted buffer.
  Return rgbBuffer
End Function
[C#]

using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;

// This example converts Y41P to RGB color space using Generic YUV Conversion.
private byte[] TestConvertGenericY41PToRgb(byte[] yuvBuffer, int width, int height)
{
   // StartUp the ColorConversion.
   RasterColorConverter.Startup();

   // Initialize the Lab buffer array
   byte[] rgbBuffer = new byte[yuvBuffer.Length];

   // Byte ordering of the format; for Y41P:
   // U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 Y7
   // 0    1    2    3    4    5    6    7    8   9    10  11
   // Put the Y component order first, then the U and V last as follows:
   // Y positions: 1,3,5,7,8,9,10,11
   // U positions: 0,4
   // V positions: 2,6   
   int[] offset = { 1, 3, 5, 7, 8, 9, 10, 11, 0, 4, 2, 6 };

   // Initialize a new ConversionParameters class object.
   ConversionParameters convParams = new ConversionParameters();

   // Initialize the YuvParameters class property.
   ConversionYuvParameters yuvParameters = ConversionYuvParameters.Empty;

   // Determine the horizontal sub-sampling of U
   yuvParameters.UH = 4;

   // Determine the vertical sub-sampling of U
   yuvParameters.UV = 1;

   // Determine the horizontal sub-sampling of V
   yuvParameters.VH = 4;

   // Determine the vertical sub-sampling of V
   yuvParameters.VV = 1;

   // Set the byte ordering
   yuvParameters.Offsets = offset;

   // The YUV values range
   yuvParameters.Range = ConversionYuvRange.UseFull;

   // This represents the macro pixels(smallest group of pixels allowed), 
   // which indicates how many actual pixels are in the macro pixel. 
   // This value is important only in non – planar format
   yuvParameters.MacroPixel = 8;

   // This is a Boolean value that represents the type of the YUV format (Planar = true, or non – Planar = false.)
   yuvParameters.Planar = false;

   convParams.YuvParameters = yuvParameters;

   // Determine the type of conversion to be used in the conversion, for YUVGeneric, only use UseBuiltIn
   convParams.Method = ConversionMethodFlags.UseBuiltIn;

   // Determine the type of conversion to be activated. For YUVGeneric, only UseBuiltIn
   convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn;

   // Initialize a new Converter object
   RasterColorConverter converter = new RasterColorConverter();

   try
   {
      //Initialize the conversion
      converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Rgb, convParams);

      // Convert the Buffer from Yuv to Rgb
      converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0);

      // Stop the conversion
      converter.Stop();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
      return null;
   }

   // Shutdown the ColorConversion.
   RasterColorConverter.Shutdown();

   // return the converted buffer.
   return rgbBuffer;
}

2. Converting YVU9 (Planar) to RGB using Generic YUV Conversion:

[Visual Basic]


Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ColorConversion

' This example converts YVU9 (Planar) to RGB color space using Generic YUV Conversion.
Private Function TestConvertGenericYVU9ToRGB(ByVal yuvBuffer As Byte(), ByVal width As Integer, ByVal height As Integer) As Byte()
   ' StartUp the ColorConversion.
   RasterColorConverter.Startup()

   ' Initialize the Lab buffer array
   Dim rgbBuffer As Byte() = New Byte(yuvBuffer.Length - 1){}

   ' This array of integers specifies the byte order of the yuv format. For planar formats it takes only a single value that represents the plane sequence in the buffer: 
   '            YUV, YVU, UYV, UVY, VYU, or VUY.
   Dim offset As Integer() = { ConversionYuvParameters.PlanarYvu }

   'PlanarYuv means the Y plane comes first in the buffer, then the V plane, and the U plane is last.
   ' Initialize a new Converter object
   Dim converter As RasterColorConverter = New RasterColorConverter()

   ' Initialize a new ConversionParameters class object.
   Dim convParams As ConversionParameters = New ConversionParameters()

   ' Initialize the YuvParameters property class.
   Dim yuvParameters As ConversionYuvParameters = ConversionYuvParameters.Empty

   ' Set the horizontal sub-sampling of U.
   yuvParameters.UH = 4

   ' Set the vertical sub-sampling of U.
   yuvParameters.UV = 4

   ' Set the horizontal sub-sampling of V.
   yuvParameters.VH = 4

   ' Set the vertical sub-sampling of V.
   yuvParameters.VV = 4

   ' Pass the byte order array.
   yuvParameters.Offsets = offset

   ' The range value determines the YUV range values.
   yuvParameters.Range = ConversionYuvRange.UseFull

   ' This represents the macro pixels(smallest group of pixels allowed), 
   ' which indicates how many actual pixels are in the macro pixel. 
   ' This value is important only in non – planar format   
   yuvParameters.MacroPixel = 16

   ' This is a Boolean value that represents the type of the YUV format (Planar, or non – Planar).
   yuvParameters.Planar = True
   convParams.YuvParameters = yuvParameters

   ' Set the type of conversion to be used. For YUVGeneric, use only UseBuiltIn.
   convParams.Method = ConversionMethodFlags.UseBuiltIn

   ' Set the type of conversion to be activated. For YUVGeneric, use only UseBuiltIn.
   convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn
   Try
      ' Initialize the conversion.
      converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Rgb, convParams)
      ' Convert the buffer.
      converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0)
      ' Stop the conversion.
      converter.Stop()
   Catch ex As Exception
      MessageBox.Show(ex.Message)
      Return Nothing
   End Try
   ' Shutdown the ColorConversion.
   RasterColorConverter.Shutdown()
   ' return the converted buffer.
   Return rgbBuffer
End Function
[C#]

using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;

// This example converts YVU9 (Planar) to RGB color space using Generic YUV Conversion.
private byte[] TestConvertGenericYVU9ToRGB(byte[] yuvBuffer, int width, int height)
{
   // StartUp the ColorConversion.
   RasterColorConverter.Startup();

   // Initialize the Lab buffer array
   byte[] rgbBuffer = new byte[yuvBuffer.Length];

   // This array of integers specifies the byte order of the yuv format. For planar formats it takes only a single value that represents the plane sequence in the buffer: 
   //            YUV, YVU, UYV, UVY, VYU, or VUY.
   int[] offset = { ConversionYuvParameters.PlanarYvu };
   //PlanarYuv means the Y plane comes first in the buffer, then the V plane, and the U plane is last.

   // Initialize a new Converter object
   RasterColorConverter converter = new RasterColorConverter();

   // Initialize a new ConversionParameters class object.
   ConversionParameters convParams = new ConversionParameters();

   // Initialize the YuvParameters property class.
   ConversionYuvParameters yuvParameters = ConversionYuvParameters.Empty;

   // Set the horizontal sub-sampling of U.
   yuvParameters.UH = 4;

   // Set the vertical sub-sampling of U.
   yuvParameters.UV = 4;

   // Set the horizontal sub-sampling of V.
   yuvParameters.VH = 4;

   // Set the vertical sub-sampling of V.
   yuvParameters.VV = 4;

   // Pass the byte order array.
   yuvParameters.Offsets = offset;

   // The range value determines the YUV range values.
   yuvParameters.Range = ConversionYuvRange.UseFull;

   // This represents the macro pixels(smallest group of pixels allowed), 
   // which indicates how many actual pixels are in the macro pixel. 
   // This value is important only in non – planar format   

   yuvParameters.MacroPixel = 16;

   // This is a Boolean value that represents the type of the YUV format (Planar, or non – Planar).
   yuvParameters.Planar = true;

   convParams.YuvParameters = yuvParameters;

   // Set the type of conversion to be used. For YUVGeneric, use only UseBuiltIn.
   convParams.Method = ConversionMethodFlags.UseBuiltIn;

   // Set the type of conversion to be activated. For YUVGeneric, use only UseBuiltIn.
   convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn;

   try
   {
      // Initialize the conversion.
      converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Rgb, convParams);

      // Convert the buffer.
      converter.Convert(yuvBuffer, 0, rgbBuffer, 0, width, height, 0, 0);

      // Stop the conversion.
      converter.Stop();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
      return null;
   }

   // Shutdown the ColorConversion.
   RasterColorConverter.Shutdown();

   // return the converted buffer.
   return rgbBuffer;
}