BufferedImage.setData(Raster) does not copy float and double sample values properly

Martin Desruisseaux martin.desruisseaux at geomatys.com
Thu May 4 11:02:25 UTC 2023


Hello

The BufferedImage Javadoc does not mention any constraint about the data 
type. In practice, BufferedImage with floating point values can be 
rendered as well as integers (only slower) provided that a compatible 
ColorModel was supplied at construction time. However calls to 
setData(Raster) unexpectedly cast floating point values to integers. For 
example sample value 0.8 become 0. This is demonstrated by a test case, 
together with a proposed fix, in the following pull request:

    https://github.com/openjdk/jdk/pull/13797

The proposed fix is very simple: just replace the whole method body by a 
call to WritableRaster.setRect(Raster), which handles all supported data 
types correctly. The pull request description contains code comparison 
for showing that this is equivalent, except for robustness to integer 
underflow. The pull request discusses the possible impact on behavior, 
which are:

  * Change in behavior under integer underflow.
  * One more user-overrideable method get invoked.

Martin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20230504/7923245a/attachment.htm>


More information about the client-libs-dev mailing list