-
Notifications
You must be signed in to change notification settings - Fork 1
/
SrcCode.cpp
101 lines (80 loc) · 3.19 KB
/
SrcCode.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
#include <Magick++.h>
#include <iostream>
#include <cmath>
#define ui unsigned int
using namespace std;
using namespace Magick;
//Functions List
ui brightnessCriteria(ui, ui, ui);
//Program.
int main(int argc,char **argv)
{
//Change value of this variable {true, false} to enable/disable Image Inversion respectively.
bool invertImage = false;
//Required.
InitializeMagick(*argv);
string brightToAscii = "`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
// Construct the image object. Seperating image construction from the
// the read operation ensures that a failure to read the image file
// doesn't render the image object useless.
Image testImage;
try {
//This command takes a capture from the webcam.
system("fswebcam -r 640x480 --jpeg 85 -D 1 --no-banner --save testImage.jpg");
// Read a file into image object.
testImage.read("testImage.jpg");
cout << "Image Loaded\n";
//Resizing image so as to see the complete image on the terminal.
testImage.resize(Geometry(150, 100));
ui width = testImage.columns(); // returns an unsigned int representing the my_image width
ui height = testImage.rows(); // returns an unsigned int representing the my_image heigth
cout << "Brightness Matrix Size: " << width << ' ' << height << '\n';
ui brightnessLvl;
//Below statement required if magick API works on the previous cache of image.
testImage.modifyImage();
Pixels imageCache(testImage);
for(ui x = 0; x < height; x++)
{
for(ui y = 0; y < width; y++)
{
Color c = testImage.pixelColor(y, x);
ui r = c.redQuantum() / 256;
ui g = c.greenQuantum() / 256;
ui b = c.blueQuantum() / 256;
brightnessLvl = brightnessCriteria(r, g, b);
brightnessLvl = brightnessLvl / ( 256 / brightToAscii.size() + 0.5);
if(invertImage)
brightnessLvl = brightToAscii.size() - 1 - brightnessLvl;
//Below if statement was added as an error check. I noticed while debugging that brightnessLvl was > 255 in some cases.
if(brightnessLvl >= 0 && brightnessLvl < brightToAscii.size())
{
cout << brightToAscii[brightnessLvl];
cout << brightToAscii[brightnessLvl];
}
}
cout << '\n';
}
}
catch( Exception &error_ )
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
ui brightnessCriteria(ui red, ui green, ui blue)
{
//Change the mode value between {1, 2, 3} to set different parameters to calculate brightness.
int mode = 2;
ui brightLvl;
switch(mode)
{
case 1: brightLvl = (red + green + blue) / 3;
break;
case 2: brightLvl = ( max(red, max(green, blue)) + min(red, min(green, blue)) ) / 2;
break;
case 3: brightLvl = ( 21*red + 72*green + 7*blue) / 100;
break;
}
return brightLvl;
}