Reputation: 17604
Is it possible to change the color space of an existing BufferedImage object without copying all pixels to a new BufferedImage object? I couldn't find a method for that, but maybe I just didn't find it? I would like to change the color space from BufferedImage.TYPE_4BYTE_ABGR to BufferedImage.TYPE_BYTE_BINARY.
Upvotes: 1
Views: 1645
Reputation: 18069
No.
From the Java Doc:
TYPE_4BYTE_ABGR
Represents an image with 8-bit RGBA color components with the colors Blue, Green, and Red stored in 3 bytes and 1 byte of alpha.
TYPE_BYTE_BINARY
Represents an opaque byte-packed 1, 2, or 4 bit image.
The size of the pixels are different - 4B vs 1B. Even if it is possible to modify the image's metadata in the Java structure object, since the pixel size is different this shouldn't work in place(*).
(*) The new buffer is smaller, so theoretically it should be possible to reuse the memory by creating a new image that contains the same buffer (however not fully used).
You will still need to write your own loop which iterates the pixels in the order of placement in the buffer, modifies their color space (ARGB->Intensity), and writes them into the place that source pixel /4 was in.
Try to use this constructor to reuse the buffer: BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties)
Upvotes: 2