tags:

views:

596

answers:

4

I'm trying to convert a multipage color tif file to a c# CompressionCCITT3 tif in C#. I realize that I need to make sure that all pixels are 1 bit. I have not found a useful example of this online.

thanks.

+1  A: 

Check out: http://bobpowell.net/onebit.htm

You need this conversion as CCITT3 and CCITT4 don't support color (if I remember right).

jasonlaflair
A: 

I saw the above code, and it looked like it was converting every pixel with manual logic.

Would this work for you?

Imports System.Drawing.Imaging

'get the color tif file

Dim bmpColorTIF As New Bitmap("C:\color.tif")

'select the an area of the tif (will grab all frames)

Dim rectColorTIF As New Rectangle(0, 0, bmpColorTIF.Width, bmpColorTIF.Height )

'clone the rectangle as 1-bit color tif

Dim bmpBlackWhiteTIF As Bitmap = bmpColorTIF.Clone(rectColorTIF, PixelFormat.Format1bppIndexed)

'do what you want with the new bitmap (save, etc)

...

Note: there are a ton of pixelformats to choose from.

+1  A: 

Pimping disclaimer: I work for Atalasoft, a company that makes .NET imaging software.

Using dotImage, this task becomes something like this:

FileSystemImageSource source = new FileSystemImageSource("path-to-your-file.tif", true); // true = loop over all frames
// tiff encoder will auto-select an appropriate compression - CCITT4 for 1 bit.
TiffEncoder encoder = new TiffEncoder();
encoder.Append = true;

// DynamicThresholdCommand is very good for documents.  For pictures, use DitherCommand
DynamicThresholdCommand threshold = new DynamicThresholdCommand();

using (FileStream outstm = new FileStream("path-to-output.tif", FileMode.Create)) {
    while (source.HasMoreImages()) {
        AtalaImage image = source.AcquireNext();
        AtalaImage finalImage = image;
        // convert when needed.
        if (image.PixelFormat != PixelFormat.Pixel1bppIndexed) {
            finalImage = threshold.Apply().Image;
        }
        encoder.Save(outstm, finalImage, null);
        if (finalImage != image) {
            finalImage.Dispose();
        }
        source.Release(image);
    }
}

The Bob Powell example is good, as far as it goes, but it has a number of problems, not the least of which is that it's using a simple threshold, which is terrific if you want speed and don't actually care what your output looks like or your input domain is such that really is pretty much black and white already - just represented in color. Binarization is a tricky problem. When your task is to reduce available information by 1/24th, how to keep the right information and throw away the rest is a challenge. DotImage has six different tools (IIRC) for binarization. SimpleThreshold is bottom of the barrel, from my point of view.

plinth
A: 

I suggest to experiment with the desired results first using tiff and image utilities before diving into the coding. I found VIPS to be a handy tool. The next option is to look into what LibTIFF can do. I've had good results with the free LibTiff.NET using c# (see also stackoverflow). I was very disappointed by the GDI tiff functionality, although your milage may vary (I need the missing 16-bit-grayscale). Also you can use the LibTiff utilities (i.e. see http://www.libtiff.org/man/tiffcp.1.html)

Adriaan