|
| 1 | +package org.openspim.toolkit; |
| 2 | + |
| 3 | +import ij.IJ; |
| 4 | +import ij.ImagePlus; |
| 5 | +import ij.ImageStack; |
| 6 | + |
| 7 | +import java.awt.Component; |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | +import java.io.File; |
| 11 | +import java.io.FilenameFilter; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | + |
| 15 | +import javax.swing.JButton; |
| 16 | +import javax.swing.JCheckBox; |
| 17 | +import javax.swing.JFileChooser; |
| 18 | +import javax.swing.JTextField; |
| 19 | +import javax.swing.filechooser.FileNameExtensionFilter; |
| 20 | + |
| 21 | +import org.openspim.gui.LayoutUtils; |
| 22 | + |
| 23 | +public class SliceCollector implements Processor { |
| 24 | + @Override |
| 25 | + public int getCapabilities() { |
| 26 | + return Processor.POST_FUSION; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void performProcessing(Params params) throws InterruptedException, ExecutionException { |
| 31 | + File exemplar = params.spec.iterator().next(); |
| 32 | + File[] files = OpenSPIMToolkit.getOutputDirectory(exemplar).listFiles(tifFilter); |
| 33 | + Arrays.sort(files); |
| 34 | + |
| 35 | + ImageStack stack = null; |
| 36 | + |
| 37 | + for (File f : files) { |
| 38 | + ImagePlus imp = IJ.openImage(f.getAbsolutePath()); |
| 39 | + |
| 40 | + if(stack == null) |
| 41 | + stack = new ImageStack(imp.getWidth(), imp.getHeight()); |
| 42 | + |
| 43 | + stack.addSlice(imp.getProcessor()); |
| 44 | + imp.close(); |
| 45 | + } |
| 46 | + |
| 47 | + ImagePlus total = new ImagePlus("collected-output", stack); |
| 48 | + if(!pathBox.getText().isEmpty()) |
| 49 | + IJ.saveAsTiff(total, pathBox.getText()); |
| 50 | + |
| 51 | + if(show.isSelected()) |
| 52 | + total.show(); |
| 53 | + else |
| 54 | + total.close(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String toString() { |
| 59 | + return "Slice Collector"; |
| 60 | + } |
| 61 | + |
| 62 | + private static FilenameFilter tifFilter = new FilenameFilter() { |
| 63 | + @Override |
| 64 | + public boolean accept(File dir, String name) { |
| 65 | + return name.endsWith(".tif") || name.endsWith(".tiff"); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + private static JTextField pathBox = new JTextField("collected-output.tif", 24); |
| 70 | + private static JButton browseBtn = new JButton("Browse"); |
| 71 | + private static JFileChooser chooser = new JFileChooser(); |
| 72 | + private static JCheckBox show = new JCheckBox(); |
| 73 | + |
| 74 | + static { |
| 75 | + chooser.addChoosableFileFilter(new FileNameExtensionFilter("Tagged Image File Format", "tif", "tiff")); |
| 76 | + chooser.setAcceptAllFileFilterUsed(true); |
| 77 | + |
| 78 | + browseBtn.addActionListener(new ActionListener() { |
| 79 | + @Override |
| 80 | + public void actionPerformed(ActionEvent ae) { |
| 81 | + if(chooser.showSaveDialog(browseBtn) != JFileChooser.APPROVE_OPTION) |
| 82 | + return; |
| 83 | + |
| 84 | + pathBox.setText(chooser.getSelectedFile().getAbsolutePath()); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Component getControlPanel() { |
| 91 | + return LayoutUtils.form( |
| 92 | + "Save Path:", LayoutUtils.horizPanel(pathBox, browseBtn), |
| 93 | + "Show Final Image:", show |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments