forked from godspeed1989/fbv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
363 lines (308 loc) · 7.35 KB
/
main.c
File metadata and controls
363 lines (308 loc) · 7.35 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "config.h"
#include "fbv.h"
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#include <termios.h>
#include <signal.h>
#define PAN_STEPPING 20
static float opt_alpha = 0.f;
static int opt_stretch = 0;
static int opt_delay = 0;
static int opt_enlarge = 0;
static inline void do_rotate(struct image *i, int rot)
{
if(rot)
{
unsigned char *image, *alpha = NULL;
int t;
image = rotate(i->rgb, i->width, i->height, rot);
if(i->alpha)
alpha = alpha_rotate(i->alpha, i->width, i->height, rot);
if(i->do_free)
{
free(i->alpha);
free(i->rgb);
}
i->rgb = image;
i->alpha = alpha;
i->do_free = 1;
if(rot & 1)
{
t = i->width;
i->width = i->height;
i->height = t;
}
}
}
static inline void do_enlarge(struct image *i, int screen_width, int screen_height)
{
if((i->width > screen_width) || (i->height > screen_height))
return;
if((i->width < screen_width) || (i->height < screen_height))
{
int xsize = i->width, ysize = i->height;
unsigned char * image, * alpha = NULL;
if((i->height * screen_width / i->width) <= screen_height)
{
xsize = screen_width;
ysize = i->height * screen_width / i->width;
goto have_sizes;
}
if((i->width * screen_height / i->height) <= screen_width)
{
xsize = i->width * screen_height / i->height;
ysize = screen_height;
goto have_sizes;
}
return;
have_sizes:
image = simple_resize(i->rgb, i->width, i->height, xsize, ysize);
if(i->alpha)
alpha = alpha_resize(i->alpha, i->width, i->height, xsize, ysize);
if(i->do_free)
{
free(i->alpha);
free(i->rgb);
}
i->rgb = image;
i->alpha = alpha;
i->do_free = 1;
i->width = xsize;
i->height = ysize;
}
}
static inline void do_fit_to_screen(struct image *i, int screen_width, int screen_height, int cal)
{
if((i->width > screen_width) || (i->height > screen_height))
{
unsigned char * new_image, * new_alpha = NULL;
int nx_size = i->width, ny_size = i->height;
if((i->height * screen_width / i->width) <= screen_height)
{
nx_size = screen_width;
ny_size = i->height * screen_width / i->width;
}
else
{
nx_size = i->width * screen_height / i->height;
ny_size = screen_height;
}
if(cal)
new_image = color_average_resize(i->rgb, i->width, i->height, nx_size, ny_size);
else
new_image = simple_resize(i->rgb, i->width, i->height, nx_size, ny_size);
if(i->alpha)
new_alpha = alpha_resize(i->alpha, i->width, i->height, nx_size, ny_size);
if(i->do_free)
{
free(i->alpha);
free(i->rgb);
}
i->rgb = new_image;
i->alpha = new_alpha;
i->do_free = 1;
i->width = nx_size;
i->height = ny_size;
}
}
static inline void fsleep(float delay)
{
float integral;
float fractional = modff(delay, &integral);
struct timespec ts = {
.tv_sec = integral,
.tv_nsec = fractional * 1e9
};
nanosleep(&ts, NULL);
}
int show_image(char *filename)
{
int (*load)(char *, unsigned char *, unsigned char **, int, int);
unsigned char * image = NULL;
unsigned char * alpha = NULL;
int ret = 1;
int x_size, y_size, screen_width, screen_height;
int x_pan, y_pan, x_offs, y_offs;
float delay = opt_delay;
int transform_stretch = opt_stretch, transform_enlarge = opt_enlarge;
int transform_cal = (opt_stretch == 2);
int transform_rotation = 0;
struct image i;
#ifdef FBV_SUPPORT_PNG
if(fh_png_id(filename))
if(fh_png_getsize(filename, &x_size, &y_size) == FH_ERROR_OK)
{
load = fh_png_load;
goto identified;
}
#endif
#ifdef FBV_SUPPORT_JPEG
if(fh_jpeg_id(filename))
if(fh_jpeg_getsize(filename, &x_size, &y_size) == FH_ERROR_OK)
{
load = fh_jpeg_load;
goto identified;
}
#endif
#ifdef FBV_SUPPORT_BMP
if(fh_bmp_id(filename))
if(fh_bmp_getsize(filename, &x_size, &y_size) == FH_ERROR_OK)
{
load = fh_bmp_load;
goto identified;
}
#endif
fprintf(stderr, "%s: Unable to access file or file format unknown.\n", filename);
return(1);
identified:
if(!(image = (unsigned char*)malloc(x_size * y_size * 3)))
{
fprintf(stderr, "%s: Out of memory.\n", filename);
goto error;
}
if(load(filename, image, &alpha, x_size, y_size) != FH_ERROR_OK)
{
fprintf(stderr, "%s: Image data is corrupt?\n", filename);
goto error;
}
if(!opt_alpha)
{
free(alpha);
alpha = NULL;
}
if(getCurrentRes(&screen_width, &screen_height))
goto error;
i.do_free = 0;
if(i.do_free)
{
free(i.rgb);
free(i.alpha);
}
i.width = x_size;
i.height = y_size;
i.rgb = image;
i.alpha = alpha;
i.do_free = 0;
if(transform_rotation)
do_rotate(&i, transform_rotation);
if(transform_stretch)
do_fit_to_screen(&i, screen_width, screen_height, transform_cal);
if(transform_enlarge)
do_enlarge(&i, screen_width, screen_height);
x_pan = y_pan = 0;
if(i.width < screen_width)
x_offs = (screen_width - i.width) / 2;
else
x_offs = 0;
if(i.height < screen_height)
y_offs = (screen_height - i.height) / 2;
else
y_offs = 0;
if(fb_display(i.rgb, i.alpha, i.width, i.height, x_pan, y_pan, x_offs, y_offs))
goto error;
if (delay)
fsleep(delay);
else
while (true) pause();
error:
free(image);
free(alpha);
if(i.do_free)
{
free(i.rgb);
free(i.alpha);
}
return ret;
}
void help(char *name)
{
printf("Usage: %s [options] image1 image2 image3 ...\n\n"
"Available options:\n"
" -h, --help Show this help\n"
" -a, --alpha Use the alpha channel (if applicable)\n"
" -f, --stretch Strech (using a simple resizing routine) the image to fit onto screen if necessary\n"
" -k, --colorstretch Strech (using a 'color average' resizing routine) the image to fit onto screen if necessary\n"
" -e, --enlarge Enlarge the image to fit the whole screen if necessary\n"
" -s <delay>, --delay <d> Display each image specified number of seconds. If not specified, display forever.\n\n"
" Copyright (C) 2000 - 2004 Mateusz Golicz, Tomasz Sterna.\n"
" Copyright (C) 2013 yanlin, godspeed1989@gitbub\n"
" Copyright (C) 2019 Anton Leontiev\n", name);
}
void sighandler(int s)
{
_exit(128 + s);
}
int main(int argc, char **argv)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"alpha", no_argument, 0, 'a'},
{"stretch", no_argument, 0, 'f'},
{"colorstrech", no_argument, 0, 'k'},
{"delay", required_argument, 0, 's'},
{"enlarge", no_argument, 0, 'e'},
{0, 0, 0, 0}
};
int c, i;
if(argc < 2)
{
help(argv[0]);
fprintf(stderr, "Error: Required argument missing.\n");
return 1;
}
while((c = getopt_long_only(argc, argv, "hcafks:e", long_options, NULL)) != EOF)
{
switch(c)
{
case 'a':
opt_alpha = 1;
break;
case 's':
opt_delay = atof(optarg);
break;
case 'h':
help(argv[0]);
return(0);
case 'f':
opt_stretch = 1;
break;
case 'k':
opt_stretch = 2;
break;
case 'e':
opt_enlarge = 1;
break;
}
}
if(!argv[optind])
{
fprintf(stderr, "Required argument missing! Consult %s -h.\n", argv[0]);
return 1;
}
signal(SIGHUP, sighandler);
signal(SIGINT, sighandler);
signal(SIGQUIT, sighandler);
signal(SIGSEGV, sighandler);
signal(SIGTERM, sighandler);
signal(SIGABRT, sighandler);
i = optind;
while(argv[i])
{
int r = show_image(argv[i]);
if(r == 0)
break;
i += r;
if(i < optind)
i = optind;
}
return 0;
}