From 09ba206fe69980770e8a4f3a89beab6594af2a78 Mon Sep 17 00:00:00 2001
From: Gregoire Mercier <gregoire.mercier@telecom-bretagne.eu>
Date: Mon, 13 Apr 2009 12:14:12 +0200
Subject: [PATCH] BUG: Forward and Inverse SubsampleImageFilter

---
 Code/BasicFilters/otbSubsampleImageFilter.txx | 66 +++++++++++++------
 .../otbSubsampledImageRegionConstIterator.h   | 16 ++---
 .../otbSubsampledImageRegionConstIterator.txx |  1 +
 3 files changed, 55 insertions(+), 28 deletions(-)

diff --git a/Code/BasicFilters/otbSubsampleImageFilter.txx b/Code/BasicFilters/otbSubsampleImageFilter.txx
index f92e29444d..01eba921ee 100644
--- a/Code/BasicFilters/otbSubsampleImageFilter.txx
+++ b/Code/BasicFilters/otbSubsampleImageFilter.txx
@@ -127,37 +127,65 @@ SubsampleImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
 
   SubsampledImageRegionConstIterator< InputImageType > inputIter 
     ( this->GetInput(), inputRegionForThread );
-  if ( static_cast<int>( DirectionOfTransformation ) == FORWARD )
-    inputIter.SetSubsampleFactor( GetSubsampleFactor() );
+  inputIter.SetSubsampleFactor( static_cast<int>( DirectionOfTransformation ) == FORWARD ?
+    GetSubsampleFactor() : 1 );
   inputIter.GoToBegin();
 
   SubsampledImageRegionIterator< OutputImageType > outputIter
     ( this->GetOutput(), outputRegionForThread );
-  //outputIter.SetSubsampleFactor(1);
+  outputIter.SetSubsampleFactor(1);
   outputIter.GoToBegin();
 
+  switch ( DirectionOfTransformation )
+  {
+    case FORWARD:
+    {
+      inputIter.SetSubsampleFactor( GetSubsampleFactor() );
+      inputIter.GoToBegin();
+
+      while ( !inputIter.IsAtEnd() && !outputIter.IsAtEnd() )
+      {
+        outputIter.SetLocation( static_cast< typename SubsampledImageRegionIterator< OutputImageType >::OffsetType >
+                            ( inputIter.GetLocationOffset() ) );
+        outputIter.Set( static_cast< OutputPixelType >( inputIter.Get() ) );
+        ++inputIter;
+      }
+
+      break;
+    }
+    case INVERSE:
+    {
+      inputIter.SetSubsampleFactor( 1 );
+      inputIter.GoToBegin();
+
+      while ( !inputIter.IsAtEnd() && !outputIter.IsAtEnd() )
+      {
+        InputImageIndexType inputIndex = inputIter.GetLocationIndex();
+        OutputImageIndexType outputIndex;
+        for ( unsigned int i = 0; i < OutputImageDimension; i++ )
+        {
+          outputIndex[i] = inputIndex[i] * GetSubsampleFactor();
+        }
+        outputIter.SetLocation( outputIndex );
+        outputIter.Set( static_cast< OutputPixelType >( inputIter.Get() ) );
+        ++inputIter;
+      }
+
+      break;
+    }
+    default:
+    {
+      itkExceptionMacro(<<"otb::SubsampleImageFilter have to be FORWARD or INVERSE only!!");
+      break;
+    }
+  }
+
   std::cerr << "thread=" << threadId 
     << " inputRegionIndex=[" << inputRegionForThread.GetIndex()[0] << "," << inputRegionForThread.GetIndex()[1] 
     << "] inputRegionSize=[" << inputRegionForThread.GetSize()[0] << "," << inputRegionForThread.GetSize()[1]
     << "]\n\toutputRegionIndex=[" << outputRegionForThread.GetIndex()[0] << "," << outputRegionForThread.GetIndex()[1]
     << "] outputRegionSize=[" << outputRegionForThread.GetSize()[0] << "," << outputRegionForThread.GetSize()[1]
     << "]\n";
-
-  while ( !inputIter.IsAtEnd() && !outputIter.IsAtEnd() )
-  {
-    InputImageIndexType inputIndex = inputIter.GetLocationIndex();
-    OutputImageIndexType outputIndex;
-    for ( unsigned int i = 0; i < OutputImageDimension; i++ )
-    {
-      outputIndex[i] = inputIndex[i] * GetSubsampleFactor();
-    }
-
-    outputIter.SetLocation( outputIndex );
-
-    outputIter.Set( static_cast< OutputPixelType >( inputIter.Get() ) );
-
-    ++inputIter;
-  }
 }
 
 
diff --git a/Code/Common/otbSubsampledImageRegionConstIterator.h b/Code/Common/otbSubsampledImageRegionConstIterator.h
index bc553d6f93..c1bfa8cf68 100644
--- a/Code/Common/otbSubsampledImageRegionConstIterator.h
+++ b/Code/Common/otbSubsampledImageRegionConstIterator.h
@@ -71,7 +71,8 @@ public:
 
   /** Offset typedef support. This explicit redefinition allows to Set/Get 
    * the location of the iterator. */
-  typedef typename Superclass::OffsetType OffsetType;
+  //typedef typename Superclass::OffsetType OffsetType;
+  typedef unsigned long OffsetType;
 
 
   /** PixelContainer typedef support. Used to refer to the container for
@@ -104,8 +105,7 @@ public:
 
     for ( unsigned int i = 0; i < ImageIteratorDimension; i++ )
     {
-      m_LastUsableIndex[i] = startIndex[i] 
-        + static_cast<IndexValueType>( m_SubsampleFactor * ((size[i]-1) / m_SubsampleFactor) );
+      m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>( size[i]-1 );
     }
   }
 
@@ -123,8 +123,7 @@ public:
 
     for ( unsigned int i = 0; i < ImageIteratorDimension; i++ )
     {
-      m_LastUsableIndex[i] = startIndex[i] 
-        + static_cast<IndexValueType>( m_SubsampleFactor * ((size[i]-1) / m_SubsampleFactor) );
+      m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>( size[i]-1 );
     }
   }
 
@@ -145,8 +144,7 @@ public:
 
     for ( unsigned int i = 0; i < ImageIteratorDimension; i++ )
     {
-      m_LastUsableIndex[i] = startIndex[i] 
-        + static_cast<IndexValueType>( m_SubsampleFactor * ((size[i]-1) / m_SubsampleFactor) );
+      m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>( size[i]-1 );
     }
   }
 
@@ -166,10 +164,10 @@ public:
 
     for ( unsigned int i = 0; i < ImageIteratorDimension; i++ )
     {
-      m_LastUsableIndex[i] = startIndex[i] 
-        + static_cast<IndexValueType>( m_SubsampleFactor * ((size[i]-1) / m_SubsampleFactor) );
+      m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>( size[i]-1 );
     }
 
+    m_SubSampledEndOffset = this->m_Image->ComputeOffset( m_LastUsableIndex ) + 1;
   }
 
   /** Set / Get the subsample factor */
diff --git a/Code/Common/otbSubsampledImageRegionConstIterator.txx b/Code/Common/otbSubsampledImageRegionConstIterator.txx
index c1f612b214..57755008f5 100644
--- a/Code/Common/otbSubsampledImageRegionConstIterator.txx
+++ b/Code/Common/otbSubsampledImageRegionConstIterator.txx
@@ -25,6 +25,7 @@
 
 namespace otb {
 
+
 template < class TImage >
 void
 SubsampledImageRegionConstIterator< TImage >
-- 
GitLab