ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkImageToImageFilterDetail.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 /*=========================================================================
00019  *
00020  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
00021  *
00022  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00023  *
00024  *  For complete copyright, license and disclaimer of warranty information
00025  *  please refer to the NOTICE file at the top of the ITK source tree.
00026  *
00027  *=========================================================================*/
00028 #ifndef __itkImageToImageFilterDetail_h
00029 #define __itkImageToImageFilterDetail_h
00030 
00031 #include "itkImageRegion.h"
00032 #include "itkSmartPointer.h"
00033 
00034 namespace itk
00035 {
00044 namespace ImageToImageFilterDetail
00045 {
00046 
00055 struct DispatchBase {};
00056 
00064 template< bool >
00065 struct BooleanDispatch {};
00066 
00075 template< int >
00076 struct IntDispatch:public DispatchBase {};
00077 
00092 template< unsigned int >
00093 struct UnsignedIntDispatch:public DispatchBase {};
00095 
00103 template< bool B1, bool B2 >
00104 struct BinaryBooleanDispatch {
00105 
00108   typedef BooleanDispatch< B1 > FirstType;
00109   typedef BooleanDispatch< B2 > SecondType;
00110 };
00111 
00118 template< int D1, int D2 >
00119 struct BinaryIntDispatch {
00120 
00123   typedef IntDispatch< D1 > FirstType;
00124   typedef IntDispatch< D2 > SecondType;
00125 };
00126 
00138 template< unsigned int D1, unsigned int D2 >
00139 struct BinaryUnsignedIntDispatch:public DispatchBase {
00140 
00143   typedef UnsignedIntDispatch< D1 > FirstType;
00144   typedef UnsignedIntDispatch< D2 > SecondType;
00145 
00161   typedef IntDispatch < ( D1 > D2 ) - ( D1 < D2 ) > ComparisonType;
00162   typedef IntDispatch< 0 >                          FirstEqualsSecondType;
00163   typedef IntDispatch< 1 >                          FirstGreaterThanSecondType;
00164   typedef IntDispatch< -1 >                         FirstLessThanSecondType;
00165 };
00166 
00183 template< unsigned int D1, unsigned int D2 >
00184 void ImageToImageFilterDefaultCopyRegion(const typename
00185                                          BinaryUnsignedIntDispatch< D1, D2 >::FirstEqualsSecondType &,
00186                                          ImageRegion< D1 > & destRegion,
00187                                          const ImageRegion< D2 > & srcRegion)
00188 {
00189   destRegion = srcRegion;
00190 }
00191 
00208 template< unsigned int D1, unsigned int D2 >
00209 void ImageToImageFilterDefaultCopyRegion(const typename
00210                                          BinaryUnsignedIntDispatch< D1, D2 >::FirstLessThanSecondType &,
00211                                          ImageRegion< D1 > & destRegion,
00212                                          const ImageRegion< D2 > & srcRegion)
00213 {
00214   // Source dimension is greater than the destination dimension, copy the
00215   // first part of the source into the destination
00216   unsigned int dim;
00217 
00218   Index< D1 >         destIndex;
00219   Size< D1 >          destSize;
00220   const Index< D2 > & srcIndex = srcRegion.GetIndex();
00221   const Size< D2 > &  srcSize = srcRegion.GetSize();
00222 
00223   // copy what we can
00224   for ( dim = 0; dim < D1; ++dim )
00225     {
00226     destIndex[dim] = srcIndex[dim];
00227     destSize[dim] = srcSize[dim];
00228     }
00229 
00230   destRegion.SetIndex(destIndex);
00231   destRegion.SetSize(destSize);
00232 }
00233 
00250 template< unsigned int D1, unsigned int D2 >
00251 void ImageToImageFilterDefaultCopyRegion(const typename
00252                                          BinaryUnsignedIntDispatch< D1, D2 >::FirstGreaterThanSecondType &,
00253                                          ImageRegion< D1 > & destRegion,
00254                                          const ImageRegion< D2 > & srcRegion)
00255 {
00256   // Source dimension is less than the destination dimension, copy source
00257   // into the first part of the destination and set zeros elsewhere.
00258   unsigned int dim;
00259 
00260   Index< D1 >         destIndex;
00261   Size< D1 >          destSize;
00262   const Index< D2 > & srcIndex = srcRegion.GetIndex();
00263   const Size< D2 > &  srcSize = srcRegion.GetSize();
00264 
00265   // copy what we can
00266   for ( dim = 0; dim < D2; ++dim )
00267     {
00268     destIndex[dim] = srcIndex[dim];
00269     destSize[dim] = srcSize[dim];
00270     }
00271   // fill in the rest of the dimensions with zero/one
00272   for (; dim < D1; ++dim )
00273     {
00274     destIndex[dim] = 0;
00275     destSize[dim] = 1;
00276     }
00277 
00278   destRegion.SetIndex(destIndex);
00279   destRegion.SetSize(destSize);
00280 }
00281 
00320 template< unsigned int D1, unsigned int D2 >
00321 class ITK_EXPORT ImageRegionCopier
00322 {
00323 public:
00324   virtual void operator()(ImageRegion< D1 > & destRegion,
00325                           const ImageRegion< D2 > & srcRegion) const
00326   {
00327     typedef typename BinaryUnsignedIntDispatch< D1, D2 >::ComparisonType ComparisonType;
00328     ImageToImageFilterDefaultCopyRegion< D1, D2 >(
00329       ComparisonType(),
00330       destRegion, srcRegion);
00331   }
00332 
00333   virtual ~ImageRegionCopier() {}
00334 };
00335 
00338 template< unsigned int D1, unsigned int D2 >
00339 std::ostream & operator<<(std::ostream & os,
00340                           const ImageRegionCopier< D1, D2 > &)
00341 {
00342   os << "ImageRegionCopier: "
00343      << typeid( ImageRegionCopier< D1, D2 > ).name() << std::endl;
00344   return os;
00345 }
00347 
00349 template< unsigned int D1, unsigned int D2 >
00350 bool operator!=(const ImageRegionCopier< D1, D2 > & c1,
00351                 const ImageRegionCopier< D1, D2 > & c2)
00352 {
00353   return &c1 != &c2;
00354 }
00355 } // end of namespace ImageToImageFilterDetail
00356 } // end namespace itk
00357 
00358 #endif
00359