Skip to content

Commit

Permalink
Merge pull request #14 from tumcms/hechth/issue11
Browse files Browse the repository at this point in the history
Operators == and != and fill function for Image class
  • Loading branch information
hechth authored Aug 6, 2020
2 parents 4217aaa + 1728925 commit f83e38d
Show file tree
Hide file tree
Showing 6 changed files with 1,383 additions and 628 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lib/*
make.bat
Tools/updateVersion.pyo
build/*
.vscode
external/log_install_*
BlueFrameworkConfig.cmake
BlueFrameworkConfigVersion.cmake
141 changes: 92 additions & 49 deletions ImageProcessing/src/BlueFramework/ImageProcessing/Image.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of BlueFramework, a simple 3D engine.
Copyright (c) 2019 Technical University of Munich
Chair of Computational Modeling and Simulation.
Copyright (c) 2019 Technical University of Munich
Chair of Computational Modeling and Simulation.
BlueFramework is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 3
Expand All @@ -20,18 +20,16 @@
#ifndef BlueFramework_ImageProcessing_Color_f5371cca_cd2f_43c7_b795_35427ef324dc_h
#define BlueFramework_ImageProcessing_Color_f5371cca_cd2f_43c7_b795_35427ef324dc_h

#include "BlueFramework/Core/assert.h"
#include "BlueFramework/ImageProcessing/Color.h"
#include "BlueFramework/ImageProcessing/namespace.h"
#include "BlueFramework/Core/assert.h"

BLUEFRAMEWORK_IMAGEPROCESSING_NAMESPACE_BEGIN

template<typename ColorType>
template <typename ColorType>
class Image {
public:
Image(const int width, const int height) :
width_(width),
height_(height) {
Image(const int width, const int height) : width_(width), height_(height) {
data_ = new ColorType[width_ * height_];
}

Expand All @@ -46,7 +44,7 @@ class Image {
}
}

Image& operator= (Image const& src) {
Image& operator=(Image const& src) {
width_ = src.width_;
height_ = src.height_;
int size = width_ * height_;
Expand All @@ -63,26 +61,41 @@ class Image {
delete[] data_;
}

void resize(int width, int height)
{
void resize(int width, int height) {
if (data_)
delete[] data_;
width_ = width;
height_ = height;
data_ = new ColorType[width_ * height_];
}

int getWidth() const { return width_; }
int getHeight() const { return height_; }
const bool checkBounds(const int x, const int y) const { return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); }
const int getRowPitch() const { return static_cast<int>(getElementByteSize() * getWidth()); }
const int getDepthPitch() const { return getRowPitch() * getHeight(); }
const size_t getElementByteSize() const { return sizeof(ColorType); }
const ColorType* getData() const { return data_; }
ColorType* getData() { return data_; }
int getWidth() const {
return width_;
}
int getHeight() const {
return height_;
}
const bool checkBounds(const int x, const int y) const {
return x >= 0 && x < getWidth() && y >= 0 && y < getHeight();
}
const int getRowPitch() const {
return static_cast<int>(getElementByteSize() * getWidth());
}
const int getDepthPitch() const {
return getRowPitch() * getHeight();
}
const size_t getElementByteSize() const {
return sizeof(ColorType);
}
const ColorType* getData() const {
return data_;
}
ColorType* getData() {
return data_;
}

/*
The following layout is used:
The following layout is used:
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
Expand All @@ -92,53 +105,83 @@ class Image {
+-----+-----+-----+
*/
void setPixelColor(const int x, const int y, const ColorType& color) {
BLUE_ASSERT(checkBounds(x, y));
data_[x + y*width_] = color;
BLUE_ASSERT(checkBounds(x, y));
data_[x + y * width_] = color;
}

/*
The following layout is used:
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
The following layout is used:
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
*/
void setPixelColorStandardCartesian(const int x, const int y, const ColorType& color) {
BLUE_ASSERT(checkBounds(x, y));
data_[x + (height_-y-1)*width_] = color;
BLUE_ASSERT(checkBounds(x, y));
data_[x + (height_ - y - 1) * width_] = color;
}

void fill(const ColorType& color) {
for (int x = 0; x < this->getWidth(); x++) {
for (int y = 0; y < this->getHeight(); y++) {
setPixelColor(x, y, color);
}
}
}

/*
The following layout is used:
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
The following layout is used:
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
*/
ColorType getPixelColor(const int x, const int y) const {
BLUE_ASSERT(checkBounds(x, y));
return data_[x + y*width_];
BLUE_ASSERT(checkBounds(x, y));
return data_[x + y * width_];
}

bool operator==(const Image& other) const {
BLUE_ASSERT(this->getWidth() == other.getWidth(), "different widths");
BLUE_ASSERT(this->getHeight() == other.getHeight(), "different heights");

if (this->getWidth() != other.getWidth() || this->getHeight() != other.getHeight()) {
return false;
}

for (int x = 0; x < this->getWidth(); x++) {
for (int y = 0; y < this->getHeight(); y++) {
if (this->getPixelColor(x, y) != other.getPixelColor(x, y))
return false;
}
}

return true;
}

bool operator!=(const Image& other) const {
return !(this->operator==(other));
}

private:
int width_;
int height_;

/*
The following layout is used:
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
The following layout is used:
+-----+-----+-----+
|(0|0)|(1|0)|(2|0)|
+-----+-----+-----+
|(0|1)|(1|1)|(2|1)|
+-----+-----+-----+
|(0|2)|(1|2)|(2|2)|
+-----+-----+-----+
*/
ColorType* data_;
};
Expand Down
Loading

0 comments on commit f83e38d

Please sign in to comment.