Free Support Forum - aspose.com

Tiff resize operation uses more than 3g memory

Hi all,14228601.zip (52.5 KB)

I have a problem with OutOfMemory when doing resize for big images like 14228601.tif in the attachment. It looks that the process uses more than 3g then doing image.resize((int) (image.getWidth() * yScale), (int) (image.getHeight() * xScale), resizeType); operation.
Below is my code. Please, advice, how could I reduce the memory usage?

I am using Aspose.Imaging for Java 20.9.

public static void resizeTiff2(int size){
String dataDir = “F:\_PROJECTS\TIFF\”;
int pageIndex = 1;

    //LoadOptions options = new LoadOptions();
    //options.setBufferSizeHint(50);
   // try(TiffImage multiImage = (TiffImage)com.aspose.imaging.Image.load(dataDir + "03857395.tif"/*, options*/))
    try(TiffImage multiImage = (TiffImage)com.aspose.imaging.Image.load(dataDir + "14228601.TIF"))
  //  try(TiffImage multiImage = (TiffImage)com.aspose.imaging.Image.load(dataDir + "00000090.tif"))
    {
        int resUnit = 1;
        float xRes = (float) multiImage.getHorizontalResolution();
        float yRes = (float) multiImage.getVerticalResolution();
        System.out.println("height: " + String.valueOf(multiImage.getHeight()));
        System.out.println("wigth: " + String.valueOf(multiImage.getWidth()));

        try(TiffOptions tiffOptions = multiImage.getFrames()[0].getFrameOptions()) {
            // A TIFF can have different vertical and horizontal resolutions
            // which we'll need to account for when scaling.
            resUnit = tiffOptions.getResolutionUnit();
            switch (resUnit) {
                case 2: // inches
                    break;
                case 3: // centimeters
                    xRes *= 2.54;
                    yRes *= 2.54;
                    break;
                default: // none
                    xRes = yRes = 300; // TODO DPI as in FastStone (didn't find dpi for this case)
            }
        }

        com.aspose.imaging.Image image = null;
        RenderedImage renderedImage = null;

        try {
            int pageCount = multiImage.getPageCount();

           // int resizeType = com.aspose.imaging.ResizeType.NearestNeighbourResample;
            int resizeType = com.aspose.imaging.ResizeType.BilinearResample;

            if (pageIndex == -1)
                pageIndex = 0;

  		if (pageIndex < pageCount) {
  			image = multiImage.getFrames()[pageIndex];
  		} else {
  			image = multiImage.getFrames()[0];
  		}


            ScaleCalculator sc = new ScaleCalculator( image.getWidth(), image.getHeight(), xRes, yRes );
            sc.calculate( size );

            float xScale = sc.xScale, yScale = sc.yScale;
            System.out.println( String.format( "Scale (%.6f, %.6f)", xScale, yScale ) );
            
            //float xScale = (float)0.051334;
            //float yScale = (float)0.051334;

           // xScale = yScale = 1f;


            if (!(xScale == 1f && yScale == 1f)) {
                //final long time = System.currentTimeMillis();
                // modify the image resolutions
                xRes *= xScale;
                yRes *= yScale;


                final long time = System.currentTimeMillis();
                image.resize((int) (image.getWidth() * yScale), (int) (image.getHeight() * xScale), resizeType);
                System.out.println("Resize TIFF image using in " + (System.currentTimeMillis() - time) + " milliseconds");
            }

           // renderedImage = ((RasterCachedImage)image).toBitmap();
           // System.out.println(String.format("source image: width = %s",
           //         renderedImage.toString()));


         //   image.save(dataDir + "test" + size + ".PNG" , new com.aspose.imaging.imageoptions.PngOptions());

        }
        finally {
            if(image != null) {
                image.dispose();
            }
          
        }

@Togi

The amount of memory consumed is dependent on type and size of file you are trying to load along with rendering operating performed. You can consider increasing the Java Heap Size on your end and see which size does not throw issue. There is no mechanism in API to reduce the memory requirement while loading any image.