From 2b13c0d303365b810816f4f6392dd85c8c1b0b77 Mon Sep 17 00:00:00 2001 From: Guan-schoolmate Date: Fri, 8 Sep 2023 06:54:26 +0000 Subject: [PATCH] [DIP] Modify the Img constructor, replacing it with MemRef --- examples/DIPDialect/resize2D.cpp | 4 +- examples/DIPDialect/rotation2D.cpp | 2 +- .../Interfaces/buddy/DIP/ImageContainer.h | 102 ++---------------- 3 files changed, 13 insertions(+), 95 deletions(-) diff --git a/examples/DIPDialect/resize2D.cpp b/examples/DIPDialect/resize2D.cpp index bfa691a97a..dabfea156d 100644 --- a/examples/DIPDialect/resize2D.cpp +++ b/examples/DIPDialect/resize2D.cpp @@ -60,8 +60,8 @@ bool testImplementation(int argc, char *argv[]) { // Define Img with the output of Resize2D. intptr_t sizes[2] = {output.getSizes()[0], output.getSizes()[1]}; - Img outputImageResize2D(sizes, output.getData()); - + Img outputImageResize2D(output.getData(),sizes); + dip::imwrite(argv[2], outputImageResize2D); return 1; diff --git a/examples/DIPDialect/rotation2D.cpp b/examples/DIPDialect/rotation2D.cpp index 1138ad6fd5..99af46e7c6 100644 --- a/examples/DIPDialect/rotation2D.cpp +++ b/examples/DIPDialect/rotation2D.cpp @@ -39,7 +39,7 @@ bool testImplementation(int argc, char *argv[]) { // Define a Img with the output of Rotate2D. intptr_t sizes[2] = {output.getSizes()[0], output.getSizes()[1]}; - Img outputImageRotate2D(sizes, output.getData()); + Img outputImageRotate2D(output.getData(),sizes); dip::imwrite(argv[2], outputImageRotate2D); diff --git a/frontend/Interfaces/buddy/DIP/ImageContainer.h b/frontend/Interfaces/buddy/DIP/ImageContainer.h index ade6587f2d..912d6a6b81 100644 --- a/frontend/Interfaces/buddy/DIP/ImageContainer.h +++ b/frontend/Interfaces/buddy/DIP/ImageContainer.h @@ -32,7 +32,7 @@ using namespace dip; // - N represents the number of dimensions. template class Img : public MemRef { public: - Img(); + Img(){}; /** * @brief overload @@ -40,13 +40,13 @@ template class Img : public MemRef { * @param data Pointer to the user data. * they just initialize the matrix header that points to the specified data. */ - Img(intptr_t *sizes, T *data); + Img(T *data, intptr_t sizes[N]); /** * @brief overload * @param sizes Array of integers specifying an n-dimensional array shape. */ - Img(intptr_t *sizes); + Img(intptr_t sizes[N]); /** * @brief overload @@ -75,26 +75,15 @@ template class Img : public MemRef { */ Img(cv::Mat image, intptr_t sizes[N] = nullptr, bool norm = false); - /** - * @brief overload - * @param sizes Array of integers specifying an n-dimensional array shape. - */ - void create(intptr_t *sizes); - int channels(); }; -// Image Constructor from Img. -template Img::Img() : MemRef() {} - /** * @brief overload * @param sizes Array of integers specifying an n-dimensional array shape. */ template -Img::Img(intptr_t *sizes) : MemRef() { - create(sizes); -} +Img::Img(intptr_t sizes[N]) : MemRef(sizes) {} /** * @brief overload @@ -102,18 +91,7 @@ Img::Img(intptr_t *sizes) : MemRef() { * matrix. */ template -Img::Img(const Img &m) : MemRef() { - for (size_t i = 0; i < N; i++) { - this->sizes[i] = m.sizes[i]; - this->strides[i] = m.strides[i]; - } - this->size = m.size; - this->allocated = new T[this->size]; - this->aligned = this->allocated; - for (size_t i = 0; i < this->size; i++) { - this->aligned[i] = m.aligned[i]; - } -} +Img::Img(const Img &m) : MemRef(m) {} // Move Constructor. // This constructor is used to initialize a MemRef object from a rvalue. @@ -122,16 +100,8 @@ Img::Img(const Img &m) : MemRef() { // Steal members from the original object. // Assign the NULL pointer to the original aligned and allocated members to // avoid the double free error. -template Img::Img(Img &&m) : MemRef() { - this->aligned = m.aligned; - this->allocated = m.allocated; - this->size = m.size; - std::swap(this->sizes, m.sizes); - std::swap(this->strides, m.strides); - // Assign the NULL pointer to the original aligned and allocated members to - // avoid the double free error. - m.allocated = m.aligned = nullptr; -} +template +Img::Img(Img &&m) : MemRef(m) {} // Move Assignment Operator. // Note that the original object no longer owns the members and spaces. @@ -141,36 +111,7 @@ template Img::Img(Img &&m) : MemRef() { // Assign the NULL pointer to the original aligned and allocated members to // avoid the double free error. template Img &Img::operator=(Img &&m) { - if (this != &m) { - // Free the original aligned and allocated space. - delete[] this->allocated; - // Steal members of the original object. - std::swap(this->size, m.size); - std::swap(this->allocated, m.allocated); - std::swap(this->aligned, m.aligned); - std::swap(this->sizes, m.sizes); - std::swap(this->strides, m.strides); - // Assign the NULL pointer to the original aligned and allocated members to - // avoid the double free error. - m.allocated = m.aligned = nullptr; - } - return *this; -} - -/** - * @brief overload - * @param sizes Array of integers specifying an n-dimensional array shape. - */ -template void Img::create(intptr_t *sizes) { - for (size_t i = 0; i < N; i++) { - this->sizes[i] = sizes[i]; - } - this->setStrides(); - this->size = this->product(this->sizes); - if (this->size > 0) { - this->allocated = new T[this->size]; - this->aligned = this->allocated; - } + MemRef::operator=(m); } /** @@ -180,23 +121,7 @@ template void Img::create(intptr_t *sizes) { */ template Img &Img::operator=(const Img &m) { - if (this == &m) { - return *this; - } else { - for (size_t i = 0; i < N; i++) { - this->sizes[i] = m.sizes[i]; - this->strides[i] = m.strides[i]; - } - this->size = m.size; - // Allocate new space and deep copy. - T *ptr = new T[this->size]; - for (size_t i = 0; i < this->size; i++) { - ptr[i] = m.aligned[i]; - } - this->allocated = ptr; - this->aligned = ptr; - } - return *this; + MemRef::operator=(m); } /** @@ -206,14 +131,7 @@ Img &Img::operator=(const Img &m) { * they just initialize the matrix header that points to the specified data. */ template -Img::Img(intptr_t *sizes, T *data) : MemRef() { - for (size_t i = 0; i < N; i++) { - this->sizes[i] = sizes[i]; - } - this->size = this->product(this->sizes); - this->setStrides(); - this->aligned = data; -} +Img::Img(T *data, intptr_t sizes[N]) : MemRef(data, sizes) {} // Image Constructor from OpenCV Mat. template