DotImage Knowledgebase

Home : Using TiffFile to resample FAX images with unequal resolutions in a multi-page TIFF
Q10153 - HOWTO: Using 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 desireable 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 filename, and the second argument as the output filename.

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();
                tfile.Read(fs);
                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;
                            image.Dispose();
                            //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.Remove(ifd);
                            tfile.Images.Insert(i, new TiffDirectory(newImage));
                        }
                    }
                }
                //this save will only re-encode the images that were resampled
                tfile.Save(outputfile);
            }
        }
    }
}

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2014. InstantASP Ltd. All Rights Reserved

preload preload preload