Rishabh Sharma
Rishabh Sharma

Reputation: 31

What is the fastest way to read a large tiff image in java?

I currently use the JAI library to read the tiff image but it is very very slow large tiff images (I need to work with satellite images of size around 1GB). I need to read the height of each point from the tiff image and then color it accordingly.

I am reading the image by creating a PlanarImage and iterating through every pixel by using the image.getData().getPixel(x,y,arr) method.

Suggest me a better way of implementing the solution.

Edit: I found the error.I was creating a new raster of the image for every pixel by calling the image.getData() method in the for loop.Creating a raster just once and then using its getPixel() function in the loop solved my problem.

Upvotes: 1

Views: 1662

Answers (2)

Harald K
Harald K

Reputation: 27094

From the JavaDoc of PlanarImage.getData():

The returned Raster is semantically a copy.

This means that for every pixel of your image, you are creating a copy of the entire image in memory... This cannot give good performance.

Using getTile(x, y) or getTiles() should be faster.

Try:

PlanarImage image;

final int tilesX = image.getNumXTiles();
final int tilesY = image.getNumYTiles();

int[] arr = null;

for (int ty = image.getMinTileY(); ty < tilesY; ty++) {
    for (int tx = startX; tx < image.getMinTileX(); tx++) {
        Raster tile = image.getTile(tx, ty);
        final int w = tile.getWidth();
        final int h = tile.getHeight();

        for (int y = tile.getMinY(); y < h; y++) {
            for (int x = tile.getMinX(); x < w; x++) {
                arr = tile.getPixel(x, y, arr);
                // do stuff with arr
            }
        }
    }
} 

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

A 1 GB compressed image is likely to be about 20 GB+ when loaded into memory. The only way to handle this in Java is to load it with a very large heap space.

You are dealing with very large images and the simplest way to make this faster is to use a faster PC. I suggest an over clocked i7 3960X which you can get for a reasonable price http://www.cpubenchmark.net/high_end_cpus.html

Upvotes: 0

Related Questions