LEADTOOLS (Leadtools assembly)
LEAD Technologies, Inc

StartDithering Method

Example 





The palette this uses for dithering. You can specify your own palette, or use a null reference (Nothing in Visual Basic) for LEAD's fixed palette.
Number of colors used in the palette. If the palette contains more colors, only the first colors colors are used. Valid values are 2 to 256.
Initializes the buffered dithering of an image. .NET support Silverlight support WinRT support
Syntax
public void StartDithering( 
   RasterColor[] palette,
   int colors
)
'Declaration
 
Public Sub StartDithering( _
   ByVal palette() As RasterColor, _
   ByVal colors As Integer _
) 
'Usage
 
Dim instance As RasterImage
Dim palette() As RasterColor
Dim colors As Integer
 
instance.StartDithering(palette, colors)
public void StartDithering( 
   RasterColor[] palette,
   int colors
)
 function Leadtools.RasterImage.StartDithering( 
   palette ,
   colors 
)
public:
void StartDithering( 
   array<RasterColor>^ palette,
   int colors
) 

Parameters

palette
The palette this uses for dithering. You can specify your own palette, or use a null reference (Nothing in Visual Basic) for LEAD's fixed palette.
colors
Number of colors used in the palette. If the palette contains more colors, only the first colors colors are used. Valid values are 2 to 256.
Remarks

The following flow chart shows how the methods relate to each other:

Dithering Flow Chart

The following properties from the RasterImage are used to control the dithering operation:

This method does not support signed images.

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

Example
 
Public Sub StartDitheringExample()
      Dim codecs As RasterCodecs = New RasterCodecs()
      ' Load an image that has BottomLeft ViewPerspective
      Dim image As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1.CMP"))

      Dim Palette As RasterColor() = RasterPalette.Fixed(8)

      ' Create the new palletized image.
      Dim destinationImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, image.Width, image.Height, 8, image.Order, image.ViewPerspective, Palette, IntPtr.Zero, 0)

      ' Set the dithering method.
      image.DitheringMethod = RasterDitheringMethod.StevensonArce

      ' Initialize the dithering process.
      image.StartDithering(Palette, 256)

      ' Allocate the output buffer for 8-bit data.
      Dim InBuffer As Byte() = New Byte(image.Width * 3 - 1) {} ' Buffer to hold the input row.
      Dim OutBuffer As Byte() = New Byte(image.Width - 1) {} ' Buffer to hold the output row.

      ' Use DitherLine method to process each row in the image.
      Dim i As Integer = 0
      Do While i < image.Height
         image.GetRow(i, InBuffer, 0, image.BytesPerLine)
         image.DitherLine(InBuffer, 0, OutBuffer, 0)
         destinationImage.SetRow(i, OutBuffer, 0, destinationImage.BytesPerLine)
         i += 1
      Loop
      ' End the dithering process.
      image.StopDithering()

      codecs.Save(destinationImage, Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1_DitherLine.BMP"), RasterImageFormat.Bmp, 0)

      image.Dispose()
      destinationImage.Dispose()
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void StartDitheringExample()
   {
      RasterCodecs codecs = new RasterCodecs();
      // Load an image that has BottomLeft ViewPerspective
      RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1.CMP"));

      RasterColor[] Palette = RasterPalette.Fixed(256);

      // Create the new palletized image.
      RasterImage destinationImage = new RasterImage(RasterMemoryFlags.Conventional, image.Width, image.Height, 8, image.Order, image.ViewPerspective, Palette, IntPtr.Zero, 0);

      // Set the dithering method.
      image.DitheringMethod = RasterDitheringMethod.StevensonArce;

      // Initialize the dithering process.
      image.StartDithering(Palette, 256);

      // Allocate the output buffer for 8-bit data.
      byte[] InBuffer = new byte[image.Width * 3];// Buffer to hold the input row.
      byte[] OutBuffer = new byte[image.Width];// Buffer to hold the output row.

      // Use DitherLine method to process each row in the image.
      for(int i = 0; i < image.Height; i++)
      {
         image.GetRow(i, InBuffer, 0, image.BytesPerLine);
         image.DitherLine(InBuffer, 0, OutBuffer, 0);
         destinationImage.SetRow(i, OutBuffer, 0, destinationImage.BytesPerLine);
      }
      // End the dithering process.
      image.StopDithering();

      codecs.Save(destinationImage, Path.Combine(LEAD_VARS.ImagesDir, "IMAGE1_DitherLine.BMP"), RasterImageFormat.Bmp, 0);

      image.Dispose();
      destinationImage.Dispose();
      codecs.Dispose();
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
RasterImageExamples.prototype.StartDitheringExample = function () {
   Tools.SetLicense();
   with (Leadtools) {
      with (Leadtools.Codecs) {
         var codecs = new RasterCodecs();

         // Load an image that has BottomLeft ViewPerspective
         var srcFileName = "Assets\\Image1.cmp";
         var image;
         var destinationImage;
         return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (loadFile) {
            return codecs.loadAsync(LeadStreamFactory.create(loadFile))
         })
            .then(function (img) {
               image = img;

               var Palette = [RasterColorHelper.create(0, 0, 0),
                  RasterColorHelper.create(255, 255, 255),
                  RasterColorHelper.create(255, 0, 0),
                  RasterColorHelper.create(0, 255, 0),
                  RasterColorHelper.create(0, 0, 255),
                  RasterColorHelper.create(255, 255, 0),
                  RasterColorHelper.create(0, 255, 255),
                  RasterColorHelper.create(255, 0, 255),
                  RasterColorHelper.create(128, 128, 0),
                  RasterColorHelper.create(0, 128, 128),
                  RasterColorHelper.create(128, 128, 128),
                  RasterColorHelper.create(128, 0, 128),
                  RasterColorHelper.create(128, 255, 128),
                  RasterColorHelper.create(128, 255, 255),
                  RasterColorHelper.create(255, 128, 128),
                  RasterColorHelper.create(255, 255, 128)
               ];

               // Create the new palletized image.
               destinationImage = new RasterImage(RasterMemoryFlags.conventional, image.width, image.height, 4, image.order, image.viewPerspective, Palette);

               // Set the dithering method.
               image.ditheringMethod = RasterDitheringMethod.stevensonArce;

               // Initialize the dithering process.
               image.startDithering(Palette, Palette.length);

               // Allocate the output buffer for 8-bit data.
               var InBuffer = new Uint8Array(image.bytesPerLine);// Buffer to hold the input row.
               var OutBuffer = new Uint8Array(destinationImage.bytesPerLine);// Buffer to hold the output row.

               // Use DitherLine method to process each row in the image.
               image.accessData();
               for (var i = 0; i < image.height; i++) {
                  image.getRow(i, InBuffer, 0, image.bytesPerLine);
                  image.ditherLine(InBuffer, OutBuffer);
                  destinationImage.setRow(i, OutBuffer, 0, destinationImage.bytesPerLine);
               }
               // End the dithering process.
               image.stopDithering();
               image.releaseData();

               return Tools.AppLocalFolder().createFileAsync("IMAGE1_DitherLine.BMP")
            })
            .then(function (saveFile) {
               var saveStream = LeadStreamFactory.create(saveFile);
               return codecs.saveAsync(destinationImage, saveStream, RasterImageFormat.bmp, 0)
            })
            .then(function () {

               image.Dispose();
               destinationImage.Dispose();
               codecs.Dispose();
            });
      }
   }
}
[TestMethod]
public async Task StartDitheringExample()
{
   RasterCodecs codecs = new RasterCodecs();
   // Load an image that has BottomLeft ViewPerspective
   string srcFileName = @"Assets\Image1.cmp";
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));
   RasterColor[] Palette = { RasterColorHelper.Create(0,0,0),
                             RasterColorHelper.Create(255,255,255),
                             RasterColorHelper.Create(255,0,0),
                             RasterColorHelper.Create(0,255,0),
                             RasterColorHelper.Create(0,0,255),
                             RasterColorHelper.Create(255,255,0),
                             RasterColorHelper.Create(0,255,255),
                             RasterColorHelper.Create(255,0,255),
                             RasterColorHelper.Create(128,128,0),
                             RasterColorHelper.Create(0,128,128),
                             RasterColorHelper.Create(128,128,128),
                             RasterColorHelper.Create(128,0,128),
                             RasterColorHelper.Create(128,255,128),
                             RasterColorHelper.Create(128,255,255),
                             RasterColorHelper.Create(255,128,128),
                             RasterColorHelper.Create(255,255,128),
                           };

   // Create the new palletized image.
   RasterImage destinationImage = new RasterImage(RasterMemoryFlags.Conventional, image.Width, image.Height, 4, image.Order, image.ViewPerspective, Palette);

   // Set the dithering method.
   image.DitheringMethod = RasterDitheringMethod.StevensonArce;

   // Initialize the dithering process.
   image.StartDithering(Palette, Palette.Length);

   // Allocate the output buffer for 8-bit data.
   byte[] InBuffer = new byte[image.BytesPerLine];// Buffer to hold the input row.
   byte[] OutBuffer = new byte[destinationImage.BytesPerLine];// Buffer to hold the output row.

   // Use DitherLine method to process each row in the image.
   image.AccessData();
   for (int i = 0; i < image.Height; i++)
   {
      image.GetRow(i, InBuffer, 0, image.BytesPerLine);
      image.DitherLine(InBuffer, 0, OutBuffer, 0);
      destinationImage.SetRow(i, OutBuffer, 0, destinationImage.BytesPerLine);
   }
   // End the dithering process.
   image.StopDithering();
   image.ReleaseData();

   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync("IMAGE1_DitherLine.BMP");
   ILeadStream saveStream = LeadStreamFactory.Create(saveFile);
   await codecs.SaveAsync(destinationImage, saveStream, RasterImageFormat.Bmp, 0);

   image.Dispose();
   destinationImage.Dispose();
   codecs.Dispose();
}
public void StartDitheringExample(RasterImage image, Stream destStream)
{
   RasterColor[] Palette = RasterPalette.Fixed(256);
   // Create the new palletized image.
   RasterImage destinationImage = new RasterImage(RasterMemoryFlags.Conventional, image.Width, image.Height, 8, image.Order, image.ViewPerspective, Palette, null, 0);

   // Set the dithering method.
   image.DitheringMethod = RasterDitheringMethod.StevensonArce;

   // Initialize the dithering process.
   image.StartDithering(Palette, 256);

   // Allocate the output buffer for 8-bit data.
   byte[] InBuffer = new byte[image.Width * 3];// Buffer to hold the input row.
   byte[] OutBuffer = new byte[image.Width];// Buffer to hold the output row.

   // Use DitherLine method to process each row in the image.
   for (int i = 0; i < image.Height; i++)
   {
      image.GetRow(i, InBuffer, 0, image.BytesPerLine);
      image.DitherLine(InBuffer, 0, OutBuffer, 0);
      destinationImage.SetRow(i, OutBuffer, 0, destinationImage.BytesPerLine);
   }
   // End the dithering process.
   image.StopDithering();

   RasterCodecs codecs = new RasterCodecs();
   codecs.Save(destinationImage, destStream, RasterImageFormat.Bmp, 0);

   image.Dispose();
   destinationImage.Dispose();
}
Public Sub StartDitheringExample(ByVal image As RasterImage, ByVal destStream As Stream)
   Dim Palette As RasterColor() = RasterPalette.Fixed(256)
   ' Create the new palletized image.
   Dim destinationImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, image.Width, image.Height, 8, image.Order, image.ViewPerspective, Palette, Nothing, 0)

   ' Set the dithering method.
   image.DitheringMethod = RasterDitheringMethod.StevensonArce

   ' Initialize the dithering process.
   image.StartDithering(Palette, 256)

   ' Allocate the output buffer for 8-bit data.
   Dim InBuffer As Byte() = New Byte(image.Width * 3 - 1){} ' Buffer to hold the input row.
   Dim OutBuffer As Byte() = New Byte(image.Width - 1){} ' Buffer to hold the output row.

   ' Use DitherLine method to process each row in the image.
   Dim i As Integer = 0
   Do While i < image.Height
      image.GetRow(i, InBuffer, 0, image.BytesPerLine)
      image.DitherLine(InBuffer, 0, OutBuffer, 0)
      destinationImage.SetRow(i, OutBuffer, 0, destinationImage.BytesPerLine)
      i += 1
   Loop
   ' End the dithering process.
   image.StopDithering()

   Dim codecs As RasterCodecs = New RasterCodecs()
   codecs.Save(destinationImage, destStream, RasterImageFormat.Bmp, 0)

   image.Dispose()
   destinationImage.Dispose()
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterImage Class
RasterImage Members
Introduction to Image Processing With LEADTOOLS
Dithering Methods
Codecs.CodecsThumbnailOptions.DitheringMethod property
ImageProcessing.ColorResolutionCommand.DitheringMethod property
RasterDitheringMethod enumeration
RasterDefaults.DitheringMethod property
RasterImage.DitheringMethod property
RasterImage.DitherLine method
RasterImage.StopDithering method
RasterBufferConverter.Convert

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.