Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageConverter.java
1 package ij.process;
2 
3 import java.awt.*;
4 import java.awt.image.*;
5 import ij.*;
6 import ij.gui.*;
7 import ij.measure.*;
8 
10 public class ImageConverter {
11  private ImagePlus imp;
12  private int type;
13  //private static boolean doScaling = Prefs.getBoolean(Prefs.SCALE_CONVERSIONS,true);
14  private static boolean doScaling = true;
15 
17  public ImageConverter(ImagePlus imp) {
18  this.imp = imp;
19  type = imp.getType();
20  }
21 
23  public synchronized void convertToGray8() {
24  ImageProcessor ip = imp.getProcessor();
25  if (type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
26  imp.setProcessor(null, ip.convertToByte(doScaling));
27  imp.setCalibration(imp.getCalibration()); //update calibration
28  } else if (type==ImagePlus.COLOR_RGB)
29  imp.setProcessor(null, ip.convertToByte(doScaling));
30  else if (ip.isPseudoColorLut()) {
31  boolean invertedLut = ip.isInvertedLut();
32  ip.setColorModel(LookUpTable.createGrayscaleColorModel(invertedLut));
33  imp.updateAndDraw();
34  } else {
35  ip = new ColorProcessor(imp.getImage());
36  imp.setProcessor(null, ip.convertToByte(doScaling));
37  }
38  }
39 
41  public void convertToGray16() {
42  if (type==ImagePlus.GRAY16)
43  return;
44  if (!(type==ImagePlus.GRAY8 || type==ImagePlus.GRAY32))
45  throw new IllegalArgumentException("Unsupported conversion");
46  ImageProcessor ip = imp.getProcessor();
47  imp.trimProcessor();
48  imp.setProcessor(null, ip.convertToShort(doScaling));
49  imp.setCalibration(imp.getCalibration()); //update calibration
50  }
51 
53  public void convertToGray32() {
54  if (type==ImagePlus.GRAY32)
55  return;
56  if (!(type==ImagePlus.GRAY8 || type==ImagePlus.GRAY16))
57  throw new IllegalArgumentException("Unsupported conversion");
58  ImageProcessor ip = imp.getProcessor();
59  imp.trimProcessor();
60  Calibration cal = imp.getCalibration();
62  imp.setProcessor(null, ip.convertToFloat());
63  imp.setCalibration(cal); //update calibration
64  }
65 
67  public void convertToRGB() {
68  ImageProcessor ip = imp.getProcessor();
69  imp.setProcessor(null, ip.convertToRGB());
70  imp.setCalibration(imp.getCalibration()); //update calibration
71  }
72 
73 
76  public void convertRGBtoIndexedColor(int nColors) {
77  if (type!=ImagePlus.COLOR_RGB)
78  throw new IllegalArgumentException("Image must be RGB");
79  if (nColors<2) nColors = 2;
80  if (nColors>256) nColors = 256;
81 
82  // get RGB pixels
83  IJ.showProgress(0.1);
84  IJ.showStatus("Grabbing pixels");
85  int width = imp.getWidth();
86  int height = imp.getHeight();
87  ImageProcessor ip = imp.getProcessor();
88  ip.snapshot();
89  int[] pixels = (int[])ip.getPixels();
90  imp.trimProcessor();
91 
92  // convert to 8-bits
93  long start = System.currentTimeMillis();
94  MedianCut mc = new MedianCut(pixels, width, height);
95  ImageProcessor ip2 = mc.convertToByte(nColors);
96  imp.setProcessor(null, ip2);
97  }
98 
101  public static void setDoScaling(boolean scaleConversions) {
102  doScaling = scaleConversions;
103  }
104 
106  public static boolean getDoScaling() {
107  return doScaling;
108  }
109 }