# HG changeset patch # User Sebastien Jodogne # Date 1457110399 -3600 # Node ID 369897749653e87720b3176dbc2a6cac03f49fac # Parent 5e1ee8f4fc9e0633ef7ad55b163f1ff5e0fbc57d ImageAccessor::GetRegion diff -r 5e1ee8f4fc9e -r 369897749653 Core/Images/ImageAccessor.cpp --- 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(buffer_) + y * pitch_; + return buffer_ + y * pitch_; } else { @@ -143,7 +143,7 @@ if (buffer_ != NULL) { - return reinterpret_cast(buffer_) + y * pitch_; + return buffer_ + y * pitch_; } else { @@ -174,7 +174,7 @@ width_ = width; height_ = height; pitch_ = pitch; - buffer_ = const_cast(buffer); + buffer_ = reinterpret_cast(const_cast(buffer)); if (GetBytesPerPixel() * width_ > pitch_) { @@ -194,7 +194,7 @@ width_ = width; height_ = height; pitch_ = pitch; - buffer_ = buffer; + buffer_ = reinterpret_cast(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; + } + } diff -r 5e1ee8f4fc9e -r 369897749653 Core/Images/ImageAccessor.h --- 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 +#include 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; }; }