Need C# function to convert grayscale TIFF to black & white (monochrome/1BPP) TIFF

前端 未结 7 1693
清歌不尽
清歌不尽 2021-01-14 09:18

I need a C# function that will take a Byte[] of an 8 bit grayscale TIFF, and return a Byte[] of a 1 bit (black & white) TIFF.

I\'m fairly new to working with TIF

7条回答
  •  心在旅途
    2021-01-14 09:52

    My company's product, dotImage, will do this.

    Given an image, you can convert from multi-bit to single bit using several methods including simple threshold, global threshold, local threshold, adaptive threshold, dithering (ordered and Floyd Steinberg), and dynamic threshold. The right choice depends on the type of the input image (document, image, graph).

    The typical code looks like this:

    AtalaImage image = new AtalaImage("path-to-tiff", null);
    ImageCommand threshold = SomeFactoryToConstructAThresholdCommand();
    AtalaImage finalImage = threshold.Apply(image).Image;
    

    SomeFactoryToConstructAThresholdCommand() is a method that will return a new command that will process the image. It could be as simple as

    return new DynamicThresholdCommand();
    

    or

    return new GlobalThresholdCommand();
    

    And generally speaking, if you're looking to convert an entire multi-page tiff to black and white, you would do something like this:

    // open a sequence of images
    FileSystemImageSource source = new FileSystemImageSource("path-to-tiff", true);
    
    using (FileStream outstm = new FileStream("outputpath", FileMode.Create)) {
        // make an encoder and a threshold command
        TiffEncoder encoder = new TiffEncoder(TiffCompression.Auto, true);
        // dynamic is good for documents -- needs the DocumentImaging SDK
        ImageCommand threshold = new DynamicThreshold();
    
        while (source.HasMoreImages()) {
            // get next image
            AtalaImage image = source.AcquireNext();
            AtalaImage final = threshold.Apply(image).Image;
            try {
                encoder.Save(outstm, final, null);
            }
            finally {
                // free memory from current image
                final.Dispose();
                // release the source image back to the image source
                source.Release(image);
            }
        }
    }
    

提交回复
热议问题