-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMatrix.cpp
More file actions
185 lines (154 loc) · 4.67 KB
/
Copy pathImageMatrix.cpp
File metadata and controls
185 lines (154 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "ImageMatrix.h"
#include <iostream>
// Default constructor
ImageMatrix::ImageMatrix(){
height=0;
width=0;
data=nullptr;
}
// Parameterized constructor for creating a blank image of given size
ImageMatrix::ImageMatrix(int imgHeight, int imgWidth) : height(imgHeight), width(imgWidth) {
data=new double*[height];
for (int i = 0; i < height; ++i) {
data[i] = new double[width];
for (int j = 0; j < width; ++j) {
data[i][j] = 0;
}
}
}
// Parameterized constructor for loading image from file. PROVIDED FOR YOUR CONVENIENCE
ImageMatrix::ImageMatrix(const std::string &filepath) {
// Create an ImageLoader object and load the image
ImageLoader imageLoader(filepath);
// Get the dimensions of the loaded image
height = imageLoader.getHeight();
width = imageLoader.getWidth();
// Allocate memory for the matrix
data = new double*[height];
for (int i = 0; i < height; ++i) {
data[i] = new double[width];
}
// Copy data from imageLoader to data
double** imageData = imageLoader.getImageData();
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; j++) {
data[i][j] = imageData[i][j];
}
}
}
// Destructor
ImageMatrix::~ImageMatrix() {
if (data!=nullptr){
for (int i = 0; i < height; ++i){
delete[] data[i];
}
delete[] data;
}
}
// Parameterized constructor - direct initialization with 2D matrix
ImageMatrix::ImageMatrix(const double** inputMatrix, int imgHeight, int imgWidth){
height = imgHeight;
width= imgWidth;
data = new double*[height];
for (int i = 0; i < height; ++i) {
data[i] = new double[width];
for (int j = 0; j < width; ++j) {
data[i][j] = inputMatrix[i][j];
}
}
}
// Copy constructor
ImageMatrix::ImageMatrix(const ImageMatrix &other) {
height= other.height;
width = other.width;
data = new double*[height];
for (int i = 0; i < height; ++i) {
data[i] = new double[width];
for (int j = 0; j < width; ++j) {
data[i][j] = other.data[i][j];
}
}
}
// Copy assignment operator
ImageMatrix& ImageMatrix::operator=(const ImageMatrix &other) {
if (this == &other) {
return *this;
}
if (data != nullptr) {
for (int i = 0; i < height; ++i) {
delete[] data[i];
}
delete[] data;
}
height = other.height;
width = other.width;
data = new double*[height];
for (int i = 0; i < height; ++i) {
data[i] = new double[width];
for (int j = 0; j < width; ++j) {
data[i][j] = other.data[i][j];
}
}
return *this;
}
// Overloaded operators
// Overloaded operator + to add two matrices
ImageMatrix ImageMatrix::operator+(const ImageMatrix &other) const {
if (height != other.height || width!= other.width){
//If they are not of the same size, just return an empty object?
return ImageMatrix();
}
ImageMatrix summation(height,width);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
summation.data[i][j] = data[i][j]+other.data[i][j];
}
}
return summation;
}
// Overloaded operator - to subtract two matrices
ImageMatrix ImageMatrix::operator-(const ImageMatrix &other) const {
if (height != other.height || width!= other.width){
//If they are not of the same size, just return an empty object.
return ImageMatrix();
}
ImageMatrix subtraction(height,width);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
subtraction.data[i][j] = data[i][j]-other.data[i][j];
}
}
return subtraction;
}
// Overloaded operator * to multiply a matrix with a scalar
ImageMatrix ImageMatrix::operator*(const double &scalar) const {
ImageMatrix multiplocation(height,width);
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
multiplocation.data[i][j] = data[i][j]*scalar;
}
}
return multiplocation;
}
// Getter function to access the data in the matrix
double** ImageMatrix::get_data() const {
return data;
}
// Getter function to access the data at the index (i, j)
double ImageMatrix::get_data(int i, int j) const {
return data[i][j];
}
int ImageMatrix::get_height() const {
return height;
}
int ImageMatrix::get_width() const {
return width;
}
//Setter Function
void ImageMatrix::set_data(int i, int j, double newValue) {
if (i >= 0 && i < height && j >= 0 && j < width) {
data[i][j] = newValue;
} else {
std::cout<<"Cell out of bounds"<<std::endl;
}
}