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
|
package ij.plugin.filter;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.ArrayList;
/** Displays the active image's look-up table. */
public class LutViewer implements PlugInFilter {
ImagePlus imp;
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_ALL-DOES_RGB+NO_UNDO+NO_CHANGES;
}
public void run(ImageProcessor ip) {
int xMargin = 35;
int yMargin = 20;
int width = 256;
int height = 128;
int x, y, x1, y1, x2, y2;
int imageWidth, imageHeight;
int barHeight = 12;
boolean isGray;
double scale;
ip = imp.getChannelProcessor();
IndexColorModel cm = (IndexColorModel)ip.getColorModel();
LookUpTable lut = new LookUpTable(cm);
int mapSize = lut.getMapSize();
byte[] reds = lut.getReds();
byte[] greens = lut.getGreens();
byte[] blues = lut.getBlues();
isGray = lut.isGrayscale();
imageWidth = width + 2*xMargin;
imageHeight = height + 3*yMargin;
Image img = IJ.getInstance().createImage(imageWidth, imageHeight);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, imageWidth, imageHeight);
g.setColor(Color.black);
g.drawRect(xMargin, yMargin, width, height);
scale = 256.0/mapSize;
if (isGray)
g.setColor(Color.black);
else
g.setColor(Color.red);
x1 = xMargin;
y1 = yMargin + height - (reds[0]&0xff)/2;
for (int i = 1; i<256; i++) {
x2 = xMargin + i;
y2 = yMargin + height - (reds[(int)(i/scale)]&0xff)/2;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
if (!isGray) {
g.setColor(Color.green);
x1 = xMargin;
y1 = yMargin + height - (greens[0]&0xff)/2;
for (int i = 1; i<256; i++) {
x2 = xMargin + i;
y2 = yMargin + height - (greens[(int)(i/scale)]&0xff)/2;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
if (!isGray) {
g.setColor(Color.blue);
x1 = xMargin;
y1 = yMargin + height - (blues[0]&0xff)/2;
for (int i = 1; i<255; i++) {
x2 = xMargin + i;
y2 = yMargin + height - (blues[(int)(i/scale)]&0xff)/2;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
x = xMargin;
y = yMargin + height + 2;
lut.drawColorBar(g, x, y, 256, barHeight);
y += barHeight + 15;
g.setColor(Color.black);
g.drawString("0", x - 4, y);
g.drawString(""+(mapSize-1), x + width - 10, y);
g.drawString("255", 7, yMargin + 4);
g.dispose();
ImagePlus imp = new ImagePlus("Look-Up Table", img);
//imp.show();
new LutWindow(imp, new ImageCanvas(imp), ip);
}
} // LutViewer class
class LutWindow extends ImageWindow implements ActionListener {
private Button button;
private ImageProcessor ip;
LutWindow(ImagePlus imp, ImageCanvas ic, ImageProcessor ip) {
super(imp, ic);
this.ip = ip;
addPanel();
}
void addPanel() {
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
button = new Button(" List... ");
button.addActionListener(this);
panel.add(button);
add(panel);
pack();
}
public void actionPerformed(ActionEvent e) {
Object b = e.getSource();
if (b==button)
list(ip);
}
void list(ImageProcessor ip) {
IndexColorModel icm = (IndexColorModel)ip.getColorModel();
int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
ArrayList list = new ArrayList();
String headings = "Index\tRed\tGreen\tBlue";
for (int i=0; i<size; i++)
list.add(i+"\t"+(r[i]&255)+"\t"+(g[i]&255)+"\t"+(b[i]&255));
TextWindow tw = new TextWindow("LUT", headings, list, 250, 400);
}
} // LutWindow class
|