-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasciiart.cpp
48 lines (41 loc) · 1.5 KB
/
asciiart.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
#include "asciiart.hpp"
AsciiArt::AsciiArt()
{
charList = "MNHQ$OC?7>!:-;. ";//Characters corresponding to pixel value in a range: 0-255
}
std::string AsciiArt::toAscii(CImg<unsigned int> img)
{
std::string result = "";
for(int y = 0; y < img.height(); y += Y_DIM)
{
for(int x = 0; x < img.width(); x += X_DIM)//Cycle through image with [X_DIM]x[Y_DIM] dimension
{
int avg = 0;
for(unsigned int i = 0; i < Y_DIM; i++)//Cycle through [Y_DIM] dimension within image
{
for(unsigned int j = 0; j < X_DIM; j++)//Cycle through [X_DIM] dimension within image
{
avg += *img.data((x + j), y + i, 0);
}
}
avg /= (Y_DIM * X_DIM);//Average pixel value
char chart = assignCharacter(avg);//Get corresponding character for average value of pixels inside [X_DIM]:[Y_DIM] dimension within image
result += chart;
}
result += '\n';
}
return result;
}
char AsciiArt::assignCharacter(int value)
{
double per_item = 255.0 / charList.size();//Divide equally list for each character inside
for(unsigned int i = 0; i < charList.size(); i++)
{
if((i * per_item) >= value)
{
//std::cout << "i: " << i << " |value: " << value << " |char_size: " << charList.size() << " |per_item: " << per_item << " char: " << charList.at(i) << std::endl;
return charList.at(i);
}
}
return ' ';
}