]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageView.cxx
Improved the documentation and the error messages of ColorLayerImageView.
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ColorLayerImageView.cxx
1 /**
2  *  \file 
3  *  \author Eduardo Davila and Claire Mouton.
4  *  \brief Class bbtk::ThresholdImageView. 
5  *  \date September 2012
6  */
7
8 #include "ColorLayerImageView.h"
9
10 #include <vtkImageReslice.h>
11 #include <vtkLookupTable.h>
12 #include <vtkImageData.h>
13
14 #include <algorithm>
15
16
17 //=========================================================================
18 //=========================================================================
19 //=========================================================================
20 //=========================================================================
21 ColorLayerImageView::ColorLayerImageView( )
22 {
23   _z2           =       0;
24   _fix_dynamic  =       true;
25   _color_type   =       false;
26 }
27
28 //=========================================================================
29 ColorLayerImageView::~ColorLayerImageView()
30 {
31 }
32 //=========================================================================
33
34
35 //----------------------------------------------------------------------------
36 void ColorLayerImageView::SetSliceFixDynamic(bool fix_dynamic)
37 {
38   _fix_dynamic = fix_dynamic;
39 }
40
41 //----------------------------------------------------------------------------
42 void ColorLayerImageView::SetZ2(int z2)  
43 {
44   _z2 = z2;
45 }
46
47 //----------------------------------------------------------------------------
48 int ColorLayerImageView::GetZ() // virtual 
49 {
50   int result=0;
51   if (_fix_dynamic==false)
52     {
53       result = _z2;
54     }
55   else
56     {
57       result = LayerImageBase::GetZ();
58     }  
59
60   return result;
61 }
62
63 //----------------------------------------------------------------------------
64 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
65 {
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;
72 }
73
74 //----------------------------------------------------------------------------
75 double ColorLayerImageView::GetBaseColors(unsigned int index)
76 {
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.
80   else
81     {
82       std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
83       return -1.0;
84     }
85 }
86
87 //----------------------------------------------------------------------------
88 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
89 {
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)
92     {
93       sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
94       _grey_level_boundary = grey_level_boundary;
95     }
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;
99 }
100
101 //----------------------------------------------------------------------------
102 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
103 {  
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.
107   else
108     {
109       std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
110       return -1.0;
111     }
112 }
113
114 //----------------------------------------------------------------------------
115 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
116 {
117   _color_type = color_type;
118
119
120 //----------------------------------------------------------------------------
121 int ColorLayerImageView::GetBaseColorNb()
122 {
123   return _base_color.size()/3;
124 }
125
126 //----------------------------------------------------------------------------
127 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
128 {
129   // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
130   _grey_level_boundary.clear();
131
132   // This avoids a potential division by 0 through delta during the lookup table configuration.
133   if (_range[1]==0)
134     {
135       _range[1]=255;
136     }
137           
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]);
141
142   _grey_level_boundary.push_back((double)minTot);
143   
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 ++)
147     {
148       _grey_level_boundary.push_back((double)minTot + i * delta);
149     }
150 }
151
152 //----------------------------------------------------------------------------
153 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
154 {
155   // CM Sets the default behaviour concerning the lookup table.
156   _grey_level_boundary.clear();
157   _base_color.clear();
158   // Base colors are set to blue, yellow and red.
159   // Blue.
160   _base_color.push_back(0.0);
161   _base_color.push_back(0.0);
162   _base_color.push_back(1.0);
163   // Yellow.
164   _base_color.push_back(1.0);
165   _base_color.push_back(1.0);
166   _base_color.push_back(0.0);
167   // Red.
168   _base_color.push_back(1.0);
169   _base_color.push_back(0.0);
170   _base_color.push_back(0.0);
171
172   // This avoids a potential division by 0 through delta during the lookup table configuration.
173   if (_range[1]==0)
174     {
175       _range[1]=255;
176     }
177           
178   int minTot = floor (_range[0]);
179   int maxTot = ceil (_range[1]);
180           
181   _grey_level_boundary.push_back((double)minTot);
182  
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);
187
188   _grey_level_boundary.push_back((double)maxTot);
189 }
190
191 //----------------------------------------------------------------------------
192 void ColorLayerImageView::ConfigLookupTable()  // virtual
193 {
194   // CM
195   // Grey level extrema retrieved from the image grey level extrema.
196   GetImage()->GetScalarRange(_range);
197
198
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();
204
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.
206   else
207     if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
208       {
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();     
212       }
213   // ------------------ End Of Setting Default Values
214
215
216   // ------------------ Defining the Threshold Table
217   vtkLookupTable* thresholdTable = GetThresholdTable();
218
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( );
226
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++)
231     {
232       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
233     }
234   for (int i = GetGreyLevelBoundaries( GetBaseColorNb() ) + 1 ; i <= ceil(_range[1]); i++)
235     {
236       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
237     }
238         
239   int delta;
240
241   // _color_type true, i.e. plain colors.
242   // Sets the plain color for each grey level interval.
243   if (_color_type)
244     {
245       for (int i = 0; i < GetBaseColorNb(); i++)
246         {
247           delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
248           for (int ii = 1; ii <= delta; ii++)
249             {
250               thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
251                                             GetBaseColors(i*3), 
252                                             GetBaseColors(i*3 + 1),
253                                             GetBaseColors(i*3 + 2),
254                                             1);
255             }
256         }
257     } // End Of if (_color_type)
258
259         
260   // _color_type false, i.e. gradient color
261   else
262     {
263       // First 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++)
267         {
268           thresholdTable->SetTableValue( GetGreyLevelBoundaries(0) + ii, GetBaseColors(0) * ii/delta,
269                                          GetBaseColors(1) * ii/delta, GetBaseColors(2) * ii/delta, 1);
270         }
271             
272       // If other colors exist:
273       // Creates linear ranges between one color and the following in the color vector.
274       if (GetBaseColorNb() > 1)
275         {
276           for (int i = 1; i < GetBaseColorNb(); i++)
277             {
278               delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
279               for (int ii = 1; ii <= delta; ii++)
280                 {
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 ,
286                                                 1);
287                 }
288             }
289         }
290     } //  End Of if (!_color_type)
291         
292   thresholdTable->Build( );
293   // ------------------ End Of Defining the Threshold Table
294
295   //EO CM
296 }
297
298 // EOF
299