Fast loading a PNG image into an array in java

I want to download (and decode) PNG images and convert them to a one-dimensional array in Java. I can obviously do this with ImageIO.read () and then copy the pixels to an array, but that will double the amount of memory (raster + final array) and require more processing time than I would like.

How should I do it? In the worst case scenario, I can independently implement the PNG specification, but this seems like a pretty complicated task. Ideally, I would like to implement PNG into which I can "connect". Less ideal, but still good, it would be easy to understand (as opposed to com.sun code) a PNG reader that I can (and will be allowed to) modify.

+3
source share
3 answers

If you are trying to get the pixel data as an array, you can use ImageIO.read()to get BufferedImage, and then use BufferedImage.getRaster().getDataBuffer()to get DataBuffer. From there you need to check what type BufferedImageyou have in order to determine how to distinguish DataBuffer. For instance. if you have an image TYPE_INT_RGB, then you must overlay on DataBufferInt, and then you can call DataBufferInt.getData()to retrieve int[]that contains the data. What you get in this way is not a copy --- this is the actual array supporting it BufferedImage.

, : ImageIO.read() , . , TYPE_CUSTOM, , , , .

, ImageIO.read(), , , ImageIO.read() , . , ImageIO, ImageIO.getImageReaders(), Iterator , , , ImageReader.getImageTypes(0) Iterator , . , , , .

, . , , BufferedImage.getRGB() ( , GZIPOutputStream), BufferedImage , , , BufferedImage.setRGB(). ( -, , , . LGPL'd .)

+8

1D-? ?

.

PNG (ImageIO.read() BufferedImage, ), ... 1D- ( BufferedImage.getRaster().getDataBuffer()).

+3

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


All Articles