From 6376d230dd7ca431cc1585026cbaf9a545c257e2 Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Mon, 24 Aug 2009 11:36:04 +0800
Subject: [PATCH] ENH: add public accessor to ListSample of ImageLayer to be
 able to use samples from a different image

---
 Code/Visualization/otbImageLayer.h   | 23 +++++--
 Code/Visualization/otbImageLayer.txx | 89 ++++++++++++++--------------
 2 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/Code/Visualization/otbImageLayer.h b/Code/Visualization/otbImageLayer.h
index f7f5a0afaa..2448e08e98 100644
--- a/Code/Visualization/otbImageLayer.h
+++ b/Code/Visualization/otbImageLayer.h
@@ -179,6 +179,22 @@ public:
   /** Get the pixel location */
   virtual PointType GetPixelLocation(const IndexType & index);
 
+  /** Get the list sample used by the rendering function */
+  virtual ListSamplePointerType GetListSample()
+  {
+//     this->UpdateListSample();//FIXME condition to IsModified
+    return m_ListSample;
+  }
+
+  /** Set the list sample used by the rendering function */
+  virtual void SetListSample(ListSamplePointerType listSample)
+  {
+    m_ListSample = listSample;
+    m_ListSampleProvided = true;
+    m_RenderingFunction->SetListSample(m_ListSample);
+  }
+
+
 protected:
   /** Constructor */
   ImageLayer();
@@ -190,12 +206,6 @@ protected:
   /** Update the histogram */
   virtual void UpdateListSample();
 
-  virtual ListSamplePointerType GetListSample()
-  {
-//     this->UpdateListSample();//FIXME condition to IsModified
-    return m_ListSample;
-  }
-
   /** Update the images */
   virtual void RenderImages();
 
@@ -219,6 +229,7 @@ private:
 
   /** List sample used to compute the histogram by the rendering function*/
   ListSamplePointerType m_ListSample;
+  bool m_ListSampleProvided;//To remember if the list sample was provided manually by the user
 
   /** Rendering function */
   RenderingFunctionPointerType m_RenderingFunction;
diff --git a/Code/Visualization/otbImageLayer.txx b/Code/Visualization/otbImageLayer.txx
index 2f3e5f1c06..6e55f08a53 100644
--- a/Code/Visualization/otbImageLayer.txx
+++ b/Code/Visualization/otbImageLayer.txx
@@ -31,7 +31,7 @@ namespace otb
 
 template <class TImage, class TOutputImage>
 ImageLayer<TImage,TOutputImage>
-::ImageLayer() : m_Quicklook(), m_Image(), m_ListSample(), m_RenderingFunction(),
+::ImageLayer() : m_Quicklook(), m_Image(), m_ListSample(), m_ListSampleProvided(false), m_RenderingFunction(),
                  m_QuicklookRenderingFilter(), m_ExtractRenderingFilter(), m_ScaledExtractRenderingFilter(),
                  m_ExtractFilter(), m_ScaledExtractFilter()
 {
@@ -153,57 +153,60 @@ void
 ImageLayer<TImage,TOutputImage>
 ::UpdateListSample()
 {
-//   otbMsgDevMacro(<<"ImageLayer::UpdateListSample():"<<" ("<<this->GetName()<<")"<< " Entering method");
-  // Declare the source of the histogram
-  ImagePointerType histogramSource;
+  if(!m_ListSampleProvided)
+  {
+  //   otbMsgDevMacro(<<"ImageLayer::UpdateListSample():"<<" ("<<this->GetName()<<")"<< " Entering method");
+    // Declare the source of the histogram
+    ImagePointerType histogramSource;
 
-  // if there is a quicklook, use it for histogram generation
-  if(m_Quicklook.IsNotNull())
-    {
-    histogramSource = m_Quicklook;
-    }
-  else
-    {
-    // Else use the full image (update the data)
-    // REVIEW: Not sure the region is right here. Should be the
-    // largest ?
-    // REPLY: might be... didn't change anything
-    //
-    histogramSource = m_Image;
-    histogramSource->SetRequestedRegion(this->GetExtractRegion());
-    }
+    // if there is a quicklook, use it for histogram generation
+    if(m_Quicklook.IsNotNull())
+      {
+      histogramSource = m_Quicklook;
+      }
+    else
+      {
+      // Else use the full image (update the data)
+      // REVIEW: Not sure the region is right here. Should be the
+      // largest ?
+      // REPLY: might be... didn't change anything
+      //
+      histogramSource = m_Image;
+      histogramSource->SetRequestedRegion(this->GetExtractRegion());
+      }
 
-  // Check if we need to generate the histogram again
-  if( m_ListSample.IsNull() || m_ListSample->Size() == 0 || (histogramSource->GetUpdateMTime() < histogramSource->GetPipelineMTime()) )
-    {
-    otbMsgDevMacro(<<"ImageLayer::UpdateListSample():"<<" ("<<this->GetName()<<")"<< " Regenerating histogram due to pippeline update.");
+    // Check if we need to generate the histogram again
+    if( m_ListSample.IsNull() || m_ListSample->Size() == 0 || (histogramSource->GetUpdateMTime() < histogramSource->GetPipelineMTime()) )
+      {
+      otbMsgDevMacro(<<"ImageLayer::UpdateListSample():"<<" ("<<this->GetName()<<")"<< " Regenerating histogram due to pippeline update.");
 
-    // Update the histogram source
-    histogramSource->Update();
+      // Update the histogram source
+      histogramSource->Update();
 
-    // Iterate on the image
-    itk::ImageRegionConstIterator<ImageType> it(histogramSource,histogramSource->GetBufferedRegion());
+      // Iterate on the image
+      itk::ImageRegionConstIterator<ImageType> it(histogramSource,histogramSource->GetBufferedRegion());
 
-    // declare a list to store the samples
-    m_ListSample->Clear();
+      // declare a list to store the samples
+      m_ListSample->Clear();
 
-    unsigned int sampleSize = VisualizationPixelTraits::PixelSize(it.Get());
-    m_ListSample->SetMeasurementVectorSize(sampleSize);
+      unsigned int sampleSize = VisualizationPixelTraits::PixelSize(it.Get());
+      m_ListSample->SetMeasurementVectorSize(sampleSize);
 
-    // Fill the samples list
-    it.GoToBegin();
-    while(!it.IsAtEnd())
-    {
-      SampleType sample(sampleSize);
-      VisualizationPixelTraits::Convert( it.Get(), sample );
-      m_ListSample->PushBack(sample);
-      ++it;
-    }
-    otbMsgDevMacro(<<"ImageLayer::UpdateListSample()"<<" ("<<this->GetName()<<")"<< " Sample list generated ("<<m_ListSample->Size()<<" samples, "<< sampleSize <<" bands)");
+      // Fill the samples list
+      it.GoToBegin();
+      while(!it.IsAtEnd())
+      {
+        SampleType sample(sampleSize);
+        VisualizationPixelTraits::Convert( it.Get(), sample );
+        m_ListSample->PushBack(sample);
+        ++it;
+      }
+      otbMsgDevMacro(<<"ImageLayer::UpdateListSample()"<<" ("<<this->GetName()<<")"<< " Sample list generated ("<<m_ListSample->Size()<<" samples, "<< sampleSize <<" bands)");
 
-    m_RenderingFunction->SetListSample(m_ListSample);
+      m_RenderingFunction->SetListSample(m_ListSample);
 
-    }
+      }
+  }
 }
 
 
-- 
GitLab