-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.cpp
59 lines (53 loc) · 1.64 KB
/
code.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
#include "lodepng.h"
#include <vector>
#include <iostream>
#include <map>
#include <fstream>
//decoding function
unsigned DecodeOneStep(std::vector<unsigned char> &image, const char* filename) {
unsigned width, height;
unsigned error = lodepng::decode(image, width, height, filename);
if (error) std::cout << "I CAN'T WORK!!\n";
return width;
}
//turning image to grayscale function
void ToGray(std::vector<unsigned char> &image, std::vector<unsigned> &gray_image, int n) {
for (int i = 0; i < n; i += 4) gray_image.push_back((image[i] + image[i + 1] + image[i + 2]) / 3);
}
int main() {
std::vector<unsigned char> image;
unsigned w = 0;
std::vector<unsigned> gray_image;
//map to encode image to ASCII
std::map<unsigned, char> ENCODE_MAP = { {(unsigned)0, '@'},
{(unsigned)1, '&'},
{(unsigned)2, '#'},
{(unsigned)3, 'G'},
{(unsigned)4, 'H'},
{(unsigned)5, 'P'},
{(unsigned)6, 'I'},
{(unsigned)7, 'L'},
{(unsigned)8, '|'},
{(unsigned)9, '('},
{(unsigned)10, '['},
{(unsigned)11, ':'},
{(unsigned)12, '*'},
{(unsigned)13, '"'},
{(unsigned)14, '^'},
{(unsigned)15, '.'},
};
w = DecodeOneStep(image, "your_image.png");
ToGray(image, gray_image, image.size());
//turning image into string to write it into file
std::string finalstr = "";
for (int i = 0; i < gray_image.size(); ++i) {
finalstr += ENCODE_MAP[gray_image[i] / 16];
if ((i + 1) % w == 0) finalstr += "\n";
}
//writing string into file
std::ofstream file;
file.open("result.txt");
if (file.is_open()) file << finalstr;
file.close();
return 0;
}