-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
112 lines (93 loc) · 2.02 KB
/
main.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
//#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
using namespace std;
class Image
{
int width = 0, height = 0;
string name_I;
public:
void set_resolution(int, int);
void set_name(string);
vector <int> create_I()
{
vector< int > vec_I{ 0 };
int dim = width * height * 3;
float a, b, d, e;
a = 255 / (float)width;
b = 255 / (float)height;
vec_I.clear();
for (int y = 1; y <= height; y++)
{
for (int x = 1; x <= width; x++)
{
d = a * (float)x;
e = b * (float)y;
int r = 255 - (int)e; // gradient vertical: up - white, down - black
int g = 255 - (int)d; // gradient horizontal: left - white, right - black.
int b = ((int)d * (int)e) / 255; // blending mode (multiply ), between e & d.
vec_I.push_back(r);
vec_I.push_back(g);
vec_I.push_back(b);
}
}
return vec_I;
}
void writing_I();
void open_I();
};
void Image::set_resolution(int k, int j)
{
width = k;
height = j;
}
void Image::set_name(string nn)
{
name_I = nn;
}
void Image::writing_I()
{
vector< int > vec_Img = create_I();
ofstream img(name_I.c_str());
string header_I = "P3 " + to_string(width) + " " + to_string(height) + " " + "255 ";
ostringstream str_I;
if (!vec_Img.empty())
{
copy(vec_Img.begin(), vec_Img.end(), ostream_iterator <int>(str_I, " "));
}
string img_Full = header_I + str_I.str();
img << img_Full;
img.close();
}
void Image::open_I()
{
#if defined(_WIN32) || defined(__CYGWIN__)
// Windows (x86 or x64)
system(name_I.c_str());
#elif defined(__linux__)
// Linux
// ...
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS
string O_nume = "open " + nume_I;
system(O_nume.c_str());
#elif defined(unix) || defined(__unix__) || defined(__unix)
// Unix like OS
// ...
#else
#error Unknown environment!
#endif
}
int main()
{
Image img_1;
img_1.set_resolution(1024, 1024);
img_1.set_name("Image_1024.ppm");
img_1.writing_I();
img_1.open_I();
return 0;
}