main.c
上传用户:hengzhunsh
上传日期:2013-09-07
资源大小:19k
文件大小:13k
- /*
- fbv -- simple image viewer for the linux framebuffer
- Copyright (C) 2000, 2001, 2003, 2004 Mateusz 'mteg' 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 <stdio.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <stdlib.h>
- #include <termios.h>
- #include <string.h>
- #include <signal.h>
- #include "config.h"
- #include "fbv.h"
- #define PAN_STEPPING 20
- static int opt_clear = 1,
- opt_alpha = 0,
- opt_hide_cursor = 1,
- opt_image_info = 1,
- opt_stretch = 0,
- opt_delay = 0,
- opt_enlarge = 0,
- opt_ignore_aspect = 0;
- void setup_console(int t)
- {
- struct termios our_termios;
- static struct termios old_termios;
- if(t)
- {
- tcgetattr(0, &old_termios);
- memcpy(&our_termios, &old_termios, sizeof(struct termios));
- our_termios.c_lflag &= !(ECHO | ICANON);
- tcsetattr(0, TCSANOW, &our_termios);
- }
- else
- tcsetattr(0, TCSANOW, &old_termios);
-
- }
- 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, int ignoreaspect)
- {
- if(((i->width > screen_width) || (i->height > screen_height)) && (!ignoreaspect))
- return;
- if((i->width < screen_width) || (i->height < screen_height))
- {
- int xsize = i->width, ysize = i->height;
- unsigned char * image, * alpha = NULL;
-
- if(ignoreaspect)
- {
- if(i->width < screen_width)
- xsize = screen_width;
- if(i->height < screen_height)
- ysize = screen_height;
-
- goto have_sizes;
- }
-
- 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 ignoreaspect, 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(ignoreaspect)
- {
- if(i->width > screen_width)
- nx_size = screen_width;
- if(i->height > screen_height)
- ny_size = screen_height;
- }
- else
- {
- 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;
- }
- }
- int show_image(char *filename)
- {
- int (*load)(char *, unsigned char *, unsigned char **, int, int);
- unsigned char * image = NULL;
- unsigned char * alpha = NULL;
-
- int x_size, y_size, screen_width, screen_height;
- int x_pan, y_pan, x_offs, y_offs, refresh = 1, c, ret = 1;
- int delay = opt_delay, retransform = 1;
-
- int transform_stretch = opt_stretch, transform_enlarge = opt_enlarge, transform_cal = (opt_stretch == 2),
- transform_iaspect = opt_ignore_aspect, transform_rotation = 0;
-
- struct image i;
-
- #ifdef FBV_SUPPORT_GIF
- if(fh_gif_id(filename))
- if(fh_gif_getsize(filename, &x_size, &y_size) == FH_ERROR_OK)
- {
- load = fh_gif_load;
- goto identified;
- }
- #endif
- #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_mem;
- }
-
- if(load(filename, image, &alpha, x_size, y_size) != FH_ERROR_OK)
- {
- fprintf(stderr, "%s: Image data is corrupt?n", filename);
- goto error_mem;
- }
-
- if(!opt_alpha)
- {
- free(alpha);
- alpha = NULL;
- }
-
-
- getCurrentRes(&screen_width, &screen_height);
- i.do_free = 0;
- while(1)
- {
- if(retransform)
- {
- 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_iaspect, transform_cal);
-
- if(transform_enlarge)
- do_enlarge(&i, screen_width, screen_height, transform_iaspect);
- x_pan = y_pan = 0;
- refresh = 1; retransform = 0;
- if(opt_clear)
- {
- printf("