Atalasoft Knowledge Base

HOWTO: Use TiffFile to Resample FAX images with Unequal Resolutions in a Multi-Page TIFF


Many FAX images are stored as 1-bit TIFF's with an X resolution that is greater than Y resolution. When viewing these images without compensating for the resolution, the image appears squashed. That's because the actual pixel aspect ratio is not square, it's rectangular. The Atalasoft viewer controls will compensate for the resolution and view these images as they are intended, but some operations do not yield desirable results, such as drawing lines or text on these images, or sending the image to our barcode reader. The code below demonstrates how to find any page in a multipage TIFF that is one of these images, and correct it, without the need to re-encode the entire TIFF. This is especially important for TIFF images with many pages. Here is the code, which is compiled as a simple console app. To run it just call the application with the first argument as the input file name, and the second argument as the output file name.

NOTE: The following code is intended as an example of processing every page on a TIFF file, and can not be used on other file types. For a general how-to for a single AtalaImage (regardless of file type), please see:
HOWTO: Fix Non-square Resolution of a Single AtalaImage

Example Code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using Atalasoft.Imaging;
using Atalasoft.Imaging.Codec;
using Atalasoft.Imaging.Codec.Tiff;
using Atalasoft.Imaging.ImageProcessing;
using Atalasoft.Imaging.Metadata;

namespace FixMixedTiffResolution
    class Program
        static void Main(string[] args)
            string filename = args[0];
            string outputfile = args[1];
            using (FileStream fs = File.OpenRead(filename))
                //now open a TIFF file since we want to replace the TIFF,
                //only re-encoding if necessary
                TiffFile tfile = new TiffFile();
                for (int i = 0; i < tfile.Images.Count; i++)
                    TiffDirectory ifd = tfile.Images[i];
                    //get the resolution info from the file
                    TiffTag xresTag = ifd.Tags.LookupTag(TiffTagID.XResolution);
                    TiffTag yresTag = ifd.Tags.LookupTag(TiffTagID.YResolution);
                    Rational xres = (Rational)xresTag.Data;
                    Rational yres = (Rational)yresTag.Data;
                    if (xres.Numerator / xres.Denominator != yres.Numerator / yres.Denominator)
                        //x and y resolutions differ
                        int bits = Convert.ToInt32(ifd.Tags.LookupTag(TiffTagID.BitsPerSample).Data);
                        if (bits == 1)
                            //this is a mixed resolution FAX image
                            //decode the image data from a new stream
                            AtalaImage image = new AtalaImage(filename, i, null);
                            //resize the image by the ratio of DPI
                            ResampleCommand resize = new ResampleCommand(new Size(image.Width, Convert.ToInt32(image.Height * ((double)(xres.Numerator / xres.Denominator) / (double)(yres.Numerator / yres.Denominator)))));
                            AtalaImage newImage = resize.Apply(image).Image;
                            //set the resolutions equal
                            newImage.Resolution = new Dpi(image.Resolution.X, image.Resolution.X, image.Resolution.Units);
                            //replace the image with the resized one
                            tfile.Images.Insert(i, new TiffDirectory(newImage));
                //this save will only re-encode the images that were resampled

Original Article:
Q10153 - HOWTO: Using TiffFile to resample FAX images with unequal resolutions in a multi-page TIFF

Last Modified: 5 Months Ago
Last Modified By: Administrator
Article not rated yet.
Article has been viewed 177 times.
Also In This Category