user1321402
user1321402

Reputation: 1

Java heap space error when creating TIFF image

As I am new to TIFF handling with JAI, I am trying to create an RGB TIFF image of 6000*6000 with float data. Actually the code works for 5000*5000 image but when I increase the size, I am getting a Java heap space error at line tiledImage.setData(pattern);

Please tell me whether is it correct way to create RGB/Multi band RIFF image with tiling concept. Or is there any way to create?

The error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.awt.image.DataBufferFloat.<init>(DataBufferFloat.java:53)
        at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:409)
        at javax.media.jai.RecyclingTileFactory.createTile(RecyclingTileFactory.java:397)
        at javax.media.jai.PlanarImage.createWritableRaster(PlanarImage.java:1995)
        at javax.media.jai.TiledImage.createTile(TiledImage.java:998)
        at javax.media.jai.TiledImage.getWritableTile(TiledImage.java:1118)
        at javax.media.jai.TiledImage.setData(TiledImage.java:1241)
        at javaapplication17.Test3.main(Test3.java:69)
Java Result: 1

The code:

public class Test3 {

    public static void main(String a[]) {
        int imageHeight = 6000;
        int imageWidth = 6000;
        WritableRaster raster;
        int[] bandOffsets = new int[3];
        bandOffsets[0] = 2;
        bandOffsets[1] = 1;
        bandOffsets[2] = 0;
        PixelInterleavedSampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, imageWidth, imageHeight, 3, 3 * 6000, bandOffsets);
// Origin is 0,0.
        WritableRaster pattern = Raster.createWritableRaster(sm, new Point(0, 0));
        float[] bandValues = new float[3];
        bandValues[0] = 90;
        bandValues[1] = 45;
        bandValues[2] = 45;
       // Set values for the pattern raster.
        for (int y = 0; y < pattern.getHeight(); y++) {
            for (int x = 0; x < pattern.getWidth(); x++) {
                pattern.setPixel(x, y, bandValues);
                bandValues[1] = (bandValues[1] + 1) % 255;
                bandValues[2] = (bandValues[2] + 1) % 255;
            }
        }
        ColorModel colorModel = PlanarImage.createColorModel(sm);

        // Create a TiledImage using the SampleModel.
        TiledImage tiledImage = new TiledImage(0, 0, imageWidth, imageHeight, 0, 0, sm, colorModel);
        // Set the data of the tiled image to be the raster.
        tiledImage.setData(pattern);
        // Save the image on a file.
           JAI.create("filestore", tiledImage, "rgbpattern_test4.tif", "TIFF");
    }
}

Upvotes: 0

Views: 919

Answers (1)

Stephen C
Stephen C

Reputation: 719336

The simple solution is to increase the heap size using the -Xmx option.

Upvotes: 2

Related Questions