Convert image formats and resize images in Java

So, let's say I want to transcode PNG to JPEG in Java. An image has extreme resolution, say, for example, 10,000 x 10,000 pixels. Using the "standard" Java Script API Writers and Reader, you need at some point to have a full image decoded in RAM, which takes up a very large amount of RAM (hundreds of MB). I watched other tools do it, and I found that ImageMagick uses disk storage on disk, but it seems too slow for my needs. So I need a real streaming recorder. And with the help of true streaming, I mean reading and processing data using switchers or bunkers, not only transmitting the stream as input, but its full decoding in advance.

Now, firstly, the theory - is it even possible, given the JPEG and PNG algorithms, to do this using streams or say in data bunkers? So there is no need to have a full image encoded in memory (or other storage)? In JPEG compression, the first few steps can be performed in streams, but I believe that Huffman coding should build a whole tree of cost probabilities after quantization, so he needs to analyze the whole image - so the whole image needs to be decoded in advance or somehow on demand by region.

And the golden question is, if the above can be achieved, is there any Java library that can really work that way? And save a lot of RAM?

+4
source share
3 answers

If I create a 10,000 x 10,000 PNG file full of incompressible noise, with ImageMagick like this:

convert -size 10000x10000 xc:gray +noise random image.png

I see that ImageMagick uses 675M RAM to create the resulting 572MB file.

I can convert it to JPEG using the vipsfollowing:

vips im_copy image.png output.jpg

and vipswhen converting, it uses no more than 100 MB of RAM and takes 7 seconds on a reasonable iMac specification for about 4 years - albeit with an SSD.

+1
source

I wrote a PNG encoder / decoder that does this (progressively read and write what is only required to store a string in memory) for the PNG format: PNGJ

I don’t know if there is something similar with JPEG

0
source

, ​​. , . -. PNG GIF . JPEG ( ). TIFF . BMP . PSD . Etc.

- , , , (, , , , seek )... () , , (, JPEG , ).

If you can live with a disk buffer, although as a second best option, I created several classes that allow BufferedImagenio to be supported MappedByteBuffer(file with memory mapping Buffers, sort of like virtual memory). Although the performance is not really similar to the images in memory, it is also not entirely useless. Take a look MappedImageFactoryand MappedFileBuffer.

0
source

Source: https://habr.com/ru/post/1584017/


All Articles