-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (94 loc) · 3.09 KB
/
main.cpp
File metadata and controls
109 lines (94 loc) · 3.09 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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <vector>
#include "write_png.h"
#define COLUMNS 1024
#define LINES 768
static const char* filename = "image_cpp.png";
static const char* software = "Awesome phase image simulator";
static const char* version = "1.2.3";
static const char* sim_params = R"---(# Image simulation parameters
mesh: cute_sample.msh
foo: 42
bar: 53
)---";
static Colormap jet_colors = {
{0, {0, 0, 1}}, // blue
{0.25, {0, 1, 1}}, // cyan
{0.5, {0, 1, 0}}, // green
{0.75, {1, 1, 0}}, // yellow
{1, {1, 0, 0}} // red
};
// Read a colormap from a file.
// The file format is text, with four values per line: (x, r, g, b).
// All values should be between 0 and 1.
// Empty lines and lines starting with '#' are comments and are ignored.
static Colormap read_colormap(std::string filename) {
std::ifstream f(filename);
if (!f.is_open())
throw std::ios_base::failure(filename);
std::string line;
Colormap colormap;
while (std::getline(f, line)) {
if (line.empty() || line[0] == '#') // skip comments
continue;
std::istringstream input(line);
double x, r, g, b;
input >> x >> r >> g >> b;
ColormapEntry entry = {x, {r, g, b}};
colormap.push_back(entry);
}
return colormap;
}
static double sq(double x) { return x * x; }
int main(int argc, char *argv[]) {
try {
// Parse command line
Colormap colormap;
bool use_palette = (argc >= 2) && (strcmp(argv[1], "-c") == 0);
if (use_palette) {
if (argc >= 3)
colormap = read_colormap(argv[2]);
else
colormap = jet_colors;
}
// Create the pixel matrix using std::vector
std::vector<std::vector<double>> pixels(LINES, std::vector<double>(COLUMNS));
// Fill the matrix with "bullseye" values
for (int i = 0; i < LINES; i++) {
for (int j = 0; j < COLUMNS; j++) {
double r = std::sqrt(sq(i - (LINES - 1.0) / 2) + sq(j - (COLUMNS - 1.0) / 2));
pixels[i][j] = std::cos(sq(2e-2 * r));
}
}
// Prepare the Image object
Image image{
.width = COLUMNS,
.height = LINES,
.pixels = pixels,
.pixel_size = 1e-9, // 1 nm pixels
.unit = "rad",
.metadata = {
{"Software", software},
{"Version", version},
{"Simulation parameters", sim_params}
}
};
// Open the output file
std::ofstream out(filename, std::ios::binary);
if (out.fail()) {
std::cerr << "Error: Unable to open the file " << filename << std::endl;
return 1;
}
// Call the write_png_image function
write_png_image(out, image, colormap);
std::cout << "Image saved in " << filename << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}