1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.embed.swing; 27 28 import java.awt.image.BufferedImage; 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 import javafx.application.Application; 32 import javafx.application.Platform; 33 import javafx.scene.image.Image; 34 import javafx.scene.image.PixelFormat; 35 import javafx.scene.image.PixelReader; 36 import javafx.stage.Stage; 37 import junit.framework.AssertionFailedError; 38 import org.junit.AfterClass; 39 import static org.junit.Assert.assertEquals; 40 import static org.junit.Assert.assertNotNull; 41 import static org.junit.Assert.assertTrue; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 public class SwingFXUtilsTest { 46 static final boolean verbose = false; 47 48 // Used to launch the application before running any test 49 private static final CountDownLatch launchLatch = new CountDownLatch(1); 50 51 // Application class. An instance is created and initialized before running 52 // the first test, and it lives through the execution of all tests. 53 public static class MyApp extends Application { 54 @Override 55 public void start(Stage primaryStage) throws Exception { 56 Platform.setImplicitExit(false); 57 assertTrue(Platform.isFxApplicationThread()); 58 assertNotNull(primaryStage); 59 60 launchLatch.countDown(); 61 } 62 } 63 64 @BeforeClass 65 public static void doSetupOnce() { 66 // Start the Application 67 new Thread(() -> Application.launch(MyApp.class, (String[]) null)).start(); 68 69 try { 70 if (!launchLatch.await(5000, TimeUnit.MILLISECONDS)) { 71 throw new AssertionFailedError("Timeout waiting for Application to launch"); 72 } 73 } catch (InterruptedException ex) { 74 AssertionFailedError err = new AssertionFailedError("Unexpected exception"); 75 err.initCause(ex); 76 throw err; 77 } 78 79 assertEquals(0, launchLatch.getCount()); 80 } 81 82 @AfterClass 83 public static void doTeardownOnce() { 84 Platform.exit(); 85 } 86 87 @Test 88 public void testFromFXImg() { 89 testFromFXImg("alpha.png"); 90 testFromFXImg("opaque.gif"); 91 testFromFXImg("opaque.jpg"); 92 testFromFXImg("opaque.png"); 93 testFromFXImg("trans.gif"); 94 } 95 96 static void testFromFXImg(String imgfilename) { 97 Image img = new Image("javafx/embed/swing/"+imgfilename); 98 boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB); 99 BufferedImage bimg = SwingFXUtils.fromFXImage(img, null); 100 checkBimg(img, bimg); 101 boolean reusesitself = reusesBimg(img, bimg, true); 102 boolean reusesxrgb = reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired); 103 boolean reusesargb = reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true); 104 boolean reusesargbpre = reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true); 105 if (verbose) { 106 System.out.println(imgfilename+" type = "+img.getPixelReader().getPixelFormat()); 107 System.out.println(imgfilename+" bimg type = "+bimg.getType()); 108 System.out.println(imgfilename+" reuses own bimg = "+reusesitself); 109 System.out.println(imgfilename+" reuses rgb bimg = "+reusesxrgb); 110 System.out.println(imgfilename+" reuses argb bimg = "+reusesargb); 111 System.out.println(imgfilename+" reuses argb pre bimg = "+reusesargbpre); 112 System.out.println(); 113 } 114 } 115 116 static boolean reusesBimg(Image img, int type, boolean required) { 117 int iw = (int) img.getWidth(); 118 int ih = (int) img.getHeight(); 119 BufferedImage bimg = new BufferedImage(iw, ih, type); 120 return reusesBimg(img, bimg, required); 121 } 122 123 static boolean reusesBimg(Image img, BufferedImage bimg, boolean required) { 124 BufferedImage ret = SwingFXUtils.fromFXImage(img, bimg); 125 checkBimg(img, ret); 126 if (required) { 127 assertTrue(bimg == ret); 128 } 129 return (bimg == ret); 130 } 131 132 static void checkBimg(Image img, BufferedImage bimg) { 133 PixelReader pr = img.getPixelReader(); 134 int iw = (int) img.getWidth(); 135 int ih = (int) img.getHeight(); 136 for (int y = 0; y < ih; y++) { 137 for (int x = 0; x < iw; x++) { 138 int imgargb = pr.getArgb(x, y); 139 int bimgargb = bimg.getRGB(x, y); 140 if (imgargb != bimgargb) { 141 System.err.println(">>>> wrong color in bimg: "+hex(bimgargb)+ 142 " at "+x+", "+y+ 143 " should be: "+hex(imgargb)); 144 assertEquals(imgargb, bimgargb); 145 } 146 } 147 } 148 } 149 150 static String hex(int i) { 151 return String.format("0x%08x", i); 152 } 153 }