#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include "fbv.h"
#include "draw_picture.h"
unsigned char * rotate(unsigned char *i, int ox, int oy, int *rot)
{
unsigned char * n, * p;
int x, y;
assert(n = (unsigned char*) malloc(ox * oy * 3));
switch(*rot)
{
case 1: /* 90 deg right */
p = n + (oy - 1) * 3;
for(y = 0; y<oy; y++, p -= 3)
{
unsigned char * r = p;
for(x = 0; x<ox; x++, r += oy * 3)
{
r[0] = *(i++);
r[1] = *(i++);
r[2] = *(i++);
}
}
break;
case 2: /* 180 deg */
i += ox * oy * 3; p = n;
for(y = ox * oy; y > 0; y--)
{
i -= 3;
p[0] = i[0];
p[1] = i[1];
p[2] = i[2];
p += 3;
}
break;
case 3: /* 90 deg left */
p = n;
for(y = 0; y<oy; y++, p += 3)
{
unsigned char * r = p + ((ox * 3) * oy);
for(x = 0; x<ox; x++)
{
r -= oy * 3;
r[0] = *(i++);
r[1] = *(i++);
r[2] = *(i++);
}
}
break;
}
return(n);
}
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->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;
}
}
}
void help(char *name)
{
printf("Usage: %s image x_offs y_offs rotate...\n\n"
"Available options:\n"
" --help | -h : Show this help\n"
" 1 : Rotate the image 90 degrees right\n"
" 2 : Rotate the image 90 degrees left\n"
" 3 : Rotate the image 180 degrees\n"
"Copyright (C) 2012,fzxy.\n", name);
}
int bmp_show(char*filename,int*x_offs,int*y_offs,int *rot)
{
int x,y,bpp;
unsigned char *alpha = NULL;
unsigned char *buff = NULL;
struct image i;
fh_bmp_getsize(filename,&x,&y);
buff = (unsigned char*)malloc(x*y*3);
fh_bmp_load(filename,buff,&alpha,x,y,&bpp);
i.width = x;
i.height = y;
i.rgb = buff;
i.alpha = alpha;
i.do_free = 0;
if(rot!=0)do_rotate(&i,rot);
bmp_display(i.rgb,i.width,i.height,x_offs,y_offs,bpp);
free(alpha);
free(buff);
return 0;
}
int jpeg_show(char*filename,int*x_offs,int*y_offs,int *rot)
{
/* int x,y;
unsigned char *alpha = NULL;
unsigned char *buff = NULL;
struct image i;
fh_jpeg_getsize(filename,&x,&y);
buff = (unsigned char*)malloc(x*y*3);
fh_jpeg_load(filename,buff,&alpha,x,y);
i.width = x;
i.height = y;
i.rgb = buff;
i.alpha = alpha;
i.do_free = 0;
if(rot!=0)do_rotate(&i,rot);
jpeg_display(i.rgb, i.alpha, i.width, i.height, x_offs,y_offs);
free(alpha);
free(buff);
*/ return 0;
}
int gif_show(char*filename,int*x_offs,int*y_offs,int *rot)
{/* int x,y;
unsigned char *alpha = NULL;
unsigned char *buff = NULL;
struct image i;
fh_gif_getsize(filename,&x,&y);
buff = (unsigned char*)malloc(x*y*3);
fh_gif_load(filename,buff,&alpha,x,y);
i.width = x;
i.height = y;
i.rgb = buff;
i.alpha = alpha;
i.do_free = 0;
if(rot!=0)do_rotate(&i,rot);
gif_display(i.rgb, i.alpha, i.width, i.height, x_offs,y_offs);
free(alpha);
free(buff);
*/ return 0;
}
int png_show(char*filename,int*offs,int*y_offs,int *rot)
{
/*int x,y;
unsigned char *alpha = NULL;
unsigned char *buff = NULL;
struct image i;
fh_png_getsize(filename,&x,&y);
buff = (unsigned char*)malloc(x*y*3);
fh_png_load(filename,buff,&alpha,x,y);
i.width = x;
i.height = y;
i.rgb = buff;
i.alpha = alpha;
i.do_free = 0;
if(rot!=0)do_rotate(&i,rot);
png_display(i.rgb, i.alpha, i.width, i.height,x_offs,y_offs);
free(alpha);
free(buff);*/
return 0;
}
int read_header(char*filename)
{
int fd;
unsigned char bmp_id[2],gif_id[4],png_id[4],jpeg_id[10];
fd=open(filename,O_RDONLY);
if(fd==-1) return(0);
read(fd,bmp_id,2);
lseek(fd,0,SEEK_SET);
read(fd,gif_id,4);
lseek(fd,0,SEEK_SET);
read(fd,png_id,4);
lseek(fd,0,SEEK_SET);
read(fd,jpeg_id,10);
close(fd);
if ( bmp_id[0]=='B' && bmp_id[1]=='M' ){ printf("I'm BMP picture!\n");return 1;}
else if(gif_id[0]=='G' && gif_id[1]=='I' && gif_id[2]=='F'){ printf("I'm GIF picture!\n");return 2;}
else if(png_id[1]=='P' && png_id[2]=='N' && png_id[3]=='G'){ printf("I'm PNG picture!\n");return 3;}
else
{
if(jpeg_id[6]=='J' && jpeg_id[7]=='F' && jpeg_id[8]=='I' && jpeg_id[9]=='F'){ printf("I'm JPEG picture!\n");return 4;}
else if(jpeg_id[0]==0xff && jpeg_id[1]==0xd8 && jpeg_id[2]==0xff){ printf("I'm JPEG picture!\n");return 4;}
}
return 0;
}
int show_pic(char*filename,int*x_offs,int*y_offs,int *rot )
{
int type = read_header(filename);
switch(type)
{
case 1:bmp_show(filename,x_offs,y_offs,rot);break;
case 2:gif_show(filename,x_offs,y_offs,rot);break;
case 3:png_show(filename,x_offs,y_offs,rot);break;
case 4:jpeg_show(filename,x_offs,y_offs,rot);break;
default:
printf("Cannot identify the type of the picture!\nPlease check your pictures����\n");
break;
}
return 0;
}
- 1
- 2
- 3
- 4
前往页