View Javadoc
1   package com.github.celldynamics.quimp.utils;
2   
3   import java.util.concurrent.TimeUnit;
4   
5   import ij.ImageJ;
6   import ij.ImagePlus;
7   import ij.WindowManager;
8   import ij.plugin.RGBStackMerge;
9   import ij.process.ColorProcessor;
10  import ij.process.ImageProcessor;
11  import ij.process.LUT;
12  
13  /**
14   * Contain IJ based procedures.
15   * 
16   * @author p.baniukiewicz
17   *
18   */
19  public class IJTools {
20  
21    /**
22     * Time to wait after exit.
23     * 
24     * @see #exitIj(ImageJ)
25     */
26    public static int WAIT_TIME = 2;
27  
28    /**
29     * Return composite image created from background cell image and FG and BG pixels.
30     * 
31     * @param org Original image
32     * @param small Foreground mask
33     * @param big Background mask
34     * @return Composite image
35     */
36    public static ImagePlus getComposite(ImagePlus org, ImagePlus small, ImagePlus big) {
37      ImagePlus ret = RGBStackMerge
38              .mergeChannels(new ImagePlus[] { small, big, null, org, null, null, null }, false);
39      return ret;
40    }
41  
42    /**
43     * Return RGB image created from R, G, B 8-bit images.
44     * 
45     * <p>Slices can be also grayscale images serve as background.
46     * 
47     * @param red red slice
48     * @param green green slice
49     * @param blue blue slice
50     * @return Composite image
51     */
52    public static ColorProcessor getRGB(ImagePlus red, ImagePlus green, ImagePlus blue) {
53      return getRGB(red.getProcessor(), green.getProcessor(), blue.getProcessor());
54    }
55  
56    /**
57     * Return RGB image created from R, G, B 8-bit images.
58     * 
59     * <p>Slices can be also grayscale images serve as background.
60     * 
61     * @param red red slice
62     * @param green green slice
63     * @param blue blue slice
64     * @return Composite image
65     */
66    public static ColorProcessor getRGB(ImageProcessor red, ImageProcessor green,
67            ImageProcessor blue) {
68      ColorProcessor ret = new ColorProcessor(red.getWidth(), red.getHeight());
69  
70      ret.setChannel(1, red.convertToByteProcessor());
71      ret.setChannel(2, green.convertToByteProcessor());
72      ret.setChannel(3, blue.convertToByteProcessor());
73      return ret;
74    }
75  
76    /**
77     * Convert LUT to grayscale.
78     * 
79     * @return 8-bit grayscale LUT
80     */
81    public static LUT getGrayLut() {
82      byte[] l = new byte[256];
83      for (int i = 0; i < 256; i++) {
84        l[i] = (byte) i;
85      }
86      return new LUT(l, l, l);
87    }
88  
89    /**
90     * Close all images without saving.
91     */
92    public static void closeAllImages() {
93      int[] img = WindowManager.getIDList();
94      if (img != null) {
95        for (int s : img) {
96          ImagePlus id = WindowManager.getImage(s);
97          if (id != null) {
98            id.changes = false;
99          }
100       }
101     }
102     WindowManager.closeAllWindows();
103   }
104 
105   /**
106    * Exit IJ and wait time to accomplish.
107    * 
108    * @param ij ImageJ app object
109    * 
110    * @throws InterruptedException InterruptedException
111    */
112   public static void exitIj(ImageJ ij) throws InterruptedException {
113     if (ij != null) {
114       ij.quit();
115       TimeUnit.SECONDS.sleep(WAIT_TIME);
116     }
117   }
118 
119 }