Skip to content

Commit e076262

Browse files
committed
Real-time conversion and Multi-thread support
1 parent 50e995a commit e076262

File tree

4 files changed

+155
-191
lines changed

4 files changed

+155
-191
lines changed

Vid2Ascii/Gui.cpp

+53-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include "Vid2Ascii.h"
33
#include <windows.h>
4+
#include <cmath>
45

56
#define NOMINMAX
67
#define WIN32_LEAN_AND_MEAN
@@ -14,19 +15,42 @@ void Vid2Ascii::setCursorPosition(int x, int y)
1415

1516
void Vid2Ascii::progressBar(int idx, int total_frames)
1617
{
17-
float barWidth = 40;
18-
float progress = idx * 1.0 / total_frames;
19-
float pos = barWidth * progress;
18+
/*
19+
[#] - Current
20+
[=] - Processed
21+
[ ] - Unprocessed
22+
*/
23+
int num_ths;
24+
(use_n_threads == 0) ? num_ths = 1 : num_ths = use_n_threads;
25+
26+
float max_frames = total_frames / num_ths;
2027

21-
std::cout << "[" << (int)(idx * 100) / total_frames << "%]";
28+
int barWidth = 40;
29+
float sub_section = barWidth / num_ths;
2230

2331
std::string bar;
24-
bar += "[";
32+
for (int i = 0; i < num_ths; i++) {
33+
float processed_frames = (frames_processed[i] * 1.0 / max_frames) * sub_section;
34+
processed_frames = round(processed_frames);
35+
float unprocessed_frames = ((max_frames - frames_processed[i]) * 1.0 / max_frames) * sub_section;
36+
unprocessed_frames = round(unprocessed_frames);
37+
38+
std::string processed(processed_frames, '=');
39+
std::string unprocessed(unprocessed_frames, ' ');
40+
41+
bar += processed;
42+
bar += unprocessed;
43+
}
44+
45+
float progress = idx * 1.0 / total_frames;
46+
int pos = barWidth * progress;
2547
for (int i = 0; i < barWidth; i++) {
26-
(i <= pos ? bar += "#" : bar += "=");
48+
if (i <= pos)
49+
bar[i] = '#';
2750
}
28-
bar += "]";
29-
std::cout << bar << " " << idx << "/" << total_frames << " ";
51+
bar = '[' + std::to_string((int)(idx * 100) / total_frames) + "%][" + bar + "] ";
52+
bar += std::to_string(idx) + '/' + std::to_string(total_frames) + " ";
53+
std::cout << bar;
3054
}
3155

3256
void Vid2Ascii::display_adjustedOutputSize(int ascii_height, int ascii_width)
@@ -41,7 +65,7 @@ void Vid2Ascii::display_adjustedOutputSize(int ascii_height, int ascii_width)
4165
}
4266
}
4367

44-
void Vid2Ascii::adjustOutputSize(float _resize_ratio, float _cell_height, float _cell_width)
68+
void Vid2Ascii::adjustOutput(float _resize_ratio, float _cell_height, float _cell_width, unsigned _use_n_threads)
4569
{
4670
system("CLS");
4771
output_vid.resize_ratio = _resize_ratio;
@@ -55,13 +79,15 @@ void Vid2Ascii::adjustOutputSize(float _resize_ratio, float _cell_height, float
5579
output_vid.ascii_height = output_vid.height / _cell_height;
5680
output_vid.ascii_width = output_vid.width / _cell_width;
5781

82+
use_n_threads = _use_n_threads;
83+
5884
int ascii_height = output_vid.ascii_height;
5985
int ascii_width = output_vid.ascii_width;
6086
display_adjustedOutputSize(ascii_height, ascii_width);
6187

6288
setCursorPosition(0, ascii_height + 1);
6389
char res;
64-
std::cout << "Resize output size? y/n -> ";
90+
std::cout << "Resize output? y/n -> ";
6591
std::cin >> res;
6692
res = std::tolower(res);
6793

@@ -78,6 +104,22 @@ void Vid2Ascii::adjustOutputSize(float _resize_ratio, float _cell_height, float
78104
std::cout << "New cell height: ";
79105
std::cin >> _cell_height;
80106

81-
adjustOutputSize(_resize_ratio, _cell_height, _cell_width);
107+
if (max_threads != 0) {
108+
std::cout << "---" << std::endl;
109+
std::cout << "Current number of threads used: " << _use_n_threads << std::endl;
110+
std::cout << "Max number of threads supported by your hardware: " << max_threads << std::endl;
111+
std::cout << "Recommended number of threads to use: 1" << std::endl;
112+
std::cout << "Use: ";
113+
unsigned int num_ths;
114+
std::cin >> num_ths;
115+
if (num_ths > max_threads) {
116+
std::cout << "Cannot have " << num_ths << " threads" << std::endl;
117+
}
118+
else {
119+
_use_n_threads = num_ths;
120+
}
121+
std::cout << "---" << std::endl;
122+
}
123+
adjustOutput(_resize_ratio, _cell_height, _cell_width, _use_n_threads);
82124
}
83125
}

0 commit comments

Comments
 (0)