LEADTOOLS Color Conversion (Leadtools.ColorConversion assembly)

ConvertDirectToImage(ConversionColorFormat,ConversionColorFormat,Byte[],Int32,RasterImage,Int32,Int32,Int32,Int32) Method

Show in webframe
Example 





Format of the input data.
Format of the output data.
A byte array containing the input buffer.
Offset to the first byte of the srcBuffer data buffer.
An RasterImage object that will hold the converted data.
Width of pixels to be processed.
Height of pixels to be processed.
Each scanline in the input buffer is a multiple of inAlign bytes.
Each scan line in the output buffer is a multiple of outAlign bytes.
Converts image data in a buffer from one color conversion model to RGB\BGR directly using built in algorithms, and sets the output as RasterImage.
Syntax
public static void ConvertDirectToImage( 
   ConversionColorFormat srcFormat,
   ConversionColorFormat destFormat,
   byte[] srcBuffer,
   int srcBufferOffset,
   RasterImage image,
   int width,
   int height,
   int inAlign,
   int outAlign
)
'Declaration
 
Public Overloads Shared Sub ConvertDirectToImage( _
   ByVal srcFormat As ConversionColorFormat, _
   ByVal destFormat As ConversionColorFormat, _
   ByVal srcBuffer() As Byte, _
   ByVal srcBufferOffset As Integer, _
   ByVal image As RasterImage, _
   ByVal width As Integer, _
   ByVal height As Integer, _
   ByVal inAlign As Integer, _
   ByVal outAlign As Integer _
) 
'Usage
 
Dim srcFormat As ConversionColorFormat
Dim destFormat As ConversionColorFormat
Dim srcBuffer() As Byte
Dim srcBufferOffset As Integer
Dim image As RasterImage
Dim width As Integer
Dim height As Integer
Dim inAlign As Integer
Dim outAlign As Integer
 
RasterColorConverterEngine.ConvertDirectToImage(srcFormat, destFormat, srcBuffer, srcBufferOffset, image, width, height, inAlign, outAlign)

            

            
public:
static void ConvertDirectToImage( 
   ConversionColorFormat srcFormat,
   ConversionColorFormat destFormat,
   array<byte>^ srcBuffer,
   int srcBufferOffset,
   RasterImage^ image,
   int width,
   int height,
   int inAlign,
   int outAlign
) 

Parameters

srcFormat
Format of the input data.
destFormat
Format of the output data.
srcBuffer
A byte array containing the input buffer.
srcBufferOffset
Offset to the first byte of the srcBuffer data buffer.
image
An RasterImage object that will hold the converted data.
width
Width of pixels to be processed.
height
Height of pixels to be processed.
inAlign
Each scanline in the input buffer is a multiple of inAlign bytes.
outAlign
Each scan line in the output buffer is a multiple of outAlign bytes.
Remarks
There is no need to call the Start and Stop methods to initialize or stop the conversion engine.

This method works only for conversion to BGR or RGB color space formats.

For more information about Alignment Parameters, refer to Alignment Parameters.



To convert to the Yp41 type the input buffer length must be multiple of 8

Example
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ColorConversion

Public Sub ConvertDirectToImageExample()
   ' Initialize the RasterCodecs class
   Dim codecs As RasterCodecs = New RasterCodecs

   ' StartUp the ColorConversion.
   RasterColorConverterEngine.Startup()

   ' The input file name
   Dim inputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")

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

   ' Image buffer array
   Dim bgrBuffer(bgrImage.BytesPerLine * bgrImage.Height) As Byte

   ' get image buffer
   For i As Integer = 0 To bgrImage.Height - 1
      bgrImage.GetRow(i, bgrBuffer, (i * bgrImage.BytesPerLine), bgrImage.BytesPerLine)
   Next
   ' Initialize a new Converter object
   Dim converter As New RasterColorConverterEngine

   ' output Buffer array
   Dim labBuffer(bgrBuffer.Length) As Byte

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

   ' Initialize the labParams class property.
   Dim labParameters As ConversionLabParameters = ConversionLabParameters.Empty

   ' Set its properties
   labParameters.AOffset = 128
   labParameters.ARange = 170
   labParameters.BOffset = 96
   labParameters.BRange = 200
   labParameters.LOffset = 0
   labParameters.LRange = 100
   labParameters.Mask = ConversionLabMaskFlags.AOffset Or _
      ConversionLabMaskFlags.ARange Or _
      ConversionLabMaskFlags.BOffset Or _
      ConversionLabMaskFlags.BRange Or _
      ConversionLabMaskFlags.LOffset Or _
      ConversionLabMaskFlags.LRange

   convParams.LabParameters = labParameters

   ' Initialize an image to hold the converted buffer.
   Dim labImage As RasterImage = Nothing

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

      ' Change the Lab properties
      convParams.Method = ConversionMethodFlags.ChangeLab

      ' Set the updated properties
      converter.SetParameters(convParams)

      ' convert the image buffer 
      converter.Convert(bgrBuffer, _
         0, _
         labBuffer, _
         0, _
         bgrImage.Width, _
         bgrImage.Height, _
         CInt(bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))), _
         0)

      ' stop the conversion
      converter.Stop()

      ' Initialize labImage.
      labImage = New RasterImage(RasterMemoryFlags.Conventional, bgrImage.Width, bgrImage.Height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, IntPtr.Zero, 0)

      ' convert the image buffer
      ' The srcBufferOffset parameter is an offset to the start byte of the data in the source buffer.
      ' For example, if the image data started at byte 5, then this variable should = 5.
      ' In our example here, the image data starts at byte 0.

      ' Note that the srcBuffer can be also passed to this function as an IntPtr pointer.
      RasterColorConverterEngine.ConvertDirectToImage( _
         ConversionColorFormat.Lab, _
         ConversionColorFormat.Bgr, _
         labBuffer, _
         0, _
         labImage, _
         bgrImage.Width, _
         bgrImage.Height, _
         0, _
         CInt(bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))))

      ' dispose the used image
      bgrImage.Dispose()
   Catch ex As Exception
      MessageBox.Show(ex.Message)
   End Try
   ' Shutdown the ColorConversion.
   RasterColorConverterEngine.Shutdown()

   ' the output File Name.
   Dim outputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "ResultImage.bmp")

   ' Save the result image.
   codecs.Save(labImage, outputFileName, RasterImageFormat.Bmp, 24)

End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;

public void ConvertDirectToImageExample()
{
   // Initialize the RasterCodecs class
   RasterCodecs codecs = new RasterCodecs();

   // StartUp the ColorConversion.
   RasterColorConverterEngine.Startup();

   // The input file name
   string inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");

   // load the input image as Bgr.
   RasterImage bgrImage = codecs.Load(inputFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);

   // Image buffer array
   byte[] bgrBuffer = new byte[bgrImage.BytesPerLine * bgrImage.Height];

   // get image buffer
   for(int i = 0; i < bgrImage.Height; i ++)
       bgrImage.GetRow(i, bgrBuffer, i * bgrImage.BytesPerLine, bgrImage.BytesPerLine);

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

   // output Buffer array
   byte[] labBuffer = new byte[bgrBuffer.Length];

   ConversionParameters convParams = new ConversionParameters();

   // Initialize the labParams structure property.
   ConversionLabParameters labParameters = ConversionLabParameters.Empty;

   // Set its properties
   labParameters.AOffset = 128;
   labParameters.ARange = 170;
   labParameters.BOffset = 96;
   labParameters.BRange = 200;
   labParameters.LOffset = 0;
   labParameters.LRange = 100;
   labParameters.Mask = ConversionLabMaskFlags.AOffset |
      ConversionLabMaskFlags.ARange |
      ConversionLabMaskFlags.BOffset |
      ConversionLabMaskFlags.BRange |
      ConversionLabMaskFlags.LOffset |
      ConversionLabMaskFlags.LRange;

   convParams.LabParameters = labParameters;

   // Initialize an image to hold the converted buffer.
   RasterImage labImage = null;

   try
   {
      // Start the color conversion
      converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Lab, null);

      // Change the Lab properties
      convParams.Method = ConversionMethodFlags.ChangeLab;

      // Set the updated properties
      converter.SetParameters(convParams);

      // convert the image buffer 
      converter.Convert(bgrBuffer, // input buffer
         0, // offset from the beginning of the source buffer
         labBuffer, // output buffer
         0, // offset from the beginning of the destination buffer
         bgrImage.Width, // pixels width
         bgrImage.Height, // pixels height
         bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)),
         0); // 0 bytes align

      // stop the conversion
      converter.Stop();

      // Initialize labImage.
      labImage = new RasterImage(RasterMemoryFlags.Conventional, bgrImage.Width, bgrImage.Height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);

      // convert the image buffer
      // The srcBufferOffset parameter is an offset to the start byte of the data in the source buffer.
      // For example, if the image data started at byte 5, then this variable should = 5.
      // In our example here, the image data starts at byte 0.

      // Note that the srcBuffer can be also passed to this function as an IntPtr pointer.
      RasterColorConverterEngine.ConvertDirectToImage(
         ConversionColorFormat.Lab,
         ConversionColorFormat.Bgr,
         labBuffer, // converted buffer
         0, // offset from the beginning of the source buffer
         labImage, // image to be view
         bgrImage.Width, // pixels width
         bgrImage.Height, // pixels height
         0, // 0 bytes align
         bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)));

      // dispose the used image
      bgrImage.Dispose();
   }
   catch(Exception ex)
   {
      MessageBox.Show(ex.Message);
   }

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

   // the output File Name.
   string outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "ResultImage.bmp");

   // Save the converted Image
   codecs.Save(labImage, outputFileName, RasterImageFormat.Bmp, 24);

}

static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;

public void ConvertDirectToImageExample()
{
   // Initialize the RasterCodecs class
   RasterCodecs codecs = new RasterCodecs();

   // StartUp the ColorConversion.
   RasterColorConverterEngine.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);

   // Image buffer array
   byte[] bgrBuffer = new byte[bgrImage.BytesPerLine * bgrImage.Height];

   // get image buffer
   for (int i = 0; i < bgrImage.Height; i++)
      bgrImage.GetRow(i, bgrBuffer, i * bgrImage.BytesPerLine, bgrImage.BytesPerLine);

   // output Buffer array
   byte[] labBuffer = new byte[bgrBuffer.Length];

   // Initialize an image to hold the converted buffer.
   RasterImage labImage = null;

   try
   {

      // convert the image buffer 
      RasterColorConverterEngine.ConvertDirect(ConversionColorFormat.Bgr,
         ConversionColorFormat.Lab,
         bgrBuffer, // input buffer
         0, // offset from the beginning of the source buffer
         labBuffer, // output buffer
         0, // offset from the beginning of the destination buffer
         bgrImage.Width, // pixels width
         bgrImage.Height, // pixels height
         bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)),
         0); // 0 bytes align

      // Initialize labImage.
      labImage = new RasterImage(RasterMemoryFlags.Conventional, bgrImage.Width, bgrImage.Height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0);

      // convert the image buffer
      // The srcBufferOffset parameter is an offset to the start byte of the data in the source buffer.
      // For example, if the image data started at byte 5, then this variable should = 5.
      // In our example here, the image data starts at byte 0.

      // Note that the srcBuffer can be also passed to this function as an IntPtr pointer.
      RasterColorConverterEngine.ConvertDirectToImage(
         ConversionColorFormat.Lab,
         ConversionColorFormat.Bgr,
         labBuffer, // converted buffer
         0, // offset from the beginning of the source buffer
         labImage, // image to be view
         bgrImage.Width, // pixels width
         bgrImage.Height, // pixels height
         0, // 0 bytes align
         bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)));

      // dispose the used image
      bgrImage.Dispose();
   }
   catch (Exception)
   {
   }

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

   // the output File Name.
   string outputFileName = LeadtoolsExamples.Common.ImagesPath.Path + "ResultImage.bmp";

   // Save the converted Image
   codecs.Save(labImage, outputFileName, RasterImageFormat.Bmp, 24);
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ColorConversion

      
Public Sub ConvertDirectToImageExample()
   ' Initialize the RasterCodecs class
   Dim codecs As RasterCodecs = New RasterCodecs()
   ' StartUp the ColorConversion.
   RasterColorConverterEngine.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)

   ' Image buffer array
   Dim bgrBuffer As Byte() = New Byte(bgrImage.BytesPerLine * bgrImage.Height - 1){}

   ' get image buffer
   Dim i As Integer = 0
   Do While i < bgrImage.Height
      bgrImage.GetRow(i, bgrBuffer, i * bgrImage.BytesPerLine, bgrImage.BytesPerLine)
      i += 1
   Loop

   ' output Buffer array
   Dim labBuffer As Byte() = New Byte(bgrBuffer.Length - 1){}

   ' Initialize an image to hold the converted buffer.
   Dim labImage As RasterImage = Nothing

   Try

      ' convert the image buffer 
      RasterColorConverterEngine.ConvertDirect(ConversionColorFormat.Bgr, ConversionColorFormat.Lab, bgrBuffer, 0, labBuffer, 0, bgrImage.Width, bgrImage.Height, bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)), 0) ' 0 bytes align

      ' Initialize labImage.
      labImage = New RasterImage(RasterMemoryFlags.Conventional, bgrImage.Width, bgrImage.Height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0)

      ' convert the image buffer
      ' The srcBufferOffset parameter is an offset to the start byte of the data in the source buffer.
      ' For example, if the image data started at byte 5, then this variable should = 5.
      ' In our example here, the image data starts at byte 0.

      ' Note that the srcBuffer can be also passed to this function as an IntPtr pointer.
      RasterColorConverterEngine.ConvertDirectToImage(ConversionColorFormat.Lab, ConversionColorFormat.Bgr, labBuffer, 0, labImage, bgrImage.Width, bgrImage.Height, 0, bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)))

      ' dispose the used image
      bgrImage.Dispose()
   Catch e1 As Exception
   End Try

   ' Shutdown the ColorConversion.
   RasterColorConverterEngine.Shutdown()

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

   ' Save the converted Image
   codecs.Save(labImage, outputFileName, RasterImageFormat.Bmp, 24)
End Sub
Requirements

Target Platforms

See Also

Reference

RasterColorConverterEngine Class
RasterColorConverterEngine Members
Overload List
Alignment Parameters
Start Method
Stop Method
SetParameters Method
Convert
ConvertToImage

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.