Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Converter.java
1 package ij.plugin;
2 import ij.*;
3 import ij.process.*;
4 import ij.gui.*;
5 import java.awt.*;
6 
8 public class Converter implements PlugIn {
9 
10  private ImagePlus imp;
11 
12  public void run(String arg) {
13  imp = IJ.getInstance().getImagePlus();
14  if (imp!=null) {
15  if (imp.lock()) { //v1.24f
16  convert(arg);
17  imp.unlock();
18  }
19  } else
20  IJ.noImage();
21  }
22 
26  public void convert(String item) {
27  int type = imp.getType();
28  String msg = "Converting to " + item;
29  IJ.showStatus(msg + "...");
30  long start = System.currentTimeMillis();
31  boolean isRoi = imp.getRoi()!=null;
32  imp.killRoi();
33  boolean saveChanges = imp.changes;
34  imp.changes = IJ.getApplet()==null; //if not applet, set 'changes' flag
35  try {
36  // do single image conversions
37  Undo.setup(Undo.TYPE_CONVERSION, imp);
38  ImageConverter ic = new ImageConverter(imp);
39  if (item.equals("8-bit"))
40  ic.convertToGray8();
41  else if (item.equals("16-bit"))
42  ic.convertToGray16();
43  else if (item.equals("32-bit"))
44  ic.convertToGray32();
45  else if (item.equals("RGB Color")) {
46  ic.convertToRGB();
47  } else if (item.equals("8-bit Color")) {
48  int nColors = getNumber();
49  start = System.currentTimeMillis();
50  if (nColors!=0)
51  ic.convertRGBtoIndexedColor(nColors);
52  } else {
53  imp.changes = saveChanges;
54  return;
55  }
56  IJ.showProgress(1.0);
57 
58  }
59  catch (IllegalArgumentException e) {
60  unsupportedConversion(imp);
61  IJ.showStatus("");
62  Undo.reset();
63  imp.changes = saveChanges;
65  return;
66  }
67  if (isRoi)
68  imp.restoreRoi();
69  IJ.showTime(imp, start, "");
70  imp.repaintWindow();
72  }
73 
74  void unsupportedConversion(ImagePlus imp) {
75  IJ.showMessage("Converter",
76  "Supported Conversions:\n" +
77  " \n" +
78  "8-bit -> 16-bit*\n" +
79  "8-bit -> 32-bit*\n" +
80  "8-bit -> RGB Color*\n" +
81  "16-bit -> 8-bit*\n" +
82  "16-bit -> 32-bit*\n" +
83  "16-bit -> RGB Color*\n" +
84  "32-bit -> 8-bit*\n" +
85  "32-bit -> 16-bit\n" +
86  "32-bit -> RGB Color*\n" +
87  "8-bit Color -> 8-bit (grayscale)*\n" +
88  "8-bit Color -> RGB Color\n" +
89  "RGB Color -> 8-bit (grayscale)*\n" +
90  "RGB Color -> 8-bit Color*\n" +
91  "RGB Color -> RGB Stack\n" +
92  "RGB Color -> HSB Stack\n" +
93  "RGB Stack -> RGB Color\n" +
94  "HSB Stack -> RGB Color\n" +
95  " \n" +
96  "* works with stacks\n"
97  );
98  }
99 
100  int getNumber() {
101  if (imp.getType()!=ImagePlus.COLOR_RGB)
102  return 256;
103  GenericDialog gd = new GenericDialog("MedianCut");
104  gd.addNumericField("Number of Colors (2-256):", 256, 0);
105  gd.showDialog();
106  if (gd.wasCanceled())
107  return 0;
108  int n = (int)gd.getNextNumber();
109  if (n<2) n = 2;
110  if (n>256) n = 256;
111  return n;
112  }
113 
114 }