import java.awt.image.BufferedImage; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.image.Image; import javafx.scene.image.PixelFormat; import javafx.scene.image.PixelReader; import javafx.stage.Stage; public class ReadImages extends Application { static boolean verbose; static int errors; @Override public void start(Stage stage) { // Not reached... } public static void test() { String imgnames[] = { "alpha.png", "opaque.gif", "opaque.jpg", "opaque.png", "trans.gif", }; for (int i = 0; i < imgnames.length; i++) { String name = imgnames[i]; Image img = new Image(name); boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB); BufferedImage bimg = SwingFXUtils.fromFXImage(img, null); checkBimg(img, bimg); System.out.println(name+" type = "+img.getPixelReader().getPixelFormat()); System.out.println(name+" bimg type = "+bimg.getType()); System.out.println(name+" reuses own bimg = "+reusesBimg(img, bimg, true)); System.out.println(name+" reuses rgb bimg = "+reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired)); System.out.println(name+" reuses argb bimg = "+reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true)); System.out.println(name+" reuses argb pre bimg = "+reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true)); System.out.println(); } if (errors == 0) { System.out.println("No errors encountered"); } else { System.out.flush(); throw new RuntimeException(errors+" errors encountered"); } System.exit(0); } static boolean reusesBimg(Image img, int type, boolean required) { int iw = (int) img.getWidth(); int ih = (int) img.getHeight(); BufferedImage bimg = new BufferedImage(iw, ih, type); return reusesBimg(img, bimg, required); } static boolean reusesBimg(Image img, BufferedImage bimg, boolean required) { BufferedImage ret = SwingFXUtils.fromFXImage(img, bimg); checkBimg(img, ret); if (bimg == ret) { return true; } else { if (required) errors++; return false; } } static void checkBimg(Image img, BufferedImage bimg) { PixelReader pr = img.getPixelReader(); int iw = (int) img.getWidth(); int ih = (int) img.getHeight(); for (int y = 0; y < ih; y++) { for (int x = 0; x < iw; x++) { int imgargb = pr.getArgb(x, y); int bimgargb = bimg.getRGB(x, y); if (imgargb != bimgargb) { System.out.println(">>>> wrong color in bimg: "+hex(bimgargb)+ " at "+x+", "+y+ " should be: "+hex(imgargb)); errors++; } else if (verbose) { System.out.println(hex(imgargb)+" => "+hex(bimgargb)); } } } } static String hex(int i) { return String.format("0x%08x", i); } public static void main(String argv[]) { verbose = (argv.length > 0); test(); } }