3 * \author Eduardo Davila and Claire Mouton.
4 * \brief Class bbtk::ThresholdImageView.
8 #include "ColorLayerImageView.h"
10 #include <vtkImageReslice.h>
11 #include <vtkLookupTable.h>
12 #include <vtkImageData.h>
17 //=========================================================================
18 //=========================================================================
19 //=========================================================================
20 //=========================================================================
21 ColorLayerImageView::ColorLayerImageView( )
28 //=========================================================================
29 ColorLayerImageView::~ColorLayerImageView()
32 //=========================================================================
35 //----------------------------------------------------------------------------
36 void ColorLayerImageView::SetSliceFixDynamic(bool fix_dynamic)
38 _fix_dynamic = fix_dynamic;
41 //----------------------------------------------------------------------------
42 void ColorLayerImageView::SetZ2(int z2)
47 //----------------------------------------------------------------------------
48 int ColorLayerImageView::GetZ() // virtual
51 if (_fix_dynamic==false)
57 result = LayerImageBase::GetZ();
63 //----------------------------------------------------------------------------
64 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
66 // The base color vector must be of a size multiple of 3, not null.
67 if (base_color.size() != 0 && base_color.size() % 3 == 0)
68 _base_color = base_color;
69 // Otherwise, an exception should be thrown.
70 else if (base_color.size() != 0)
71 std::cout << "CM ColorLayerImageView::SetBaseColors : ERROR!!! The base color vector has an inconsistent size. It must be of a size multiple of 3, different from 0, but its size is: "<< base_color.size() << ". Therefore, the default values will be used as base colors." << std::endl;
74 //----------------------------------------------------------------------------
75 double ColorLayerImageView::GetBaseColors(unsigned int index)
77 if (_base_color.size() > index)
78 return _base_color.at(index);
79 // An exception should be thrown if the index does not exist in the color vector.
82 std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
87 //----------------------------------------------------------------------------
88 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
90 // The size must be greater than or equal to 2 (at least min and max must exist).
91 if ( grey_level_boundary.size() >= 2)
93 sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
94 _grey_level_boundary = grey_level_boundary;
96 // Otherwise, an exception should be thrown.
97 else if (grey_level_boundary.size() != 0)
98 std::cout << "CM ColorLayerImageView::SetGreyLevelBoundaries : ERROR!!! The grey level boundaries vector has an inconsistent size. It must be of a size greater than or equal to 2 (at least min and max must exist), but its size is: " << grey_level_boundary.size() << ". Therefore, the histogram will be equally split." << std::endl;
101 //----------------------------------------------------------------------------
102 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
104 if (_grey_level_boundary.size() > index)
105 return _grey_level_boundary.at(index);
106 // An exception should be thrown if the index does not exist in the grey level boundary vector.
109 std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
114 //----------------------------------------------------------------------------
115 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
117 _color_type = color_type;
120 //----------------------------------------------------------------------------
121 int ColorLayerImageView::GetBaseColorNb()
123 return _base_color.size()/3;
126 //----------------------------------------------------------------------------
127 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
129 // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
130 _grey_level_boundary.clear();
132 // This avoids a potential division by 0 through delta during the lookup table configuration.
138 // Grey level extrema are set by default to the image grey level extrema.
139 int minTot = floor (_range[0]);
140 int maxTot = ceil (_range[1]);
142 _grey_level_boundary.push_back((double)minTot);
144 // By default, the histogram is split into BaseColorNb areas of equal width.
145 double delta = (maxTot - minTot)/GetBaseColorNb();
146 for (int i = 1; i <= GetBaseColorNb() ; i ++)
148 _grey_level_boundary.push_back((double)minTot + i * delta);
152 //----------------------------------------------------------------------------
153 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
155 // CM Sets the default behaviour concerning the lookup table.
156 _grey_level_boundary.clear();
158 // Base colors are set to blue, yellow and red.
160 _base_color.push_back(0.0);
161 _base_color.push_back(0.0);
162 _base_color.push_back(1.0);
164 _base_color.push_back(1.0);
165 _base_color.push_back(1.0);
166 _base_color.push_back(0.0);
168 _base_color.push_back(1.0);
169 _base_color.push_back(0.0);
170 _base_color.push_back(0.0);
172 // This avoids a potential division by 0 through delta during the lookup table configuration.
178 int minTot = floor (_range[0]);
179 int maxTot = ceil (_range[1]);
181 _grey_level_boundary.push_back((double)minTot);
183 // By default, the histogram is split into three areas of equal width.
184 double delta = (maxTot - minTot)/3.0;
185 _grey_level_boundary.push_back(minTot + delta);
186 _grey_level_boundary.push_back(minTot + 2*delta);
188 _grey_level_boundary.push_back((double)maxTot);
191 //----------------------------------------------------------------------------
192 void ColorLayerImageView::ConfigLookupTable() // virtual
195 // Grey level extrema retrieved from the image grey level extrema.
196 GetImage()->GetScalarRange(_range);
199 // ------------------ Setting Default Values
200 // Checks the size consistency of vectors _base_color and _grey_level_boundary.
201 // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
202 if (GetBaseColorNb() == 0)
203 SetDefaultBaseColorAndGreyLevelBoundary();
205 // If at least one color has been set, set the grey level boundaries to build an equipartition of the image grey levels, keeping the base colors defined.
207 if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
209 std::cout << "CM ColorLayerImageView::ConfigLookupTable : ERROR!!! Inconsistency between the sizes of vectors _base_color and _grey_level_boundary. _base_color.size()/3 (=" <<_base_color.size()/3 << ") should be equal to _grey_level_boundary.size() - 1 (=" << _grey_level_boundary.size() - 1 << ")." << std::endl;
210 std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
211 SetDefaultGreyLevelBoundary();
213 // ------------------ End Of Setting Default Values
216 // ------------------ Defining the Threshold Table
217 vtkLookupTable* thresholdTable = GetThresholdTable();
219 // CM Number of table values consistent with the effective number of values (see loop below).
220 thresholdTable->SetNumberOfTableValues(std::max( GetGreyLevelBoundaries( GetBaseColorNb() ), ceil(_range[1]) ) - std::min( GetGreyLevelBoundaries(0), floor(_range[0]) ) + 1);
221 thresholdTable->SetTableRange(_range);
222 thresholdTable->SetAlphaRange(0, 1);
223 thresholdTable->SetValueRange(0, 1);
224 thresholdTable->SetSaturationRange(0, 0);
225 thresholdTable->SetRampToLinear( );
227 // Defines the lookup table.
228 // If the image has a degradation in one (or both) extremum (lower or higher),
229 // sets the corresponding grey levels as transparent white in the lookup table.
230 for (int i = floor(_range[0]); i <= GetGreyLevelBoundaries(0); i++)
232 thresholdTable -> SetTableValue( i, 0, 0, 0, 0);
234 for (int i = GetGreyLevelBoundaries( GetBaseColorNb() ) + 1 ; i <= ceil(_range[1]); i++)
236 thresholdTable -> SetTableValue( i, 0, 0, 0, 0);
241 // _color_type true, i.e. plain colors.
242 // Sets the plain color for each grey level interval.
245 for (int i = 0; i < GetBaseColorNb(); i++)
247 delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
248 for (int ii = 1; ii <= delta; ii++)
250 thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
252 GetBaseColors(i*3 + 1),
253 GetBaseColors(i*3 + 2),
257 } // End Of if (_color_type)
260 // _color_type false, i.e. gradient color
264 // Creates a linear range from white to the first color.
265 delta = GetGreyLevelBoundaries(1) - GetGreyLevelBoundaries(0);
266 for (int ii = 1; ii <= delta ; ii++)
268 thresholdTable->SetTableValue( GetGreyLevelBoundaries(0) + ii, GetBaseColors(0) * ii/delta,
269 GetBaseColors(1) * ii/delta, GetBaseColors(2) * ii/delta, 1);
272 // If other colors exist:
273 // Creates linear ranges between one color and the following in the color vector.
274 if (GetBaseColorNb() > 1)
276 for (int i = 1; i < GetBaseColorNb(); i++)
278 delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
279 for (int ii = 1; ii <= delta; ii++)
281 // Color computation : previous_color + (current_color - previous_color)/delta * ii
282 thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
283 GetBaseColors((i-1)*3) + (GetBaseColors(i*3) - GetBaseColors((i-1)*3)) / delta * ii ,
284 GetBaseColors((i-1)*3 + 1) + (GetBaseColors(i*3 + 1) - GetBaseColors((i-1)*3 + 1)) / delta * ii ,
285 GetBaseColors((i-1)*3 + 2) + (GetBaseColors(i*3 + 2) - GetBaseColors((i-1)*3 + 2)) / delta * ii ,
290 } // End Of if (!_color_type)
292 thresholdTable->Build( );
293 // ------------------ End Of Defining the Threshold Table