Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snem/drawansi.h
28550 views
1
#ifndef uint16
2
#define uint16 unsigned short
3
#endif
4
#include <stdio.h>
5
// COLOR_MODE 0:ansi 1:grayscale 2:xterm-color256 3: konsole 24bit
6
7
#define COLOR_MODE 2
8
9
// palette theme
10
//#define LINUX_CONSOLE
11
//#define PCMAN
12
#define XTERM_COLOR
13
14
15
#define COLS 80
16
#define ROWS 30
17
18
#define TRUE 1
19
#define FALSE 0
20
#if COLOR_MODE <= 1
21
static char gray_string[]=" .:-=+*#%$";
22
#endif
23
#if COLOR_MODE == 0
24
static unsigned ansi_color_table[65536][3];
25
static int ansi_color_table_ready=FALSE;
26
#ifdef PCMAN
27
static unsigned int dct[16][3] = { //default color table
28
//Darker color
29
{0,0,0}, //0;30m Black
30
{128,0,0}, //0;31m Dark red
31
{0,128,0}, //0;32m Dark green
32
{128,128,0}, //0;33m Brown
33
{0,0,128}, //0;34m Dark blue
34
{128,0,128}, //0;35m Dark magenta
35
{0,128,128}, //0;36m Dark cyan
36
{192,192,192}, //0;37m Light gray
37
//Bright color
38
{128,128,128}, //1;30m Gray
39
{255,0,0}, //1;31m Red
40
{0,255,0}, //1;32m Green
41
{255,255,0}, //1;33m Yellow
42
{0,0,255}, //1;34m Blue
43
{255,0,255}, //1;35m Magenta
44
{0,255,255}, //1;36m Cyan
45
{255, 255,255} //1;37m White
46
};
47
#define LIGHT_ADJUST(x) ((x)/2)
48
#endif
49
#ifdef XTERM_COLOR
50
static unsigned int dct[16][3] = {
51
{0,0,0}, {205,0,0}, {0,205,0}, {205,205,0},
52
{0,0,238}, {205,0,205}, {0,205,205}, {229,229,229},
53
{127,127,127}, {0xff,0,0}, {0,255,0}, {0xff,0xff,0},
54
{92,92,0xff}, {0xff,0,0xff}, {0,0xff,0xff}, {0xff,0xff,0xff}};
55
#define LIGHT_ADJUST(x) ((x)*10/16)
56
#endif
57
#ifdef TANGO_COLOR
58
static unsigned int dct[16][3] = {
59
{0,0,0}, {205,0,0}, {0,205,0}, {205,205,0},
60
{0,0,238}, {205,0,205}, {0,205,205}, {229,229,229},
61
{127,127,127}, {0xff,0,0}, {0,255,0}, {0xff,0xff,0},
62
{92,92,0xff}, {0xff,0,0xff}, {0,0xff,0xff}, {0xff,0xff,0xff}};
63
#define LIGHT_ADJUST(x) ((x)*9/13)
64
#endif
65
#ifdef LINUX_CONSOLE
66
static unsigned int dct[16][3] = {
67
{0,0,0}, {0xaa,0,0}, {0,0xaa,0}, {0xaa,0x55,0},
68
{0,0,0xaa}, {0xaa,0,0xaa}, {0,0xaa,0xaa}, {0xaa,0xaa,0xaa},
69
{0x55,0x55,0x55}, {0xff,0x55,0x55}, {0x55,0xff,0x55}, {0xff,0xff,0x55},
70
{0x55,0x55,0xff}, {0xff,0x55,0xff}, {0x55,0xff,0xff}, {0xff,0xff,0xff}};
71
#define LIGHT_ADJUST(x) ((x)*7/13)
72
#endif
73
static void init_ansi_color_table(){
74
unsigned int c;
75
for(c=0;c<65536;c++){
76
int best_fg=0, best_bg=0, best_level,min_diff=0xffffff;
77
int fg,bg,level, diff,r,g,b,rmean;
78
int rgb[3], rgb2[3];
79
int i;
80
rgb[0]=((c>>11)&0x1f)<<3;
81
rgb[1]=((c>>5)&0x3f)<<2;
82
rgb[2]=(c&0x1f)<<3;
83
for(fg=0;fg<16;fg++)
84
for(bg=0;bg<8;bg++)
85
for(level=0;level<10;level++){
86
for(i=0;i<3;i++)
87
rgb2[i]=(dct[fg][i]*level+dct[bg][i]*(18-level))/36- LIGHT_ADJUST(rgb[i]);
88
rmean = (2*rgb[0]+rgb2[0])/2;
89
r=rgb2[0];g=rgb2[1];b=rgb2[2];
90
diff=(((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8);
91
if(diff < min_diff){
92
best_level=level;
93
best_fg=fg;
94
best_bg=bg;
95
min_diff=diff;
96
}
97
}
98
ansi_color_table[c][0]=best_fg;
99
ansi_color_table[c][1]=best_bg;
100
ansi_color_table[c][2]=best_level;
101
}
102
ansi_color_table_ready=TRUE;
103
}
104
105
#endif
106
static void print_color(uint32 c, uint32 c2){
107
#if COLOR_MODE>=1
108
unsigned int r,g,b, level;
109
r=((c>>11)&0x1f)<<3;
110
g=((c>>5)&0x3f)<<2;
111
b=(c&0x1f)<<3;
112
#endif
113
#if COLOR_MODE >=2
114
#if COLOR_MODE == 2
115
level=16+(r*6/256)*36+(g*6/256)*6+(b*6/256);
116
printf("\033[48;5;%d;", level);
117
#else
118
printf("\033[48;2;%d;%d;%d;", r,g,b);
119
#endif
120
r=((c2>>11)&0x1f)<<3;
121
g=((c2>>5)&0x3f)<<2;
122
b=(c2&0x1f)<<3;
123
#if COLOR_MODE == 2
124
level=16+(r*6/256)*36+(g*6/256)*6+(b*6/256);
125
printf("38;5;%dm\xe2\x96\x84", level);
126
#else
127
printf("38;2;%d;%d;%dm\xe2\x96\x84", r, g,b);
128
#endif
129
#endif
130
#if COLOR_MODE == 1
131
level=(((r * 0.30) + (g * 0.59) + (b * 0.11))/256.0*10.0);
132
if(level>9) level=9;
133
printf("%c",gray_string[level]);
134
#endif
135
#if COLOR_MODE == 0
136
static unsigned int last_fg, last_bg;
137
if(!ansi_color_table_ready) init_ansi_color_table();
138
if(c2 || last_fg!=ansi_color_table[c][0] || last_bg!=ansi_color_table[c][1])
139
printf("\033[%d;3%d;4%dm", ansi_color_table[c][0]>>3,
140
ansi_color_table[c][0]&7,
141
ansi_color_table[c][1]);
142
printf("%c", gray_string[ansi_color_table[c][2]]);
143
last_fg=ansi_color_table[c][0];
144
last_bg=ansi_color_table[c][1];
145
#endif
146
147
148
}
149
static uint32 RGB565FromRGB24(uint32 c){
150
unsigned r,g,b;
151
b=((c>>3)&0x1f);
152
g=((c>>10)&0x3f);
153
r=((c>>19)&0x1f);
154
return (r<<11)|(g<<5)|b;
155
156
}
157
#define RGB565FromRGB555(t) ( (((t)&0x1f)<<11) | (((t)&0x3e0)<<1) | (((t)&0x7c00)>>10) )
158
static unsigned get_average_color(int x1, int y1, int x2, int y2, void * data, int bpp, int pitch){
159
#if 0
160
unsigned char *bp=(unsigned char *)data+y1*pitch;
161
return RGB565FromRGB24(((uint32 *)bp)[x1]);
162
#else
163
int x,y;
164
unsigned int r=0,g=0,b=0;
165
unsigned color;
166
unsigned char *bp;
167
int size=(y2-y1)*(x2-x1);
168
if (size <=0) {y2=y1+1; x2=x1+1;size=1;}
169
for(y=y1;y<y2;y++){
170
bp=(unsigned char *)data+y*pitch;
171
for(x=x1;x<x2;x++){
172
color = RGB565FromRGB24(((uint32 *)bp)[x]);
173
r+=color>>11;
174
g+=(color>>5)&0x3f;
175
b+=color&0x1f;
176
}
177
}
178
r/=size;
179
g/=size;
180
b/=size;
181
return (r<<11)|(g<<5)|b;
182
#endif
183
}
184
static void drawansi (int width, int height, void *data, int bpp, int pitch)
185
{
186
unsigned c1,c2;
187
int x,y, xstep, ystep, xstart, xend, ystart, yend;
188
int cols=COLS, rows=ROWS;
189
xstep=width/cols;
190
ystep=height/rows;
191
xstart=(width-xstep*cols)/2;
192
ystart=(height-ystep*rows)/2;
193
xend=xstart+cols*xstep;
194
yend=ystart+rows*ystep;
195
printf("\033[H");
196
uint32 reset=1;
197
for(y=ystart;y<yend;y+=ystep)
198
{
199
if(y!=ystart) printf("\r\n");
200
for(x=xstart;x<xend;x+=xstep)
201
{
202
#if COLOR_MODE >=2
203
c1=get_average_color(x, y, x+xstep, y+ystep/2, data, bpp, pitch);
204
c2=get_average_color(x, y+ystep/2, x+xstep, y+ystep, data, bpp, pitch);
205
print_color(c1, c2);
206
#else
207
c1=get_average_color(x, y, x+xstep, y+ystep, data, bpp, pitch);
208
print_color(c1, reset);
209
reset=0;
210
#endif
211
}
212
213
}
214
printf("\033[0m");
215
fflush(stdout);
216
217
}
218
219