[opengtl-commits] [590] add an Iterator to AbstractImage to iterate over underlying buffer of images

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 590
Author:   cyrille
Date:     2009-03-07 21:01:33 +0100 (Sat, 07 Mar 2009)

Log Message:
-----------
add an Iterator to AbstractImage to iterate over underlying buffer of images

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/Program.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.h
    trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.h
    trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/Program.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/Program.cpp	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/Program.cpp	2009-03-07 20:01:33 UTC (rev 590)
@@ -404,11 +404,16 @@
 
 void Program::apply(const GTLCore::AbstractImage& input, GTLCore::AbstractImage& output) const
 {
-  const GTLCore::BufferImage* inputBI = dynamic_cast<const GTLCore::BufferImage*>( &input );
-  GTLCore::BufferImage* outputBI = dynamic_cast<GTLCore::BufferImage*>( &output );
-  GTL_ASSERT( inputBI );
-  GTL_ASSERT( outputBI );
-  apply( *inputBI->buffer(), *outputBI->buffer() );
+  GTLCore::AbstractImage::ConstIterator* itSrc = input.createIterator();
+  GTLCore::AbstractImage::Iterator* itDst = output.createIterator();
+  
+  while( itSrc->next() and itDst->next())
+  {
+    apply( *itSrc->current(), *itDst->current() );
+  }
+  
+  delete itDst;
+  delete itSrc;
 }
 
 

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.cpp	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.cpp	2009-03-07 20:01:33 UTC (rev 590)
@@ -35,6 +35,22 @@
   int pixelSize;
 };
 
+AbstractImage::ConstIterator::ConstIterator()
+{
+}
+
+AbstractImage::ConstIterator::~ConstIterator()
+{
+}
+
+AbstractImage::Iterator::Iterator()
+{
+}
+
+AbstractImage::Iterator::~Iterator()
+{
+}
+
 AbstractImage::AbstractImage( const GTLCore::PixelDescription& _pixelDescription ) : d(new Private(_pixelDescription))
 {
   GTL_ASSERT(d->pixelDescription.bitsSize() % 8 == 0);
@@ -80,7 +96,15 @@
       {
         if( errorCount < 100 )
         {
-          GTL_ERROR( "Pixel (" << x << ", " << y << ") is different." );
+          String val1, val2;
+          const char* px1 = data( x, y);
+          const char* px2 = _image->data( x, y);
+          for( int i = 0; i < _pixelSize; ++i)
+          {
+            val1 += String::number(static_cast<unsigned int>(px1[i]));
+            val2 += String::number(static_cast<unsigned int>(px2[i]));
+          }
+          GTL_ERROR( "Pixel (" << x << ", " << y << ") is different. " << val1 << " != " << val2 );
         } else if( errorCount == 100 )
         {
           GTL_ERROR( "and more...");

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.h	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AbstractImage.h	2009-03-07 20:01:33 UTC (rev 590)
@@ -21,6 +21,7 @@
 #define _GTLCORE_ABSTRACT_IMAGE_H_
 
 namespace GTLCore {
+  class Buffer;
   class PixelDescription;
   class Region;
   /**
@@ -38,6 +39,45 @@
         Nearest,
         Linear
       };
+      /**
+       * Iterator allows to access Image data as buffer. A \ref BufferImage
+       * will return an iterator that gives access to a single buffer,
+       * while a titled image will give an iterator that after each call to \ref next
+       * will give access to a different tile.
+       */
+      class ConstIterator {
+        protected:
+          ConstIterator();
+        public:
+          virtual ~ConstIterator();
+        public:
+          virtual bool next() = 0;
+          virtual bool hasNext() const = 0;
+          /**
+           * @return the current buffer.
+           */
+          virtual const Buffer* current() const = 0;
+          /**
+           * @return the area covered by the current buffer.
+           */
+          virtual Region area() const = 0;
+      };
+      /**
+       * Iterator allows to access Image data as buffer. A \ref BufferImage
+       * will return an iterator that gives access to a single buffer,
+       * while a titled image will give an iterator that after each call to \ref next
+       * will give access to a different tile.
+       *
+       * This Iterator gives access to a non const buffer.
+       */
+      class Iterator : public ConstIterator {
+        protected:
+          Iterator();
+        public:
+          virtual ~Iterator();
+        public:
+          virtual Buffer* current() = 0;
+      };
     public:
       AbstractImage( const GTLCore::PixelDescription& _pixelDescription );
       virtual ~AbstractImage();
@@ -61,6 +101,8 @@
        *         different)
        */
       int compare( const AbstractImage* _image, const GTLCore::Region& _region) const;
+      virtual ConstIterator* createIterator() const = 0;
+      virtual Iterator* createIterator() = 0;
     protected:
       int pixelSize() const;
     private:

Modified: trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.cpp	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.cpp	2009-03-07 20:01:33 UTC (rev 590)
@@ -22,6 +22,7 @@
 #include <cstring>
 
 #include <GTLCore/Buffer.h>
+#include <GTLCore/Region.h>
 
 #include "Debug.h"
 
@@ -101,3 +102,92 @@
 {
   return d->buffer;
 }
+
+class BufferImageConstIterator : public AbstractImage::ConstIterator 
+{
+  public:
+    BufferImageConstIterator( const Buffer* buffer, const Region& region) : m_buffer(buffer), m_region(region), m_done(false)
+    {
+    }
+    bool next()
+    {
+      bool oldDone = m_done;
+      m_done = true;
+      return not oldDone;
+    }
+    bool hasNext() const
+    {
+      return m_done;
+    }
+    const Buffer* current() const
+    {
+      if(m_done)
+      {
+        return m_buffer;
+      } else {
+        return 0;
+      }
+    }
+    Region area() const
+    {
+      return m_region;
+    }
+  private:
+    const Buffer* m_buffer;
+    Region m_region;
+    bool m_done;
+};
+
+AbstractImage::ConstIterator* BufferImage::createIterator() const
+{
+  return new BufferImageConstIterator(buffer(), Region(0, 0, width(), height()));
+}
+
+class BufferImageIterator : public AbstractImage::Iterator 
+{
+  public:
+    BufferImageIterator( Buffer* buffer, const Region& region) : m_buffer(buffer), m_region(region), m_done(false)
+    {
+    }
+    bool next()
+    {
+      bool oldDone = m_done;
+      m_done = true;
+      return not oldDone;
+    }
+    bool hasNext() const
+    {
+      return m_done;
+    }
+    const Buffer* current() const
+    {
+      if(m_done)
+      {
+        return m_buffer;
+      } else {
+        return 0;
+      }
+    }
+    Buffer* current()
+    {
+      if(m_done)
+      {
+        return m_buffer;
+      } else {
+        return 0;
+      }
+    }
+    Region area() const
+    {
+      return m_region;
+    }
+  private:
+    Buffer* m_buffer;
+    Region m_region;
+    bool m_done;
+};
+
+AbstractImage::Iterator* BufferImage::createIterator()
+{
+  return new BufferImageIterator(buffer(), Region(0, 0, width(), height()));
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.h	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenGTL/GTLCore/BufferImage.h	2009-03-07 20:01:33 UTC (rev 590)
@@ -34,7 +34,6 @@
    * @ingroup GTLCore
    */
   class BufferImage : public AbstractImage {
-    friend class OpenCTL::Program;
     public:
       /**
        * @param _width width of the image
@@ -58,6 +57,8 @@
        * @return the height of the image
        */
       int height() const;
+      AbstractImage::ConstIterator* createIterator() const;
+      AbstractImage::Iterator* createIterator();
     protected:
       int lineWidth() const;
       const GTLCore::Buffer* buffer() const;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h	2009-03-07 20:00:41 UTC (rev 589)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Utils_p.h	2009-03-07 20:01:33 UTC (rev 590)
@@ -65,6 +65,14 @@
     return t1 > t2 ? t1 : t2;
   }
   
+  template<typename _T_>
+  inline _T_ bound( _T_ min, _T_ val, _T_ max )
+  {
+    if( val < min ) return min;
+    if( val > max ) return max;
+    return val;
+  }
+  
 template <typename T> inline void toUnaligned(const T src, unsigned char *dest)
 {
     memcpy(dest, &src, sizeof(T));


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/