Skip to content

Commit

Permalink
[DIP] Modify the Img constructor, replacing it with MemRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Guan-schoolmate committed Sep 8, 2023
1 parent b5ae137 commit 2b13c0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 95 deletions.
4 changes: 2 additions & 2 deletions examples/DIPDialect/resize2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, 2> outputImageResize2D(sizes, output.getData());
Img<float, 2> outputImageResize2D(output.getData(),sizes);

dip::imwrite(argv[2], outputImageResize2D);

return 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/DIPDialect/rotation2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, 2> outputImageRotate2D(sizes, output.getData());
Img<float, 2> outputImageRotate2D(output.getData(),sizes);

dip::imwrite(argv[2], outputImageRotate2D);

Expand Down
102 changes: 10 additions & 92 deletions frontend/Interfaces/buddy/DIP/ImageContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ using namespace dip;
// - N represents the number of dimensions.
template <typename T, size_t N> class Img : public MemRef<T, N> {
public:
Img();
Img(){};

/**
* @brief overload
* @param sizes Array of integers specifying an n-dimensional array shape.
* @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
Expand Down Expand Up @@ -75,45 +75,23 @@ template <typename T, size_t N> class Img : public MemRef<T, N> {
*/
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 <typename T, size_t N> Img<T, N>::Img() : MemRef<T, N>() {}

/**
* @brief overload
* @param sizes Array of integers specifying an n-dimensional array shape.
*/
template <typename T, size_t N>
Img<T, N>::Img(intptr_t *sizes) : MemRef<T, N>() {
create(sizes);
}
Img<T, N>::Img(intptr_t sizes[N]) : MemRef<T, N>(sizes) {}

/**
* @brief overload
* @param m Array that (as a whole or partly) is assigned to the constructed
* matrix.
*/
template <typename T, size_t N>
Img<T, N>::Img(const Img<T, N> &m) : MemRef<T, N>() {
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<T, N>::Img(const Img<T, N> &m) : MemRef<T, N>(m) {}

// Move Constructor.
// This constructor is used to initialize a MemRef object from a rvalue.
Expand All @@ -122,16 +100,8 @@ Img<T, N>::Img(const Img<T, N> &m) : MemRef<T, N>() {
// Steal members from the original object.
// Assign the NULL pointer to the original aligned and allocated members to
// avoid the double free error.
template <typename T, size_t N> Img<T, N>::Img(Img<T, N> &&m) : MemRef<T, N>() {
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 <typename T, size_t N>
Img<T, N>::Img(Img<T, N> &&m) : MemRef<T, N>(m) {}

// Move Assignment Operator.
// Note that the original object no longer owns the members and spaces.
Expand All @@ -141,36 +111,7 @@ template <typename T, size_t N> Img<T, N>::Img(Img<T, N> &&m) : MemRef<T, N>() {
// Assign the NULL pointer to the original aligned and allocated members to
// avoid the double free error.
template <typename T, size_t N> Img<T, N> &Img<T, N>::operator=(Img<T, N> &&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 <typename T, size_t N> void Img<T, N>::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<T, N>::operator=(m);
}

/**
Expand All @@ -180,23 +121,7 @@ template <typename T, size_t N> void Img<T, N>::create(intptr_t *sizes) {
*/
template <typename T, size_t N>
Img<T, N> &Img<T, N>::operator=(const Img<T, N> &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<T, N>::operator=(m);
}

/**
Expand All @@ -206,14 +131,7 @@ Img<T, N> &Img<T, N>::operator=(const Img<T, N> &m) {
* they just initialize the matrix header that points to the specified data.
*/
template <typename T, size_t N>
Img<T, N>::Img(intptr_t *sizes, T *data) : MemRef<T, N>() {
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<T, N>::Img(T *data, intptr_t sizes[N]) : MemRef<T, N>(data, sizes) {}

// Image Constructor from OpenCV Mat.
template <typename T, size_t N>
Expand Down

0 comments on commit 2b13c0d

Please sign in to comment.