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

Drawing Scaled Images on Canvas without interpolation

XMLWordPrintable

    • generic
    • generic

      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.

        1. download.jpg
          download.jpg
          11 kB
        2. DrawImage_Comp.png
          DrawImage_Comp.png
          1.13 MB
        3. DrawScaledImageTest.java
          2 kB

            arapte Ambarish Rapte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: