-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.hpp
213 lines (181 loc) · 4.64 KB
/
Image.hpp
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
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef ncImage
#define ncImage
#include "nncurses/nncurses.hpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <unordered_map>
#include <string>
#include <array>
#include <cmath>
using std::string,std::array,std::unordered_map,std::out_of_range;
using cv::Mat,cv::Size,cv::Scalar,cv::VideoCapture,nc::Stopwatch;
template<size_t N>
struct std::hash<std::array<uint8_t, N>> {
auto operator() (const std::array<uint8_t, N>& key) const {
std::hash<uint8_t> hasher;
size_t result = 0;
for(size_t i = 0; i < N; ++i) {
result = result * 31 + hasher(key[i]); // ??
}
return result;
}
};
namespace nc{
Mat HexToXterm(uint8_t greynessTresh,uint8_t brightnessTresh,bool gsBlack,Mat rgbImg){
int height=rgbImg.rows;
int width=rgbImg.cols;
Mat xtermImage=Mat(height,width,CV_8UC1,Scalar(0));
for (int row=0;row<height;row++){
uchar* resizedBPtr = rgbImg.ptr<uchar>(row);
uchar* resizedGPtr = resizedBPtr+1;
uchar* resizedRPtr = resizedBPtr+2;
uchar* xtermPtr = xtermImage.ptr<uchar>(row);
for (int col=0;col<width;col++){
int colIndex=col*3;
xtermPtr[col]=approxXt(gsBlack,greynessTresh,brightnessTresh,array<int16_t,3>{resizedRPtr[colIndex],resizedGPtr[colIndex],resizedBPtr[colIndex]});
}
}
return xtermImage;
}
class Image{ //AAHGVGVNHSG
public:
Mat image;
uint8_t greynessTresh;
uint8_t brightnessTresh;
bool gsBlack;
bool converted=false;
int height,width;
int rHeight,rWidth;
int tHeight,tWidth;
Mat xtermImage;
const string character=blocks[0b1100];
Image(uint8_t greynessTresh,uint8_t brightnessTresh,bool gsBlack,Mat image):
image(image),
greynessTresh(greynessTresh),
brightnessTresh(brightnessTresh),
gsBlack(gsBlack)
{
if (image.empty()) throw -1;
height=image.rows;
width=image.cols;
}
void proccess(int nheight,int nwidth){
rHeight=nheight;
rWidth=nwidth;
Size newSize(rWidth,rHeight);
Mat resized;
resize(image,resized,newSize);
xtermImage=HexToXterm(greynessTresh,brightnessTresh,gsBlack,resized);
converted=true;
tHeight=(rHeight/2)+(rHeight%2);
tWidth=rWidth;
}
void proccess(Screen* screen){ //fill as much as possible while mantaining ratio
int maxHeight=screen->rows*2;
int maxWidth=screen->cols;
double transformX=(double)width/maxWidth;
double transformY=(double)height/maxHeight;
double transform= transformY>transformX ? transformY : transformX;
proccess(height/transform,width/transform);
}
void render(Screen* screen,int startX,int startY){
if (converted){
for (int row=0;row<rHeight/2;row++){
uchar* topPtr = xtermImage.ptr<uchar>(row*2);
uchar* btmPtr = xtermImage.ptr<uchar>(row*2+1);
for (int col=0;col<rWidth;col++){
screen->screen[row+startY][col+startX]=Texture(
character,
Style(
topPtr[col],
btmPtr[col],
0
)
);
}
}
if ( height%2==1 ){
uchar* ptr = xtermImage.ptr<uchar>(height-1);
for (int col=0;col<rWidth;col++){
screen->screen[startY+rHeight-1][startX+col]=Texture(
character,
Style(
ptr[col],
-1,0
)
);
}
}
}else{
throw -1;
}
}
void render(Screen* scr){
render(scr,midOfst(scr->cols,tWidth), midOfst(scr->rows,tHeight));
}
};
class VideoTimer{ //SKRERERRERERERRERE
public:
VideoCapture& video;
double fps;
double frameTime;
double sleepTime;
bool stopped;
Stopwatch timer;
VideoTimer(VideoCapture& video):
video(video),
fps(video.get(cv::CAP_PROP_FPS)),
frameTime(((1.0f/fps)*1000.0f)-5)
{}
int frame(){
return video.get(cv::CAP_PROP_POS_FRAMES);
}
double ms(){
return video.get(cv::CAP_PROP_POS_MSEC);
}
void ms(double ms){
video.set(cv::CAP_PROP_POS_MSEC,ms);
timer.othTime=ms;
timer.stopped=true;
}
void frame(int id){ //AAAAAAAAA
timer.othTime=((id/fps)*1000)+(fmod(id,fps)*frameTime);
video.set(cv::CAP_PROP_POS_MSEC,timer.othTime);
timer.stopped=true;
}
int frames(){
return video.get(cv::CAP_PROP_FRAME_COUNT);
}
void start(){
timer.start();
}
void restart(){
timer.reset();
}
void stop(){
timer.stop();
}
double time(){
return timer.time();
}
void sync(){ //>0 is too fast, <0 is too slow
double currTime=timer.time();
sleepTime=ms()-currTime;
sleepTime= sleepTime>0 ? sleepTime : 0;
int sleepTimeMs=sleepTime;
int sleepTimeMcrs=(sleepTime-(int)sleepTime)*1000;
sleep(0,sleepTimeMs,sleepTimeMcrs,0);
}
};
}
#undef string
#undef array
#undef unordered_map
#undef Mat
#undef Size
#undef Scalar
#undef VideoCapture
#undef Image
#endif