//$Id: ImageHandler.java,v 1.4 2007/03/01 20:56:28 Sasha Buzko Exp $ // //Copyright (c) 2000-2003 San Diego Supercomputer Center (SDSC), //a facility operated jointly by the University of California, //San Diego (UCSD) and General Atomics, San Diego, California, USA. // //Users and possessors of this source code are hereby granted a //nonexclusive, royalty-free copyright and design patent license to //use this code in individual software. License is not granted for //commercial resale, in whole or in part, without prior written //permission from SDSC. This source is provided "AS IS" without express //or implied warranty of any kind. // //For further information, please see: // //History: //$Log: ImageHandler.java,v $ //Revision 1.4 2007/03/01 20:56:28 Sasha Buzko //Added MD video // //Revision 1.3 2006/10/21 18:41:27 Sasha Buzko //*** empty log message *** // //Revision 1.2 2006/07/25 04:08:05 Sasha Buzko //*** empty log message *** // //Revision 1.1 2006/05/20 17:02:09 Sasha Buzko //Updated version // //Revision 1.1 2006/04/30 20:14:06 Sasha Buzko //New version of the app // //Revision 1.1 2006/04/15 19:42:28 Sasha Buzko //Initial commit // //Revision 1.3 2006/03/09 19:01:51 Administrator //*** empty log message *** // //Revision 1.2 2005/11/13 23:44:36 Administrator //*** empty log message *** // //Revision 1.1 2005/11/13 04:35:07 Administrator //*** empty log message *** // //Revision 1.2 2003/05/15 23:44:54 moreland //Cleaned up code a bit for a preliminary release to obuzko. // //Revision 1.1 2003/05/15 22:42:45 moreland //An application which incorporates more MBT GUI layer components. // //Revision 1.0 2003/05/05 16:06:13 moreland //First version. // package edu.sdsc.mbt.image; import java.awt.image.*; import java.io.*; import java.util.*; import javax.imageio.*; import javax.imageio.spi.*; import javax.imageio.stream.*; import javax.swing.*; import java.awt.*; import edu.sdsc.mbt.viewables.*; import edu.sdsc.sirius.util.Manager; import edu.sdsc.sirius.util.FileNameFilter; public class ImageHandler { private static int number = 239; private static Vector colors = new Vector(); static { //hydrophobicity colors.add(new Color(0.93f, 0.51f, 0.93f));// Violet colors.add(new Color(0.29f, 0.00f, 0.51f));// Indigo colors.add(new Color(0.00f, 0.00f, 1.00f));// Blue colors.add(new Color(0.00f, 1.00f, 0.00f));// Green colors.add(new Color(1.00f, 1.00f, 0.00f));// Yellow colors.add(new Color(1.00f, 0.50f, 0.00f));// Orange colors.add(new Color(1.00f, 0.00f, 0.00f));// Red //type colors.add(new Color(0.0f, 0.745f, 0.3f)); colors.add(new Color(0.66f, 0.2f, 0.2f)); colors.add(new Color(1.0f, 0.0f, 0.0f)); colors.add(new Color(0.0f, 0.59f, 0.27f)); colors.add(new Color(0.86f, 0.157f, 0.58f)); colors.add(new Color(0.4f, 0.66f, 0.86f)); colors.add(new Color(0.0f, 0.0f, 0.9f)); colors.add(new Color(0.0f, 0.0f, 1.0f)); colors.add(new Color(0.78f, 0.78f, 0.235f)); colors.add(new Color(0.59f, 0.59f, 0.59f)); colors.add(new Color(1.0f, 0.5f, 0.31f)); colors.add(new Color(0.0f, 1.0f, 0.0f)); colors.add(new Color(0.0f, 0.588f, 0.27f)); colors.add(new Color(0.4f, 0.4f, 0.4f)); colors.add(new Color(0.0f, 0.0f, 0.0f)); //subdomain colors.add(new Color(178,0,0)); colors.add(new Color(255,153,25)); colors.add(new Color(255,255,0)); colors.add(new Color(102,255,102)); colors.add(new Color(25,127,0)); colors.add(new Color(0,127,76)); colors.add(new Color(127,76,230)); colors.add(new Color(76,76,230)); colors.add(new Color(178,0,230)); colors.add(new Color(178,0,127)); colors.add(new Color(255,153,230)); colors.add(new Color(102,51,102)); //conservation colors.add(Color.red); colors.add(new Color(255,153,25)); colors.add(new Color(240,240,0)); colors.add(new Color(0,200,90)); colors.add(new Color(0,190,255)); colors.add(new Color(0,100,255)); colors.add(new Color(180,100,220)); //overhead secondary structure float[] hColor = StylesPreferences.HELIX_COLOR; colors.add(new Color(hColor[0], hColor[1], hColor[2])); float[] sColor = StylesPreferences.STRAND_COLOR; colors.add(new Color(sColor[0], sColor[1], sColor[2])); float[] cColor = StylesPreferences.COIL_COLOR; colors.add(new Color(cColor[0], cColor[1], cColor[2])); float[] tColor = StylesPreferences.TURN_COLOR; colors.add(new Color(tColor[0], tColor[1], tColor[2])); //common colors used in Structure Viewer coloring colors.add(Color.WHITE); colors.add(new Color(160,160,160)); colors.add(Color.black); colors.add(Color.red); colors.add(Color.yellow); colors.add(new Color(210,210,0)); colors.add(new Color(150,150,0)); colors.add(new Color(255,100,200)); colors.add(new Color(100,200,255)); colors.add(new Color(100,100,255)); colors.add(Color.blue); colors.add(new Color(200,0,200)); colors.add(Color.green); colors.add(new Color(0,200,0)); colors.add(new Color(0,150,0)); colors.add(new Color(150,0,0)); number = colors.size(); // System.out.println(number + " colors"); }; public static void saveImage( Manager parent, BufferedImage image ) throws IOException { JFileChooser chooser = new JFileChooser(StylesPreferences.workingDirectory); chooser.setDialogTitle("Save image"); chooser.setDialogType(JFileChooser.SAVE_DIALOG); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //get image formats HashMap map = new HashMap();//filter -> descriptor Vector formats = getWriterFormats(); for (int i = 0; i < formats.size(); i++){ FormatDescriptor fd = (FormatDescriptor)formats.elementAt(i); final FileNameFilter filter = new FileNameFilter(fd.suffix, fd.description); chooser.addChoosableFileFilter(filter); map.put(filter, fd); } int result = chooser.showSaveDialog(null); if (result == JFileChooser.CANCEL_OPTION){ return; } File filename = chooser.getSelectedFile(); javax.swing.filechooser.FileFilter currentFilter = chooser.getFileFilter(); String fname = filename.toString(); FormatDescriptor descriptor = (FormatDescriptor)map.get(currentFilter); int dot = fname.lastIndexOf("."); if (dot <= 0){ //no extension supplied fname += "." + descriptor.suffix; } // Get the ImageWriter stored in this FormatDescriptor ImageWriter imageWriter = descriptor.imageWriter; //check the output location File f = new File(fname); if (f.exists()){ if (f.isDirectory()){ parent.displayErrorMessage("Selected destination is a directory"); } if (!f.canWrite()){ parent.displayErrorMessage("Selected file is not writable"); } else{ //ask the user if it's ok to overwrite the file if (JOptionPane.showConfirmDialog( parent.getApplicationFrame(), "File with this name exists. Overwrite?", "Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION){ return; } } } /// ImageIO.write(image, ) // Get an output stream for the file FileOutputStream fout = new FileOutputStream( new File(fname) ); // Turn it into an ImageOutputStreamn ImageOutputStream ios = ImageIO.createImageOutputStream( fout ); // Plug this stream into the ImageWriter imageWriter.setOutput( ios ); // Write the image imageWriter.write( image ); // Dont forget to close the file! fout.close(); image = null; } public static void saveImage( BufferedImage image, String filename ) throws IOException { //get image formats Vector formats = getWriterFormats(); FormatDescriptor descriptor = null; for (int i = 0; i < formats.size(); i++){ FormatDescriptor fd = (FormatDescriptor)formats.elementAt(i); if (fd.suffix.equals("jpg")){ descriptor = fd; break; } } String fname = filename.toString(); // Get the ImageWriter stored in this FormatDescriptor ImageWriter imageWriter = descriptor.imageWriter; // Get an output stream for the file FileOutputStream fout = new FileOutputStream( new File(fname) ); // Turn it into an ImageOutputStreamn ImageOutputStream ios = ImageIO.createImageOutputStream( fout ); // Plug this stream into the ImageWriter imageWriter.setOutput( ios ); // Write the image imageWriter.write( image ); // Dont forget to close the file! fout.close(); imageWriter.dispose(); image = null; formats = null; descriptor = null; } /** * Describes a Format plug-in for use as a JFileChooser filter. */ static private class FormatDescriptor { public String suffix; public ImageWriter imageWriter; public String description; public FormatDescriptor( String suffix, ImageWriter imageWriter, String description ) { this.suffix = suffix; this.imageWriter = imageWriter; this.description = description; } public String toString( ) { return description; } } /** * Return a vector of FormatDescriptor objects, one for each * supported Image Format plug-ins. */ static private Vector getWriterFormats( ) { Vector formats = new Vector( ); Hashtable seen = new Hashtable( ); String names[] = ImageIO.getWriterFormatNames( ); for ( int i=0; i