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     @Test
 60     public void testFromFXImg() {
 61         testFromFXImg("alpha.png");
 62         testFromFXImg("opaque.gif");
 63         testFromFXImg("opaque.jpg");
 64         testFromFXImg("opaque.png");
 65         testFromFXImg("trans.gif");
 66     }
 67 
 68     static void testFromFXImg(String imgfilename) {
 69         Image img = new Image("test/javafx/embed/swing/"+imgfilename);
 70         boolean rgbrequired = (img.getPixelReader().getPixelFormat().getType() == PixelFormat.Type.BYTE_RGB);
 71         BufferedImage bimg = SwingFXUtils.fromFXImage(img, null);
 72         checkBimg(img, bimg);
 73         boolean reusesitself = reusesBimg(img, bimg, true);
 74         boolean reusesxrgb = reusesBimg(img, BufferedImage.TYPE_INT_RGB, rgbrequired);
 75         boolean reusesargb = reusesBimg(img, BufferedImage.TYPE_INT_ARGB, true);
 76         boolean reusesargbpre = reusesBimg(img, BufferedImage.TYPE_INT_ARGB_PRE, true);
 77         if (verbose) {
 78             System.out.println(imgfilename+" type = "+img.getPixelReader().getPixelFormat());
 79             System.out.println(imgfilename+" bimg type = "+bimg.getType());
 80             System.out.println(imgfilename+" reuses own bimg = "+reusesitself);
 81             System.out.println(imgfilename+" reuses rgb bimg = "+reusesxrgb);
 82             System.out.println(imgfilename+" reuses argb bimg = "+reusesargb);
 83             System.out.println(imgfilename+" reuses argb pre bimg = "+reusesargbpre);
 84             System.out.println();
 85         }
 86     }
 87 
 88     static boolean reusesBimg(Image img, int type, boolean required) {
 89         int iw = (int) img.getWidth();
 90         int ih = (int) img.getHeight();
 91         BufferedImage bimg = new BufferedImage(iw, ih, type);
 92         return reusesBimg(img, bimg, required);
 93     }
 94 
 95     static boolean reusesBimg(Image img, BufferedImage bimg, boolean required) {
 96         BufferedImage ret = SwingFXUtils.fromFXImage(img, bimg);
 97         checkBimg(img, ret);
 98         if (required) {
 99             assertTrue(bimg == ret);
100         }
101         return (bimg == ret);
102     }
103 
104     static void checkBimg(Image img, BufferedImage bimg) {
105         PixelReader pr = img.getPixelReader();
106         int iw = (int) img.getWidth();
107         int ih = (int) img.getHeight();
108         for (int y = 0; y < ih; y++) {
109             for (int x = 0; x < iw; x++) {
110                 int imgargb = pr.getArgb(x, y);
111                 int bimgargb = bimg.getRGB(x, y);
112                 if (imgargb != bimgargb) {
113                     System.err.println(">>>> wrong color in bimg: "+hex(bimgargb)+
114                                        " at "+x+", "+y+
115                                        " should be: "+hex(imgargb));
116                     assertEquals(imgargb, bimgargb);
117                 }
118             }
119         }
120     }
121 
122     static String hex(int i) {
123         return String.format("0x%08x", i);
124     }
125 }