-
Notifications
You must be signed in to change notification settings - Fork 11
/
aimage.c
104 lines (91 loc) · 2.24 KB
/
aimage.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
// berry-dm
// Copyright © 2015,2022 Yuichiro Nakada
// clang -Os -I .. -I ../3rd/ aviewer.c -o aviewer -lm
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/ioctl.h>
#include <time.h>
//#define USE_HALF_BLOCKS
//#include "termbox.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
//#define JEBP_IMPLEMENTATION
//#include "jebp.h"
#include "aimage.h"
#if 0
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define ICEIL(dividend, divisor) \
(((dividend) + ((divisor) - 1)) / (divisor))
void aviewer(char *name, int sx, int sy)
{
unsigned char *pixels;
int width, height, bpp, frames;
// pixels = stbi_load(name, &width, &height, &bpp, 4/*RGBA*/);
pixels = stbi_xload(name, &width, &height, &frames);
int rx = MAX(ICEIL(width, sx), 1);
int ry = MAX(ICEIL(height, sy), 1);
printf("%s %dx%d r[%d,%d] / Screen %d,%d\n", name, width, height, rx, ry, sx, sy);
// putImage(pixels, width, height, rx, ry, sx, sy);
printf("frames:%d\n", frames);
char screen[frames][sx*sy*14+11*sy+1];
for (int i=0; i<frames; i++) {
simage(screen[i], &pixels[width*height*4*i+2*i], width, height, rx, ry, sx, sy);
}
stbi_image_free(pixels);
for (int i=0; i<frames; i++) {
// ECLEAR();
ELOCATE(1, 1);
printf("%s", screen[i]);
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 50000000;
nanosleep(&req, NULL);
//usleep(10000);
}
}
#endif
void usage()
{
printf("Usage: `aviewer <input> [width height]`\n");
}
int main(int argc, char *argv[])
{
#ifdef H_TERMBOX
tb_init();
// tb_select_output_mode(TB_OUTPUT_TRUECOLOR);
tb_select_output_mode(TB_OUTPUT_256);
tb_clear();
int width, height;
width = tb_width();
height = tb_height();
#else
// get terminal size
int width, height;
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
if (0 < ws.ws_col && ws.ws_col == (size_t)ws.ws_col) {
width = ws.ws_col;
height = ws.ws_row;
}
}
#endif
if (argc < 2 || 4 < argc) {
usage();
} else if (argc == 2) {
aviewer(argv[1], width, height);
} else {
aviewer(argv[1], atoi(argv[2]), atoi(argv[3]));
}
#ifdef H_TERMBOX
while (1) {
struct tb_event ev;
int t = tb_peek_event(&ev, 10);
if (t == -1) break;
if (t == TB_EVENT_KEY) break;
}
tb_shutdown();
#endif
return 0;
}