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