Skip to main content

compress without reading into memory

5 replies [Last post]
bokken
Offline
Joined: 2010-05-21
Points: 0

Is there a way to compress images without having to read entire image into memory? Basically, I would like to provide an ImageInputStream (and any required metadata on how to process content) to a ImageWriter. It appears to me that the approach of read into BufferedImage or Raster limits raw (uncompressed) size of images which can be processed.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
broumbroum
Offline
Joined: 2010-04-24
Points: 0

Hi !
It is actually possible to do this trick with TIFF image ONLY. Then you should consider NOT to compress the TIFF file, as insert,sourceRegion, replace, etc.would fail.
Answer to the question you put as the title is definitely NO. ;)
Bye.

bokken
Offline
Joined: 2010-05-21
Points: 0

If the original data is uncompressed, the RawImageReader offers a readAsRenderedImage method that returns a RenderedImage that simply wraps the ImageInputStream. That can be wrapped by an IIOImage and used as input to an ImageWriter.

Harald Kuhr

Hi bokken,

On 12. okt. 2010, at 00.25, jai-imageio@javadesktop.org wrote:

> Is there a way to compress images without having to read entire image into memory? Basically, I would like to provide an ImageInputStream (and any required metadata on how to process content) to a ImageWriter. It appears to me that the approach of read into BufferedImage or Raster limits raw (uncompressed) size of images which can be processed.

I guess it should be possible to do something like this using ImageIO (in theory, never tried it, so I have no idea if it actually works):

- query the ImageReader for image dimensions
- call prepareInsertEmpty/endInsertEmpty with the found dimensions on the ImageWriter
- create a region/raster, say one scan line
- for each scan line/region in the source image
- read into raster
- call replacePixels on the ImageWriter for this region

Of course, this assumes that the ImageWriter supports insertEmpty and replacePixels, which are both optional operations. Not sure how common that is.

Another option that could work is something a did some research on a couple of months back, using BufferedImages with file-backed Rasters/DataBuffers. While it would not save you the time of decoding the image, it will take away the (JVM heap) memory constraint. I have a PoC using NIO memory mapped files on GitHub [1]. I have successfully created images with sizes up to around 2GB using this approach. Feel free to use it as is, or as a starting point for your own solution.

As you don't display the images, you might even get away without using memory mapped files, but expect performance to degrade...

[1]: http://github.com/haraldk/TwelveMonkeys/blob/master/sandbox/sandbox-comm...

Best regards,

--
Harald K

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

bokken
Offline
Joined: 2010-05-21
Points: 0

That does look very interesting. What are the license terms for commercial use?
Are there end-states available anywhere (preferably maven repo)?

Harald Kuhr

On 13. okt. 2010, at 14.49, jai-imageio@javadesktop.org wrote:

> That does look very interesting. What are the license terms for commercial use?

Everything in the repo is currently under BSD license. So feel free.

> Are there end-states available anywhere (preferably maven repo)?

Not the sandbox, as there's no releases yet. Could be done I guess...

--
Harald K
---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net