2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.helptools.image;
15 import java.awt.Graphics2D;
16 import java.awt.Color;
17 import java.awt.image.BufferedImage;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.StringReader;
24 import java.nio.channels.FileChannel;
25 import java.util.HashMap;
28 import javax.activation.MimetypesFileTypeMap;
29 import javax.imageio.ImageIO;
30 import javax.swing.Icon;
31 import javax.swing.ImageIcon;
32 import javax.swing.JLabel;
35 * Class to handle the image conversion
36 * @author Calixte DENIZET
38 public final class ImageConverter {
40 private static Map<String, ExternalImageConverter> externalConverters = new HashMap();
41 private static MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap();
44 mimeMap.addMimeTypes("type=image/latex exts=tex,latex");
45 mimeMap.addMimeTypes("type=image/mathml exts=mml,mathml");
46 mimeMap.addMimeTypes("type=image/svg exts=svg");
50 * Register a new ExternalImageConverter
51 * @param c the converter to register
53 public static void registerExternalImageConverter(ExternalImageConverter c) {
55 externalConverters.put(c.getMimeType(), c);
60 * @param attrs the attribute of the image
61 * @param path the current XML file which is parsed
62 * @param image the filename
63 * @param destDir the destination directory
64 * @return the HTML code to insert the image
66 public static String getImageByFile(Map<String, String> attrs, String path, String image, String outputDir, String destDir) {
67 File f = new File(image);
68 if (!f.isAbsolute()) {
69 f = new File(path + File.separator + image);
72 String destFile = outputDir + File.separator + destDir + File.separator + f.getName();
74 ExternalImageConverter conv = externalConverters.get(mimeMap.getContentType(f));
78 File imageFile = new File(destFile);
79 String imageName = destDir + "/" + imageFile.getName();
81 if (f.lastModified() > imageFile.lastModified()) {
83 return conv.convertToImage(f, attrs, imageFile, imageName);
85 copyImageFile(f, destDir);
88 return "<img src=\'" + imageName + "\'/>";
93 * @param code the code to translate
94 * @param attrs the attribute of the image
96 * @param imageFile the filename
97 * @return the HTML code to insert the image
99 public static String getImageByCode(String code, Map<String, String> attrs, String mime, File imageFile, String imageName) {
100 ExternalImageConverter conv = externalConverters.get(mime);
102 return conv.convertToImage(code, attrs, imageFile, imageName);
104 System.err.println("Code not handled:\n" + code);
110 * @param code the code to translate
111 * @param img image informations
112 * @param fileName the filename
113 * @param attrs the attribute of the image
114 * @return the HTML code to insert the image
116 public static String generateCode(Image img, String fileName, Map<String, String> attrs) {
117 String style = (String) attrs.get("style");
119 boolean display = style != null && style.equals("display");
122 top = "top:" + img.descent + "px;";
125 String alignAttr = (String) attrs.get("align");
128 if (alignAttr != null) {
129 align = " style=\'text-align:" + alignAttr + "\'";
130 } else if (display) {
131 align = " style=\'text-align:center\'";
136 return "<" + div + align + "><img src=\'" + fileName + "\' style=\'position:relative;" + top + "width:" + img.width + "px;height:" + img.height + "px\'/></" + div + ">";
140 * Test if an image file exists.
141 * @param path of the parsed file
142 * @param image the image name
143 * @return true if the image exists
145 public static boolean imageExists(String path, String image) {
146 File f = new File(image);
147 if (!f.isAbsolute()) {
148 f = new File(path + File.separator + image);
155 * @param f the file to copy
156 * @param destDir the destination directory
158 public static void copyImageFile(File f, String destDir) {
159 FileChannel src = null;
160 FileChannel dest = null;
162 File destFile = new File(destDir + File.separator + f.getName());
163 if (!destFile.exists()) {
164 destFile.createNewFile();
165 } else if (f.lastModified() <= destFile.lastModified()) {
169 src = new FileInputStream(f).getChannel();
170 dest = new FileOutputStream(destFile).getChannel();
171 dest.transferFrom(src, 0, src.size());
172 } catch (IOException e) {
173 System.err.println(e);
183 } catch (IOException e) {
184 System.err.println(e);
190 * @param icon the icon to convert into PNG
191 * @param imageFile the destination file
192 * @return true if all is ok
194 public static boolean convertIconToPNG(Icon icon, File imageFile) {
195 BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
196 Graphics2D g2d = image.createGraphics();
197 icon.paintIcon(null, g2d, 0, 0);
200 ImageIO.write(image, "png", imageFile.getAbsoluteFile());
201 } catch (IOException ex) {