Converts image data in a managed buffer from one color conversion model directly to RGB\BGR using built-in algorithms, and sets the output as a RasterImage.
public static void ConvertDirectToImage(
ConversionColorFormat srcFormat,
ConversionColorFormat destFormat,
byte[] srcBuffer,
int srcBufferOffset,
RasterImage image,
int width,
int height,
int inAlign,
int outAlign
)
srcFormat
Format of the input data.
destFormat
Format of the output data.
srcBuffer
A pointer to the buffer containing the input data.
srcBufferOffset
Offset to the first byte of the srcBuffer data buffer.
image
A 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 scanline in the output buffer is a multiple of outAlign bytes.
There is no need to call the Start and Stop methods to initialize or stop the conversion engine.
Converting from any color space to YCCK color space is currently not supported.
For more information about Alignment Parameters, refer to Alignment Parameters.
To convert to the Yp41 type, the input buffer length must be a multiple of 8.
This example will convert BGR image to Lab color space, and then convert the Lab buffer to RasterImage, using the static method ConvertDirectToImage.
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];
bgrImage.Access();
// get image buffer
for (int i = 0; i < bgrImage.Height; i++)
bgrImage.GetRow(i, bgrBuffer, i * bgrImage.BytesPerLine, bgrImage.BytesPerLine);
bgrImage.Release();
// 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)
{
Debug.WriteLine(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:\LEADTOOLS22\Resources\Images";
}