ThreeB 1.1
ThreeBLoader.java
Go to the documentation of this file.
00001 import ij.*;
00002 import ij.process.*;
00003 import ij.gui.*;
00004 import java.awt.*;
00005 import ij.plugin.*;
00006 import ij.plugin.frame.*;
00007 import ij.plugin.filter.PlugInFilter;
00008 import ij.*;
00009 import ij.io.*;
00010 import ij.plugin.*;
00011 import ij.plugin.filter.*;
00012 
00013 import java.awt.event.*;
00014 import java.awt.geom.*;
00015 import java.util.*;
00016 import java.util.zip.*;
00017 import java.io.*;
00018 import javax.swing.*;
00019 import javax.swing.event.*;
00020 import java.lang.InterruptedException;
00021 import java.lang.System;
00022 import java.lang.Math.*;
00023 
00024 ///Plugin class to load up an old 3B run.
00025 ///
00026 ///@ingroup gPlugin
00027 public class  ThreeBLoader implements PlugIn {
00028     
00029     private void close(InputStream i)
00030     {
00031         //Not sure what to do here...
00032         if(i != null)
00033         {
00034             try{
00035                 i.close();
00036             }
00037             catch(IOException ioe)
00038             {
00039                 Toolkit.getDefaultToolkit().beep();
00040                 ij.IJ.showStatus("Error closing file: " + ioe.getMessage());
00041             }
00042         }
00043     }
00044 
00045     public void run(String arg) {
00046         InputStream in = null;
00047         String name;
00048         double ps;
00049 
00050         try{
00051             if(arg.equals("test"))
00052             {
00053                 name = "test_data.txt";
00054 
00055                 try{
00056                     in = new GZIPInputStream(getClass().getClassLoader().getResourceAsStream("test_data.txt.gz"));
00057                 }
00058                 catch(IOException ioe)
00059                 {
00060                     Toolkit.getDefaultToolkit().beep();
00061                     ij.IJ.showStatus("Could not open test data: " + ioe.getMessage());
00062                     return;
00063                 }
00064                 ps=100;
00065             }
00066             else
00067             {
00068                 OpenDialog o = new OpenDialog("Open 3B run...", null);
00069                 name = o.getFileName();
00070                 
00071                 try{
00072                     in = new FileInputStream(o.getDirectory() + o.getFileName());
00073                 }
00074                 catch(java.io.FileNotFoundException ferr)
00075                 {
00076                     Toolkit.getDefaultToolkit().beep();
00077                     ij.IJ.showStatus("Error opening file: " + ferr.getMessage());
00078                     return;
00079                 }
00080 
00081 
00082 
00083                 GenericDialog g = new GenericDialog("Pixel size");
00084                 g.addNumericField("Pixel size", 100., 0, 5, "nm");
00085                 g.showDialog();
00086                 ps = g.getNextNumber();
00087             }
00088 
00089             //Yay @ cargoculting
00090             InputStreamReader d = new InputStreamReader(in);
00091             BufferedReader r = new BufferedReader(d);
00092 
00093             String line;
00094             final ArrayList<Spot> spots = new ArrayList<Spot>();
00095             int iterations=0;
00096             Rectangle roi=null;
00097             final double pixel_size_in_nm_ = ps;
00098 
00099             while((line = r.readLine()) != null)
00100             {
00101                 String tokens[] = line.split("\\p{Space}+");
00102 
00103                 if(tokens[0].equals("PIXELS"))
00104                 {
00105                     if((tokens.length - 1)%2 != 0)
00106                         throw new IOException("corrupt file (bad pixels line)");
00107 
00108                     for(int i=1; i < tokens.length; i+= 2)
00109                     {
00110                         int x, y;
00111                         try
00112                         {
00113                             x = Integer.parseInt(tokens[i+0]);
00114                             y = Integer.parseInt(tokens[i+1]);
00115                         }
00116                         catch(NumberFormatException nerr)
00117                         {
00118                             throw new IOException("corrupt file (bad pixel coordinate)");
00119                         }
00120 
00121                         if(roi == null)
00122                             roi = new Rectangle(x, y, 1, 1);
00123                         else
00124                             roi.add(x, y);
00125                     }
00126                 }
00127 
00128                 if(tokens[0].matches("PASS[0-9]+:"))
00129                 {
00130                     iterations++;
00131 
00132                     if((tokens.length - 1)%4 != 0)
00133                         throw new IOException("corrupt file (pad pass line)");
00134 
00135                     for(int i=1; i < tokens.length; i+= 4)
00136                     {
00137                         Spot s = new Spot();
00138                         try
00139                         {
00140                             s.x = Double.parseDouble(tokens[i+2]);
00141                             s.y = Double.parseDouble(tokens[i+3]);
00142                         }
00143                         catch(NumberFormatException nerr)
00144                         {
00145                             throw new IOException("corrupt file (bad spot position)");
00146                         }
00147 
00148                         spots.add(s);
00149                     }
00150                 }
00151 
00152             }
00153             if(roi == null)
00154                 throw new IOException("corrupt file (no ROI)");
00155 
00156             final String fname = name;
00157             final Rectangle roi_ = roi;
00158             final int its = iterations;
00159             SwingUtilities.invokeLater(
00160             new Runnable() {
00161                     public void run() {
00162                             EControlPanel e = new EControlPanel(roi_, pixel_size_in_nm_, fname, null);
00163                             e.append(spots, its);
00164                             e.send_update_canvas_event();
00165                             e.send_status_text_message("Using " + fname + ": " + Integer.toString(its) + " iterations.");
00166                     }
00167             }
00168             );
00169 
00170 
00171 
00172 
00173         }
00174         catch(IOException another_ioe){
00175             Toolkit.getDefaultToolkit().beep();
00176             ij.IJ.showStatus("Error reading data: " + another_ioe.getMessage());
00177         }
00178         finally{
00179             close(in);
00180         }
00181 
00182     }
00183 
00184 
00185 
00186 }