Search code examples
javagraphics2d

How does Graphics2D handle subpixel accuracy?


Running in circles in grepcode trying to find the source for how a rotated image is drawn with subpixel accuracy. I am specifically interested in the method drawImage(Image img, AffineTransform xform, ImageObserver obs) which handles rotations but lacks implementation detail.

A rotation is rarely a mapping from integer (i,j) to integer (u,v) so at some intermediate level a sub-pixel view of the rotated image is created. This is not exposed to us via any API (that I know of) so I cannot inspect it, we are only given the imageraster. So how does graphics2d handle subpixel drawing when pixel coordinates are integers?


Solution

  • There is no single implementation for that, actually there is no specification on how that is implemented. The Oracle JRE implements Graphics2D in sun.java2d.SunGraphics2D (for which no source is provided in the Oracle JDK).

    And that implementation is probably just a facade for the underlying OS graphics system, which in turn will probably delegate most of the work further down to the graphics driver. Implementation will certainly contain platform specific code at some point. I suggest you take a look at the OpenJDK sources for one example of implementation: http://openjdk.java.net/groups/2d/