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

getColor throws IOOBE when PixelReader reads the same pixel twice

XMLWordPrintable

    • b17
    • x86_64
    • windows_10
    • Not verified

        ADDITIONAL SYSTEM INFORMATION :
        Reproduced at least on Windows 10 with Oracle JDK 8u271, but it probably affects other JDK's as well.

        A DESCRIPTION OF THE PROBLEM :
        See attached code to reproduce. It creates a single Image, retrieves a pixel reader and reads the same pixel twice.

        The issue seems to lie in com.sun.prism.Image#getPixelAccessor:

        {code}
            private Accessor<?> getPixelAccessor() {
                if (pixelaccessor == null) {
                   // ... Initializes pixelAccessor
                }
                if (pixelaccessor != null && pixelScale != 1.0f) {
                    pixelaccessor = new ScaledAccessor<>(pixelaccessor, pixelScale);
                }
                return pixelaccessor;
            }
        {code}

        Note the positioning of the second if-case, which should be located *inside* the first if-case.
        With this position, the pixelAccessor will be wrapped in an additional ScaledAccessor every time getPixelAccessor is called, resulting in incorrect readings on every call after the first one.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the attached code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Both calls to #getColor work.
        ACTUAL -
        The second #getColor throws an OutOfBoundsException, because the Image has applied the scale twice.

        ---------- BEGIN SOURCE ----------
        package test;

        import javafx.application.Application;
        import javafx.scene.image.Image;
        import javafx.scene.image.PixelReader;
        import javafx.stage.Stage;

        public class Main extends Application {
          @Override
          public void start(Stage primaryStage) throws Exception {
            try {
              Image image = new Image("https://static.wikia.nocookie.net/starwars/images/1/1e/Chewbacca-Fathead.png/revision/latest/scale-to-width-down/1000?cb=20161108052810");
              PixelReader pixelReader = image.getPixelReader();
              System.out.println(pixelReader.getColor((int) image.getWidth() - 1, (int) image.getHeight() - 1));
              System.out.println(pixelReader.getColor((int) image.getWidth() - 1, (int) image.getHeight() - 1)); // throws OutOfBoundsException
            } catch (Throwable e) {
              e.printStackTrace();
            }
            System.exit(0);
          }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Recreate the image each time you need it.

        FREQUENCY : always


          1. Main.java
            0.8 kB
            Praveen Narayanaswamy

              kcr Kevin Rushforth
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: