Skip to content

Commit

Permalink
Real-time conversion and Multi-thread support
Browse files Browse the repository at this point in the history
  • Loading branch information
Codehalla committed Jun 21, 2021
1 parent 50e995a commit e076262
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 191 deletions.
64 changes: 53 additions & 11 deletions Vid2Ascii/Gui.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include "Vid2Ascii.h"
#include <windows.h>
#include <cmath>

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

void Vid2Ascii::progressBar(int idx, int total_frames)
{
float barWidth = 40;
float progress = idx * 1.0 / total_frames;
float pos = barWidth * progress;
/*
[#] - Current
[=] - Processed
[ ] - Unprocessed
*/
int num_ths;
(use_n_threads == 0) ? num_ths = 1 : num_ths = use_n_threads;

float max_frames = total_frames / num_ths;

std::cout << "[" << (int)(idx * 100) / total_frames << "%]";
int barWidth = 40;
float sub_section = barWidth / num_ths;

std::string bar;
bar += "[";
for (int i = 0; i < num_ths; i++) {
float processed_frames = (frames_processed[i] * 1.0 / max_frames) * sub_section;
processed_frames = round(processed_frames);
float unprocessed_frames = ((max_frames - frames_processed[i]) * 1.0 / max_frames) * sub_section;
unprocessed_frames = round(unprocessed_frames);

std::string processed(processed_frames, '=');
std::string unprocessed(unprocessed_frames, ' ');

bar += processed;
bar += unprocessed;
}

float progress = idx * 1.0 / total_frames;
int pos = barWidth * progress;
for (int i = 0; i < barWidth; i++) {
(i <= pos ? bar += "#" : bar += "=");
if (i <= pos)
bar[i] = '#';
}
bar += "]";
std::cout << bar << " " << idx << "/" << total_frames << " ";
bar = '[' + std::to_string((int)(idx * 100) / total_frames) + "%][" + bar + "] ";
bar += std::to_string(idx) + '/' + std::to_string(total_frames) + " ";
std::cout << bar;
}

void Vid2Ascii::display_adjustedOutputSize(int ascii_height, int ascii_width)
Expand All @@ -41,7 +65,7 @@ void Vid2Ascii::display_adjustedOutputSize(int ascii_height, int ascii_width)
}
}

void Vid2Ascii::adjustOutputSize(float _resize_ratio, float _cell_height, float _cell_width)
void Vid2Ascii::adjustOutput(float _resize_ratio, float _cell_height, float _cell_width, unsigned _use_n_threads)
{
system("CLS");
output_vid.resize_ratio = _resize_ratio;
Expand All @@ -55,13 +79,15 @@ void Vid2Ascii::adjustOutputSize(float _resize_ratio, float _cell_height, float
output_vid.ascii_height = output_vid.height / _cell_height;
output_vid.ascii_width = output_vid.width / _cell_width;

use_n_threads = _use_n_threads;

int ascii_height = output_vid.ascii_height;
int ascii_width = output_vid.ascii_width;
display_adjustedOutputSize(ascii_height, ascii_width);

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

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

adjustOutputSize(_resize_ratio, _cell_height, _cell_width);
if (max_threads != 0) {
std::cout << "---" << std::endl;
std::cout << "Current number of threads used: " << _use_n_threads << std::endl;
std::cout << "Max number of threads supported by your hardware: " << max_threads << std::endl;
std::cout << "Recommended number of threads to use: 1" << std::endl;
std::cout << "Use: ";
unsigned int num_ths;
std::cin >> num_ths;
if (num_ths > max_threads) {
std::cout << "Cannot have " << num_ths << " threads" << std::endl;
}
else {
_use_n_threads = num_ths;
}
std::cout << "---" << std::endl;
}
adjustOutput(_resize_ratio, _cell_height, _cell_width, _use_n_threads);
}
}
Loading

0 comments on commit e076262

Please sign in to comment.