1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkLookupTable.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 // .NAME vtkLookupTable - map scalar values into colors via a lookup table
16 // .SECTION Description
17 // vtkLookupTable is an object that is used by mapper objects to map scalar
18 // values into RGBA (red-green-blue-alpha transparency) color specification,
19 // or RGBA into scalar values. The color table can be created by direct
20 // insertion of color values, or by specifying a hue, saturation, value, and
21 // alpha range and generating a table.
22 //
23 // A special color for NaN values in the data can be specified via
24 // SetNanColor(). In addition, a color for data values below the
25 // lookup table range minimum can be specified with
26 // SetBelowRangeColor(), and that color will be used for values below
27 // the range minimum when UseBelowRangeColor is on. Likewise, a color
28 // for data values above the lookup table range maximum can be
29 // specified with SetAboveRangeColor(), and it is used when
30 // UseAboveRangeColor is on.
31 //
32 // This class behaves differently depending on how \a IndexedLookup is set.
33 // When true, vtkLookupTable enters a mode for representing categorical color maps.
34 // By setting \a IndexedLookup to true, you indicate that the annotated
35 // values are the only valid values for which entries in the color table
36 // should be returned. The colors in the lookup \a Table are assigned
37 // to annotated values by taking the modulus of their index in the list
38 // of annotations. \a IndexedLookup changes the behavior of \a GetIndex,
39 // which in turn changes the way \a MapScalarsThroughTable2 behaves;
40 // when \a IndexedLookup is true, \a MapScalarsThroughTable2 will search for
41 // scalar values in \a AnnotatedValues and use the resulting index to
42 // determine the color. If a scalar value is not present in \a AnnotatedValues,
43 // then \a NanColor will be used.
44 //
45 // .SECTION Caveats
46 // You need to explicitly call Build() when constructing the LUT by hand.
47 //
48 // .SECTION See Also
49 // vtkLogLookupTable vtkWindowLevelLookupTable
50
51 #ifndef vtkLookupTable_h
52 #define vtkLookupTable_h
53
54 #include "vtkCommonCoreModule.h" // For export macro
55 #include "vtkScalarsToColors.h"
56
57 #include "vtkUnsignedCharArray.h" // Needed for inline method
58
59 #define VTK_RAMP_LINEAR 0
60 #define VTK_RAMP_SCURVE 1
61 #define VTK_RAMP_SQRT 2
62 #define VTK_SCALE_LINEAR 0
63 #define VTK_SCALE_LOG10 1
64
65 class VTKCOMMONCORE_EXPORT vtkLookupTable : public vtkScalarsToColors
66 {
67 public:
68 // Description:
69 // Constants for offsets of special colors (e.g., NanColor, BelowRangeColor,
70 // AboveRangeColor) from the maximum index in the lookup table.
71 const static vtkIdType BELOW_RANGE_COLOR_INDEX;
72 const static vtkIdType ABOVE_RANGE_COLOR_INDEX;
73 const static vtkIdType NAN_COLOR_INDEX;
74 const static vtkIdType NUMBER_OF_SPECIAL_COLORS;
75
76 // Description:
77 // Construct with range=[0,1]; and hsv ranges set up for rainbow color table
78 // (from red to blue).
79 static vtkLookupTable *New();
80
81 vtkTypeMacro(vtkLookupTable,vtkScalarsToColors);
82 void PrintSelf(ostream& os, vtkIndent indent);
83
84 // Description:
85 // Return true if all of the values defining the mapping have an opacity
86 // equal to 1.
87 virtual int IsOpaque();
88
89 // Description:
90 // Allocate a color table of specified size.
91 int Allocate(int sz=256, int ext=256);
92
93 // Description:
94 // Generate lookup table from hue, saturation, value, alpha min/max values.
95 // Table is built from linear ramp of each value.
96 virtual void Build();
97
98 // Description:
99 // Force the lookup table to regenerate from hue, saturation, value,
100 // and alpha min/max values. Table is built from a linear ramp of
101 // each value. ForceBuild() is useful if a lookup table has been
102 // defined manually (using SetTableValue) and then an application
103 // decides to rebuild the lookup table using the implicit process.
104 virtual void ForceBuild();
105
106 // Description:
107 // Set the shape of the table ramp to either linear or S-curve.
108 // The default is S-curve, which tails off gradually at either end.
109 // The equation used for the S-curve is y = (sin((x - 1/2)*pi) + 1)/2,
110 // while the equation for the linear ramp is simply y = x. For an
111 // S-curve greyscale ramp, you should set NumberOfTableValues to 402
112 // (which is 256*pi/2) to provide room for the tails of the ramp.
113 // The equation for the SQRT is y = sqrt(x).
114 vtkSetMacro(Ramp,int);
SetRampToLinear()115 void SetRampToLinear() { this->SetRamp(VTK_RAMP_LINEAR); };
SetRampToSCurve()116 void SetRampToSCurve() { this->SetRamp(VTK_RAMP_SCURVE); };
SetRampToSQRT()117 void SetRampToSQRT() { this->SetRamp(VTK_RAMP_SQRT); };
118 vtkGetMacro(Ramp,int);
119
120 // Description:
121 // Set the type of scale to use, linear or logarithmic. The default
122 // is linear. If the scale is logarithmic, then the TableRange must not
123 // cross the value zero.
124 void SetScale(int scale);
SetScaleToLinear()125 void SetScaleToLinear() { this->SetScale(VTK_SCALE_LINEAR); };
SetScaleToLog10()126 void SetScaleToLog10() { this->SetScale(VTK_SCALE_LOG10); };
127 vtkGetMacro(Scale,int);
128
129 // Description:
130 // Set/Get the minimum/maximum scalar values for scalar mapping. Scalar
131 // values less than minimum range value are clamped to minimum range value.
132 // Scalar values greater than maximum range value are clamped to maximum
133 // range value.
134 //
135 // The \a TableRange values are only used when \a IndexedLookup is false.
136 void SetTableRange(double r[2]);
137 virtual void SetTableRange(double min, double max);
138 vtkGetVectorMacro(TableRange,double,2);
139
140 // Description:
141 // Set the range in hue (using automatic generation). Hue ranges
142 // between [0,1].
143 vtkSetVector2Macro(HueRange,double);
144 vtkGetVector2Macro(HueRange,double);
145
146 // Description:
147 // Set the range in saturation (using automatic generation). Saturation
148 // ranges between [0,1].
149 vtkSetVector2Macro(SaturationRange,double);
150 vtkGetVector2Macro(SaturationRange,double);
151
152 // Description:
153 // Set the range in value (using automatic generation). Value ranges
154 // between [0,1].
155 vtkSetVector2Macro(ValueRange,double);
156 vtkGetVector2Macro(ValueRange,double);
157
158 // Description:
159 // Set the range in alpha (using automatic generation). Alpha ranges from
160 // [0,1].
161 vtkSetVector2Macro(AlphaRange,double);
162 vtkGetVector2Macro(AlphaRange,double);
163
164 // Description:
165 // Set the color to use when a NaN (not a number) is encountered. This is an
166 // RGBA 4-tuple of doubles in the range [0,1].
167 vtkSetVector4Macro(NanColor, double);
168 vtkGetVector4Macro(NanColor, double);
169
170 // Description:
171 // Return the \a NanColor as a pointer to 4 unsigned chars. This
172 // will overwrite any data returned by previous calls to MapValue.
173 unsigned char* GetNanColorAsUnsignedChars();
174
175 // Description:
176 // Cast a double color in a type T color. colorIn and colorOut are
177 // expected to be RGBA[4] and colorIn to be in [0.0, 1.0]
178 static void GetColorAsUnsignedChars(const double colorIn[4],
179 unsigned char colorOut[4]);
180
181 // Description:
182 // Set the color to use when a value below the range is
183 // encountered. This is an RGBA 4-tuple of doubles in the range [0, 1].
184 vtkSetVector4Macro(BelowRangeColor, double);
185 vtkGetVector4Macro(BelowRangeColor, double);
186
187 // Description:
188 // Set whether the below range color should be used.
189 vtkSetMacro(UseBelowRangeColor, int);
190 vtkGetMacro(UseBelowRangeColor, int);
191 vtkBooleanMacro(UseBelowRangeColor, int);
192
193 // Description:
194 // Set the color to use when a value above the range is
195 // encountered. This is an RGBA 4-tuple of doubles in the range [0, 1].
196 vtkSetVector4Macro(AboveRangeColor, double);
197 vtkGetVector4Macro(AboveRangeColor, double);
198
199 // Description:
200 // Set whether the below range color should be used.
201 vtkSetMacro(UseAboveRangeColor, int);
202 vtkGetMacro(UseAboveRangeColor, int);
203 vtkBooleanMacro(UseAboveRangeColor, int);
204
205 // Description:
206 // Map one value through the lookup table.
207 unsigned char* MapValue(double v);
208
209 // Description:
210 // Map one value through the lookup table and return the color as
211 // an RGB array of doubles between 0 and 1.
212 void GetColor(double x, double rgb[3]);
213
214 // Description:
215 // Map one value through the lookup table and return the alpha value
216 // (the opacity) as a double between 0 and 1.
217 double GetOpacity(double v);
218
219 // Description:
220 // Return the table index associated with a particular value.
221 //
222 // Do not use this function when \a IndexedLookup is true:
223 // in that case, the set of values \a v may take on is exactly the integers
224 // from 0 to \a GetNumberOfTableValues() - 1;
225 // and \a v serves directly as an index into \a TableValues.
226 virtual vtkIdType GetIndex(double v);
227
228 // Description:
229 // Specify the number of values (i.e., colors) in the lookup
230 // table.
231 void SetNumberOfTableValues(vtkIdType number);
GetNumberOfTableValues()232 vtkIdType GetNumberOfTableValues() { return this->NumberOfColors; };
233
234 // Description:
235 // Directly load color into lookup table. Use [0,1] double values for color
236 // component specification. Make sure that you've either used the
237 // Build() method or used SetNumberOfTableValues() prior to using this
238 // method.
239 virtual void SetTableValue(vtkIdType indx, double rgba[4]);
240
241 // Description:
242 // Directly load color into lookup table. Use [0,1] double values for color
243 // component specification.
244 virtual void SetTableValue(vtkIdType indx,
245 double r, double g, double b, double a=1.0);
246
247 // Description:
248 // Return a rgba color value for the given index into the lookup table. Color
249 // components are expressed as [0,1] double values.
250 double *GetTableValue(vtkIdType id);
251
252 // Description:
253 // Return a rgba color value for the given index into the lookup table. Color
254 // components are expressed as [0,1] double values.
255 void GetTableValue(vtkIdType id, double rgba[4]);
256
257 // Description:
258 // Get pointer to color table data. Format is array of unsigned char
259 // r-g-b-a-r-g-b-a...
GetPointer(const vtkIdType id)260 unsigned char *GetPointer(const vtkIdType id) {
261 return this->Table->GetPointer(4*id); };
262
263 // Description:
264 // Get pointer to data. Useful for direct writes into object. MaxId is bumped
265 // by number (and memory allocated if necessary). Id is the location you
266 // wish to write into; number is the number of rgba values to write.
267 unsigned char *WritePointer(const vtkIdType id, const int number);
268
269 // Description:
270 // Sets/Gets the range of scalars which will be mapped. This is a duplicate
271 // of Get/SetTableRange.
GetRange()272 double *GetRange() { return this->GetTableRange(); };
SetRange(double min,double max)273 void SetRange(double min, double max) { this->SetTableRange(min, max); };
SetRange(double rng[2])274 void SetRange(double rng[2]) { this->SetRange(rng[0], rng[1]); };
275
276 //BTX
277 // Description:
278 // Returns the log of \c range in \c log_range.
279 // There is a little more to this than simply taking the log10 of the
280 // two range values: we do conversion of negative ranges to positive
281 // ranges, and conversion of zero to a 'very small number'.
282 static void GetLogRange(const double range[2], double log_range[2]);
283
284 // Description:
285 // Apply log to value, with appropriate constraints.
286 static double ApplyLogScale(double v, const double range[2],
287 const double log_range[2]);
288 //ETX
289
290 // Description:
291 // Set the number of colors in the lookup table. Use
292 // SetNumberOfTableValues() instead, it can be used both before and
293 // after the table has been built whereas SetNumberOfColors() has no
294 // effect after the table has been built.
295 vtkSetClampMacro(NumberOfColors,vtkIdType,2,VTK_ID_MAX);
296 vtkGetMacro(NumberOfColors,vtkIdType);
297
298 // Description:
299 // Set/Get the internal table array that is used to map the scalars
300 // to colors. The table array is an unsigned char array with 4
301 // components representing RGBA.
302 void SetTable(vtkUnsignedCharArray *);
303 vtkGetObjectMacro(Table,vtkUnsignedCharArray);
304
305 // Description:
306 // map a set of scalars through the lookup table
307 void MapScalarsThroughTable2(void *input, unsigned char *output,
308 int inputDataType, int numberOfValues,
309 int inputIncrement, int outputIncrement);
310
311 // Description:
312 // Copy the contents from another LookupTable
313 void DeepCopy(vtkScalarsToColors *lut);
314
315 // Description:
316 // This should return 1 is the subclass is using log scale for mapping scalars
317 // to colors. Returns 1 is scale == VTK_SCALE_LOG10.
UsingLogScale()318 virtual int UsingLogScale()
319 {
320 return (this->GetScale() == VTK_SCALE_LOG10)? 1 : 0;
321 }
322
323 // Description:
324 // Get the number of available colors for mapping to.
325 virtual vtkIdType GetNumberOfAvailableColors();
326
327 // Description:
328 // Return a color given an integer index.
329 //
330 // This is used to assign colors to annotations (given an offset into the
331 // list of annotations).
332 // If the table is empty or \a idx < 0, then NanColor is returned.
333 virtual void GetIndexedColor(vtkIdType idx, double rgba[4]);
334
335 protected:
336 vtkLookupTable(int sze=256, int ext=256);
337 ~vtkLookupTable();
338
339 vtkIdType NumberOfColors;
340 vtkUnsignedCharArray *Table;
341 double TableRange[2];
342 double HueRange[2];
343 double SaturationRange[2];
344 double ValueRange[2];
345 double AlphaRange[2];
346 double NanColor[4];
347 double BelowRangeColor[4];
348 int UseBelowRangeColor;
349 double AboveRangeColor[4];
350 int UseAboveRangeColor;
351
352 int Scale;
353 int Ramp;
354 vtkTimeStamp InsertTime;
355 vtkTimeStamp BuildTime;
356 double RGBA[4]; //used during conversion process
357 unsigned char NanColorChar[4];
358
359 int OpaqueFlag;
360 vtkTimeStamp OpaqueFlagBuildTime;
361
362 private:
363 vtkLookupTable(const vtkLookupTable&); // Not implemented.
364 void operator=(const vtkLookupTable&); // Not implemented.
365 };
366
367 //----------------------------------------------------------------------------
WritePointer(const vtkIdType id,const int number)368 inline unsigned char *vtkLookupTable::WritePointer(const vtkIdType id,
369 const int number)
370 {
371 this->InsertTime.Modified();
372 return this->Table->WritePointer(4*id,4*number);
373 }
374
375 #endif
376
377
378
379