Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ColorConversion
Public Function GetCmykBufferFromImage(ByVal bgrImage As RasterImage) As Byte()
' StartUp the ColorConversion.
RasterColorConverterEngine.Startup()
' Image buffer array
Dim bgrBuffer(bgrImage.BytesPerLine * bgrImage.Height) As Byte
' get image buffer
Dim i As Integer
For i = 0 To bgrImage.Height - 1
bgrImage.GetRow(i, bgrBuffer, (i * bgrImage.BytesPerLine), bgrImage.BytesPerLine)
Next i
' Initialize a new Converter object
Dim converter As New RasterColorConverterEngine()
Dim cmykBuffer(bgrImage.Height * bgrImage.Width * 4) As Byte
Try
' Start the color conversion
converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Cmyk, Nothing)
' convert the image buffer
converter.Convert(bgrBuffer, _
0, _
cmykBuffer, _
0, _
bgrImage.Width, _
bgrImage.Height, _
CInt(bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))), _
0)
' stop the conversion
converter.Stop()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
RasterColorConverterEngine.Shutdown()
Return cmykBuffer
End Function
Public Function GetImageFromCmykBuffer(ByVal cmykBuffer As Byte(), ByVal width As Integer, ByVal height As Integer) As RasterImage
' StartUp the ColorConversion.
RasterColorConverterEngine.Startup()
' Initialize a new Converter object
Dim Converter As New RasterColorConverterEngine()
' Initialize an image to hold the converted buffer.
Dim cmykImage As RasterImage = Nothing
Try
cmykImage = New RasterImage(RasterMemoryFlags.Conventional, width, height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, IntPtr.Zero, 0)
' Start the color conversion
Converter.Start(ConversionColorFormat.Cmyk, ConversionColorFormat.Bgr, Nothing)
' convert the image buffer
Converter.ConvertToImage(cmykBuffer, _
0, _
cmykImage, _
width, _
height, _
0, _
CInt(cmykImage.BytesPerLine - (cmykImage.Width * (cmykImage.BitsPerPixel / 8))))
' stop the conversion
Converter.Stop()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
' Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown()
Return cmykImage
End Function
Public Sub SetParametersExample()
' StartUp the RasterCodecs.
Dim codecs As 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)
Dim width As Integer = bgrImage.Width
Dim height As Integer = bgrImage.Height
Dim cmykBuffer() As Byte = GetCmykBufferFromImage(bgrImage)
' Initialize the Rgb buffer array
Dim rgbBuffer(CInt(cmykBuffer.Length - cmykBuffer.Length / 4)) As Byte
' Initialize a new Converter object
Dim converter As New RasterColorConverterEngine
' Initialize a new ConversionParameters class object.
Dim convParams As ConversionParameters = New ConversionParameters
' we want to use the built in LEAD ICC conversion method and built in conversion
convParams.Method = ConversionMethodFlags.UseBuiltIn Or ConversionMethodFlags.UseIcc
' set the active conversion method
convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn
' Initialize WhitePoint property class.
convParams.WhitePoint = New ConversionWhitePoint
' Set WhitePoint property.
Dim whitePoint As ConversionWhitePoint
whitePoint.WhitePoint = ConversionWhitePointType.D50
convParams.WhitePoint = whitePoint
' Initialize CmykParameters property class.
convParams.CmykParameters = New ConversionCmykParameters
' Set GcrLevel property.
Dim cmykParameters As ConversionCmykParameters
cmykParameters.GcrLevel = 150
convParams.CmykParameters = cmykParameters
Try
' Initialize the ColorConversion
converter.Start(ConversionColorFormat.Cmyk, ConversionColorFormat.Rgb, convParams)
' convert the Cmyk buffer to Rgb buffer using Built-in conversion.
converter.Convert(cmykBuffer, _
0, _
rgbBuffer, _
0, _
width, _
height, _
0, _
CInt(bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))))
' Change the active method for conversion
convParams.Method = ConversionMethodFlags.ChangeActiveMethod Or ConversionMethodFlags.ChangeCmyk
' Switch to ICC conversion method
convParams.ActiveMethod = ConversionMethodFlags.UseIcc
' Set Mask property.
cmykParameters.Mask = ConversionCmykMask.Gcr
' Set GcrLevel property.
cmykParameters.GcrLevel = 175
convParams.CmykParameters = cmykParameters
' Update the conversion parameters
converter.SetParameters(convParams)
' convert the image buffer
converter.Convert(cmykBuffer, _
0, _
rgbBuffer, _
0, _
width, _
height, _
0, _
CInt(bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))))
' Stop the ColorConversion
converter.Stop()
Catch ex As Exception
MessageBox.Show(ex.Message)
rgbBuffer = Nothing
End Try
' the output File Name.
Dim outputFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "ResultImage.bmp")
' Save the converted Image
Dim cmykImage As RasterImage = GetImageFromCmykBuffer(cmykBuffer, width, height)
codecs.Save(cmykImage, outputFileName, RasterImageFormat.Bmp, 24)
' Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown()
' Shutdown the RasterCodecs
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 byte[] GetCmykBufferFromImage(RasterImage bgrImage)
{
// StartUp the ColorConversion.
RasterColorConverterEngine.Startup();
// 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();
byte[] cmykBuffer = new byte[bgrImage.Height * bgrImage.Width * 4];
try
{
// Start the color conversion
converter.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Cmyk, null);
// convert the image buffer
converter.Convert(bgrBuffer, // input buffer
0, // offset from the begining of the source buffer
cmykBuffer, // output buffer
0, // offset from the begining 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();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
RasterColorConverterEngine.Shutdown();
return cmykBuffer;
}
public RasterImage GetImageFromCmykBuffer(byte[] cmykBuffer, int width, int height)
{
// StartUp the ColorConversion.
RasterColorConverterEngine.Startup();
// Initialize a new Converter object
RasterColorConverterEngine converter = new RasterColorConverterEngine();
// Initialize an image to hold the converted buffer.
RasterImage cmykImage = null;
try
{
cmykImage = new RasterImage(RasterMemoryFlags.Conventional, width, height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);
// Start the color conversion
converter.Start(ConversionColorFormat.Cmyk, ConversionColorFormat.Bgr, null);
// convert the image buffer
converter.ConvertToImage(cmykBuffer, // converted buffer
0, // offset from the begining of the source buffer
cmykImage, // image to be save
width, // pixels width
height, // pixels height
0, // 0 bytes align
cmykImage.BytesPerLine - (cmykImage.Width * (cmykImage.BitsPerPixel / 8)));
// stop the conversion
converter.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown();
return cmykImage;
}
public void SetParametersExample()
{
// Initialize the RasterCodecs
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);
int width = bgrImage.Width;
int height = bgrImage.Height;
byte[] cmykBuffer = GetCmykBufferFromImage(bgrImage);
// Initialize the Rgb buffer array
byte[] rgbBuffer = new byte[cmykBuffer.Length - cmykBuffer.Length / 4];
// Initialize a new Converter object
RasterColorConverterEngine converter = new RasterColorConverterEngine();
// Initialize a new ConversionParameters class object.
ConversionParameters convParams = new ConversionParameters();
// we want to use the built in LEAD ICC conversion method and built in conversion
convParams.Method = ConversionMethodFlags.UseBuiltIn | ConversionMethodFlags.UseIcc;
// set the active conversion method
convParams.ActiveMethod = ConversionMethodFlags.UseBuiltIn;
// Initialize WhitePoint property class.
convParams.WhitePoint = new ConversionWhitePoint();
// Set WhitePoint property.
ConversionWhitePoint whitePoint = new ConversionWhitePoint();
whitePoint.WhitePoint = ConversionWhitePointType.D50;
convParams.WhitePoint = whitePoint;
// Initialize CmykParameters property class.
convParams.CmykParameters = new ConversionCmykParameters();
// Set GcrLevel property.
ConversionCmykParameters cmykParameters = new ConversionCmykParameters();
cmykParameters.GcrLevel = 150;
convParams.CmykParameters = cmykParameters;
try
{
// Initialize the ColorConversion
converter.Start(ConversionColorFormat.Cmyk, ConversionColorFormat.Rgb, convParams);
// convert the Cmyk buffer to Rgb buffer using Built-in conversion.
converter.Convert(cmykBuffer,
0,
rgbBuffer,
0,
width,
height,
0,
bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)));
// Change the active method for conversion
convParams.Method = ConversionMethodFlags.ChangeActiveMethod | ConversionMethodFlags.ChangeCmyk;
// Switch to ICC conversion method
convParams.ActiveMethod = ConversionMethodFlags.UseIcc;
// Set Mask property.
cmykParameters.Mask = ConversionCmykMask.Gcr;
// Set GcrLevel property.
cmykParameters.GcrLevel = 175;
convParams.CmykParameters = cmykParameters;
// Update the conversion parameters
converter.SetParameters(convParams);
// convert the image buffer
converter.Convert(cmykBuffer, 0, rgbBuffer, 0, width, height, 0, 0);
// Stop the ColorConversion
converter.Stop();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
rgbBuffer = null;
}
// the output File Name.
string outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "ResultImage.bmp");
// Save the converted Image
RasterImage cmykImage = GetImageFromCmykBuffer(cmykBuffer, width, height);
codecs.Save(cmykImage, outputFileName, RasterImageFormat.Bmp, 24);
// Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown();
// Shutdown the RasterColor
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}