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::SetX2(int x2)
72 //----------------------------------------------------------------------------
73 void ColorLayerImageView::SetY2(int y2)
79 //----------------------------------------------------------------------------
80 void ColorLayerImageView::SetZ2(int z2)
85 //----------------------------------------------------------------------------
86 int ColorLayerImageView::GetX() // virtual
89 if (_fix_dynamic==false)
95 result = LayerImageBase::GetX();
101 //----------------------------------------------------------------------------
102 int ColorLayerImageView::GetY() // virtual
105 if (_fix_dynamic==false)
111 result = LayerImageBase::GetY();
117 //----------------------------------------------------------------------------
118 int ColorLayerImageView::GetZ() // virtual
121 if (_fix_dynamic==false)
127 result = LayerImageBase::GetZ();
133 //----------------------------------------------------------------------------
134 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
136 // The base color vector must be of a size multiple of 3, not null.
137 if (base_color.size() != 0 && base_color.size() % 3 == 0)
138 _base_color = base_color;
139 // Otherwise, an exception should be thrown.
140 else if (base_color.size() != 0)
141 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;
144 //----------------------------------------------------------------------------
145 double ColorLayerImageView::GetBaseColors(unsigned int index)
147 if (_base_color.size() > index)
148 return _base_color.at(index);
149 // An exception should be thrown if the index does not exist in the color vector.
152 std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
157 //----------------------------------------------------------------------------
158 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
160 // The size must be greater than or equal to 2 (at least min and max must exist).
161 if ( grey_level_boundary.size() >= 2)
163 sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
164 _grey_level_boundary = grey_level_boundary;
166 // Otherwise, an exception should be thrown.
167 else if (grey_level_boundary.size() != 0)
168 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;
171 //----------------------------------------------------------------------------
172 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
174 if (_grey_level_boundary.size() > index)
175 return _grey_level_boundary.at(index);
176 // An exception should be thrown if the index does not exist in the grey level boundary vector.
179 std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
184 //----------------------------------------------------------------------------
185 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
187 _color_type = color_type;
190 //----------------------------------------------------------------------------
191 int ColorLayerImageView::GetBaseColorNb()
193 return _base_color.size()/3;
196 //----------------------------------------------------------------------------
197 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
199 // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
200 _grey_level_boundary.clear();
202 // This avoids a potential division by 0 through delta during the lookup table configuration.
208 // Grey level extrema are set by default to the image grey level extrema.
209 double minTot = _range[0];
210 double maxTot = _range[1];
212 _grey_level_boundary.push_back(minTot);
214 // By default, the histogram is split into BaseColorNb areas of equal width.
215 double delta = (maxTot - minTot)/GetBaseColorNb();
216 for (int i = 1; i <= GetBaseColorNb() ; i ++)
218 _grey_level_boundary.push_back(minTot + i * delta);
222 //----------------------------------------------------------------------------
223 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
225 // CM Sets the default behaviour concerning the lookup table.
226 _grey_level_boundary.clear();
228 // Base colors are set to blue, yellow and red.
230 _base_color.push_back(0.0);
231 _base_color.push_back(0.0);
232 _base_color.push_back(1.0);
234 _base_color.push_back(1.0);
235 _base_color.push_back(1.0);
236 _base_color.push_back(0.0);
238 _base_color.push_back(1.0);
239 _base_color.push_back(0.0);
240 _base_color.push_back(0.0);
242 // This avoids a potential division by 0 through delta during the lookup table configuration.
248 double minTot = floor (_range[0]);
249 double maxTot = ceil (_range[1]);
251 _grey_level_boundary.push_back(minTot);
253 // By default, the histogram is split into three areas of equal width.
254 double delta = (maxTot - minTot)/3.0;
255 _grey_level_boundary.push_back(minTot + delta);
256 _grey_level_boundary.push_back(minTot + 2*delta);
258 _grey_level_boundary.push_back(maxTot);
261 //----------------------------------------------------------------------------
262 void ColorLayerImageView::ConfigLookupTable() // virtual
267 // Grey level extrema retrieved from the image grey level extrema.
269 GetImage()->GetScalarRange(_range);
270 double minRange = _range[0];
271 double maxRange = _range[1];
273 // ------------------ Setting Default Values
274 // Checks the size consistency of vectors _base_color and _grey_level_boundary.
275 // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
276 if (GetBaseColorNb() == 0)
278 SetDefaultBaseColorAndGreyLevelBoundary();
280 else { // 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.
281 if (GetBaseColorNb() != ((int)_grey_level_boundary.size() - 1) )
283 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;
284 std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
285 SetDefaultGreyLevelBoundary();
288 // ------------------ End Of Setting Default Values
290 // ------------------ Defining the Threshold Table
291 vtkLookupTable* thresholdTable = GetThresholdTable();
292 double thresholdTable_range[2];
293 double r1,r2,g1,g2,b1,b2;
296 // CM Number of table values consistent with the effective number of values (see loop below).
297 thresholdTable_range[1]= std::max( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) , ceil(_range[1]) );
298 thresholdTable_range[0]= std::min( GetGreyLevelBoundaries(0) , floor(_range[0]) );
300 thresholdTable->SetTableRange(thresholdTable_range);
301 thresholdTable->SetAlphaRange(0, 1);
302 thresholdTable->SetValueRange(0, 1);
304 thresholdTable->SetSaturationRange(0, 0);
305 thresholdTable->SetRampToLinear( );
307 maxColorsThresholdTable = 1000;
308 thresholdTable->SetNumberOfTableValues( maxColorsThresholdTable );
311 // Defines the lookup table.
312 // If the image has a degradation in one (or both) extremum (lower or higher),
313 // sets the corresponding grey levels as transparent white in the lookup table.
315 // _color_type true, i.e. plain colors.
316 // Sets the plain color for each grey level interval.
320 if ( minRange<GetGreyLevelBoundaries(0) )
323 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(0) );
324 FillColorTable(start,end, 0,0,0, 0,0,0);
327 if ( maxRange>GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) )
329 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) );
330 end = GrayLevel_TO_colorTableIndex( maxRange );
331 FillColorTable(start,end, 0,0,0, 0,0,0);
334 for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
336 r1 = GetBaseColors(iColor*3+0);
337 g1 = GetBaseColors(iColor*3+1);
338 b1 = GetBaseColors(iColor*3+2);
339 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );
340 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );
341 FillColorTable(start,end, r1,g1,b1,r1,g1,b1);
343 } // End Of if (_color_type)
345 // _color_type false, i.e. gradient color
349 if (GetBaseColorNb() > 1)
351 for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
359 r1 = GetBaseColors((iColor-1)*3+0);
360 g1 = GetBaseColors((iColor-1)*3+1);
361 b1 = GetBaseColors((iColor-1)*3+2);
364 r2 = GetBaseColors(iColor*3+0);
365 g2 = GetBaseColors(iColor*3+1);
366 b2 = GetBaseColors(iColor*3+2);
367 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );
368 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );
369 FillColorTable( start,end, r1,g1,b1, r2,g2,b2 );
372 } // End Of if (!_color_type)
374 thresholdTable->SetRange( minRange, maxRange );
375 thresholdTable->SetValueRange( 0.0, 1.0 );
377 thresholdTable->Build( );
382 //----------------------------------------------------------------------------
383 int ColorLayerImageView::GrayLevel_TO_colorTableIndex( double VALUE )
385 GetImage()->GetScalarRange(_range);
386 double minRange = _range[0];
387 double maxRange = _range[1];
388 return maxColorsThresholdTable * (VALUE-minRange) / (maxRange-minRange);
391 //----------------------------------------------------------------------------
392 void ColorLayerImageView::FillColorTable(int start, int end, double r1, double g1, double b1, double r2, double g2, double b2)
394 vtkLookupTable* thresholdTable = GetThresholdTable();
396 double delta = end-start;
397 double dr = (r2-r1)/delta;
398 double dg = (g2-g1)/delta;
399 double db = (b2-b1)/delta;
400 for (iTable=0; iTable<=delta; iTable++)
402 thresholdTable->SetTableValue( iTable+start , r1+dr*iTable, g1+dg*iTable, b1+db*iTable,1);
405 if (start==0) thresholdTable->SetTableValue( start , r1, g1, b1,0); // The first color in the table is transparent