2 Replies Latest reply: Jul 14, 2010 2:03 PM by 843802 RSS

    How to reduce tiff size

    843802
      Hi Friends,

      I am not able to open certain tiff images in My Software, So tried to change the compression mode of tiff image. It worked fine but the problem is, it increases the size of the tiff. Is there any way to reduce the size in java with out affecting the quality of the tiff image?

      {color:#ff0000}*Note: I tried PackBits compression since my software allows only that.*{color}

      Here is my code:
      {color:#808080}TIFFEncodeParam param = new TIFFEncodeParam();
      
                  TIFFField[] extras = new TIFFField[2];
                  extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 200, (long) 1 }, { (long) 0, (long) 0 } });
                  extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 200, (long) 1 }, { (long) 0, (long) 0 } });
                  param.setExtraFields(extras);
                  param.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
                  param.setReverseFillOrder(val);
                  param.setJPEGCompressRGBToYCbCr(true);
                  RenderedImage image[]=     readMultiPageTiff(inputFileName);
                  Vector vector = new Vector();
                  if(image != null)
                  {
                      OutputStream outStr = new FileOutputStream(outputFileName);
                      BufferedOutputStream bos = new BufferedOutputStream(outStr);
                      for (int j = 1; j < image.length; j++) 
                      {
                           vector.add(image[j]);
                      }
                      param.setExtraImages(vector.iterator());
                      try 
                      {
                          ImageEncoder encImage = ImageCodec.createImageEncoder("TIFF", bos, param);
                          if(encImage == null)
                          {
                              throw new IOException("errror");
                          }
                          System.out.println("pages length "+image.length);
                          encImage.encode(image[0]);
                          System.out.println("File is created in the folder " + outputFileName);
                      } 
                      catch (Exception e) 
                      {
                          e.printStackTrace();
                          return false;
                      }
      
                      bos.flush();
                      bos.close();
                      isCompressed=true;
                  }//If
                  ss.close();TIFFEncodeParam param = new TIFFEncodeParam();
      
                  TIFFField[] extras = new TIFFField[2];
                  extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 200, (long) 1 }, { (long) 0, (long) 0 } });
                  extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] { { (long) 200, (long) 1 }, { (long) 0, (long) 0 } });
                  param.setExtraFields(extras);
                  param.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
                  param.setReverseFillOrder(val);
                  param.setJPEGCompressRGBToYCbCr(true);
                  RenderedImage image[]=     readMultiPageTiff(inputFileName);
                  Vector vector = new Vector();
                  if(image != null)
                  {
                      OutputStream outStr = new FileOutputStream(outputFileName);
                      BufferedOutputStream bos = new BufferedOutputStream(outStr);
                      for (int j = 1; j < image.length; j++) 
                      {
                           vector.add(image[j]);
                      }
                      param.setExtraImages(vector.iterator());
                      try 
                      {
                          ImageEncoder encImage = ImageCodec.createImageEncoder("TIFF", bos, param);
                          if(encImage == null)
                          {
                              throw new IOException("errror");
                          }
                          System.out.println("pages length "+image.length);
                          encImage.encode(image[0]);
                          System.out.println("File is created in the folder " + outputFileName);
                      } 
                      catch (Exception e) 
                      {
                          e.printStackTrace();
                          return false;
                      }
      
                      bos.flush();
                      bos.close();
                      isCompressed=true;
                  }//If
                  ss.close(); {color}
        • 1. Re: How to reduce tiff size
          843802
          TIFF's have a conservative compression algorithm, you can set the compression as high as you like and it will not lose any data.
          • 2. Re: How to reduce tiff size
            843802
            PackBits yields fairly large image sizes as all it does is run length encoding. If your software only allows packbits compression then by implication you are stuck with large tiff files. So are you sure your software only allows packbits?

            I would think any software that can open a tiff file should be able to handle LZW compression as well. LZW would be a better choice over packbits. And if your images are binary, then you should use one of the CCITT compressions.