aboutsummaryrefslogtreecommitdiff
path: root/core/imgv.c
blob: c9f9efb02f2456a95685725c623ed45874488f4f (plain)
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
#include "../lib/io/io.h"
#include "../lib/fb/fb.h"
#include "../lib/tctl/tctl.h"
#include "../lib/aec/aec.h"
#include "../lib/ff/ff.h"
#include "../lib/arg/arg.h"
#include "../lib/cstr/cstr.h"

u8 centerx = 0;
u8 centery = 0;
i64 width = -1;
i64 height = -1;
u8 fill = 0;
u8 only_print = 0;


void set_only_print()
{
	only_print = 1;
}


void set_width(const char *buf)
{
	width = cstr_to_i64(buf);
}


void set_height(const char *buf)
{
	height = cstr_to_i64(buf);
}


void set_fill()
{
	fill = 1;
}


void set_centerx()
{
	centerx = 1;
}


void set_centery()
{
	centery = 1;
}


void draw_image(canvas_t canvas, image_t *img)
{
	canvas_pixel_t p;
	u64 offset_x = 0;
	u64 offset_y = 0;
	float scalex = 1;
	float scaley = 1;

	if (width != -1) {
		scalex = width;
		scalex /= img->width;
	}

	if (height != -1) {
		scaley = height;
		scaley /= img->height;
	}

	if (centerx) {
		offset_x = (canvas.width - img->width) / 2;
	}

	if (centery) {
		offset_y = (canvas.height - img->height) / 2;
	}

	for (u32 y = 0; y < img->height; ++y) {
		for (u32 x = 0; x < img->width; ++x) {
			p = image_pixel_to_canvas_pixel(img->data[x + y * img->width]);
			put_pixel(canvas, (x * scalex) + offset_x, (y * scaley) + offset_y, p);
		}
	}
}


void black_clear(canvas_t canvas)
{
	canvas_pixel_t p = { 0x00, 0x00, 0x00 };
	for (int x = 0; x < canvas.width; ++x) {
		for (int y = 0; y < canvas.height; ++y) {
			put_pixel(canvas, x, y, p);
		}
	}
}


int main(int argc, const char **argv)
{
	arg_register_flag("-p", &set_only_print);
	arg_register_flag("-c", &set_centerx);
	arg_register_flag("-z", &set_centery);
	arg_register_arg("-h", &set_height);
	arg_register_arg("-w", &set_width);
	arg_parse_arg(argc, argv);

	canvas_t canvas = open_framebuffer();
	termios_t term;
	termios_t orig;

	image_t *img = new_image_from_fd(STDIN_FD);

	tcgetattr(STDOUT_FD, &term);
	orig = term;
	setcanonical(&term, 0);
	setecho(&term, 0);
	cursor_enabled(0);
	tcsetattr(STDOUT_FD, &term);

	if (!img) {
		free_image(img);
		close_framebuffer(canvas);
		cursor_enabled(1);
		tcsetattr(STDOUT_FD, &orig);
		wf(STDERR_FD, "error: cannot read image\n");
		return -1;
	}

	if (width == -1) {
		width = img->width;
	}

	if (height == -1) {
		height = img->width;
	}

	char key = 0;

	while (key != 'q') {
		draw_image(canvas, img);
		if (!only_print)
			read(STDERR_FD, &key, 1);
		else
			break;
	}

	if (!only_print)
		black_clear(canvas);

	free_image(img);
	close_framebuffer(canvas);
	cursor_enabled(1);
	tcsetattr(STDOUT_FD, &orig);

	if (!only_print) {
		move_cursor(1, 1);
		clear_screen();
	} else {
		move_cursor(0, height / 14);
	}

	return 0;
}