]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageView.cxx
#2507 creaMaracasVisu Feature New Normal - creaPanelButtonContainer ListPanel
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ColorLayerImageView.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26 /**
27  *  \file 
28  *  \author Eduardo Davila and Claire Mouton.
29  *  \brief Class bbtk::ThresholdImageView. 
30  *  \date September 2012
31  */
32
33 #include "ColorLayerImageView.h"
34
35 #include <vtkImageReslice.h>
36 #include <vtkLookupTable.h>
37 #include <vtkImageData.h>
38
39 #include <algorithm>
40
41
42 //=========================================================================
43 //=========================================================================
44 //=========================================================================
45 //=========================================================================
46 ColorLayerImageView::ColorLayerImageView( )
47 {
48   _z2                                   =       0;
49   _fix_dynamic          =       true;
50   _color_type           =       false;
51 }
52
53 //=========================================================================
54 ColorLayerImageView::~ColorLayerImageView()
55 {
56 }
57 //=========================================================================
58
59
60 //----------------------------------------------------------------------------
61 void ColorLayerImageView::SetSliceFixDynamic(bool fix_dynamic)
62 {
63   _fix_dynamic = fix_dynamic;
64 }
65
66 //----------------------------------------------------------------------------
67 void ColorLayerImageView::SetX2(int x2)  
68 {
69   _x2 = x2;
70 }
71
72 //----------------------------------------------------------------------------
73 void ColorLayerImageView::SetY2(int y2)  
74 {
75   _y2 = y2;
76 }
77
78
79 //----------------------------------------------------------------------------
80 void ColorLayerImageView::SetZ2(int z2)  
81 {
82   _z2 = z2;
83 }
84
85
86
87 //----------------------------------------------------------------------------
88 int ColorLayerImageView::GetX() // virtual 
89 {
90   int result=0;
91   if (_fix_dynamic==false)
92     {
93       result = _x2;
94     }
95   else
96     {
97       result = LayerImageBase::GetX();
98     }  
99
100   return result;
101 }
102
103 //----------------------------------------------------------------------------
104 int ColorLayerImageView::GetY() // virtual 
105 {
106   int result=0;
107   if (_fix_dynamic==false)
108     {
109       result = _y2;
110     }
111   else
112     {
113       result = LayerImageBase::GetY();
114     }  
115
116   return result;
117 }
118
119 //----------------------------------------------------------------------------
120 int ColorLayerImageView::GetZ() // virtual 
121 {
122   int result=0;
123   if (_fix_dynamic==false)
124     {
125       result = _z2;
126     }
127   else
128     {
129       result = LayerImageBase::GetZ();
130     }  
131
132   return result;
133 }
134
135 //----------------------------------------------------------------------------
136 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
137 {
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;
144 }
145
146 //----------------------------------------------------------------------------
147 double ColorLayerImageView::GetBaseColors(unsigned int index)
148 {
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.
152   else
153     {
154       std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
155       return -1.0;
156     }
157 }
158
159 //----------------------------------------------------------------------------
160 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
161 {
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)
164     {
165       sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
166       _grey_level_boundary = grey_level_boundary;
167     }
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;
171 }
172
173 //----------------------------------------------------------------------------
174 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
175 {  
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.
179   else
180     {
181       std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
182       return -1.0;
183     }
184 }
185
186 //----------------------------------------------------------------------------
187 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
188 {
189   _color_type = color_type;
190
191
192 //----------------------------------------------------------------------------
193 int ColorLayerImageView::GetBaseColorNb()
194 {
195   return _base_color.size()/3;
196 }
197
198 //----------------------------------------------------------------------------
199 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
200 {
201   // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
202   _grey_level_boundary.clear();
203
204   // This avoids a potential division by 0 through delta during the lookup table configuration.
205   if (_range[1]==0)
206     {
207       _range[1]=255;
208     }
209           
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]);
213
214   _grey_level_boundary.push_back((double)minTot);
215   
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 ++)
219   {
220                 _grey_level_boundary.push_back((double)minTot + i * delta);
221   }
222 }
223
224 //----------------------------------------------------------------------------
225 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
226 {
227   // CM Sets the default behaviour concerning the lookup table.
228   _grey_level_boundary.clear();
229   _base_color.clear();
230   // Base colors are set to blue, yellow and red.
231   // Blue.
232   _base_color.push_back(0.0);
233   _base_color.push_back(0.0);
234   _base_color.push_back(1.0);
235   // Yellow.
236   _base_color.push_back(1.0);
237   _base_color.push_back(1.0);
238   _base_color.push_back(0.0);
239   // Red.
240   _base_color.push_back(1.0);
241   _base_color.push_back(0.0);
242   _base_color.push_back(0.0);
243
244   // This avoids a potential division by 0 through delta during the lookup table configuration.
245   if (_range[1]==0)
246     {
247       _range[1]=255;
248     }
249           
250   int minTot = floor (_range[0]);
251   int maxTot = ceil (_range[1]);
252           
253   _grey_level_boundary.push_back((double)minTot);
254  
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);
259
260   _grey_level_boundary.push_back((double)maxTot);
261 }
262
263 //----------------------------------------------------------------------------
264 void ColorLayerImageView::ConfigLookupTable()  // virtual
265 {
266   // CM
267   // Grey level extrema retrieved from the image grey level extrema.
268
269
270   GetImage()->GetScalarRange(_range);
271
272
273
274   // ------------------ Setting Default Values
275   // Checks the size consistency of vectors _base_color and _grey_level_boundary.
276   // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
277   if (GetBaseColorNb() == 0)
278     SetDefaultBaseColorAndGreyLevelBoundary();
279
280   // 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   else
282     if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
283       {
284         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;
285         std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
286         SetDefaultGreyLevelBoundary();     
287       }
288   // ------------------ End Of Setting Default Values
289
290
291   // ------------------ Defining the Threshold Table
292   vtkLookupTable* thresholdTable = GetThresholdTable();
293
294   // CM Number of table values consistent with the effective number of values (see loop below).
295   double thresholdTable_range[2];
296   thresholdTable_range[1]= std::max( GetGreyLevelBoundaries( GetBaseColorNb() ), ceil(_range[1]) );
297   thresholdTable_range[0]= std::min( GetGreyLevelBoundaries(0), floor(_range[0]) );
298   thresholdTable->SetNumberOfTableValues(thresholdTable_range[1] - thresholdTable_range[0] + 1);
299   thresholdTable->SetTableRange(thresholdTable_range); 
300   thresholdTable->SetAlphaRange(0, 1);
301   thresholdTable->SetValueRange(0, 1);
302
303
304   thresholdTable->SetSaturationRange(0, 0); 
305   thresholdTable->SetRampToLinear( );
306
307   // Defines the lookup table.
308   // If the image has a degradation in one (or both) extremum (lower or higher),
309   // sets the corresponding grey levels as transparent white in the lookup table.
310   for (int i = floor(_range[0]); i <= GetGreyLevelBoundaries(0); i++)
311     {
312       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
313     }
314   for (int i = GetGreyLevelBoundaries( GetBaseColorNb() ) + 1 ; i <= ceil(_range[1]); i++)
315     {
316       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
317     }
318         
319   int delta;
320
321   // _color_type true, i.e. plain colors.
322   // Sets the plain color for each grey level interval.
323   if (_color_type)
324     {
325       for (int i = 0; i < GetBaseColorNb(); i++)
326         {
327           delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
328           for (int ii = 1; ii <= delta; ii++)
329             {
330               thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
331                                             GetBaseColors(i*3), 
332                                             GetBaseColors(i*3 + 1),
333                                             GetBaseColors(i*3 + 2),
334                                             1);
335             }
336         }
337     } // End Of if (_color_type)
338
339         
340   // _color_type false, i.e. gradient color
341   else
342     {
343       // First color:
344       // Creates a linear range from white to the first color.
345       delta = GetGreyLevelBoundaries(1) - GetGreyLevelBoundaries(0);
346       for (int ii = 1; ii <= delta ; ii++)
347         {
348           thresholdTable->SetTableValue( (GetGreyLevelBoundaries(0) + ii), GetBaseColors(0) * ii/delta,
349                                          GetBaseColors(1) * ii/delta, GetBaseColors(2) * ii/delta, 1);
350         }
351             
352       // If other colors exist:
353       // Creates linear ranges between one color and the following in the color vector.
354       if (GetBaseColorNb() > 1)
355         {
356           for (int i = 1; i < GetBaseColorNb(); i++)
357             {
358               delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
359               for (int ii = 1; ii <= delta; ii++)
360                 {
361                   // Color computation : previous_color + (current_color - previous_color)/delta * ii
362                   thresholdTable->SetTableValue((GetGreyLevelBoundaries(i) + ii),
363                                                 GetBaseColors((i-1)*3) + (GetBaseColors(i*3) - GetBaseColors((i-1)*3)) / delta * ii , 
364                                                 GetBaseColors((i-1)*3 + 1) + (GetBaseColors(i*3 + 1) - GetBaseColors((i-1)*3 + 1)) / delta * ii ,
365                                                 GetBaseColors((i-1)*3 + 2) + (GetBaseColors(i*3 + 2) - GetBaseColors((i-1)*3 + 2)) / delta * ii ,
366                                                 1);
367                 }
368             }
369         }
370     } //  End Of if (!_color_type)
371
372
373         
374    thresholdTable->SetRange(_range[0], _range[1]); // image intensity range
375    thresholdTable->SetValueRange(0.0, 1.0); // from black to white
376
377
378   thresholdTable->Build( );
379
380
381
382 /*
383 // Create a greyscale lookup table
384    vtkLookupTable* thresholdTable = GetThresholdTable();
385    thresholdTable->SetRange(0.5, 0.75); // image intensity range
386    thresholdTable->SetValueRange(0.0, 1); // from black to white
387    thresholdTable->SetHueRange(0.0, 1.0); // from black to white
388    thresholdTable->SetSaturationRange(0.0, 1.0); // no color saturation
389    thresholdTable->SetRampToLinear();
390    thresholdTable->Build();
391 */ 
392
393   // ------------------ End Of Defining the Threshold Table
394
395   //EO CM
396 }
397
398 // EOF
399