-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourceimage.cpp
185 lines (155 loc) · 5.76 KB
/
sourceimage.cpp
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
/**
* @file sourceimage.cpp
* @author Yunfei Ma ([email protected])
* @brief A program for source image object
* @version 0.1
* @date 2021-12-01
*
* @copyright Copyright (c) 2021
*
*/
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include "sourceimage.hpp"
#include "point.hpp"
#include "number.hpp"
using namespace std;
const uint64_t CANVAS_WIDTH = 50;
SourceImage::SourceImage(string name, string filename_L, string filename_U, string filename_V)
{
uint64_t size = CANVAS_WIDTH;
this->name_of_the_image = name;
this->total_value_of_the_image = 0;
// Set the size of the vector first
this->color_sub_spaces.resize(size, vector<Point<3>>(size));
// Create a pointer to the file
ifstream fin_L;
ifstream fin_U;
ifstream fin_V;
// Line to hold the row
string line_L = "";
string line_U = "";
string line_V = "";
// Open the file
fin_L.open(filename_L);
fin_U.open(filename_U);
fin_V.open(filename_V);
if (!fin_L.is_open())
{
cerr << "Error: Failed to open file : [" + filename_L + "] in the construction of source image. Please check if the name [" + name + "] from user input file [input.txt] is correct or exist in the database in folder [sourceimages]" << endl;
exit(1);
}
else if (!fin_U.is_open())
{
cerr << "Error: Failed to open file : [" + filename_U + "] in the construction of source image. Please check if the name [" + name + "] from user input file [input.txt] is correct or exist in the data base in folder [sourceimages]" << endl;
exit(1);
}
else if (!fin_V.is_open())
{
cerr << "Error: Failed to open file : [" + filename_V + "] in the construction of source image. Please check if the name [" + name + "] from user input file [input.txt] is correct or exist in the data base in folder [sourceimages]" << endl;
exit(1);
}
else
{
cout << "Successfully open source image color space files for [" + name + "]" << endl;
// Define the number of rows
uint32_t row = 0;
// Iterator throught the file by lines
while ((!fin_L.eof()) and (!fin_U.eof()) and (!fin_V.eof()))
{
// Read the line
getline(fin_L, line_L);
getline(fin_U, line_U);
getline(fin_V, line_V);
// If it is the first row, skip because it is just the variable names
if (row == 0)
{
row++;
continue;
}
// Initialize the column number
uint32_t col = 0;
// Split the string according to delimiter
uint64_t start_L = 0;
uint64_t end_L = line_L.find(',');
uint64_t start_U = 0;
uint64_t end_U = line_U.find(',');
uint64_t start_V = 0;
uint64_t end_V = line_V.find(',');
while (start_L < line_L.size())
{
string str_L = line_L.substr(start_L, end_L - start_L);
string str_U = line_U.substr(start_U, end_U - start_U);
string str_V = line_V.substr(start_V, end_V - start_V);
// Check if the entry from the input is valid number
if (!isNumber(str_L))
{
cerr << "Error: Invalid input [" + str_L + "] in file [sourceImage/" + filename_L + "] at row [" + to_string(row) + "] and column [" + to_string(col) + "]. Please use the validity of the file!" << endl;
exit(1);
}
else if (!isNumber(str_U))
{
cerr << "Error: Invalid input [" + str_U + "] in file [sourceImage/" + filename_U + "] at row [" + to_string(row) + "] and column [" + to_string(col) + "]. Please use the validity of the file!" << endl;
exit(1);
}
else if (!isNumber(str_V))
{
cerr << "Error: Invalid input [" + str_L + "] in file [sourceImage/" + filename_V + "] at row [" + to_string(row) + "] and column [" + to_string(col) + "]. Please use the validity of the file!" << endl;
exit(1);
}
// Hold the entry in the string to double
double entry_L = stod(str_L);
double entry_U = stod(str_U);
double entry_V = stod(str_V);
color_sub_spaces[row - 1][col] = Point<3>(entry_L, entry_U, entry_V);
// Change to next entry
start_L = end_L + 1;
start_U = end_U + 1;
start_V = end_V + 1;
end_L = line_L.find(",", start_L);
end_U = line_U.find(",", start_U);
end_V = line_V.find(",", start_V);
if (end_L == string::npos)
{
end_L = line_L.size();
end_U = line_U.size();
end_V = line_V.size();
}
col++;
}
row++;
}
}
// Close the file
fin_L.close();
fin_U.close();
fin_V.close();
}
uint32_t SourceImage::get_image_value()
{
return this->total_value_of_the_image;
}
string SourceImage::get_image_name()
{
return this->name_of_the_image;
}
Point<3> &SourceImage::get_tile_LUV_color(uint32_t row, uint32_t col)
{
uint32_t size = CANVAS_WIDTH;
if (row > size or col > size)
{
cerr << "Error, index row or col out of bound when accessing Source Image!" << endl;
exit(1);
}
return color_sub_spaces[row][col];
}
void SourceImage::reset_image_value()
{
this->total_value_of_the_image = 0;
}
void SourceImage::add_image_value(uint32_t price_of_painting)
{
this->total_value_of_the_image += price_of_painting;
}