Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
TypeConverter.java
1 package ij.process;
2 import java.awt.*;
3 import java.awt.image.*;
4 import ij.*;
5 import ij.gui.*;
6 import ij.measure.*;
7 
9 public class TypeConverter {
10 
11  private static final int BYTE=0, SHORT=1, FLOAT=2, RGB=3;
12  private ImageProcessor ip;
13  private int type;
14  boolean doScaling = true;
15  int width, height;
16 
17  public TypeConverter(ImageProcessor ip, boolean doScaling) {
18  this.ip = ip;
19  this.doScaling = doScaling;
20  if (ip instanceof ByteProcessor)
21  type = BYTE;
22  else if (ip instanceof ShortProcessor)
23  type = SHORT;
24  else if (ip instanceof FloatProcessor)
25  type = FLOAT;
26  else
27  type = RGB;
28  width = ip.getWidth();
29  height = ip.getHeight();
30  }
31 
34  switch (type) {
35  case BYTE:
36  return ip;
37  case SHORT:
38  return convertShortToByte();
39  case FLOAT:
40  return convertFloatToByte();
41  case RGB:
42  return convertRGBToByte();
43  default:
44  return null;
45  }
46  }
47 
49  ByteProcessor convertShortToByte() {
50  if (doScaling) {
51  Image img = ip.createImage();
52  return new ByteProcessor(img);
53  } else {
54  short[] pixels16 = (short[])ip.getPixels();
55  byte[] pixels8 = new byte[width*height];
56  int value;
57  for (int i=0; i<width*height; i++) {
58  value = pixels16[i]&0xffff;
59  if (value>255) value = 255;
60  pixels8[i] = (byte)value;
61  }
62  return new ByteProcessor(width, height, pixels8, ip.getColorModel());
63  }
64  }
65 
67  ByteProcessor convertFloatToByte() {
68  if (doScaling) {
69  Image img = ip.createImage();
70  return new ByteProcessor(img);
71  } else {
72  float[] pixels32 = (float[])ip.getPixels();
73  byte[] pixels8 = new byte[width*height];
74  float value;
75  for (int i=0; i<width*height; i++) {
76  value = pixels32[i];
77  if (value<0f) value = 0f;
78  if (value>255f) value = 255f;
79  pixels8[i] = (byte)Math.round(value);
80  }
81  return new ByteProcessor(width, height, pixels8, ip.getColorModel());
82  }
83  }
84 
89  ByteProcessor convertRGBToByte() {
90  int c, r, g, b;
91  int[] pixels32;
92  byte[] pixels8;
93  Image img8;
94 
95  //get RGB pixels
96  pixels32 = (int[])ip.getPixels();
97 
98  //convert to grayscale
99  double[] w = ColorProcessor.getWeightingFactors();
100  double rw=w[0], gw=w[1], bw=w[2];
101  pixels8 = new byte[width*height];
102  for (int i=0; i < width*height; i++) {
103  c = pixels32[i];
104  r = (c&0xff0000)>>16;
105  g = (c&0xff00)>>8;
106  b = c&0xff;
107  pixels8[i] = (byte)(r*rw + g*gw + b*bw + 0.5);
108  }
109 
110  return new ByteProcessor(width, height, pixels8, null);
111  }
112 
115  switch (type) {
116  case BYTE:
117  return convertByteToShort();
118  case SHORT:
119  return ip;
120  case FLOAT:
121  return convertFloatToShort();
122  case RGB:
123  ip = convertRGBToByte();
124  return convertByteToShort();
125  default:
126  return null;
127  }
128  }
129 
131  ShortProcessor convertByteToShort() {
132  byte[] pixels8 = (byte[])ip.getPixels();
133  short[] pixels16 = new short[width * height];
134  for (int i=0,j=0; i<width*height; i++) {
135  pixels16[i] = (short)(pixels8[i]&0xff);
136  }
137  return new ShortProcessor(width, height, pixels16, ip.getColorModel());
138  }
139 
141  ShortProcessor convertFloatToShort() {
142  float[] pixels32 = (float[])ip.getPixels();
143  short[] pixels16 = new short[width*height];
144  double min = ip.getMin();
145  double max = ip.getMax();
146  double scale;
147  if ((max-min)==0.0)
148  scale = 1.0;
149  else
150  scale = 65535.0/(max-min);
151  double value;
152  for (int i=0,j=0; i<width*height; i++) {
153  if (doScaling)
154  value = (pixels32[i]-min)*scale;
155  else
156  value = pixels32[i];
157  if (value<0.0) value = 0.0;
158  if (value>65535.0) value = 65535.0;
159  pixels16[i] = (short)value;
160  }
161  return new ShortProcessor(width, height, pixels16, ip.getColorModel());
162  }
163 
165  public ImageProcessor convertToFloat(float[] ctable) {
166  switch (type) {
167  case BYTE:
168  return convertByteToFloat(ctable);
169  case SHORT:
170  return convertShortToFloat(ctable);
171  case FLOAT:
172  return ip;
173  case RGB:
174  ip = convertRGBToByte();
175  return convertByteToFloat(null);
176  default:
177  return null;
178  }
179  }
180 
185  FloatProcessor convertByteToFloat(float[] cTable) {
186  byte[] pixels8 = (byte[])ip.getPixels();
187  boolean invertedLut = ip.isInvertedLut();
188  float[] pixels32 = new float[width*height];
189  int value;
190  if (cTable!=null && cTable.length==256)
191  for (int i=0; i<width*height; i++)
192  pixels32[i] = cTable[pixels8[i]&255];
193  else
194  for (int i=0; i<width*height; i++)
195  pixels32[i] = pixels8[i]&255;
196  ColorModel cm = ip.getColorModel();
197  return new FloatProcessor(width, height, pixels32, cm);
198  }
199 
204  FloatProcessor convertShortToFloat(float[] cTable) {
205  short[] pixels16 = (short[])ip.getPixels();
206  boolean invertedLut = false; //imp.isInvertedLut();
207  float[] pixels32 = new float[width*height];
208  int value;
209  if (cTable!=null && cTable.length==65536)
210  for (int i=0; i<width*height; i++)
211  pixels32[i] = cTable[pixels16[i]&0xffff];
212  else
213  for (int i=0; i<width*height; i++)
214  pixels32[i] = pixels16[i]&0xffff;
215  ColorModel cm = ip.getColorModel();
216  return new FloatProcessor(width, height, pixels32, cm);
217  }
218 
221  if (type==RGB)
222  return ip;
223  else {
224  ImageProcessor ip2 = ip.convertToByte(doScaling);
225  return new ColorProcessor(ip2.createImage());
226  }
227  }
228 
229 }