core/vidl/vidl_pixel_iterator.cxx
Go to the documentation of this file.
00001 // This is core/vidl/vidl_pixel_iterator.cxx
00002 #include "vidl_pixel_iterator.h"
00003 #include "vidl_pixel_iterator.txx"
00004 //:
00005 // \file
00006 // \author Matt Leotta
00007 //
00008 
00009 namespace {
00010 
00011 //: Recursive template metaprogram to populate the "vidl_has_iterator" array
00012 template <vidl_pixel_format pix_type>
00013 struct populate_has_iterator
00014 {
00015   static inline void apply(bool* element)
00016   {
00017     *element = vidl_pixel_iterator_valid<pix_type>::value;
00018     populate_has_iterator<vidl_pixel_format(pix_type-1)>::apply(--element);
00019   }
00020 };
00021 
00022 //: The base case: unknown pixel type
00023 VCL_DEFINE_SPECIALIZATION
00024 struct populate_has_iterator<VIDL_PIXEL_FORMAT_UNKNOWN>
00025 {
00026   static inline void apply(bool* element)
00027   {
00028     return;
00029   }
00030 };
00031 
00032 
00033 //: The array indicating which formats have valid iterators
00034 struct vidl_has_iterator
00035 {
00036   public:
00037     vidl_has_iterator()
00038     {
00039       populate_has_iterator<vidl_pixel_format(VIDL_PIXEL_FORMAT_ENUM_END-1)>
00040         ::apply(&array[VIDL_PIXEL_FORMAT_ENUM_END-1]);
00041     }
00042 
00043     bool operator [] (unsigned int i)
00044     { return array[i]; }
00045 
00046   private:
00047     bool array[VIDL_PIXEL_FORMAT_ENUM_END];
00048 };
00049 
00050 //: The single static instance of vidl_has_iterator
00051 vidl_has_iterator has_iterator;
00052 
00053 
00054 //: Recursive template metaprogram to make a pixel_iterator
00055 template <vidl_pixel_format pix_type>
00056 struct make_pixel_iterator
00057 {
00058   static inline vidl_pixel_iterator* apply(const vidl_frame& frame)
00059   {
00060     if (frame.pixel_format() == pix_type){
00061       if (vidl_pixel_iterator_valid<pix_type>::value)
00062         return new vidl_pixel_iterator_of<pix_type>(frame);
00063       return NULL;
00064     }
00065     return make_pixel_iterator<vidl_pixel_format(pix_type-1)>::apply(frame);
00066   }
00067 };
00068 
00069 //: The base case: unknown pixel type
00070 VCL_DEFINE_SPECIALIZATION
00071 struct make_pixel_iterator<VIDL_PIXEL_FORMAT_UNKNOWN>
00072 {
00073   static inline vidl_pixel_iterator* apply(const vidl_frame& frame)
00074   {
00075     return NULL;
00076   }
00077 };
00078 
00079 //=============================================================================
00080 
00081 }; // anonymous namespace
00082 
00083 
00084 //: Pixel iterator factory
00085 // Creates a new pixel iterator on the heap
00086 // The iterator is initialized to the first pixel in the frame
00087 // \note The user is responsible for deleting the iterator
00088 vidl_pixel_iterator*
00089 vidl_make_pixel_iterator(const vidl_frame& frame)
00090 {
00091   return make_pixel_iterator<vidl_pixel_format(VIDL_PIXEL_FORMAT_ENUM_END-1)>::apply(frame);
00092 }
00093 
00094 
00095 //: Return true if the pixel format has a valid pixel iterator implementation
00096 bool vidl_has_pixel_iterator(vidl_pixel_format fmt)
00097 {
00098   return has_iterator[fmt];
00099 }