1 /* 2 * Copyright (c) 2014, 2016 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 test.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.embed.swing.SwingFXUtils; 34 import javafx.scene.image.Image; 35 import javafx.scene.image.PixelFormat; 36 import javafx.scene.image.PixelReader; 37 import javafx.stage.Stage; 38 import junit.framework.AssertionFailedError; 39 import org.junit.AfterClass; 40 import static org.junit.Assert.assertEquals; 41 import static org.junit.Assert.assertNotNull; 42 import static org.junit.Assert.assertTrue; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 47 public class SwingFXUtilsTest { 48 static final boolean verbose = false; 49 50 // Used to launch the application before running any test 51 private static final CountDownLatch launchLatch = new CountDownLatch(1); 52 53 54 @BeforeClass 55 public static void doSetupOnce() { 56 JFXPanelTest.doSetupOnce(); 57 } 58 59 @AfterClass 60 public static void doTeardownOnce() { 61 Platform.exit(); 62 } 63 64 @Test 65 public void testFromFXImg() { 66 testFromFXImg("alpha.png"); 67 testFromFXImg("opaque.gif"); 68 testFromFXImg("opaque.jpg"); 69 testFromFXImg("opaque.png"); 70 testFromFXImg("trans.gif"); 71 } 72 73 static void testFromFXImg(String imgfilename) { 74 Image img = new Image("test/javafx/embed/swing/"+imgfilename); 75 boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB); 76 BufferedImage bimg = SwingFXUtils.fromFXImage(img, null); 77 checkBimg(img, bimg); 78 boolean reusesitself = reusesBimg(img, bimg, true); 79 boolean reusesxrgb = reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired); 80 boolean reusesargb = reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true); 81 boolean reusesargbpre = reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true); 82 if (verbose) { 83 System.out.println(imgfilename+" type = "+img.getPixelReader().getPixelFormat()); 84 System.out.println(imgfilename+" bimg type = "+bimg.getType()); 85 System.out.println(imgfilename+" reuses own bimg = "+reusesitself); 86 System.out.println(imgfilename+" reuses rgb bimg = "+reusesxrgb); 87 System.out.println(imgfilename+" reuses argb bimg = "+reusesargb); 88 System.out.println(imgfilename+" reuses argb pre bimg = "+reusesargbpre); 89 System.out.println(); 90 } 91 } 92 93 static boolean reusesBimg(Image img, int type, boolean required) { 94 int iw = (int) img.getWidth(); 95 int ih = (int) img.getHeight(); 96 BufferedImage bimg = new BufferedImage(iw, ih, type); 97 return reusesBimg(img, bimg, required); 98 } 99 100 static boolean reusesBimg(Image img, BufferedImage bimg, boolean required) { 101 BufferedImage ret = SwingFXUtils.fromFXImage(img, bimg); 102 checkBimg(img, ret); 103 if (required) { 104 assertTrue(bimg == ret); 105 } 106 return (bimg == ret); 107 } 108 109 static void checkBimg(Image img, BufferedImage bimg) { 110 PixelReader pr = img.getPixelReader(); 111 int iw = (int) img.getWidth(); 112 int ih = (int) img.getHeight(); 113 for (int y = 0; y < ih; y++) { 114 for (int x = 0; x < iw; x++) { 115 int imgargb = pr.getArgb(x, y); 116 int bimgargb = bimg.getRGB(x, y); 117 if (imgargb != bimgargb) { 118 System.err.println(">>>> wrong color in bimg: "+hex(bimgargb)+ 119 " at "+x+", "+y+ 120 " should be: "+hex(imgargb)); 121 assertEquals(imgargb, bimgargb); 122 } 123 } 124 } 125 } 126 127 static String hex(int i) { 128 return String.format("0x%08x", i); 129 } 130 }