-
Bug
-
Resolution: Fixed
-
P4
-
7u10
-
Windows 7, JDK 7u9
Either I do not understand how to create a PixelFormat of Type.BYTE_INDEXED and use it to create an indexed grayscale image (with values ranging from 0..255), or there is a problem in PixelFormat.createByteIndexedInstance(colors). I suspect something is awry in dealing with bytes as unsigned values. Documentation and sample code on these classes is a bit thin.
This test app:
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.StackPane;
import javafx.stage.Stage;
public class ByteIndexedTest extends Application
{
@Override public void start(Stage primaryStage)
{
// create an color table of ARGB colors with r == g == b
int[] colors = new int[256];
for (int i = 0; i < 256; i++)
{
colors[i] = (255 << 24) | (i << 16) | (i << 8) | i;
}
// create a byte-indexed PixelFormat with the color table
PixelFormat pixelFormat = PixelFormat.createByteIndexedInstance(colors);
int width = 256;
int height = 256;
// create an array of one-byte pixels representing indicies into the color table
byte[] pixels = new byte[width*height];
// populate to make a nice gradient dark to light, top to bottom
for (int y = 0; y < height; y++)
{
// simply cast the int to a byte - bytes in PixelFormat.Type.BYTE_INDEXED are interpreted as unsigned
byte value = (byte)(y & 0xff);
// all pixels in the current row share the same index into the color table
for (int x = 0; x < width; x++)
{
pixels[y*width+x] = value;
}
}
// create a new WritableImage
WritableImage image = new WritableImage(width,height);
// set the pixels using the pixel writer
image.getPixelWriter().setPixels(0, 0, width, height, pixelFormat, pixels, 0, width);
StackPane root = new StackPane();
root.getChildren().add(new ImageView(image));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
throws this exception:
Caused by: 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)
at ByteIndexedTest.start(ByteIndexedTest.java:46)
This test app:
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.StackPane;
import javafx.stage.Stage;
public class ByteIndexedTest extends Application
{
@Override public void start(Stage primaryStage)
{
// create an color table of ARGB colors with r == g == b
int[] colors = new int[256];
for (int i = 0; i < 256; i++)
{
colors[i] = (255 << 24) | (i << 16) | (i << 8) | i;
}
// create a byte-indexed PixelFormat with the color table
PixelFormat pixelFormat = PixelFormat.createByteIndexedInstance(colors);
int width = 256;
int height = 256;
// create an array of one-byte pixels representing indicies into the color table
byte[] pixels = new byte[width*height];
// populate to make a nice gradient dark to light, top to bottom
for (int y = 0; y < height; y++)
{
// simply cast the int to a byte - bytes in PixelFormat.Type.BYTE_INDEXED are interpreted as unsigned
byte value = (byte)(y & 0xff);
// all pixels in the current row share the same index into the color table
for (int x = 0; x < width; x++)
{
pixels[y*width+x] = value;
}
}
// create a new WritableImage
WritableImage image = new WritableImage(width,height);
// set the pixels using the pixel writer
image.getPixelWriter().setPixels(0, 0, width, height, pixelFormat, pixels, 0, width);
StackPane root = new StackPane();
root.getChildren().add(new ImageView(image));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
throws this exception:
Caused by: 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)
at ByteIndexedTest.start(ByteIndexedTest.java:46)
- duplicates
-
JDK-8124846 Setting pixels to WritableImage in PixelFormat BYTE_INDEXED throws an NPE
-
- Closed
-
- relates to
-
JDK-8116409 NPE in PixelUtils
-
- Closed
-