Skip to main content

Force a read into memory, can sometimes be very, very slow

3 replies [Last post]
newmanw10
Offline
Joined: 2009-08-17
Points: 0

I am using JAI to read, scale, and crop a lot of images and place them into a mosaic

Due to file descriptor problems I am trying to read them into memory (I think this is called a 'sink') the scaled, and cropped image. I am just calling the RenderedOp's getAsBufferedImage() method so that I can call dispose on my ImageReader and free up the file descriptor.

List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
for (MyImage myImages : myImages) {  // this could be thousands of images
  ImageReader reader = // create reader
  RenderedOp image = // get image from reader
  image = JAI.create("scale", //scale so the image is much smaller and all images will fit into memory
  image = JAI.create("crop", //
  BufferedImage bufferedImage = image.getAsBufferedImage();  // read into memory
  bufferedImages.add(bufferedImage);
  reader.dispose(); // dispose reader so the resources including file descriptor can be freed
}

// then mosaic all the buffered images together

Is this the correct way to go about this?

When I have a lot of images and I scale each indiviual image before reading it into memory things work very well. My problem comes when I only have a few images (say 2) and I don't scale those images very small (say 90% of their orginal size). When I call getAsBufferedImage() it takes quit a long time, i.e. 2 minutes. Is there something fundamental that I am doing wrong.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rgd
Offline
Joined: 2005-08-23
Points: 0

That's odd! I don't see anything obviously wrong with your approach.
Are the input images very large? I don't see why reading just 2 would
be particularly slow. And 2 minutes is definitely slow, unless the
image is really, really big.

Are you disabling the tile cache? If the inputs were tiled and the
cache was disabled it might thrash the reads in order to handle tile
boundary conditions, which could take longer. But the same thing should
happen with all the images.

You might try cropping first before scaling, that might let the scale do
less work if you e.g. crop off an entire tile.

-Bob

On 10/6/11 10:06 AM, forums@java.net wrote:
> I am using JAI to read, scale, and crop a lot of images and place them into
> a mosaic
>
> Due to file descriptor problems I am trying to read them into memory (I
> think this is called a 'sink') the scaled, and cropped image. I am just
> calling the RenderedOp's getAsBufferedImage() method so that I can call
> dispose on my ImageReader and free up the file descriptor.
>
> List bufferedImages = new ArrayList(); for
> (MyImage myImages : myImages) { // this could be thousands of images
> ImageReader reader = // create reader RenderedOp image = // get image from
> reader image = JAI.create("scale", //scale so the image is much smaller and
> all images will fit into memory image = JAI.create("crop", // BufferedImage
> bufferedImage = image.getAsBufferedImage(); // read into memory
> bufferedImages.add(bufferedImage); reader.dispose(); // dispose reader
> so the
> resources including file descriptor can be freed } // then mosaic all the
> buffered images together
> Is this the correct way to go about this?
>
> When I have a lot of images and I scale each indiviual image before reading
> it into memory things work very well. My problem comes when I only have a
> few images (say 2) and I don't scale those images very small (say 90% of
> their orginal size). When I call getAsBufferedImage() it takes quit a long
> time, i.e. 2 minutes. Is there something fundamental that I am doing
> wrong.
>
> Thanks
>
>
>
>

erunquist
Offline
Joined: 2009-05-01
Points: 0

You could try reading the image with JAI instead of ImageIO:

ParameterBlock paramBlock = new ParameterBlock();
paramBlock.add(file.getAbsolutePath());

RenderedOp renderedOp = JAI.create("fileload", paramBlock);

This may change the performance characteristics. Maybe for the better...

newmanw10
Offline
Joined: 2009-08-17
Points: 0

To make this more difficult I am using imageio-ext to read in DTED images. When I try and use the 'fileload' descriptor it get exceptions. My guess is that JAI does not know how to decode this image. Is it still possible to use the 'fileload' descriptor in this case.

Thanks.