How to decide which type of BufferedImage image to use?

The Java class BufferedImage has a long list of class variables, known as the image type, which can be used as an argument to the BufferedImage constructor.

However, Java docs provided little explanation of why these types of images are used and how this will affect the generated BufferedImage.

My question is:

  • How would the type of image influence the creation of a BufferedImage? Does it control the number of bits used to store different colors (red, green, blue) and transparency?

  • What type of image should we use if we just want to create

    • opaque image
    • transparent image
    • translucent image

I read the description in the Java Doc document many times, but just couldn't figure out how to use it. For example, this:

TYPE_INT_BGR

Represents an image with 8-bit RGB color components, corresponding to the Windows or Solaris-style BGR color model, with Blue, Green, and Red colors packed in whole pixels. Alpha no. Image has a DirectColorModel. When data with opaque alpha memory is stored in this type of image, the color data should be adjusted for the non-promoted form and alpha version, as described in the AlphaComposite documentation.

+5
source share
1 answer

If you do not have specific requirements (for example, saving memory or saving calculations or a specific native pixel format), just go with the standard TYPE_INT_ARGB , which has 8 bits per channel, 3 channels + alpha.

Skipping the alpha channel when working with 8 bits per channel will not affect the total memory occupied by the image, since each pixel will be packed into int anyway, so 8 bits will be discarded.

Basically you have:

  • TYPE_INT_ARGB , 4 bytes per pixel with alpha
  • TYPE_INT_ARGB_PRE , 4 bytes per pixel, as before, but the colors are already multiplied by the alpha of the pixel to save the calculations.
  • TYPE_INT_RGB , 4 bytes per pixel without alpha
  • TYPE_USHORT_555_RGB and TYPE_USHORT_565_RGB , 2 bytes per pixel, much less colors, you do not need to use it if you do not have memory limits.

Then there are all the same formats with exchanged channels (for example, BGR instead of RGB ). You must choose the one that is native to your platform so that there are fewer conversions.

+6
source

All Articles