-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
115 lines (93 loc) · 2.65 KB
/
ImageLoader.cpp
File metadata and controls
115 lines (93 loc) · 2.65 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
// ImageLoader.cpp
#include "ImageLoader.h"
#include <iostream>
#include <fstream>
#include <sstream>
// Default constructor
ImageLoader::ImageLoader(const std::string& filepath) : height(0), width(0), imageData() {
std::ifstream file(filepath);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filepath << std::endl;
return;
}
std::string line;
std::vector<std::vector<double>> tempImageData;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::vector<double> row;
double pixel;
while (iss >> pixel) {
row.push_back(pixel);
}
tempImageData.push_back(row);
}
file.close();
height = static_cast<int>(tempImageData.size());
if (height == 0) {
std::cerr << "Error: Empty image data." << std::endl;
return;
}
width = static_cast<int>(tempImageData[0].size());
// Allocate memory for imageData and copy data
imageData = new double*[height];
for (int i = 0; i < height; ++i) {
imageData[i] = new double[width];
for (int j = 0; j < width; ++j) {
imageData[i][j] = tempImageData[i][j];
}
}
}
// copy constructor
ImageLoader::ImageLoader(const ImageLoader &other) : height(other.height), width(other.width) {
// Allocate memory for the matrix and copy data
imageData = new double*[height];
for (int i = 0; i < height; ++i) {
imageData[i] = new double[width];
for (int j = 0; j < width; ++j) {
imageData[i][j] = other.imageData[i][j];
}
}
}
// copy assignment operator
ImageLoader& ImageLoader::operator=(const ImageLoader &other) {
if (this == &other) {
return *this; // self-assignment check
}
// Deallocate old memory
if (imageData != nullptr) {
for (int i = 0; i < height; ++i) {
delete[] imageData[i];
}
delete[] imageData;
}
// Copy from other
height = other.height;
width = other.width;
imageData = new double*[height];
for (int i = 0; i < height; ++i) {
imageData[i] = new double[width];
for (int j = 0; j < width; ++j) {
imageData[i][j] = other.imageData[i][j];
}
}
return *this;
}
// destructor
ImageLoader::~ImageLoader() {
// Deallocate memory
if (imageData != nullptr) {
for (int i = 0; i < height; ++i) {
delete[] imageData[i];
}
delete[] imageData;
}
}
int ImageLoader::getHeight() const {
return height;
}
int ImageLoader::getWidth() const {
return width;
}
double** ImageLoader::getImageData() const {
return imageData;
}