←Select platform

CalculateConvertSize Method

Summary
Calculates the size required for output buffer.
Syntax
C#
C++/CLI
Python
public static int CalculateConvertSize( 
   int inWidth, 
   int inBitsPerPixel, 
   int outWidth, 
   int outBitsPerPixel 
) 
public: 
static int CalculateConvertSize(  
   int inWidth, 
   int inBitsPerPixel, 
   int outWidth, 
   int outBitsPerPixel 
)  
def CalculateConvertSize(self,inWidth,inBitsPerPixel,outWidth,outBitsPerPixel): 

Parameters

inWidth
Width in pixels of the input image data.

inBitsPerPixel
BitsPerPixel of the input image data.

outWidth
Width in pixels of the desired output image data.

outBitsPerPixel
BitsPerPixel of the desired output image data.

Return Value

The size in bytes of the required output buffer.

Remarks

Use this method to calculate the required size of the output buffer for image data conversions.

Example

This example loads an image at 24 bits per pixel, and creates a new image at 4 bits per pixel, and then uses the Convert method to convert data from 24 bits per pixel to 4 bits per pixel.

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:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

Target Platforms

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

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