[OpenJDK 2D-Dev] BufferedImage.getTileGridXOffset() not compliant with its specification?

Sergey Bylokhov Sergey.Bylokhov at oracle.com
Sat Feb 22 20:01:49 UTC 2020


Hi, Martin.

I am not sure that the problem is in the implementation, probably the word
"origin" in the "Returns the x offset of the tile grid relative to the origin,"
should be clarified.

Is the "origin" refer the original bufferedimage or the sub-image.
Note the spec of the getSubimage():
      * The returned {@code BufferedImage} shares the same
      * data array as the original image.

So if origin is base image then implementation is correct, if the origin is
sub image then implementations should be fixed.

On 2/21/20 5:36 am, Martin Desruisseaux wrote:
> Hello all
> 
> Implementation of getTileGridXOffset() and getTileGridXOffset() in BufferedImage seems in contradiction with specification. The RenderedImage specification said:
> 
>     Returns the X offset of the tile grid relative to the origin, i.e., the X coordinate of the upper-left pixel of tile (0, 0). (Note that tile (0, 0) may not actually exist.)
> 
> Since BufferedImage has only one tile, always at index (0,0), the (x,y) coordinates of the upper-left pixel of that tile should be the image (minX, minY), which is always (0,0) in a BufferedImage. Indeed BufferedImage.getTileGridXOffset() javadoc adds the following sentence:
> 
>     This is always zero.
> 
> But the BufferedImage implementation is:
> 
>     public int getTileGridXOffset() {
>          return raster.getSampleModelTranslateX();
>     }
> 
> Which does not always returns zero. It can be demonstrated by the following test case:
> 
>     import java.awt.Dimension;
>     import java.awt.image.BufferedImage;
> 
>     class Test {
>          public static void main(String[] args) throws Exception {
>              BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
>              BufferedImage sub = img.getSubimage(10, 20, 50, 50);
>              System.out.println(sub.getTileGridXOffset());
>              System.out.println(sub.getTileGridYOffset());
>          }
>     }
> 
> Which prints -10 and -20.
> 
> Proposed fix: given that coordinates of BufferedImage == coordinates of tile (0,0) (this fact is hard-coded in BuffereImage.getMinTileX() returning the hard-coded 0 value, BuffereImage.getNumXTiles() returning the hard-coded 1 value (same for Y), and in BufferedImage.getMinX(), getMinY(), getWidth() and getHeight() delegating to the Raster), applying the same logic we should have:
> 
>     public int getTileGridXOffset() {
>          return raster.getMinX();
>     }
> 
>     public int getTileGridXOffset() {
>          return raster.getMinY();
>     }
> 
> Even if raster.getMinX() / getMinY() should always return 0, those methods are invoked for consistency with current implementation of BufferedImage.getMinX() / getMinY().
> 
> If above analysis is correct, do I need to provide a GitHub pull request or other submission mechanism?
> 
>      Regards,
> 
>          Martin
> 
> 


-- 
Best regards, Sergey.


More information about the 2d-dev mailing list