aboutsummaryrefslogtreecommitdiff
path: root/view.c
blob: f028c82cdc90be463dbcf2d39011de6b8a94f386 (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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include <unistd.h>
#include <termios.h>

#include <glib.h>
#include <gio/gio.h>
#include <gio/gunixinputstream.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixdata.h>

static FILE* tty;
static int tty_fd;
static struct termios tty_tio;

static int char_width = 8;
static int char_height = 15;
static int window_columns = 25;
static int window_rows = 80;
static int window_width = 8 * 80;
static int window_height = 15 * 25;

static bool init_terminal(int fd);
static void close_terminal();

static bool init_terminal(int fd)
{
	const char *name_of_tty = ttyname(fd);
	if (!name_of_tty) {
		return FALSE;
	}

	tty = fopen(name_of_tty, "r+");
	if (tty == NULL) {
		g_printerr("Cannot open terminal %s", name_of_tty);
		return FALSE;
	}
	tty_fd = fileno(tty);

	tcgetattr(tty_fd, &tty_tio);
	struct termios tio = tty_tio;

	tio.c_lflag &= ~(ICANON | ECHO);
	tio.c_cc[VMIN] = 4;
	tio.c_cc[VTIME] = 1;
	tcsetattr(tty_fd, TCSADRAIN, &tio);

	// Try to get window width and height in pixels using a xterm command
	fprintf(tty, "\033[14;t");
	fflush(tty);

	if (fscanf(tty, "\033[4;%d;%dt", &window_height, &window_width) != 2) {
		close_terminal();
		return FALSE;
	}

	// Also use a xterm command to get the window size in chars,
	// (could also use termios...)
	fprintf(tty, "\033[18;t");
	fflush(tty);

	if (fscanf(tty, "\033[8;%d;%dt", &window_rows, &window_columns) != 2) {
		close_terminal();
		return FALSE;
	}

	// Now calculate char height & width based on the above data
	char_width = window_width / window_columns;
	char_height = window_height / window_rows;

	return TRUE;
}

static void close_terminal()
{
	tcsetattr(tty_fd, TCSADRAIN, &tty_tio);
	fclose(tty);
}

static gboolean view_pixbuf(GdkPixbuf *pixbuf)
{
	gint width = gdk_pixbuf_get_width(pixbuf);
	gint height = gdk_pixbuf_get_height(pixbuf);	

	double scale = 1.0;
	if (width > window_width) {
		scale *= (double)window_width / width;
		width = width * scale;
		height = height * scale;
	}
	if (height > window_height || FALSE) {
		scale *= (double)window_height / height;
		width = width * scale;
		height = height * scale;
	}

	GdkPixbuf *scaled;

	if (scale != 1.0) {
		scaled = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_BILINEAR);
	} else {
		scaled = GDK_PIXBUF(g_object_ref(pixbuf));
	}

	GdkPixdata pixdata;
	gpointer pixdata_p = gdk_pixdata_from_pixbuf(&pixdata, scaled, TRUE);

	if (pixdata_p == NULL) {
		g_object_unref(scaled);
		return FALSE;
	}

	guint size;
	guint8 *data = gdk_pixdata_serialize(&pixdata, &size);
	gchar *str = g_base64_encode(data, size);

	fprintf(tty, "\033]v;%s\007", str);

	int rows = ceil(height / (double)char_height);
	while (rows > 0) {
		fprintf(tty, "\n");
		rows--;
	}

	fflush(tty);
	
	g_free(str);
	g_free(data);
	g_free(pixdata.pixel_data);
	g_object_unref(scaled);

	return TRUE;
}


int main(int argc, char * argv[])
{
	g_type_init();

	GError *error = NULL;

	if (!isatty(STDOUT_FILENO)) {
		g_printerr("Refusing to display image on not-terminal\n");
		return EXIT_FAILURE;
	}

	if (!init_terminal((STDOUT_FILENO))) {
		g_printerr("Refusing to display image on non-graphical terminal\n");
		return EXIT_FAILURE;
	}

	if (argc > 1) {
		int i;
		for (i = 1; i < argc; i++) {
			GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[i], &error);
			if (pixbuf == NULL) {
				g_printerr("Failed to open image '%s': %s\n", argv[i], error->message);
				close_terminal();
				return EXIT_FAILURE;
			}
			if (!view_pixbuf(pixbuf)) {
				g_printerr("Failed to view image '%s'\n", argv[i]);
				close_terminal();
				return EXIT_FAILURE;
			}
			g_object_unref(pixbuf);
		}
	} else {
		GInputStream *input = g_unix_input_stream_new(STDIN_FILENO, FALSE);
		GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(input, NULL, &error);
		if (pixbuf == NULL) {
			g_printerr("Failed to open stdin image: %s\n", error->message);
			close_terminal();
			return EXIT_FAILURE;
		}
		if (!view_pixbuf(pixbuf)) {
			g_printerr("Failed to view stdin image\n");
			close_terminal();
			return EXIT_FAILURE;
		}
		g_object_unref(pixbuf);
	}

	close_terminal();
	return EXIT_SUCCESS;
}