001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.main.saveload.image;
005    
006    import graphlab.graph.graph.AbstractGraphRenderer;
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.platform.Application;
009    import graphlab.platform.parameter.Parameter;
010    import graphlab.platform.parameter.Parametrizable;
011    import graphlab.platform.preferences.lastsettings.UserModifiableProperty;
012    import graphlab.plugins.main.saveload.core.GraphIOException;
013    import graphlab.plugins.main.saveload.core.extension.GraphWriterExtension;
014    
015    import javax.imageio.ImageIO;
016    import java.io.File;
017    
018    /**
019     * @author Azin Azadi
020    
021     */
022    public class SaveImage implements GraphWriterExtension, Parametrizable {
023        @UserModifiableProperty(displayName = "Default Image Extension")
024        @Parameter(name = "File Extension", description = "")
025        public static String extension = "png";
026    
027        /**
028         * saves g in file as a (.extension) image
029         *
030         * @param extension eg. jpeg, png, bmp, ... It can be any extension supported by ImageIO.write
031         */
032        public static void saveImage(GraphModel g, final File ff, String extension) {
033            Image.save(Image.Graph2Image((AbstractGraphRenderer.getCurrentGraphRenderer(Application.blackboard)), g), ff, extension);
034        }
035    
036        public String getName() {
037            return "Image";
038        }
039    
040        public String getExtension() {
041            return extension;
042        }
043    
044        public void write(File file, GraphModel graph) throws GraphIOException {
045            //todo
046    //        throw new RuntimeException("not implemented");
047            saveImage(graph, file, extension);
048        }
049    
050        public String getDescription() {
051            return extension + " File Format";
052        }
053    
054        public String checkParameters() {
055            if (!ImageIO.getImageWritersByFormatName(extension).hasNext())
056                return "This file format is not supported!";
057            else
058                return null;
059        }
060    }