ImageColormap.java
7.68 KB
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
/*
* ESO Archive
*
* $Id: ImageColormap.java,v 1.2 2009/02/20 23:10:11 abrighto Exp $
*
* who when what
* -------------- ---------- ----------------------------------------
* Allan Brighton 1999/12/09 Created
*/
package jsky.image;
import javax.media.jai.*;
/**
* Manages the colormap for an image and provides methods to select a
* predefined colormap and perform operations on it, such as rotate, shift, and
* stretch.
*
* @version $Revision: 1.2 $
* @author Allan Brighton
*/
public class ImageColormap implements Cloneable {
/** Default color lookup table to use */
public static final String DEFAULT_COLOR_LUT = "Real";
/** The number of colors in the display image */
protected static final int NUM_COLORS = 256;
/** Used to make a color image froma grayscale image using a selected colormap */
protected LookupTableJAI colorLookupTable;
/** Name of the current color lookup table */
protected String colorLookupTableName = "Ramp";
/** Copy of current color lookup table data to use for shift, rotate, scale ops */
protected byte[][] colorLut;
/** Used to modify the order of the colors in the color lookup table */
protected float[] intensityLookupTable;
/** Name of the current intensity lookup table */
protected String intensityLookupTableName = "Ramp";
/**
* Default constructor: Initialize with the default colormap.
*/
public ImageColormap() {
setColorLookupTable(DEFAULT_COLOR_LUT);
}
/**
* Create a color RGB lookup table that can be added to the image processing chain,
* so that we can manipulate the image colors.
*
* @param name the name of the colormap table to use. This is currently
* One of: "Background", "Blue", "Heat", "Isophot", "Light", "Pastel",
* "Ramp", "Real", "Smooth", "Staircase", "Standard".
* User defined maps will be implemented in a later release.
*/
public void setColorLookupTable(String name) {
colorLookupTableName = name;
int maxLut = NUM_COLORS - 1;
byte[][] blut = new byte[3][NUM_COLORS];
float[][] flut = ImageColorLUTs.getLUT(name);
if (intensityLookupTable == null || intensityLookupTableName.equals("Ramp")) {
// don't use intensity table
for (int i = 0; i < NUM_COLORS; i++) {
blut[0][i] = (byte) (flut[i][0] * maxLut);
blut[1][i] = (byte) (flut[i][1] * maxLut);
blut[2][i] = (byte) (flut[i][2] * maxLut);
}
} else {
// use intensity table
for (int i = 0; i < NUM_COLORS; i++) {
int index = (int) ((intensityLookupTable[i] * maxLut) + 0.5);
blut[0][i] = (byte) (flut[index][0] * maxLut);
blut[1][i] = (byte) (flut[index][1] * maxLut);
blut[2][i] = (byte) (flut[index][2] * maxLut);
}
}
// save a copy for manipulation
colorLut = blut.clone();
colorLookupTable = new LookupTableJAI(blut, 0);
}
/**
* Create an intensity lookup table that can be added to the image processing chain
* to rearrange the order of the colors in the colormap.
*
* @param name the name of the intensity lookup table to use. This is currently
* One of: "Equal", "Exponential", "Gamma", "Jigsaw", "Lasritt", "Logarithmic",
* "Negative", "Negative Log", "Ramp", "Staircase".
*
* User defined intensity lookup tables will be implemented in a later release.
*/
public void setIntensityLookupTable(String name) {
intensityLookupTableName = name;
intensityLookupTable = ImageColorITTs.getITT(name);
setColorLookupTable(colorLookupTableName);
}
/**
* Save the current colormap state for the next shift, rotate or scale operation.
*/
public void saveColormap() {
colorLut = colorLookupTable.getByteData().clone();
}
/**
* Rotate the colormap by the given amount.
*/
public void rotateColormap(int amount) {
byte[][] newLut = new byte[3][NUM_COLORS];
for (int i = 0; i < NUM_COLORS; i++) {
int index = (i - amount) % NUM_COLORS;
if (index < 0)
index += NUM_COLORS;
newLut[0][i] = colorLut[0][index];
newLut[1][i] = colorLut[1][index];
newLut[2][i] = colorLut[2][index];
}
colorLut = newLut.clone();
colorLookupTable = new LookupTableJAI(newLut, 0);
}
/**
* Shift the colormap by the given amount.
*/
public void shiftColormap(int amount) {
byte[][] newLut = new byte[3][NUM_COLORS];
for (int i = 0; i < NUM_COLORS; i++) {
int index = (i - amount);
if (index < 0)
index = 0;
else if (index >= NUM_COLORS)
index = NUM_COLORS - 1;
newLut[0][i] = colorLut[0][index];
newLut[1][i] = colorLut[1][index];
newLut[2][i] = colorLut[2][index];
}
colorLookupTable = new LookupTableJAI(newLut, 0);
}
/**
* Scale the colormap by the given amount.
*/
public void scaleColormap(int amount) {
byte[][] newLut = new byte[3][NUM_COLORS];
int n = NUM_COLORS - 1;
int index;
int start = Math.min(amount, NUM_COLORS / 2);
int end = NUM_COLORS - start;
if (end <= start)
end = start + 1;
int dist = end - start + 1;
for (int i = 0; i < NUM_COLORS; i++) {
if (i >= start && i <= end) {
index = ((i - start) * n) / dist;
if (index < 0)
index = 0;
else if (index > n)
index = n;
} else if (i < start) {
index = 0;
} else {
index = n;
}
if (intensityLookupTable == null)
index = (byte) ((index / 256.) * n) & 0xff;
else
index = (byte) (intensityLookupTable[index] * n) & 0xff;
newLut[0][i] = colorLut[0][index];
newLut[1][i] = colorLut[1][index];
newLut[2][i] = colorLut[2][index];
}
colorLookupTable = new LookupTableJAI(newLut, 0);
}
/**
* Reset the colormap to the default.
*/
public void setDefaultColormap() {
intensityLookupTableName = "Ramp";
setColorLookupTable(DEFAULT_COLOR_LUT);
}
/**
* Reset the colormap shift, rotate and scale settings to 0.
*/
public void resetColormap() {
setColorLookupTable(colorLookupTableName);
}
/** Return the current lookup table used to add color to a grayscale image. */
public LookupTableJAI getColorLookupTable() {
return colorLookupTable;
}
/** Return the name of the current color lookup table */
public String getColorLookupTableName() {
return colorLookupTableName;
}
/** Return the name of the current intensity lookup table */
public String getIntensityLookupTableName() {
return intensityLookupTableName;
}
/**
* Return true if this object is equivalent to the given one.
*/
public boolean equals(ImageColormap colormap) {
return (colorLookupTable == colormap.colorLookupTable
&& colorLookupTableName.equals(colormap.colorLookupTableName)
&& colorLut == colormap.colorLut
&& intensityLookupTable == colormap.intensityLookupTable
&& intensityLookupTableName.equals(colormap.intensityLookupTableName));
}
/** Return a shallow copy */
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}