Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
201 lines (157 loc) · 6.57 KB
/
Copy pathmain.cpp
File metadata and controls
201 lines (157 loc) · 6.57 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <filesystem>
#include <format>
#include <limits>
#include <print>
#include <tuple>
#include "src/metadata.hpp"
#include "src/sheet.hpp"
#include "src/tiles.hpp"
#include "argparse/argparse.hpp"
void
print_banner()
{
std::println(R"(
_/ _/
_/ _/
_/_/ _/_/_/ _/_/_/ _/_/ _/_/ _/_/_/_/ _/_/_/ _/_/_/ _/_/_/
_/ _/ _/_/ _/ _/ _/_/_/_/ _/_/_/_/ _/ _/ _/ _/ _/ _/
_/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/_/ _/_/_/ _/ _/ _/_/_/ _/_/_/ _/_/ _/_/_/ _/_/_/ _/_/_/
_/ _/
_/ _/
)");
}
#define CHECK_FLOAT_VALUE(__var__, __msg__) \
if ((__var__) <= 0.0f || (__var__) > std::numeric_limits<float>::max()) { \
std::print("{} {} isnot applicable", __var__, __msg__); \
\
return 1; \
}
#define CHECK_UINT32_VALUE(__var__, __msg__) \
if (0 == (__var__) || (__var__) > std::numeric_limits<uint32_t>::max()) { \
std::print("{} {} isnot applicable", __var__, __msg__); \
\
return 1; \
}
#define CHECK_PATH_EXIST(__var__, __msg__) \
if (!std::filesystem::exists(__var__)) { \
std::print("{} {} doesnot exist", __var__, __msg__); \
\
return 1; \
}
auto
main(int argc, char* argv[]) -> int
{
argparse::ArgumentParser program("osheet_cpp", "0.1.0");
program.add_argument("-i", "--input")
.required()
.help("specify the input video file");
program.add_argument("-f", "--font")
.default_value("Kode")
.help("Font style for the metadata");
program.add_argument("-fd", "--font_dir")
.default_value("fonts")
.help("Directory that contains the font style");
program.add_argument("-fs", "--font_size")
.default_value(16)
.help("Size of the font style for the metadata")
.scan<'i', int>();
program.add_argument("-r", "--rows")
.default_value(4)
.help("Total tile rows in the output")
.scan<'i', int>();
program.add_argument("-c", "--cols")
.default_value(4)
.help("Total tile columns in the output")
.scan<'i', int>();
program.add_argument("-tw", "--tile_w")
.default_value(320)
.help("Width of one tile")
.scan<'i', int>();
program.add_argument("-g", "--gap")
.default_value(5)
.help("Spacing between tiles")
.scan<'i', int>();
program.add_argument("-m", "--margin")
.default_value(10)
.help("Extra space between text and tiles")
.scan<'i', int>();
program.add_argument("-sg", "--sep_gap")
.default_value(8)
.help("Extra space between text and tiles")
.scan<'i', int>();
program.add_argument("-o", "--output")
.default_value("sheet.png")
.help("Output file");
try {
program.parse_args(argc, argv);
} catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}
std::string video_filepath = program.get<std::string>("--input");
std::string font_name = program.get<std::string>("--font");
std::string font_folder = program.get<std::string>("--font_dir");
SkScalar font_size = program.get<int>("--font_size");
uint32_t total_row = program.get<int>("--rows");
uint32_t total_column = program.get<int>("--cols");
uint32_t wanted_tile_width = program.get<int>("--tile_w");
SkScalar gap = program.get<int>("--gap");
SkScalar margin = program.get<int>("--margin");
SkScalar seperator_gap = program.get<int>("--sep_gap");
std::string output_filepath = program.get<std::string>("--output");
CHECK_PATH_EXIST(video_filepath, "video file");
CHECK_PATH_EXIST(font_folder, "font folder");
CHECK_FLOAT_VALUE(font_size, "font size");
CHECK_UINT32_VALUE(total_row, "total row");
CHECK_UINT32_VALUE(total_column, "total column");
CHECK_UINT32_VALUE(wanted_tile_width, "wanted tile width");
CHECK_FLOAT_VALUE(gap, "gap");
CHECK_FLOAT_VALUE(margin, "margin");
CHECK_FLOAT_VALUE(seperator_gap, "seperator gap");
print_banner();
size_t total_tiles = total_column * total_row;
auto&& tile_infos =
osheet::extract_tiles(video_filepath, total_tiles, wanted_tile_width);
if ((total_tiles + 1) != tile_infos.indices.size()) {
std::print("Cannot get video tiles from {} file", video_filepath);
return 1;
}
osheet::Metadata metadata = {};
if (!osheet::get_metadata(video_filepath, metadata)) {
std::print("Cannot get metadata from {} file", video_filepath);
return 1;
}
osheet::Sheet sheet = {};
sheet.set_font(font_name, font_size, font_folder);
if (!sheet.decode_store_images(tile_infos)) {
std::print("Cannot decode and store the images");
return 1;
}
sheet.create_meta_pairs(video_filepath, metadata);
auto [tile_width, tile_height] = sheet.get_tile_sizes();
if (0 == tile_width || 0 == tile_height) {
std::print("Cannot get tile width or height");
return 1;
}
SkScalar line_h = sheet.get_font_size() + 4.0f;
SkScalar header_h = margin + sheet.get_total_meta_pairs() * line_h + margin;
SkScalar grid_w = total_column * tile_width + (total_column - 1) * gap;
SkScalar grid_h = total_row * tile_height + (total_row - 1) * gap;
SkScalar width = margin * 2 + grid_w;
SkScalar height = margin * 2 + header_h + seperator_gap + line_h + grid_h;
SkScalar cur_y = margin + line_h;
sheet.init(width, height);
sheet.write_metadata(cur_y, margin, line_h);
sheet.draw_line(0, cur_y, width, cur_y, 4.0f);
cur_y += seperator_gap + line_h;
sheet.draw_tiles(cur_y, total_column, margin, gap);
if (!sheet.save(output_filepath)) {
std::print("Cannot save to {} file", output_filepath);
return 1;
}
std::println("The {} file is created successfully\n", output_filepath);
sheet.print_meta();
return 0;
}