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)
87 //----------------------------------------------------------------------------
88 int ColorLayerImageView::GetX() // virtual
91 if (_fix_dynamic==false)
97 result = LayerImageBase::GetX();
103 //----------------------------------------------------------------------------
104 int ColorLayerImageView::GetY() // virtual
107 if (_fix_dynamic==false)
113 result = LayerImageBase::GetY();
119 //----------------------------------------------------------------------------
120 int ColorLayerImageView::GetZ() // virtual
123 if (_fix_dynamic==false)
129 result = LayerImageBase::GetZ();
135 //----------------------------------------------------------------------------
136 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
138 // The base color vector must be of a size multiple of 3, not null.
139 if (base_color.size() != 0 && base_color.size() % 3 == 0)
140 _base_color = base_color;
141 // Otherwise, an exception should be thrown.
142 else if (base_color.size() != 0)
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;
146 //----------------------------------------------------------------------------
147 double ColorLayerImageView::GetBaseColors(unsigned int index)
149 if (_base_color.size() > index)
150 return _base_color.at(index);
151 // An exception should be thrown if the index does not exist in the color vector.
154 std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
159 //----------------------------------------------------------------------------
160 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
162 // The size must be greater than or equal to 2 (at least min and max must exist).
163 if ( grey_level_boundary.size() >= 2)
165 sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
166 _grey_level_boundary = grey_level_boundary;
168 // Otherwise, an exception should be thrown.
169 else if (grey_level_boundary.size() != 0)
170 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;
173 //----------------------------------------------------------------------------
174 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
176 if (_grey_level_boundary.size() > index)
177 return _grey_level_boundary.at(index);
178 // An exception should be thrown if the index does not exist in the grey level boundary vector.
181 std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
186 //----------------------------------------------------------------------------
187 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
189 _color_type = color_type;
192 //----------------------------------------------------------------------------
193 int ColorLayerImageView::GetBaseColorNb()
195 return _base_color.size()/3;
198 //----------------------------------------------------------------------------
199 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
201 // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
202 _grey_level_boundary.clear();
204 // This avoids a potential division by 0 through delta during the lookup table configuration.
210 // Grey level extrema are set by default to the image grey level extrema.
211 int minTot = floor (_range[0]);
212 int maxTot = ceil (_range[1]);
214 _grey_level_boundary.push_back((double)minTot);
216 // By default, the histogram is split into BaseColorNb areas of equal width.
217 double delta = (maxTot - minTot)/GetBaseColorNb();
218 for (int i = 1; i <= GetBaseColorNb() ; i ++)
220 _grey_level_boundary.push_back((double)minTot + i * delta);
224 //----------------------------------------------------------------------------
225 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
227 // CM Sets the default behaviour concerning the lookup table.
228 _grey_level_boundary.clear();
230 // Base colors are set to blue, yellow and red.
232 _base_color.push_back(0.0);
233 _base_color.push_back(0.0);
234 _base_color.push_back(1.0);
236 _base_color.push_back(1.0);
237 _base_color.push_back(1.0);
238 _base_color.push_back(0.0);
240 _base_color.push_back(1.0);
241 _base_color.push_back(0.0);
242 _base_color.push_back(0.0);
244 // This avoids a potential division by 0 through delta during the lookup table configuration.
250 int minTot = floor (_range[0]);
251 int maxTot = ceil (_range[1]);
253 _grey_level_boundary.push_back((double)minTot);
255 // By default, the histogram is split into three areas of equal width.
256 double delta = (maxTot - minTot)/3.0;
257 _grey_level_boundary.push_back(minTot + delta);
258 _grey_level_boundary.push_back(minTot + 2*delta);
260 _grey_level_boundary.push_back((double)maxTot);
263 //----------------------------------------------------------------------------
264 void ColorLayerImageView::ConfigLookupTable() // virtual
269 // Grey level extrema retrieved from the image grey level extrema.
271 GetImage()->GetScalarRange(_range);
272 double minRange = _range[0];
273 double maxRange = _range[1];
275 // ------------------ Setting Default Values
276 // Checks the size consistency of vectors _base_color and _grey_level_boundary.
277 // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
278 if (GetBaseColorNb() == 0)
280 SetDefaultBaseColorAndGreyLevelBoundary();
282 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.
283 if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
285 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;
286 std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
287 SetDefaultGreyLevelBoundary();
290 // ------------------ End Of Setting Default Values
292 // ------------------ Defining the Threshold Table
293 vtkLookupTable* thresholdTable = GetThresholdTable();
294 double thresholdTable_range[2];
295 double r1,r2,g1,g2,b1,b2;
298 // CM Number of table values consistent with the effective number of values (see loop below).
299 thresholdTable_range[1]= std::max( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) , ceil(_range[1]) );
300 thresholdTable_range[0]= std::min( GetGreyLevelBoundaries(0) , floor(_range[0]) );
302 thresholdTable->SetTableRange(thresholdTable_range);
303 thresholdTable->SetAlphaRange(0, 1);
304 thresholdTable->SetValueRange(0, 1);
306 thresholdTable->SetSaturationRange(0, 0);
307 thresholdTable->SetRampToLinear( );
309 thresholdTable->SetNumberOfTableValues(1000);
312 // Defines the lookup table.
313 // If the image has a degradation in one (or both) extremum (lower or higher),
314 // sets the corresponding grey levels as transparent white in the lookup table.
316 // _color_type true, i.e. plain colors.
317 // Sets the plain color for each grey level interval.
320 // thresholdTable->SetNumberOfTableValues(thresholdTable_range[1] - thresholdTable_range[0] + 1);
322 if ( minRange<GetGreyLevelBoundaries(0) )
325 end = 1000*(GetGreyLevelBoundaries(0)-minRange) / (maxRange-minRange);
326 FillColorTable(start,end, 0,0,0, 0,0,0);
329 if ( maxRange>GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) )
331 start = 1000*(GetGreyLevelBoundaries( _grey_level_boundary.size()-1 )-minRange) / (maxRange-minRange);;
332 end = 1000*(maxRange-minRange) / (maxRange-minRange);
333 FillColorTable(start,end, 0,0,0, 0,0,0);
337 for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
340 start = 1000*(GetGreyLevelBoundaries(iColor)-minRange) / (maxRange-minRange);
341 end = 1000*(GetGreyLevelBoundaries(iColor+1)-minRange) / (maxRange-minRange);
343 r1 = GetBaseColors(iColor*3+0);
344 g1 = GetBaseColors(iColor*3+1);
345 b1 = GetBaseColors(iColor*3+2);
347 FillColorTable(start,end, r1,g1,b1,r1,g1,b1);
350 delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
351 for (int ii = 1; ii <= delta; ii++)
353 thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
355 GetBaseColors(i*3 + 1),
356 GetBaseColors(i*3 + 2),
362 } // End Of if (_color_type)
364 // _color_type false, i.e. gradient color
368 thresholdTable->SetNumberOfTableValues(1000);
369 if (GetBaseColorNb() > 1)
371 for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
379 r1 = GetBaseColors((iColor-1)*3+0);
380 g1 = GetBaseColors((iColor-1)*3+1);
381 b1 = GetBaseColors((iColor-1)*3+2);
384 r2 = GetBaseColors(iColor*3+0);
385 g2 = GetBaseColors(iColor*3+1);
386 b2 = GetBaseColors(iColor*3+2);
387 start = 1000*(GetGreyLevelBoundaries(iColor)-minRange) / (maxRange-minRange);
388 end = 1000*(GetGreyLevelBoundaries(iColor+1)-minRange) / (maxRange-minRange);
389 FillColorTable(start,end, r1,g1,b1,r2,g2,b2);
392 } // End Of if (!_color_type)
394 thresholdTable->Build( );
400 void ColorLayerImageView::FillColorTable(int start, int end, double r1, double g1, double b1, double r2, double g2, double b2)
402 vtkLookupTable* thresholdTable = GetThresholdTable();
404 double delta = end-start;
405 double dr = (r2-r1)/delta;
406 double dg = (g2-g1)/delta;
407 double db = (b2-b1)/delta;
408 for (iTable=0; iTable<=delta; iTable++)
410 thresholdTable->SetTableValue( iTable+start , r1+dr*iTable, g1+dg*iTable, b1+db*iTable,1);