]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageView.cxx
#2651 creaMaracasVisu Feature New Normal - Color Layer: Transparent Base, Refresh
[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 int ColorLayerImageView::GetX() // virtual 
87 {
88   int result=0;
89   if (_fix_dynamic==false)
90     {
91       result = _x2;
92     }
93   else
94     {
95       result = LayerImageBase::GetX();
96     }  
97
98   return result;
99 }
100
101 //----------------------------------------------------------------------------
102 int ColorLayerImageView::GetY() // virtual 
103 {
104   int result=0;
105   if (_fix_dynamic==false)
106     {
107       result = _y2;
108     }
109   else
110     {
111       result = LayerImageBase::GetY();
112     }  
113
114   return result;
115 }
116
117 //----------------------------------------------------------------------------
118 int ColorLayerImageView::GetZ() // virtual 
119 {
120   int result=0;
121   if (_fix_dynamic==false)
122     {
123       result = _z2;
124     }
125   else
126     {
127       result = LayerImageBase::GetZ();
128     }  
129
130   return result;
131 }
132
133 //----------------------------------------------------------------------------
134 void ColorLayerImageView::SetBaseColors(std::vector<double> & base_color)
135 {
136   // The base color vector must be of a size multiple of 3, not null.
137   if (base_color.size() != 0 && base_color.size() % 3 == 0)
138     _base_color = base_color;
139   // Otherwise, an exception should be thrown.
140   else if (base_color.size() != 0)
141     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;
142 }
143
144
145 //----------------------------------------------------------------------------
146 void ColorLayerImageView::SetBaseTransparence(std::vector<double> & base_transparence)
147 {
148   _transparence_level_boundary = base_transparence;
149 }
150
151
152 //----------------------------------------------------------------------------
153 double ColorLayerImageView::GetBaseColors(unsigned int index)
154 {
155   if (_base_color.size() > index)
156     return _base_color.at(index);
157   // An exception should be thrown if the index does not exist in the color vector.
158   else
159     {
160       std::cout << "CM ColorLayerImageView::GetBaseColors : ERROR!!! The index " << index << "is out of the base color vector range." << std ::endl;
161       return -1.0;
162     }
163 }
164
165 //----------------------------------------------------------------------------
166 void ColorLayerImageView::SetGreyLevelBoundaries(std::vector<double> & grey_level_boundary)
167 {
168   // The size must be greater than or equal to 2 (at least min and max must exist).
169   if ( grey_level_boundary.size() >= 2)
170     {
171       sort ( grey_level_boundary.begin(), grey_level_boundary.end() );
172       _grey_level_boundary = grey_level_boundary;
173     }
174   // Otherwise, an exception should be thrown.
175   else if (grey_level_boundary.size() != 0)
176     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;
177 }
178
179 //----------------------------------------------------------------------------
180 double ColorLayerImageView::GetGreyLevelBoundaries(unsigned int index)
181 {  
182   if (_grey_level_boundary.size() > index)
183     return _grey_level_boundary.at(index);
184   // An exception should be thrown if the index does not exist in the grey level boundary vector.
185   else
186     {
187       std::cout << "CM ColorLayerImageView::GetGreyLevelBoundaries : ERROR!!! The index " << index << "is out of the grey level boundaries vector range." << std ::endl;
188       return -1.0;
189     }
190 }
191
192 //----------------------------------------------------------------------------
193 void ColorLayerImageView::SetPlainOrGradientColor(bool color_type)
194 {
195   _color_type = color_type;
196
197
198 //----------------------------------------------------------------------------
199 int ColorLayerImageView::GetBaseColorNb()
200 {
201   return _base_color.size()/3;
202 }
203
204 //----------------------------------------------------------------------------
205 void ColorLayerImageView::SetDefaultGreyLevelBoundary()
206 {
207   // CM Sets the default behaviour concerning the lookup table keeping the base colors already set.
208   _grey_level_boundary.clear();
209
210   // This avoids a potential division by 0 through delta during the lookup table configuration.
211   if (_range[1]==0)
212     {
213       _range[1]=255;
214     }
215           
216         int i;
217         double delta;
218   // Grey level extrema are set by default to the image grey level extrema.
219   double minTot = _range[0];
220   double maxTot = _range[1];
221
222         if (_color_type)
223         {
224            delta = (maxTot - minTot)/GetBaseColorNb();
225                 // By default, the histogram is split into BaseColorNb areas of equal width.
226                 for (i = 0; i <= GetBaseColorNb() ; i ++)
227                 {
228                         _grey_level_boundary.push_back(minTot + i * delta);
229                 } // for
230         } else {
231            delta = (maxTot - minTot)/(GetBaseColorNb()-1);
232                 for (i = 0; i < GetBaseColorNb() ; i ++)
233                 {
234                         _grey_level_boundary.push_back(minTot + i * delta);
235                 } // for                
236         } // if  
237
238 }
239
240 //----------------------------------------------------------------------------
241 void ColorLayerImageView::SetDefaultBaseColorAndGreyLevelBoundary()
242 {
243   // CM Sets the default behaviour concerning the lookup table.
244   _grey_level_boundary.clear();
245   _base_color.clear();
246   // Base colors are set to blue, yellow and red.
247   // Blue.
248   _base_color.push_back(0.0);
249   _base_color.push_back(0.0);
250   _base_color.push_back(0.5);
251
252   _base_color.push_back(0.0);
253   _base_color.push_back(0.0);
254   _base_color.push_back(1.0);
255
256   _base_color.push_back(0.0);
257   _base_color.push_back(1.0);
258   _base_color.push_back(1.0);
259
260   _base_color.push_back(1.0);
261   _base_color.push_back(1.0);
262   _base_color.push_back(0.0);
263
264   _base_color.push_back(1.0);
265   _base_color.push_back(0.0);
266   _base_color.push_back(0.0);
267
268   _base_color.push_back(0.5);
269   _base_color.push_back(0.0);
270   _base_color.push_back(0.0);
271
272
273   // This avoids a potential division by 0 through delta during the lookup table configuration.
274   if (_range[1]==0)
275     {
276       _range[1]=255;
277     }
278           
279 //  double minTot = floor (_range[0]);
280 //  double maxTot = ceil (_range[1]);
281   double minTot = _range[0];
282   double maxTot = _range[1];
283           
284   double delta = (maxTot - minTot)/8.0;
285   // By default, the histogram is split into three areas of equal width.
286
287   _grey_level_boundary.push_back(minTot + delta*0);   
288   _grey_level_boundary.push_back(minTot + delta*1);
289 //  _grey_level_boundary.push_back(minTot + delta*2);
290   _grey_level_boundary.push_back(minTot + delta*3);
291 //  _grey_level_boundary.push_back(minTot + delta*4);
292   _grey_level_boundary.push_back(minTot + delta*5);
293  // _grey_level_boundary.push_back(minTot + delta*6);
294   _grey_level_boundary.push_back(minTot + delta*7);
295   _grey_level_boundary.push_back(minTot + delta*8);
296 }
297
298 //----------------------------------------------------------------------------
299 void ColorLayerImageView::ConfigLookupTable()  // virtual
300 {
301   // CM 2014
302   // EED         28/01/2015 
303         
304   // Grey level extrema retrieved from the image grey level extrema.
305
306   GetImage()->GetScalarRange(_range);
307   double minRange = _range[0];
308   double maxRange = _range[1];
309
310   // ------------------ Setting Default Values
311   // Checks the size consistency of vectors _base_color and _grey_level_boundary.
312   // In case of inconsistency, an exception should be thrown. Instead, the default values are set.
313   if (GetBaseColorNb() == 0)
314         {
315                 SetDefaultBaseColorAndGreyLevelBoundary();
316         }
317    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.
318       if (GetBaseColorNb() != (int)_grey_level_boundary.size() )
319       {
320                         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;
321                         std::cout << "CM The default values for the base colors and the grey level boundaries will be set." << std::endl;
322                         SetDefaultGreyLevelBoundary();     
323       }
324    }
325   // ------------------ End Of Setting Default Values
326
327         int iTransparence,transparenceSize = _transparence_level_boundary.size();
328         for (iTransparence=transparenceSize ; iTransparence < GetBaseColorNb(); iTransparence++ )
329         {
330       _transparence_level_boundary.push_back(1);
331         }
332
333   // ------------------ Defining the Threshold Table
334   vtkLookupTable* thresholdTable = GetThresholdTable();
335   double thresholdTable_range[2];
336   double r1,r2,g1,g2,b1,b2,t1,t2;
337   int start,end;
338
339   // CM Number of table values consistent with the effective number of values (see loop below).
340   thresholdTable_range[1]= std::max( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) , ceil(maxRange) );
341   thresholdTable_range[0]= std::min( GetGreyLevelBoundaries(0), floor(minRange) );
342
343   thresholdTable->SetTableRange(thresholdTable_range); 
344   thresholdTable->SetAlphaRange( 0,1 );
345   thresholdTable->SetValueRange( 0,1 );
346
347   thresholdTable->SetSaturationRange( 0,0 ); 
348   thresholdTable->SetRampToLinear( );
349
350   maxColorsThresholdTable = 1000;
351   thresholdTable->SetNumberOfTableValues( maxColorsThresholdTable );
352
353
354   // Defines the lookup table.
355   // If the image has a degradation in one (or both) extremum (lower or higher),
356   // sets the corresponding grey levels as transparent white in the lookup table.
357
358   // _color_type true, i.e. plain colors.
359   // Sets the plain color for each grey level interval.
360   if (_color_type)  // Plain
361   {
362      if ( minRange<GetGreyLevelBoundaries(0) )
363           {
364                         start = 0;      
365                         end     = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(0) );  
366                         FillColorTable(start,end, 0,0,0, 0,0,0, 0,0);
367      }
368
369      if ( maxRange>GetGreyLevelBoundaries(  _grey_level_boundary.size()-1   ) )
370           {
371                         start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries( _grey_level_boundary.size()-1 ) );  
372                         end     = GrayLevel_TO_colorTableIndex( maxRange );  
373                         FillColorTable(start,end, 0,0,0, 0,0,0, 0,0);
374      }
375       for (int iColor = 0; iColor < GetBaseColorNb(); iColor++)
376                 {
377                         r1              = GetBaseColors(iColor*3+0);
378                         g1              = GetBaseColors(iColor*3+1);
379                         b1              = GetBaseColors(iColor*3+2);
380                         start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );  
381                         end     = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );  
382          t1     = _transparence_level_boundary[iColor];
383 //                      t2              = _transparence_level_boundary[iColor+1];
384                         FillColorTable(start,end, r1,g1,b1,r1,g1,b1,t1,t1);
385                 } // for i
386         }    else  {  // _color_type false, i.e. gradient color
387       if (GetBaseColorNb() > 1)
388                 {
389                         for (int iColor = 0; iColor < GetBaseColorNb()-1; iColor++)
390                    {
391                                 r1              = GetBaseColors( (iColor+0)*3+0 );
392                                 g1              = GetBaseColors( (iColor+0)*3+1 );
393                                 b1              = GetBaseColors( (iColor+0)*3+2 );
394                                 r2              = GetBaseColors( (iColor+1)*3+0);
395                                 g2              = GetBaseColors( (iColor+1)*3+1);
396                                 b2              = GetBaseColors( (iColor+1)*3+2);
397                                 start = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor) );  
398                                 end     = GrayLevel_TO_colorTableIndex( GetGreyLevelBoundaries(iColor+1) );  
399                  t1     = _transparence_level_boundary[iColor];
400                                 t2              = _transparence_level_boundary[iColor+1];
401                                 FillColorTable( start,end, r1,g1,b1, r2,g2,b2, t1,t2 );
402                         }// for 
403                 } //if                          
404   } //  End Of if (!_color_type)
405
406   double minRangeForColorBar, maxRangeForColorBar;
407   GetRangeForColorBar( minRangeForColorBar, maxRangeForColorBar );
408   thresholdTable->SetRange( minRangeForColorBar, maxRangeForColorBar );
409
410 //  thresholdTable->SetTableRange( minRange, maxRange );
411 //  thresholdTable->SetValueRange( 0.0, 1.0 );
412 //  thresholdTable->SetAboveRangeColor(0,0,0,0);        
413 //  thresholdTable->SetBelowRangeColor(0,0,0,0);        
414
415   thresholdTable->Build( );
416
417   //EO CM EED
418 }
419
420 //----------------------------------------------------------------------------
421 int ColorLayerImageView::GrayLevel_TO_colorTableIndex( double VALUE )
422 {
423   GetImage()->Update();
424   GetImage()->GetScalarRange(_range);
425   double minRange = _range[0];
426   double maxRange = _range[1];
427   return  maxColorsThresholdTable * (VALUE-minRange) / (maxRange-minRange);     
428 }
429
430 //----------------------------------------------------------------------------
431 void ColorLayerImageView::FillColorTable(int start, int end, double r1, double g1, double b1, double r2, double g2, double b2, double t1, double t2)
432 {
433    vtkLookupTable* thresholdTable = GetThresholdTable();
434         int      iTable;
435         double delta    = end-start;
436         double dr               = (r2-r1)/delta;
437         double dg               = (g2-g1)/delta;
438         double db               = (b2-b1)/delta;
439    double dt      = (t2-t1)/delta;
440         for (iTable=0; iTable<=delta; iTable++)
441         {
442                 thresholdTable->SetTableValue(  iTable+start , r1+dr*iTable, g1+dg*iTable, b1+db*iTable,t1+dt*iTable);
443         } // for iTable
444
445
446 }
447
448 // EOF
449