3 import java.awt.image.*;
11 private static final int BYTE=0, SHORT=1, FLOAT=2, RGB=3;
14 boolean doScaling =
true;
19 this.doScaling = doScaling;
38 return convertShortToByte();
40 return convertFloatToByte();
42 return convertRGBToByte();
54 short[] pixels16 = (
short[])ip.
getPixels();
55 byte[] pixels8 =
new byte[width*height];
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;
62 return new ByteProcessor(width, height, pixels8, ip.
getColorModel());
67 ByteProcessor convertFloatToByte() {
70 return new ByteProcessor(img);
72 float[] pixels32 = (
float[])ip.
getPixels();
73 byte[] pixels8 =
new byte[width*height];
75 for (
int i=0; i<width*height; i++) {
77 if (value<0f) value = 0f;
78 if (value>255f) value = 255f;
79 pixels8[i] = (byte)Math.round(value);
81 return new ByteProcessor(width, height, pixels8, ip.
getColorModel());
89 ByteProcessor convertRGBToByte() {
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++) {
104 r = (c&0xff0000)>>16;
107 pixels8[i] = (byte)(r*rw + g*gw + b*bw + 0.5);
110 return new ByteProcessor(width, height, pixels8, null);
117 return convertByteToShort();
121 return convertFloatToShort();
123 ip = convertRGBToByte();
124 return convertByteToShort();
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);
137 return new ShortProcessor(width, height, pixels16, ip.
getColorModel());
141 ShortProcessor convertFloatToShort() {
142 float[] pixels32 = (
float[])ip.
getPixels();
143 short[] pixels16 =
new short[width*height];
150 scale = 65535.0/(max-min);
152 for (
int i=0,j=0; i<width*height; i++) {
154 value = (pixels32[i]-min)*scale;
157 if (value<0.0) value = 0.0;
158 if (value>65535.0) value = 65535.0;
159 pixels16[i] = (short)value;
161 return new ShortProcessor(width, height, pixels16, ip.
getColorModel());
168 return convertByteToFloat(ctable);
170 return convertShortToFloat(ctable);
174 ip = convertRGBToByte();
175 return convertByteToFloat(null);
188 float[] pixels32 =
new float[width*height];
190 if (cTable!=null && cTable.length==256)
191 for (
int i=0; i<width*height; i++)
192 pixels32[i] = cTable[pixels8[i]&255];
194 for (
int i=0; i<width*height; i++)
195 pixels32[i] = pixels8[i]&255;
204 FloatProcessor convertShortToFloat(
float[] cTable) {
205 short[] pixels16 = (
short[])ip.
getPixels();
206 boolean invertedLut =
false;
207 float[] pixels32 =
new float[width*height];
209 if (cTable!=null && cTable.length==65536)
210 for (
int i=0; i<width*height; i++)
211 pixels32[i] = cTable[pixels16[i]&0xffff];
213 for (
int i=0; i<width*height; i++)
214 pixels32[i] = pixels16[i]&0xffff;
216 return new FloatProcessor(width, height, pixels32, cm);