Changeset 714 for trunk/loci/visbio/view


Ignore:
Timestamp:
02/24/06 14:49:33 (14 years ago)
Author:
curtis
Message:

Begin adaptation to new loci.formats package.
Current code works, but still uses ImageFamily, which will be phased out.

Location:
trunk/loci/visbio/view
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/view/CaptureHandler.java

    r630 r714  
    2626import ij.*; 
    2727import ij.io.FileSaver; 
    28 import java.awt.Image; 
     28import java.awt.image.BufferedImage; 
    2929import java.io.IOException; 
    30 import java.io.File; 
    3130import java.rmi.RemoteException; 
    3231import java.util.Vector; 
     
    3433import javax.swing.JOptionPane; 
    3534import javax.swing.filechooser.FileFilter; 
     35import loci.formats.*; 
    3636import loci.visbio.SystemManager; 
    3737import loci.visbio.WindowManager; 
     
    4040import org.w3c.dom.Element; 
    4141import visad.*; 
    42 import visad.data.avi.AVIForm; 
    43 import visad.util.*; 
     42import visad.util.Util; 
    4443 
    4544/** Provides logic for capturing display screenshots and movies. */ 
     
    111110 
    112111  /** Gets a snapshot of the display. */ 
    113   public Image getSnapshot() { return window.getDisplay().getImage(); } 
     112  public BufferedImage getSnapshot() { return window.getDisplay().getImage(); } 
    114113 
    115114  /** Saves a snapshot of the display to a file specified by the user. */ 
     
    195194 
    196195    // get output filename(s) from the user 
    197     String file = null; 
    198     int dot = -1; 
     196    String name = null; 
    199197    boolean tiff = false, jpeg = false; 
    200198    if (movie) { 
    201199      int rval = movieBox.showSaveDialog(captureWindow); 
    202200      if (rval != JFileChooser.APPROVE_OPTION) return; 
    203       file = movieBox.getSelectedFile().getPath(); 
    204       if (file.indexOf(".") < 0) file = file + ".avi"; 
     201      name = movieBox.getSelectedFile().getPath(); 
     202      if (name.indexOf(".") < 0) name += ".avi"; 
    205203    } 
    206204    else { 
    207205      int rval = imageBox.showSaveDialog(captureWindow); 
    208206      if (rval != JFileChooser.APPROVE_OPTION) return; 
    209       file = imageBox.getSelectedFile().getPath(); 
     207      name = imageBox.getSelectedFile().getPath(); 
     208      String sel = ((ExtensionFileFilter) 
     209        imageBox.getFileFilter()).getExtension(); 
    210210      String ext = ""; 
    211       dot = file.lastIndexOf("."); 
    212       if (dot >= 0) ext = file.substring(dot + 1).toLowerCase(); 
    213       tiff = ext.equals("tif") || ext.equals("tiff"); 
    214       jpeg = ext.equals("jpg") || ext.equals("jpeg"); 
    215       FileFilter filter = imageBox.getFileFilter(); 
    216       String desc = filter.getDescription(); 
    217       if (desc.startsWith("JPEG")) { 
    218         if (!jpeg) { 
    219           file += ".jpg"; 
    220           jpeg = true; 
    221           dot = file.lastIndexOf("."); 
    222         } 
    223       } 
    224       else if (desc.startsWith("TIFF")) { 
    225         if (!tiff) { 
    226           file += ".tif"; 
    227           tiff = true; 
    228           dot = file.lastIndexOf("."); 
    229         } 
    230       } 
    231       if (!tiff && !jpeg) { 
    232         JOptionPane.showMessageDialog(captureWindow, "Invalid filename (" + 
    233           file + "): extension must be TIFF or JPEG.", 
    234           "Cannot create image sequence", JOptionPane.ERROR_MESSAGE); 
    235         return; 
    236       } 
     211      int dot = name.lastIndexOf("."); 
     212      if (dot >= 0) ext = name.substring(dot + 1).toLowerCase(); 
     213      if (sel.equals("tif") && !ext.equals("tif")) name += ".tif"; 
     214      else if (sel.equals("jpg") && !ext.equals("jpg")) name += ".jpg"; 
    237215    } 
    238216 
    239217    // capture image sequence in a separate thread 
    240     final boolean aviMovie = movie; 
    241     final String filename = file; 
    242     final int dotIndex = dot; 
    243     final boolean isTiff = tiff, isJpeg = jpeg; 
     218    final boolean doMovie = movie; 
     219    final String filename = name; 
    244220    final Vector pos = matrices; 
    245221    final int frm = framesPerTrans; 
     
    252228        wm.setWaitCursor(true); 
    253229 
    254         // step incremental from position to position, grabbing images 
     230        setProgress(0, "Capturing movie"); 
     231 
     232        String prefix, ext; 
     233        int dot = filename.lastIndexOf("."); 
     234        if (dot < 0) dot = filename.length(); 
     235        String pre = filename.substring(0, dot); 
     236        String post = filename.substring(dot + 1); 
     237 
     238        // step incrementally from position to position, grabbing images 
     239        int count = 1; 
     240        ImageWriter writer = new ImageWriter(); 
    255241        double[] mxStart = (double[]) pos.elementAt(0); 
    256         Image[] images = new Image[total]; 
    257         int count = 0; 
    258242        for (int i=1; i<size; i++) { 
    259243          double[] mxEnd = (double[]) pos.elementAt(i); 
    260244          double[] mx = new double[mxStart.length]; 
    261245          for (int j=0; j<frm; j++) { 
    262             setProgress(100 * count / total, 
    263               "Capturing image " + (count + 1) + "/" + total); 
     246            setProgress(100 * (count - 1) / total, 
     247              "Saving image " + count + "/" + total); 
    264248            double p = (double) j / frm; 
    265249            if (doSine) p = sine(p); 
     
    267251              mx[k] = p * (mxEnd[k] - mxStart[k]) + mxStart[k]; 
    268252            } 
    269             images[count++] = captureImage(pc, mx, d); 
     253            BufferedImage image = captureImage(pc, mx, d); 
     254            String name = doMovie ? filename : (pre + count + post); 
     255            try { writer.save(name, image, !doMovie); } 
     256            catch (IOException exc) { exc.printStackTrace(); } 
     257            catch (FormatException exc) { exc.printStackTrace(); } 
     258            count++; 
    270259          } 
    271260          mxStart = mxEnd; 
     
    273262 
    274263        // cap off last frame 
    275         setProgress(100, "Capturing image " + total + "/" + total); 
    276         images[count] = captureImage(pc, mxStart, d); 
    277  
    278         // save movie data 
    279         if (aviMovie) { 
    280           try { 
    281             // convert image frames into VisAD data objects 
    282             FlatField[] ff = new FlatField[total]; 
    283             for (int i=0; i<total; i++) { 
    284               setProgress(100 * i / total, 
    285                 "Processing image " + (i + 1) + "/" + total); 
    286               ff[i] = DataUtility.makeField(images[i]); 
    287             } 
    288             setProgress(100, "Saving movie"); 
    289  
    290             // compile frames into a single data object 
    291             RealType index = RealType.getRealType("index"); 
    292             FieldImpl field = DataUtil.makeField(ff, index); 
    293  
    294             // write data out to AVI file 
    295             AVIForm saver = new AVIForm(); 
    296             saver.setFrameRate(fps); 
    297             saver.save(filename, field, true); 
    298           } 
    299           catch (VisADException exc) { exc.printStackTrace(); } 
    300           catch (RemoteException exc) { exc.printStackTrace(); } 
    301           catch (IOException exc) { exc.printStackTrace(); } 
    302         } 
    303         else { 
    304           for (int i=0; i<total; i++) { 
    305             String num = "" + (i + 1); 
    306             int len = ("" + total).length(); 
    307             while (num.length() < len) num = "0" + num; 
    308             String s = filename.substring(0, dotIndex) + 
    309               num + filename.substring(dotIndex); 
    310             setProgress(100 * i / total, "Saving " + 
    311               new File(s).getName() + " (" + (i + 1) + "/" + total + ")"); 
    312             FileSaver saver = new FileSaver(new ImagePlus("null", images[i])); 
    313             if (isTiff) saver.saveAsTiff(s); 
    314             else if (isJpeg) saver.saveAsJpeg(s); 
    315           } 
    316         } 
     264        setProgress(100, "Saving image " + count + "/" + total); 
     265        BufferedImage image = captureImage(pc, mxStart, d); 
     266        String name = doMovie ? filename : (pre + count + post); 
     267        try { writer.save(name, image, true); } 
     268        catch (IOException exc) { exc.printStackTrace(); } 
     269        catch (FormatException exc) { exc.printStackTrace(); } 
    317270 
    318271        // clean up 
    319272        setProgress(100, "Finishing up"); 
    320         images = null; 
     273        image = null; 
    321274        SystemManager.gc(); 
    322275 
     
    369322      // snapshot file chooser 
    370323      imageBox = new JFileChooser(); 
    371       imageBox.addChoosableFileFilter(new ExtensionFileFilter( 
    372         new String[] {"jpg", "jpeg"}, "JPEG files")); 
    373       imageBox.addChoosableFileFilter(new ExtensionFileFilter( 
    374         new String[] {"tif", "tiff"}, "TIFF files")); 
     324      imageBox.addChoosableFileFilter( 
     325        new ExtensionFileFilter("jpg", "JPEG images")); 
     326      imageBox.addChoosableFileFilter( 
     327        new ExtensionFileFilter("tif", "TIFF images")); 
    375328 
    376329      // movie file chooser 
    377330      movieBox = new JFileChooser(); 
    378       movieBox.addChoosableFileFilter(new ExtensionFileFilter( 
    379         new String[] {"avi"}, "AVI movies")); 
     331      movieBox.addChoosableFileFilter( 
     332        new ExtensionFileFilter("avi", "AVI movies")); 
    380333    } 
    381334 
     
    442395   * with the specified projection matrix. 
    443396   */ 
    444   protected Image captureImage(ProjectionControl pc, 
     397  protected BufferedImage captureImage(ProjectionControl pc, 
    445398    double[] mx, DisplayImpl d) 
    446399  { 
    447     Image image = null; 
     400    BufferedImage image = null; 
    448401    try { 
    449402      pc.setMatrix(mx); 
  • trunk/loci/visbio/view/ColorPane.java

    r630 r714  
    3535import javax.swing.border.TitledBorder; 
    3636import javax.swing.event.*; 
     37import loci.formats.ExtensionFileFilter; 
    3738import loci.visbio.util.*; 
    3839import visad.*; 
     
    4041import visad.java2d.DisplayImplJ2D; 
    4142import visad.util.ColorMapWidget; 
    42 import visad.util.ExtensionFileFilter; 
    4343 
    4444/** ColorPane is a dialog pane for adjusting color settings. */ 
Note: See TracChangeset for help on using the changeset viewer.