package ij.plugin;
import ij.*;
import ij.io.*;
import ij.process.*;
import ij.gui.ImageWindow;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.net.*;
/** Opens NIH Image look-up tables (LUTs), 768 byte binary LUTs
(256 reds, 256 greens and 256 blues), LUTs in text format,
or generates the LUT specified by the string argument
passed to the run() method. */
public class LutLoader extends ImagePlus implements PlugIn {
private static String defaultDirectory = null;
/** If 'arg'="", displays a file open dialog and opens the specified
LUT. If 'arg' is a path, opens the LUT specified by the path. If
'arg'="fire", "ice", etc., uses a method to generate the LUT. */
public void run(String arg) {
FileInfo fi = new FileInfo();
fi.reds = new byte[256];
fi.greens = new byte[256];
fi.blues = new byte[256];
fi.lutSize = 256;
int nColors = 0;
if (arg.equals("invert"))
{invertLut(); return;}
else if (arg.equals("fire"))
nColors = fire(fi.reds, fi.greens, fi.blues);
else if (arg.equals("grays"))
nColors = grays(fi.reds, fi.greens, fi.blues);
else if (arg.equals("ice"))
nColors = ice(fi.reds, fi.greens, fi.blues);
else if (arg.equals("spectrum"))
nColors = spectrum(fi.reds, fi.greens, fi.blues);
else if (arg.equals("3-3-2 RGB"))
nColors = rgb332(fi.reds, fi.greens, fi.blues);
else if (arg.equals("red"))
nColors = primaryColor(4, fi.reds, fi.greens, fi.blues);
else if (arg.equals("green"))
nColors = primaryColor(2, fi.reds, fi.greens, fi.blues);
else if (arg.equals("blue"))
nColors = primaryColor(1, fi.reds, fi.greens, fi.blues);
else if (arg.equals("cyan"))
nColors = primaryColor(3, fi.reds, fi.greens, fi.blues);
else if (arg.equals("magenta"))
nColors = primaryColor(5, fi.reds, fi.greens, fi.blues);
else if (arg.equals("yellow"))
nColors = primaryColor(6, fi.reds, fi.greens, fi.blues);
else if (arg.equals("redgreen"))
nColors = redGreen(fi.reds, fi.greens, fi.blues);
if (nColors>0) {
if (nColors<256)
interpolate(fi.reds, fi.greens, fi.blues, nColors);
fi.fileName = arg;
showLut(fi, true);
Menus.updateMenus();
return;
}
OpenDialog od = new OpenDialog("Open LUT...", arg);
fi.directory = od.getDirectory();
fi.fileName = od.getFileName();
if (fi.fileName==null)
return;
if (openLut(fi))
showLut(fi, arg.equals(""));
IJ.showStatus("");
}
void showLut(FileInfo fi, boolean showImage) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp!=null) {
if (imp.getType()==ImagePlus.COLOR_RGB)
IJ.error("LUTs cannot be assiged to RGB Images.");
else if (imp.isComposite() && ((CompositeImage)imp).getMode()==IJ.GRAYSCALE) {
CompositeImage cimp = (CompositeImage)imp;
cimp.setMode(IJ.COLOR);
int saveC = cimp.getChannel();
IndexColorModel cm = new IndexColorModel(8, 256, fi.reds, fi.greens, fi.blues);
for (int c=1; c<=cimp.getNChannels(); c++) {
cimp.setC(c);
cimp.setChannelColorModel(cm);
}
imp.setC(saveC);
imp.updateAndRepaintWindow();
} else {
ImageProcessor ip = imp.getChannelProcessor();
IndexColorModel cm = new IndexColorModel(8, 256, fi.reds, fi.greens, fi.blues);
if (imp.isComposite())
((CompositeImage)imp).setChannelColorModel(cm);
else
ip.setColorModel(cm);
if (imp.getStackSize()>1)
imp.getStack().setColorModel(cm);
imp.updateAndRepaintWindow();
if (IJ.isMacro() && imp.getWindow()!=null)
IJ.wait(25);
}
} else
createImage(fi, showImage);
}
void invertLut() {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null)
{IJ.noImage(); return;}
if (imp.getType()==ImagePlus.COLOR_RGB)
{IJ.error("RGB images do not use LUTs"); return;}
if (imp.isComposite()) {
CompositeImage ci = (CompositeImage)imp;
LUT lut = ci.getChannelLut();
if (lut!=null)
ci.setChannelLut(lut.createInvertedLut());
} else {
ImageProcessor ip = imp.getProcessor();
ip.invertLut();
if (imp.getStackSize()>1)
imp.getStack().setColorModel(ip.getColorModel());
}
imp.updateAndRepaintWindow();
}
int fire(byte[] reds, byte[] greens, byte[] blues) {
int[] r = {0,0,1,25,49,73,98,122,146,162,173,184,195,207,217,229,240,252,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
int[] g = {0,0,0,0,0,0,0,0,0,0,0,0,0,14,35,57,79,101,117,133,147,161,175,190,205,219,234,248,255,255,255,255};
int[] b = {0,61,96,130,165,192,220,227,210,181,151,122,93,64,35,5,0,0,0,0,0,0,0,0,0,0,0,35,98,160,223,255};
for (int i=0; i10000) {
error(path);
return false;
}
}
int size = 0;
try {
if (length>768)
size = openBinaryLut(fi, isURL, false); // attempt to read NIH Image LUT
if (size==0 && (length==0||length==768||length==970))
size = openBinaryLut(fi, isURL, true); // otherwise read raw LUT
if (size==0 && length>768)
size = openTextLut(fi);
if (size==0)
error(path);
} catch (IOException e) {
IJ.error("LUT Loader", ""+e);
}
return size==256;
}
private void error(String path) {
IJ.error("LUT Reader", "This is not an ImageJ or NIH Image LUT, a 768 byte \nraw LUT, or a LUT in text format.\n \n"+path);
}
/** Opens an NIH Image LUT or a 768 byte binary LUT. */
int openBinaryLut(FileInfo fi, boolean isURL, boolean raw) throws IOException {
InputStream is;
if (isURL)
is = new URL(fi.url+fi.fileName).openStream();
else
is = new FileInputStream(fi.directory + fi.fileName);
DataInputStream f = new DataInputStream(is);
int nColors = 256;
if (!raw) {
// attempt to read 32 byte NIH Image LUT header
int id = f.readInt();
if (id!=1229147980) { // 'ICOL'
f.close();
return 0;
}
int version = f.readShort();
nColors = f.readShort();
int start = f.readShort();
int end = f.readShort();
long fill1 = f.readLong();
long fill2 = f.readLong();
int filler = f.readInt();
}
f.read(fi.reds, 0, nColors);
f.read(fi.greens, 0, nColors);
f.read(fi.blues, 0, nColors);
if (nColors<256)
interpolate(fi.reds, fi.greens, fi.blues, nColors);
f.close();
return 256;
}
int openTextLut(FileInfo fi) throws IOException {
TextReader tr = new TextReader();
tr.hideErrorMessages();
ImageProcessor ip = tr.open(fi.directory+fi.fileName);
if (ip==null)
return 0;
int width = ip.getWidth();
int height = ip.getHeight();
if (width<3||width>4||height<256||height>258)
return 0;
int x = width==4?1:0;
int y = height>256?1:0;
ip.setRoi(x, y, 3, 256);
ip = ip.crop();
for (int i=0; i<256; i++) {
fi.reds[i] = (byte)ip.getPixelValue(0,i);
fi.greens[i] = (byte)ip.getPixelValue(1,i);
fi.blues[i] = (byte)ip.getPixelValue(2,i);
}
return 256;
}
void createImage(FileInfo fi, boolean show) {
IndexColorModel cm = new IndexColorModel(8, 256, fi.reds, fi.greens, fi.blues);
ByteProcessor bp = createImage(cm);
setProcessor(fi.fileName, bp);
if (show) show();
}
/** Opens the specified ImageJ LUT and returns
it as an IndexColorModel. Since 1.43t. */
public static IndexColorModel open(String path) throws IOException {
return open(new FileInputStream(path));
}
/** Opens an ImageJ LUT using an InputStream
and returns it as an IndexColorModel. Since 1.43t. */
public static IndexColorModel open(InputStream stream) throws IOException {
DataInputStream f = new DataInputStream(stream);
byte[] reds = new byte[256];
byte[] greens = new byte[256];
byte[] blues = new byte[256];
f.read(reds, 0, 256);
f.read(greens, 0, 256);
f.read(blues, 0, 256);
f.close();
return new IndexColorModel(8, 256, reds, greens, blues);
}
/** Creates a 256x32 image from an IndexColorModel. Since 1.43t. */
public static ByteProcessor createImage(IndexColorModel cm) {
int width = 256;
int height = 32;
byte[] pixels = new byte[width*height];
ByteProcessor bp = new ByteProcessor(width, height, pixels, cm);
int[] ramp = new int[width];
for (int i=0; i
Ads help maintain this website.