Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
GrayscaleCommand Class
See Also  Members   Example 
Leadtools.ImageProcessing Namespace : GrayscaleCommand Class



Converts a 1-, 4-, 8-, 16-, 24-, or 32-bit image to an 8-bit, 12-bit, or 16-bit grayscale image.

Syntax

Visual Basic (Declaration)  
Public Class GrayscaleCommand 
   Inherits RasterCommand
   Implements IRasterCommand 
Visual Basic (Usage) Copy Code
Dim instance As GrayscaleCommand
C#  
public class GrayscaleCommand : RasterCommand, IRasterCommand  
C++/CLI  
public ref class GrayscaleCommand : public RasterCommand, IRasterCommand  

Example

This example will load a 24-bit image then convert it to a grayscale image with 8, 12 and 16 bits/pixel

Visual Basic Copy Code
Public Sub GrayscaleCommandExample()
 RasterCodecs.Startup()
 Dim codecs As RasterCodecs = New RasterCodecs()

 Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
 Dim destFileName8 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale8.jpg"
 Dim destFileName12 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale12.jpg"
 Dim destFileName16 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale16.jpg"

 ' Load the source image from disk as 24-bits/pixel
 Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)

 ' Check the grayscale mode
 Console.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode)

 ' Convert to 8-bit grayscale
 Dim command As GrayscaleCommand = New GrayscaleCommand()
 command.BitsPerPixel = 8
 command.Run(image)

 ' Check the grayscale mode
 Console.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode)

 ' Save it to disk

 codecs.Options.Jpeg.Save.QualityFactor = 2
 codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8)
 image.Dispose()

 ' Load the image again this time grayscale to 12-bits/pixel
 image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
 command.BitsPerPixel = 12
 command.Run(image)
 Console.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode)
 codecs.Options.Jpeg.Save.QualityFactor = 2
 codecs.Save(image, destFileName12, RasterImageFormat.Jpeg, 12)
 image.Dispose()

 ' Load the image again this time grayscale to 16-bits/pixel
 image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
 command.BitsPerPixel = 16
 command.Run(image)
 Console.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode)

 ' When saving 16-bit jpegs, you must use lossless quality factor
 codecs.Options.Jpeg.Save.QualityFactor = 0
 codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16)
 image.Dispose()

 ' Clean Up
 image.Dispose()
 RasterCodecs.Shutdown()
      End Sub
C# Copy Code
public void GrayscaleCommandExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string destFileName8 = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale8.jpg"; 
   string destFileName12 = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale12.jpg"; 
   string destFileName16 = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_grayscale16.jpg"; 
 
   // Load the source image from disk as 24-bits/pixel 
   RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
 
   // Check the grayscale mode 
   Console.WriteLine("Grsyscale mode of original image = {0}", image.GrayscaleMode); 
 
   // Convert to 8-bit grayscale 
   GrayscaleCommand command = new GrayscaleCommand(); 
   command.BitsPerPixel = 8; 
   command.Run(image); 
 
   // Check the grayscale mode 
   Console.WriteLine("Grsyscale mode after grayscale command with 8 bpp = {0}", image.GrayscaleMode); 
 
   // Save it to disk 
 
   codecs.Options.Jpeg.Save.QualityFactor = 2; 
   codecs.Save(image, destFileName8, RasterImageFormat.Jpeg, 8); 
   image.Dispose(); 
 
   // Load the image again this time grayscale to 12-bits/pixel 
   image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
   command.BitsPerPixel = 12; 
   command.Run(image); 
   Console.WriteLine("Grsyscale mode after grayscale command with 12 bpp = {0}", image.GrayscaleMode); 
   codecs.Options.Jpeg.Save.QualityFactor = 2; 
   codecs.Save(image, destFileName12, RasterImageFormat.Jpeg, 12); 
   image.Dispose(); 
 
   // Load the image again this time grayscale to 16-bits/pixel 
   image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
   command.BitsPerPixel = 16; 
   command.Run(image); 
   Console.WriteLine("Grsyscale mode after grayscale command with 16 bpp = {0}", image.GrayscaleMode); 
 
   // When saving 16-bit jpegs, you must use lossless quality factor 
   codecs.Options.Jpeg.Save.QualityFactor = 0; 
   codecs.Save(image, destFileName16, RasterImageFormat.Jpeg, 16); 
   image.Dispose(); 
 
   // Clean Up 
   image.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

Support for 12 and 16-bit grayscale images is available only in the Document/Medical Imaging editions.

The resulting image can be an 8-bit, 12-bit, or 16-bit grayscale image. Once the function is complete, the RasterImage.GrayscaleMode property will indicate the type of grayscale image.

When converting to 12-bit or 16-bit grayscale, the RasterImage.GetLookupTable is not used. When converting to 8-bit grayscale, the RasterImage.GetLookupTable is used to get the RGB for each input pixel. The grayscale value corresponding to that RGB triple is used in the destination image.

This method does not support signed images.

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

For more information, refer to Grayscale Images.

Inheritance Hierarchy

System.Object
   Leadtools.ImageProcessing.RasterCommand
      Leadtools.ImageProcessing.GrayscaleCommand

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also