Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8323108

BufferedImage.setData(Raster) should not cast float and double values to integers

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 23
    • 17, 21, 22
    • client-libs
    • None
    • 2d
    • b07
    • generic
    • generic

    Description

      The BufferedImage Javadoc does not mention any constraint about the data type. In practice, BufferedImage with floating point values can be rendered by Java2D as well as integers, 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 the SetData test case in this pull request.

      An easy fix, which is proposed in this pull request, is to replace the whole BufferedImage.setData(Raster) method body by a simple call to WritableRaster.setRect(Raster), which handles all DataBuffer types correctly. The method contracts documented in their respective Javadoc are compatible. Furthermore an examination of their source code shows that they are equivalent, except for the differences noted in the Behavioural changes section below.

      Attachments

        Issue Links

          Activity

            People

              prr Philip Race
              serb Sergey Bylokhov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: