< prev index next >

modules/web/src/main/java/com/sun/javafx/webkit/prism/PrismImage.java

Print this page




   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 com.sun.javafx.webkit.prism;
  27 

  28 import java.awt.image.BufferedImage;
  29 import java.io.ByteArrayOutputStream;
  30 import java.io.IOException;
  31 import java.util.Base64;
  32 import java.util.Iterator;
  33 import javax.imageio.ImageIO;
  34 import javax.imageio.ImageWriter;
  35 
  36 import com.sun.javafx.webkit.UIClientImpl;
  37 import com.sun.prism.Image;
  38 import com.sun.prism.Graphics;
  39 import com.sun.webkit.graphics.WCImage;
  40 
  41 /**
  42  * @author Alexey.Ushakov
  43  */
  44 abstract class PrismImage extends WCImage {
  45     abstract Image getImage();
  46     abstract Graphics getGraphics();
  47     abstract void draw(Graphics g,
  48             int dstx1, int dsty1, int dstx2, int dsty2,
  49             int srcx1, int srcy1, int srcx2, int srcy2);
  50     abstract void dispose();
  51 
  52     @Override
  53     public Object getPlatformImage() {
  54        return getImage();
  55     }
  56 
  57     @Override
  58     public void deref() {
  59         super.deref();
  60         if (!hasRefs()) {
  61             dispose();
  62         }
  63     }
  64 
  65     @Override
  66     protected final String toDataURL(String mimeType) {
  67         Object image = UIClientImpl.toBufferedImage(javafx.scene.image.Image.impl_fromPlatformImage(getImage()));
  68         if (image instanceof BufferedImage) {
  69             Iterator<ImageWriter> it = ImageIO.getImageWritersByMIMEType(mimeType);
  70             while (it.hasNext()) {
  71                 ByteArrayOutputStream output = new ByteArrayOutputStream();
  72                 ImageWriter writer = it.next();
  73                 try {
  74                     writer.setOutput(ImageIO.createImageOutputStream(output));
  75                     writer.write((BufferedImage) image);
  76                 }
  77                 catch (IOException exception) {
  78                     continue; // try next image writer
  79                 }
  80                 finally {
  81                     writer.dispose();
  82                 }
  83                 StringBuilder sb = new StringBuilder();
  84                 sb.append("data:").append(mimeType).append(";base64,");
  85                 sb.append(Base64.getMimeEncoder().encodeToString(output.toByteArray()));
  86                 return sb.toString();
  87             }


   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 com.sun.javafx.webkit.prism;
  27 
  28 import com.sun.javafx.tk.Toolkit;
  29 import java.awt.image.BufferedImage;
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.IOException;
  32 import java.util.Base64;
  33 import java.util.Iterator;
  34 import javax.imageio.ImageIO;
  35 import javax.imageio.ImageWriter;
  36 
  37 import com.sun.javafx.webkit.UIClientImpl;
  38 import com.sun.prism.Image;
  39 import com.sun.prism.Graphics;
  40 import com.sun.webkit.graphics.WCImage;
  41 
  42 /**
  43  * @author Alexey.Ushakov
  44  */
  45 abstract class PrismImage extends WCImage {
  46     abstract Image getImage();
  47     abstract Graphics getGraphics();
  48     abstract void draw(Graphics g,
  49             int dstx1, int dsty1, int dstx2, int dsty2,
  50             int srcx1, int srcy1, int srcx2, int srcy2);
  51     abstract void dispose();
  52 
  53     @Override
  54     public Object getPlatformImage() {
  55        return getImage();
  56     }
  57 
  58     @Override
  59     public void deref() {
  60         super.deref();
  61         if (!hasRefs()) {
  62             dispose();
  63         }
  64     }
  65 
  66     @Override
  67     protected final String toDataURL(String mimeType) {
  68         Object image = UIClientImpl.toBufferedImage(Toolkit.getImageAccessor().fromPlatformImage(getImage()));
  69         if (image instanceof BufferedImage) {
  70             Iterator<ImageWriter> it = ImageIO.getImageWritersByMIMEType(mimeType);
  71             while (it.hasNext()) {
  72                 ByteArrayOutputStream output = new ByteArrayOutputStream();
  73                 ImageWriter writer = it.next();
  74                 try {
  75                     writer.setOutput(ImageIO.createImageOutputStream(output));
  76                     writer.write((BufferedImage) image);
  77                 }
  78                 catch (IOException exception) {
  79                     continue; // try next image writer
  80                 }
  81                 finally {
  82                     writer.dispose();
  83                 }
  84                 StringBuilder sb = new StringBuilder();
  85                 sb.append("data:").append(mimeType).append(";base64,");
  86                 sb.append(Base64.getMimeEncoder().encodeToString(output.toByteArray()));
  87                 return sb.toString();
  88             }
< prev index next >