This repository was archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (90 loc) · 3.48 KB
/
main.cpp
File metadata and controls
114 lines (90 loc) · 3.48 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
#include <chrono>
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
bool is_video(const std::string& file_ext) {
if (file_ext == ".mp3") return true;
if (file_ext == ".mp4") return true;
if (file_ext == ".avi") return true;
return false;
}
bool is_image(const std::string& file_ext) {
if (file_ext == ".png") return true;
if (file_ext == ".jpg") return true;
if (file_ext == ".jpeg") return true;
if (file_ext == ".webp") return true;
return false;
}
char translate_pixel(const int pixel_brightness) {
const std::string ascii_chars = " `.-':_,^=;><+/!rc*#$Bg0MNWQ%&@";
const char ascii = ascii_chars[pixel_brightness * ascii_chars.length() / 256];
return ascii;
}
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: ./charify <FILE>" << std::endl;
return 1;
}
std::string working_dir = std::filesystem::current_path();
working_dir = working_dir.substr(0, working_dir.find_last_of("/\\"));
std::string file_name = argv[1];
std::string file_ext = file_name.substr(file_name.find_first_of('.'));
std::string file_path = working_dir + "/media/" + file_name;
struct stat sb{};
if (stat(&file_path[0], &sb) != 0) {
std::cerr << "Error: File " << argv[1] << " cannot be found in media folder." << std::endl;;
return 1;
}
struct winsize window{};
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window);
int display_width = window.ws_col;
int display_height = window.ws_row;
if (display_width < 75 || display_height < 25) {
std::cerr << "Error: Please use a larger terminal. Minimum resolution is 75 pixels by 25 pixels." << std::endl;
return 1;
}
if (is_video(file_ext)) {
cv::VideoCapture video(file_path);
double fps = video.get(cv::CAP_PROP_FPS);
int frame_duration_ms = static_cast<int>(1000 / fps);
cv::Mat frame, gray_frame, resized_frame;
while (true) {
video >> frame;
if (frame.empty()) {
break;
}
cv::cvtColor(frame, gray_frame, cv::COLOR_BGR2GRAY);
cv::resize(gray_frame, resized_frame, cv::Size(display_width, display_height), 0, 0, cv::INTER_LINEAR);
std::string ascii_frame;
for (int i = 0; i < display_height; i++) {
for (int j = 0; j < display_width; j++) {
ascii_frame += translate_pixel(resized_frame.at<uchar>(i, j));
}
ascii_frame += "\n";
}
system("clear");
std::cout << ascii_frame;
std::this_thread::sleep_for(std::chrono::milliseconds(frame_duration_ms));
}
} else if (is_image(file_ext)) {
cv::Mat image = cv::imread(file_path);
cv::Mat gray_image, resized_image;
cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);
cv::resize(gray_image, resized_image, cv::Size(display_width, display_height), 0, 0, cv::INTER_LINEAR);
std::string ascii_frame;
for (int i = 0; i < display_height; i++) {
for (int j = 0; j < display_width; j++) {
ascii_frame += translate_pixel(resized_image.at<uchar>(i, j));
}
ascii_frame += "\n";
}
std::cout << ascii_frame;
} else {
std::cerr << "Error: Unknown file extension." << std::endl;
return 1;
}
return 0;
}