changeset 10991:74d558eb71f6

8202277: WebView image capture fails with standalone FX due to dependency on javafx.swing Reviewed-by: kcr, arajkumar
author psadhukhan
date Wed, 18 Jul 2018 10:36:16 +0530
parents 3617ee7f1241
children cf58066ad6c2
files modules/javafx.web/src/main/java/com/sun/javafx/webkit/UIClientImpl.java modules/javafx.web/src/test/java/test/javafx/scene/web/SwingDependencyTest.java
diffstat 2 files changed, 222 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.web/src/main/java/com/sun/javafx/webkit/UIClientImpl.java	Tue Jul 17 09:11:48 2018 -0700
+++ b/modules/javafx.web/src/main/java/com/sun/javafx/webkit/UIClientImpl.java	Wed Jul 18 10:36:16 2018 +0530
@@ -38,12 +38,17 @@
 import com.sun.webkit.WebPage;
 import com.sun.webkit.graphics.WCImage;
 import com.sun.webkit.graphics.WCRectangle;
+import java.awt.AlphaComposite;
+import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
+import java.awt.image.DataBufferInt;
+import java.awt.image.SampleModel;
+import java.awt.image.SinglePixelPackedSampleModel;
 import java.io.File;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
-import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
 import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -54,6 +59,9 @@
 import javafx.event.EventHandler;
 import javafx.geometry.Rectangle2D;
 import javafx.scene.image.Image;
+import javafx.scene.image.PixelFormat;
+import javafx.scene.image.PixelReader;
+import javafx.scene.image.WritablePixelFormat;
 import javafx.scene.input.ClipboardContent;
 import javafx.scene.input.DataFormat;
 import javafx.scene.input.Dragboard;
@@ -63,6 +71,7 @@
 import javafx.scene.web.WebEngine;
 import javafx.scene.web.WebEvent;
 import javafx.scene.web.WebView;
+import javafx.scene.paint.Color;
 import javafx.stage.FileChooser;
 import javafx.stage.FileChooser.ExtensionFilter;
 import javafx.stage.Window;
@@ -394,15 +403,137 @@
         return accessor.getView() != null && content != null;
     }
 
+    private static int
+            getBestBufferedImageType(PixelFormat<?> fxFormat, BufferedImage bimg,
+                                     boolean isOpaque)
+    {
+        if (bimg != null) {
+            int bimgType = bimg.getType();
+            if (bimgType == BufferedImage.TYPE_INT_ARGB ||
+                bimgType == BufferedImage.TYPE_INT_ARGB_PRE ||
+                (isOpaque &&
+                     (bimgType == BufferedImage.TYPE_INT_BGR ||
+                      bimgType == BufferedImage.TYPE_INT_RGB)))
+            {
+                // We will allow the caller to give us a BufferedImage
+                // that has an alpha channel, but we might not otherwise
+                // construct one ourselves.
+                // We will also allow them to choose their own premultiply
+                // type which may not match the image.
+                // If left to our own devices we might choose a more specific
+                // format as indicated by the choices below.
+                return bimgType;
+            }
+        }
+        switch (fxFormat.getType()) {
+            default:
+            case BYTE_BGRA_PRE:
+            case INT_ARGB_PRE:
+                return BufferedImage.TYPE_INT_ARGB_PRE;
+            case BYTE_BGRA:
+            case INT_ARGB:
+                return BufferedImage.TYPE_INT_ARGB;
+            case BYTE_RGB:
+                return BufferedImage.TYPE_INT_RGB;
+            case BYTE_INDEXED:
+                return (fxFormat.isPremultiplied()
+                        ? BufferedImage.TYPE_INT_ARGB_PRE
+                        : BufferedImage.TYPE_INT_ARGB);
+        }
+    }
+
+    private static WritablePixelFormat<IntBuffer>
+        getAssociatedPixelFormat(BufferedImage bimg)
+    {
+        switch (bimg.getType()) {
+            // We lie here for xRGB, but we vetted that the src data was opaque
+            // so we can ignore the alpha.  We use ArgbPre instead of Argb
+            // just to get a loop that does not have divides in it if the
+            // PixelReader happens to not know the data is opaque.
+            case BufferedImage.TYPE_INT_RGB:
+            case BufferedImage.TYPE_INT_ARGB_PRE:
+                return PixelFormat.getIntArgbPreInstance();
+            case BufferedImage.TYPE_INT_ARGB:
+                return PixelFormat.getIntArgbInstance();
+            default:
+                // Should not happen...
+                throw new InternalError("Failed to validate BufferedImage type");
+        }
+    }
+
+    private static boolean checkFXImageOpaque(PixelReader pr, int iw, int ih) {
+        for (int x = 0; x < iw; x++) {
+            for (int y = 0; y < ih; y++) {
+                Color color = pr.getColor(x,y);
+                if (color.getOpacity() != 1.0) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    private static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
+        PixelReader pr = img.getPixelReader();
+        if (pr == null) {
+            return null;
+        }
+        int iw = (int) img.getWidth();
+        int ih = (int) img.getHeight();
+        PixelFormat<?> fxFormat = pr.getPixelFormat();
+        boolean srcPixelsAreOpaque = false;
+        switch (fxFormat.getType()) {
+            case INT_ARGB_PRE:
+            case INT_ARGB:
+            case BYTE_BGRA_PRE:
+            case BYTE_BGRA:
+                // Check fx image opacity only if
+                // supplied BufferedImage is without alpha channel
+                if (bimg != null &&
+                        (bimg.getType() == BufferedImage.TYPE_INT_BGR ||
+                         bimg.getType() == BufferedImage.TYPE_INT_RGB)) {
+                    srcPixelsAreOpaque = checkFXImageOpaque(pr, iw, ih);
+                }
+                break;
+            case BYTE_RGB:
+                srcPixelsAreOpaque = true;
+                break;
+        }
+        int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg, srcPixelsAreOpaque);
+        if (bimg != null) {
+            int bw = bimg.getWidth();
+            int bh = bimg.getHeight();
+            if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
+                bimg = null;
+            } else if (iw < bw || ih < bh) {
+                Graphics2D g2d = bimg.createGraphics();
+                g2d.setComposite(AlphaComposite.Clear);
+                g2d.fillRect(0, 0, bw, bh);
+                g2d.dispose();
+            }
+        }
+        if (bimg == null) {
+            bimg = new BufferedImage(iw, ih, prefBimgType);
+        }
+        DataBufferInt db = (DataBufferInt)bimg.getRaster().getDataBuffer();
+        int data[] = db.getData();
+        int offset = bimg.getRaster().getDataBuffer().getOffset();
+        int scan =  0;
+        SampleModel sm = bimg.getRaster().getSampleModel();
+        if (sm instanceof SinglePixelPackedSampleModel) {
+            scan = ((SinglePixelPackedSampleModel)sm).getScanlineStride();
+        }
+
+        WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
+        pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
+        return bimg;
+    }
+
     // Method to implement the following via reflection:
     //     SwingFXUtils.fromFXImage(img, null)
     public static BufferedImage toBufferedImage(Image img) {
         try {
-            Class swingFXUtilsCls = Class.forName("javafx.embed.swing.SwingFXUtils");
-            Method m_fromFXImage = swingFXUtilsCls.getMethod("fromFXImage",
-                    Image.class, BufferedImage.class);
-            Object bimg = m_fromFXImage.invoke(null, img, null);
-            return (BufferedImage)bimg;
+            return fromFXImage(img, null);
         } catch (Exception ex) {
             ex.printStackTrace(System.err);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/javafx.web/src/test/java/test/javafx/scene/web/SwingDependencyTest.java	Wed Jul 18 10:36:16 2018 +0530
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package test.javafx.scene.web;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import javafx.scene.web.WebEngine;
+import javafx.scene.web.WebView;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test the Image to DataURL function
+ */
+public class SwingDependencyTest extends TestBase {
+
+    private final PrintStream err = System.err;
+
+    private static final String htmlContent = "\n"
+        + "<!DOCTYPE html>\n"
+        + "<html>\n"
+        + "<body>\n"
+        + "<canvas id=\"theCanvas\" width=\"200\" height=\"100\">\n"
+        + "</canvas>\n"
+        + "<p id = \"encodedText\">\n"
+        + "</p>\n"
+        + "<script>\n"
+        + "var c = document.getElementById(\"theCanvas\");\n"
+        + "var ctx = c.getContext(\"2d\");\n"
+        + "var my_gradient=ctx.createLinearGradient(0,0,0,75);\n"
+        + "my_gradient.addColorStop(0,\"red\");\n"
+        + "my_gradient.addColorStop(0.5,\"green\");\n"
+        + "my_gradient.addColorStop(1,\"blue\");\n"
+        + "ctx.fillStyle=my_gradient;\n"
+        + "ctx.fillRect(0,0,150,75);\n"
+        + "var dataURL = c.toDataURL();\n"
+        + "document.getElementById(\"encodedText\").innerHTML=dataURL;\n"
+        + "</script>\n"
+        + "</body>\n"
+        + "</html>\n";
+
+    @Test
+    public void testSwingDependency() throws Exception {
+        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+
+        System.setErr(new PrintStream(bytes));
+
+        loadContent(htmlContent);
+
+        System.setErr(err);
+
+        Assert.assertFalse("ClassNotFoundException found",
+                            bytes.toString().contains("ClassNotFoundException"));
+    }
+
+    @After
+    public void resetSystemErr() {
+        System.setErr(err);
+    }
+}