1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
28 * \author Eduardo Davila and Claire Mouton.
29 * \brief Class bbtk::ThresholdImageView.
30 * \date September 2012
33 #include "ColorLayerImageView.h"
35 #include <vtkImageReslice.h>
36 #include <vtkLookupTable.h>
37 #include <vtkImageData.h>
42 //=========================================================================
43 //=========================================================================
44 //=========================================================================
45 //=========================================================================
46 ColorLayerImageView::ColorLayerImageView( )
53 //=========================================================================
54 ColorLayerImageView::~ColorLayerImageView()
57 //=========================================================================
60 //----------------------------------------------------------------------------
61 void ColorLayerImageView::SetSliceFixDynamic(bool fix_dynamic)
63 _fix_dynamic = fix_dynamic;
66 //----------------------------------------------------------------------------
67 void ColorLayerImageView::SetZ2(int z2)
72 //----------------------------------------------------------------------------
73 int ColorLayerImageView::GetZ() // virtual
76 if (_fix_dynamic==false)
82 result = LayerImageBase::GetZ();
88 //----------------------------------------------------------------------------
89 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
91 // The base color vector must be of a size multiple of 3, not null.
92 if (base_color.size() != 0 && base_color.size() % 3 == 0)
93 _base_color = base_color;
94 // Otherwise, an exception should be thrown.
95 else if (base_color.size() != 0)
96 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;
99 //----------------------------------------------------------------------------
100 double ColorLayerImageView::GetBaseColors(unsigned int index)
102 if (_base_color.size() > index)
103 return _base_color.at(index);
104 // An exception should be thrown if the index does not exist in the color vector.
107 std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
112 //----------------------------------------------------------------------------
113 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
115 // The size must be greater than or equal to 2 (at least min and max must exist).
116 if ( grey_level_boundary.size() >= 2)
118 sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
119 _grey_level_boundary = grey_level_boundary;
121 // Otherwise, an exception should be thrown.
122 else if (grey_level_boundary.size() != 0)
123 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;
126 //----------------------------------------------------------------------------
127 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
129 if (_grey_level_boundary.size() > index)
130 return _grey_level_boundary.at(index);
131 // An exception should be thrown if the index does not exist in the grey level boundary vector.
134 std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
139 //----------------------------------------------------------------------------
140 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
142 _color_type = color_type;
145 //----------------------------------------------------------------------------
146 int ColorLayerImageView::GetBaseColorNb()
148 return _base_color.size()/3;
151 //----------------------------------------------------------------------------
152 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
154 // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
155 _grey_level_boundary.clear();
157 // This avoids a potential division by 0 through delta during the lookup table configuration.
163 // Grey level extrema are set by default to the image grey level extrema.
164 int minTot = floor (_range[0]);
165 int maxTot = ceil (_range[1]);
167 _grey_level_boundary.push_back((double)minTot);
169 // By default, the histogram is split into BaseColorNb areas of equal width.
170 double delta = (maxTot - minTot)/GetBaseColorNb();
171 for (int i = 1; i <= GetBaseColorNb() ; i ++)
173 _grey_level_boundary.push_back((double)minTot + i * delta);
177 //----------------------------------------------------------------------------
178 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
180 // CM Sets the default behaviour concerning the lookup table.
181 _grey_level_boundary.clear();
183 // Base colors are set to blue, yellow and red.
185 _base_color.push_back(0.0);
186 _base_color.push_back(0.0);
187 _base_color.push_back(1.0);
189 _base_color.push_back(1.0);
190 _base_color.push_back(1.0);
191 _base_color.push_back(0.0);
193 _base_color.push_back(1.0);
194 _base_color.push_back(0.0);
195 _base_color.push_back(0.0);
197 // This avoids a potential division by 0 through delta during the lookup table configuration.
203 int minTot = floor (_range[0]);
204 int maxTot = ceil (_range[1]);
206 _grey_level_boundary.push_back((double)minTot);
208 // By default, the histogram is split into three areas of equal width.
209 double delta = (maxTot - minTot)/3.0;
210 _grey_level_boundary.push_back(minTot + delta);
211 _grey_level_boundary.push_back(minTot + 2*delta);
213 _grey_level_boundary.push_back((double)maxTot);
216 //----------------------------------------------------------------------------
217 void ColorLayerImageView::ConfigLookupTable() // virtual
220 // Grey level extrema retrieved from the image grey level extrema.
221 GetImage()->GetScalarRange(_range);
224 // ------------------ Setting Default Values
225 // Checks the size consistency of vectors _base_color and _grey_level_boundary.
226 // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
227 if (GetBaseColorNb() == 0)
228 SetDefaultBaseColorAndGreyLevelBoundary();
230 // 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.
232 if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
234 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;
235 std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
236 SetDefaultGreyLevelBoundary();
238 // ------------------ End Of Setting Default Values
241 // ------------------ Defining the Threshold Table
242 vtkLookupTable* thresholdTable = GetThresholdTable();
244 // CM Number of table values consistent with the effective number of values (see loop below).
245 thresholdTable->SetNumberOfTableValues(std::max( GetGreyLevelBoundaries( GetBaseColorNb() ), ceil(_range[1]) ) - std::min( GetGreyLevelBoundaries(0), floor(_range[0]) ) + 1);
246 thresholdTable->SetTableRange(_range);
247 thresholdTable->SetAlphaRange(0, 1);
248 thresholdTable->SetValueRange(0, 1);
249 thresholdTable->SetSaturationRange(0, 0);
250 thresholdTable->SetRampToLinear( );
252 // Defines the lookup table.
253 // If the image has a degradation in one (or both) extremum (lower or higher),
254 // sets the corresponding grey levels as transparent white in the lookup table.
255 for (int i = floor(_range[0]); i <= GetGreyLevelBoundaries(0); i++)
257 thresholdTable -> SetTableValue( i, 0, 0, 0, 0);
259 for (int i = GetGreyLevelBoundaries( GetBaseColorNb() ) + 1 ; i <= ceil(_range[1]); i++)
261 thresholdTable -> SetTableValue( i, 0, 0, 0, 0);
266 // _color_type true, i.e. plain colors.
267 // Sets the plain color for each grey level interval.
270 for (int i = 0; i < GetBaseColorNb(); i++)
272 delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
273 for (int ii = 1; ii <= delta; ii++)
275 thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
277 GetBaseColors(i*3 + 1),
278 GetBaseColors(i*3 + 2),
282 } // End Of if (_color_type)
285 // _color_type false, i.e. gradient color
289 // Creates a linear range from white to the first color.
290 delta = GetGreyLevelBoundaries(1) - GetGreyLevelBoundaries(0);
291 for (int ii = 1; ii <= delta ; ii++)
293 thresholdTable->SetTableValue( GetGreyLevelBoundaries(0) + ii, GetBaseColors(0) * ii/delta,
294 GetBaseColors(1) * ii/delta, GetBaseColors(2) * ii/delta, 1);
297 // If other colors exist:
298 // Creates linear ranges between one color and the following in the color vector.
299 if (GetBaseColorNb() > 1)
301 for (int i = 1; i < GetBaseColorNb(); i++)
303 delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
304 for (int ii = 1; ii <= delta; ii++)
306 // Color computation : previous_color + (current_color - previous_color)/delta * ii
307 thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
308 GetBaseColors((i-1)*3) + (GetBaseColors(i*3) - GetBaseColors((i-1)*3)) / delta * ii ,
309 GetBaseColors((i-1)*3 + 1) + (GetBaseColors(i*3 + 1) - GetBaseColors((i-1)*3 + 1)) / delta * ii ,
310 GetBaseColors((i-1)*3 + 2) + (GetBaseColors(i*3 + 2) - GetBaseColors((i-1)*3 + 2)) / delta * ii ,
315 } // End Of if (!_color_type)
317 thresholdTable->Build( );
318 // ------------------ End Of Defining the Threshold Table