JDK-8192965 : Drawing Scaled Images on Canvas without interpolation
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: 8,9,10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2017-12-03
  • Updated: 2019-02-27
  • Resolved: 2019-02-27
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
When drawing pixelated Images on a Canvas with a given width/height, they can only be drawn with Interpolation, which is not always what the developer wants. There is a workarounds by doing this yourself with PixelReader/Writer, but it performs very poorly.

JUSTIFICATION :
The developer sometimes Needs Control (like in Swing, or HTML5) about the interpolation method, or if it should be disabled. I.e. useful in 2D-Gamedev.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An ability to turn off Interpolation, the resulting Image drawn with a different resolution on a canvas should be pixelated.
ACTUAL -
The Image is always smoothed out.

---------- BEGIN SOURCE ----------
Image image = ...;
canvas.setWidth(scale * width);
canvas.setHeight(scale * height);
GraphicsContext gc = canvas.getGraphicsContext2D();    
gc.drawImage(image, 0, 0, scale * width, scale * height);
// this gives same result
// gc.scale(scale, scale);
// gc.drawImage(editableImage, 0, 0, width, height);
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
PixelReader reader = image.getPixelReader();
PixelWriter writer = gc.getPixelWriter();
for (int y = 0; y < scale * height; ++y) {
    for (int x = 0; x < scale * width; ++x) {
        writer.setArgb(x, y, reader.getArgb(x / scale, y / scale));
    }
}

Performs poorly and doesn't Support Transparent Pixels drawn over each other.


Comments
Submitter is requesting to improve the performance for drawing scaled images using PixelReader/Writer method. Prepared test case based on Submitter's description. When drawing : 1. Using Graphics.drawImage() 2. Using Graphics PixelReader/Writer Image rendering in #2 is poor than #1. PFA screenshot of test case using both methods in 9.0.1+11 Same behavior in JDK 8 (Checked in 8u144) & JDK 10 (10-ea+32).
04-12-2017