15

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

However, Java docs did a minimal explanation what these image types are used for and how would it affect the BufferedImage to be created.

My question is:

  1. How would an image type affect the BufferedImage to be created? Does it control the number of bits used to store various colors (Red,Green,Blue) and its transparency?

  2. Which image type should we use if we just want to create

    • an opaque image
    • a transparent image
    • a translucent image

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

TYPE_INT_BGR

Represents an image with 8-bit RGB color components, corresponding to a Windows- or Solaris- style BGR color model, with the colors Blue, Green, and Red packed into integer pixels. There is no alpha. The image has a DirectColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded, as described in the AlphaComposite documentation.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • What's a translucent image? How is it different to a transparent image? – Andy Turner Sep 05 '15 at 15:15
  • @AndyTurner I remember I read it somewhere about making parts of the image transparent while translucent is on the adjusting the opacity of the whole image. For example, a gif file allows transparency but not translucency, while png files supports both. This is an example where one may need to use a different image type. No? – user3437460 Sep 05 '15 at 15:18

1 Answers1

20

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

Skipping the alpha channel when working with 8 bits per channel won't affect the total memory occupied by the image since every pixel will be packed in an int in any case so 8 bits will be discarded.

Basically you have:

  • TYPE_INT_ARGB, 4 bytes per pixel with alpha channel
  • TYPE_INT_ARGB_PRE, 4 bytes per pixel, same as before but colors are already multiplied by the alpha of the pixel to save computations
  • TYPE_INT_RGB, 4 bytes per pixel without alpha channel
  • TYPE_USHORT_555_RGB and TYPE_USHORT_565_RGB, 2 bytes per pixel, much less colors, don't need to use it unless you have memory constraints

Then there are all the same kind of formats with swapped channels (eg. BGR instead that RGB). You should choose the one native of your platform so that less conversion should be done.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • thanks for your quick reply. So I can just use `TYPE_InT_ARGB` for opaque, transparent & translucent image unless I have some other special needs like mentioned by you? – user3437460 Sep 05 '15 at 15:27
  • Yes, a translucent image is not translucent per se (unless all pixels have alpha). You can obtain translucency by drawing the image with alpha blending over the destination framebuffer. – Jack Sep 05 '15 at 15:28
  • And so long the image type has support for alpha channel, it will be able to support transparency am I right to say that? This is my last question! :D – user3437460 Sep 05 '15 at 15:29
  • @user3437460 Yes, you are right to say that. – SMMH Jul 11 '22 at 18:32