From 764d8968ade03a09f407afa5e367a218e32852aa Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@c-s.fr>
Date: Wed, 14 Feb 2007 13:08:46 +0000
Subject: [PATCH] - Correction quicklook avec nouveau
 StreamingShrinkImageFilter - Correction StreamingImageFilter - Correction
 segfault ICD

---
 .../otbStreamingShrinkImageFilter.h           | 16 +--
 .../otbStreamingShrinkImageFilter.txx         | 98 ++++++++++++-------
 Code/Visu/otbImageWidgetBase.txx              | 22 ++++-
 .../otbStreamingShrinkImageFilter.cxx         |  2 +-
 .../otbStreamingShrinkImageFilterNew.cxx      |  2 +-
 5 files changed, 92 insertions(+), 48 deletions(-)

diff --git a/Code/BasicFilters/otbStreamingShrinkImageFilter.h b/Code/BasicFilters/otbStreamingShrinkImageFilter.h
index 75e5605415..3513d088df 100644
--- a/Code/BasicFilters/otbStreamingShrinkImageFilter.h
+++ b/Code/BasicFilters/otbStreamingShrinkImageFilter.h
@@ -36,14 +36,14 @@ namespace otb
    * For exemple, with a 6000X6000 image and a 10 shrinkFactor, it will read 600 lines of 5990 pixels 
    * instead of the whole image.
    */
-template <class TImage> 
+template <class TInputImage,class TOutputImage> 
 class ITK_EXPORT StreamingShrinkImageFilter
-: public itk::ImageToImageFilter<TImage,TImage>
+: public itk::ImageToImageFilter<TInputImage,TOutputImage>
 {
  public:
   /** Standard typedefs */
   typedef StreamingShrinkImageFilter               Self;
-  typedef itk::ImageToImageFilter<TImage,TImage>   Superclass;
+  typedef itk::ImageToImageFilter<TInputImage,TOutputImage>   Superclass;
   typedef itk::SmartPointer<Self>                  Pointer;
   typedef itk::SmartPointer<const Self>            ConstPointer;
   
@@ -54,8 +54,10 @@ class ITK_EXPORT StreamingShrinkImageFilter
   itkTypeMacro(StreamingShrinkImageFilter,ImageToImageFilter);
   
   /** Template parameter typedefs */
-  typedef TImage ImageType;
-  typedef typename ImageType::Pointer ImagePointerType;
+  typedef TInputImage InputImageType;
+  typedef typename InputImageType::Pointer InputImagePointerType;
+  typedef TOutputImage OutputImageType;
+  typedef typename OutputImageType::Pointer OutputImagePointerType;
   
   /** Shrink factor accessor */
   itkSetMacro(ShrinkFactor,unsigned int);
@@ -66,7 +68,9 @@ class ITK_EXPORT StreamingShrinkImageFilter
    * As such, it must override the GenerateOutputInformation method in order to compute
    * the output size from the input size.
    */
-  void GenerateOutputInformation(void);
+  virtual void GenerateOutputInformation(void);
+
+  virtual void GenerateInputRequestedRegion(void);
   /** Main computation method */
   virtual void UpdateOutputData(itk::DataObject * output);
   
diff --git a/Code/BasicFilters/otbStreamingShrinkImageFilter.txx b/Code/BasicFilters/otbStreamingShrinkImageFilter.txx
index 888a7ca77d..37732ef4fc 100644
--- a/Code/BasicFilters/otbStreamingShrinkImageFilter.txx
+++ b/Code/BasicFilters/otbStreamingShrinkImageFilter.txx
@@ -25,26 +25,26 @@
 namespace otb
 {
 /** Constructor */
-template <class TImage>
-StreamingShrinkImageFilter<TImage>
+template <class TInputImage, class TOutputImage>
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
 ::StreamingShrinkImageFilter()
 {
   // Default shrink factor
   m_ShrinkFactor=10;
 }
 /** Destructor */
-template <class TImage>
-StreamingShrinkImageFilter<TImage>
+template <class TInputImage, class TOutputImage>
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
 ::~StreamingShrinkImageFilter()
 {}
 /** 
- * StreamingShrinkImageFilter produces an ouptut whose size is different from its input.
+ * StreamingShrinkImageFilter produces an output whose size is different from its input.
  * As such, it must override the GenerateOutputInformation method in order to compute
  * the output size from the input size.
  */
-template <class TImage>
+template <class TInputImage, class TOutputImage>
 void
-StreamingShrinkImageFilter<TImage>
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
 ::GenerateOutputInformation(void)
 {
   // call the superclass' implementation of this method
@@ -56,39 +56,61 @@ StreamingShrinkImageFilter<TImage>
 
   // we need to compute the output spacing, the output image size, and the
   // output image start index
-  const typename ImageType::SpacingType&
+  const typename InputImageType::SpacingType&
     inputSpacing = inputPtr->GetSpacing();
-  const typename ImageType::SizeType&   inputSize
+  const typename InputImageType::SizeType&   inputSize
     = inputPtr->GetLargestPossibleRegion().GetSize();
-  const typename ImageType::IndexType&  inputStartIndex
+  const typename InputImageType::IndexType&  inputStartIndex
     = inputPtr->GetLargestPossibleRegion().GetIndex();
-  otbMsgDebugMacro(<<"Input idnex "<<inputStartIndex);
+  otbMsgDebugMacro(<<"Input index "<<inputStartIndex);
   otbMsgDebugMacro(<<"Input size: "<<inputSize);
 
-  typename ImageType::SpacingType      outputSpacing;
-  typename ImageType::SizeType         outputSize;
-  typename ImageType::IndexType        outputStartIndex;
+  typename OutputImageType::SpacingType      outputSpacing;
+  typename OutputImageType::SizeType         outputSize;
+  typename OutputImageType::IndexType        outputStartIndex;
 
-  for (unsigned int i = 0; i < ImageType::ImageDimension; i++)
+  for (unsigned int i = 0; i < OutputImageType::ImageDimension; i++)
     {
       outputSpacing[i] = inputSpacing[i] * static_cast<double>( m_ShrinkFactor);
       outputSize[i] = inputSize[i]/m_ShrinkFactor;
-      outputStartIndex[i] = inputStartIndex[i];
+      //outputStartIndex[i] = inputStartIndex[i];
+      outputStartIndex[i]=0;
     }
   outputPtr->SetSpacing( outputSpacing );
-  typename ImageType::RegionType outputLargestPossibleRegion;
+  typename OutputImageType::RegionType outputLargestPossibleRegion;
   outputLargestPossibleRegion.SetSize( outputSize );
   outputLargestPossibleRegion.SetIndex( outputStartIndex );
-  otbMsgDebugMacro(<<"Output largest possible region: "<<outputLargestPossibleRegion);
+  // otbMsgDebugMacro(<<"Output largest possible region: "<<outputLargestPossibleRegion);
   outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
 }
 
+template <class TInputImage, class TOutputImage>
+void
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
+::GenerateInputRequestedRegion(void)
+{
+  // otbMsgDebugMacro(<<"Call to GenerateInputRequestedRegion");
+  // if(this->GetInput())
+//     {
+      InputImagePointerType inputPtr =  const_cast<InputImageType * >( this->GetInput(0) );
+      // otbMsgDebugMacro(<<"Input largest possible region: "<<inputPtr->GetLargestPossibleRegion());
+      typename InputImageType::IndexType index = inputPtr->GetLargestPossibleRegion().GetIndex();
+      typename InputImageType::SizeType size;
+      size.Fill(0);
+      typename InputImageType::RegionType region;
+      region.SetSize(size);
+      region.SetIndex(index);
+      inputPtr->SetRequestedRegion(region);
+//     }
+}
+
 /** Main computation method */
-template <class TImage>
+template <class TInputImage, class TOutputImage>
 void
-StreamingShrinkImageFilter<TImage>
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
 ::UpdateOutputData(itk::DataObject *itkNotUsed(output))
 {
+  // otbMsgDebugMacro(<<"Entering UpdateOutputData");
   /**
    * prevent chasing our tail
    */
@@ -120,38 +142,44 @@ StreamingShrinkImageFilter<TImage>
   /**
    * Allocate the output buffer. 
    */
-  ImagePointerType outputPtr = this->GetOutput(0);
-  typename ImageType::RegionType outputRegion = outputPtr->GetRequestedRegion();
+  OutputImagePointerType outputPtr = this->GetOutput(0);
+  typename OutputImageType::RegionType outputRegion = outputPtr->GetRequestedRegion();
   outputPtr->SetBufferedRegion( outputRegion );
   outputPtr->Allocate();
   
   /**
    * Grab the input
    */
-  ImagePointerType inputPtr =  const_cast<ImageType * >( this->GetInput(0) );
+  InputImagePointerType inputPtr =  const_cast<InputImageType * >( this->GetInput(0) );
   
-  typedef itk::ImageRegionIteratorWithIndex<ImageType> IteratorType;
+  // otbMsgDebugMacro(<<"Input Largest possible region: "<<inputPtr->GetLargestPossibleRegion());
+  typename InputImageType::IndexType origin = inputPtr->GetLargestPossibleRegion().GetIndex();
+
+  typedef itk::ImageRegionIteratorWithIndex<OutputImageType> OutputIteratorType;
+  typedef itk::ImageRegionIteratorWithIndex<InputImageType> InputIteratorType;
 
-  IteratorType it(outputPtr,outputRegion);
+  OutputIteratorType it(outputPtr,outputRegion);
   it.GoToBegin();
   
-  typename ImageType::SizeType size = outputRegion.GetSize();
+  typename OutputImageType::SizeType size = outputRegion.GetSize();
 
   for(unsigned int i=0;i<size[1]&&!it.IsAtEnd();++i)
 	{
-	  typename ImageType::IndexType readIndex;
-	  readIndex[0] = 0;
-	  readIndex[1] = i*m_ShrinkFactor;
-	  typename ImageType::SizeType readSize;
+	  typename InputImageType::IndexType readIndex;
+	  readIndex[0] = origin[0];
+	  readIndex[1] = i*m_ShrinkFactor+origin[1];
+	  typename InputImageType::SizeType readSize;
 	  readSize[0]=size[0]*m_ShrinkFactor;
 	  readSize[1]=1;
-	  typename ImageType::RegionType readRegion;
+	  typename InputImageType::RegionType readRegion;
 	  readRegion.SetSize(readSize);
 	  readRegion.SetIndex(readIndex);
+	  // otbMsgDebugMacro(<<"Read region: "<<readRegion);
+//  	  otbMsgDebugMacro(<<"Largest input region: "<<inputPtr->GetLargestPossibleRegion());
 	  inputPtr->SetRequestedRegion(readRegion);
 	  inputPtr->PropagateRequestedRegion();
 	  inputPtr->UpdateOutputData();
-	  IteratorType readIt(inputPtr,readRegion);
+	  InputIteratorType readIt(inputPtr,readRegion);
 	  unsigned int count=0;
 	  for(readIt.GoToBegin();!readIt.IsAtEnd()&&!it.IsAtEnd();++readIt,++count)
 	    {
@@ -191,13 +219,13 @@ StreamingShrinkImageFilter<TImage>
   this->m_Updating = false;
 }
 /** PrintSelf method */
-template <class TImage>
+template <class TInputImage, class TOutputImage>
 void
-StreamingShrinkImageFilter<TImage>
+StreamingShrinkImageFilter<TInputImage, TOutputImage>
 ::PrintSelf(std::ostream& os, itk::Indent indent) const
 {
   Superclass::PrintSelf(os,indent);
-  os << indent << "Shrink facotr: " << m_ShrinkFactor
+  os << indent << "Shrink factor: " << m_ShrinkFactor
      << std::endl;
 }
 } // End namespace otb
diff --git a/Code/Visu/otbImageWidgetBase.txx b/Code/Visu/otbImageWidgetBase.txx
index 5fce2c23c4..ef621c3a65 100644
--- a/Code/Visu/otbImageWidgetBase.txx
+++ b/Code/Visu/otbImageWidgetBase.txx
@@ -56,8 +56,10 @@ ImageWidgetBase<TPixel>
   m_ImageOverlay = NULL;
   m_OpenGlImageOverlayBuffer = NULL;
   m_ImageOverlayOpacity = 128;
+  m_MinComponentValues.SetSize(1);
+  m_MaxComponentValues.SetSize(1);
   m_MinComponentValues.Fill(0);
-  m_MaxComponentValues.Fill(256);
+  m_MaxComponentValues.Fill(255);
 }
 /**
  * Destructor.
@@ -264,19 +266,29 @@ unsigned char
 ImageWidgetBase<TPixel>
 ::Normalize(PixelType value, unsigned int channelIndex)
 {
-  if(value>m_MaxComponentValues[channelIndex])
+  PixelType max = 255;
+  PixelType min = 0;
+  if(channelIndex<m_MaxComponentValues.GetSize())
+    {
+      max = m_MaxComponentValues[channelIndex];
+    }
+   if(channelIndex<m_MinComponentValues.GetSize())
+    {
+      min = m_MinComponentValues[channelIndex];
+    }
+  if(value>max)
     {
       return 255;
     }
 
-  else if(value<m_MinComponentValues[channelIndex])
+  else if(value<min)
     {
       return 0;
     }
   else
     {
-      return static_cast<unsigned char>(255*static_cast<double>(value-m_MinComponentValues[channelIndex])
-      /static_cast<double>(m_MaxComponentValues[channelIndex]-m_MinComponentValues[channelIndex]));
+      return static_cast<unsigned char>(255*static_cast<double>(value-min)
+      /static_cast<double>(max-min));
     }
 }
 
diff --git a/Testing/Code/BasicFilters/otbStreamingShrinkImageFilter.cxx b/Testing/Code/BasicFilters/otbStreamingShrinkImageFilter.cxx
index dbb3fef3f8..47fe07245f 100644
--- a/Testing/Code/BasicFilters/otbStreamingShrinkImageFilter.cxx
+++ b/Testing/Code/BasicFilters/otbStreamingShrinkImageFilter.cxx
@@ -34,7 +34,7 @@ int otbStreamingShrinkImageFilter( int argc, char * argv[] )
       typedef otb::VectorImage<PixelType,Dimension> ImageType;
       typedef otb::ImageFileReader<ImageType> ReaderType;
       typedef otb::ImageFileWriter<ImageType> WriterType;
-      typedef otb::StreamingShrinkImageFilter<ImageType> ShrinkType;
+      typedef otb::StreamingShrinkImageFilter<ImageType,ImageType> ShrinkType;
 
       ReaderType::Pointer reader = ReaderType::New();
       ShrinkType::Pointer shrink = ShrinkType::New();
diff --git a/Testing/Code/BasicFilters/otbStreamingShrinkImageFilterNew.cxx b/Testing/Code/BasicFilters/otbStreamingShrinkImageFilterNew.cxx
index 66c6f9bcd1..e4bc5afa64 100644
--- a/Testing/Code/BasicFilters/otbStreamingShrinkImageFilterNew.cxx
+++ b/Testing/Code/BasicFilters/otbStreamingShrinkImageFilterNew.cxx
@@ -27,7 +27,7 @@ int otbStreamingShrinkImageFilterNew( int argc, char * argv[] )
 
       typedef unsigned char PixelType;
       typedef otb::VectorImage<PixelType,Dimension> ImageType;
-      typedef otb::StreamingShrinkImageFilter<ImageType> ShrinkType;
+      typedef otb::StreamingShrinkImageFilter<ImageType,ImageType> ShrinkType;
 
       ShrinkType::Pointer shrink = ShrinkType::New();
     }
-- 
GitLab