forked from uobikiemukot/fbv-freebsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c.old
348 lines (316 loc) · 8.76 KB
/
main.c.old
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
/*
fbv -- simple image viewer for the linux framebuffer
Copyright (C) 2000, 2001, 2003 Mateusz Golicz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include "fbv.h"
#include <stdio.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
#define SHOWDELAY 100000
#define NEXT_IMG -3
#define PREV_IMG -4
extern unsigned char * simple_resize(unsigned char * orgin,int ox,int oy,int dx,int dy);
extern unsigned char * alpha_resize(unsigned char * orgin,int ox,int oy,int dx,int dy);
extern unsigned char * color_average_resize(unsigned char * orgin,int ox,int oy,int dx,int dy);
int clear=1,delay=0,hide=1,dispinfo=1,allowstrech=0,opt_alpha = 0, allow_enlarging = 0, ignore_ratio = 0;
struct formathandler *fh_root=NULL;
struct termios oldtermios;
struct termios ourtermios;
int imm_getchar(int s,int us)
{
struct timeval tv;
unsigned char c;
fd_set fds;
FD_ZERO(&fds);
FD_SET(0,&fds);
tv.tv_sec=s; tv.tv_usec=us;
if(select(1,&fds,NULL,NULL,&tv))
{
read(0,&c,1);
return((int) c);
}
else
return(EOF);
}
void contoraw(void)
{
tcgetattr(0,&oldtermios);
memcpy(&ourtermios,&oldtermios,sizeof(struct termios));
ourtermios.c_lflag&=!(ECHO|ICANON);
tcsetattr(0,TCSANOW,&ourtermios);
}
void connorm(void)
{
tcsetattr(0,TCSANOW,&oldtermios);
}
void add_format(int (*picsize)(char *,int *,int*),int (*picread)(char *,unsigned char *,unsigned char**,int,int), int (*id)(char*))
{
struct formathandler *fhn;
fhn=(struct formathandler*) malloc(sizeof(struct formathandler));
fhn->get_size=picsize; fhn->get_pic=picread; fhn->id_pic=id;
fhn->next=fh_root; fh_root=fhn;
}
#ifdef FBV_SUPPORT_GIF
extern int fh_gif_getsize(char *,int *,int*);
extern int fh_gif_load(char *,unsigned char *,unsigned char **, int,int);
extern int fh_gif_id(char *);
#endif
#ifdef FBV_SUPPORT_JPEG
extern int fh_jpeg_getsize(char *,int *,int*);
extern int fh_jpeg_load(char *,unsigned char *,unsigned char **, int,int);
extern int fh_jpeg_id(char *);
#endif
#ifdef FBV_SUPPORT_PNG
extern int fh_png_getsize(char *,int *,int*);
extern int fh_png_load(char *,unsigned char *,unsigned char **,int,int);
extern int fh_png_id(char *);
#endif
#ifdef FBV_SUPPORT_BMP
extern int fh_bmp_getsize(char *,int *,int*);
extern int fh_bmp_load(char *,unsigned char *,unsigned char **, int,int);
extern int fh_bmp_id(char *);
#endif
void init_handlers(void)
{
#ifdef FBV_SUPPORT_GIF
add_format(fh_gif_getsize,fh_gif_load,fh_gif_id);
#endif
#ifdef FBV_SUPPORT_JPEG
add_format(fh_jpeg_getsize,fh_jpeg_load,fh_jpeg_id);
#endif
#ifdef FBV_SUPPORT_PNG
add_format(fh_png_getsize,fh_png_load,fh_png_id);
#endif
#ifdef FBV_SUPPORT_BMP
add_format(fh_bmp_getsize,fh_bmp_load,fh_bmp_id);
#endif
}
struct formathandler * fh_getsize(char *name,int *x,int *y)
{
struct formathandler *fh;
for(fh=fh_root;fh!=NULL;fh=fh->next)
{
if(fh->id_pic(name))
if(fh->get_size(name,x,y)==FH_ERROR_OK) return(fh);
}
return(NULL);
}
int show_image(char *name)
{
int x,y,xs,ys,xpos,ypos,xdelta,ydelta,c,eol,xstep,ystep,rfrsh,imx,imy;
unsigned char *buffer;
unsigned char *alpha = NULL;
struct formathandler *fh;
eol=1;
if((fh=fh_getsize(name,&x,&y)))
{
buffer=(unsigned char *) malloc(x*y*3);
if(fh->get_pic(name,buffer,&alpha,x,y)==FH_ERROR_OK)
{
if(clear) { printf("\033[H\033[J"); fflush(stdout); usleep(SHOWDELAY); } /* temporary solution */
if(dispinfo) printf("%s\n%s\n%d x %d\n",IDSTRING,name,x,y);
contoraw();
getCurrentRes(&xs,&ys);
if((x>xs || y>ys) && allowstrech)
{
if(ignore_ratio)
{
if(x > xs)
imx = xs;
if(y > ys)
imy = ys;
}
else
{
if( (y*xs/x) <= ys)
{
imx=xs;
imy=y*xs/x;
}
else
{
imx=x*ys/y;
imy=ys;
}
}
if(allowstrech==1)
buffer=simple_resize(buffer,x,y,imx,imy);
else
buffer=color_average_resize(buffer,x,y,imx,imy);
if(alpha)
alpha=alpha_resize(alpha,x,y,imx,imy);
x=imx; y=imy;
}
if(((x < xs) || (y < ys)) && allow_enlarging)
{
int new_x, new_y;
if(ignore_ratio)
{
if(x < xs)
new_x = xs;
if(y < ys)
new_y = ys;
}
else
{
}
}
if(x<xs) xpos=(xs-x)/2; else xpos=0;
if(y<ys) ypos=(ys-y)/2; else ypos=0;
xdelta=0; ydelta=0;
xstep=min(max(x/20,1),xs);
ystep=min(max(y/20,1),ys);
for(eol=-1,rfrsh=1;eol==-1;)
{
if(rfrsh)
fb_display(buffer, opt_alpha ? alpha : NULL, x,y,xdelta,ydelta,xpos,ypos);
rfrsh=0;
if(!delay)
{
c=getchar();
switch(c)
{
case 'a': case 'D':
xdelta-=xstep;
if(xdelta<0) xdelta=0;
rfrsh=1;
break;
case 'd': case 'C':
if(xpos) break;
xdelta+=xstep;
if(xdelta>(x-xs)) xdelta=x-xs;
rfrsh=1;
break;
case 'w': case 'A':
ydelta-=ystep;
if(ydelta<0) ydelta=0;
rfrsh=1;
break;
case 'x': case 'B':
if(ypos) break;
ydelta+=ystep;
if(ydelta>(y-ys)) ydelta=y-ys;
rfrsh=1;
break;
case ' ': case 10: eol=1; break;
case 'r': rfrsh=1; break;
case '.': case '>': eol=NEXT_IMG; break;
case ',': case '<': case 127: case 255: eol=PREV_IMG; break;
case 'q': eol=0; break;
}
}
else
{
if(imm_getchar(delay / 10,delay % 10)=='q') eol=0; else eol=1;
break;
}
}
connorm();
if(clear) { printf("\033[0m\033[H\033[J"); fflush(stdout); }
}
else
printf("Unable to read file !\n");
free(buffer);
free(alpha);
}
else
printf("Unable to read file or file format not recognized!\n");
return(eol);
}
void help(char *name)
{
printf("Usage: %s [options] image1 image2 image3 ...\n\n"
"Available options:\n"
" --help | -h : Show this help\n"
" --alpha | -a : Use alpha channel (if applicable)\n"
" --noclear | -c : Do not clear the screen before/after displaying image\n"
" --unhide | -u : Do not hide/show the cursor before/after displaying image\n"
" --noinfo | -i : Supress image information\n"
" --stretch | -f : Strech (using a simple resizing routine) the image to fit onto screen if necessary\n"
" --colorstretch | -k : Strech (using a 'color average' resizing routine) the image to fit onto screen if necessary\n"
" --enlarge | -e : Enlarge the image to fit the whole screen if necessary\n"
" --ignore-ratio | -r : Ignore the image aspect while resizing\n"
" --delay | -s <delay> slideshow, wait 'delay' tenths of a second before displaying each image\n\n"
"Use a,d,w and x to scroll the image\n\n"
"fbv 0.99 Copyright (C) 2000 - 2003 Mateusz Golicz, Tomasz Sterna.\n", name);
}
extern int optind;
extern char *optarg;
int main(int argc,char **argv)
{
int opt,a,r;
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"noclear", no_argument, 0, 'c'},
{"alpha", no_argument, 0, 'a'},
{"unhide", no_argument, 0, 'h'},
{"noinfo", no_argument, 0, 'i'},
{"stretch", no_argument, 0, 'f'},
{"colorstrech", no_argument, 0, 'k'},
{"delay", required_argument, 0, 's'},
{"enlarge", no_argument, 0, 'e'},
{"ignore-ratio", no_argument, 0, 'r'},
{0, 0, 0, 0}
};
init_handlers();
if(argc<2)
help(argv[0]);
else
{
for(;;)
{
opt=getopt_long_only(argc,argv,"achukfis:er",long_options,NULL);
if(opt==EOF) break;
switch(opt)
{
case 'a': opt_alpha = 1; break;
case 'c': clear=0; break;
case 's': if(optarg) delay=atoi(optarg); break;
case 'u': hide=0; break;
case 'h': help(argv[0]); break;
case 'i': dispinfo=0; break;
case 'f': allowstrech=1; break;
case 'k': allowstrech=2; break;
case 'e': allow_enlarging = 1; break;
case 'r': ignore_ratio = 1; break;
}
}
if(argv[optind]==NULL) {printf("You have to specify a filename!\n"); return(1);}
while(imm_getchar(0,0)!=EOF);
if(hide) printf("\033[?25l");
for(a=optind;argv[a]!=NULL;a++)
{
r=show_image(argv[a]);
if(!r) break;
if(r==PREV_IMG)
{
if((a-1)>=optind)
a-=2;
else
a-=1;
}
}
if(hide) printf("\033[?25h");
}
return(0);
}