]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageView.cxx
Support #1768 CREATIS Licence insertion
[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::SetZ2(int z2)  
68 {
69   _z2 = z2;
70 }
71
72 //----------------------------------------------------------------------------
73 int ColorLayerImageView::GetZ() // virtual 
74 {
75   int result=0;
76   if (_fix_dynamic==false)
77     {
78       result = _z2;
79     }
80   else
81     {
82       result = LayerImageBase::GetZ();
83     }  
84
85   return result;
86 }
87
88 //----------------------------------------------------------------------------
89 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
90 {
91   // The base color vector must be of a size multiple of 3, not null.
92   if (base_color.size() != 0 && base_color.size() % 3 == 0)
93     _base_color = base_color;
94   // Otherwise, an exception should be thrown.
95   else if (base_color.size() != 0)
96     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;
97 }
98
99 //----------------------------------------------------------------------------
100 double ColorLayerImageView::GetBaseColors(unsigned int index)
101 {
102   if (_base_color.size() > index)
103     return _base_color.at(index);
104   // An exception should be thrown if the index does not exist in the color vector.
105   else
106     {
107       std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
108       return -1.0;
109     }
110 }
111
112 //----------------------------------------------------------------------------
113 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
114 {
115   // The size must be greater than or equal to 2 (at least min and max must exist).
116   if ( grey_level_boundary.size() >= 2)
117     {
118       sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
119       _grey_level_boundary = grey_level_boundary;
120     }
121   // Otherwise, an exception should be thrown.
122   else if (grey_level_boundary.size() != 0)
123     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;
124 }
125
126 //----------------------------------------------------------------------------
127 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
128 {  
129   if (_grey_level_boundary.size() > index)
130     return _grey_level_boundary.at(index);
131   // An exception should be thrown if the index does not exist in the grey level boundary vector.
132   else
133     {
134       std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
135       return -1.0;
136     }
137 }
138
139 //----------------------------------------------------------------------------
140 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
141 {
142   _color_type = color_type;
143
144
145 //----------------------------------------------------------------------------
146 int ColorLayerImageView::GetBaseColorNb()
147 {
148   return _base_color.size()/3;
149 }
150
151 //----------------------------------------------------------------------------
152 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
153 {
154   // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
155   _grey_level_boundary.clear();
156
157   // This avoids a potential division by 0 through delta during the lookup table configuration.
158   if (_range[1]==0)
159     {
160       _range[1]=255;
161     }
162           
163   // Grey level extrema are set by default to the image grey level extrema.
164   int minTot = floor (_range[0]);
165   int maxTot = ceil (_range[1]);
166
167   _grey_level_boundary.push_back((double)minTot);
168   
169   // By default, the histogram is split into BaseColorNb areas of equal width.
170   double delta = (maxTot - minTot)/GetBaseColorNb();
171   for (int i = 1; i <= GetBaseColorNb() ; i ++)
172     {
173       _grey_level_boundary.push_back((double)minTot + i * delta);
174     }
175 }
176
177 //----------------------------------------------------------------------------
178 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
179 {
180   // CM Sets the default behaviour concerning the lookup table.
181   _grey_level_boundary.clear();
182   _base_color.clear();
183   // Base colors are set to blue, yellow and red.
184   // Blue.
185   _base_color.push_back(0.0);
186   _base_color.push_back(0.0);
187   _base_color.push_back(1.0);
188   // Yellow.
189   _base_color.push_back(1.0);
190   _base_color.push_back(1.0);
191   _base_color.push_back(0.0);
192   // Red.
193   _base_color.push_back(1.0);
194   _base_color.push_back(0.0);
195   _base_color.push_back(0.0);
196
197   // This avoids a potential division by 0 through delta during the lookup table configuration.
198   if (_range[1]==0)
199     {
200       _range[1]=255;
201     }
202           
203   int minTot = floor (_range[0]);
204   int maxTot = ceil (_range[1]);
205           
206   _grey_level_boundary.push_back((double)minTot);
207  
208   // By default, the histogram is split into three areas of equal width.
209   double delta = (maxTot - minTot)/3.0;
210   _grey_level_boundary.push_back(minTot + delta);
211   _grey_level_boundary.push_back(minTot + 2*delta);
212
213   _grey_level_boundary.push_back((double)maxTot);
214 }
215
216 //----------------------------------------------------------------------------
217 void ColorLayerImageView::ConfigLookupTable()  // virtual
218 {
219   // CM
220   // Grey level extrema retrieved from the image grey level extrema.
221   GetImage()->GetScalarRange(_range);
222
223
224   // ------------------ Setting Default Values
225   // Checks the size consistency of vectors _base_color and _grey_level_boundary.
226   // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
227   if (GetBaseColorNb() == 0)
228     SetDefaultBaseColorAndGreyLevelBoundary();
229
230   // 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.
231   else
232     if (GetBaseColorNb() != (_grey_level_boundary.size() - 1) )
233       {
234         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;
235         std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
236         SetDefaultGreyLevelBoundary();     
237       }
238   // ------------------ End Of Setting Default Values
239
240
241   // ------------------ Defining the Threshold Table
242   vtkLookupTable* thresholdTable = GetThresholdTable();
243
244   // CM Number of table values consistent with the effective number of values (see loop below).
245   thresholdTable->SetNumberOfTableValues(std::max( GetGreyLevelBoundaries( GetBaseColorNb() ), ceil(_range[1]) ) - std::min(  GetGreyLevelBoundaries(0), floor(_range[0]) ) + 1);
246   thresholdTable->SetTableRange(_range); 
247   thresholdTable->SetAlphaRange(0, 1);
248   thresholdTable->SetValueRange(0, 1);
249   thresholdTable->SetSaturationRange(0, 0); 
250   thresholdTable->SetRampToLinear( );
251
252   // Defines the lookup table.
253   // If the image has a degradation in one (or both) extremum (lower or higher),
254   // sets the corresponding grey levels as transparent white in the lookup table.
255   for (int i = floor(_range[0]); i <= GetGreyLevelBoundaries(0); i++)
256     {
257       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
258     }
259   for (int i = GetGreyLevelBoundaries( GetBaseColorNb() ) + 1 ; i <= ceil(_range[1]); i++)
260     {
261       thresholdTable -> SetTableValue( i, 0, 0, 0, 0);  
262     }
263         
264   int delta;
265
266   // _color_type true, i.e. plain colors.
267   // Sets the plain color for each grey level interval.
268   if (_color_type)
269     {
270       for (int i = 0; i < GetBaseColorNb(); i++)
271         {
272           delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
273           for (int ii = 1; ii <= delta; ii++)
274             {
275               thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
276                                             GetBaseColors(i*3), 
277                                             GetBaseColors(i*3 + 1),
278                                             GetBaseColors(i*3 + 2),
279                                             1);
280             }
281         }
282     } // End Of if (_color_type)
283
284         
285   // _color_type false, i.e. gradient color
286   else
287     {
288       // First color:
289       // Creates a linear range from white to the first color.
290       delta = GetGreyLevelBoundaries(1) - GetGreyLevelBoundaries(0);
291       for (int ii = 1; ii <= delta ; ii++)
292         {
293           thresholdTable->SetTableValue( GetGreyLevelBoundaries(0) + ii, GetBaseColors(0) * ii/delta,
294                                          GetBaseColors(1) * ii/delta, GetBaseColors(2) * ii/delta, 1);
295         }
296             
297       // If other colors exist:
298       // Creates linear ranges between one color and the following in the color vector.
299       if (GetBaseColorNb() > 1)
300         {
301           for (int i = 1; i < GetBaseColorNb(); i++)
302             {
303               delta = GetGreyLevelBoundaries(i+1) - GetGreyLevelBoundaries(i);
304               for (int ii = 1; ii <= delta; ii++)
305                 {
306                   // Color computation : previous_color + (current_color - previous_color)/delta * ii
307                   thresholdTable->SetTableValue(GetGreyLevelBoundaries(i) + ii,
308                                                 GetBaseColors((i-1)*3) + (GetBaseColors(i*3) - GetBaseColors((i-1)*3)) / delta * ii , 
309                                                 GetBaseColors((i-1)*3 + 1) + (GetBaseColors(i*3 + 1) - GetBaseColors((i-1)*3 + 1)) / delta * ii ,
310                                                 GetBaseColors((i-1)*3 + 2) + (GetBaseColors(i*3 + 2) - GetBaseColors((i-1)*3 + 2)) / delta * ii ,
311                                                 1);
312                 }
313             }
314         }
315     } //  End Of if (!_color_type)
316         
317   thresholdTable->Build( );
318   // ------------------ End Of Defining the Threshold Table
319
320   //EO CM
321 }
322
323 // EOF
324