3 Replies Latest reply: Nov 17, 2010 12:10 AM by Maxideon RSS

    Resizing a TIFF File: Overwriting TIFFFields does not have any effect

    810030
      Hi,

      I have some TIFF files that I am receiving through a FAX server. Some of the TIFF files have an image length of *1077 pixels and a DPI of 200x98*. These images open fine in generic viewers like Microsoft Image Viewer and IrfanView and the image size in the information dialog shows up fine (1752x2158). But when I open the images in a LeadTools viewer (that works off TIFF Header tags), the image appears stretched out.

      I am trying to re-sample the image to make it a true Letter size image (1700x2200) with resolution of 200x200. I have been able to set the TAG_X_RESOLUTION and TAG_Y_RESOLUTION which I can see changed in the Tag Viewer. But changing the following tags does not have any effect on the resulting image:

      *
      TAG_IMAGE_WIDTH
      TAG_IMAGE_LENGTH
      TAG_ROWS_PER_STRIP
      *

      the following is the code I am using, I have tried all possible ways (removing TIFFFeilds and then adding them), but it has no effect. The last options is to use a Print Driver from within Java and Print the image (that re-samples it into a 8.5x11 inch image with 200 DPI). At this point, I am just curious about writing TIFFFields with images. Any ideas are appreciated:

      Thanks,
      Manuj

      +
      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.util.Iterator;

      import javax.imageio.IIOImage;
      import javax.imageio.ImageIO;
      import javax.imageio.ImageReader;
      import javax.imageio.ImageWriter;
      import javax.imageio.metadata.IIOMetadata;
      import javax.imageio.stream.ImageInputStream;
      import javax.imageio.stream.ImageOutputStream;

      import org.w3c.dom.NamedNodeMap;
      import org.w3c.dom.Node;

      import com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet;
      import com.sun.media.imageio.plugins.tiff.TIFFDirectory;
      import com.sun.media.imageio.plugins.tiff.TIFFField;
      import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;
      import com.sun.media.imageio.plugins.tiff.TIFFTag;
      import com.sun.media.imageioimpl.plugins.tiff.TIFFT6Compressor;

      .....

                     //set the input stream for to the reader
                     tiffFileReader.setInput(tiffFileInputStream);     
                     
                     //define the writer
                     ImageWriter tiffWriter = (ImageWriter) ImageIO.getImageWritersByMIMEType("image/tiff").next();

                     //define the writer param with compression;
                     TIFFImageWriteParam writeParam = (TIFFImageWriteParam)tiffWriter.getDefaultWriteParam();
                     TIFFT6Compressor compressor = new TIFFT6Compressor();
                     writeParam.setCompressionMode(TIFFImageWriteParam.MODE_EXPLICIT);
                     writeParam.setCompressionType(compressor.getCompressionType());
                     
                     writeParam.setTIFFCompressor(compressor);
                     writeParam.setCompressionQuality(Float.parseFloat("1"));


      // get the metaData
                     IIOMetadata imageMetadata = null;
                     IIOImage testImage = null;

                     for(int i=0;i<filePageCount;i++)
                     {
                          imageMetadata = tiffFileReader.getImageMetadata(i);
                     
                          TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
                          
                // Get {X,Y}Resolution tags.
                BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
                TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
                TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
                
                TIFFTag tagImageWidth = base.getTag(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
      TIFFTag tagImageLength = base.getTag(BaselineTIFFTagSet.TAG_IMAGE_LENGTH);
                
                
                TIFFTag tagRowsPerStrip = base.getTag(BaselineTIFFTagSet.TAG_ROWS_PER_STRIP);
                TIFFField fieldRowsPerStrip = new TIFFField(tagRowsPerStrip, TIFFTag.TIFF_SHORT, 1, (Object)new char[]{2200});
                
                // Create {X,Y}Resolution fields.
                TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL,1, new long[][] {{200, 1}});
                TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL,1, new long[][] {{200, 1}});
                
                // Create Width/Height fields.
                TIFFField fieldImageWidth = new TIFFField(tagImageWidth,TIFFTag.TIFF_SHORT,1, (Object)new char[]{1728});
                TIFFField fieldImageLength = new TIFFField(tagImageLength, TIFFTag.TIFF_SHORT,1, (Object)new char[]{2200});
                
                //TIFFTag imageLengthTag = fieldImageLength.getTag();
                
                // Append {X,Y}Resolution fields to directory.
                dir.addTIFFField(fieldXRes);
                dir.addTIFFField(fieldYRes);
                
                //add Image Length and height parameters
                dir.addTIFFField(fieldImageWidth);
                dir.addTIFFField(fieldImageLength);
                
                
                // dir.removeTIFFField(278);
                dir.addTIFFField(fieldRowsPerStrip);
      .....
      testImage = new IIOImage(tiffFileReader.read(i), null, dir.getAsMetadata());
      +

      The resulting image with this carries the updated DPI values (200x200) but still carries the old values of 1752x1077, the length being exactly half of what Irfan view is showing.

      Edited by: Manuj on Nov 2, 2010 10:48 AM