1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
package ij.process;
import java.awt.*;
import java.awt.image.*;
import ij.*;
import ij.gui.*;
import ij.measure.*;
import ij.plugin.frame.Recorder;
/** This class converts an ImagePlus object to a different type. */
public class ImageConverter {
private ImagePlus imp;
private int type;
//private static boolean doScaling = Prefs.getBoolean(Prefs.SCALE_CONVERSIONS,true);
private static boolean doScaling = true;
/** Constructs an ImageConverter based on an ImagePlus object. */
public ImageConverter(ImagePlus imp) {
this.imp = imp;
type = imp.getType();
}
/** Converts this ImagePlus to 8-bit grayscale. */
public synchronized void convertToGray8() {
if (imp.getStackSize()>1) {
new StackConverter(imp).convertToGray8();
return;
}
ImageProcessor ip = imp.getProcessor();
if (type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
if (Prefs.calibrateConversions) {
if (type==ImagePlus.GRAY16 && imp.getCalibration().calibrated())
convertToGray32();
convertAndCalibrate(imp,"8-bit");
} else {
imp.setProcessor(null, ip.convertToByte(doScaling));
imp.setCalibration(imp.getCalibration()); //update calibration
}
record();
} else if (type==ImagePlus.COLOR_RGB)
imp.setProcessor(null, ip.convertToByte(doScaling));
else if (ip.isPseudoColorLut()) {
boolean invertedLut = ip.isInvertedLut();
ip.setColorModel(LookUpTable.createGrayscaleColorModel(invertedLut));
imp.updateAndDraw();
} else {
ip = new ColorProcessor(imp.getImage());
imp.setProcessor(null, ip.convertToByte(doScaling));
}
ImageProcessor ip2 = imp.getProcessor();
if (Prefs.useInvertingLut && ip2 instanceof ByteProcessor && !ip2.isInvertedLut()&& !ip2.isColorLut()) {
ip2.invertLut();
ip2.invert();
}
}
/** Converts this ImagePlus to 16-bit grayscale. */
public void convertToGray16() {
if (type==ImagePlus.GRAY16)
return;
if (!(type==ImagePlus.GRAY8||type==ImagePlus.GRAY32||type==ImagePlus.COLOR_RGB))
throw new IllegalArgumentException("Unsupported conversion");
if (imp.getStackSize()>1) {
new StackConverter(imp).convertToGray16();
return;
}
ImageProcessor ip = imp.getProcessor();
if (type==ImagePlus.GRAY32)
record();
imp.trimProcessor();
if (Prefs.calibrateConversions && type==ImagePlus.GRAY32)
convertAndCalibrate(imp,"16-bit");
else {
imp.setProcessor(null, ip.convertToShort(doScaling));
imp.setCalibration(imp.getCalibration()); //update calibration
}
}
public static void convertAndCalibrate(ImagePlus imp, String type) {
setDoScaling(true);
Calibration cal = imp.getCalibration();
ImageProcessor ip = imp.getProcessor();
int stackSize = imp.getStackSize();
double min = ip.getMin();
double max = ip.getMax();
ip.resetMinAndMax();
double min2 = ip.getMin();
double max2 = ip.getMax();
boolean eightBitConversion = type.equals("8-bit");
if (stackSize>1) {
cal.disableDensityCalibration();
ImageStatistics stats = new StackStatistics(imp);
min2 = stats.min;
max2 = stats.max;
convertStack(imp, eightBitConversion, min2, max2);
} else {
ImageProcessor ip2 = null;
if (eightBitConversion)
ip2 = ip.convertToByte(true);
else
ip2 = ip.convertToShort(true);
imp.setProcessor(null,ip2);
}
int maxRange = eightBitConversion?255:65535;
double[] x = {0,maxRange};
double[] y = {min2,max2};
CurveFitter cf = new CurveFitter(x,y);
cf.doFit(CurveFitter.STRAIGHT_LINE, false);
int np = cf.getNumParams();
double[] p = cf.getParams();
double[] parameters = new double[np];
for (int i=0; i<np; i++)
parameters[i] = p[i];
cal.setFunction(Calibration.STRAIGHT_LINE, parameters, type+" converted");
min = cal.getRawValue(min);
max = cal.getRawValue(max);
if (IJ.debugMode) IJ.log("convertAndCalibrate: "+min+" "+max);
imp.setDisplayRange(min,max);
imp.updateAndDraw();
}
private static void convertStack(ImagePlus imp, boolean eightBitConversion, double min, double max) {
int nSlices = imp.getStackSize();
int width = imp.getWidth();
int height = imp.getHeight();
ImageStack stack1 = imp.getStack();
ImageStack stack2 = new ImageStack(width, height);
String label;
int inc = nSlices/20;
if (inc<1) inc = 1;
ImageProcessor ip1, ip2;
for(int i=1; i<=nSlices; i++) {
label = stack1.getSliceLabel(1);
ip1 = stack1.getProcessor(1);
ip1.setMinAndMax(min, max);
if (eightBitConversion)
ip2 = ip1.convertToByte(true);
else
ip2 = ip1.convertToShort(true);
stack1.deleteSlice(1);
stack2.addSlice(label, ip2);
if ((i%inc)==0) {
IJ.showProgress((double)i/nSlices);
IJ.showStatus("Converting to 16-bits: "+i+"/"+nSlices);
}
}
IJ.showProgress(1.0);
imp.setStack(null, stack2);
ImageConverter.record();
}
public static void record() {
if (Recorder.record) {
if (Prefs.calibrateConversions) {
boolean state = true;
if (Recorder.scriptMode())
Recorder.recordCall("Prefs.calibrateConversions = true;", true);
else
Recorder.recordString("setOption(\"CalibrateConversions\", "+state+");\n");
} else {
Boolean state = ImageConverter.getDoScaling();
if (Recorder.scriptMode())
Recorder.recordCall("ImageConverter.setDoScaling("+state+");", true);
else
Recorder.recordString("setOption(\"ScaleConversions\", "+state+");\n");
}
}
}
/** Converts this ImagePlus to 32-bit grayscale. */
public void convertToGray32() {
if (type==ImagePlus.GRAY32)
return;
if (!(type==ImagePlus.GRAY8||type==ImagePlus.GRAY16||type==ImagePlus.COLOR_RGB))
throw new IllegalArgumentException("Unsupported conversion");
Calibration cal = imp.getCalibration();
double min = cal.getCValue(imp.getDisplayRangeMin());
double max = cal.getCValue(imp.getDisplayRangeMax());
if (imp.getStackSize()>1) {
new StackConverter(imp).convertToGray32();
IJ.setMinAndMax(imp, min, max);
return;
}
ImageProcessor ip = imp.getProcessor();
imp.trimProcessor();
imp.setProcessor(null, ip.convertToFloat());
imp.setCalibration(cal); //update calibration
IJ.setMinAndMax(imp, min, max);
}
/** Converts this ImagePlus to RGB. */
public void convertToRGB() {
if (imp.getBitDepth()==24)
return;
if (imp.getStackSize()>1) {
new StackConverter(imp).convertToRGB();
return;
}
ImageProcessor ip = imp.getProcessor();
imp.setProcessor(null, ip.convertToRGB());
imp.setCalibration(imp.getCalibration()); //update calibration
}
/** Converts an RGB image to an RGB (red, green and blue) stack. */
public void convertToRGBStack() {
if (type!=ImagePlus.COLOR_RGB)
throw new IllegalArgumentException("Image must be RGB");
//convert to RGB Stack
ColorProcessor cp;
if (imp.getType()==ImagePlus.COLOR_RGB)
cp = (ColorProcessor)imp.getProcessor();
else
cp = new ColorProcessor(imp.getImage());
int width = imp.getWidth();
int height = imp.getHeight();
byte[] R = new byte[width*height];
byte[] G = new byte[width*height];
byte[] B = new byte[width*height];
cp.getRGB(R, G, B);
imp.trimProcessor();
// Create stack and select Red channel
ColorModel cm = LookUpTable.createGrayscaleColorModel(false);
ImageStack stack = new ImageStack(width, height, cm);
stack.addSlice("Red", R);
stack.addSlice("Green", G);
stack.addSlice("Blue", B);
imp.setStack(null, stack);
imp.setDimensions(3, 1, 1);
if (imp.isComposite())
((CompositeImage)imp).setMode(IJ.GRAYSCALE);
}
/** Converts an RGB image to a HSB (hue, saturation and brightness) stack. */
public void convertToHSB() {
if (type!=ImagePlus.COLOR_RGB)
throw new IllegalArgumentException("Image must be RGB");;
ColorProcessor cp;
if (imp.getType()==ImagePlus.COLOR_RGB)
cp = (ColorProcessor)imp.getProcessor();
else
cp = new ColorProcessor(imp.getImage());
ImageStack stack = cp.getHSBStack();
imp.trimProcessor();
imp.setStack(null, stack);
imp.setDimensions(3, 1, 1);
}
/** Converts an RGB image to a 32-bit HSB (hue, saturation and brightness) stack. */
public void convertToHSB32() {
if (type!=ImagePlus.COLOR_RGB)
throw new IllegalArgumentException("Image must be RGB");;
ColorProcessor cp;
if (imp.getType()==ImagePlus.COLOR_RGB)
cp = (ColorProcessor)imp.getProcessor();
else
cp = new ColorProcessor(imp.getImage());
ImageStack stack = cp.getHSB32Stack();
imp.trimProcessor();
imp.setStack(null, stack);
imp.setDimensions(3, 1, 1);
}
/** Converts an RGB image to a Lab stack. */
public void convertToLab() {
if (type!=ImagePlus.COLOR_RGB)
throw new IllegalArgumentException("Image must be RGB");
ColorSpaceConverter converter = new ColorSpaceConverter();
ImagePlus imp2 = converter.RGBToLab(imp);
Point loc = null;
ImageWindow win = imp.getWindow();
if (win!=null)
loc = win.getLocation();
ImageWindow.setNextLocation(loc);
imp2.show();
imp.hide();
imp2.copyAttributes(imp);
imp.changes = false;
imp.close();
}
/** Converts a 2 or 3 slice 8-bit stack to RGB. */
public void convertRGBStackToRGB() {
int stackSize = imp.getStackSize();
if (stackSize<2 || stackSize>3 || type!=ImagePlus.GRAY8)
throw new IllegalArgumentException("2 or 3 slice 8-bit stack required");
int width = imp.getWidth();
int height = imp.getHeight();
ImageStack stack = imp.getStack();
byte[] R = (byte[])stack.getPixels(1);
byte[] G = (byte[])stack.getPixels(2);
byte[] B;
if (stackSize>2)
B = (byte[])stack.getPixels(3);
else
B = new byte[width*height];
imp.trimProcessor();
ColorProcessor cp = new ColorProcessor(width, height);
cp.setRGB(R, G, B);
if (imp.isInvertedLut())
cp.invert();
imp.setImage(cp.createImage());
imp.killStack();
if (IJ.isLinux())
imp.setTitle(imp.getTitle());
}
/** Converts a 3-slice (hue, saturation, brightness) 8-bit stack to RGB. */
public void convertHSBToRGB() {
if (imp.getStackSize()!=3)
throw new IllegalArgumentException("3-slice 8-bit stack required");
ImageStack stack = imp.getStack();
byte[] H = (byte[])stack.getPixels(1);
byte[] S = (byte[])stack.getPixels(2);
byte[] B = (byte[])stack.getPixels(3);
int width = imp.getWidth();
int height = imp.getHeight();
imp.trimProcessor();
ColorProcessor cp = new ColorProcessor(width, height);
cp.setHSB(H, S, B);
imp.setImage(cp.createImage());
imp.killStack();
if (IJ.isLinux())
imp.setTitle(imp.getTitle());
}
/** Converts a 3-slice (hue, saturation, brightness) 32-bit stack to RGB. */
public void convertHSB32ToRGB() {
if (imp.getStackSize()!=3)
throw new IllegalArgumentException("3-slice 8-bit stack required");
ImageStack stack = imp.getStack();
float[] H = (float[])stack.getPixels(1);
float[] S = (float[])stack.getPixels(2);
float[] B = (float[])stack.getPixels(3);
int width = imp.getWidth();
int height = imp.getHeight();
imp.trimProcessor();
ColorProcessor cp = new ColorProcessor(width, height);
cp.setHSB(H, S, B);
imp.setImage(cp.createImage());
imp.killStack();
if (IJ.isLinux())
imp.setTitle(imp.getTitle());
}
/** Converts a Lab stack to RGB. */
public void convertLabToRGB() {
if (imp.getStackSize()!=3)
throw new IllegalArgumentException("3-slice 32-bit stack required");
ColorSpaceConverter converter = new ColorSpaceConverter();
ImagePlus imp2 = converter.LabToRGB(imp);
imp2.setCalibration(imp.getCalibration());
imp.setImage(imp2);
}
/** Converts an RGB image to 8-bits indexed color. 'nColors' must
be greater than 1 and less than or equal to 256. */
public void convertRGBtoIndexedColor(int nColors) {
if (type!=ImagePlus.COLOR_RGB)
throw new IllegalArgumentException("Image must be RGB");
if (nColors<2) nColors = 2;
if (nColors>256) nColors = 256;
// get RGB pixels
IJ.showProgress(0.1);
IJ.showStatus("Grabbing pixels");
int width = imp.getWidth();
int height = imp.getHeight();
ImageProcessor ip = imp.getProcessor();
ip.snapshot();
int[] pixels = (int[])ip.getPixels();
imp.trimProcessor();
// convert to 8-bits
long start = System.currentTimeMillis();
MedianCut mc = new MedianCut(pixels, width, height);
ImageProcessor ip2 = mc.convertToByte(nColors);
imp.setProcessor(null, ip2);
imp.setTypeToColor256();
}
/** Set true to scale to 0-255 when converting short to byte or float
to byte and to 0-65535 when converting float to short. */
public static void setDoScaling(boolean scaleConversions) {
doScaling = scaleConversions;
IJ.register(ImageConverter.class);
}
/** Returns true if scaling is enabled. */
public static boolean getDoScaling() {
return doScaling;
}
}
|