-
Notifications
You must be signed in to change notification settings - Fork 15
/
stiv.c
168 lines (152 loc) · 3.87 KB
/
stiv.c
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
/* tiv - terminal image viewer - copyleft 2013-2015 - pancake */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define XY(b,x,y) ( b+((y)*(w*3))+(x*3) )
#define ABS(x) (((x)<0)?-(x):(x))
#define POND(x,y) (ABS((x)) * (y))
void (*renderer)(const unsigned char*, const unsigned char *);
static int reduce8 (int r, int g, int b) {
int colors_len = 8;
int select = 0;
int odistance = -1;
int i, k = 1;
int colors[][3] = {
{ 0x00,0x00,0x00 }, // black
{ 0xd0,0x10,0x10 }, // red
{ 0x10,0xe0,0x10 }, // green
{ 0xf7,0xf5,0x3a }, // yellow
{ 0x10,0x10,0xf0 }, // blue // XXX
{ 0xfb,0x3d,0xf8 }, // pink
{ 0x10,0xf0,0xf0 }, // turqoise
{ 0xf0,0xf0,0xf0 }, // white
};
r /= k; r *= k;
g /= k; g *= k;
b /= k; b *= k;
// B&W
if (r<30 && g<30 && b<30) return 0;
if (r>200&& g>200&& b>200) return 7;
odistance = -1;
for (i = 0; i<colors_len; i++) {
int distance =
POND (colors[i][0]-r, r)
+ POND (colors[i][1]-g, g)
+ POND (colors[i][2]-b, b);
if (odistance == -1 || distance < odistance) {
odistance = distance;
select = i;
}
}
return select;
}
void
render_ansi(const unsigned char *c, const unsigned char *d) {
int fg = 0;
int color = reduce8 (c[0], c[1], c[2]);
if (color == -1)return;
//if (c[0]<30 && c[1]<30 && c[2]<30) fg = 1;
printf ("\x1b[%dm", color+(fg?30:40));
}
static int rgb(int r, int g, int b) {
if (r<0) r=0; if (r>255) r = 255;
if (g<0) g=0; if (g>255) g = 255;
if (b<0) b=0; if (b>255) b = 255;
r = (int)(r/50.6);
g = (int)(g/50.6);
b = (int)(b/50.6);
return 16 + (r*36) + (g*6) + b;
}
void
render_256(const unsigned char *c, const unsigned char *d) {
printf ("\x1b[%d;5;%dm", 38, rgb (c[0], c[1], c[2]));
printf ("\x1b[%d;5;%dm", 48, rgb (d[0], d[1], d[2]));
}
void
render_rgb(const unsigned char *c, const unsigned char *d) {
printf ("\x1b[38;2;%d;%d;%dm", c[0], c[1], c[2]);
printf ("\x1b[48;2;%d;%d;%dm", d[0], d[1], d[2]);
}
void
render_greyscale(const unsigned char *c, const unsigned char *d) {
int color, color1, color2, k;
color1 = (c[0]+c[1]+c[2]) / 3;
color2 = (d[0]+d[1]+d[2]) / 3;
k = 231 + ((int)((float)color1/10.3));
if (k<232) k = 232;
printf ("\x1b[%d;5;%dm", 48, k); // bg
k = 231 + ((int)((float)color2/10.3));
if (k<232) k = 232;
printf ("\x1b[%d;5;%dm", 38, k); // fg
}
void
render_ascii(const unsigned char *c, const unsigned char *d) {
const char *pal = " `.,-:+*%$#";
int idx, pal_len = strlen (pal);
float p = (c[0]+c[1]+c[2])/3;
float q = (d[0]+d[1]+d[2])/3;
idx = ((p+q)/2) / (255/pal_len);
if (idx >= pal_len) idx = pal_len-1;
printf ("%c", pal[idx]);
}
static void dorender (unsigned char *buf, int len, int w, int h) {
unsigned char *c, *d;
int x, y;
for (y=0; y<h; y+=2) {
for (x=0; x<w; x++) {
c = XY (buf, x, y);
d = XY (buf, x, y+1);
if (d> (buf+len)) break;
renderer (c, d);
if (renderer != render_ascii)
render_ascii (c, d);
}
printf ((renderer==render_ascii)?"\n":"\x1b[0m\n");
}
}
static void selectrenderer(const char *arg) {
switch (arg[0]) {
case 'a':
renderer = (arg[1] == 'n')?
render_ansi: render_ascii;
break;
case 'g': renderer = render_greyscale; break;
case '2': renderer = render_256; break;
default:
renderer = render_rgb;
break;
}
}
#ifndef MAIN
int
main(int argc, const char **argv) {
unsigned char *buf, *c, *d;
int n, x, y, w, h, imgsz, readsz;
if (argc<3) {
printf ("stiv . suckless terminal image viewer\n");
printf ("Usage: stiv [width] [height] [ascii|ansi|grey|256|rgb] < rgb24\n");
return 1;
}
w = atoi (argv[1]);
h = atoi (argv[2]);
if (argc>3) {
selectrenderer (argv[3]);
} else renderer = render_rgb;
if (w<1 || h<1) {
printf ("Invalid arguments\n");
return 1;
}
imgsz = w * h * 3;
buf = malloc (imgsz);
readsz = 0;
do {
n = read(0, buf+readsz, imgsz);
if (n<1) break;
readsz += n;
} while (readsz < imgsz);
dorender (buf, readsz, w, h);
free (buf);
return 0;
}
#endif