-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_reader.cpp
More file actions
58 lines (48 loc) · 1.58 KB
/
image_reader.cpp
File metadata and controls
58 lines (48 loc) · 1.58 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
//reads the images stored in a file
#include "image_reader.h"
#include <istream>
#include <ostream>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
ImagesReader::ImagesReader() {}
//takes an image and makes it a 2d array
istream & operator >> (istream &in, ImagesReader &c) {
int image_with_pixel_values[c.kImageLength][c.kImageLength];
for (int i = 0; i < c.kImageLength; i++) {
for (int j = 0; j < c.kImageLength; j++) {
image_with_pixel_values[i][j] = 0;
}
}
int times_to_loop = c.kImageLength;
string image_data_line;
for (int line = 0; line < times_to_loop; line++) {
getline(in, image_data_line);
if (image_data_line.empty()) {
continue;
}
for (unsigned long long character_in_line = 0; character_in_line < image_data_line.size(); character_in_line++) {
if (image_data_line.at(character_in_line) == c.kGrayValue || image_data_line.at(character_in_line) == c.kBlackValue) {
image_with_pixel_values[line][character_in_line] = 1;
}
}
}
for (int i = 0; i < c.kImageLength; i++) {
for (int j = 0; j < c.kImageLength; j++) {
c.actual_image[i][j] = image_with_pixel_values[i][j];
}
}
return in;
}
//outputs an image that is a 2d array
ostream &operator<<(ostream &out, const ImagesReader &c) {
for (int i= 0; i < c.kImageLength; i++) {
for (int j = 0; j < c.kImageLength; j++) {
out << c.actual_image[i][j];
}
out << std::endl;
}
return out;
}