changeset 1922:369897749653

ImageAccessor::GetRegion
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Mar 2016 17:53:19 +0100
parents 5e1ee8f4fc9e
children 6ac7f31fc543
files Core/Images/ImageAccessor.cpp Core/Images/ImageAccessor.h
diffstat 2 files changed, 50 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageAccessor.cpp	Fri Mar 04 10:58:34 2016 +0100
+++ b/Core/Images/ImageAccessor.cpp	Fri Mar 04 17:53:19 2016 +0100
@@ -121,7 +121,7 @@
   {
     if (buffer_ != NULL)
     {
-      return reinterpret_cast<const uint8_t*>(buffer_) + y * pitch_;
+      return buffer_ + y * pitch_;
     }
     else
     {
@@ -143,7 +143,7 @@
 
     if (buffer_ != NULL)
     {
-      return reinterpret_cast<uint8_t*>(buffer_) + y * pitch_;
+      return buffer_ + y * pitch_;
     }
     else
     {
@@ -174,7 +174,7 @@
     width_ = width;
     height_ = height;
     pitch_ = pitch;
-    buffer_ = const_cast<void*>(buffer);
+    buffer_ = reinterpret_cast<uint8_t*>(const_cast<void*>(buffer));
 
     if (GetBytesPerPixel() * width_ > pitch_)
     {
@@ -194,7 +194,7 @@
     width_ = width;
     height_ = height;
     pitch_ = pitch;
-    buffer_ = buffer;
+    buffer_ = reinterpret_cast<uint8_t*>(buffer);
 
     if (GetBytesPerPixel() * width_ > pitch_)
     {
@@ -232,4 +232,43 @@
     buffer.Flatten(target);
   }
 
+
+
+  ImageAccessor ImageAccessor::GetRegion(unsigned int x,
+                                         unsigned int y,
+                                         unsigned int width,
+                                         unsigned int height) const
+  {
+    if (x + width > width_ ||
+        y + height > height_)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+    
+    ImageAccessor result;
+
+    if (width == 0 ||
+        height == 0)
+    {
+      result.AssignWritable(format_, 0, 0, 0, NULL);
+    }
+    else
+    {
+      uint8_t* p = (buffer_ + 
+                    y * pitch_ + 
+                    x * GetBytesPerPixel());
+
+      if (readOnly_)
+      {
+        result.AssignReadOnly(format_, width, height, pitch_, p);
+      }
+      else
+      {
+        result.AssignWritable(format_, width, height, pitch_, p);
+      }
+    }
+
+    return result;
+  }
+
 }
--- a/Core/Images/ImageAccessor.h	Fri Mar 04 10:58:34 2016 +0100
+++ b/Core/Images/ImageAccessor.h	Fri Mar 04 17:53:19 2016 +0100
@@ -35,6 +35,7 @@
 #include "../Enumerations.h"
 
 #include <string>
+#include <stdint.h>
 
 namespace Orthanc
 {
@@ -46,7 +47,7 @@
     unsigned int width_;
     unsigned int height_;
     unsigned int pitch_;
-    void *buffer_;
+    uint8_t *buffer_;
 
   public:
     ImageAccessor()
@@ -119,5 +120,10 @@
                         void *buffer);
 
     void ToMatlabString(std::string& target) const; 
+
+    ImageAccessor GetRegion(unsigned int x,
+                            unsigned int y,
+                            unsigned int width,
+                            unsigned int height) const;
   };
 }