Leadtools.ColorConversion Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
YuvParameters Property
See Also  Example
Leadtools.ColorConversion Namespace > ConversionParameters Class : YuvParameters Property



Gets or sets the YUV conversion properties.

Syntax

Visual Basic (Declaration) 
Public Property YuvParameters As ConversionYuvParameters
Visual Basic (Usage)Copy Code
Dim instance As ConversionParameters
Dim value As ConversionYuvParameters
 
instance.YuvParameters = value
 
value = instance.YuvParameters
C# 
public ConversionYuvParameters YuvParameters {get; set;}
C++/CLI 
public:
property ConversionYuvParameters YuvParameters {
   ConversionYuvParameters get();
   void set (ConversionYuvParameters value);
}

Return Value

A ConversionYuvParameters structure that contains the CIELab conversion properties.

Example

This example will convert a buffer from Yuv to Rgb Color Space.

Visual BasicCopy Code
Public Function GetYuvBufferFromImage(ByVal bgrImage As RasterImage) As Byte()
   ' StartUp the ColorConversion.
   RasterColorConverter.Startup()
   ' Image buffer array
   Dim bgrBuffer(bgrImage.Width * bgrImage.Height * 3) As Byte

   ' get image buffer
   Dim i As Integer
   For i = 0 To bgrImage.Height
      bgrImage.GetRow(i, bgrBuffer, (i * bgrImage.Width * 3), bgrImage.Width * 3)
   Next i

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

   Dim yuvBuffer(bgrBuffer.Length) As Byte

   Try
      ' Start the color conversion
      converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Yuv, Nothing)

      ' convert the image buffer
      converter.Convert(bgrBuffer, _
         0, _
         yuvBuffer, _
         0, _
         bgrImage.Width, _
         bgrImage.Height, _
         0, _
         0)

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

   RasterColorConverter.Shutdown()

   Return yuvBuffer
End Function

Public Function GetImageFromYuvBuffer(ByVal yuvBuffer As Byte(), ByVal width As Integer, ByVal height As Integer) As RasterImage
   ' StartUp the ColorConversion.
   RasterColorConverter.Startup()

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

   ' Initialize an image to hold the converted buffer.
   Dim yuvImage As RasterImage = Nothing
   Try
      yuvImage = New RasterImage(RasterMemoryFlags.Conventional, width, height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, IntPtr.Zero, 0)

      ' Start the color conversion
      Converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Bgr, Nothing)

      ' convert the image buffer
      Converter.ConvertToImage(yuvBuffer, _
         0, _
         yuvImage, _
         width, _
         height, _
         0, _
         0)

      ' stop the conversion
      Converter.Stop()
   Catch ex As Exception
      MessageBox.Show(ex.Message)
   End Try

   ' Shutdown the ColorConversion.
   RasterColorConverter.Shutdown()
   Return yuvImage
End Function


Public Sub YuvParametersExampleExample()
   ' Startup the RasterCodecs
   RasterCodecs.Startup()
   Dim codecs As New RasterCodecs()

   ' StartUp the ColorConversion.
   RasterColorConverter.Startup()

   ' The input file name
   Dim inputFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"

   ' load the input image as Bgr.
   Dim bgrImage As RasterImage = codecs.Load(inputFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)

   Dim width As Integer = bgrImage.Width
   Dim height As Integer = bgrImage.Height
   Dim yuvBuffer() As Byte = GetYuvBufferFromImage(bgrImage)

   ' Initialize the Rgb buffer array
   Dim rgbBuffer(yuvBuffer.Length) As Byte

   ' 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 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)

      ' Change the Yuv Parameters
      convParams.Method = ConversionMethodFlags.ChangeYuv

      ' Change the MacroPixel value
      yuvParameters.Mask = ConversionYuvMaskFlags.MacroPixel

      ' Reset the MacroPixel Property.
      yuvParameters.MacroPixel = 16
      convParams.YuvParameters = yuvParameters

      ' 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)
      rgbBuffer = Nothing
   End Try

   ' the output File Name.
   Dim outputFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "ResultImage.bmp"

   ' Save the converted Image
   Dim yuvImage As RasterImage = GetImageFromYuvBuffer(yuvBuffer, width, height)
   codecs.Save(yuvImage, outputFileName, RasterImageFormat.Bmp, 24)

   ' Shutdown the ColorConversion.
   RasterColorConverter.Shutdown()

   ' Shutdown the RasterCodecs
   RasterCodecs.Shutdown()
End Sub
C#Copy Code
public byte[] GetYuvBufferFromImage(RasterImage bgrImage) 

   // StartUp the ColorConversion. 
   RasterColorConverter.Startup(); 
   // Image buffer array 
   byte[] bgrBuffer = new byte[bgrImage.Width * bgrImage.Height * 3]; 
 
   // get image buffer 
   for (int i = 0; i < bgrImage.Height; i++) 
      bgrImage.GetRow(i, bgrBuffer, (i * bgrImage.Width * 3), bgrImage.Width * 3); 
 
   // Initialize a new Converter object 
   RasterColorConverter converter = new RasterColorConverter(); 
 
   byte[] yuvBuffer = new byte[bgrBuffer.Length]; 
 
   try 
   { 
      // Start the color conversion 
      converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Yuv, null); 
 
      // convert the image buffer  
      converter.Convert(bgrBuffer, // input buffer 
         0, // offset from the begining of the source buffer 
         yuvBuffer, // output buffer 
         0, // offset from the begining of the destination buffer 
         bgrImage.Width, // pixels width 
         bgrImage.Height, // pixels height 
         0, // 0 bytes align 
         0); // 0 bytes align 
 
      // stop the conversion 
      converter.Stop(); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.Message); 
   } 
 
   RasterColorConverter.Shutdown(); 
 
   return yuvBuffer; 

 
public RasterImage GetImageFromYuvBuffer(byte[] yuvBuffer, int width, int height) 

   // StartUp the ColorConversion. 
   RasterColorConverter.Startup(); 
 
   // Initialize a new Converter object 
   RasterColorConverter converter = new RasterColorConverter(); 
 
   // Initialize an image to hold the converted buffer. 
   RasterImage yuvImage = null; 
   try 
   { 
      yuvImage = new RasterImage(RasterMemoryFlags.Conventional, width, height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0); 
 
      // Start the color conversion 
      converter.Start(ConversionColorFormat.Yuv, ConversionColorFormat.Bgr, null); 
 
      // convert the image buffer 
      converter.ConvertToImage(yuvBuffer, // converted buffer 
         0,                // offset from the begining of the source buffer 
         yuvImage,        // image to be save 
         width,   // pixels width 
         height,  // pixels height 
         0,                // 0 bytes align 
         0);               // 0 bytes align 
 
      // stop the conversion 
      converter.Stop(); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.Message); 
   } 
   // Shutdown the ColorConversion. 
   RasterColorConverter.Shutdown(); 
   return yuvImage; 

 
 
public void YuvParametersPropertyExample() 

   // Startup the RasterCodecs 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // StartUp the ColorConversion. 
   RasterColorConverter.Startup(); 
 
   // The input file name 
   string inputFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
 
   // load the input image as Bgr. 
   RasterImage bgrImage = codecs.Load(inputFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
 
   int width = bgrImage.Width; 
   int height = bgrImage.Height; 
 
   byte[] yuvBuffer = GetYuvBufferFromImage(bgrImage); 
 
   // Initialize the Rgb 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; 
 
   // set the yuv parameters 
   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); 
 
      // Change the Yuv Parameters 
      convParams.Method = ConversionMethodFlags.ChangeYuv; 
 
      // Change the MacroPixel value 
      yuvParameters.Mask = ConversionYuvMaskFlags.MacroPixel; 
 
      // Reset the MacroPixel Property. 
      yuvParameters.MacroPixel = 16; 
      convParams.YuvParameters = yuvParameters; 
 
      // 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); 
      rgbBuffer = null; 
   } 
 
   // the output File Name. 
   string outputFileName = LeadtoolsExamples.Common.ImagesPath.Path + "ResultImage.bmp"; 
 
   // Save the converted Image 
   RasterImage yuvImage = GetImageFromYuvBuffer(yuvBuffer, width, height); 
   codecs.Save(yuvImage, outputFileName, RasterImageFormat.Bmp, 24); 
 
   // Shutdown the ColorConversion. 
   RasterColorConverter.Shutdown(); 
 
   // Shutdown the RasterCodecs 
   RasterCodecs.Shutdown(); 
}

Remarks

The YUV conversion properties. If this property value is null(Nothing), default values are assumed.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also