2022-02-22 00:21:11 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <queue>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
#include "video.h"
|
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
#define CHANGE_THRESHOLD 3
|
|
|
|
|
2022-02-23 02:47:23 +00:00
|
|
|
#define CHAR_Y 4
|
|
|
|
#define CHAR_X 4
|
|
|
|
#define DIFF_CASES 11
|
|
|
|
|
|
|
|
const char characters[DIFF_CASES][4] = {"\u2584", // bottom half block
|
2022-02-23 03:30:02 +00:00
|
|
|
"\u2590", // right half block
|
|
|
|
"\u2598", // top left quarter
|
|
|
|
"\u259d", // top right quarter
|
|
|
|
"\u2596", // bottom left quarter
|
|
|
|
"\u2597", // bottom right quarter
|
|
|
|
"\u259e", // diagonal
|
|
|
|
"\u2582", // lower quarter block
|
2022-02-25 04:18:28 +00:00
|
|
|
"\u2586", // lower 3 quarters block
|
2022-02-23 03:30:02 +00:00
|
|
|
"\u258e",
|
2022-02-25 04:18:28 +00:00
|
|
|
"\u258a"};
|
|
|
|
|
|
|
|
const int pixelmap[DIFF_CASES][CHAR_Y * CHAR_X] = {{0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1},
|
|
|
|
{0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1},
|
|
|
|
{1, 1, 0, 0,
|
|
|
|
1, 1, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0},
|
|
|
|
{0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0},
|
|
|
|
{0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
1, 1, 0, 0,
|
|
|
|
1, 1, 0, 0},
|
|
|
|
{0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1},
|
|
|
|
{0, 0, 1, 1,
|
|
|
|
0, 0, 1, 1,
|
|
|
|
1, 1, 0, 0,
|
|
|
|
1, 1, 0, 0},
|
|
|
|
{0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
1, 1, 1, 1},
|
|
|
|
{0, 0, 0, 0,
|
|
|
|
1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1},
|
|
|
|
{1, 0, 0, 0,
|
|
|
|
1, 0, 0, 0,
|
|
|
|
1, 0, 0, 0,
|
|
|
|
1, 0, 0, 0},
|
|
|
|
{1, 1, 1, 0,
|
|
|
|
1, 1, 1, 0,
|
|
|
|
1, 1, 1, 0,
|
|
|
|
1, 1, 1, 0}};
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define VC_EXTRALEAN
|
|
|
|
#include <Windows.h>
|
2022-02-23 03:30:02 +00:00
|
|
|
#elif defined(__linux__) || defined(__APPLE__)
|
2022-02-22 00:21:11 +00:00
|
|
|
|
2022-02-23 03:30:02 +00:00
|
|
|
#include <fcntl.h>
|
2022-02-22 00:21:11 +00:00
|
|
|
#include <sys/ioctl.h>
|
2022-02-23 03:30:02 +00:00
|
|
|
#include <unistd.h>
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
#endif // Windows/Linux
|
|
|
|
|
|
|
|
void get_terminal_size(int &width, int &height) {
|
2022-02-25 04:18:28 +00:00
|
|
|
width = -1;
|
|
|
|
height = -1;
|
2022-02-23 03:30:02 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
2022-02-22 00:21:11 +00:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
|
|
|
width = (int)(csbi.srWindow.Right-csbi.srWindow.Left+1);
|
|
|
|
height = (int)(csbi.srWindow.Bottom-csbi.srWindow.Top+1);
|
2022-02-23 03:30:02 +00:00
|
|
|
#else
|
|
|
|
int result;
|
2022-02-22 00:21:11 +00:00
|
|
|
struct winsize w{};
|
2022-02-23 03:30:02 +00:00
|
|
|
//credit https://github.com/sindresorhus/macos-term-size for the macos terminal code
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
int tty_fd = open("/dev/tty", O_EVTONLY | O_NONBLOCK);
|
|
|
|
if (tty_fd == -1) {
|
|
|
|
fprintf(stderr, "Opening `/dev/tty` failed (%d): %s\n", errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = ioctl(tty_fd, TIOCGWINSZ, &w);
|
|
|
|
close(tty_fd);
|
|
|
|
#elif defined(__linux__)
|
|
|
|
result = ioctl(fileno(stdout), TIOCGWINSZ, &w);
|
|
|
|
#endif // MacOS/Linux
|
|
|
|
if (result == -1) {
|
|
|
|
fprintf(stderr, "Getting the size failed (%d): %s\n", errno, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2022-02-22 00:21:11 +00:00
|
|
|
width = (int) (w.ws_col);
|
|
|
|
height = (int) (w.ws_row);
|
2022-02-23 03:30:02 +00:00
|
|
|
#endif
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int w = -1, h = -1;
|
|
|
|
int im_w, im_h;
|
|
|
|
double scale_factor = 0.0;
|
|
|
|
int small_dims[2];
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
char *frame;
|
|
|
|
char *old;
|
|
|
|
bool alloc = false;
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
int diff = 0;
|
2022-02-23 02:47:23 +00:00
|
|
|
int pixel[CHAR_Y][CHAR_X][3];
|
2022-02-22 00:21:11 +00:00
|
|
|
bool refresh = false;
|
|
|
|
bool begin = true;
|
|
|
|
|
|
|
|
int r, c;
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
char printbuf[50000000];
|
2022-02-22 15:46:23 +00:00
|
|
|
const char *shapechar;
|
2022-02-22 00:21:11 +00:00
|
|
|
int count = 0, curr_frame = 0;;
|
|
|
|
double fps;
|
|
|
|
int period = 0;
|
|
|
|
|
|
|
|
int printing_time, elapsed;
|
|
|
|
double avg_fps = 0;
|
|
|
|
int total_time = 0, frame10_time = 0;
|
|
|
|
std::queue<int> frametimes;
|
|
|
|
int msg_y = 0;
|
|
|
|
int dropped = 0;
|
|
|
|
double skip;
|
|
|
|
|
2022-02-23 03:30:02 +00:00
|
|
|
std::chrono::time_point<std::chrono::steady_clock> start, stop, videostart, videostop, printtime;
|
2022-02-22 00:21:11 +00:00
|
|
|
long long total_printing_time = 0;
|
|
|
|
|
|
|
|
int diffthreshold = 10;
|
|
|
|
|
|
|
|
int curr_w, curr_h, orig_w = -1, orig_h = -1;
|
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
int mindiff, diffbg, diffpixel;
|
|
|
|
int min_fg, min_bg, max_fg, max_bg;
|
2022-02-23 02:47:23 +00:00
|
|
|
int cases[DIFF_CASES];
|
2022-02-22 15:46:23 +00:00
|
|
|
int case_min = 0;
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
bool bgsame = false, pixelsame = false;
|
|
|
|
|
|
|
|
int prevpixelbg[3] = {1000, 1000, 1000};
|
|
|
|
int pixelbg[3], pixelchar[3];
|
|
|
|
int prevpixel[3] = {1000, 1000, 1000};
|
|
|
|
|
2022-02-23 02:47:23 +00:00
|
|
|
int sx = 4, sy = 8;
|
2022-02-25 04:18:28 +00:00
|
|
|
int skipy = sy / CHAR_Y, skipx = sx / CHAR_X;
|
2022-02-23 02:47:23 +00:00
|
|
|
|
2022-02-22 00:21:11 +00:00
|
|
|
void terminateProgram([[maybe_unused]] int sig_num) {
|
2022-02-23 03:30:02 +00:00
|
|
|
videostop = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
long long total_video_time = (long long) std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
videostop - videostart).count();
|
|
|
|
printf("\u001b[0m\u001b[%d;%dHframes: %5d, dropped: %5d, total time: %5.2fs, printing time: %5.2fs \u001b[?25h",
|
|
|
|
msg_y, 0, curr_frame, dropped, (double) total_video_time / 1000000.0,
|
|
|
|
(double) total_printing_time / 1000000.0);
|
|
|
|
fflush(stdout);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2022-02-23 03:30:02 +00:00
|
|
|
videostart = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
signal(SIGINT, terminateProgram);
|
|
|
|
setvbuf(stdout, printbuf, _IOLBF, sizeof(printbuf));
|
|
|
|
// Create a VideoCapture object and open the input file
|
|
|
|
// If the input is the web camera, pass 0 instead of the video file name
|
|
|
|
if (argc <= 1 || strlen(argv[1]) <= 0) {
|
|
|
|
printf("\u001b[0mplease provide the filename as the first input argument");
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-23 03:30:02 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
if (std::__fs::filesystem::exists(argv[1])) {
|
|
|
|
#else
|
2022-02-22 00:21:11 +00:00
|
|
|
if (std::filesystem::exists(argv[1])) {
|
2022-02-23 03:30:02 +00:00
|
|
|
#endif
|
2022-02-22 00:21:11 +00:00
|
|
|
if (argc > 2) diffthreshold = std::stoi(argv[2], nullptr, 10);
|
|
|
|
diffthreshold = std::max(std::min(255, diffthreshold), 0);
|
2022-02-25 04:18:28 +00:00
|
|
|
video cap(argv[1]);
|
2022-02-22 00:21:11 +00:00
|
|
|
// Check if camera opened successfully
|
|
|
|
if (!cap.isOpened()) {
|
|
|
|
printf("\u001b[0mError opening video stream or file\n");
|
|
|
|
fflush(stdout);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
fps = cap.get_fps();
|
2022-02-22 00:21:11 +00:00
|
|
|
period = (int) (1000000.0 / fps);
|
2022-02-23 03:30:02 +00:00
|
|
|
start = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
while (true) {
|
|
|
|
count++;
|
|
|
|
curr_frame++;
|
|
|
|
|
|
|
|
get_terminal_size(curr_w, curr_h);
|
|
|
|
if (curr_w != orig_w || curr_h != orig_h) {
|
|
|
|
orig_w = curr_w;
|
|
|
|
orig_h = curr_h;
|
|
|
|
w = curr_w;
|
|
|
|
h = curr_h;
|
|
|
|
h -= 1;
|
|
|
|
msg_y = h;
|
2022-02-23 02:47:23 +00:00
|
|
|
h *= sy;
|
|
|
|
w *= sx;
|
2022-02-25 04:18:28 +00:00
|
|
|
im_w = cap.get_width();
|
|
|
|
im_h = cap.get_height();
|
2022-02-22 00:21:11 +00:00
|
|
|
scale_factor = std::min((double) w / (double) im_w, (double) h / (double) im_h);
|
|
|
|
small_dims[0] = int((double) im_w * scale_factor);
|
|
|
|
small_dims[1] = int((double) im_h * scale_factor);
|
|
|
|
if (small_dims[0] == 0 || small_dims[1] == 0) {
|
|
|
|
printf("\u001b[%d;%dHterminal dimensions is too small! (%d, %d) \n",
|
|
|
|
msg_y, 0, curr_w, curr_h);
|
|
|
|
fflush(stdout);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
refresh = true;
|
|
|
|
if (begin) {
|
|
|
|
begin = false;
|
|
|
|
printf("\u001b[?25l");
|
|
|
|
printf("terminal dimensions: (w %4d, h %4d)\n", curr_w, curr_h);
|
|
|
|
printf("frame dimensions: (w %4d, h %4d)\n", im_w, im_h);
|
2022-02-25 04:18:28 +00:00
|
|
|
printf("display dimensions: (w %4d, h %4d)\n", small_dims[0], small_dims[1] / (sy / sx));
|
2022-02-22 00:21:11 +00:00
|
|
|
printf("scaling: %f\n", scale_factor);
|
|
|
|
printf("frames per second: %f\n", fps);
|
|
|
|
fflush(stdout);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2022-02-23 03:30:02 +00:00
|
|
|
start = std::chrono::steady_clock::now();
|
|
|
|
videostart = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
2022-02-25 04:18:28 +00:00
|
|
|
if (alloc) {
|
|
|
|
std::free(frame);
|
|
|
|
std::free(old);
|
|
|
|
}
|
|
|
|
cap.setResize(small_dims[0], small_dims[1]);
|
|
|
|
frame = (char *) std::malloc(cap.get_dst_buf_size());
|
|
|
|
old = (char *) std::malloc(cap.get_dst_buf_size());
|
|
|
|
alloc = true;
|
|
|
|
|
|
|
|
memset(old, 0, sizeof(*old));
|
|
|
|
|
2022-02-22 08:13:17 +00:00
|
|
|
printf("\u001b[0;0H\u001b[48;2;0;0;0m");
|
|
|
|
for (int i = 0; i < curr_w * curr_h; i++) printf(" ");
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
int ret = cap.get_frame(small_dims[0], small_dims[1], frame);
|
|
|
|
|
2022-02-23 03:30:02 +00:00
|
|
|
stop = std::chrono::steady_clock::now();
|
2022-02-25 04:18:28 +00:00
|
|
|
elapsed = (int) std::chrono::duration_cast<std::chrono::microseconds>(stop - videostart).count();
|
|
|
|
int frame_time = (int) std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
|
2022-02-23 03:30:02 +00:00
|
|
|
start = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
total_time = elapsed;
|
|
|
|
avg_fps = (double) count * 1000000.0 / (double) total_time;
|
|
|
|
frametimes.push(frame_time);
|
|
|
|
frame10_time += frame_time;
|
|
|
|
if (frametimes.size() > 10) {
|
|
|
|
frame10_time -= frametimes.front();
|
|
|
|
frametimes.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curr_frame * period - elapsed > 0)
|
2022-02-25 04:18:28 +00:00
|
|
|
std::this_thread::sleep_until(
|
|
|
|
std::chrono::microseconds(curr_frame * period - elapsed - frame10_time / frametimes.size()) +
|
|
|
|
stop);
|
2022-02-22 00:21:11 +00:00
|
|
|
else {
|
|
|
|
skip = (double) elapsed / (double) period - (double) curr_frame;
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int i = 0; i < std::floor(skip); i++) ret = cap.get_frame(small_dims[0], small_dims[1], frame);
|
2022-02-22 00:21:11 +00:00
|
|
|
dropped += std::floor(skip);
|
|
|
|
curr_frame += std::floor(skip);
|
2022-02-25 04:18:28 +00:00
|
|
|
std::this_thread::sleep_until(
|
|
|
|
std::chrono::microseconds(curr_frame * period - frame10_time / frametimes.size()) + videostart);
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:13:17 +00:00
|
|
|
printf("\u001b[%d;%dH\u001b[48;2;0;0;0;38;2;255;255;255m fps: %5.2f | avg_fps: %5.2f | print: %6.2fms | dropped: %5d | curr_frame: %5d ",
|
2022-02-22 00:21:11 +00:00
|
|
|
msg_y, 0, (double) frametimes.size() * 1000000.0 / frame10_time, avg_fps,
|
|
|
|
(double) printing_time / 1000.0, dropped, curr_frame);
|
2022-02-22 16:11:51 +00:00
|
|
|
prevpixelbg[0] = 1000;
|
|
|
|
prevpixelbg[1] = 1000;
|
|
|
|
prevpixelbg[2] = 1000;
|
|
|
|
prevpixel[0] = 1000;
|
|
|
|
prevpixel[1] = 1000;
|
|
|
|
prevpixel[2] = 1000;
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
// If the frame is empty, break immediately
|
2022-02-25 04:18:28 +00:00
|
|
|
if (ret < 0) {
|
2022-02-22 00:21:11 +00:00
|
|
|
printf("\u001b[0mError reading video stream or file\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = -1;
|
|
|
|
c = -1;
|
2022-02-25 04:18:28 +00:00
|
|
|
char *row[CHAR_Y];
|
|
|
|
char *oldrow[CHAR_Y];
|
|
|
|
for (int ay = 0; ay < cap.get_height() / sy; ay++) {
|
|
|
|
for (int x = 0; x < cap.get_width() / sx; x++) {
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++) {
|
2022-02-25 04:18:28 +00:00
|
|
|
row[i] = frame + (ay * sy + i * skipy) * 3 * cap.get_width();
|
|
|
|
oldrow[i] = old + (ay * sy + i * skipy) * 3 * cap.get_width();
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++)
|
|
|
|
for (int j = 0; j < CHAR_X; j++)
|
2022-02-22 00:21:11 +00:00
|
|
|
for (int k = 0; k < 3; k++)
|
2022-02-25 04:18:28 +00:00
|
|
|
pixel[i][j][k] = (unsigned char) (*(row[i] + (x * sx + j * skipx) * 3 + k));
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
diff = 0;
|
|
|
|
if (refresh) {
|
|
|
|
diff = 255;
|
2022-02-22 15:46:23 +00:00
|
|
|
} else {
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++)
|
|
|
|
for (int j = 0; j < CHAR_X; j++)
|
2022-02-22 15:46:23 +00:00
|
|
|
for (int k = 0; k < 3; k++)
|
2022-02-25 04:18:28 +00:00
|
|
|
diff = std::max(diff, std::abs(
|
|
|
|
(unsigned char) (*(oldrow[i] + (x * sx + j * skipx) * 3 + k)) -
|
|
|
|
pixel[i][j][k]));
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (diff >= diffthreshold) {
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int &case_it: cases) case_it = 0;
|
2022-02-22 15:46:23 +00:00
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
for (int case_it = 0; case_it < sizeof(cases) / sizeof(cases[0]); case_it++) {
|
|
|
|
min_fg = 256;
|
|
|
|
min_bg = 256;
|
|
|
|
max_fg = 0;
|
|
|
|
max_bg = 0;
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++)
|
|
|
|
for (int j = 0; j < CHAR_X; j++) {
|
|
|
|
if (pixelmap[case_it][i * CHAR_X + j]) {
|
2022-02-22 15:46:23 +00:00
|
|
|
min_fg = std::min(min_fg, pixel[i][j][k]);
|
|
|
|
max_fg = std::max(max_fg, pixel[i][j][k]);
|
|
|
|
} else {
|
|
|
|
min_bg = std::min(min_bg, pixel[i][j][k]);
|
|
|
|
max_bg = std::max(max_bg, pixel[i][j][k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cases[case_it] = std::max(cases[case_it], std::max(max_fg - min_fg, max_bg - min_bg));
|
|
|
|
}
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
mindiff = 256;
|
|
|
|
case_min = 0;
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int case_it = 0; case_it < sizeof(cases) / sizeof(cases[0]); case_it++) {
|
2022-02-22 15:46:23 +00:00
|
|
|
if (cases[case_it] < mindiff) {
|
|
|
|
case_min = case_it;
|
|
|
|
mindiff = cases[case_it];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shapechar = characters[case_min];
|
2022-02-22 00:21:11 +00:00
|
|
|
|
|
|
|
diffbg = 0;
|
|
|
|
diffpixel = 0;
|
|
|
|
bgsame = false;
|
|
|
|
pixelsame = false;
|
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
int bg_count = 0, fg_count = 0;
|
2022-02-25 04:18:28 +00:00
|
|
|
pixelchar[k] = 0;
|
|
|
|
pixelbg[k] = 0;
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++)
|
|
|
|
for (int j = 0; j < CHAR_X; j++) {
|
|
|
|
if (pixelmap[case_min][i * CHAR_X + j]) {
|
2022-02-22 15:46:23 +00:00
|
|
|
pixelchar[k] += pixel[i][j][k];
|
|
|
|
fg_count++;
|
|
|
|
} else {
|
|
|
|
pixelbg[k] += pixel[i][j][k];
|
|
|
|
bg_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pixelchar[k] /= fg_count;
|
|
|
|
pixelbg[k] /= bg_count;
|
|
|
|
diffbg = std::max(diffbg, std::abs(pixelbg[k] - prevpixelbg[k]));
|
|
|
|
diffpixel = std::max(diffpixel, std::abs(pixelchar[k] - prevpixel[k]));
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
if (diffbg < CHANGE_THRESHOLD) {
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int k = 0; k < 3; k++) pixelbg[k] = prevpixelbg[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
bgsame = true;
|
|
|
|
} else
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int k = 0; k < 3; k++) prevpixelbg[k] = pixelbg[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
if (diffpixel < CHANGE_THRESHOLD) {
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int k = 0; k < 3; k++) pixelchar[k] = prevpixel[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
pixelsame = true;
|
|
|
|
} else
|
2022-02-25 04:18:28 +00:00
|
|
|
for (int k = 0; k < 3; k++) prevpixel[k] = pixelchar[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
|
|
|
|
for (int k = 0; k < 3; k++)
|
2022-02-23 02:47:23 +00:00
|
|
|
for (int i = 0; i < CHAR_Y; i++)
|
|
|
|
for (int j = 0; j < CHAR_X; j++) {
|
|
|
|
if (pixelmap[case_min][i * CHAR_X + j])
|
2022-02-25 04:18:28 +00:00
|
|
|
*(oldrow[i] + (x * sx + j * skipx) * 3 + k) = (char) pixelchar[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
else
|
2022-02-25 04:18:28 +00:00
|
|
|
*(oldrow[i] + (x * sx + j * skipx) * 3 + k) = (char) pixelbg[k];
|
2022-02-22 15:46:23 +00:00
|
|
|
}
|
2022-02-22 00:21:11 +00:00
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
if (r != ay || c != x) {
|
|
|
|
printf("\u001b[%d;%dH", ay, x);
|
2022-02-22 00:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bgsame && !pixelsame)
|
|
|
|
printf("\u001b[48;2;%d;%d;%d;38;2;%d;%d;%dm%s", pixelbg[2], pixelbg[1], pixelbg[0],
|
|
|
|
pixelchar[2], pixelchar[1], pixelchar[0], shapechar);
|
|
|
|
else if (!bgsame)
|
|
|
|
printf("\u001b[48;2;%d;%d;%dm%s", pixelbg[2], pixelbg[1], pixelbg[0], shapechar);
|
|
|
|
else if (!pixelsame)
|
2022-02-22 15:46:23 +00:00
|
|
|
printf("\u001b[38;2;%d;%d;%dm%s", pixelchar[2], pixelchar[1], pixelchar[0], shapechar);
|
|
|
|
else
|
|
|
|
printf("%s", shapechar);
|
2022-02-22 00:21:11 +00:00
|
|
|
|
2022-02-22 15:46:23 +00:00
|
|
|
r = ay;
|
2022-02-22 00:21:11 +00:00
|
|
|
c = x + 1;
|
|
|
|
if (c == curr_w) {
|
|
|
|
c = 0;
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refresh = false;
|
2022-02-23 03:30:02 +00:00
|
|
|
printtime = std::chrono::steady_clock::now();
|
2022-02-22 00:21:11 +00:00
|
|
|
fflush(stdout);
|
|
|
|
|
2022-02-25 04:18:28 +00:00
|
|
|
printing_time = (int) std::chrono::duration_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::steady_clock::now() - printtime).count();
|
2022-02-22 00:21:11 +00:00
|
|
|
total_printing_time += printing_time;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("\u001b[0mfile not found\n");
|
|
|
|
fflush(stdout);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
terminateProgram(0);
|
|
|
|
return 0;
|
2022-02-23 02:47:23 +00:00
|
|
|
}
|