I am working with the Java class RescaleOp
to change the brightness of BufferedImage
instances. The alpha channel consistently causes problems. See refs below -- thanks to @trashgod for his impressive Java2D insights.
- Ref 1: Java Buffered Image RescaleOp Transparency Issue
- Ref 2: How to setSize of image using RescaleOp
The docs from RescaleOp
clearly state for instances of BufferedImage
, alpha channel is not scaled in the single factor constructors -- I interpret as either float
or float[1]
.
Quote from JDK6: (emphasis added)
For BufferedImages, rescaling operates on color and alpha components. The number of sets of scaling constants may be one, in which case the same constants are applied to all color (but not alpha) components. Otherwise, the number of sets of scaling constants may equal the number of Source color components, in which case no rescaling of the alpha component (if present) is performed. If neither of these cases apply, the number of sets of scaling constants must equal the number of Source color components plus alpha components, in which case all color and alpha components are rescaled.
For a BufferedImage
with type BufferedImage.TYPE_INT_ARGB
, there are four channels (R-G-B-A), where alpha is the last channel. (Why didn't they call it BufferedImage.TYPE_INT_RGBA
?) I tried these RescaleOp
transformations without success: (assume float scaleFactor = 1.25f
and float offset = 0.0f
)
new RescaleOp(scaleFactor, offset, (RenderingHints) null)
new RescaleOp(new float[] { scaleFactor },
new float[] { offset },
(RenderingHints) null)
new RescaleOp(new float[] { scaleFactor, scaleFactor, scaleFactor },
new float[] { offset, offset, offset },
(RenderingHints) null)
Only this works: (assume float alphaScaleFactor = 1.0f
)
new RescaleOp(new float[] { scaleFactor, scaleFactor, scaleFactor, alphaScaleFactor },
new float[] { offset, offset, offset, offset },
(RenderingHints) null)
- Do I misunderstand the official JDK docs?
- Or, is this a bug that could/should be fixed in future JDKs?
- Is there a way to find (at runtime) the alpha channel index?
- Methods that might help:
ColorModel BufferedImage.getColorModel()
int ColorModel.getNumColorComponents()
boolean ColorModel.hasAlpha()
int ColorModel.getNumComponents()
(may include optional alpha channel)ColorSpace ColorModel.getColorSpace()
- Methods that might help:
Please advise.