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 //EED if ((base_color.size() != 0) && (base_color.size() % 3 == 0))
138 if ( base_color.size() % 3 == 0)
140 _base_color = base_color;
141 } else if (base_color.size() != 0) // Otherwise, an exception should be thrown.
143 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;
148 //----------------------------------------------------------------------------
149 void ColorLayerImageView::SetBaseTransparence(std::vector<double> & base_transparence)
151 _transparence_level_boundary = base_transparence;
155 //----------------------------------------------------------------------------
156 double ColorLayerImageView::GetBaseColors(unsigned int index)
158 if (_base_color.size() > index)
159 return _base_color.at(index);
160 // An exception should be thrown if the index does not exist in the color vector.
163 std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
168 //----------------------------------------------------------------------------
169 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
171 _grey_level_boundary = grey_level_boundary;
175 // The size must be greater than or equal to 2 (at least min and max must exist).
176 if ( grey_level_boundary.size() >= 2)
178 sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
179 _grey_level_boundary = grey_level_boundary;
181 // Otherwise, an exception should be thrown.
182 else if (grey_level_boundary.size() != 0)
183 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;
188 //----------------------------------------------------------------------------
189 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
191 if (_grey_level_boundary.size() > index)
192 return _grey_level_boundary.at(index);
193 // An exception should be thrown if the index does not exist in the grey level boundary vector.
196 std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
201 //----------------------------------------------------------------------------
202 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
204 _color_type = color_type;
207 //----------------------------------------------------------------------------
208 int ColorLayerImageView::GetBaseColorNb()
210 return _base_color.size()/3;
213 //----------------------------------------------------------------------------
214 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
217 // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
218 _grey_level_boundary.clear();
220 // This avoids a potential division by 0 through delta during the lookup table configuration.
228 // Grey level extrema are set by default to the image grey level extrema.
229 double minTot = _range[0];
230 double maxTot = _range[1];
234 delta = (maxTot - minTot)/GetBaseColorNb();
235 // By default, the histogram is split into BaseColorNb areas of equal width.
236 for (i = 0; i <= GetBaseColorNb() ; i ++)
238 _grey_level_boundary.push_back(minTot + i * delta);
241 delta = (maxTot - minTot)/(GetBaseColorNb()-1);
242 for (i = 0; i < GetBaseColorNb() ; i ++)
244 _grey_level_boundary.push_back(minTot + i * delta);
249 //----------------------------------------------------------------------------
250 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
253 // CM Sets the default behaviour concerning the lookup table.
254 _grey_level_boundary.clear();
256 // Base colors are set to blue, yellow and red.
258 _base_color.push_back(0.0);
259 _base_color.push_back(0.0);
260 _base_color.push_back(0.5);
262 _base_color.push_back(0.0);
263 _base_color.push_back(0.0);
264 _base_color.push_back(1.0);
266 _base_color.push_back(0.0);
267 _base_color.push_back(1.0);
268 _base_color.push_back(1.0);
270 _base_color.push_back(1.0);
271 _base_color.push_back(1.0);
272 _base_color.push_back(0.0);
274 _base_color.push_back(1.0);
275 _base_color.push_back(0.0);
276 _base_color.push_back(0.0);
278 _base_color.push_back(0.5);
279 _base_color.push_back(0.0);
280 _base_color.push_back(0.0);
283 // This avoids a potential division by 0 through delta during the lookup table configuration.
289 // double minTot = floor (_range[0]);
290 // double maxTot = ceil (_range[1]);
291 double minTot = _range[0];
292 double maxTot = _range[1];
294 double delta = (maxTot - minTot)/8.0;
295 // By default, the histogram is split into three areas of equal width.
297 _grey_level_boundary.push_back(minTot + delta*0);
298 _grey_level_boundary.push_back(minTot + delta*1);
299 // _grey_level_boundary.push_back(minTot + delta*2);
300 _grey_level_boundary.push_back(minTot + delta*3);
301 // _grey_level_boundary.push_back(minTot + delta*4);
302 _grey_level_boundary.push_back(minTot + delta*5);
303 // _grey_level_boundary.push_back(minTot + delta*6);
304 _grey_level_boundary.push_back(minTot + delta*7);
305 _grey_level_boundary.push_back(minTot + delta*8);
308 //----------------------------------------------------------------------------
309 void ColorLayerImageView::ConfigLookupTable() // virtual
314 // Grey level extrema retrieved from the image grey level extrema.
316 // GetImage()->GetScalarRange(_range);
317 GetImageScalarRange();
318 double minRange = _range[0];
319 double maxRange = _range[1];
321 // ------------------ Setting Default Values
322 // Checks the size consistency of vectors _base_color and _grey_level_boundary.
323 // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
325 if (GetBaseColorNb() == 0)
327 SetDefaultBaseColorAndGreyLevelBoundary();
328 } 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.
329 if (_color_type==true) // Plain
331 if (GetBaseColorNb() != (int)_grey_level_boundary.size()-1)
333 //EED 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;
334 //EED std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
335 SetDefaultGreyLevelBoundary();
339 if (_color_type==false) // Gradiant
341 if (GetBaseColorNb() != (int)_grey_level_boundary.size())
343 //EED 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() (=" << _grey_level_boundary.size()<<")." << std::endl;
344 //EED std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
345 SetDefaultGreyLevelBoundary();
350 // ------------------ End Of Setting Default Values
352 int iTransparence,transparenceSize = _transparence_level_boundary.size();
353 for (iTransparence=transparenceSize ; iTransparence < GetBaseColorNb(); iTransparence++ )
355 _transparence_level_boundary.push_back(1);
359 // ------------------ Defining the Threshold Table
360 vtkLookupTable* thresholdTable = GetThresholdTable();
362 double thresholdTable_range[2];
363 double r1,r2,g1,g2,b1,b2,t1,t2;
366 // CM Number of table values consistent with the effective number of values (see loop below).
367 thresholdTable_range[1]= std::max( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) , ceil(maxRange) );
368 thresholdTable_range[0]= std::min( GetGreyLevelBoundaries(0), floor(minRange) );
370 thresholdTable_range[1]= GetGreyLevelBoundaries( _grey_level_boundary.size()-1 );
371 thresholdTable_range[0]= GetGreyLevelBoundaries(0);
373 thresholdTable->SetTableRange(thresholdTable_range);
374 thresholdTable->SetAlphaRange( 0,1 );
375 thresholdTable->SetValueRange( 0,1 );
377 thresholdTable->SetSaturationRange( 0,0 );
378 thresholdTable->SetRampToLinear( );
380 maxColorsThresholdTable = 1000;
381 thresholdTable->SetNumberOfTableValues( maxColorsThresholdTable+1 );
382 thresholdTable->SetNanColor(0,0,0,0);
385 // Defines the lookup table.
386 // If the image has a degradation in one (or both) extremum (lower or higher),
387 // sets the corresponding grey levels as transparent white in the lookup table.
389 // _color_type true, i.e. plain colors.
390 // Sets the plain color for each grey level interval.
391 if (_color_type) // Plain
393 if ( minRange<GetGreyLevelBoundaries(0) )
396 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(0) );
397 FillColorTable(start,end, 0,0,0, 0,0,0, 0,0);
400 if ( maxRange>GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) )
402 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) );
403 end = GrayLevel_TO_colorTableIndex( maxRange );
404 FillColorTable(start,end, 0,0,0, 0,0,0, 0,0);
406 for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
408 r1 = GetBaseColors(iColor*3+0);
409 g1 = GetBaseColors(iColor*3+1);
410 b1 = GetBaseColors(iColor*3+2);
411 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );
412 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );
413 t1 = _transparence_level_boundary[iColor];
414 // t2 = _transparence_level_boundary[iColor+1];
415 FillColorTable(start,end, r1,g1,b1,r1,g1,b1,t1,t1);
417 } else { // _color_type false, i.e. gradient color
418 if (GetBaseColorNb() > 1)
420 for (int iColor = 0; iColor < GetBaseColorNb()-1; iColor++)
422 r1 = GetBaseColors( (iColor+0)*3+0 );
423 g1 = GetBaseColors( (iColor+0)*3+1 );
424 b1 = GetBaseColors( (iColor+0)*3+2 );
425 r2 = GetBaseColors( (iColor+1)*3+0);
426 g2 = GetBaseColors( (iColor+1)*3+1);
427 b2 = GetBaseColors( (iColor+1)*3+2);
428 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );
429 end = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );
430 t1 = _transparence_level_boundary[iColor];
431 t2 = _transparence_level_boundary[iColor+1];
432 //printf("EED ColorLayerImageView::ConfigLookupTable Make something with transparence \n");
433 FillColorTable( start,end, r1,g1,b1, r2,g2,b2, t1,t2 );
436 } // End Of if (!_color_type)
438 double minRangeForColorBar, maxRangeForColorBar;
439 GetRangeForColorBar( minRangeForColorBar, maxRangeForColorBar );
440 thresholdTable->SetRange( minRangeForColorBar, maxRangeForColorBar );
442 // thresholdTable->SetTableRange( minRange, maxRange );
443 // thresholdTable->SetValueRange( 0.0, 1.0 );
444 // thresholdTable->SetAboveRangeColor(0,0,0,0);
445 // thresholdTable->SetBelowRangeColor(0,0,0,0);
447 thresholdTable->Build( );
451 //----------------------------------------------------------------------------
452 int ColorLayerImageView::GrayLevel_TO_colorTableIndex( double VALUE )
455 //EED 2017-01-01 Migration VTK7
456 #if VTK_MAJOR_VERSION <= 5
457 GetImage()->Update();
466 // GetImage()->GetScalarRange(_range);
467 GetImageScalarRange();
469 double minRange = _range[0];
470 double maxRange = _range[1];
471 return maxColorsThresholdTable * (VALUE-minRange) / (maxRange-minRange);
474 //----------------------------------------------------------------------------
475 void ColorLayerImageView::FillColorTable(int start, int end, double r1, double g1, double b1, double r2, double g2, double b2, double t1, double t2)
477 vtkLookupTable* thresholdTable = GetThresholdTable();
479 double delta = end-start;
480 double dr = (r2-r1)/delta;
481 double dg = (g2-g1)/delta;
482 double db = (b2-b1)/delta;
483 double dt = (t2-t1)/delta;
484 for (iTable=0; iTable<=delta; iTable++)
486 thresholdTable->SetTableValue( iTable+start , r1+dr*iTable, g1+dg*iTable, b1+db*iTable,t1+dt*iTable);