←Select platform

Convert(IntPtr,int,int,int,RasterByteOrder,RasterByteOrder,RasterColor[],RasterColor[],RasterColor16[],RasterColor16[],int,int,int,RasterConvertBufferFlags) Method

Summary
Converts data in the specified unmanaged buffer to the specified bits per pixel and color order.
Syntax
C#
C++/CLI
Python
public: 
static void Convert(  
   IntPtr buffer, 
   int width, 
   int inBitsPerPixel, 
   int outBitsPerPixel, 
   RasterByteOrder inOrder, 
   RasterByteOrder outOrder, 
   array<RasterColor>^ inPalette, 
   array<RasterColor>^ outPalette, 
   array<RasterColor16>^ inPalette16, 
   array<RasterColor16>^ outPalette16, 
   int lowBit, 
   int highBit, 
   int alpha, 
   RasterConvertBufferFlags flags 
)  
def Convert(self,flags): 

Parameters

buffer
The input buffer.

width
Width in pixels of the input image data.

inBitsPerPixel
BitsPerPixel of the input image data.

outBitsPerPixel
BitsPerPixel of the desired output image data.

inOrder
The input color order.

outOrder
The output color order.

inPalette
The palette or 8-bit LUT for the existing data, before conversion. If the input data is not palettized and not grayscale, use NULL.

outPalette
The palette for the converted data. If the data is converted to 16 or 24 bits per pixel color, use NULL for no palette.

inPalette16
The 16-bit LUT for the existing data, before conversion. Use NULL for no 16-bit LUT.

outPalette16
The 16-bit LUT for the converted data. Reserved for future use. Use NULL.

lowBit
Value indicating the low bit in the source buffer, if the source buffer contains grayscale data.

highBit
Value indicating the high bit in the source buffer, if the source buffer contains grayscale data.

alpha
The alpha value if the destination bits per pixel contains an alpha component

flags
Flags indicating whether to treat 16 bit data as grayscale or color.

Remarks

Note: This method will also work for 12 and 16-bit grayscale images, but only in the Document/Medical Imaging editions. If you attempt to use this method with a 12 or 16-bit grayscale image, but you do not have a Medical Imaging edition, it will throw an exception.

The conversion uses only one buffer, which must be large enough to hold the data before and after conversion.

Image data that is 8 bits per pixel or less must use a palette, and this method can use such data as input, output, or both. Therefore, you may need to specify the palette for the input, or for the output, or both.

If either  inBitsPerPixel or  outBitsPerPixel is 16,  flags is used to determine whether the data should be treated as color or grayscale.

If nBitsPerPixelSrc is 12, it is assumed to be grayscale. However, the  flags parameter should also reflect that it is grayscale for future compatibility. If the source is grayscale,  inPalette can be set to a palette. The palette should contain N entries. If the source uses  lowBit and  highBit, then N equals 2 raised to the power of ( highBit -  lowBit + 1). Otherwise, N equals 2 raised to the power of  inBitsPerPixel.

The  flags parameter supersedes  inOrder and  outOrder. If you specify RasterByteOrder.Bgr for  inOrder, but use RasterConvertBufferFlags.SourceGray in  flags, it will be assumed that the source buffer contains grayscale data.

For more information, refer to Introduction to Image Processing With LEADTOOLS.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
 
 
public void CalculateConvertSizeExample() 
{ 
	string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
	string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_4bits.bmp"); 
 
	using (RasterCodecs codecs = new RasterCodecs()) 
	{ 
		codecs.ThrowExceptionsOnInvalidImages = true; 
 
		// Load the image, at 24 bit per pixel. 
		RasterImage srcImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
		Assert.IsNotNull(srcImage); 
		Assert.IsTrue(srcImage.BitsPerPixel == 24); 
 
		// Create a new 4-bit image. 
		using (RasterImage destImage = new RasterImage( 
		   RasterMemoryFlags.Conventional, 
		   srcImage.Width, 
		   srcImage.Height, 
		   4, 
		   srcImage.Order, 
		   srcImage.ViewPerspective, 
		   srcImage.GetPalette(), 
		   IntPtr.Zero, 
		   0)) 
		{ 
			Assert.IsNotNull(destImage); 
			Assert.IsTrue(destImage.BitsPerPixel == 4); 
 
			int bufferSize = RasterBufferConverter.CalculateConvertSize( 
			   srcImage.Width, 
			   srcImage.BitsPerPixel, 
			   destImage.Width, 
			   destImage.BitsPerPixel); 
 
			// Allocate the buffer in unmanaged memory 
			IntPtr buffer = Marshal.AllocHGlobal(bufferSize); 
			Assert.IsFalse(buffer == IntPtr.Zero); 
 
			// Process each row from srcImage to destImage. 
			srcImage.Access(); 
			destImage.Access(); 
 
			for (int i = 0; i < srcImage.Height; i++) 
			{ 
				srcImage.GetRow(i, buffer, srcImage.BytesPerLine); 
				RasterBufferConverter.Convert( 
				   buffer, 
				   srcImage.Width, 
				   srcImage.BitsPerPixel, 
				   destImage.BitsPerPixel, 
				   srcImage.Order, 
				   destImage.Order, 
				   null, 
				   null, 
				   0, 
				   8, 
				   0, 
				   RasterConvertBufferFlags.None); 
				destImage.SetRow(i, buffer, destImage.BytesPerLine); 
			} 
 
			destImage.Release(); 
			srcImage.Release(); 
 
			// Save the destination image back to disk 
			codecs.Save(destImage, destFileName, RasterImageFormat.Bmp, 4); 
 
			// Clean up 
			Marshal.FreeHGlobal(buffer); 
		} 
	} 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.5.16
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.