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

Setting pixels to WritableImage in PixelFormat BYTE_INDEXED throws an NPE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7u13, 7u15, 8
    • javafx
    • None
    • Windows 7 64-Bit

      I tried to construct a byte array and set it to a WritableImage using PixelWriter's setPixels method.

      If I use a byte-indexed pixel format, I get an NPE.


      java.lang.NullPointerException
      at com.sun.javafx.image.impl.BaseByteToByteConverter.<init>(BaseByteToByteConverter.java:45)
      at com.sun.javafx.image.impl.General$ByteToByteGeneralConverter.<init>(General.java:69)
      at com.sun.javafx.image.impl.General.create(General.java:44)
      at com.sun.javafx.image.PixelUtils.getB2BConverter(PixelUtils.java:223)
      at com.sun.prism.Image$ByteAccess.setPixels(Image.java:770)
      at com.sun.prism.Image.setPixels(Image.java:606)
      at javafx.scene.image.WritableImage$2.setPixels(WritableImage.java:199)

      Short self-contained correct example to reproduce:

      import java.nio.ByteBuffer;
       
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.image.ImageView;
      import javafx.scene.image.PixelFormat;
      import javafx.scene.image.WritableImage;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;
       
      public class IndexedColorTestApp extends Application {
       
          public static void main(String[] args) {
              launch(args);
          }
       
          @Override
          public void start(Stage primaryStage) {
              BorderPane borderPane = new BorderPane();
              Scene scene = new Scene(borderPane, 600, 1100);
              primaryStage.setScene(scene);
       
              ImageView imageView = new ImageView();
              borderPane.setCenter(imageView);
              primaryStage.show();
       
              int imageWidth = 200;
              int imageHeight = 200;
              WritableImage writableImage = new WritableImage(imageWidth, imageHeight);
       
              // this works
              byte[] rgbBytePixels = new byte[imageWidth * imageHeight * 3];
              PixelFormat<ByteBuffer> byteRgbFormat = PixelFormat.getByteRgbInstance();
              writableImage.getPixelWriter().setPixels(0, 0, imageWidth, imageHeight,
                                                       byteRgbFormat, rgbBytePixels, 0, imageWidth * 3);
              imageView.setImage(writableImage);
       
              // this throws an NPE in setPixels()
              byte[] indexedBytePixels = new byte[imageWidth * imageHeight];
              int[] colorPalette = new int[256];
              PixelFormat<ByteBuffer> byteIndexedFormat = PixelFormat.createByteIndexedInstance(colorPalette);
              writableImage.getPixelWriter().setPixels(0, 0, imageWidth, imageHeight,
                                                       byteIndexedFormat, indexedBytePixels, 0, imageWidth);
              imageView.setImage(writableImage);
          }
       
      }

            flar Jim Graham
            apabstjfx Andreas Pabst (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: