This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageaos.cpp
209 lines (179 loc) · 6.79 KB
/
imageaos.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Source file containing functions exclusive to the AOS version */
#include "imageaos.hpp"
#include "common_gauss.cpp"
#include "common_hst.hpp"
#include "common_mono.cpp"
#include "common_rw.hpp"
#include <cstring>
#include <filesystem>
#include <iostream>
std::vector<struct Pixel> read_pixels(const std::filesystem::path &path, uint32_t start, uint32_t width, uint32_t height)
// Reads the RGB values of each pixel in the image
{
std::ifstream f;
f.open(path, std::ios::in | std::ios::binary);
/* We do not need to check if it exists or if it could be opened because read_header() already did */
f.seekg(start);
int px = int(width * height);
std::vector<Pixel> img(px);
/* To calculate how many bytes of padding we have in each row, first we find out how many bytes the colors take up:
* that is three bytes per color. That amount modulo 4 tells us how many bytes over we are, and 4 minus that amount
* gives us how many bytes we have left. If the color bytes modulo 4 is 0, then that amount is 4, and we may skip
* information from the image, so we apply modulo 4 once again to convert that to 0.
*/
const int padding_bytes = (4 - (int(width) * 3) % 4) % 4;
int read = 0;
for (int i = 0; i < px; i++) {
f.read(reinterpret_cast<char *>(&img[i].b), sizeof(uint8_t));
read++;
f.read(reinterpret_cast<char *>(&img[i].g), sizeof(uint8_t));
read++;
f.read(reinterpret_cast<char *>(&img[i].r), sizeof(uint8_t));
read++;
if (read == static_cast<int>(width) * 3) {
read = 0;
f.ignore(padding_bytes);
}
}
return img;
}
void write_bmp(std::filesystem::path &path, const Header &header, std::vector<Pixel> image)
// Writes a (valid) bitmap file in the specified directory using a given header and the color values for its pixels
{
write_header(path, header);
std::ofstream f;
f.open(path, std::ios::in | std::ios::binary);
/* We do not need to check for errors because write_header() already did */
f.seekp(int(header.img_start));
const int padding_bytes = (4 - (static_cast<int>(header.img_width) * 3) % 4) % 4;
int px = int(header.img_width * header.img_height);
int zero = 0;
int wrote = 0;
for (int i = 0; i < px; i++) {
f.write(reinterpret_cast<char *>(&image[i].b), sizeof(uint8_t));
wrote++;
f.write(reinterpret_cast<char *>(&image[i].g), sizeof(uint8_t));
wrote++;
f.write(reinterpret_cast<char *>(&image[i].r), sizeof(uint8_t));
wrote++;
if (wrote == static_cast<int>(header.img_width) * 3) {
f.write(reinterpret_cast<const char *>(&zero), padding_bytes);
wrote = 0;
}
}
}
void count(std::vector<int> &freq_red, std::vector<int> &freq_green, std::vector<int> &freq_blue, const std::vector<Pixel> &img) {
int n = static_cast<int>(img.size());// Save the number of pixels in the image
for (int i = 0; i < n; i++) {
freq_red[img[i].r]++;
freq_green[img[i].g]++;
freq_blue[img[i].b]++;
}
}
void histogram(const std::vector<Pixel> &img, std::filesystem::path path) {
std::ofstream f = open_file(path);
// Declare 3 vectors to count the frequencies, one for each color
std::vector<int> freq_red, freq_green, freq_blue;
freq_red.resize(256);// size = 256 as we can have those values
freq_green.resize(256);
freq_blue.resize(256);
count(freq_red, freq_green, freq_blue, img);
print_to_file(freq_red, f);
print_to_file(freq_green, f);
print_to_file(freq_blue, f);
}
Pixel getim(int position, const std::vector<Pixel> &img) {
Pixel p{};
p.r = img[position].r;
p.g = img[position].g;
p.b = img[position].b;
return p;
}
int multiply(uint8_t color, int m) {
int ip = static_cast<int>(color);
int mip = m * ip;
return mip;
}
Pixel pixel_to_zero(Pixel p) {
p.r = static_cast<uint8_t>(0);
p.g = static_cast<uint8_t>(0);
p.b = static_cast<uint8_t>(0);
return p;
}
Pixel getres(int sumatoryr, int sumatoryg, int sumatoryb) {
Pixel res{};
int w = 273;
int resr = sumatoryr / w;
int resg = sumatoryg / w;
int resb = sumatoryb / w;
res.r = static_cast<uint8_t>(resr);
res.g = static_cast<uint8_t>(resg);
res.b = static_cast<uint8_t>(resb);
return res;
}
Pixel getmim(int i, int j, const std::vector<Pixel> &img, const Header &h) {
int sumatoryr = 0, sumatoryg = 0, sumatoryb = 0;
for (int s = -3; s < 2; s++) {
for (int t = -3; t < 2; t++) {
Pixel p{};
int position = ((i + s) * static_cast<int>(h.img_width)) + (j + t);
if (i + s < 0 || j + t < 0 || i + s > static_cast<int>(h.img_height) - 1 || j + t > static_cast<int>(h.img_width) - 1) {
p = pixel_to_zero(p);
} else {
p = getim(position, img);
}
int m = getm(s, t);
int mimr = multiply(p.r, m);
int mimg = multiply(p.g, m);
int mimb = multiply(p.b, m);
sumatoryr += mimr;
sumatoryg += mimg;
sumatoryb += mimb;
}
}
Pixel res = getres(sumatoryr, sumatoryg, sumatoryb);
return res;
}
std::vector<Pixel> gauss(const std::vector<Pixel> &img, const Header &h) {
std::vector<Pixel> res;
res.resize(h.img_height * h.img_width);
for (int i = 0; i < static_cast<int>(h.img_height); i++) {
for (int j = 0; j < static_cast<int>(h.img_width); j++) {
Pixel resij = getmim(i, j, img, h);
res[i * h.img_width + j] = resij;
}
}
return res;
}
void call_histogram(const std::vector<Pixel> &image, const std::filesystem::path &new_file) {
std::filesystem::path output_file = new_file.parent_path();
output_file /= new_file.stem();
output_file += ".hst";
histogram(image, output_file);
}
std::vector<Pixel> mono(std::vector<Pixel> &image) {
std::vector<Pixel> grayscale_img;
for (auto &i: image) {
std::vector<uint8_t> colors = {i.r, i.g, i.b};
std::vector<double> norm_col = normalize(colors);
norm_col = linear_intensity_transform(norm_col);
double gray = gamma_correct(linear_transform(norm_col[0], norm_col[1], norm_col[2]));
u_int8_t den_gray = denormalize(gray);
Pixel x = {den_gray, den_gray, den_gray};
grayscale_img.push_back(x);
}
return grayscale_img;
}
std::vector<Pixel> perform_op(std::vector<Pixel> image, std::string &op, const std::filesystem::path &new_file, const Header &header) {
const char *string = op.c_str();
if (strcmp(string, "histo") == 0) {
call_histogram(image, new_file);
}
if (strcmp(string, "mono") == 0) {
image = mono(image);
}
if (strcmp(string, "gauss") == 0) {
image = gauss(image, header);
}
return image;
}