Implements a transcoder to generate a ImageIcon 92/16592/5
Marcos CARDINOT [Tue, 2 Jun 2015 23:01:00 +0000 (20:01 -0300)]
Change-Id: If2e7a26b1bd44602b833d0d7ac7806af515c0c7c

scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/ImageIconTranscoder.java [new file with mode: 0644]

diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/ImageIconTranscoder.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/ImageIconTranscoder.java
new file mode 100644 (file)
index 0000000..4ab9805
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2015 - Marcos CARDINOT
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ */
+
+package org.scilab.modules.xcos.utils;
+
+import java.awt.image.BufferedImage;
+
+import javax.swing.ImageIcon;
+
+import org.apache.batik.transcoder.TranscoderException;
+import org.apache.batik.transcoder.TranscoderOutput;
+import org.apache.batik.transcoder.image.ImageTranscoder;
+
+/**
+ * A simple transcoder that can be used to generate ImageIcon and BufferedImage.
+ * @author Marcos Cardinot <mcardinot@gmail.com>
+ */
+public class ImageIconTranscoder extends ImageTranscoder {
+    private BufferedImage bufferedImage;
+    private ImageIcon imgIcon;
+
+    /**
+     * Default constructor
+     */
+    public ImageIconTranscoder() {
+    }
+
+    /**
+     * Creates a new ARGB image with the specified dimension.
+     * @param width the image width
+     * @param height the image height
+     * @return BufferedImage
+     */
+    public BufferedImage createImage(int width, int height) {
+        return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+    }
+
+    /**
+     * Writes a image.
+     * @param img the image to write
+     * @param output the output where to store the image
+     * @throws TranscoderException if an error occurred while storing the image
+     */
+    public void writeImage(BufferedImage img, TranscoderOutput output)
+            throws TranscoderException {
+        bufferedImage = img;
+        imgIcon = new ImageIcon(img);
+    }
+
+    /**
+     * Get the image in BufferedImage format
+     * @return BufferedImage
+     */
+    public BufferedImage getBufferedImage() {
+        return bufferedImage;
+    }
+
+    /**
+     * Get the image in ImageIcon format
+     * @return ImageIcon
+     */
+    public ImageIcon getImageIcon() {
+        return imgIcon;
+    }
+
+    /**
+     * Set the dimension of the image.
+     * @param width the image width
+     * @param height the image height
+     */
+    public void setDimensions(int width, int height) {
+        hints.put(KEY_WIDTH, new Float(width));
+        hints.put(KEY_HEIGHT, new Float(height));
+    }
+}